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_Prefs_Special_PgpPrivateKey implements Horde_Core_Prefs_Ui_Special
24: {
25: 26:
27: public function init(Horde_Core_Prefs_Ui $ui)
28: {
29: }
30:
31: 32:
33: public function display(Horde_Core_Prefs_Ui $ui)
34: {
35: global $conf, $injector, $page_output, $prefs, $session;
36:
37: $page_output->addScriptPackage('IMP_Script_Package_Imp');
38:
39: $view = new Horde_View(array(
40: 'templatePath' => IMP_TEMPLATES . '/prefs'
41: ));
42: $view->addHelper('Horde_Core_View_Helper_Help');
43:
44: if (!Horde::isConnectionSecure()) {
45: $view->notsecure = true;
46: } else {
47: $pgp_url = IMP_Basic_Pgp::url();
48:
49: $view->has_key = ($prefs->getValue('pgp_public_key') && $prefs->getValue('pgp_private_key'));
50: if ($view->has_key) {
51: $view->viewpublic = Horde::link($pgp_url->copy()->add('actionID', 'view_personal_public_key'), _("View Personal Public Key"), null, 'view_key');
52: $view->infopublic = Horde::link($pgp_url->copy()->add('actionID', 'info_personal_public_key'), _("Information on Personal Public Key"), null, 'info_key');
53: $view->sendkey = Horde::link($ui->selfUrl(array(
54: 'special' => true,
55: 'token' => true
56: ))->add('send_pgp_key', 1), _("Send Key to Public Keyserver"));
57:
58: if ($injector->getInstance('IMP_Crypt_Pgp')->getPassphrase('personal')) {
59: $view->passphrase = Horde::link($ui->selfUrl(array(
60: 'special' => true,
61: 'token' => true
62: ))->add('unset_pgp_passphrase', 1), _("Unload Passphrase")) . _("Unload Passphrase");
63: } else {
64: $imple = $injector->getInstance('Horde_Core_Factory_Imple')->create('IMP_Ajax_Imple_PassphraseDialog', array(
65: 'params' => array(
66: 'reload' => $ui->selfUrl()->setRaw(true)
67: ),
68: 'type' => 'pgpPersonal'
69: ));
70: $view->passphrase = Horde::link('#', _("Enter Passphrase"), null, null, null, null, null, array('id' => $imple->getDomId())) . _("Enter Passphrase");
71: }
72:
73: $view->viewprivate = Horde::link($pgp_url->copy()->add('actionID', 'view_personal_private_key'), _("View Personal Private Key"), null, 'view_key');
74: $view->infoprivate = Horde::link($pgp_url->copy()->add('actionID', 'info_personal_private_key'), _("Information on Personal Private Key"), null, 'info_key');
75: $page_output->addInlineScript(array(
76: '$("delete_pgp_privkey").observe("click", function(e) { if (!window.confirm(' . json_encode(_("Are you sure you want to delete your keypair? (This is NOT recommended!)")) . ')) { e.stop(); } })'
77: ), true);
78: } else {
79: $page_output->addScriptFile('pgp.js');
80: Horde_Core_Ui_JsCalendar::init();
81: $page_output->addInlineJsVars(array(
82: 'ImpPgp.months' => Horde_Core_Ui_JsCalendar::months()
83: ));
84:
85: $imp_identity = $injector->getInstance('IMP_Identity');
86: $view->fullname = $imp_identity->getFullname();
87: $view->fromaddr = $imp_identity->getFromAddress()->bare_address;
88:
89: if (!empty($conf['pgp']['keylength'])) {
90: $page_output->addInlineScript(array(
91: '$("create_pgp_key").observe("click", function(e) { if (!window.confirm(' . json_encode(_("Key generation may take a long time to complete. Continue with key generation?")) . ')) { e.stop(); } })'
92: ), true);
93: }
94:
95: if ($session->get('imp', 'file_upload')) {
96: $view->import_pgp_private = true;
97: $page_output->addInlineScript(array(
98: '$("import_pgp_personal").observe("click", function(e) { ' . Horde::popupJs($pgp_url, array('params' => array('actionID' => 'import_personal_key', 'reload' => base64_encode($ui->selfUrl()->setRaw(true))), 'height' => 275, 'width' => 750, 'urlencode' => true)) . '; e.stop(); })'
99: ), true);
100: }
101: }
102: }
103:
104: return $view->render('pgpprivatekey');
105: }
106:
107: 108:
109: public function update(Horde_Core_Prefs_Ui $ui)
110: {
111: global $conf, $injector, $notification;
112:
113: $imp_pgp = $injector->getInstance('IMP_Crypt_Pgp');
114:
115: if (isset($ui->vars->delete_pgp_privkey)) {
116: $imp_pgp->deletePersonalKeys();
117: $notification->push(_("Personal PGP keys deleted successfully."), 'horde.success');
118: } elseif (isset($ui->vars->create_pgp_key) &&
119: !empty($conf['pgp']['keylength'])) {
120:
121: try {
122: $email = IMP::parseAddressList($ui->vars->generate_email, array(
123: 'validate' => true
124: ));
125: } catch (Horde_Mail_Exception $e) {
126: $notification->push($e);
127: return false;
128: }
129:
130: 131:
132: if (empty($ui->vars->generate_realname) || empty($email)) {
133: $notification->push(_("Name and/or email cannot be empty"), 'horde.error');
134: } elseif (empty($ui->vars->generate_passphrase1) ||
135: empty($ui->vars->generate_passphrase2)) {
136: $notification->push(_("Passphrases cannot be empty"), 'horde.error');
137: } elseif ($ui->vars->generate_passphrase1 !== $ui->vars->generate_passphrase2) {
138: $notification->push(_("Passphrases do not match"), 'horde.error');
139: } else {
140: 141:
142: $expire_date = $ui->vars->generate_expire
143: ? null
144: : ($ui->vars->generate_expire_date / 1000);
145:
146: try {
147: $imp_pgp->generatePersonalKeys(
148: $ui->vars->generate_realname,
149: $email[0]->bare_address_idn,
150: $ui->vars->generate_passphrase1,
151: $ui->vars->generate_comment,
152: $conf['pgp']['keylength'],
153: $expire_date
154: );
155: $notification->push(_("Personal PGP keypair generated successfully."), 'horde.success');
156: } catch (Exception $e) {
157: $notification->push($e);
158: }
159: }
160: } elseif (isset($ui->vars->send_pgp_key)) {
161: try {
162: $imp_pgp->sendToPublicKeyserver($imp_pgp->getPersonalPublicKey());
163: $notification->push(_("Key successfully sent to the public keyserver."), 'horde.success');
164: } catch (Exception $e) {
165: $notification->push($e);
166: }
167: } elseif (isset($ui->vars->unset_pgp_passphrase)) {
168: $imp_pgp->unsetPassphrase('personal');
169: $notification->push(_("PGP passphrase successfully unloaded."), 'horde.success');
170: }
171:
172: return false;
173: }
174:
175: }
176: