/
agendav.php
194 lines (159 loc) · 7.06 KB
/
agendav.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Roundcube AgenDAV Plugin
*
* @author Stephan Blanke
* @license GNU GPLv3+
*
* Configuration (see config.inc.php.dist)
*
**/
require_once('Encrypt.php');
function log_message() {};
class agendav extends rcube_plugin
{
// all tasks
public $task = '.*';
// we've got no ajax handlers
public $noajax = true;
// skip frames
public $noframe = true;
function init()
{
$this->load_config();
$this->add_texts('localization/', false);
// register task
$this->register_task('agendav');
// register actions
$this->register_action('index', array($this, 'action'));
$this->register_action('prefs', array($this, 'action'));
$this->add_hook('startup', array($this, 'startup'));
$this->add_hook('login_after', array($this, 'login_after'));
$this->add_hook('session_destroy', array($this, 'logout'));
}
function get_db_driver()
{
$rcmail = rcmail::get_instance();
// try to translate CI db driver name to proper PDO driver name
switch($rcmail->config->get('agendav_dbtype', false)) {
case 'mysqli':
return 'mysql';
case 'oci8':
return 'oci';
case 'postgre':
return 'pgsql';
default:
return $rcmail->config->get('agendav_dbtype', false);
}
}
function login_after($args)
{
$rcmail = rcmail::get_instance();
$dbh = new PDO($this->get_db_driver().':dbname='.$rcmail->config->get('agendav_dbname', false).';host='.$rcmail->config->get('agendav_dbhost', false), $rcmail->config->get('agendav_dbuser', false), $rcmail->config->get('agendav_dbpass', false));
$stmt = $dbh->prepare('insert into '.$rcmail->config->get('agendav_dbprefix', false).'sessions(session_id, ip_address, user_agent,last_activity,user_data) values (:id, :ip, :user_agent, :last_activity, :user_data)');
$stmt->bindParam(':id', $guid);
$stmt->bindParam(':ip', $ip);
$stmt->bindParam(':user_agent', $user_agent);
$stmt->bindParam(':last_activity', $last_activity);
$stmt->bindParam(':user_data', $user_data);
// encrypt password
$encrypt = new CI_Encrypt();
$encrypt->set_key(md5($rcmail->config->get('agendav_encryption_key', false)));
// create all necessary infos for the agendav session line
$password = $encrypt->encode($rcmail->get_user_password());
$username = $rcmail->get_user_name();
$guid = sprintf('%04x%04x%04x%04x%04x%04x%04x%04x', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
$ip = rcube_utils::remote_addr();
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$last_activity = time();
// read existing preferences array
$pref_stmt = $dbh->prepare('select options from '.$rcmail->config->get('agendav_dbprefix', false).'prefs where username=:username');
$pref_stmt->bindParam(':username', $rcmail->get_user_name());
$pref_stmt->execute();
$prefs = $pref_stmt->fetch(PDO::FETCH_ASSOC);
$options = serialize(json_decode($prefs['options'],true));
$options = ($options == "N;")?"a:0:{}":$options;
// need to replace 'null' with an empty array, otherwise agendav fails to load calendars if user prefs are empty
$user_data = 'a:4:{s:4:"user";s:'.strlen($username).':"'.$username.'";s:6:"passwd";s:'.strlen($password).':"'.$password.'";s:5:"prefs";'.$options.'s:19:"available_calendars";a:0:{}}';
// create session in agendav
$stmt->execute();
// destroy database connection
$dbh = null;
// create cookie containing the agendav session_id
setcookie('agendav_sessid', $guid, 0);
// save agendav session_id in the session, so it can be used on during roundcube logoff to kill the agendav session
$_SESSION['agendav_sessid'] = $guid;
}
function logout($args)
{
$rcmail = rcmail::get_instance();
$dbh = new PDO($this->get_db_driver().':dbname='.$rcmail->config->get('agendav_dbname', false).';host='.$rcmail->config->get('agendav_dbhost', false), $rcmail->config->get('agendav_dbuser', false), $rcmail->config->get('agendav_dbpass', false));
$stmt = $dbh->prepare("delete from sessions where session_id=:id");
$stmt->bindParam(':id', $_SESSION['agendav_sessid']);
$stmt->execute();
setcookie('agendav_sessid', '', time() - 3600);
}
function startup($args)
{
$rcmail = rcmail::get_instance();
// add taskbar button
$this->add_button(array(
'command' => 'agendav',
'class' => 'button-agendav',
'classsel' => 'button-agendav button-selected',
'innerclass' => 'button-inner',
'label' => 'agendav.agendav',
), 'taskbar');
// add style for taskbar button (must be here) and AgenDAV UI
$skin_path = $this->local_skin_path();
if (is_file($this->home . "/$skin_path/agendav.css")) {
$this->include_stylesheet("$skin_path/agendav.css");
}
}
function action()
{
$rcmail = rcmail::get_instance();
// register UI objects
$rcmail->output->add_handlers(array(
'agendavcontent' => array($this, 'content'),
'tablink' => array($this, 'tablink'),
));
if ($rcmail->action == 'prefs')
$rcmail->output->set_pagetitle($this->gettext('agendav').' :: '.$this->gettext('prefs'));
else
$rcmail->output->set_pagetitle($this->gettext('agendav'));
$rcmail->output->send('agendav.agendav');
}
function tablink($attrib)
{
$rcmail = rcmail::get_instance();
$attrib['name'] = 'agendavlink' . $attrib['action'];
$attrib['href'] = $rcmail->url(array('_action' => $attrib['action'], '_extwin' => !empty($_REQUEST['_extwin']) ? 1 : null));
return $rcmail->output->button($attrib);
}
function content($attrib)
{
$rcmail = rcmail::get_instance();
switch ($rcmail->action) {
case 'prefs':
$src = $this->api->url.'agendav/'.$rcmail->config->get('agendav_path', false).'/web/public/index.php/prefs';
break;
default:
$src = $this->api->url.'agendav/'.$rcmail->config->get('agendav_path', false).'/web/public/index.php/main';
break;
}
$attrib['src'] = $this->resolve_language($src);
if (empty($attrib['id']))
$attrib['id'] = 'rcmailagendavcontent';
$attrib['name'] = $attrib['id'];
return $rcmail->output->frame($attrib);
}
private function resolve_language($path)
{
// resolve language placeholder
$rcmail = rcmail::get_instance();
$langmap = $rcmail->config->get('agendav_language_map', array('*' => 'en_US'));
$lang = !empty($langmap[$_SESSION['language']]) ? $langmap[$_SESSION['language']] : $langmap['*'];
return str_replace('%l', $lang, $path);
}
}