| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/include/main.inc | |
|---|
| 6 | | | |
|---|
| 7 | | This file is part of the RoundCube Webmail client | |
|---|
| 8 | | Copyright (C) 2005-2007, RoundCube Dev, - Switzerland | |
|---|
| 9 | | Licensed under the GNU GPL | |
|---|
| 10 | | | |
|---|
| 11 | | PURPOSE: | |
|---|
| 12 | | Provide basic functions for the webmail package | |
|---|
| 13 | | | |
|---|
| 14 | +-----------------------------------------------------------------------+ |
|---|
| 15 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 16 | +-----------------------------------------------------------------------+ |
|---|
| 17 | |
|---|
| 18 | $Id$ |
|---|
| 19 | |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * RoundCube Webmail common functions |
|---|
| 24 | * |
|---|
| 25 | * @package Core |
|---|
| 26 | * @author Thomas Bruederli <roundcube@gmail.com> |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | require_once('lib/des.inc'); |
|---|
| 30 | require_once('lib/utf7.inc'); |
|---|
| 31 | require_once('lib/utf8.class.php'); |
|---|
| 32 | require_once('include/rcube_user.inc'); |
|---|
| 33 | require_once('include/rcube_shared.inc'); |
|---|
| 34 | require_once('include/rcmail_template.inc'); |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | // define constannts for input reading |
|---|
| 38 | define('RCUBE_INPUT_GET', 0x0101); |
|---|
| 39 | define('RCUBE_INPUT_POST', 0x0102); |
|---|
| 40 | define('RCUBE_INPUT_GPC', 0x0103); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Initial startup function |
|---|
| 45 | * to register session, create database and imap connections |
|---|
| 46 | * |
|---|
| 47 | * @param string Current task |
|---|
| 48 | */ |
|---|
| 49 | function rcmail_startup($task='mail') |
|---|
| 50 | { |
|---|
| 51 | global $sess_id, $sess_user_lang; |
|---|
| 52 | global $CONFIG, $INSTALL_PATH, $BROWSER, $OUTPUT, $_SESSION, $IMAP, $DB, $USER; |
|---|
| 53 | |
|---|
| 54 | // check client |
|---|
| 55 | $BROWSER = rcube_browser(); |
|---|
| 56 | |
|---|
| 57 | // load configuration |
|---|
| 58 | $CONFIG = rcmail_load_config(); |
|---|
| 59 | |
|---|
| 60 | // set session garbage collecting time according to session_lifetime |
|---|
| 61 | if (!empty($CONFIG['session_lifetime'])) |
|---|
| 62 | ini_set('session.gc_maxlifetime', ($CONFIG['session_lifetime']) * 120); |
|---|
| 63 | |
|---|
| 64 | // prepare DB connection |
|---|
| 65 | $dbwrapper = empty($CONFIG['db_backend']) ? 'db' : $CONFIG['db_backend']; |
|---|
| 66 | $dbclass = "rcube_" . $dbwrapper; |
|---|
| 67 | require_once("include/$dbclass.inc"); |
|---|
| 68 | |
|---|
| 69 | $DB = new $dbclass($CONFIG['db_dsnw'], $CONFIG['db_dsnr'], $CONFIG['db_persistent']); |
|---|
| 70 | $DB->sqlite_initials = $INSTALL_PATH.'SQL/sqlite.initial.sql'; |
|---|
| 71 | $DB->db_connect('w'); |
|---|
| 72 | |
|---|
| 73 | // use database for storing session data |
|---|
| 74 | include_once('include/session.inc'); |
|---|
| 75 | |
|---|
| 76 | // init session |
|---|
| 77 | session_start(); |
|---|
| 78 | $sess_id = session_id(); |
|---|
| 79 | |
|---|
| 80 | // create session and set session vars |
|---|
| 81 | if (!isset($_SESSION['auth_time'])) |
|---|
| 82 | { |
|---|
| 83 | $_SESSION['user_lang'] = rcube_language_prop($CONFIG['locale_string']); |
|---|
| 84 | $_SESSION['auth_time'] = time(); |
|---|
| 85 | $_SESSION['temp'] = true; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | // set session vars global |
|---|
| 89 | $sess_user_lang = rcube_language_prop($_SESSION['user_lang']); |
|---|
| 90 | |
|---|
| 91 | // create user object |
|---|
| 92 | $USER = new rcube_user($_SESSION['user_id']); |
|---|
| 93 | |
|---|
| 94 | // overwrite config with user preferences |
|---|
| 95 | $CONFIG = array_merge($CONFIG, (array)$USER->get_prefs()); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | // reset some session parameters when changing task |
|---|
| 99 | if ($_SESSION['task'] != $task) |
|---|
| 100 | unset($_SESSION['page']); |
|---|
| 101 | |
|---|
| 102 | // set current task to session |
|---|
| 103 | $_SESSION['task'] = $task; |
|---|
| 104 | |
|---|
| 105 | // create IMAP object |
|---|
| 106 | if ($task=='mail') |
|---|
| 107 | rcmail_imap_init(); |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | // set localization |
|---|
| 111 | if ($CONFIG['locale_string']) |
|---|
| 112 | setlocale(LC_ALL, $CONFIG['locale_string']); |
|---|
| 113 | else if ($sess_user_lang) |
|---|
| 114 | setlocale(LC_ALL, $sess_user_lang); |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | register_shutdown_function('rcmail_shutdown'); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | * Load roundcube configuration array |
|---|
| 123 | * |
|---|
| 124 | * @return array Named configuration parameters |
|---|
| 125 | */ |
|---|
| 126 | function rcmail_load_config() |
|---|
| 127 | { |
|---|
| 128 | global $INSTALL_PATH; |
|---|
| 129 | |
|---|
| 130 | // load config file |
|---|
| 131 | include_once('config/main.inc.php'); |
|---|
| 132 | $conf = is_array($rcmail_config) ? $rcmail_config : array(); |
|---|
| 133 | |
|---|
| 134 | // load host-specific configuration |
|---|
| 135 | rcmail_load_host_config($conf); |
|---|
| 136 | |
|---|
| 137 | $conf['skin_path'] = $conf['skin_path'] ? unslashify($conf['skin_path']) : 'skins/default'; |
|---|
| 138 | |
|---|
| 139 | // load db conf |
|---|
| 140 | include_once('config/db.inc.php'); |
|---|
| 141 | $conf = array_merge($conf, $rcmail_config); |
|---|
| 142 | |
|---|
| 143 | if (empty($conf['log_dir'])) |
|---|
| 144 | $conf['log_dir'] = $INSTALL_PATH.'logs'; |
|---|
| 145 | else |
|---|
| 146 | $conf['log_dir'] = unslashify($conf['log_dir']); |
|---|
| 147 | |
|---|
| 148 | // set PHP error logging according to config |
|---|
| 149 | if ($conf['debug_level'] & 1) |
|---|
| 150 | { |
|---|
| 151 | ini_set('log_errors', 1); |
|---|
| 152 | ini_set('error_log', $conf['log_dir'].'/errors'); |
|---|
| 153 | } |
|---|
| 154 | if ($conf['debug_level'] & 4) |
|---|
| 155 | ini_set('display_errors', 1); |
|---|
| 156 | else |
|---|
| 157 | ini_set('display_errors', 0); |
|---|
| 158 | |
|---|
| 159 | return $conf; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | /** |
|---|
| 164 | * Load a host-specific config file if configured |
|---|
| 165 | * This will merge the host specific configuration with the given one |
|---|
| 166 | * |
|---|
| 167 | * @param array Global configuration parameters |
|---|
| 168 | */ |
|---|
| 169 | function rcmail_load_host_config(&$config) |
|---|
| 170 | { |
|---|
| 171 | $fname = NULL; |
|---|
| 172 | |
|---|
| 173 | if (is_array($config['include_host_config'])) |
|---|
| 174 | $fname = $config['include_host_config'][$_SERVER['HTTP_HOST']]; |
|---|
| 175 | else if (!empty($config['include_host_config'])) |
|---|
| 176 | $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php'; |
|---|
| 177 | |
|---|
| 178 | if ($fname && is_file('config/'.$fname)) |
|---|
| 179 | { |
|---|
| 180 | include('config/'.$fname); |
|---|
| 181 | $config = array_merge($config, $rcmail_config); |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * Create unique authorization hash |
|---|
| 188 | * |
|---|
| 189 | * @param string Session ID |
|---|
| 190 | * @param int Timestamp |
|---|
| 191 | * @return string The generated auth hash |
|---|
| 192 | */ |
|---|
| 193 | function rcmail_auth_hash($sess_id, $ts) |
|---|
| 194 | { |
|---|
| 195 | global $CONFIG; |
|---|
| 196 | |
|---|
| 197 | $auth_string = sprintf('rcmail*sess%sR%s*Chk:%s;%s', |
|---|
| 198 | $sess_id, |
|---|
| 199 | $ts, |
|---|
| 200 | $CONFIG['ip_check'] ? $_SERVER['REMOTE_ADDR'] : '***.***.***.***', |
|---|
| 201 | $_SERVER['HTTP_USER_AGENT']); |
|---|
| 202 | |
|---|
| 203 | if (function_exists('sha1')) |
|---|
| 204 | return sha1($auth_string); |
|---|
| 205 | else |
|---|
| 206 | return md5($auth_string); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | * Check the auth hash sent by the client against the local session credentials |
|---|
| 212 | * |
|---|
| 213 | * @return boolean True if valid, False if not |
|---|
| 214 | */ |
|---|
| 215 | function rcmail_authenticate_session() |
|---|
| 216 | { |
|---|
| 217 | global $CONFIG, $SESS_CLIENT_IP, $SESS_CHANGED; |
|---|
| 218 | |
|---|
| 219 | // advanced session authentication |
|---|
| 220 | if ($CONFIG['double_auth']) |
|---|
| 221 | { |
|---|
| 222 | $now = time(); |
|---|
| 223 | $valid = ($_COOKIE['sessauth'] == rcmail_auth_hash(session_id(), $_SESSION['auth_time']) || |
|---|
| 224 | $_COOKIE['sessauth'] == rcmail_auth_hash(session_id(), $_SESSION['last_auth'])); |
|---|
| 225 | |
|---|
| 226 | // renew auth cookie every 5 minutes (only for GET requests) |
|---|
| 227 | if (!$valid || ($_SERVER['REQUEST_METHOD']!='POST' && $now-$_SESSION['auth_time'] > 300)) |
|---|
| 228 | { |
|---|
| 229 | $_SESSION['last_auth'] = $_SESSION['auth_time']; |
|---|
| 230 | $_SESSION['auth_time'] = $now; |
|---|
| 231 | setcookie('sessauth', rcmail_auth_hash(session_id(), $now)); |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | else |
|---|
| 235 | $valid = $CONFIG['ip_check'] ? $_SERVER['REMOTE_ADDR'] == $SESS_CLIENT_IP : true; |
|---|
| 236 | |
|---|
| 237 | // check session filetime |
|---|
| 238 | if (!empty($CONFIG['session_lifetime']) && isset($SESS_CHANGED) && $SESS_CHANGED + $CONFIG['session_lifetime']*60 < time()) |
|---|
| 239 | $valid = false; |
|---|
| 240 | |
|---|
| 241 | return $valid; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | /** |
|---|
| 246 | * Create global IMAP object and connect to server |
|---|
| 247 | * |
|---|
| 248 | * @param boolean True if connection should be established |
|---|
| 249 | */ |
|---|
| 250 | function rcmail_imap_init($connect=FALSE) |
|---|
| 251 | { |
|---|
| 252 | global $CONFIG, $DB, $IMAP, $OUTPUT; |
|---|
| 253 | |
|---|
| 254 | $IMAP = new rcube_imap($DB); |
|---|
| 255 | $IMAP->debug_level = $CONFIG['debug_level']; |
|---|
| 256 | $IMAP->skip_deleted = $CONFIG['skip_deleted']; |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | // connect with stored session data |
|---|
| 260 | if ($connect) |
|---|
| 261 | { |
|---|
| 262 | if (!($conn = $IMAP->connect($_SESSION['imap_host'], $_SESSION['username'], decrypt_passwd($_SESSION['password']), $_SESSION['imap_port'], $_SESSION['imap_ssl']))) |
|---|
| 263 | $OUTPUT->show_message('imaperror', 'error'); |
|---|
| 264 | |
|---|
| 265 | rcmail_set_imap_prop(); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | // enable caching of imap data |
|---|
| 269 | if ($CONFIG['enable_caching']===TRUE) |
|---|
| 270 | $IMAP->set_caching(TRUE); |
|---|
| 271 | |
|---|
| 272 | // set pagesize from config |
|---|
| 273 | if (isset($CONFIG['pagesize'])) |
|---|
| 274 | $IMAP->set_pagesize($CONFIG['pagesize']); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | /** |
|---|
| 279 | * Set root dir and last stored mailbox |
|---|
| 280 | * This must be done AFTER connecting to the server! |
|---|
| 281 | */ |
|---|
| 282 | function rcmail_set_imap_prop() |
|---|
| 283 | { |
|---|
| 284 | global $CONFIG, $IMAP; |
|---|
| 285 | |
|---|
| 286 | // set root dir from config |
|---|
| 287 | if (!empty($CONFIG['imap_root'])) |
|---|
| 288 | $IMAP->set_rootdir($CONFIG['imap_root']); |
|---|
| 289 | |
|---|
| 290 | if (is_array($CONFIG['default_imap_folders'])) |
|---|
| 291 | $IMAP->set_default_mailboxes($CONFIG['default_imap_folders']); |
|---|
| 292 | |
|---|
| 293 | if (!empty($_SESSION['mbox'])) |
|---|
| 294 | $IMAP->set_mailbox($_SESSION['mbox']); |
|---|
| 295 | if (isset($_SESSION['page'])) |
|---|
| 296 | $IMAP->set_page($_SESSION['page']); |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | /** |
|---|
| 301 | * Do these things on script shutdown |
|---|
| 302 | */ |
|---|
| 303 | function rcmail_shutdown() |
|---|
| 304 | { |
|---|
| 305 | global $IMAP, $CONTACTS; |
|---|
| 306 | |
|---|
| 307 | if (is_object($IMAP)) |
|---|
| 308 | { |
|---|
| 309 | $IMAP->close(); |
|---|
| 310 | $IMAP->write_cache(); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | if (is_object($CONTACTS)) |
|---|
| 314 | $CONTACTS->close(); |
|---|
| 315 | |
|---|
| 316 | // before closing the database connection, write session data |
|---|
| 317 | session_write_close(); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | /** |
|---|
| 322 | * Destroy session data and remove cookie |
|---|
| 323 | */ |
|---|
| 324 | function rcmail_kill_session() |
|---|
| 325 | { |
|---|
| 326 | global $USER; |
|---|
| 327 | |
|---|
| 328 | if ((isset($_SESSION['sort_col']) && $_SESSION['sort_col']!=$a_user_prefs['message_sort_col']) || |
|---|
| 329 | (isset($_SESSION['sort_order']) && $_SESSION['sort_order']!=$a_user_prefs['message_sort_order'])) |
|---|
| 330 | { |
|---|
| 331 | $a_user_prefs = array('message_sort_col' => $_SESSION['sort_col'], 'message_sort_order' => $_SESSION['sort_order']); |
|---|
| 332 | $USER->save_prefs($a_user_prefs); |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | $_SESSION = array('user_lang' => $GLOBALS['sess_user_lang'], 'auth_time' => time(), 'temp' => true); |
|---|
| 336 | setcookie('sessauth', '-del-', time()-60); |
|---|
| 337 | $USER->reset(); |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | /** |
|---|
| 342 | * Return correct name for a specific database table |
|---|
| 343 | * |
|---|
| 344 | * @param string Table name |
|---|
| 345 | * @return string Translated table name |
|---|
| 346 | */ |
|---|
| 347 | function get_table_name($table) |
|---|
| 348 | { |
|---|
| 349 | global $CONFIG; |
|---|
| 350 | |
|---|
| 351 | // return table name if configured |
|---|
| 352 | $config_key = 'db_table_'.$table; |
|---|
| 353 | |
|---|
| 354 | if (strlen($CONFIG[$config_key])) |
|---|
| 355 | return $CONFIG[$config_key]; |
|---|
| 356 | |
|---|
| 357 | return $table; |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | /** |
|---|
| 362 | * Return correct name for a specific database sequence |
|---|
| 363 | * (used for Postres only) |
|---|
| 364 | * |
|---|
| 365 | * @param string Secuence name |
|---|
| 366 | * @return string Translated sequence name |
|---|
| 367 | */ |
|---|
| 368 | function get_sequence_name($sequence) |
|---|
| 369 | { |
|---|
| 370 | global $CONFIG; |
|---|
| 371 | |
|---|
| 372 | // return table name if configured |
|---|
| 373 | $config_key = 'db_sequence_'.$sequence; |
|---|
| 374 | |
|---|
| 375 | if (strlen($CONFIG[$config_key])) |
|---|
| 376 | return $CONFIG[$config_key]; |
|---|
| 377 | |
|---|
| 378 | return $table; |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | /** |
|---|
| 383 | * Check the given string and returns language properties |
|---|
| 384 | * |
|---|
| 385 | * @param string Language code |
|---|
| 386 | * @param string Peropert name |
|---|
| 387 | * @return string Property value |
|---|
| 388 | */ |
|---|
| 389 | function rcube_language_prop($lang, $prop='lang') |
|---|
| 390 | { |
|---|
| 391 | global $INSTALL_PATH; |
|---|
| 392 | static $rcube_languages, $rcube_language_aliases, $rcube_charsets; |
|---|
| 393 | |
|---|
| 394 | if (empty($rcube_languages)) |
|---|
| 395 | @include($INSTALL_PATH.'program/localization/index.inc'); |
|---|
| 396 | |
|---|
| 397 | // check if we have an alias for that language |
|---|
| 398 | if (!isset($rcube_languages[$lang]) && isset($rcube_language_aliases[$lang])) |
|---|
| 399 | $lang = $rcube_language_aliases[$lang]; |
|---|
| 400 | |
|---|
| 401 | // try the first two chars |
|---|
| 402 | if (!isset($rcube_languages[$lang]) && strlen($lang)>2) |
|---|
| 403 | { |
|---|
| 404 | $lang = substr($lang, 0, 2); |
|---|
| 405 | $lang = rcube_language_prop($lang); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | if (!isset($rcube_languages[$lang])) |
|---|
| 409 | $lang = 'en_US'; |
|---|
| 410 | |
|---|
| 411 | // language has special charset configured |
|---|
| 412 | if (isset($rcube_charsets[$lang])) |
|---|
| 413 | $charset = $rcube_charsets[$lang]; |
|---|
| 414 | else |
|---|
| 415 | $charset = 'UTF-8'; |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | if ($prop=='charset') |
|---|
| 419 | return $charset; |
|---|
| 420 | else |
|---|
| 421 | return $lang; |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | /** |
|---|
| 426 | * Init output object for GUI and add common scripts. |
|---|
| 427 | * This will instantiate a rcmail_template object and set |
|---|
| 428 | * environment vars according to the current session and configuration |
|---|
| 429 | */ |
|---|
| 430 | function rcmail_load_gui() |
|---|
| 431 | { |
|---|
| 432 | global $CONFIG, $OUTPUT, $sess_user_lang; |
|---|
| 433 | |
|---|
| 434 | // init output page |
|---|
| 435 | $OUTPUT = new rcmail_template($CONFIG, $GLOBALS['_task']); |
|---|
| 436 | $OUTPUT->set_env('comm_path', $GLOBALS['COMM_PATH']); |
|---|
| 437 | |
|---|
| 438 | if (is_array($CONFIG['javascript_config'])) |
|---|
| 439 | { |
|---|
| 440 | foreach ($CONFIG['javascript_config'] as $js_config_var) |
|---|
| 441 | $OUTPUT->set_env($js_config_var, $CONFIG[$js_config_var]); |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | if (!empty($GLOBALS['_framed'])) |
|---|
| 445 | $OUTPUT->set_env('framed', true); |
|---|
| 446 | |
|---|
| 447 | // set locale setting |
|---|
| 448 | rcmail_set_locale($sess_user_lang); |
|---|
| 449 | |
|---|
| 450 | // set user-selected charset |
|---|
| 451 | if (!empty($CONFIG['charset'])) |
|---|
| 452 | $OUTPUT->set_charset($CONFIG['charset']); |
|---|
| 453 | |
|---|
| 454 | // register common UI objects |
|---|
| 455 | $OUTPUT->add_handlers(array( |
|---|
| 456 | 'loginform' => 'rcmail_login_form', |
|---|
| 457 | 'username' => 'rcmail_current_username', |
|---|
| 458 | 'message' => 'rcmail_message_container', |
|---|
| 459 | 'charsetselector' => 'rcmail_charset_selector', |
|---|
| 460 | )); |
|---|
| 461 | |
|---|
| 462 | // add some basic label to client |
|---|
| 463 | if (!$OUTPUT->ajax_call) |
|---|
| 464 | rcube_add_label('loading', 'movingmessage'); |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | /** |
|---|
| 469 | * Set localization charset based on the given language. |
|---|
| 470 | * This also creates a global property for mbstring usage. |
|---|
| 471 | */ |
|---|
| 472 | function rcmail_set_locale($lang) |
|---|
| 473 | { |
|---|
| 474 | global $OUTPUT, $MBSTRING; |
|---|
| 475 | static $s_mbstring_loaded = NULL; |
|---|
| 476 | |
|---|
| 477 | // settings for mbstring module (by Tadashi Jokagi) |
|---|
| 478 | if (is_null($s_mbstring_loaded)) |
|---|
| 479 | $MBSTRING = $s_mbstring_loaded = extension_loaded("mbstring"); |
|---|
| 480 | else |
|---|
| 481 | $MBSTRING = $s_mbstring_loaded = FALSE; |
|---|
| 482 | |
|---|
| 483 | if ($MBSTRING) |
|---|
| 484 | mb_internal_encoding(RCMAIL_CHARSET); |
|---|
| 485 | |
|---|
| 486 | $OUTPUT->set_charset(rcube_language_prop($lang, 'charset')); |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | /** |
|---|
| 491 | * Auto-select IMAP host based on the posted login information |
|---|
| 492 | * |
|---|
| 493 | * @return string Selected IMAP host |
|---|
| 494 | */ |
|---|
| 495 | function rcmail_autoselect_host() |
|---|
| 496 | { |
|---|
| 497 | global $CONFIG; |
|---|
| 498 | |
|---|
| 499 | $host = isset($_POST['_host']) ? get_input_value('_host', RCUBE_INPUT_POST) : $CONFIG['default_host']; |
|---|
| 500 | if (is_array($host)) |
|---|
| 501 | { |
|---|
| 502 | list($user, $domain) = explode('@', get_input_value('_user', RCUBE_INPUT_POST)); |
|---|
| 503 | if (!empty($domain)) |
|---|
| 504 | { |
|---|
| 505 | foreach ($host as $imap_host => $mail_domains) |
|---|
| 506 | if (is_array($mail_domains) && in_array($domain, $mail_domains)) |
|---|
| 507 | { |
|---|
| 508 | $host = $imap_host; |
|---|
| 509 | break; |
|---|
| 510 | } |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | // take the first entry if $host is still an array |
|---|
| 514 | if (is_array($host)) |
|---|
| 515 | $host = array_shift($host); |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | return $host; |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | /** |
|---|
| 523 | * Perfom login to the IMAP server and to the webmail service. |
|---|
| 524 | * This will also create a new user entry if auto_create_user is configured. |
|---|
| 525 | * |
|---|
| 526 | * @param string IMAP user name |
|---|
| 527 | * @param string IMAP password |
|---|
| 528 | * @param string IMAP host |
|---|
| 529 | * @return boolean True on success, False on failure |
|---|
| 530 | */ |
|---|
| 531 | function rcmail_login($user, $pass, $host=NULL) |
|---|
| 532 | { |
|---|
| 533 | global $CONFIG, $IMAP, $DB, $USER, $sess_user_lang; |
|---|
| 534 | $user_id = NULL; |
|---|
| 535 | |
|---|
| 536 | if (!$host) |
|---|
| 537 | $host = $CONFIG['default_host']; |
|---|
| 538 | |
|---|
| 539 | // Validate that selected host is in the list of configured hosts |
|---|
| 540 | if (is_array($CONFIG['default_host'])) |
|---|
| 541 | { |
|---|
| 542 | $allowed = FALSE; |
|---|
| 543 | foreach ($CONFIG['default_host'] as $key => $host_allowed) |
|---|
| 544 | { |
|---|
| 545 | if (!is_numeric($key)) |
|---|
| 546 | $host_allowed = $key; |
|---|
| 547 | if ($host == $host_allowed) |
|---|
| 548 | { |
|---|
| 549 | $allowed = TRUE; |
|---|
| 550 | break; |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | if (!$allowed) |
|---|
| 554 | return FALSE; |
|---|
| 555 | } |
|---|
| 556 | else if (!empty($CONFIG['default_host']) && $host != $CONFIG['default_host']) |
|---|
| 557 | return FALSE; |
|---|
| 558 | |
|---|
| 559 | // parse $host URL |
|---|
| 560 | $a_host = parse_url($host); |
|---|
| 561 | if ($a_host['host']) |
|---|
| 562 | { |
|---|
| 563 | $host = $a_host['host']; |
|---|
| 564 | $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE; |
|---|
| 565 | $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : $CONFIG['default_port']); |
|---|
| 566 | } |
|---|
| 567 | else |
|---|
| 568 | $imap_port = $CONFIG['default_port']; |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | /* Modify username with domain if required |
|---|
| 572 | Inspired by Marco <P0L0_notspam_binware.org> |
|---|
| 573 | */ |
|---|
| 574 | // Check if we need to add domain |
|---|
| 575 | if (!empty($CONFIG['username_domain']) && !strpos($user, '@')) |
|---|
| 576 | { |
|---|
| 577 | if (is_array($CONFIG['username_domain']) && isset($CONFIG['username_domain'][$host])) |
|---|
| 578 | $user .= '@'.$CONFIG['username_domain'][$host]; |
|---|
| 579 | else if (is_string($CONFIG['username_domain'])) |
|---|
| 580 | $user .= '@'.$CONFIG['username_domain']; |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | // try to resolve email address from virtuser table |
|---|
| 584 | if (!empty($CONFIG['virtuser_file']) && strpos($user, '@')) |
|---|
| 585 | $user = rcube_user::email2user($user); |
|---|
| 586 | |
|---|
| 587 | // lowercase username if it's an e-mail address (#1484473) |
|---|
| 588 | if (strpos($user, '@')) |
|---|
| 589 | $user = strtolower($user); |
|---|
| 590 | |
|---|
| 591 | // query if user already registered |
|---|
| 592 | if ($existing = rcube_user::query($user, $host)) |
|---|
| 593 | $USER = $existing; |
|---|
| 594 | |
|---|
| 595 | // user already registered -> overwrite username |
|---|
| 596 | if ($USER->ID) |
|---|
| 597 | { |
|---|
| 598 | $user_id = $USER->ID; |
|---|
| 599 | $user = $USER->data['username']; |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | // exit if IMAP login failed |
|---|
| 603 | if (!($imap_login = $IMAP->connect($host, $user, $pass, $imap_port, $imap_ssl))) |
|---|
| 604 | return false; |
|---|
| 605 | |
|---|
| 606 | // user already registered |
|---|
| 607 | if ($USER->ID) |
|---|
| 608 | { |
|---|
| 609 | // get user prefs |
|---|
| 610 | $CONFIG = array_merge($CONFIG, (array)$USER->get_prefs()); |
|---|
| 611 | |
|---|
| 612 | // set user specific language |
|---|
| 613 | if (!empty($USER->data['language'])) |
|---|
| 614 | $sess_user_lang = $_SESSION['user_lang'] = $USER->data['language']; |
|---|
| 615 | |
|---|
| 616 | // update user's record |
|---|
| 617 | $USER->touch(); |
|---|
| 618 | } |
|---|
| 619 | // create new system user |
|---|
| 620 | else if ($CONFIG['auto_create_user']) |
|---|
| 621 | { |
|---|
| 622 | if ($created = rcube_user::create($user, $host)) |
|---|
| 623 | { |
|---|
| 624 | $USER = $created; |
|---|
| 625 | |
|---|
| 626 | // get existing mailboxes |
|---|
| 627 | $a_mailboxes = $IMAP->list_mailboxes(); |
|---|
| 628 | } |
|---|
| 629 | } |
|---|
| 630 | else |
|---|
| 631 | { |
|---|
| 632 | raise_error(array( |
|---|
| 633 | 'code' => 600, |
|---|
| 634 | 'type' => 'php', |
|---|
| 635 | 'file' => "config/main.inc.php", |
|---|
| 636 | 'message' => "Acces denied for new user $user. 'auto_create_user' is disabled" |
|---|
| 637 | ), true, false); |
|---|
| 638 | } |
|---|
| 639 | |
|---|
| 640 | if ($USER->ID) |
|---|
| 641 | { |
|---|
| 642 | $_SESSION['user_id'] = $USER->ID; |
|---|
| 643 | $_SESSION['username'] = $USER->data['username']; |
|---|
| 644 | $_SESSION['imap_host'] = $host; |
|---|
| 645 | $_SESSION['imap_port'] = $imap_port; |
|---|
| 646 | $_SESSION['imap_ssl'] = $imap_ssl; |
|---|
| 647 | $_SESSION['user_lang'] = $sess_user_lang; |
|---|
| 648 | $_SESSION['password'] = encrypt_passwd($pass); |
|---|
| 649 | $_SESSION['login_time'] = mktime(); |
|---|
| 650 | |
|---|
| 651 | // force reloading complete list of subscribed mailboxes |
|---|
| 652 | rcmail_set_imap_prop(); |
|---|
| 653 | $IMAP->clear_cache('mailboxes'); |
|---|
| 654 | |
|---|
| 655 | if ($CONFIG['create_default_folders']) |
|---|
| 656 | $IMAP->create_default_folders(); |
|---|
| 657 | |
|---|
| 658 | return TRUE; |
|---|
| 659 | } |
|---|
| 660 | |
|---|
| 661 | return FALSE; |
|---|
| 662 | } |
|---|
| 663 | |
|---|
| 664 | |
|---|
| 665 | /** |
|---|
| 666 | * Load virtuser table in array |
|---|
| 667 | * |
|---|
| 668 | * @return array Virtuser table entries |
|---|
| 669 | */ |
|---|
| 670 | function rcmail_getvirtualfile() |
|---|
| 671 | { |
|---|
| 672 | global $CONFIG; |
|---|
| 673 | if (empty($CONFIG['virtuser_file']) || !is_file($CONFIG['virtuser_file'])) |
|---|
| 674 | return FALSE; |
|---|
| 675 | |
|---|
| 676 | // read file |
|---|
| 677 | $a_lines = file($CONFIG['virtuser_file']); |
|---|
| 678 | return $a_lines; |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | |
|---|
| 682 | /** |
|---|
| 683 | * Find matches of the given pattern in virtuser table |
|---|
| 684 | * |
|---|
| 685 | * @param string Regular expression to search for |
|---|
| 686 | * @return array Matching entries |
|---|
| 687 | */ |
|---|
| 688 | function rcmail_findinvirtual($pattern) |
|---|
| 689 | { |
|---|
| 690 | $result = array(); |
|---|
| 691 | $virtual = rcmail_getvirtualfile(); |
|---|
| 692 | if ($virtual==FALSE) |
|---|
| 693 | return $result; |
|---|
| 694 | |
|---|
| 695 | // check each line for matches |
|---|
| 696 | foreach ($virtual as $line) |
|---|
| 697 | { |
|---|
| 698 | $line = trim($line); |
|---|
| 699 | if (empty($line) || $line{0}=='#') |
|---|
| 700 | continue; |
|---|
| 701 | |
|---|
| 702 | if (eregi($pattern, $line)) |
|---|
| 703 | $result[] = $line; |
|---|
| 704 | } |
|---|
| 705 | |
|---|
| 706 | return $result; |
|---|
| 707 | } |
|---|
| 708 | |
|---|
| 709 | |
|---|
| 710 | /** |
|---|
| 711 | * Overwrite action variable |
|---|
| 712 | * |
|---|
| 713 | * @param string New action value |
|---|
| 714 | */ |
|---|
| 715 | function rcmail_overwrite_action($action) |
|---|
| 716 | { |
|---|
| 717 | global $OUTPUT; |
|---|
| 718 | $GLOBALS['_action'] = $action; |
|---|
| 719 | $OUTPUT->set_env('action', $action); |
|---|
| 720 | } |
|---|
| 721 | |
|---|
| 722 | |
|---|
| 723 | /** |
|---|
| 724 | * Compose an URL for a specific action |
|---|
| 725 | * |
|---|
| 726 | * @param string Request action |
|---|
| 727 | * @param array More URL parameters |
|---|
| 728 | * @param string Request task (omit if the same) |
|---|
| 729 | * @return The application URL |
|---|
| 730 | */ |
|---|
| 731 | function rcmail_url($action, $p=array(), $task=null) |
|---|
| 732 | { |
|---|
| 733 | global $MAIN_TASKS, $COMM_PATH; |
|---|
| 734 | $qstring = ''; |
|---|
| 735 | $base = $COMM_PATH; |
|---|
| 736 | |
|---|
| 737 | if ($task && in_array($task, $MAIN_TASKS)) |
|---|
| 738 | $base = ereg_replace('_task=[a-z]+', '_task='.$task, $COMM_PATH); |
|---|
| 739 | |
|---|
| 740 | if (is_array($p)) |
|---|
| 741 | foreach ($p as $key => $val) |
|---|
| 742 | $qstring .= '&'.urlencode($key).'='.urlencode($val); |
|---|
| 743 | |
|---|
| 744 | return $base . ($action ? '&_action='.$action : '') . $qstring; |
|---|
| 745 | } |
|---|
| 746 | |
|---|
| 747 | |
|---|
| 748 | // @deprecated |
|---|
| 749 | function show_message($message, $type='notice', $vars=NULL) |
|---|
| 750 | { |
|---|
| 751 | global $OUTPUT; |
|---|
| 752 | $OUTPUT->show_message($message, $type, $vars); |
|---|
| 753 | } |
|---|
| 754 | |
|---|
| 755 | |
|---|
| 756 | /** |
|---|
| 757 | * Encrypt IMAP password using DES encryption |
|---|
| 758 | * |
|---|
| 759 | * @param string Password to encrypt |
|---|
| 760 | * @return string Encryprted string |
|---|
| 761 | */ |
|---|
| 762 | function encrypt_passwd($pass) |
|---|
| 763 | { |
|---|
| 764 | $cypher = des(get_des_key(), $pass, 1, 0, NULL); |
|---|
| 765 | return base64_encode($cypher); |
|---|
| 766 | } |
|---|
| 767 | |
|---|
| 768 | |
|---|
| 769 | /** |
|---|
| 770 | * Decrypt IMAP password using DES encryption |
|---|
| 771 | * |
|---|
| 772 | * @param string Encrypted password |
|---|
| 773 | * @return string Plain password |
|---|
| 774 | */ |
|---|
| 775 | function decrypt_passwd($cypher) |
|---|
| 776 | { |
|---|
| 777 | $pass = des(get_des_key(), base64_decode($cypher), 0, 0, NULL); |
|---|
| 778 | return preg_replace('/\x00/', '', $pass); |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | |
|---|
| 782 | /** |
|---|
| 783 | * Return a 24 byte key for the DES encryption |
|---|
| 784 | * |
|---|
| 785 | * @return string DES encryption key |
|---|
| 786 | */ |
|---|
| 787 | function get_des_key() |
|---|
| 788 | { |
|---|
| 789 | $key = !empty($GLOBALS['CONFIG']['des_key']) ? $GLOBALS['CONFIG']['des_key'] : 'rcmail?24BitPwDkeyF**ECB'; |
|---|
| 790 | $len = strlen($key); |
|---|
| 791 | |
|---|
| 792 | // make sure the key is exactly 24 chars long |
|---|
| 793 | if ($len<24) |
|---|
| 794 | $key .= str_repeat('_', 24-$len); |
|---|
| 795 | else if ($len>24) |
|---|
| 796 | substr($key, 0, 24); |
|---|
| 797 | |
|---|
| 798 | return $key; |
|---|
| 799 | } |
|---|
| 800 | |
|---|
| 801 | |
|---|
| 802 | /** |
|---|
| 803 | * Read directory program/localization and return a list of available languages |
|---|
| 804 | * |
|---|
| 805 | * @return array List of available localizations |
|---|
| 806 | */ |
|---|
| 807 | function rcube_list_languages() |
|---|
| 808 | { |
|---|
| 809 | global $CONFIG, $INSTALL_PATH; |
|---|
| 810 | static $sa_languages = array(); |
|---|
| 811 | |
|---|
| 812 | if (!sizeof($sa_languages)) |
|---|
| 813 | { |
|---|
| 814 | @include($INSTALL_PATH.'program/localization/index.inc'); |
|---|
| 815 | |
|---|
| 816 | if ($dh = @opendir($INSTALL_PATH.'program/localization')) |
|---|
| 817 | { |
|---|
| 818 | while (($name = readdir($dh)) !== false) |
|---|
| 819 | { |
|---|
| 820 | if ($name{0}=='.' || !is_dir($INSTALL_PATH.'program/localization/'.$name)) |
|---|
| 821 | continue; |
|---|
| 822 | |
|---|
| 823 | if ($label = $rcube_languages[$name]) |
|---|
| 824 | $sa_languages[$name] = $label ? $label : $name; |
|---|
| 825 | } |
|---|
| 826 | closedir($dh); |
|---|
| 827 | } |
|---|
| 828 | } |
|---|
| 829 | return $sa_languages; |
|---|
| 830 | } |
|---|
| 831 | |
|---|
| 832 | |
|---|
| 833 | /** |
|---|
| 834 | * Add a localized label to the client environment |
|---|
| 835 | */ |
|---|
| 836 | function rcube_add_label() |
|---|
| 837 | { |
|---|
| 838 | global $OUTPUT; |
|---|
| 839 | |
|---|
| 840 | $arg_list = func_get_args(); |
|---|
| 841 | foreach ($arg_list as $i => $name) |
|---|
| 842 | $OUTPUT->command('add_label', $name, rcube_label($name)); |
|---|
| 843 | } |
|---|
| 844 | |
|---|
| 845 | |
|---|
| 846 | /** |
|---|
| 847 | * Garbage collector function for temp files. |
|---|
| 848 | * Remove temp files older than two days |
|---|
| 849 | */ |
|---|
| 850 | function rcmail_temp_gc() |
|---|
| 851 | { |
|---|
| 852 | $tmp = unslashify($CONFIG['temp_dir']); |
|---|
| 853 | $expire = mktime() - 172800; // expire in 48 hours |
|---|
| 854 | |
|---|
| 855 | if ($dir = opendir($tmp)) |
|---|
| 856 | { |
|---|
| 857 | while (($fname = readdir($dir)) !== false) |
|---|
| 858 | { |
|---|
| 859 | if ($fname{0} == '.') |
|---|
| 860 | continue; |
|---|
| 861 | |
|---|
| 862 | if (filemtime($tmp.'/'.$fname) < $expire) |
|---|
| 863 | @unlink($tmp.'/'.$fname); |
|---|
| 864 | } |
|---|
| 865 | |
|---|
| 866 | closedir($dir); |
|---|
| 867 | } |
|---|
| 868 | } |
|---|
| 869 | |
|---|
| 870 | |
|---|
| 871 | /** |
|---|
| 872 | * Garbage collector for cache entries. |
|---|
| 873 | * Remove all expired message cache records |
|---|
| 874 | */ |
|---|
| 875 | function rcmail_message_cache_gc() |
|---|
| 876 | { |
|---|
| 877 | global $DB, $CONFIG; |
|---|
| 878 | |
|---|
| 879 | // no cache lifetime configured |
|---|
| 880 | if (empty($CONFIG['message_cache_lifetime'])) |
|---|
| 881 | return; |
|---|
| 882 | |
|---|
| 883 | // get target timestamp |
|---|
| 884 | $ts = get_offset_time($CONFIG['message_cache_lifetime'], -1); |
|---|
| 885 | |
|---|
| 886 | $DB->query("DELETE FROM ".get_table_name('messages')." |
|---|
| 887 | WHERE created < ".$DB->fromunixtime($ts)); |
|---|
| 888 | } |
|---|
| 889 | |
|---|
| 890 | |
|---|
| 891 | /** |
|---|
| 892 | * Convert a string from one charset to another. |
|---|
| 893 | * Uses mbstring and iconv functions if possible |
|---|
| 894 | * |
|---|
| 895 | * @param string Input string |
|---|
| 896 | * @param string Suspected charset of the input string |
|---|
| 897 | * @param string Target charset to convert to; defaults to RCMAIL_CHARSET |
|---|
| 898 | * @return Converted string |
|---|
| 899 | */ |
|---|
| 900 | function rcube_charset_convert($str, $from, $to=NULL) |
|---|
| 901 | { |
|---|
| 902 | global $MBSTRING; |
|---|
| 903 | |
|---|
| 904 | $from = strtoupper($from); |
|---|
| 905 | $to = $to==NULL ? strtoupper(RCMAIL_CHARSET) : strtoupper($to); |
|---|
| 906 | |
|---|
| 907 | if ($from==$to || $str=='' || empty($from)) |
|---|
| 908 | return $str; |
|---|
| 909 | |
|---|
| 910 | // convert charset using iconv module |
|---|
| 911 | if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') |
|---|
| 912 | { |
|---|
| 913 | $iconv_map = array('KS_C_5601-1987' => 'EUC-KR'); |
|---|
| 914 | return iconv(($iconv_map[$from] ? $iconv_map[$from] : $from), ($iconv_map[$to] ? $iconv_map[$to] : $to) . "//IGNORE", $str); |
|---|
| 915 | } |
|---|
| 916 | |
|---|
| 917 | // convert charset using mbstring module |
|---|
| 918 | if ($MBSTRING) |
|---|
| 919 | { |
|---|
| 920 | $mb_map = array('UTF-7' => 'UTF7-IMAP', 'KS_C_5601-1987' => 'EUC-KR'); |
|---|
| 921 | |
|---|
| 922 | // return if convert succeeded |
|---|
| 923 | if (($out = mb_convert_encoding($str, ($mb_map[$to] ? $mb_map[$to] : $to), ($mb_map[$from] ? $mb_map[$from] : $from))) != '') |
|---|
| 924 | return $out; |
|---|
| 925 | } |
|---|
| 926 | |
|---|
| 927 | $conv = new utf8(); |
|---|
| 928 | |
|---|
| 929 | // convert string to UTF-8 |
|---|
| 930 | if ($from=='UTF-7') |
|---|
| 931 | $str = utf7_to_utf8($str); |
|---|
| 932 | else if (($from=='ISO-8859-1') && function_exists('utf8_encode')) |
|---|
| 933 | $str = utf8_encode($str); |
|---|
| 934 | else if ($from!='UTF-8') |
|---|
| 935 | { |
|---|
| 936 | $conv->loadCharset($from); |
|---|
| 937 | $str = $conv->strToUtf8($str); |
|---|
| 938 | } |
|---|
| 939 | |
|---|
| 940 | // encode string for output |
|---|
| 941 | if ($to=='UTF-7') |
|---|
| 942 | return utf8_to_utf7($str); |
|---|
| 943 | else if ($to=='ISO-8859-1' && function_exists('utf8_decode')) |
|---|
| 944 | return utf8_decode($str); |
|---|
| 945 | else if ($to!='UTF-8') |
|---|
| 946 | { |
|---|
| 947 | $conv->loadCharset($to); |
|---|
| 948 | return $conv->utf8ToStr($str); |
|---|
| 949 | } |
|---|
| 950 | |
|---|
| 951 | // return UTF-8 string |
|---|
| 952 | return $str; |
|---|
| 953 | } |
|---|
| 954 | |
|---|
| 955 | |
|---|
| 956 | /** |
|---|
| 957 | * Replacing specials characters to a specific encoding type |
|---|
| 958 | * |
|---|
| 959 | * @param string Input string |
|---|
| 960 | * @param string Encoding type: text|html|xml|js|url |
|---|
| 961 | * @param string Replace mode for tags: show|replace|remove |
|---|
| 962 | * @param boolean Convert newlines |
|---|
| 963 | * @return The quoted string |
|---|
| 964 | */ |
|---|
| 965 | function rep_specialchars_output($str, $enctype='', $mode='', $newlines=TRUE) |
|---|
| 966 | { |
|---|
| 967 | global $OUTPUT_TYPE, $OUTPUT; |
|---|
| 968 | static $html_encode_arr, $js_rep_table, $xml_rep_table; |
|---|
| 969 | |
|---|
| 970 | if (!$enctype) |
|---|
| 971 | $enctype = $GLOBALS['OUTPUT_TYPE']; |
|---|
| 972 | |
|---|
| 973 | // encode for plaintext |
|---|
| 974 | if ($enctype=='text') |
|---|
| 975 | return str_replace("\r\n", "\n", $mode=='remove' ? strip_tags($str) : $str); |
|---|
| 976 | |
|---|
| 977 | // encode for HTML output |
|---|
| 978 | if ($enctype=='html') |
|---|
| 979 | { |
|---|
| 980 | if (!$html_encode_arr) |
|---|
| 981 | { |
|---|
| 982 | $html_encode_arr = get_html_translation_table(HTML_SPECIALCHARS); |
|---|
| 983 | unset($html_encode_arr['?']); |
|---|
| 984 | } |
|---|
| 985 | |
|---|
| 986 | $ltpos = strpos($str, '<'); |
|---|
| 987 | $encode_arr = $html_encode_arr; |
|---|
| 988 | |
|---|
| 989 | // don't replace quotes and html tags |
|---|
| 990 | if (($mode=='show' || $mode=='') && $ltpos!==false && strpos($str, '>', $ltpos)!==false) |
|---|
| 991 | { |
|---|
| 992 | unset($encode_arr['"']); |
|---|
| 993 | unset($encode_arr['<']); |
|---|
| 994 | unset($encode_arr['>']); |
|---|
| 995 | unset($encode_arr['&']); |
|---|
| 996 | } |
|---|
| 997 | else if ($mode=='remove') |
|---|
| 998 | $str = strip_tags($str); |
|---|
| 999 | |
|---|
| 1000 | // avoid douple quotation of & |
|---|
| 1001 | $out = preg_replace('/&([a-z]{2,5}|#[0-9]{2,4});/', '&\\1;', strtr($str, $encode_arr)); |
|---|
| 1002 | |
|---|
| 1003 | return $newlines ? nl2br($out) : $out; |
|---|
| 1004 | } |
|---|
| 1005 | |
|---|
| 1006 | if ($enctype=='url') |
|---|
| 1007 | return rawurlencode($str); |
|---|
| 1008 | |
|---|
| 1009 | // if the replace tables for XML and JS are not yet defined |
|---|
| 1010 | if (!$js_rep_table) |
|---|
| 1011 | { |
|---|
| 1012 | $js_rep_table = $xml_rep_table = array(); |
|---|
| 1013 | $xml_rep_table['&'] = '&'; |
|---|
| 1014 | |
|---|
| 1015 | for ($c=160; $c<256; $c++) // can be increased to support more charsets |
|---|
| 1016 | { |
|---|
| 1017 | $xml_rep_table[Chr($c)] = "&#$c;"; |
|---|
| 1018 | |
|---|
| 1019 | if ($OUTPUT->get_charset()=='ISO-8859-1') |
|---|
| 1020 | $js_rep_table[Chr($c)] = sprintf("\\u%04x", $c); |
|---|
| 1021 | } |
|---|
| 1022 | |
|---|
| 1023 | $xml_rep_table['"'] = '"'; |
|---|
| 1024 | } |
|---|
| 1025 | |
|---|
| 1026 | // encode for XML |
|---|
| 1027 | if ($enctype=='xml') |
|---|
| 1028 | return strtr($str, $xml_rep_table); |
|---|
| 1029 | |
|---|
| 1030 | // encode for javascript use |
|---|
| 1031 | if ($enctype=='js') |
|---|
| 1032 | { |
|---|
| 1033 | if ($OUTPUT->get_charset()!='UTF-8') |
|---|
| 1034 | $str = rcube_charset_convert($str, RCMAIL_CHARSET, $OUTPUT->get_charset()); |
|---|
| 1035 | |
|---|
| 1036 | return preg_replace(array("/\r?\n/", "/\r/"), array('\n', '\n'), addslashes(strtr($str, $js_rep_table))); |
|---|
| 1037 | } |
|---|
| 1038 | |
|---|
| 1039 | // no encoding given -> return original string |
|---|
| 1040 | return $str; |
|---|
| 1041 | } |
|---|
| 1042 | |
|---|
| 1043 | /** |
|---|
| 1044 | * Quote a given string. |
|---|
| 1045 | * Shortcut function for rep_specialchars_output |
|---|
| 1046 | * |
|---|
| 1047 | * @return string HTML-quoted string |
|---|
| 1048 | * @see rep_specialchars_output() |
|---|
| 1049 | */ |
|---|
| 1050 | function Q($str, $mode='strict', $newlines=TRUE) |
|---|
| 1051 | { |
|---|
| 1052 | return rep_specialchars_output($str, 'html', $mode, $newlines); |
|---|
| 1053 | } |
|---|
| 1054 | |
|---|
| 1055 | /** |
|---|
| 1056 | * Quote a given string for javascript output. |
|---|
| 1057 | * Shortcut function for rep_specialchars_output |
|---|
| 1058 | * |
|---|
| 1059 | * @return string JS-quoted string |
|---|
| 1060 | * @see rep_specialchars_output() |
|---|
| 1061 | */ |
|---|
| 1062 | function JQ($str) |
|---|
| 1063 | { |
|---|
| 1064 | return rep_specialchars_output($str, 'js'); |
|---|
| 1065 | } |
|---|
| 1066 | |
|---|
| 1067 | |
|---|
| 1068 | /** |
|---|
| 1069 | * Read input value and convert it for internal use |
|---|
| 1070 | * Performs stripslashes() and charset conversion if necessary |
|---|
| 1071 | * |
|---|
| 1072 | * @param string Field name to read |
|---|
| 1073 | * @param int Source to get value from (GPC) |
|---|
| 1074 | * @param boolean Allow HTML tags in field value |
|---|
| 1075 | * @param string Charset to convert into |
|---|
| 1076 | * @return string Field value or NULL if not available |
|---|
| 1077 | */ |
|---|
| 1078 | function get_input_value($fname, $source, $allow_html=FALSE, $charset=NULL) |
|---|
| 1079 | { |
|---|
| 1080 | global $OUTPUT; |
|---|
| 1081 | $value = NULL; |
|---|
| 1082 | |
|---|
| 1083 | if ($source==RCUBE_INPUT_GET && isset($_GET[$fname])) |
|---|
| 1084 | $value = $_GET[$fname]; |
|---|
| 1085 | else if ($source==RCUBE_INPUT_POST && isset($_POST[$fname])) |
|---|
| 1086 | $value = $_POST[$fname]; |
|---|
| 1087 | else if ($source==RCUBE_INPUT_GPC) |
|---|
| 1088 | { |
|---|
| 1089 | if (isset($_POST[$fname])) |
|---|
| 1090 | $value = $_POST[$fname]; |
|---|
| 1091 | else if (isset($_GET[$fname])) |
|---|
| 1092 | $value = $_GET[$fname]; |
|---|
| 1093 | else if (isset($_COOKIE[$fname])) |
|---|
| 1094 | $value = $_COOKIE[$fname]; |
|---|
| 1095 | } |
|---|
| 1096 | |
|---|
| 1097 | // strip slashes if magic_quotes enabled |
|---|
| 1098 | if ((bool)get_magic_quotes_gpc()) |
|---|
| 1099 | $value = stripslashes($value); |
|---|
| 1100 | |
|---|
| 1101 | // remove HTML tags if not allowed |
|---|
| 1102 | if (!$allow_html) |
|---|
| 1103 | $value = strip_tags($value); |
|---|
| 1104 | |
|---|
| 1105 | // convert to internal charset |
|---|
| 1106 | if (is_object($OUTPUT)) |
|---|
| 1107 | return rcube_charset_convert($value, $OUTPUT->get_charset(), $charset); |
|---|
| 1108 | else |
|---|
| 1109 | return $value; |
|---|
| 1110 | } |
|---|
| 1111 | |
|---|
| 1112 | /** |
|---|
| 1113 | * Remove all non-ascii and non-word chars |
|---|
| 1114 | * except . and - |
|---|
| 1115 | */ |
|---|
| 1116 | function asciiwords($str) |
|---|
| 1117 | { |
|---|
| 1118 | return preg_replace('/[^a-z0-9.-_]/i', '', $str); |
|---|
| 1119 | } |
|---|
| 1120 | |
|---|
| 1121 | /** |
|---|
| 1122 | * Remove single and double quotes from given string |
|---|
| 1123 | * |
|---|
| 1124 | * @param string Input value |
|---|
| 1125 | * @return string Dequoted string |
|---|
| 1126 | */ |
|---|
| 1127 | function strip_quotes($str) |
|---|
| 1128 | { |
|---|
| 1129 | return preg_replace('/[\'"]/', '', $str); |
|---|
| 1130 | } |
|---|
| 1131 | |
|---|
| 1132 | |
|---|
| 1133 | /** |
|---|
| 1134 | * Remove new lines characters from given string |
|---|
| 1135 | * |
|---|
| 1136 | * @param string Input value |
|---|
| 1137 | * @return string Stripped string |
|---|
| 1138 | */ |
|---|
| 1139 | function strip_newlines($str) |
|---|
| 1140 | { |
|---|
| 1141 | return preg_replace('/[\r\n]/', '', $str); |
|---|
| 1142 | } |
|---|
| 1143 | |
|---|
| 1144 | |
|---|
| 1145 | /** |
|---|
| 1146 | * Check if a specific template exists |
|---|
| 1147 | * |
|---|
| 1148 | * @param string Template name |
|---|
| 1149 | * @return boolean True if template exists |
|---|
| 1150 | */ |
|---|
| 1151 | function template_exists($name) |
|---|
| 1152 | { |
|---|
| 1153 | global $CONFIG; |
|---|
| 1154 | $skin_path = $CONFIG['skin_path']; |
|---|
| 1155 | |
|---|
| 1156 | // check template file |
|---|
| 1157 | return is_file("$skin_path/templates/$name.html"); |
|---|
| 1158 | } |
|---|
| 1159 | |
|---|
| 1160 | |
|---|
| 1161 | /** |
|---|
| 1162 | * Wrapper for rcmail_template::parse() |
|---|
| 1163 | * @deprecated |
|---|
| 1164 | */ |
|---|
| 1165 | function parse_template($name='main', $exit=true) |
|---|
| 1166 | { |
|---|
| 1167 | $GLOBALS['OUTPUT']->parse($name, $exit); |
|---|
| 1168 | } |
|---|
| 1169 | |
|---|
| 1170 | |
|---|
| 1171 | /** |
|---|
| 1172 | * Create a HTML table based on the given data |
|---|
| 1173 | * |
|---|
| 1174 | * @param array Named table attributes |
|---|
| 1175 | * @param mixed Table row data. Either a two-dimensional array or a valid SQL result set |
|---|
| 1176 | * @param array List of cols to show |
|---|
| 1177 | * @param string Name of the identifier col |
|---|
| 1178 | * @return string HTML table code |
|---|
| 1179 | */ |
|---|
| 1180 | function rcube_table_output($attrib, $table_data, $a_show_cols, $id_col) |
|---|
| 1181 | { |
|---|
| 1182 | global $DB; |
|---|
| 1183 | |
|---|
| 1184 | // allow the following attributes to be added to the <table> tag |
|---|
| 1185 | $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary')); |
|---|
| 1186 | |
|---|
| 1187 | $table = '<table' . $attrib_str . ">\n"; |
|---|
| 1188 | |
|---|
| 1189 | // add table title |
|---|
| 1190 | $table .= "<thead><tr>\n"; |
|---|
| 1191 | |
|---|
| 1192 | foreach ($a_show_cols as $col) |
|---|
| 1193 | $table .= '<td class="'.$col.'">' . Q(rcube_label($col)) . "</td>\n"; |
|---|
| 1194 | |
|---|
| 1195 | $table .= "</tr></thead>\n<tbody>\n"; |
|---|
| 1196 | |
|---|
| 1197 | $c = 0; |
|---|
| 1198 | if (!is_array($table_data)) |
|---|
| 1199 | { |
|---|
| 1200 | while ($table_data && ($sql_arr = $DB->fetch_assoc($table_data))) |
|---|
| 1201 | { |
|---|
| 1202 | $zebra_class = $c%2 ? 'even' : 'odd'; |
|---|
| 1203 | |
|---|
| 1204 | $table .= sprintf('<tr id="rcmrow%d" class="contact '.$zebra_class.'">'."\n", $sql_arr[$id_col]); |
|---|
| 1205 | |
|---|
| 1206 | // format each col |
|---|
| 1207 | foreach ($a_show_cols as $col) |
|---|
| 1208 | { |
|---|
| 1209 | $cont = Q($sql_arr[$col]); |
|---|
| 1210 | $table .= '<td class="'.$col.'">' . $cont . "</td>\n"; |
|---|
| 1211 | } |
|---|
| 1212 | |
|---|
| 1213 | $table .= "</tr>\n"; |
|---|
| 1214 | $c++; |
|---|
| 1215 | } |
|---|
| 1216 | } |
|---|
| 1217 | else |
|---|
| 1218 | { |
|---|
| 1219 | foreach ($table_data as $row_data) |
|---|
| 1220 | { |
|---|
| 1221 | $zebra_class = $c%2 ? 'even' : 'odd'; |
|---|
| 1222 | |
|---|
| 1223 | $table .= sprintf('<tr id="rcmrow%d" class="contact '.$zebra_class.'">'."\n", $row_data[$id_col]); |
|---|
| 1224 | |
|---|
| 1225 | // format each col |
|---|
| 1226 | foreach ($a_show_cols as $col) |
|---|
| 1227 | { |
|---|
| 1228 | $cont = Q($row_data[$col]); |
|---|
| 1229 | $table .= '<td class="'.$col.'">' . $cont . "</td>\n"; |
|---|
| 1230 | } |
|---|
| 1231 | |
|---|
| 1232 | $table .= "</tr>\n"; |
|---|
| 1233 | $c++; |
|---|
| 1234 | } |
|---|
| 1235 | } |
|---|
| 1236 | |
|---|
| 1237 | // complete message table |
|---|
| 1238 | $table .= "</tbody></table>\n"; |
|---|
| 1239 | |
|---|
| 1240 | return $table; |
|---|
| 1241 | } |
|---|
| 1242 | |
|---|
| 1243 | |
|---|
| 1244 | /** |
|---|
| 1245 | * Create an edit field for inclusion on a form |
|---|
| 1246 | * |
|---|
| 1247 | * @param string col field name |
|---|
| 1248 | * @param string value field value |
|---|
| 1249 | * @param array attrib HTML element attributes for field |
|---|
| 1250 | * @param string type HTML element type (default 'text') |
|---|
| 1251 | * @return string HTML field definition |
|---|
| 1252 | */ |
|---|
| 1253 | function rcmail_get_edit_field($col, $value, $attrib, $type='text') |
|---|
| 1254 | { |
|---|
| 1255 | $fname = '_'.$col; |
|---|
| 1256 | $attrib['name'] = $fname; |
|---|
| 1257 | |
|---|
| 1258 | if ($type=='checkbox') |
|---|
| 1259 | { |
|---|
| 1260 | $attrib['value'] = '1'; |
|---|
| 1261 | $input = new checkbox($attrib); |
|---|
| 1262 | } |
|---|
| 1263 | else if ($type=='textarea') |
|---|
| 1264 | { |
|---|
| 1265 | $attrib['cols'] = $attrib['size']; |
|---|
| 1266 | $input = new textarea($attrib); |
|---|
| 1267 | } |
|---|
| 1268 | else |
|---|
| 1269 | $input = new textfield($attrib); |
|---|
| 1270 | |
|---|
| 1271 | // use value from post |
|---|
| 1272 | if (!empty($_POST[$fname])) |
|---|
| 1273 | $value = get_input_value($fname, RCUBE_INPUT_POST); |
|---|
| 1274 | |
|---|
| 1275 | $out = $input->show($value); |
|---|
| 1276 | |
|---|
| 1277 | return $out; |
|---|
| 1278 | } |
|---|
| 1279 | |
|---|
| 1280 | |
|---|
| 1281 | /** |
|---|
| 1282 | * Return the mail domain configured for the given host |
|---|
| 1283 | * |
|---|
| 1284 | * @param string IMAP host |
|---|
| 1285 | * @return string Resolved SMTP host |
|---|
| 1286 | */ |
|---|
| 1287 | function rcmail_mail_domain($host) |
|---|
| 1288 | { |
|---|
| 1289 | global $CONFIG; |
|---|
| 1290 | |
|---|
| 1291 | $domain = $host; |
|---|
| 1292 | if (is_array($CONFIG['mail_domain'])) |
|---|
| 1293 | { |
|---|
| 1294 | if (isset($CONFIG['mail_domain'][$host])) |
|---|
| 1295 | $domain = $CONFIG['mail_domain'][$host]; |
|---|
| 1296 | } |
|---|
| 1297 | else if (!empty($CONFIG['mail_domain'])) |
|---|
| 1298 | $domain = $CONFIG['mail_domain']; |
|---|
| 1299 | |
|---|
| 1300 | return $domain; |
|---|
| 1301 | } |
|---|
| 1302 | |
|---|
| 1303 | |
|---|
| 1304 | /** |
|---|
| 1305 | * Replace all css definitions with #container [def] |
|---|
| 1306 | * |
|---|
| 1307 | * @param string CSS source code |
|---|
| 1308 | * @param string Container ID to use as prefix |
|---|
| 1309 | * @return string Modified CSS source |
|---|
| 1310 | */ |
|---|
| 1311 | function rcmail_mod_css_styles($source, $container_id, $base_url = '') |
|---|
| 1312 | { |
|---|
| 1313 | $a_css_values = array(); |
|---|
| 1314 | $last_pos = 0; |
|---|
| 1315 | |
|---|
| 1316 | // cut out all contents between { and } |
|---|
| 1317 | while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos))) |
|---|
| 1318 | { |
|---|
| 1319 | $key = sizeof($a_css_values); |
|---|
| 1320 | $a_css_values[$key] = substr($source, $pos+1, $pos2-($pos+1)); |
|---|
| 1321 | $source = substr($source, 0, $pos+1) . "<<str_replacement[$key]>>" . substr($source, $pos2, strlen($source)-$pos2); |
|---|
| 1322 | $last_pos = $pos+2; |
|---|
| 1323 | } |
|---|
| 1324 | |
|---|
| 1325 | // remove html commends and add #container to each tag selector. |
|---|
| 1326 | // also replace body definition because we also stripped off the <body> tag |
|---|
| 1327 | $styles = preg_replace( |
|---|
| 1328 | array( |
|---|
| 1329 | '/(^\s*<!--)|(-->\s*$)/', |
|---|
| 1330 | '/(^\s*|,\s*|\}\s*)([a-z0-9\._#][a-z0-9\.\-_]*)/im', |
|---|
| 1331 | '/@import\s+(url\()?[\'"]?([^\)\'"]+)[\'"]?(\))?/ime', |
|---|
| 1332 | '/<<str_replacement\[([0-9]+)\]>>/e', |
|---|
| 1333 | "/$container_id\s+body/i" |
|---|
| 1334 | ), |
|---|
| 1335 | array( |
|---|
| 1336 | '', |
|---|
| 1337 | "\\1#$container_id \\2", |
|---|
| 1338 | "sprintf(\"@import url('./bin/modcss.php?u=%s&c=%s')\", urlencode(make_absolute_url('\\2','$base_url')), urlencode($container_id))", |
|---|
| 1339 | "\$a_css_values[\\1]", |
|---|
| 1340 | "$container_id div.rcmBody" |
|---|
| 1341 | ), |
|---|
| 1342 | $source); |
|---|
| 1343 | |
|---|
| 1344 | return $styles; |
|---|
| 1345 | } |
|---|
| 1346 | |
|---|
| 1347 | /** |
|---|
| 1348 | * Try to autodetect operating system and find the correct line endings |
|---|
| 1349 | * |
|---|
| 1350 | * @return string The appropriate mail header delimiter |
|---|
| 1351 | */ |
|---|
| 1352 | function rcmail_header_delm() |
|---|
| 1353 | { |
|---|
| 1354 | global $CONFIG; |
|---|
| 1355 | |
|---|
| 1356 | // use the configured delimiter for headers |
|---|
| 1357 | if (!empty($CONFIG['mail_header_delimiter'])) |
|---|
| 1358 | return $CONFIG['mail_header_delimiter']; |
|---|
| 1359 | else if (strtolower(substr(PHP_OS, 0, 3)=='win')) |
|---|
| 1360 | return "\r\n"; |
|---|
| 1361 | else if (strtolower(substr(PHP_OS, 0, 3)=='mac')) |
|---|
| 1362 | return "\r\n"; |
|---|
| 1363 | else |
|---|
| 1364 | return "\n"; |
|---|
| 1365 | } |
|---|
| 1366 | |
|---|
| 1367 | |
|---|
| 1368 | /** |
|---|
| 1369 | * Compose a valid attribute string for HTML tags |
|---|
| 1370 | * |
|---|
| 1371 | * @param array Named tag attributes |
|---|
| 1372 | * @param array List of allowed attributes |
|---|
| 1373 | * @return string HTML formatted attribute string |
|---|
| 1374 | */ |
|---|
| 1375 | function create_attrib_string($attrib, $allowed_attribs=array('id', 'class', 'style')) |
|---|
| 1376 | { |
|---|
| 1377 | // allow the following attributes to be added to the <iframe> tag |
|---|
| 1378 | $attrib_str = ''; |
|---|
| 1379 | foreach ($allowed_attribs as $a) |
|---|
| 1380 | if (isset($attrib[$a])) |
|---|
| 1381 | $attrib_str .= sprintf(' %s="%s"', $a, str_replace('"', '"', $attrib[$a])); |
|---|
| 1382 | |
|---|
| 1383 | return $attrib_str; |
|---|
| 1384 | } |
|---|
| 1385 | |
|---|
| 1386 | |
|---|
| 1387 | /** |
|---|
| 1388 | * Convert a HTML attribute string attributes to an associative array (name => value) |
|---|
| 1389 | * |
|---|
| 1390 | * @param string Input string |
|---|
| 1391 | * @return array Key-value pairs of parsed attributes |
|---|
| 1392 | */ |
|---|
| 1393 | function parse_attrib_string($str) |
|---|
| 1394 | { |
|---|
| 1395 | $attrib = array(); |
|---|
| 1396 | preg_match_all('/\s*([-_a-z]+)=(["\'])([^"]+)\2/Ui', stripslashes($str), $regs, PREG_SET_ORDER); |
|---|
| 1397 | |
|---|
| 1398 | // convert attributes to an associative array (name => value) |
|---|
| 1399 | if ($regs) |
|---|
| 1400 | foreach ($regs as $attr) |
|---|
| 1401 | $attrib[strtolower($attr[1])] = $attr[3]; |
|---|
| 1402 | |
|---|
| 1403 | return $attrib; |
|---|
| 1404 | } |
|---|
| 1405 | |
|---|
| 1406 | |
|---|
| 1407 | /** |
|---|
| 1408 | * Convert the given date to a human readable form |
|---|
| 1409 | * This uses the date formatting properties from config |
|---|
| 1410 | * |
|---|
| 1411 | * @param mixed Date representation (string or timestamp) |
|---|
| 1412 | * @param string Date format to use |
|---|
| 1413 | * @return string Formatted date string |
|---|
| 1414 | */ |
|---|
| 1415 | function format_date($date, $format=NULL) |
|---|
| 1416 | { |
|---|
| 1417 | global $CONFIG, $sess_user_lang; |
|---|
| 1418 | |
|---|
| 1419 | $ts = NULL; |
|---|
| 1420 | |
|---|
| 1421 | if (is_numeric($date)) |
|---|
| 1422 | $ts = $date; |
|---|
| 1423 | else if (!empty($date)) |
|---|
| 1424 | $ts = @strtotime($date); |
|---|
| 1425 | |
|---|
| 1426 | if (empty($ts)) |
|---|
| 1427 | return ''; |
|---|
| 1428 | |
|---|
| 1429 | // get user's timezone |
|---|
| 1430 | $tz = $CONFIG['timezone']; |
|---|
| 1431 | if ($CONFIG['dst_active']) |
|---|
| 1432 | $tz++; |
|---|
| 1433 | |
|---|
| 1434 | // convert time to user's timezone |
|---|
| 1435 | $timestamp = $ts - date('Z', $ts) + ($tz * 3600); |
|---|
| 1436 | |
|---|
| 1437 | // get current timestamp in user's timezone |
|---|
| 1438 | $now = time(); // local time |
|---|
| 1439 | $now -= (int)date('Z'); // make GMT time |
|---|
| 1440 | $now += ($tz * 3600); // user's time |
|---|
| 1441 | $now_date = getdate($now); |
|---|
| 1442 | |
|---|
| 1443 | $today_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday'], $now_date['year']); |
|---|
| 1444 | $week_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday']-6, $now_date['year']); |
|---|
| 1445 | |
|---|
| 1446 | // define date format depending on current time |
|---|
| 1447 | if ($CONFIG['prettydate'] && !$format && $timestamp > $today_limit && $timestamp < $now) |
|---|
| 1448 | return sprintf('%s %s', rcube_label('today'), date($CONFIG['date_today'] ? $CONFIG['date_today'] : 'H:i', $timestamp)); |
|---|
| 1449 | else if ($CONFIG['prettydate'] && !$format && $timestamp > $week_limit && $timestamp < $now) |
|---|
| 1450 | $format = $CONFIG['date_short'] ? $CONFIG['date_short'] : 'D H:i'; |
|---|
| 1451 | else if (!$format) |
|---|
| 1452 | $format = $CONFIG['date_long'] ? $CONFIG['date_long'] : 'd.m.Y H:i'; |
|---|
| 1453 | |
|---|
| 1454 | |
|---|
| 1455 | // parse format string manually in order to provide localized weekday and month names |
|---|
| 1456 | // an alternative would be to convert the date() format string to fit with strftime() |
|---|
| 1457 | $out = ''; |
|---|
| 1458 | for($i=0; $i<strlen($format); $i++) |
|---|
| 1459 | { |
|---|
| 1460 | if ($format{$i}=='\\') // skip escape chars |
|---|
| 1461 | continue; |
|---|
| 1462 | |
|---|
| 1463 | // write char "as-is" |
|---|
| 1464 | if ($format{$i}==' ' || $format{$i-1}=='\\') |
|---|
| 1465 | $out .= $format{$i}; |
|---|
| 1466 | // weekday (short) |
|---|
| 1467 | else if ($format{$i}=='D') |
|---|
| 1468 | $out .= rcube_label(strtolower(date('D', $timestamp))); |
|---|
| 1469 | // weekday long |
|---|
| 1470 | else if ($format{$i}=='l') |
|---|
| 1471 | $out .= rcube_label(strtolower(date('l', $timestamp))); |
|---|
| 1472 | // month name (short) |
|---|
| 1473 | else if ($format{$i}=='M') |
|---|
| 1474 | $out .= rcube_label(strtolower(date('M', $timestamp))); |
|---|
| 1475 | // month name (long) |
|---|
| 1476 | else if ($format{$i}=='F') |
|---|
| 1477 | $out .= rcube_label(strtolower(date('F', $timestamp))); |
|---|
| 1478 | else |
|---|
| 1479 | $out .= date($format{$i}, $timestamp); |
|---|
| 1480 | } |
|---|
| 1481 | |
|---|
| 1482 | return $out; |
|---|
| 1483 | } |
|---|
| 1484 | |
|---|
| 1485 | |
|---|
| 1486 | /** |
|---|
| 1487 | * Compose a valid representaion of name and e-mail address |
|---|
| 1488 | * |
|---|
| 1489 | * @param string E-mail address |
|---|
| 1490 | * @param string Person name |
|---|
| 1491 | * @return string Formatted string |
|---|
| 1492 | */ |
|---|
| 1493 | function format_email_recipient($email, $name='') |
|---|
| 1494 | { |
|---|
| 1495 | if ($name && $name != $email) |
|---|
| 1496 | return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email); |
|---|
| 1497 | else |
|---|
| 1498 | return $email; |
|---|
| 1499 | } |
|---|
| 1500 | |
|---|
| 1501 | |
|---|
| 1502 | |
|---|
| 1503 | /****** debugging functions ********/ |
|---|
| 1504 | |
|---|
| 1505 | |
|---|
| 1506 | /** |
|---|
| 1507 | * Print or write debug messages |
|---|
| 1508 | * |
|---|
| 1509 | * @param mixed Debug message or data |
|---|
| 1510 | */ |
|---|
| 1511 | function console($msg) |
|---|
| 1512 | { |
|---|
| 1513 | if (!is_string($msg)) |
|---|
| 1514 | $msg = var_export($msg, true); |
|---|
| 1515 | |
|---|
| 1516 | if (!($GLOBALS['CONFIG']['debug_level'] & 4)) |
|---|
| 1517 | write_log('console', $msg); |
|---|
| 1518 | else if ($GLOBALS['REMOTE_REQUEST']) |
|---|
| 1519 | print "/*\n $msg \n*/\n"; |
|---|
| 1520 | else |
|---|
| 1521 | { |
|---|
| 1522 | print '<div style="background:#eee; border:1px solid #ccc; margin-bottom:3px; padding:6px"><pre>'; |
|---|
| 1523 | print $msg; |
|---|
| 1524 | print "</pre></div>\n"; |
|---|
| 1525 | } |
|---|
| 1526 | } |
|---|
| 1527 | |
|---|
| 1528 | |
|---|
| 1529 | /** |
|---|
| 1530 | * Append a line to a logfile in the logs directory. |
|---|
| 1531 | * Date will be added automatically to the line. |
|---|
| 1532 | * |
|---|
| 1533 | * @param $name Name of logfile |
|---|
| 1534 | * @param $line Line to append |
|---|
| 1535 | */ |
|---|
| 1536 | function write_log($name, $line) |
|---|
| 1537 | { |
|---|
| 1538 | global $CONFIG, $INSTALL_PATH; |
|---|
| 1539 | |
|---|
| 1540 | if (!is_string($line)) |
|---|
| 1541 | $line = var_export($line, true); |
|---|
| 1542 | |
|---|
| 1543 | $log_entry = sprintf("[%s]: %s\n", |
|---|
| 1544 | date("d-M-Y H:i:s O", mktime()), |
|---|
| 1545 | $line); |
|---|
| 1546 | |
|---|
| 1547 | if (empty($CONFIG['log_dir'])) |
|---|
| 1548 | $CONFIG['log_dir'] = $INSTALL_PATH.'logs'; |
|---|
| 1549 | |
|---|
| 1550 | // try to open specific log file for writing |
|---|
| 1551 | if ($fp = @fopen($CONFIG['log_dir'].'/'.$name, 'a')) |
|---|
| 1552 | { |
|---|
| 1553 | fwrite($fp, $log_entry); |
|---|
| 1554 | fclose($fp); |
|---|
| 1555 | } |
|---|
| 1556 | } |
|---|
| 1557 | |
|---|
| 1558 | |
|---|
| 1559 | /** |
|---|
| 1560 | * @access private |
|---|
| 1561 | */ |
|---|
| 1562 | function rcube_timer() |
|---|
| 1563 | { |
|---|
| 1564 | list($usec, $sec) = explode(" ", microtime()); |
|---|
| 1565 | return ((float)$usec + (float)$sec); |
|---|
| 1566 | } |
|---|
| 1567 | |
|---|
| 1568 | |
|---|
| 1569 | /** |
|---|
| 1570 | * @access private |
|---|
| 1571 | */ |
|---|
| 1572 | function rcube_print_time($timer, $label='Timer') |
|---|
| 1573 | { |
|---|
| 1574 | static $print_count = 0; |
|---|
| 1575 | |
|---|
| 1576 | $print_count++; |
|---|
| 1577 | $now = rcube_timer(); |
|---|
| 1578 | $diff = $now-$timer; |
|---|
| 1579 | |
|---|
| 1580 | if (empty($label)) |
|---|
| 1581 | $label = 'Timer '.$print_count; |
|---|
| 1582 | |
|---|
| 1583 | console(sprintf("%s: %0.4f sec", $label, $diff)); |
|---|
| 1584 | } |
|---|
| 1585 | |
|---|
| 1586 | |
|---|
| 1587 | /** |
|---|
| 1588 | * Return the mailboxlist in HTML |
|---|
| 1589 | * |
|---|
| 1590 | * @param array Named parameters |
|---|
| 1591 | * @return string HTML code for the gui object |
|---|
| 1592 | */ |
|---|
| 1593 | function rcmail_mailbox_list($attrib) |
|---|
| 1594 | { |
|---|
| 1595 | global $IMAP, $CONFIG, $OUTPUT, $COMM_PATH; |
|---|
| 1596 | static $s_added_script = FALSE; |
|---|
| 1597 | static $a_mailboxes; |
|---|
| 1598 | |
|---|
| 1599 | // add some labels to client |
|---|
| 1600 | rcube_add_label('purgefolderconfirm'); |
|---|
| 1601 | rcube_add_label('deletemessagesconfirm'); |
|---|
| 1602 | |
|---|
| 1603 | // $mboxlist_start = rcube_timer(); |
|---|
| 1604 | |
|---|
| 1605 | $type = $attrib['type'] ? $attrib['type'] : 'ul'; |
|---|
| 1606 | $add_attrib = $type=='select' ? array('style', 'class', 'id', 'name', 'onchange') : |
|---|
| 1607 | array('style', 'class', 'id'); |
|---|
| 1608 | |
|---|
| 1609 | if ($type=='ul' && !$attrib['id']) |
|---|
| 1610 | $attrib['id'] = 'rcmboxlist'; |
|---|
| 1611 | |
|---|
| 1612 | // allow the following attributes to be added to the <ul> tag |
|---|
| 1613 | $attrib_str = create_attrib_string($attrib, $add_attrib); |
|---|
| 1614 | |
|---|
| 1615 | $out = '<' . $type . $attrib_str . ">\n"; |
|---|
| 1616 | |
|---|
| 1617 | // add no-selection option |
|---|
| 1618 | if ($type=='select' && $attrib['noselection']) |
|---|
| 1619 | $out .= sprintf('<option value="0">%s</option>'."\n", |
|---|
| 1620 | rcube_label($attrib['noselection'])); |
|---|
| 1621 | |
|---|
| 1622 | // get mailbox list |
|---|
| 1623 | $mbox_name = $IMAP->get_mailbox_name(); |
|---|
| 1624 | |
|---|
| 1625 | // for these mailboxes we have localized labels |
|---|
| 1626 | $special_mailboxes = array('inbox', 'sent', 'drafts', 'trash', 'junk'); |
|---|
| 1627 | |
|---|
| 1628 | |
|---|
| 1629 | // build the folders tree |
|---|
| 1630 | if (empty($a_mailboxes)) |
|---|
| 1631 | { |
|---|
| 1632 | // get mailbox list |
|---|
| 1633 | $a_folders = $IMAP->list_mailboxes(); |
|---|
| 1634 | $delimiter = $IMAP->get_hierarchy_delimiter(); |
|---|
| 1635 | $a_mailboxes = array(); |
|---|
| 1636 | |
|---|
| 1637 | // rcube_print_time($mboxlist_start, 'list_mailboxes()'); |
|---|
| 1638 | |
|---|
| 1639 | foreach ($a_folders as $folder) |
|---|
| 1640 | rcmail_build_folder_tree($a_mailboxes, $folder, $delimiter); |
|---|
| 1641 | } |
|---|
| 1642 | |
|---|
| 1643 | // var_dump($a_mailboxes); |
|---|
| 1644 | |
|---|
| 1645 | if ($type=='select') |
|---|
| 1646 | $out .= rcmail_render_folder_tree_select($a_mailboxes, $special_mailboxes, $mbox_name, $attrib['maxlength']); |
|---|
| 1647 | else |
|---|
| 1648 | $out .= rcmail_render_folder_tree_html($a_mailboxes, $special_mailboxes, $mbox_name, $attrib['maxlength']); |
|---|
| 1649 | |
|---|
| 1650 | // rcube_print_time($mboxlist_start, 'render_folder_tree()'); |
|---|
| 1651 | |
|---|
| 1652 | |
|---|
| 1653 | if ($type=='ul') |
|---|
| 1654 | $OUTPUT->add_gui_object('mailboxlist', $attrib['id']); |
|---|
| 1655 | |
|---|
| 1656 | return $out . "</$type>"; |
|---|
| 1657 | } |
|---|
| 1658 | |
|---|
| 1659 | |
|---|
| 1660 | |
|---|
| 1661 | |
|---|
| 1662 | /** |
|---|
| 1663 | * Create a hierarchical array of the mailbox list |
|---|
| 1664 | * @access private |
|---|
| 1665 | */ |
|---|
| 1666 | function rcmail_build_folder_tree(&$arrFolders, $folder, $delm='/', $path='') |
|---|
| 1667 | { |
|---|
| 1668 | $pos = strpos($folder, $delm); |
|---|
| 1669 | if ($pos !== false) |
|---|
| 1670 | { |
|---|
| 1671 | $subFolders = substr($folder, $pos+1); |
|---|
| 1672 | $currentFolder = substr($folder, 0, $pos); |
|---|
| 1673 | } |
|---|
| 1674 | else |
|---|
| 1675 | { |
|---|
| 1676 | $subFolders = false; |
|---|
| 1677 | $currentFolder = $folder; |
|---|
| 1678 | } |
|---|
| 1679 | |
|---|
| 1680 | $path .= $currentFolder; |
|---|
| 1681 | |
|---|
| 1682 | if (!isset($arrFolders[$currentFolder])) |
|---|
| 1683 | { |
|---|
| 1684 | $arrFolders[$currentFolder] = array('id' => $path, |
|---|
| 1685 | 'name' => rcube_charset_convert($currentFolder, 'UTF-7'), |
|---|
| 1686 | 'folders' => array()); |
|---|
| 1687 | } |
|---|
| 1688 | |
|---|
| 1689 | if (!empty($subFolders)) |
|---|
| 1690 | rcmail_build_folder_tree($arrFolders[$currentFolder]['folders'], $subFolders, $delm, $path.$delm); |
|---|
| 1691 | } |
|---|
| 1692 | |
|---|
| 1693 | |
|---|
| 1694 | /** |
|---|
| 1695 | * Return html for a structured list <ul> for the mailbox tree |
|---|
| 1696 | * @access private |
|---|
| 1697 | */ |
|---|
| 1698 | function rcmail_render_folder_tree_html(&$arrFolders, &$special, &$mbox_name, $maxlength, $nestLevel=0) |
|---|
| 1699 | { |
|---|
| 1700 | global $COMM_PATH, $IMAP, $CONFIG, $OUTPUT; |
|---|
| 1701 | |
|---|
| 1702 | $idx = 0; |
|---|
| 1703 | $out = ''; |
|---|
| 1704 | foreach ($arrFolders as $key => $folder) |
|---|
| 1705 | { |
|---|
| 1706 | $zebra_class = ($nestLevel*$idx)%2 ? 'even' : 'odd'; |
|---|
| 1707 | $title = ''; |
|---|
| 1708 | |
|---|
| 1709 | $folder_lc = strtolower($folder['id']); |
|---|
| 1710 | if (in_array($folder_lc, $special)) |
|---|
| 1711 | $foldername = rcube_label($folder_lc); |
|---|
| 1712 | else |
|---|
| 1713 | { |
|---|
| 1714 | $foldername = $folder['name']; |
|---|
| 1715 | |
|---|
| 1716 | // shorten the folder name to a given length |
|---|
| 1717 | if ($maxlength && $maxlength>1) |
|---|
| 1718 | { |
|---|
| 1719 | $fname = abbrevate_string($foldername, $maxlength); |
|---|
| 1720 | if ($fname != $foldername) |
|---|
| 1721 | $title = ' title="'.Q($foldername).'"'; |
|---|
| 1722 | $foldername = $fname; |
|---|
| 1723 | } |
|---|
| 1724 | } |
|---|
| 1725 | |
|---|
| 1726 | // add unread message count display |
|---|
| 1727 | if ($unread_count = $IMAP->messagecount($folder['id'], 'RECENT', ($folder['id']==$mbox_name))) |
|---|
| 1728 | $foldername .= sprintf(' (%d)', $unread_count); |
|---|
| 1729 | |
|---|
| 1730 | // make folder name safe for ids and class names |
|---|
| 1731 | $folder_id = preg_replace('/[^A-Za-z0-9\-_]/', '', $folder['id']); |
|---|
| 1732 | $class_name = preg_replace('/[^a-z0-9\-_]/', '', $folder_lc); |
|---|
| 1733 | |
|---|
| 1734 | // set special class for Sent, Drafts, Trash and Junk |
|---|
| 1735 | if ($folder['id']==$CONFIG['sent_mbox']) |
|---|
| 1736 | $class_name = 'sent'; |
|---|
| 1737 | else if ($folder['id']==$CONFIG['drafts_mbox']) |
|---|
| 1738 | $class_name = 'drafts'; |
|---|
| 1739 | else if ($folder['id']==$CONFIG['trash_mbox']) |
|---|
| 1740 | $class_name = 'trash'; |
|---|
| 1741 | else if ($folder['id']==$CONFIG['junk_mbox']) |
|---|
| 1742 | $class_name = 'junk'; |
|---|
| 1743 | |
|---|
| 1744 | $js_name = htmlspecialchars(JQ($folder['id'])); |
|---|
| 1745 | $out .= sprintf('<li id="rcmli%s" class="mailbox %s %s%s%s"><a href="%s"'. |
|---|
| 1746 | ' onclick="return %s.command(\'list\',\'%s\',this)"'. |
|---|
| 1747 | ' onmouseover="return %s.focus_folder(\'%s\')"' . |
|---|
| 1748 | ' onmouseout="return %s.unfocus_folder(\'%s\')"' . |
|---|
| 1749 | ' onmouseup="return %s.folder_mouse_up(\'%s\')"%s>%s</a>', |
|---|
| 1750 | $folder_id, |
|---|
| 1751 | $class_name, |
|---|
| 1752 | $zebra_class, |
|---|
| 1753 | $unread_count ? ' unread' : '', |
|---|
| 1754 | $folder['id']==$mbox_name ? ' selected' : '', |
|---|
| 1755 | Q(rcmail_url('', array('_mbox' => $folder['id']))), |
|---|
| 1756 | JS_OBJECT_NAME, |
|---|
| 1757 | $js_name, |
|---|
| 1758 | JS_OBJECT_NAME, |
|---|
| 1759 | $js_name, |
|---|
| 1760 | JS_OBJECT_NAME, |
|---|
| 1761 | $js_name, |
|---|
| 1762 | JS_OBJECT_NAME, |
|---|
| 1763 | $js_name, |
|---|
| 1764 | $title, |
|---|
| 1765 | Q($foldername)); |
|---|
| 1766 | |
|---|
| 1767 | if (!empty($folder['folders'])) |
|---|
| 1768 | $out .= "\n<ul>\n" . rcmail_render_folder_tree_html($folder['folders'], $special, $mbox_name, $maxlength, $nestLevel+1) . "</ul>\n"; |
|---|
| 1769 | |
|---|
| 1770 | $out .= "</li>\n"; |
|---|
| 1771 | $idx++; |
|---|
| 1772 | } |
|---|
| 1773 | |
|---|
| 1774 | return $out; |
|---|
| 1775 | } |
|---|
| 1776 | |
|---|
| 1777 | |
|---|
| 1778 | /** |
|---|
| 1779 | * Return html for a flat list <select> for the mailbox tree |
|---|
| 1780 | * @access private |
|---|
| 1781 | */ |
|---|
| 1782 | function rcmail_render_folder_tree_select(&$arrFolders, &$special, &$mbox_name, $maxlength, $nestLevel=0) |
|---|
| 1783 | { |
|---|
| 1784 | global $IMAP, $OUTPUT; |
|---|
| 1785 | |
|---|
| 1786 | $idx = 0; |
|---|
| 1787 | $out = ''; |
|---|
| 1788 | foreach ($arrFolders as $key=>$folder) |
|---|
| 1789 | { |
|---|
| 1790 | $folder_lc = strtolower($folder['id']); |
|---|
| 1791 | if (in_array($folder_lc, $special)) |
|---|
| 1792 | $foldername = rcube_label($folder_lc); |
|---|
| 1793 | else |
|---|
| 1794 | { |
|---|
| 1795 | $foldername = $folder['name']; |
|---|
| 1796 | |
|---|
| 1797 | // shorten the folder name to a given length |
|---|
| 1798 | if ($maxlength && $maxlength>1) |
|---|
| 1799 | $foldername = abbrevate_string($foldername, $maxlength); |
|---|
| 1800 | } |
|---|
| 1801 | |
|---|
| 1802 | $out .= sprintf('<option value="%s">%s%s</option>'."\n", |
|---|
| 1803 | htmlspecialchars($folder['id']), |
|---|
| 1804 | str_repeat(' ', $nestLevel*4), |
|---|
| 1805 | Q($foldername)); |
|---|
| 1806 | |
|---|
| 1807 | if (!empty($folder['folders'])) |
|---|
| 1808 | $out .= rcmail_render_folder_tree_select($folder['folders'], $special, $mbox_name, $maxlength, $nestLevel+1); |
|---|
| 1809 | |
|---|
| 1810 | $idx++; |
|---|
| 1811 | } |
|---|
| 1812 | |
|---|
| 1813 | return $out; |
|---|
| 1814 | } |
|---|
| 1815 | |
|---|
| 1816 | ?> |
|---|