1<?php
2/**
3 * Copyright 2013-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 2013-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/gpl GPL
11 * @package   IMP
12 */
13
14/**
15 * Defines AJAX actions used for remote server access.
16 *
17 * @author    Michael Slusarz <slusarz@horde.org>
18 * @category  Horde
19 * @copyright 2013-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/gpl GPL
21 * @package   IMP
22 */
23class IMP_Ajax_Application_Handler_Remote extends Horde_Core_Ajax_Application_Handler
24{
25    /**
26     * AJAX action: Login to a remote account.
27     *
28     * Variables used:
29     *   - password: (string) Remote server password.
30     *   - password_base64: (boolean) If true, password is base64 encoded.
31     *   - remoteid: (string) Remote server ID (base64url encoded).
32     *   - unsub: (boolean) If true, show unsubscribed mailboxes.
33     *
34     * @return boolean  An object with the following properties:
35     *   - success: (boolean) True if login was successful.
36     */
37    public function remoteLogin()
38    {
39        global $injector, $notification, $prefs;
40
41        $remote = $injector->getInstance('IMP_Remote');
42        $remoteid = IMP_Mailbox::formFrom($this->vars->remoteid);
43
44        $res = new stdClass;
45        $res->success = false;
46
47        if (!isset($remote[$remoteid])) {
48            $notification->push(_("Could not find remote server configuration."), 'horde.error');
49            return $res;
50        }
51
52        $password = $this->vars->password;
53        if ($this->vars->password_base64) {
54            $password = base64_decode($password);
55        }
56
57        $remote_ob = $remote[$remoteid];
58
59        try {
60            $remote_ob->createImapObject($password);
61            $remote_ob->imp_imap->login();
62            $res->success = true;
63            $notification->push(sprintf(_("Successfully authenticated to %s."), $remote_ob->label), 'horde.success');
64
65            $ftree = $injector->getInstance('IMP_Ftree');
66
67            $ftree->delete($remote_ob);
68            $ftree->insert($remote_ob);
69
70            $ftree[$remote_ob]->open = true;
71            $this->_base->queue->setMailboxOpt('expand', 1);
72
73            $iterator = new IMP_Ftree_IteratorFilter(
74                new IMP_Ftree_Iterator($ftree[$remote_ob])
75            );
76            if ($this->vars->unsub) {
77                $ftree->loadUnsubscribed();
78                $iterator->remove($iterator::UNSUB);
79            }
80
81            switch ($prefs->getValue('nav_expanded')) {
82            case IMP_Ftree_Prefs_Expanded::NO:
83                $iterator->add($iterator::CHILDREN);
84                break;
85
86            case IMP_Ftree_Prefs_Expanded::LAST:
87                $iterator->add($iterator::EXPANDED);
88                break;
89            }
90
91            array_map(
92                array($ftree->eltdiff, 'add'),
93                iterator_to_array($iterator, false)
94            );
95        } catch (Exception $e) {
96            $notification->push(sprintf(_("Could not authenticate to %s."), $remote_ob->label), 'horde.error');
97        }
98
99        return $res;
100    }
101
102    /**
103     * AJAX action: Logout from a remote account.
104     *
105     * Variables used:
106     *   - remoteid: (string) Remote server ID (base64url encoded).
107     *
108     * @return boolean  True.
109     */
110    public function remoteLogout()
111    {
112        global $injector, $notification;
113
114        $remote = $injector->getInstance('IMP_Remote');
115        $remoteid = IMP_Mailbox::formFrom($this->vars->remoteid);
116        $remote_ob = $remote[$remoteid];
117
118        $injector->getInstance('IMP_Factory_Imap')->destroy($remoteid);
119
120        $ftree = $injector->getInstance('IMP_Ftree');
121        $ftree->delete($remote_ob);
122        $ftree->insert($remote_ob);
123
124        $notification->push(sprintf(_("Logged out of %s."), $remote_ob->label), 'horde.success');
125
126        return true;
127    }
128
129}
130