1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class IMP_Basic_Pgp extends IMP_Basic_Base
24: {
25: 26: 27:
28: protected $_pgp;
29:
30: 31:
32: protected function _init()
33: {
34: global $browser, $injector, $notification;
35:
36: $this->_pgp = $injector->getInstance('IMP_Crypt_Pgp');
37:
38:
39: switch ($this->vars->actionID) {
40: case 'import_public_key':
41: $this->_importKeyDialog('process_import_public_key');
42: break;
43:
44: case 'process_import_public_key':
45: $import_keys = $this->_pgp->getKeys($this->vars->import_key);
46: try {
47: $browser->wasFileUploaded('upload_key', _("key"));
48: $import_keys = array_merge_recursive($import_keys, $this->_pgp->getKeys(file_get_contents($_FILES['upload_key']['tmp_name'])));
49: } catch (Horde_Browser_Exception $e) {}
50:
51: if (count($import_keys['public'])) {
52: foreach ($import_keys['public'] as $val) {
53: $key_info = $this->_pgp->addPublicKey($val);
54: foreach ($key_info['signature'] as $sig) {
55: $notification->push(sprintf(_("PGP Public Key for \"%s (%s)\" was successfully added."), $sig['name'], $sig['email']), 'horde.success');
56: }
57: }
58: $this->_reloadWindow();
59: }
60:
61: $notification->push(_("No PGP public key imported."), 'horde.error');
62: $this->vars->actionID = 'import_public_key';
63: $this->_importKeyDialog('process_import_public_key');
64: break;
65:
66: case 'import_personal_key':
67: $this->_importKeyDialog('process_import_personal_key');
68: break;
69:
70: case 'process_import_personal_key':
71: $import_key = $this->_pgp->getKeys($this->vars->import_key);
72:
73: if (empty($import_key['public']) || empty($import_key['private'])) {
74: try {
75: $browser->wasFileUploaded('upload_key', _("key"));
76: $import_key = array_merge_recursive($import_key, $this->_pgp->getKeys(file_get_contents($_FILES['upload_key']['tmp_name'])));
77: } catch (Horde_Browser_Exception $e) {
78: if ($e->getCode() != UPLOAD_ERR_NO_FILE) {
79: $notification->push($e, 'horde.error');
80: }
81: }
82: }
83:
84: if (!empty($import_key['public']) &&
85: !empty($import_key['private'])) {
86: $this->_pgp->addPersonalPublicKey($import_key['public'][0]);
87: $this->_pgp->addPersonalPrivateKey($import_key['private'][0]);
88: $notification->push(_("Personal PGP key successfully added."), 'horde.success');
89: $this->_reloadWindow();
90: }
91:
92: $notification->push(_("Personal PGP key not imported."), 'horde.error');
93:
94: $this->vars->actionID = 'import_personal_key';
95: $this->_importKeyDialog('process_import_personal_key');
96: break;
97:
98: case 'view_public_key':
99: case 'info_public_key':
100: try {
101: $key = $this->_pgp->getPublicKey($this->vars->email, array('noserver' => true));
102: } catch (Horde_Exception $e) {
103: $key = $e->getMessage();
104: }
105:
106: if ($this->vars->actionID == 'view_public_key') {
107: $this->_textWindowOutput('PGP Public Key', $key);
108: }
109: $this->_printKeyInfo($key);
110: break;
111:
112: case 'view_personal_public_key':
113: $this->_textWindowOutput('PGP Personal Public Key', $this->_pgp->getPersonalPublicKey());
114: break;
115:
116: case 'info_personal_public_key':
117: $this->_printKeyInfo($this->_pgp->getPersonalPublicKey());
118: break;
119:
120: case 'view_personal_private_key':
121: $this->_textWindowOutput('PGP Personal Private Key', $this->_pgp->getPersonalPrivateKey());
122: break;
123:
124: case 'info_personal_private_key':
125: $this->_printKeyInfo($this->_pgp->getPersonalPrivateKey());
126: break;
127: }
128: }
129:
130: 131:
132: public static function url(array $opts = array())
133: {
134: return Horde::url('basic.php')->add('page', 'pgp');
135: }
136:
137: 138: 139: 140: 141:
142: protected function _importKeyDialog($target)
143: {
144: global $notification, $page_output, $registry;
145:
146: $page_output->topbar = $page_output->sidebar = false;
147: $page_output->addInlineScript(array(
148: '$$("INPUT.horde-cancel").first().observe("click", function() { window.close(); })'
149: ), true);
150:
151: $this->title = _("Import PGP Key");
152:
153: 154:
155: if ($registry->getView() == Horde_Registry::VIEW_DYNAMIC) {
156: $notification->detach('status');
157: $notification->attach('status');
158: }
159:
160: $view = new Horde_View(array(
161: 'templatePath' => IMP_TEMPLATES . '/pgp'
162: ));
163: $view->addHelper('Text');
164:
165: $view->reload = $this->vars->reload;
166: $view->selfurl = self::url();
167: $view->target = $target;
168:
169: $this->output = $view->render('import_key');
170: }
171:
172: 173: 174:
175: protected function _reloadWindow()
176: {
177: echo Horde::wrapInlineScript(array(
178: 'opener.focus();'.
179: 'opener.location.href="' . base64_decode($this->vars->reload) . '";',
180: 'window.close();'
181: ));
182: exit;
183: }
184:
185: 186: 187: 188: 189:
190: protected function _printKeyInfo($key = '')
191: {
192: try {
193: $key_info = $this->_pgp->pgpPrettyKey($key);
194: } catch (Horde_Crypt_Exception $e) {
195: Horde::log($e, 'INFO');
196: $key_info = $e->getMessage();
197: }
198:
199: $this->_textWindowOutput('PGP Key Information', $key_info);
200: }
201:
202: 203: 204: 205: 206: 207:
208: protected function _textWindowOutput($name, $msg)
209: {
210: $GLOBALS['browser']->downloadHeaders($name, 'text/plain; charset=' . 'UTF-8', true, strlen($msg));
211: echo $msg;
212: exit;
213: }
214:
215: }
216: