From 12df24e67559c6ab3a5e175d614b1b40bb21b239 Mon Sep 17 00:00:00 2001 From: Boris Gulay Date: Thu, 12 May 2016 22:19:01 +0300 Subject: [PATCH] Yess!! Switching finally works!!! --- ident_switch.js | 1 + ident_switch.php | 52 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/ident_switch.js b/ident_switch.js index 8620e89..949e0b9 100644 --- a/ident_switch.js +++ b/ident_switch.js @@ -10,5 +10,6 @@ $(function() { }); function plugin_switchIdent_switch(val) { + console.log("qwe1"); rcmail.http_post('plugin.ident_switch.switch', { '_ident-id': val }); } diff --git a/ident_switch.php b/ident_switch.php index 58f7fb9..c2d023c 100644 --- a/ident_switch.php +++ b/ident_switch.php @@ -201,16 +201,31 @@ class ident_switch extends rcube_plugin { $rc = rcmail::get_instance(); + $my_postfix = '_iswitch'; + $my_postfix_len = strlen($my_postfix); + + error_log('Switcing account!'); // TODO: Add nornal logging here! + $identId = rcube_utils::get_input_value('_ident-id', RCUBE_INPUT_POST); error_log($identId); if (-1 == $identId) { // Switch to main account - // TODO: Erite code! + foreach ($_SESSION as $k => $v) + { + if (substr($k, 0, 7) == 'STORAGE' && substr($k, -$my_postfix_len) == $my_postfix) + { + error_log($k . '=>' . $v); + $_SESSION[$k] = $_SESSION[$k . $my_postfix]; + $rc->session->remove($k . $my_postfix); + } + } + $_SESSION['username'] = $rc->user->data['username']; + $_SESSION['password'] = $_SESSION['password' . $my_postfix]; } else { - $sql = 'SELECT server, username, password FROM ' . $rc->db->table_name($this->table) . ' WHERE id = ? AND user_id = ?'; + $sql = 'SELECT host, flags, port, username, password FROM ' . $rc->db->table_name($this->table) . ' WHERE id = ? AND user_id = ?'; $q = $rc->db->query($sql, $identId ,$rc->user->ID); $r = $rc->db->fetch_assoc($q); if (is_array($r)) @@ -226,25 +241,44 @@ class ident_switch extends rcube_plugin if (!$port) $port = 143; // Default port here! + // If we are in default account now + // save everything with STORAGE + if ($_SESSION['username'] == $rc->user->data['username']) + { + foreach ($_SESSION as $k => $v) + { + if (substr($k, 0, 7) == 'STORAGE' && substr($k, -$my_postfix_len) != $my_postfix) + { + error_log($k . '=>' . $v); + $_SESSION[$k . $my_postfix] = $_SESSION[$k]; + $rc->session->remove($k); + } + } + } + $_SESSION['password' . $my_postfix] = $_SESSION['password']; + error_log('Pwd: ' . $_SESSION['password']); + $_SESSION['storage_host'] = $r['host']; $_SESSION['storage_ssl'] = $ssl; $_SESSION['storage_port'] = $port; $_SESSION['username'] = $r['username']; - $_SESSION['password'] = $rc->decrypt($r['password']); + $_SESSION['password'] = $r['password']; $rc->session->remove('folders'); - $rc->output->redirect( - array( - '_task' => 'mail', - '_mbox' => rcube_utils::get_input_value('_mbox', RCUBE_INPUT_GET), - ) - ); } else { // TODO: Show message in browser error_log('Requested account not found!'); + return; } } + + $rc->output->redirect( + array( + '_task' => 'mail', + '_mbox' => rcube_utils::get_input_value('_mbox', RCUBE_INPUT_GET), + ) + ); } }