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