commit | author | age
|
197601
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| program/include/rcmail.php | |
|
6 |
| | |
|
7 |
| This file is part of the RoundCube Webmail client | |
|
8 |
| Copyright (C) 2008, RoundCube Dev. - Switzerland | |
|
9 |
| Licensed under the GNU GPL | |
|
10 |
| | |
|
11 |
| PURPOSE: | |
|
12 |
| Application class providing core functions and holding | |
|
13 |
| instances of all 'global' objects like db- and imap-connections | |
|
14 |
+-----------------------------------------------------------------------+ |
|
15 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
16 |
+-----------------------------------------------------------------------+ |
|
17 |
|
c8ae24
|
18 |
$Id: rcmail.php 328 2006-08-30 17:41:21Z thomasb $ |
197601
|
19 |
|
T |
20 |
*/ |
|
21 |
|
|
22 |
|
|
23 |
/** |
|
24 |
* Application class of RoundCube Webmail |
|
25 |
* implemented as singleton |
|
26 |
* |
|
27 |
* @package Core |
|
28 |
*/ |
|
29 |
class rcmail |
|
30 |
{ |
1854c4
|
31 |
static public $main_tasks = array('mail','settings','addressbook','login','logout'); |
197601
|
32 |
|
T |
33 |
static private $instance; |
|
34 |
|
|
35 |
public $config; |
|
36 |
public $user; |
|
37 |
public $db; |
|
38 |
public $imap; |
|
39 |
public $output; |
|
40 |
public $task = 'mail'; |
|
41 |
public $action = ''; |
|
42 |
public $comm_path = './'; |
|
43 |
|
|
44 |
private $texts; |
|
45 |
|
|
46 |
|
|
47 |
/** |
|
48 |
* This implements the 'singleton' design pattern |
|
49 |
* |
|
50 |
* @return object qvert The one and only instance |
|
51 |
*/ |
|
52 |
static function get_instance() |
|
53 |
{ |
|
54 |
if (!self::$instance) { |
|
55 |
self::$instance = new rcmail(); |
|
56 |
self::$instance->startup(); // init AFTER object was linked with self::$instance |
|
57 |
} |
|
58 |
|
|
59 |
return self::$instance; |
|
60 |
} |
|
61 |
|
|
62 |
|
|
63 |
/** |
|
64 |
* Private constructor |
|
65 |
*/ |
|
66 |
private function __construct() |
|
67 |
{ |
|
68 |
// load configuration |
|
69 |
$this->config = new rcube_config(); |
|
70 |
|
|
71 |
register_shutdown_function(array($this, 'shutdown')); |
|
72 |
} |
|
73 |
|
|
74 |
|
|
75 |
/** |
|
76 |
* Initial startup function |
|
77 |
* to register session, create database and imap connections |
|
78 |
* |
|
79 |
* @todo Remove global vars $DB, $USER |
|
80 |
*/ |
|
81 |
private function startup() |
|
82 |
{ |
|
83 |
$config_all = $this->config->all(); |
|
84 |
|
b77d0d
|
85 |
// initialize syslog |
A |
86 |
if ($this->config->get('log_driver') == 'syslog') { |
|
87 |
$syslog_id = $this->config->get('syslog_id', 'roundcube'); |
|
88 |
$syslog_facility = $this->config->get('syslog_facility', LOG_USER); |
|
89 |
openlog($syslog_id, LOG_ODELAY, $syslog_facility); |
|
90 |
} |
|
91 |
|
197601
|
92 |
// set task and action properties |
T |
93 |
$this->set_task(strip_quotes(get_input_value('_task', RCUBE_INPUT_GPC))); |
6ea6c9
|
94 |
$this->action = asciiwords(get_input_value('_action', RCUBE_INPUT_GPC)); |
197601
|
95 |
|
T |
96 |
// connect to database |
|
97 |
$GLOBALS['DB'] = $this->get_dbh(); |
|
98 |
|
|
99 |
// use database for storing session data |
|
100 |
include_once('include/session.inc'); |
|
101 |
|
|
102 |
// set session domain |
|
103 |
if (!empty($config_all['session_domain'])) { |
|
104 |
ini_set('session.cookie_domain', $config_all['session_domain']); |
|
105 |
} |
|
106 |
// set session garbage collecting time according to session_lifetime |
|
107 |
if (!empty($config_all['session_lifetime'])) { |
|
108 |
ini_set('session.gc_maxlifetime', ($config_all['session_lifetime']) * 120); |
|
109 |
} |
|
110 |
|
75da0b
|
111 |
// start PHP session (if not in CLI mode) |
A |
112 |
if ($_SERVER['REMOTE_ADDR']) |
|
113 |
session_start(); |
197601
|
114 |
|
T |
115 |
// set initial session vars |
|
116 |
if (!isset($_SESSION['auth_time'])) { |
|
117 |
$_SESSION['auth_time'] = time(); |
|
118 |
$_SESSION['temp'] = true; |
|
119 |
} |
|
120 |
|
|
121 |
// create user object |
|
122 |
$this->set_user(new rcube_user($_SESSION['user_id'])); |
|
123 |
|
|
124 |
// reset some session parameters when changing task |
|
125 |
if ($_SESSION['task'] != $this->task) |
|
126 |
unset($_SESSION['page']); |
|
127 |
|
|
128 |
// set current task to session |
|
129 |
$_SESSION['task'] = $this->task; |
|
130 |
|
|
131 |
// create IMAP object |
|
132 |
if ($this->task == 'mail') |
|
133 |
$this->imap_init(); |
|
134 |
} |
|
135 |
|
|
136 |
|
|
137 |
/** |
|
138 |
* Setter for application task |
|
139 |
* |
|
140 |
* @param string Task to set |
|
141 |
*/ |
|
142 |
public function set_task($task) |
|
143 |
{ |
|
144 |
if (!in_array($task, self::$main_tasks)) |
|
145 |
$task = 'mail'; |
|
146 |
|
|
147 |
$this->task = $task; |
c719f3
|
148 |
$this->comm_path = $this->url(array('task' => $task)); |
197601
|
149 |
|
T |
150 |
if ($this->output) |
|
151 |
$this->output->set_env('task', $task); |
|
152 |
} |
|
153 |
|
|
154 |
|
|
155 |
/** |
|
156 |
* Setter for system user object |
|
157 |
* |
|
158 |
* @param object rcube_user Current user instance |
|
159 |
*/ |
|
160 |
public function set_user($user) |
|
161 |
{ |
|
162 |
if (is_object($user)) { |
|
163 |
$this->user = $user; |
|
164 |
$GLOBALS['USER'] = $this->user; |
|
165 |
|
|
166 |
// overwrite config with user preferences |
|
167 |
$this->config->merge((array)$this->user->get_prefs()); |
|
168 |
} |
|
169 |
|
c8ae24
|
170 |
$_SESSION['language'] = $this->user->language = $this->language_prop($this->config->get('language', $_SESSION['language'])); |
531abb
|
171 |
|
197601
|
172 |
// set localization |
e80f50
|
173 |
setlocale(LC_ALL, $_SESSION['language'] . '.utf8', 'en_US.utf8'); |
14de18
|
174 |
|
A |
175 |
// workaround for http://bugs.php.net/bug.php?id=18556 |
|
176 |
if (in_array($_SESSION['language'], array('tr_TR', 'ku', 'az_AZ'))) |
|
177 |
setlocale(LC_CTYPE, 'en_US' . '.utf8'); |
197601
|
178 |
} |
T |
179 |
|
|
180 |
|
|
181 |
/** |
|
182 |
* Check the given string and return a valid language code |
|
183 |
* |
|
184 |
* @param string Language code |
|
185 |
* @return string Valid language code |
|
186 |
*/ |
|
187 |
private function language_prop($lang) |
|
188 |
{ |
|
189 |
static $rcube_languages, $rcube_language_aliases; |
c8ae24
|
190 |
|
T |
191 |
// user HTTP_ACCEPT_LANGUAGE if no language is specified |
|
192 |
if (empty($lang) || $lang == 'auto') { |
|
193 |
$accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); |
|
194 |
$lang = str_replace('-', '_', $accept_langs[0]); |
|
195 |
} |
|
196 |
|
197601
|
197 |
if (empty($rcube_languages)) { |
T |
198 |
@include(INSTALL_PATH . 'program/localization/index.inc'); |
|
199 |
} |
c3ab75
|
200 |
|
197601
|
201 |
// check if we have an alias for that language |
T |
202 |
if (!isset($rcube_languages[$lang]) && isset($rcube_language_aliases[$lang])) { |
|
203 |
$lang = $rcube_language_aliases[$lang]; |
|
204 |
} |
|
205 |
// try the first two chars |
c3ab75
|
206 |
else if (!isset($rcube_languages[$lang])) { |
7e78b2
|
207 |
$short = substr($lang, 0, 2); |
A |
208 |
|
235086
|
209 |
// check if we have an alias for the short language code |
T |
210 |
if (!isset($rcube_languages[$short]) && isset($rcube_language_aliases[$short])) { |
|
211 |
$lang = $rcube_language_aliases[$short]; |
|
212 |
} |
c3ab75
|
213 |
// expand 'nn' to 'nn_NN' |
T |
214 |
else if (!isset($rcube_languages[$short])) { |
235086
|
215 |
$lang = $short.'_'.strtoupper($short); |
T |
216 |
} |
197601
|
217 |
} |
T |
218 |
|
1854c4
|
219 |
if (!isset($rcube_languages[$lang]) || !is_dir(INSTALL_PATH . 'program/localization/' . $lang)) { |
197601
|
220 |
$lang = 'en_US'; |
T |
221 |
} |
|
222 |
|
|
223 |
return $lang; |
|
224 |
} |
|
225 |
|
|
226 |
|
|
227 |
/** |
|
228 |
* Get the current database connection |
|
229 |
* |
e93e54
|
230 |
* @return object rcube_mdb2 Database connection object |
197601
|
231 |
*/ |
T |
232 |
public function get_dbh() |
|
233 |
{ |
|
234 |
if (!$this->db) { |
|
235 |
$config_all = $this->config->all(); |
|
236 |
|
9e8e5f
|
237 |
$this->db = new rcube_mdb2($config_all['db_dsnw'], $config_all['db_dsnr'], $config_all['db_persistent']); |
197601
|
238 |
$this->db->sqlite_initials = INSTALL_PATH . 'SQL/sqlite.initial.sql'; |
T |
239 |
$this->db->set_debug((bool)$config_all['sql_debug']); |
|
240 |
$this->db->db_connect('w'); |
|
241 |
} |
|
242 |
|
|
243 |
return $this->db; |
|
244 |
} |
|
245 |
|
|
246 |
|
|
247 |
/** |
ade8e1
|
248 |
* Return instance of the internal address book class |
T |
249 |
* |
|
250 |
* @param boolean True if the address book needs to be writeable |
|
251 |
* @return object rcube_contacts Address book object |
|
252 |
*/ |
|
253 |
public function get_address_book($id, $writeable = false) |
|
254 |
{ |
|
255 |
$contacts = null; |
|
256 |
$ldap_config = (array)$this->config->get('ldap_public'); |
|
257 |
$abook_type = strtolower($this->config->get('address_book_type')); |
|
258 |
|
|
259 |
if ($id && $ldap_config[$id]) { |
|
260 |
$contacts = new rcube_ldap($ldap_config[$id]); |
|
261 |
} |
|
262 |
else if ($abook_type == 'ldap') { |
|
263 |
// Use the first writable LDAP address book. |
|
264 |
foreach ($ldap_config as $id => $prop) { |
|
265 |
if (!$writeable || $prop['writable']) { |
|
266 |
$contacts = new rcube_ldap($prop); |
|
267 |
break; |
|
268 |
} |
|
269 |
} |
|
270 |
} |
|
271 |
else { |
|
272 |
$contacts = new rcube_contacts($this->db, $this->user->ID); |
|
273 |
} |
|
274 |
|
|
275 |
return $contacts; |
|
276 |
} |
|
277 |
|
|
278 |
|
|
279 |
/** |
197601
|
280 |
* Init output object for GUI and add common scripts. |
T |
281 |
* This will instantiate a rcmail_template object and set |
|
282 |
* environment vars according to the current session and configuration |
0ece58
|
283 |
* |
T |
284 |
* @param boolean True if this request is loaded in a (i)frame |
|
285 |
* @return object rcube_template Reference to HTML output object |
197601
|
286 |
*/ |
T |
287 |
public function load_gui($framed = false) |
|
288 |
{ |
|
289 |
// init output page |
0ece58
|
290 |
if (!($this->output instanceof rcube_template)) |
T |
291 |
$this->output = new rcube_template($this->task, $framed); |
197601
|
292 |
|
d22455
|
293 |
foreach (array('flag_for_deletion','read_when_deleted') as $js_config_var) { |
197601
|
294 |
$this->output->set_env($js_config_var, $this->config->get($js_config_var)); |
T |
295 |
} |
95d90f
|
296 |
|
A |
297 |
// set keep-alive/check-recent interval |
|
298 |
if ($keep_alive = $this->config->get('keep_alive')) { |
|
299 |
// be sure that it's less than session lifetime |
|
300 |
if ($session_lifetime = $this->config->get('session_lifetime')) |
|
301 |
$keep_alive = min($keep_alive, $session_lifetime * 60 - 30); |
|
302 |
$this->output->set_env('keep_alive', max(60, $keep_alive)); |
|
303 |
} |
197601
|
304 |
|
T |
305 |
if ($framed) { |
|
306 |
$this->comm_path .= '&_framed=1'; |
|
307 |
$this->output->set_env('framed', true); |
|
308 |
} |
|
309 |
|
|
310 |
$this->output->set_env('task', $this->task); |
|
311 |
$this->output->set_env('action', $this->action); |
|
312 |
$this->output->set_env('comm_path', $this->comm_path); |
|
313 |
$this->output->set_charset($this->config->get('charset', RCMAIL_CHARSET)); |
|
314 |
|
|
315 |
// add some basic label to client |
|
316 |
$this->output->add_label('loading'); |
|
317 |
|
|
318 |
return $this->output; |
|
319 |
} |
|
320 |
|
|
321 |
|
|
322 |
/** |
|
323 |
* Create an output object for JSON responses |
0ece58
|
324 |
* |
T |
325 |
* @return object rcube_json_output Reference to JSON output object |
197601
|
326 |
*/ |
T |
327 |
public function init_json() |
|
328 |
{ |
0ece58
|
329 |
if (!($this->output instanceof rcube_json_output)) |
T |
330 |
$this->output = new rcube_json_output($this->task); |
197601
|
331 |
|
T |
332 |
return $this->output; |
|
333 |
} |
|
334 |
|
|
335 |
|
|
336 |
/** |
|
337 |
* Create global IMAP object and connect to server |
|
338 |
* |
|
339 |
* @param boolean True if connection should be established |
|
340 |
* @todo Remove global $IMAP |
|
341 |
*/ |
1854c4
|
342 |
public function imap_init($connect = false) |
197601
|
343 |
{ |
T |
344 |
$this->imap = new rcube_imap($this->db); |
|
345 |
$this->imap->debug_level = $this->config->get('debug_level'); |
|
346 |
$this->imap->skip_deleted = $this->config->get('skip_deleted'); |
|
347 |
|
|
348 |
// enable caching of imap data |
|
349 |
if ($this->config->get('enable_caching')) { |
|
350 |
$this->imap->set_caching(true); |
|
351 |
} |
|
352 |
|
|
353 |
// set pagesize from config |
|
354 |
$this->imap->set_pagesize($this->config->get('pagesize', 50)); |
230f94
|
355 |
|
T |
356 |
// Setting root and delimiter before iil_Connect can save time detecting them |
|
357 |
// using NAMESPACE and LIST |
|
358 |
$options = array( |
|
359 |
'imap' => $this->config->get('imap_auth_type', 'check'), |
|
360 |
'delimiter' => isset($_SESSION['imap_delimiter']) ? $_SESSION['imap_delimiter'] : $this->config->get('imap_delimiter'), |
|
361 |
); |
|
362 |
|
|
363 |
if (isset($_SESSION['imap_root'])) |
|
364 |
$options['rootdir'] = $_SESSION['imap_root']; |
|
365 |
else if ($imap_root = $this->config->get('imap_root')) |
|
366 |
$options['rootdir'] = $imap_root; |
|
367 |
|
|
368 |
$this->imap->set_options($options); |
1854c4
|
369 |
|
197601
|
370 |
// set global object for backward compatibility |
T |
371 |
$GLOBALS['IMAP'] = $this->imap; |
1854c4
|
372 |
|
T |
373 |
if ($connect) |
|
374 |
$this->imap_connect(); |
|
375 |
} |
|
376 |
|
|
377 |
|
|
378 |
/** |
|
379 |
* Connect to IMAP server with stored session data |
|
380 |
* |
|
381 |
* @return bool True on success, false on error |
|
382 |
*/ |
|
383 |
public function imap_connect() |
|
384 |
{ |
|
385 |
$conn = false; |
|
386 |
|
0ece58
|
387 |
if ($_SESSION['imap_host'] && !$this->imap->conn) { |
230f94
|
388 |
if (!($conn = $this->imap->connect($_SESSION['imap_host'], $_SESSION['username'], $this->decrypt_passwd($_SESSION['password']), $_SESSION['imap_port'], $_SESSION['imap_ssl']))) { |
1854c4
|
389 |
if ($this->output) |
T |
390 |
$this->output->show_message($this->imap->error_code == -1 ? 'imaperror' : 'sessionerror', 'error'); |
|
391 |
} |
|
392 |
|
|
393 |
$this->set_imap_prop(); |
|
394 |
} |
|
395 |
|
0ece58
|
396 |
return $conn; |
197601
|
397 |
} |
T |
398 |
|
|
399 |
|
|
400 |
/** |
|
401 |
* Perfom login to the IMAP server and to the webmail service. |
|
402 |
* This will also create a new user entry if auto_create_user is configured. |
|
403 |
* |
|
404 |
* @param string IMAP user name |
|
405 |
* @param string IMAP password |
|
406 |
* @param string IMAP host |
|
407 |
* @return boolean True on success, False on failure |
|
408 |
*/ |
|
409 |
function login($username, $pass, $host=NULL) |
|
410 |
{ |
|
411 |
$user = NULL; |
|
412 |
$config = $this->config->all(); |
|
413 |
|
|
414 |
if (!$host) |
|
415 |
$host = $config['default_host']; |
|
416 |
|
|
417 |
// Validate that selected host is in the list of configured hosts |
|
418 |
if (is_array($config['default_host'])) { |
|
419 |
$allowed = false; |
|
420 |
foreach ($config['default_host'] as $key => $host_allowed) { |
|
421 |
if (!is_numeric($key)) |
|
422 |
$host_allowed = $key; |
|
423 |
if ($host == $host_allowed) { |
|
424 |
$allowed = true; |
|
425 |
break; |
|
426 |
} |
|
427 |
} |
|
428 |
if (!$allowed) |
|
429 |
return false; |
|
430 |
} |
|
431 |
else if (!empty($config['default_host']) && $host != $config['default_host']) |
|
432 |
return false; |
|
433 |
|
|
434 |
// parse $host URL |
|
435 |
$a_host = parse_url($host); |
|
436 |
if ($a_host['host']) { |
|
437 |
$host = $a_host['host']; |
|
438 |
$imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? $a_host['scheme'] : null; |
|
439 |
$imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : $config['default_port']); |
|
440 |
} |
|
441 |
else |
|
442 |
$imap_port = $config['default_port']; |
|
443 |
|
|
444 |
|
|
445 |
/* Modify username with domain if required |
|
446 |
Inspired by Marco <P0L0_notspam_binware.org> |
|
447 |
*/ |
|
448 |
// Check if we need to add domain |
|
449 |
if (!empty($config['username_domain']) && !strpos($username, '@')) { |
|
450 |
if (is_array($config['username_domain']) && isset($config['username_domain'][$host])) |
|
451 |
$username .= '@'.$config['username_domain'][$host]; |
|
452 |
else if (is_string($config['username_domain'])) |
|
453 |
$username .= '@'.$config['username_domain']; |
|
454 |
} |
|
455 |
|
|
456 |
// try to resolve email address from virtuser table |
|
457 |
if (!empty($config['virtuser_file']) && strpos($username, '@')) |
|
458 |
$username = rcube_user::email2user($username); |
|
459 |
|
|
460 |
// lowercase username if it's an e-mail address (#1484473) |
|
461 |
if (strpos($username, '@')) |
ec0171
|
462 |
$username = rc_strtolower($username); |
197601
|
463 |
|
T |
464 |
// user already registered -> overwrite username |
|
465 |
if ($user = rcube_user::query($username, $host)) |
|
466 |
$username = $user->data['username']; |
|
467 |
|
|
468 |
// exit if IMAP login failed |
230f94
|
469 |
if (!($imap_login = $this->imap->connect($host, $username, $pass, $imap_port, $imap_ssl))) |
197601
|
470 |
return false; |
T |
471 |
|
|
472 |
// user already registered -> update user's record |
|
473 |
if (is_object($user)) { |
|
474 |
$user->touch(); |
|
475 |
} |
|
476 |
// create new system user |
|
477 |
else if ($config['auto_create_user']) { |
|
478 |
if ($created = rcube_user::create($username, $host)) { |
|
479 |
$user = $created; |
|
480 |
|
|
481 |
// get existing mailboxes (but why?) |
|
482 |
// $a_mailboxes = $this->imap->list_mailboxes(); |
|
483 |
} |
|
484 |
} |
|
485 |
else { |
|
486 |
raise_error(array( |
|
487 |
'code' => 600, |
|
488 |
'type' => 'php', |
bba657
|
489 |
'file' => RCMAIL_CONFIG_DIR."/main.inc.php", |
197601
|
490 |
'message' => "Acces denied for new user $username. 'auto_create_user' is disabled" |
T |
491 |
), true, false); |
|
492 |
} |
|
493 |
|
|
494 |
// login succeeded |
|
495 |
if (is_object($user) && $user->ID) { |
|
496 |
$this->set_user($user); |
|
497 |
|
|
498 |
// set session vars |
|
499 |
$_SESSION['user_id'] = $user->ID; |
|
500 |
$_SESSION['username'] = $user->data['username']; |
|
501 |
$_SESSION['imap_host'] = $host; |
|
502 |
$_SESSION['imap_port'] = $imap_port; |
|
503 |
$_SESSION['imap_ssl'] = $imap_ssl; |
1854c4
|
504 |
$_SESSION['password'] = $this->encrypt_passwd($pass); |
197601
|
505 |
$_SESSION['login_time'] = mktime(); |
c8ae24
|
506 |
|
T |
507 |
if ($_REQUEST['_timezone'] != '_default_') |
|
508 |
$_SESSION['timezone'] = floatval($_REQUEST['_timezone']); |
197601
|
509 |
|
T |
510 |
// force reloading complete list of subscribed mailboxes |
|
511 |
$this->set_imap_prop(); |
|
512 |
$this->imap->clear_cache('mailboxes'); |
|
513 |
|
|
514 |
if ($config['create_default_folders']) |
|
515 |
$this->imap->create_default_folders(); |
|
516 |
|
|
517 |
return true; |
|
518 |
} |
|
519 |
|
|
520 |
return false; |
|
521 |
} |
|
522 |
|
|
523 |
|
|
524 |
/** |
|
525 |
* Set root dir and last stored mailbox |
|
526 |
* This must be done AFTER connecting to the server! |
|
527 |
*/ |
|
528 |
public function set_imap_prop() |
|
529 |
{ |
|
530 |
$this->imap->set_charset($this->config->get('default_charset', RCMAIL_CHARSET)); |
|
531 |
|
|
532 |
if ($default_folders = $this->config->get('default_imap_folders')) { |
|
533 |
$this->imap->set_default_mailboxes($default_folders); |
|
534 |
} |
|
535 |
if (!empty($_SESSION['mbox'])) { |
|
536 |
$this->imap->set_mailbox($_SESSION['mbox']); |
|
537 |
} |
|
538 |
if (isset($_SESSION['page'])) { |
|
539 |
$this->imap->set_page($_SESSION['page']); |
|
540 |
} |
230f94
|
541 |
|
T |
542 |
// cache IMAP root and delimiter in session for performance reasons |
|
543 |
$_SESSION['imap_root'] = $this->imap->root_dir; |
|
544 |
$_SESSION['imap_delimiter'] = $this->imap->delimiter; |
197601
|
545 |
} |
T |
546 |
|
1854c4
|
547 |
|
T |
548 |
/** |
|
549 |
* Auto-select IMAP host based on the posted login information |
|
550 |
* |
|
551 |
* @return string Selected IMAP host |
|
552 |
*/ |
|
553 |
public function autoselect_host() |
|
554 |
{ |
|
555 |
$default_host = $this->config->get('default_host'); |
257f88
|
556 |
$host = null; |
1854c4
|
557 |
|
257f88
|
558 |
if (is_array($default_host)) { |
T |
559 |
$post_host = get_input_value('_host', RCUBE_INPUT_POST); |
|
560 |
|
|
561 |
// direct match in default_host array |
|
562 |
if ($default_host[$post_host] || in_array($post_host, array_values($default_host))) { |
|
563 |
$host = $post_host; |
|
564 |
} |
|
565 |
|
|
566 |
// try to select host by mail domain |
1854c4
|
567 |
list($user, $domain) = explode('@', get_input_value('_user', RCUBE_INPUT_POST)); |
T |
568 |
if (!empty($domain)) { |
257f88
|
569 |
foreach ($default_host as $imap_host => $mail_domains) { |
1854c4
|
570 |
if (is_array($mail_domains) && in_array($domain, $mail_domains)) { |
T |
571 |
$host = $imap_host; |
|
572 |
break; |
|
573 |
} |
|
574 |
} |
|
575 |
} |
|
576 |
|
|
577 |
// take the first entry if $host is still an array |
257f88
|
578 |
if (empty($host)) { |
T |
579 |
$host = array_shift($default_host); |
|
580 |
} |
|
581 |
} |
|
582 |
else if (empty($default_host)) { |
|
583 |
$host = get_input_value('_host', RCUBE_INPUT_POST); |
1854c4
|
584 |
} |
eec34e
|
585 |
else |
T |
586 |
$host = $default_host; |
1854c4
|
587 |
|
T |
588 |
return $host; |
|
589 |
} |
|
590 |
|
|
591 |
|
|
592 |
/** |
|
593 |
* Get localized text in the desired language |
|
594 |
* |
|
595 |
* @param mixed Named parameters array or label name |
|
596 |
* @return string Localized text |
|
597 |
*/ |
|
598 |
public function gettext($attrib) |
|
599 |
{ |
|
600 |
// load localization files if not done yet |
|
601 |
if (empty($this->texts)) |
|
602 |
$this->load_language(); |
|
603 |
|
|
604 |
// extract attributes |
|
605 |
if (is_string($attrib)) |
|
606 |
$attrib = array('name' => $attrib); |
|
607 |
|
|
608 |
$nr = is_numeric($attrib['nr']) ? $attrib['nr'] : 1; |
|
609 |
$vars = isset($attrib['vars']) ? $attrib['vars'] : ''; |
|
610 |
|
|
611 |
$command_name = !empty($attrib['command']) ? $attrib['command'] : NULL; |
|
612 |
$alias = $attrib['name'] ? $attrib['name'] : ($command_name && $command_label_map[$command_name] ? $command_label_map[$command_name] : ''); |
|
613 |
|
|
614 |
// text does not exist |
|
615 |
if (!($text_item = $this->texts[$alias])) { |
|
616 |
/* |
|
617 |
raise_error(array( |
|
618 |
'code' => 500, |
|
619 |
'type' => 'php', |
|
620 |
'line' => __LINE__, |
|
621 |
'file' => __FILE__, |
|
622 |
'message' => "Missing localized text for '$alias' in '$sess_user_lang'"), TRUE, FALSE); |
|
623 |
*/ |
|
624 |
return "[$alias]"; |
|
625 |
} |
|
626 |
|
|
627 |
// make text item array |
|
628 |
$a_text_item = is_array($text_item) ? $text_item : array('single' => $text_item); |
|
629 |
|
|
630 |
// decide which text to use |
|
631 |
if ($nr == 1) { |
|
632 |
$text = $a_text_item['single']; |
|
633 |
} |
|
634 |
else if ($nr > 0) { |
|
635 |
$text = $a_text_item['multiple']; |
|
636 |
} |
|
637 |
else if ($nr == 0) { |
|
638 |
if ($a_text_item['none']) |
|
639 |
$text = $a_text_item['none']; |
|
640 |
else if ($a_text_item['single']) |
|
641 |
$text = $a_text_item['single']; |
|
642 |
else if ($a_text_item['multiple']) |
|
643 |
$text = $a_text_item['multiple']; |
|
644 |
} |
|
645 |
|
|
646 |
// default text is single |
|
647 |
if ($text == '') { |
|
648 |
$text = $a_text_item['single']; |
|
649 |
} |
|
650 |
|
|
651 |
// replace vars in text |
|
652 |
if (is_array($attrib['vars'])) { |
|
653 |
foreach ($attrib['vars'] as $var_key => $var_value) |
|
654 |
$a_replace_vars[$var_key{0}=='$' ? substr($var_key, 1) : $var_key] = $var_value; |
|
655 |
} |
|
656 |
|
|
657 |
if ($a_replace_vars) |
|
658 |
$text = preg_replace('/\$\{?([_a-z]{1}[_a-z0-9]*)\}?/ei', '$a_replace_vars["\1"]', $text); |
|
659 |
|
|
660 |
// format output |
|
661 |
if (($attrib['uppercase'] && strtolower($attrib['uppercase']=='first')) || $attrib['ucfirst']) |
|
662 |
return ucfirst($text); |
|
663 |
else if ($attrib['uppercase']) |
|
664 |
return strtoupper($text); |
|
665 |
else if ($attrib['lowercase']) |
|
666 |
return strtolower($text); |
|
667 |
|
|
668 |
return $text; |
|
669 |
} |
|
670 |
|
|
671 |
|
|
672 |
/** |
|
673 |
* Load a localization package |
|
674 |
* |
|
675 |
* @param string Language ID |
|
676 |
*/ |
|
677 |
public function load_language($lang = null) |
|
678 |
{ |
c8ae24
|
679 |
$lang = $this->language_prop(($lang ? $lang : $_SESSION['language'])); |
1854c4
|
680 |
|
T |
681 |
// load localized texts |
|
682 |
if (empty($this->texts) || $lang != $_SESSION['language']) { |
|
683 |
$this->texts = array(); |
|
684 |
|
|
685 |
// get english labels (these should be complete) |
|
686 |
@include(INSTALL_PATH . 'program/localization/en_US/labels.inc'); |
|
687 |
@include(INSTALL_PATH . 'program/localization/en_US/messages.inc'); |
|
688 |
|
|
689 |
if (is_array($labels)) |
9d9f8d
|
690 |
$this->texts = $labels; |
1854c4
|
691 |
if (is_array($messages)) |
9d9f8d
|
692 |
$this->texts = array_merge($this->texts, $messages); |
1854c4
|
693 |
|
T |
694 |
// include user language files |
|
695 |
if ($lang != 'en' && is_dir(INSTALL_PATH . 'program/localization/' . $lang)) { |
|
696 |
include_once(INSTALL_PATH . 'program/localization/' . $lang . '/labels.inc'); |
|
697 |
include_once(INSTALL_PATH . 'program/localization/' . $lang . '/messages.inc'); |
|
698 |
|
|
699 |
if (is_array($labels)) |
|
700 |
$this->texts = array_merge($this->texts, $labels); |
|
701 |
if (is_array($messages)) |
|
702 |
$this->texts = array_merge($this->texts, $messages); |
|
703 |
} |
|
704 |
|
|
705 |
$_SESSION['language'] = $lang; |
|
706 |
} |
|
707 |
} |
|
708 |
|
|
709 |
|
|
710 |
/** |
|
711 |
* Read directory program/localization and return a list of available languages |
|
712 |
* |
|
713 |
* @return array List of available localizations |
|
714 |
*/ |
|
715 |
public function list_languages() |
|
716 |
{ |
|
717 |
static $sa_languages = array(); |
|
718 |
|
|
719 |
if (!sizeof($sa_languages)) { |
|
720 |
@include(INSTALL_PATH . 'program/localization/index.inc'); |
|
721 |
|
|
722 |
if ($dh = @opendir(INSTALL_PATH . 'program/localization')) { |
|
723 |
while (($name = readdir($dh)) !== false) { |
|
724 |
if ($name{0}=='.' || !is_dir(INSTALL_PATH . 'program/localization/' . $name)) |
|
725 |
continue; |
|
726 |
|
|
727 |
if ($label = $rcube_languages[$name]) |
|
728 |
$sa_languages[$name] = $label ? $label : $name; |
|
729 |
} |
|
730 |
closedir($dh); |
|
731 |
} |
|
732 |
} |
|
733 |
|
|
734 |
return $sa_languages; |
|
735 |
} |
|
736 |
|
|
737 |
|
|
738 |
/** |
|
739 |
* Check the auth hash sent by the client against the local session credentials |
|
740 |
* |
|
741 |
* @return boolean True if valid, False if not |
|
742 |
*/ |
|
743 |
function authenticate_session() |
|
744 |
{ |
|
745 |
global $SESS_CLIENT_IP, $SESS_CHANGED; |
|
746 |
|
|
747 |
// advanced session authentication |
|
748 |
if ($this->config->get('double_auth')) { |
|
749 |
$now = time(); |
|
750 |
$valid = ($_COOKIE['sessauth'] == $this->get_auth_hash(session_id(), $_SESSION['auth_time']) || |
|
751 |
$_COOKIE['sessauth'] == $this->get_auth_hash(session_id(), $_SESSION['last_auth'])); |
|
752 |
|
|
753 |
// renew auth cookie every 5 minutes (only for GET requests) |
|
754 |
if (!$valid || ($_SERVER['REQUEST_METHOD']!='POST' && $now - $_SESSION['auth_time'] > 300)) { |
|
755 |
$_SESSION['last_auth'] = $_SESSION['auth_time']; |
|
756 |
$_SESSION['auth_time'] = $now; |
cefd1d
|
757 |
rcmail::setcookie('sessauth', $this->get_auth_hash(session_id(), $now), 0); |
1854c4
|
758 |
} |
T |
759 |
} |
|
760 |
else { |
|
761 |
$valid = $this->config->get('ip_check') ? $_SERVER['REMOTE_ADDR'] == $SESS_CLIENT_IP : true; |
|
762 |
} |
|
763 |
|
|
764 |
// check session filetime |
|
765 |
$lifetime = $this->config->get('session_lifetime'); |
|
766 |
if (!empty($lifetime) && isset($SESS_CHANGED) && $SESS_CHANGED + $lifetime*60 < time()) { |
|
767 |
$valid = false; |
|
768 |
} |
|
769 |
|
|
770 |
return $valid; |
|
771 |
} |
|
772 |
|
|
773 |
|
|
774 |
/** |
|
775 |
* Destroy session data and remove cookie |
|
776 |
*/ |
|
777 |
public function kill_session() |
|
778 |
{ |
c8ae24
|
779 |
$_SESSION = array('language' => $this->user->language, 'auth_time' => time(), 'temp' => true); |
cefd1d
|
780 |
rcmail::setcookie('sessauth', '-del-', time() - 60); |
1854c4
|
781 |
$this->user->reset(); |
T |
782 |
} |
|
783 |
|
|
784 |
|
|
785 |
/** |
|
786 |
* Do server side actions on logout |
|
787 |
*/ |
|
788 |
public function logout_actions() |
|
789 |
{ |
|
790 |
$config = $this->config->all(); |
|
791 |
|
|
792 |
// on logout action we're not connected to imap server |
|
793 |
if (($config['logout_purge'] && !empty($config['trash_mbox'])) || $config['logout_expunge']) { |
|
794 |
if (!$this->authenticate_session()) |
|
795 |
return; |
|
796 |
|
|
797 |
$this->imap_init(true); |
|
798 |
} |
|
799 |
|
|
800 |
if ($config['logout_purge'] && !empty($config['trash_mbox'])) { |
|
801 |
$this->imap->clear_mailbox($config['trash_mbox']); |
|
802 |
} |
|
803 |
|
|
804 |
if ($config['logout_expunge']) { |
|
805 |
$this->imap->expunge('INBOX'); |
|
806 |
} |
|
807 |
} |
|
808 |
|
|
809 |
|
|
810 |
/** |
|
811 |
* Function to be executed in script shutdown |
|
812 |
* Registered with register_shutdown_function() |
|
813 |
*/ |
197601
|
814 |
public function shutdown() |
T |
815 |
{ |
|
816 |
if (is_object($this->imap)) { |
|
817 |
$this->imap->close(); |
|
818 |
$this->imap->write_cache(); |
|
819 |
} |
|
820 |
|
|
821 |
if (is_object($this->contacts)) |
|
822 |
$this->contacts->close(); |
|
823 |
|
|
824 |
// before closing the database connection, write session data |
75da0b
|
825 |
if ($_SERVER['REMOTE_ADDR']) |
A |
826 |
session_write_close(); |
197601
|
827 |
} |
T |
828 |
|
1854c4
|
829 |
|
T |
830 |
/** |
|
831 |
* Create unique authorization hash |
|
832 |
* |
|
833 |
* @param string Session ID |
|
834 |
* @param int Timestamp |
|
835 |
* @return string The generated auth hash |
|
836 |
*/ |
|
837 |
private function get_auth_hash($sess_id, $ts) |
|
838 |
{ |
|
839 |
$auth_string = sprintf('rcmail*sess%sR%s*Chk:%s;%s', |
|
840 |
$sess_id, |
|
841 |
$ts, |
|
842 |
$this->config->get('ip_check') ? $_SERVER['REMOTE_ADDR'] : '***.***.***.***', |
|
843 |
$_SERVER['HTTP_USER_AGENT']); |
|
844 |
|
|
845 |
if (function_exists('sha1')) |
|
846 |
return sha1($auth_string); |
|
847 |
else |
|
848 |
return md5($auth_string); |
|
849 |
} |
|
850 |
|
|
851 |
/** |
|
852 |
* Encrypt IMAP password using DES encryption |
|
853 |
* |
|
854 |
* @param string Password to encrypt |
|
855 |
* @return string Encryprted string |
|
856 |
*/ |
26539d
|
857 |
public function encrypt_passwd($pass) |
1854c4
|
858 |
{ |
T |
859 |
if (function_exists('mcrypt_module_open') && ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, ""))) { |
|
860 |
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); |
|
861 |
mcrypt_generic_init($td, $this->config->get_des_key(), $iv); |
|
862 |
$cypher = mcrypt_generic($td, $pass); |
|
863 |
mcrypt_generic_deinit($td); |
|
864 |
mcrypt_module_close($td); |
|
865 |
} |
|
866 |
else if (function_exists('des')) { |
|
867 |
$cypher = des($this->config->get_des_key(), $pass, 1, 0, NULL); |
|
868 |
} |
|
869 |
else { |
|
870 |
$cypher = $pass; |
|
871 |
|
|
872 |
raise_error(array( |
|
873 |
'code' => 500, |
|
874 |
'type' => 'php', |
|
875 |
'file' => __FILE__, |
|
876 |
'message' => "Could not convert encrypt password. Make sure Mcrypt is installed or lib/des.inc is available" |
|
877 |
), true, false); |
|
878 |
} |
|
879 |
|
|
880 |
return base64_encode($cypher); |
|
881 |
} |
|
882 |
|
|
883 |
|
|
884 |
/** |
|
885 |
* Decrypt IMAP password using DES encryption |
|
886 |
* |
|
887 |
* @param string Encrypted password |
|
888 |
* @return string Plain password |
|
889 |
*/ |
|
890 |
public function decrypt_passwd($cypher) |
|
891 |
{ |
|
892 |
if (function_exists('mcrypt_module_open') && ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, ""))) { |
|
893 |
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); |
|
894 |
mcrypt_generic_init($td, $this->config->get_des_key(), $iv); |
|
895 |
$pass = mdecrypt_generic($td, base64_decode($cypher)); |
|
896 |
mcrypt_generic_deinit($td); |
|
897 |
mcrypt_module_close($td); |
|
898 |
} |
|
899 |
else if (function_exists('des')) { |
|
900 |
$pass = des($this->config->get_des_key(), base64_decode($cypher), 0, 0, NULL); |
|
901 |
} |
|
902 |
else { |
|
903 |
$pass = base64_decode($cypher); |
|
904 |
} |
|
905 |
|
|
906 |
return preg_replace('/\x00/', '', $pass); |
|
907 |
} |
|
908 |
|
c719f3
|
909 |
|
T |
910 |
/** |
|
911 |
* Build a valid URL to this instance of RoundCube |
|
912 |
* |
|
913 |
* @param mixed Either a string with the action or url parameters as key-value pairs |
|
914 |
* @return string Valid application URL |
|
915 |
*/ |
|
916 |
public function url($p) |
|
917 |
{ |
|
918 |
if (!is_array($p)) |
fde466
|
919 |
$p = array('_action' => @func_get_arg(0)); |
cf1777
|
920 |
|
T |
921 |
if (!$p['task'] || !in_array($p['task'], rcmail::$main_tasks)) |
|
922 |
$p['task'] = $this->task; |
|
923 |
|
1038a6
|
924 |
$p['_task'] = $p['task']; |
A |
925 |
unset($p['task']); |
|
926 |
|
cf1777
|
927 |
$url = './'; |
T |
928 |
$delm = '?'; |
|
929 |
foreach (array_reverse($p) as $par => $val) |
|
930 |
{ |
|
931 |
if (!empty($val)) { |
|
932 |
$url .= $delm.urlencode($par).'='.urlencode($val); |
|
933 |
$delm = '&'; |
|
934 |
} |
|
935 |
} |
c719f3
|
936 |
return $url; |
T |
937 |
} |
cefd1d
|
938 |
|
T |
939 |
|
|
940 |
/** |
|
941 |
* Helper method to set a cookie with the current path and host settings |
|
942 |
* |
|
943 |
* @param string Cookie name |
|
944 |
* @param string Cookie value |
|
945 |
* @param string Expiration time |
|
946 |
*/ |
|
947 |
public static function setcookie($name, $value, $exp = 0) |
|
948 |
{ |
|
949 |
$cookie = session_get_cookie_params(); |
|
950 |
setcookie($name, $value, $exp, $cookie['path'], $cookie['domain'], |
|
951 |
($_SERVER['HTTPS'] && ($_SERVER['HTTPS'] != 'off'))); |
|
952 |
} |
197601
|
953 |
} |
T |
954 |
|
|
955 |
|