-
Notifications
You must be signed in to change notification settings - Fork 0
/
hybrid_cluster.php
107 lines (86 loc) · 3.71 KB
/
hybrid_cluster.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Hybrid Cluster Roundcube Plugin
*
* Provides Email time machine support
*
* XXX Because of lack of documentation in RCB this reaches up into RCB
* internals a bit. May require more careful construction at a later date.
*
* @version 1.0
* @author Rob Haswell
* @url http://www.hybrid-cluster.com/
*/
class hybrid_cluster extends rcube_plugin {
public $task = "mail";
public function init() {
require_once "{$_SERVER['DOCUMENT_ROOT']}/include/HybridClusterAPI.class.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/include/HybridClusterAPIInternalException.class.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/include/jsonRPCClient.class.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/include/Spyc.class.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/include/Site.class.php";
$this->include_script('client.js');
$this->add_hook('template_container', Array($this, 'template_container'));
$this->register_action('plugin.hc_select_snapshot', array($this, 'select_snapshot'));
}
private function getUsername() {
$rcmail = rcmail::get_instance();
$user = $rcmail->user;
list($username) = explode("#", $user->data['username']);
return $username;
}
private function getSnapshot() {
$rcmail = rcmail::get_instance();
$user = $rcmail->user;
list($username, $snapshot) = explode("#", $user->data['username']);
return (string)$snapshot;
}
public function template_container($params) {
if (!isset($params['content']))
$params['content'] = "";
ob_start();
switch ($params['name']) {
case 'taskbar':
$rcmail = rcmail::get_instance();
$username = $this->getUsername();
$cur_snapshot = $this->getSnapshot();
$api = HybridClusterAPI::get();
$snapshots = $api->availableSnapshotsForEmail($username);
$snapshots = array_reverse($snapshots);
$input = new html_select(Array("id" => "hc-snapshot"));
$input->add('Current', '');
foreach ($snapshots as $snapshot)
$input->add(date('jS M \'y &\\n\d\a\s\h; H:i', $snapshot['timestamp']), $snapshot['name']);
?>
<div style="position: absolute; top: 7px; right: 380px">
<label>Select snapshot:
<?=$input->show($cur_snapshot); ?>
</label>
</div>
<?php
break;
}
return Array("content" => $params['content'] . ob_get_clean());
}
public function select_snapshot($args) {
$rcmail = rcmail::get_instance();
$username = $this->getUsername();
$snapshot = $_POST['snapshot'];
$db = $rcmail->get_dbh();
$res = $db->query("select password from mailboxes where username=?", $username);
$row = $db->fetch_assoc($res);
$userdata = array('user' => $_SESSION['username'], 'host' => $_SESSION['imap_host'], 'lang' => $RCMAIL->user->language);
$rcmail->logout_actions();
$rcmail->kill_session();
$rcmail->plugins->exec_hook('logout_after', $userdata);
//session_destroy();
$rcmail->session->remove('temp');
$rcmail->session->regenerate_id(false);
$rcmail->session_init();
$new_username = $snapshot ? "{$username}#{$snapshot}" : $username;
$rcmail->login($new_username, $row['password']);
// send auth cookie if necessary
$rcmail->session->set_auth_cookie();
$rcmail->output->command('plugin.hc_select_snapshot_callback', array('message' => 'Switching to snapshot'));
}
}