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