/**
* AJAX action: Login to a remote account.
*
* Variables used:
* - password: (string) Remote server password.
* - password_base64: (boolean) If true, password is base64 encoded.
* - password_save: (boolean) If true, password is saved (encrypted)
* to config data.
* - remoteid: (string) Remote server ID (base64url encoded).
* - unsub: (boolean) If true, show unsubscribed mailboxes.
*
* @return boolean An object with the following properties:
* - success: (boolean) True if login was successful.
*/
public function remoteLogin()
{
global $injector, $notification, $prefs;
$remote = $injector->getInstance('IMP_Remote');
$remoteid = IMP_Mailbox::formFrom($this->vars->remoteid);
$res = new stdClass();
$res->success = false;
if (!isset($remote[$remoteid])) {
$notification->push(_("Could not find remote server configuration."), 'horde.error');
return $res;
}
$password = $this->vars->password;
if ($this->vars->password_base64) {
$password = base64_decode($password);
}
$remote_ob = $remote[$remoteid];
try {
switch ($remote_ob->login($password, $this->vars->password_save)) {
case $remote_ob::LOGIN_BAD_CHANGED:
$remote[$remoteid] = $remote_ob;
// Fall-through
// Fall-through
case $remote_ob::LOGIN_BAD:
throw new Exception();
case $remote_ob::LOGIN_OK_CHANGED:
$remote[$remoteid] = $remote_ob;
break;
}
$res->success = true;
$notification->push(sprintf(_("Successfully authenticated to %s."), $remote_ob->label), 'horde.success');
$ftree = $injector->getInstance('IMP_Ftree');
$ftree->delete($remote_ob);
$ftree->insert($remote_ob);
$ftree[$remote_ob]->open = true;
$this->_base->queue->setMailboxOpt('expand', 1);
$iterator = new IMP_Ftree_IteratorFilter(new IMP_Ftree_Iterator($ftree[$remote_ob]));
if ($this->vars->unsub) {
$ftree->loadUnsubscribed();
$iterator->remove($iterator::UNSUB);
}
switch ($prefs->getValue('nav_expanded')) {
case IMP_Ftree_Prefs_Expanded::NO:
$iterator->add($iterator::CHILDREN);
break;
case IMP_Ftree_Prefs_Expanded::LAST:
$iterator->add($iterator::EXPANDED);
break;
}
array_map(array($ftree->eltdiff, 'add'), iterator_to_array($iterator, false));
} catch (Exception $e) {
$notification->push(sprintf(_("Could not authenticate to %s."), $remote_ob->label), 'horde.error');
}
return $res;
}