1<?php
2/**
3 * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (GPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/gpl.
7 *
8 * @category  Horde
9 * @copyright 2012-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/gpl GPL
11 * @package   IMP
12 */
13
14/**
15 * Defines AJAX actions used in the IMP passphrase dialog.
16 *
17 * @author    Michael Slusarz <slusarz@horde.org>
18 * @category  Horde
19 * @copyright 2012-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/gpl GPL
21 * @package   IMP
22 */
23class IMP_Ajax_Application_Handler_Passphrase extends Horde_Core_Ajax_Application_Handler
24{
25    /**
26     * AJAX action: Check passphrase.
27     *
28     * Variables required in form input:
29     *   - dialog_input: (string) Input from the dialog screen.
30     *   - reload: (mixed) If set, reloads page instead of returning data.
31     *   - symmetricid: (string) The symmetric ID to process.
32     *   - type: (string) The passphrase type.
33     *
34     * @return boolean  True on success.
35     */
36    public function checkPassphrase()
37    {
38        global $injector, $notification;
39
40        $result = false;
41
42        try {
43            Horde::requireSecureConnection();
44
45            switch ($this->vars->type) {
46            case 'pgpPersonal':
47            case 'pgpSymmetric':
48                if ($this->vars->dialog_input) {
49                    $imp_pgp = $injector->getInstance('IMP_Crypt_Pgp');
50                    if ((($this->vars->type == 'pgpPersonal') &&
51                         $imp_pgp->storePassphrase('personal', $this->vars->dialog_input)) ||
52                        (($this->vars->type == 'pgpSymmetric') &&
53                         $imp_pgp->storePassphrase('symmetric', $this->vars->dialog_input, $this->vars->symmetricid))) {
54                        $result = true;
55                        $notification->push(_("PGP passhprase stored in session."), 'horde.success');
56                    } else {
57                        $notification->push(_("Invalid passphrase entered."), 'horde.error');
58                    }
59                } else {
60                    $notification->push(_("No passphrase entered."), 'horde.error');
61                }
62                break;
63
64            case 'smimePersonal':
65                if ($this->vars->dialog_input) {
66                    $imp_smime = $injector->getInstance('IMP_Crypt_Smime');
67                    if ($imp_smime->storePassphrase($this->vars->dialog_input)) {
68                        $result = true;
69                        $notification->push(_("S/MIME passphrase stored in session."), 'horde.success');
70                    } else {
71                        $notification->push(_("Invalid passphrase entered."), 'horde.error');
72                    }
73                } else {
74                    $notification->push(_("No passphrase entered."), 'horde.error');
75                }
76                break;
77            }
78        } catch (Horde_Exception $e) {
79            $notification->push($e, 'horde.error');
80        }
81
82        return ($result && $this->vars->reload)
83            ? new Horde_Core_Ajax_Response_HordeCore_Reload($this->vars->reload)
84            : $result;
85    }
86
87}
88