| 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 | /** |
|---|
| 44 | * Return correct name for a specific database table |
|---|
| 45 | * |
|---|
| 46 | * @param string Table name |
|---|
| 47 | * @return string Translated table name |
|---|
| 48 | */ |
|---|
| 49 | function get_table_name($table) |
|---|
| 50 | { |
|---|
| 51 | global $CONFIG; |
|---|
| 52 | |
|---|
| 53 | // return table name if configured |
|---|
| 54 | $config_key = 'db_table_'.$table; |
|---|
| 55 | |
|---|
| 56 | if (strlen($CONFIG[$config_key])) |
|---|
| 57 | return $CONFIG[$config_key]; |
|---|
| 58 | |
|---|
| 59 | return $table; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Return correct name for a specific database sequence |
|---|
| 65 | * (used for Postgres only) |
|---|
| 66 | * |
|---|
| 67 | * @param string Secuence name |
|---|
| 68 | * @return string Translated sequence name |
|---|
| 69 | */ |
|---|
| 70 | function get_sequence_name($sequence) |
|---|
| 71 | { |
|---|
| 72 | // return table name if configured |
|---|
| 73 | $config_key = 'db_sequence_'.$sequence; |
|---|
| 74 | $opt = rcmail::get_instance()->config->get($config_key); |
|---|
| 75 | |
|---|
| 76 | if (!empty($opt)) |
|---|
| 77 | return $opt; |
|---|
| 78 | |
|---|
| 79 | return $sequence; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * Get localized text in the desired language |
|---|
| 85 | * It's a global wrapper for rcmail::gettext() |
|---|
| 86 | * |
|---|
| 87 | * @param mixed Named parameters array or label name |
|---|
| 88 | * @return string Localized text |
|---|
| 89 | * @see rcmail::gettext() |
|---|
| 90 | */ |
|---|
| 91 | function rcube_label($p) |
|---|
| 92 | { |
|---|
| 93 | return rcmail::get_instance()->gettext($p); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * Overwrite action variable |
|---|
| 99 | * |
|---|
| 100 | * @param string New action value |
|---|
| 101 | */ |
|---|
| 102 | function rcmail_overwrite_action($action) |
|---|
| 103 | { |
|---|
| 104 | $app = rcmail::get_instance(); |
|---|
| 105 | $app->action = $action; |
|---|
| 106 | $app->output->set_env('action', $action); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * Compose an URL for a specific action |
|---|
| 112 | * |
|---|
| 113 | * @param string Request action |
|---|
| 114 | * @param array More URL parameters |
|---|
| 115 | * @param string Request task (omit if the same) |
|---|
| 116 | * @return The application URL |
|---|
| 117 | */ |
|---|
| 118 | function rcmail_url($action, $p=array(), $task=null) |
|---|
| 119 | { |
|---|
| 120 | $app = rcmail::get_instance(); |
|---|
| 121 | return $app->url((array)$p + array('_action' => $action, 'task' => $task)); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * Garbage collector function for temp files. |
|---|
| 127 | * Remove temp files older than two days |
|---|
| 128 | */ |
|---|
| 129 | function rcmail_temp_gc() |
|---|
| 130 | { |
|---|
| 131 | $tmp = unslashify($CONFIG['temp_dir']); |
|---|
| 132 | $expire = mktime() - 172800; // expire in 48 hours |
|---|
| 133 | |
|---|
| 134 | if ($dir = opendir($tmp)) |
|---|
| 135 | { |
|---|
| 136 | while (($fname = readdir($dir)) !== false) |
|---|
| 137 | { |
|---|
| 138 | if ($fname{0} == '.') |
|---|
| 139 | continue; |
|---|
| 140 | |
|---|
| 141 | if (filemtime($tmp.'/'.$fname) < $expire) |
|---|
| 142 | @unlink($tmp.'/'.$fname); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | closedir($dir); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | /** |
|---|
| 151 | * Garbage collector for cache entries. |
|---|
| 152 | * Remove all expired message cache records |
|---|
| 153 | */ |
|---|
| 154 | function rcmail_message_cache_gc() |
|---|
| 155 | { |
|---|
| 156 | global $DB, $CONFIG; |
|---|
| 157 | |
|---|
| 158 | // no cache lifetime configured |
|---|
| 159 | if (empty($CONFIG['message_cache_lifetime'])) |
|---|
| 160 | return; |
|---|
| 161 | |
|---|
| 162 | // get target timestamp |
|---|
| 163 | $ts = get_offset_time($CONFIG['message_cache_lifetime'], -1); |
|---|
| 164 | |
|---|
| 165 | $DB->query("DELETE FROM ".get_table_name('messages')." |
|---|
| 166 | WHERE created < ".$DB->fromunixtime($ts)); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | /** |
|---|
| 171 | * Convert a string from one charset to another. |
|---|
| 172 | * Uses mbstring and iconv functions if possible |
|---|
| 173 | * |
|---|
| 174 | * @param string Input string |
|---|
| 175 | * @param string Suspected charset of the input string |
|---|
| 176 | * @param string Target charset to convert to; defaults to RCMAIL_CHARSET |
|---|
| 177 | * @return Converted string |
|---|
| 178 | */ |
|---|
| 179 | function rcube_charset_convert($str, $from, $to=NULL) |
|---|
| 180 | { |
|---|
| 181 | static $mbstring_loaded = null, $convert_warning = false; |
|---|
| 182 | |
|---|
| 183 | $from = strtoupper($from); |
|---|
| 184 | $to = $to==NULL ? strtoupper(RCMAIL_CHARSET) : strtoupper($to); |
|---|
| 185 | $error = false; $conv = null; |
|---|
| 186 | |
|---|
| 187 | if ($from==$to || $str=='' || empty($from)) |
|---|
| 188 | return $str; |
|---|
| 189 | |
|---|
| 190 | $aliases = array( |
|---|
| 191 | 'US-ASCII' => 'ISO-8859-1', |
|---|
| 192 | 'UNKNOWN-8BIT' => 'ISO-8859-15', |
|---|
| 193 | 'X-UNKNOWN' => 'ISO-8859-15', |
|---|
| 194 | 'X-USER-DEFINED' => 'ISO-8859-15', |
|---|
| 195 | 'ISO-8859-8-I' => 'ISO-8859-8', |
|---|
| 196 | 'KS_C_5601-1987' => 'EUC-KR', |
|---|
| 197 | ); |
|---|
| 198 | |
|---|
| 199 | // convert charset using iconv module |
|---|
| 200 | if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') |
|---|
| 201 | { |
|---|
| 202 | $aliases['GB2312'] = 'GB18030'; |
|---|
| 203 | $_iconv = iconv(($aliases[$from] ? $aliases[$from] : $from), ($aliases[$to] ? $aliases[$to] : $to) . "//IGNORE", $str); |
|---|
| 204 | if ($_iconv !== false) |
|---|
| 205 | { |
|---|
| 206 | return $_iconv; |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | // settings for mbstring module (by Tadashi Jokagi) |
|---|
| 211 | if (is_null($mbstring_loaded)) { |
|---|
| 212 | if ($mbstring_loaded = extension_loaded("mbstring")) |
|---|
| 213 | mb_internal_encoding(RCMAIL_CHARSET); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | // convert charset using mbstring module |
|---|
| 217 | if ($mbstring_loaded) |
|---|
| 218 | { |
|---|
| 219 | $aliases['UTF-7'] = 'UTF7-IMAP'; |
|---|
| 220 | $aliases['WINDOWS-1257'] = 'ISO-8859-13'; |
|---|
| 221 | |
|---|
| 222 | // return if convert succeeded |
|---|
| 223 | if (($out = mb_convert_encoding($str, ($aliases[$to] ? $aliases[$to] : $to), ($aliases[$from] ? $aliases[$from] : $from))) != '') |
|---|
| 224 | return $out; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | if (class_exists('utf8')) |
|---|
| 229 | $conv = new utf8(); |
|---|
| 230 | |
|---|
| 231 | // convert string to UTF-8 |
|---|
| 232 | if ($from == 'UTF-7') |
|---|
| 233 | $str = utf7_to_utf8($str); |
|---|
| 234 | else if (($from == 'ISO-8859-1') && function_exists('utf8_encode')) |
|---|
| 235 | $str = utf8_encode($str); |
|---|
| 236 | else if ($from != 'UTF-8' && $conv) |
|---|
| 237 | { |
|---|
| 238 | $conv->loadCharset($from); |
|---|
| 239 | $str = $conv->strToUtf8($str); |
|---|
| 240 | } |
|---|
| 241 | else if ($from != 'UTF-8') |
|---|
| 242 | $error = true; |
|---|
| 243 | |
|---|
| 244 | // encode string for output |
|---|
| 245 | if ($to == 'UTF-7') |
|---|
| 246 | return utf8_to_utf7($str); |
|---|
| 247 | else if ($to == 'ISO-8859-1' && function_exists('utf8_decode')) |
|---|
| 248 | return utf8_decode($str); |
|---|
| 249 | else if ($to != 'UTF-8' && $conv) |
|---|
| 250 | { |
|---|
| 251 | $conv->loadCharset($to); |
|---|
| 252 | return $conv->utf8ToStr($str); |
|---|
| 253 | } |
|---|
| 254 | else if ($to != 'UTF-8') |
|---|
| 255 | $error = true; |
|---|
| 256 | |
|---|
| 257 | // report error |
|---|
| 258 | if ($error && !$convert_warning) |
|---|
| 259 | { |
|---|
| 260 | raise_error(array( |
|---|
| 261 | 'code' => 500, |
|---|
| 262 | 'type' => 'php', |
|---|
| 263 | 'file' => __FILE__, |
|---|
| 264 | 'message' => "Could not convert string charset. Make sure iconv is installed or lib/utf8.class is available" |
|---|
| 265 | ), true, false); |
|---|
| 266 | |
|---|
| 267 | $convert_warning = true; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | // return UTF-8 string |
|---|
| 271 | return $str; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | /** |
|---|
| 276 | * Replacing specials characters to a specific encoding type |
|---|
| 277 | * |
|---|
| 278 | * @param string Input string |
|---|
| 279 | * @param string Encoding type: text|html|xml|js|url |
|---|
| 280 | * @param string Replace mode for tags: show|replace|remove |
|---|
| 281 | * @param boolean Convert newlines |
|---|
| 282 | * @return The quoted string |
|---|
| 283 | */ |
|---|
| 284 | function rep_specialchars_output($str, $enctype='', $mode='', $newlines=TRUE) |
|---|
| 285 | { |
|---|
| 286 | global $OUTPUT; |
|---|
| 287 | static $html_encode_arr = false; |
|---|
| 288 | static $js_rep_table = false; |
|---|
| 289 | static $xml_rep_table = false; |
|---|
| 290 | |
|---|
| 291 | $charset = $OUTPUT->get_charset(); |
|---|
| 292 | $is_iso_8859_1 = false; |
|---|
| 293 | if ($charset == 'ISO-8859-1') { |
|---|
| 294 | $is_iso_8859_1 = true; |
|---|
| 295 | } |
|---|
| 296 | if (!$enctype) |
|---|
| 297 | $enctype = $OUTPUT->type; |
|---|
| 298 | |
|---|
| 299 | // encode for plaintext |
|---|
| 300 | if ($enctype=='text') |
|---|
| 301 | return str_replace("\r\n", "\n", $mode=='remove' ? strip_tags($str) : $str); |
|---|
| 302 | |
|---|
| 303 | // encode for HTML output |
|---|
| 304 | if ($enctype=='html') |
|---|
| 305 | { |
|---|
| 306 | if (!$html_encode_arr) |
|---|
| 307 | { |
|---|
| 308 | $html_encode_arr = get_html_translation_table(HTML_SPECIALCHARS); |
|---|
| 309 | unset($html_encode_arr['?']); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | $ltpos = strpos($str, '<'); |
|---|
| 313 | $encode_arr = $html_encode_arr; |
|---|
| 314 | |
|---|
| 315 | // don't replace quotes and html tags |
|---|
| 316 | if (($mode=='show' || $mode=='') && $ltpos!==false && strpos($str, '>', $ltpos)!==false) |
|---|
| 317 | { |
|---|
| 318 | unset($encode_arr['"']); |
|---|
| 319 | unset($encode_arr['<']); |
|---|
| 320 | unset($encode_arr['>']); |
|---|
| 321 | unset($encode_arr['&']); |
|---|
| 322 | } |
|---|
| 323 | else if ($mode=='remove') |
|---|
| 324 | $str = strip_tags($str); |
|---|
| 325 | |
|---|
| 326 | // avoid douple quotation of & |
|---|
| 327 | $out = preg_replace('/&([A-Za-z]{2,6}|#[0-9]{2,4});/', '&\\1;', strtr($str, $encode_arr)); |
|---|
| 328 | |
|---|
| 329 | return $newlines ? nl2br($out) : $out; |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | if ($enctype=='url') |
|---|
| 333 | return rawurlencode($str); |
|---|
| 334 | |
|---|
| 335 | // if the replace tables for XML and JS are not yet defined |
|---|
| 336 | if ($js_rep_table===false) |
|---|
| 337 | { |
|---|
| 338 | $js_rep_table = $xml_rep_table = array(); |
|---|
| 339 | $xml_rep_table['&'] = '&'; |
|---|
| 340 | |
|---|
| 341 | for ($c=160; $c<256; $c++) // can be increased to support more charsets |
|---|
| 342 | { |
|---|
| 343 | $xml_rep_table[Chr($c)] = "&#$c;"; |
|---|
| 344 | |
|---|
| 345 | if ($is_iso_8859_1) |
|---|
| 346 | $js_rep_table[Chr($c)] = sprintf("\\u%04x", $c); |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | $xml_rep_table['"'] = '"'; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | // encode for XML |
|---|
| 353 | if ($enctype=='xml') |
|---|
| 354 | return strtr($str, $xml_rep_table); |
|---|
| 355 | |
|---|
| 356 | // encode for javascript use |
|---|
| 357 | if ($enctype=='js') |
|---|
| 358 | { |
|---|
| 359 | if ($charset!='UTF-8') |
|---|
| 360 | $str = rcube_charset_convert($str, RCMAIL_CHARSET,$charset); |
|---|
| 361 | |
|---|
| 362 | return preg_replace(array("/\r?\n/", "/\r/", '/<\\//'), array('\n', '\n', '<\\/'), addslashes(strtr($str, $js_rep_table))); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | // no encoding given -> return original string |
|---|
| 366 | return $str; |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | /** |
|---|
| 370 | * Quote a given string. |
|---|
| 371 | * Shortcut function for rep_specialchars_output |
|---|
| 372 | * |
|---|
| 373 | * @return string HTML-quoted string |
|---|
| 374 | * @see rep_specialchars_output() |
|---|
| 375 | */ |
|---|
| 376 | function Q($str, $mode='strict', $newlines=TRUE) |
|---|
| 377 | { |
|---|
| 378 | return rep_specialchars_output($str, 'html', $mode, $newlines); |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | /** |
|---|
| 382 | * Quote a given string for javascript output. |
|---|
| 383 | * Shortcut function for rep_specialchars_output |
|---|
| 384 | * |
|---|
| 385 | * @return string JS-quoted string |
|---|
| 386 | * @see rep_specialchars_output() |
|---|
| 387 | */ |
|---|
| 388 | function JQ($str) |
|---|
| 389 | { |
|---|
| 390 | return rep_specialchars_output($str, 'js'); |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | /** |
|---|
| 395 | * Read input value and convert it for internal use |
|---|
| 396 | * Performs stripslashes() and charset conversion if necessary |
|---|
| 397 | * |
|---|
| 398 | * @param string Field name to read |
|---|
| 399 | * @param int Source to get value from (GPC) |
|---|
| 400 | * @param boolean Allow HTML tags in field value |
|---|
| 401 | * @param string Charset to convert into |
|---|
| 402 | * @return string Field value or NULL if not available |
|---|
| 403 | */ |
|---|
| 404 | function get_input_value($fname, $source, $allow_html=FALSE, $charset=NULL) |
|---|
| 405 | { |
|---|
| 406 | global $OUTPUT; |
|---|
| 407 | $value = NULL; |
|---|
| 408 | |
|---|
| 409 | if ($source==RCUBE_INPUT_GET && isset($_GET[$fname])) |
|---|
| 410 | $value = $_GET[$fname]; |
|---|
| 411 | else if ($source==RCUBE_INPUT_POST && isset($_POST[$fname])) |
|---|
| 412 | $value = $_POST[$fname]; |
|---|
| 413 | else if ($source==RCUBE_INPUT_GPC) |
|---|
| 414 | { |
|---|
| 415 | if (isset($_POST[$fname])) |
|---|
| 416 | $value = $_POST[$fname]; |
|---|
| 417 | else if (isset($_GET[$fname])) |
|---|
| 418 | $value = $_GET[$fname]; |
|---|
| 419 | else if (isset($_COOKIE[$fname])) |
|---|
| 420 | $value = $_COOKIE[$fname]; |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | // strip slashes if magic_quotes enabled |
|---|
| 424 | if ((bool)get_magic_quotes_gpc()) |
|---|
| 425 | $value = stripslashes($value); |
|---|
| 426 | |
|---|
| 427 | // remove HTML tags if not allowed |
|---|
| 428 | if (!$allow_html) |
|---|
| 429 | $value = strip_tags($value); |
|---|
| 430 | |
|---|
| 431 | // convert to internal charset |
|---|
| 432 | if (is_object($OUTPUT)) |
|---|
| 433 | return rcube_charset_convert($value, $OUTPUT->get_charset(), $charset); |
|---|
| 434 | else |
|---|
| 435 | return $value; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | /** |
|---|
| 439 | * Remove all non-ascii and non-word chars |
|---|
| 440 | * except . and - |
|---|
| 441 | */ |
|---|
| 442 | function asciiwords($str, $css_id = false) |
|---|
| 443 | { |
|---|
| 444 | $allowed = 'a-z0-9\_\-' . (!$css_id ? '\.' : ''); |
|---|
| 445 | return preg_replace("/[^$allowed]/i", '', $str); |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | /** |
|---|
| 449 | * Remove single and double quotes from given string |
|---|
| 450 | * |
|---|
| 451 | * @param string Input value |
|---|
| 452 | * @return string Dequoted string |
|---|
| 453 | */ |
|---|
| 454 | function strip_quotes($str) |
|---|
| 455 | { |
|---|
| 456 | return preg_replace('/[\'"]/', '', $str); |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | /** |
|---|
| 461 | * Remove new lines characters from given string |
|---|
| 462 | * |
|---|
| 463 | * @param string Input value |
|---|
| 464 | * @return string Stripped string |
|---|
| 465 | */ |
|---|
| 466 | function strip_newlines($str) |
|---|
| 467 | { |
|---|
| 468 | return preg_replace('/[\r\n]/', '', $str); |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | /** |
|---|
| 473 | * Create a HTML table based on the given data |
|---|
| 474 | * |
|---|
| 475 | * @param array Named table attributes |
|---|
| 476 | * @param mixed Table row data. Either a two-dimensional array or a valid SQL result set |
|---|
| 477 | * @param array List of cols to show |
|---|
| 478 | * @param string Name of the identifier col |
|---|
| 479 | * @return string HTML table code |
|---|
| 480 | */ |
|---|
| 481 | function rcube_table_output($attrib, $table_data, $a_show_cols, $id_col) |
|---|
| 482 | { |
|---|
| 483 | global $RCMAIL; |
|---|
| 484 | |
|---|
| 485 | $table = new html_table(/*array('cols' => count($a_show_cols))*/); |
|---|
| 486 | |
|---|
| 487 | // add table header |
|---|
| 488 | foreach ($a_show_cols as $col) |
|---|
| 489 | $table->add_header($col, Q(rcube_label($col))); |
|---|
| 490 | |
|---|
| 491 | $c = 0; |
|---|
| 492 | if (!is_array($table_data)) |
|---|
| 493 | { |
|---|
| 494 | $db = $RCMAIL->get_dbh(); |
|---|
| 495 | while ($table_data && ($sql_arr = $db->fetch_assoc($table_data))) |
|---|
| 496 | { |
|---|
| 497 | $zebra_class = $c % 2 ? 'even' : 'odd'; |
|---|
| 498 | $table->add_row(array('id' => 'rcmrow' . $sql_arr[$id_col], 'class' => "contact $zebra_class")); |
|---|
| 499 | |
|---|
| 500 | // format each col |
|---|
| 501 | foreach ($a_show_cols as $col) |
|---|
| 502 | $table->add($col, Q($sql_arr[$col])); |
|---|
| 503 | |
|---|
| 504 | $c++; |
|---|
| 505 | } |
|---|
| 506 | } |
|---|
| 507 | else |
|---|
| 508 | { |
|---|
| 509 | foreach ($table_data as $row_data) |
|---|
| 510 | { |
|---|
| 511 | $zebra_class = $c % 2 ? 'even' : 'odd'; |
|---|
| 512 | $table->add_row(array('id' => 'rcmrow' . $row_data[$id_col], 'class' => "contact $zebra_class")); |
|---|
| 513 | |
|---|
| 514 | // format each col |
|---|
| 515 | foreach ($a_show_cols as $col) |
|---|
| 516 | $table->add($col, Q($row_data[$col])); |
|---|
| 517 | |
|---|
| 518 | $c++; |
|---|
| 519 | } |
|---|
| 520 | } |
|---|
| 521 | |
|---|
| 522 | return $table->show($attrib); |
|---|
| 523 | } |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | /** |
|---|
| 527 | * Create an edit field for inclusion on a form |
|---|
| 528 | * |
|---|
| 529 | * @param string col field name |
|---|
| 530 | * @param string value field value |
|---|
| 531 | * @param array attrib HTML element attributes for field |
|---|
| 532 | * @param string type HTML element type (default 'text') |
|---|
| 533 | * @return string HTML field definition |
|---|
| 534 | */ |
|---|
| 535 | function rcmail_get_edit_field($col, $value, $attrib, $type='text') |
|---|
| 536 | { |
|---|
| 537 | $fname = '_'.$col; |
|---|
| 538 | $attrib['name'] = $fname; |
|---|
| 539 | |
|---|
| 540 | if ($type=='checkbox') |
|---|
| 541 | { |
|---|
| 542 | $attrib['value'] = '1'; |
|---|
| 543 | $input = new html_checkbox($attrib); |
|---|
| 544 | } |
|---|
| 545 | else if ($type=='textarea') |
|---|
| 546 | { |
|---|
| 547 | $attrib['cols'] = $attrib['size']; |
|---|
| 548 | $input = new html_textarea($attrib); |
|---|
| 549 | } |
|---|
| 550 | else |
|---|
| 551 | $input = new html_inputfield($attrib); |
|---|
| 552 | |
|---|
| 553 | // use value from post |
|---|
| 554 | if (!empty($_POST[$fname])) |
|---|
| 555 | $value = get_input_value($fname, RCUBE_INPUT_POST); |
|---|
| 556 | |
|---|
| 557 | $out = $input->show($value); |
|---|
| 558 | |
|---|
| 559 | return $out; |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | |
|---|
| 563 | /** |
|---|
| 564 | * Replace all css definitions with #container [def] |
|---|
| 565 | * and remove css-inlined scripting |
|---|
| 566 | * |
|---|
| 567 | * @param string CSS source code |
|---|
| 568 | * @param string Container ID to use as prefix |
|---|
| 569 | * @return string Modified CSS source |
|---|
| 570 | */ |
|---|
| 571 | function rcmail_mod_css_styles($source, $container_id, $base_url = '') |
|---|
| 572 | { |
|---|
| 573 | $a_css_values = array(); |
|---|
| 574 | $last_pos = 0; |
|---|
| 575 | |
|---|
| 576 | // ignore the whole block if evil styles are detected |
|---|
| 577 | $stripped = preg_replace('/[^a-z\(:]/', '', rcmail_xss_entitiy_decode($source)); |
|---|
| 578 | if (preg_match('/expression|behavior|url\(|import/', $stripped)) |
|---|
| 579 | return ''; |
|---|
| 580 | |
|---|
| 581 | // cut out all contents between { and } |
|---|
| 582 | while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos))) |
|---|
| 583 | { |
|---|
| 584 | $key = sizeof($a_css_values); |
|---|
| 585 | $a_css_values[$key] = substr($source, $pos+1, $pos2-($pos+1)); |
|---|
| 586 | $source = substr($source, 0, $pos+1) . "<<str_replacement[$key]>>" . substr($source, $pos2, strlen($source)-$pos2); |
|---|
| 587 | $last_pos = $pos+2; |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | // remove html comments and add #container to each tag selector. |
|---|
| 591 | // also replace body definition because we also stripped off the <body> tag |
|---|
| 592 | $styles = preg_replace( |
|---|
| 593 | array( |
|---|
| 594 | '/(^\s*<!--)|(-->\s*$)/', |
|---|
| 595 | '/(^\s*|,\s*|\}\s*)([a-z0-9\._#][a-z0-9\.\-_]*)/im', |
|---|
| 596 | '/@import\s+(url\()?[\'"]?([^\)\'"]+)[\'"]?(\))?/ime', |
|---|
| 597 | '/<<str_replacement\[([0-9]+)\]>>/e', |
|---|
| 598 | "/$container_id\s+body/i" |
|---|
| 599 | ), |
|---|
| 600 | array( |
|---|
| 601 | '', |
|---|
| 602 | "\\1#$container_id \\2", |
|---|
| 603 | "sprintf(\"@import url('./bin/modcss.php?u=%s&c=%s')\", urlencode(make_absolute_url('\\2','$base_url')), urlencode($container_id))", |
|---|
| 604 | "\$a_css_values[\\1]", |
|---|
| 605 | "$container_id div.rcmBody" |
|---|
| 606 | ), |
|---|
| 607 | $source); |
|---|
| 608 | |
|---|
| 609 | return $styles; |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | |
|---|
| 613 | /** |
|---|
| 614 | * Decode escaped entities used by known XSS exploits. |
|---|
| 615 | * See http://downloads.securityfocus.com/vulnerabilities/exploits/26800.eml for examples |
|---|
| 616 | * |
|---|
| 617 | * @param string CSS content to decode |
|---|
| 618 | * @return string Decoded string |
|---|
| 619 | */ |
|---|
| 620 | function rcmail_xss_entitiy_decode($content) |
|---|
| 621 | { |
|---|
| 622 | $out = html_entity_decode(html_entity_decode($content)); |
|---|
| 623 | $out = preg_replace('/\\\([0-9a-f]{4})/ie', "chr(hexdec('\\1'))", $out); |
|---|
| 624 | $out = preg_replace('#/\*.*\*/#Um', '', $out); |
|---|
| 625 | return $out; |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | /** |
|---|
| 630 | * Compose a valid attribute string for HTML tags |
|---|
| 631 | * |
|---|
| 632 | * @param array Named tag attributes |
|---|
| 633 | * @param array List of allowed attributes |
|---|
| 634 | * @return string HTML formatted attribute string |
|---|
| 635 | */ |
|---|
| 636 | function create_attrib_string($attrib, $allowed_attribs=array('id', 'class', 'style')) |
|---|
| 637 | { |
|---|
| 638 | // allow the following attributes to be added to the <iframe> tag |
|---|
| 639 | $attrib_str = ''; |
|---|
| 640 | foreach ($allowed_attribs as $a) |
|---|
| 641 | if (isset($attrib[$a])) |
|---|
| 642 | $attrib_str .= sprintf(' %s="%s"', $a, str_replace('"', '"', $attrib[$a])); |
|---|
| 643 | |
|---|
| 644 | return $attrib_str; |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | |
|---|
| 648 | /** |
|---|
| 649 | * Convert a HTML attribute string attributes to an associative array (name => value) |
|---|
| 650 | * |
|---|
| 651 | * @param string Input string |
|---|
| 652 | * @return array Key-value pairs of parsed attributes |
|---|
| 653 | */ |
|---|
| 654 | function parse_attrib_string($str) |
|---|
| 655 | { |
|---|
| 656 | $attrib = array(); |
|---|
| 657 | preg_match_all('/\s*([-_a-z]+)=(["\'])??(?(2)([^\2]+)\2|(\S+?))/Ui', stripslashes($str), $regs, PREG_SET_ORDER); |
|---|
| 658 | |
|---|
| 659 | // convert attributes to an associative array (name => value) |
|---|
| 660 | if ($regs) |
|---|
| 661 | foreach ($regs as $attr) |
|---|
| 662 | { |
|---|
| 663 | $attrib[strtolower($attr[1])] = $attr[3] . $attr[4]; |
|---|
| 664 | } |
|---|
| 665 | |
|---|
| 666 | return $attrib; |
|---|
| 667 | } |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | /** |
|---|
| 671 | * Convert the given date to a human readable form |
|---|
| 672 | * This uses the date formatting properties from config |
|---|
| 673 | * |
|---|
| 674 | * @param mixed Date representation (string or timestamp) |
|---|
| 675 | * @param string Date format to use |
|---|
| 676 | * @return string Formatted date string |
|---|
| 677 | */ |
|---|
| 678 | function format_date($date, $format=NULL) |
|---|
| 679 | { |
|---|
| 680 | global $CONFIG; |
|---|
| 681 | |
|---|
| 682 | $ts = NULL; |
|---|
| 683 | |
|---|
| 684 | if (is_numeric($date)) |
|---|
| 685 | $ts = $date; |
|---|
| 686 | else if (!empty($date)) |
|---|
| 687 | { |
|---|
| 688 | while (($ts = @strtotime($date))===false) |
|---|
| 689 | { |
|---|
| 690 | // if we have a date in non-rfc format |
|---|
| 691 | // remove token from the end and try again |
|---|
| 692 | $d = explode(' ', $date); |
|---|
| 693 | array_pop($d); |
|---|
| 694 | if (!$d) break; |
|---|
| 695 | $date = implode(' ', $d); |
|---|
| 696 | } |
|---|
| 697 | } |
|---|
| 698 | |
|---|
| 699 | if (empty($ts)) |
|---|
| 700 | return ''; |
|---|
| 701 | |
|---|
| 702 | // get user's timezone |
|---|
| 703 | if ($CONFIG['timezone'] === 'auto') |
|---|
| 704 | $tz = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : date('Z')/3600; |
|---|
| 705 | else { |
|---|
| 706 | $tz = $CONFIG['timezone']; |
|---|
| 707 | if ($CONFIG['dst_active']) |
|---|
| 708 | $tz++; |
|---|
| 709 | } |
|---|
| 710 | |
|---|
| 711 | // convert time to user's timezone |
|---|
| 712 | $timestamp = $ts - date('Z', $ts) + ($tz * 3600); |
|---|
| 713 | |
|---|
| 714 | // get current timestamp in user's timezone |
|---|
| 715 | $now = time(); // local time |
|---|
| 716 | $now -= (int)date('Z'); // make GMT time |
|---|
| 717 | $now += ($tz * 3600); // user's time |
|---|
| 718 | $now_date = getdate($now); |
|---|
| 719 | |
|---|
| 720 | $today_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday'], $now_date['year']); |
|---|
| 721 | $week_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday']-6, $now_date['year']); |
|---|
| 722 | |
|---|
| 723 | // define date format depending on current time |
|---|
| 724 | if ($CONFIG['prettydate'] && !$format && $timestamp > $today_limit && $timestamp < $now) |
|---|
| 725 | return sprintf('%s %s', rcube_label('today'), date($CONFIG['date_today'] ? $CONFIG['date_today'] : 'H:i', $timestamp)); |
|---|
| 726 | else if ($CONFIG['prettydate'] && !$format && $timestamp > $week_limit && $timestamp < $now) |
|---|
| 727 | $format = $CONFIG['date_short'] ? $CONFIG['date_short'] : 'D H:i'; |
|---|
| 728 | else if (!$format) |
|---|
| 729 | $format = $CONFIG['date_long'] ? $CONFIG['date_long'] : 'd.m.Y H:i'; |
|---|
| 730 | |
|---|
| 731 | |
|---|
| 732 | // parse format string manually in order to provide localized weekday and month names |
|---|
| 733 | // an alternative would be to convert the date() format string to fit with strftime() |
|---|
| 734 | $out = ''; |
|---|
| 735 | for($i=0; $i<strlen($format); $i++) |
|---|
| 736 | { |
|---|
| 737 | if ($format{$i}=='\\') // skip escape chars |
|---|
| 738 | continue; |
|---|
| 739 | |
|---|
| 740 | // write char "as-is" |
|---|
| 741 | if ($format{$i}==' ' || $format{$i-1}=='\\') |
|---|
| 742 | $out .= $format{$i}; |
|---|
| 743 | // weekday (short) |
|---|
| 744 | else if ($format{$i}=='D') |
|---|
| 745 | $out .= rcube_label(strtolower(date('D', $timestamp))); |
|---|
| 746 | // weekday long |
|---|
| 747 | else if ($format{$i}=='l') |
|---|
| 748 | $out .= rcube_label(strtolower(date('l', $timestamp))); |
|---|
| 749 | // month name (short) |
|---|
| 750 | else if ($format{$i}=='M') |
|---|
| 751 | $out .= rcube_label(strtolower(date('M', $timestamp))); |
|---|
| 752 | // month name (long) |
|---|
| 753 | else if ($format{$i}=='F') |
|---|
| 754 | $out .= rcube_label('long'.strtolower(date('M', $timestamp))); |
|---|
| 755 | else if ($format{$i}=='x') |
|---|
| 756 | $out .= strftime('%x %X', $timestamp); |
|---|
| 757 | else |
|---|
| 758 | $out .= date($format{$i}, $timestamp); |
|---|
| 759 | } |
|---|
| 760 | |
|---|
| 761 | return $out; |
|---|
| 762 | } |
|---|
| 763 | |
|---|
| 764 | |
|---|
| 765 | /** |
|---|
| 766 | * Compose a valid representaion of name and e-mail address |
|---|
| 767 | * |
|---|
| 768 | * @param string E-mail address |
|---|
| 769 | * @param string Person name |
|---|
| 770 | * @return string Formatted string |
|---|
| 771 | */ |
|---|
| 772 | function format_email_recipient($email, $name='') |
|---|
| 773 | { |
|---|
| 774 | if ($name && $name != $email) |
|---|
| 775 | { |
|---|
| 776 | // Special chars as defined by RFC 822 need to in quoted string (or escaped). |
|---|
| 777 | return sprintf('%s <%s>', preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $name) ? '"'.addcslashes($name, '"').'"' : $name, $email); |
|---|
| 778 | } |
|---|
| 779 | else |
|---|
| 780 | return $email; |
|---|
| 781 | } |
|---|
| 782 | |
|---|
| 783 | |
|---|
| 784 | |
|---|
| 785 | /****** debugging functions ********/ |
|---|
| 786 | |
|---|
| 787 | |
|---|
| 788 | /** |
|---|
| 789 | * Print or write debug messages |
|---|
| 790 | * |
|---|
| 791 | * @param mixed Debug message or data |
|---|
| 792 | */ |
|---|
| 793 | function console() |
|---|
| 794 | { |
|---|
| 795 | $msg = array(); |
|---|
| 796 | foreach (func_get_args() as $arg) |
|---|
| 797 | $msg[] = !is_string($arg) ? var_export($arg, true) : $arg; |
|---|
| 798 | |
|---|
| 799 | if (!($GLOBALS['CONFIG']['debug_level'] & 4)) |
|---|
| 800 | write_log('console', join(";\n", $msg)); |
|---|
| 801 | else if ($GLOBALS['OUTPUT']->ajax_call) |
|---|
| 802 | print "/*\n " . join(";\n", $msg) . " \n*/\n"; |
|---|
| 803 | else |
|---|
| 804 | { |
|---|
| 805 | print '<div style="background:#eee; border:1px solid #ccc; margin-bottom:3px; padding:6px"><pre>'; |
|---|
| 806 | print join(";<br/>\n", $msg); |
|---|
| 807 | print "</pre></div>\n"; |
|---|
| 808 | } |
|---|
| 809 | } |
|---|
| 810 | |
|---|
| 811 | |
|---|
| 812 | /** |
|---|
| 813 | * Append a line to a logfile in the logs directory. |
|---|
| 814 | * Date will be added automatically to the line. |
|---|
| 815 | * |
|---|
| 816 | * @param $name name of log file |
|---|
| 817 | * @param line Line to append |
|---|
| 818 | */ |
|---|
| 819 | function write_log($name, $line) |
|---|
| 820 | { |
|---|
| 821 | global $CONFIG; |
|---|
| 822 | |
|---|
| 823 | if (!is_string($line)) |
|---|
| 824 | $line = var_export($line, true); |
|---|
| 825 | |
|---|
| 826 | $log_entry = sprintf("[%s]: %s\n", |
|---|
| 827 | date("d-M-Y H:i:s O", mktime()), |
|---|
| 828 | $line); |
|---|
| 829 | |
|---|
| 830 | if ($CONFIG['log_driver'] == 'syslog') { |
|---|
| 831 | if ($name == 'errors') |
|---|
| 832 | $prio = LOG_ERR; |
|---|
| 833 | else |
|---|
| 834 | $prio = LOG_INFO; |
|---|
| 835 | syslog($prio, $log_entry); |
|---|
| 836 | } else { |
|---|
| 837 | // log_driver == 'file' is assumed here |
|---|
| 838 | if (empty($CONFIG['log_dir'])) |
|---|
| 839 | $CONFIG['log_dir'] = INSTALL_PATH.'logs'; |
|---|
| 840 | |
|---|
| 841 | // try to open specific log file for writing |
|---|
| 842 | if ($fp = @fopen($CONFIG['log_dir'].'/'.$name, 'a')) { |
|---|
| 843 | fwrite($fp, $log_entry); |
|---|
| 844 | fclose($fp); |
|---|
| 845 | } |
|---|
| 846 | } |
|---|
| 847 | } |
|---|
| 848 | |
|---|
| 849 | |
|---|
| 850 | /** |
|---|
| 851 | * @access private |
|---|
| 852 | */ |
|---|
| 853 | function rcube_timer() |
|---|
| 854 | { |
|---|
| 855 | list($usec, $sec) = explode(" ", microtime()); |
|---|
| 856 | return ((float)$usec + (float)$sec); |
|---|
| 857 | } |
|---|
| 858 | |
|---|
| 859 | |
|---|
| 860 | /** |
|---|
| 861 | * @access private |
|---|
| 862 | */ |
|---|
| 863 | function rcube_print_time($timer, $label='Timer') |
|---|
| 864 | { |
|---|
| 865 | static $print_count = 0; |
|---|
| 866 | |
|---|
| 867 | $print_count++; |
|---|
| 868 | $now = rcube_timer(); |
|---|
| 869 | $diff = $now-$timer; |
|---|
| 870 | |
|---|
| 871 | if (empty($label)) |
|---|
| 872 | $label = 'Timer '.$print_count; |
|---|
| 873 | |
|---|
| 874 | console(sprintf("%s: %0.4f sec", $label, $diff)); |
|---|
| 875 | } |
|---|
| 876 | |
|---|
| 877 | |
|---|
| 878 | /** |
|---|
| 879 | * Return the mailboxlist in HTML |
|---|
| 880 | * |
|---|
| 881 | * @param array Named parameters |
|---|
| 882 | * @return string HTML code for the gui object |
|---|
| 883 | */ |
|---|
| 884 | function rcmail_mailbox_list($attrib) |
|---|
| 885 | { |
|---|
| 886 | global $RCMAIL; |
|---|
| 887 | static $a_mailboxes; |
|---|
| 888 | |
|---|
| 889 | $attrib += array('maxlength' => 100, 'relanames' => false); |
|---|
| 890 | |
|---|
| 891 | // add some labels to client |
|---|
| 892 | $RCMAIL->output->add_label('purgefolderconfirm', 'deletemessagesconfirm'); |
|---|
| 893 | |
|---|
| 894 | $type = $attrib['type'] ? $attrib['type'] : 'ul'; |
|---|
| 895 | unset($attrib['type']); |
|---|
| 896 | |
|---|
| 897 | if ($type=='ul' && !$attrib['id']) |
|---|
| 898 | $attrib['id'] = 'rcmboxlist'; |
|---|
| 899 | |
|---|
| 900 | // get mailbox list |
|---|
| 901 | $mbox_name = $RCMAIL->imap->get_mailbox_name(); |
|---|
| 902 | |
|---|
| 903 | // build the folders tree |
|---|
| 904 | if (empty($a_mailboxes)) { |
|---|
| 905 | // get mailbox list |
|---|
| 906 | $a_folders = $RCMAIL->imap->list_mailboxes(); |
|---|
| 907 | $delimiter = $RCMAIL->imap->get_hierarchy_delimiter(); |
|---|
| 908 | $a_mailboxes = array(); |
|---|
| 909 | |
|---|
| 910 | foreach ($a_folders as $folder) |
|---|
| 911 | rcmail_build_folder_tree($a_mailboxes, $folder, $delimiter); |
|---|
| 912 | } |
|---|
| 913 | |
|---|
| 914 | if ($type=='select') { |
|---|
| 915 | $select = new html_select($attrib); |
|---|
| 916 | |
|---|
| 917 | // add no-selection option |
|---|
| 918 | if ($attrib['noselection']) |
|---|
| 919 | $select->add(rcube_label($attrib['noselection']), '0'); |
|---|
| 920 | |
|---|
| 921 | rcmail_render_folder_tree_select($a_mailboxes, $mbox_name, $attrib['maxlength'], $select, $attrib['realnames']); |
|---|
| 922 | $out = $select->show(); |
|---|
| 923 | } |
|---|
| 924 | else { |
|---|
| 925 | $js_mailboxlist = array(); |
|---|
| 926 | $out = html::tag('ul', $attrib, rcmail_render_folder_tree_html($a_mailboxes, $mbox_name, $js_mailboxlist, $attrib), html::$common_attrib); |
|---|
| 927 | |
|---|
| 928 | $RCMAIL->output->add_gui_object('mailboxlist', $attrib['id']); |
|---|
| 929 | $RCMAIL->output->set_env('mailboxes', $js_mailboxlist); |
|---|
| 930 | $RCMAIL->output->set_env('collapsed_folders', $RCMAIL->config->get('collapsed_folders')); |
|---|
| 931 | } |
|---|
| 932 | |
|---|
| 933 | return $out; |
|---|
| 934 | } |
|---|
| 935 | |
|---|
| 936 | |
|---|
| 937 | /** |
|---|
| 938 | * Return the mailboxlist as html_select object |
|---|
| 939 | * |
|---|
| 940 | * @param array Named parameters |
|---|
| 941 | * @return object html_select HTML drop-down object |
|---|
| 942 | */ |
|---|
| 943 | function rcmail_mailbox_select($p = array()) |
|---|
| 944 | { |
|---|
| 945 | global $RCMAIL; |
|---|
| 946 | |
|---|
| 947 | $p += array('maxlength' => 100, 'relanames' => false); |
|---|
| 948 | $a_mailboxes = array(); |
|---|
| 949 | |
|---|
| 950 | foreach ($RCMAIL->imap->list_mailboxes() as $folder) |
|---|
| 951 | rcmail_build_folder_tree($a_mailboxes, $folder, $RCMAIL->imap->get_hierarchy_delimiter()); |
|---|
| 952 | |
|---|
| 953 | $select = new html_select($p); |
|---|
| 954 | |
|---|
| 955 | if ($p['noselection']) |
|---|
| 956 | $select->add($p['noselection'], ''); |
|---|
| 957 | |
|---|
| 958 | rcmail_render_folder_tree_select($a_mailboxes, $mbox, $p['maxlength'], $select, $p['realnames']); |
|---|
| 959 | |
|---|
| 960 | return $select; |
|---|
| 961 | } |
|---|
| 962 | |
|---|
| 963 | |
|---|
| 964 | /** |
|---|
| 965 | * Create a hierarchical array of the mailbox list |
|---|
| 966 | * @access private |
|---|
| 967 | */ |
|---|
| 968 | function rcmail_build_folder_tree(&$arrFolders, $folder, $delm='/', $path='') |
|---|
| 969 | { |
|---|
| 970 | $pos = strpos($folder, $delm); |
|---|
| 971 | if ($pos !== false) { |
|---|
| 972 | $subFolders = substr($folder, $pos+1); |
|---|
| 973 | $currentFolder = substr($folder, 0, $pos); |
|---|
| 974 | $virtual = !isset($arrFolders[$currentFolder]); |
|---|
| 975 | } |
|---|
| 976 | else { |
|---|
| 977 | $subFolders = false; |
|---|
| 978 | $currentFolder = $folder; |
|---|
| 979 | $virtual = false; |
|---|
| 980 | } |
|---|
| 981 | |
|---|
| 982 | $path .= $currentFolder; |
|---|
| 983 | |
|---|
| 984 | if (!isset($arrFolders[$currentFolder])) { |
|---|
| 985 | $arrFolders[$currentFolder] = array( |
|---|
| 986 | 'id' => $path, |
|---|
| 987 | 'name' => rcube_charset_convert($currentFolder, 'UTF-7'), |
|---|
| 988 | 'virtual' => $virtual, |
|---|
| 989 | 'folders' => array()); |
|---|
| 990 | } |
|---|
| 991 | else |
|---|
| 992 | $arrFolders[$currentFolder]['virtual'] = $virtual; |
|---|
| 993 | |
|---|
| 994 | if (!empty($subFolders)) |
|---|
| 995 | rcmail_build_folder_tree($arrFolders[$currentFolder]['folders'], $subFolders, $delm, $path.$delm); |
|---|
| 996 | } |
|---|
| 997 | |
|---|
| 998 | |
|---|
| 999 | /** |
|---|
| 1000 | * Return html for a structured list <ul> for the mailbox tree |
|---|
| 1001 | * @access private |
|---|
| 1002 | */ |
|---|
| 1003 | function rcmail_render_folder_tree_html(&$arrFolders, &$mbox_name, &$jslist, $attrib, $nestLevel=0) |
|---|
| 1004 | { |
|---|
| 1005 | global $RCMAIL, $CONFIG; |
|---|
| 1006 | |
|---|
| 1007 | $maxlength = intval($attrib['maxlength']); |
|---|
| 1008 | $realnames = (bool)$attrib['realnames']; |
|---|
| 1009 | $msgcounts = $RCMAIL->imap->get_cache('messagecount'); |
|---|
| 1010 | |
|---|
| 1011 | $idx = 0; |
|---|
| 1012 | $out = ''; |
|---|
| 1013 | foreach ($arrFolders as $key => $folder) { |
|---|
| 1014 | $zebra_class = (($nestLevel+1)*$idx) % 2 == 0 ? 'even' : 'odd'; |
|---|
| 1015 | $title = null; |
|---|
| 1016 | |
|---|
| 1017 | if (($folder_class = rcmail_folder_classname($folder['id'])) && !$realnames) { |
|---|
| 1018 | $foldername = rcube_label($folder_class); |
|---|
| 1019 | } |
|---|
| 1020 | else { |
|---|
| 1021 | $foldername = $folder['name']; |
|---|
| 1022 | |
|---|
| 1023 | // shorten the folder name to a given length |
|---|
| 1024 | if ($maxlength && $maxlength > 1) { |
|---|
| 1025 | $fname = abbreviate_string($foldername, $maxlength); |
|---|
| 1026 | if ($fname != $foldername) |
|---|
| 1027 | $title = $foldername; |
|---|
| 1028 | $foldername = $fname; |
|---|
| 1029 | } |
|---|
| 1030 | } |
|---|
| 1031 | |
|---|
| 1032 | // make folder name safe for ids and class names |
|---|
| 1033 | $folder_id = asciiwords($folder['id'], true); |
|---|
| 1034 | $classes = array('mailbox'); |
|---|
| 1035 | |
|---|
| 1036 | // set special class for Sent, Drafts, Trash and Junk |
|---|
| 1037 | if ($folder['id']==$CONFIG['sent_mbox']) |
|---|
| 1038 | $classes[] = 'sent'; |
|---|
| 1039 | else if ($folder['id']==$CONFIG['drafts_mbox']) |
|---|
| 1040 | $classes[] = 'drafts'; |
|---|
| 1041 | else if ($folder['id']==$CONFIG['trash_mbox']) |
|---|
| 1042 | $classes[] = 'trash'; |
|---|
| 1043 | else if ($folder['id']==$CONFIG['junk_mbox']) |
|---|
| 1044 | $classes[] = 'junk'; |
|---|
| 1045 | else if ($folder['id']=='INBOX') |
|---|
| 1046 | $classes[] = 'inbox'; |
|---|
| 1047 | else |
|---|
| 1048 | $classes[] = '_'.asciiwords($folder_class ? $folder_class : strtolower($folder['id']), true); |
|---|
| 1049 | |
|---|
| 1050 | $classes[] = $zebra_class; |
|---|
| 1051 | |
|---|
| 1052 | if ($folder['id'] == $mbox_name) |
|---|
| 1053 | $classes[] = 'selected'; |
|---|
| 1054 | |
|---|
| 1055 | $collapsed = preg_match('/&'.rawurlencode($folder['id']).'&/', $RCMAIL->config->get('collapsed_folders')); |
|---|
| 1056 | $unread = $msgcounts ? intval($msgcounts[$folder['id']]['UNSEEN']) : 0; |
|---|
| 1057 | |
|---|
| 1058 | if ($folder['virtual']) |
|---|
| 1059 | $classes[] = 'virtual'; |
|---|
| 1060 | else if ($unread) |
|---|
| 1061 | $classes[] = 'unread'; |
|---|
| 1062 | |
|---|
| 1063 | $js_name = JQ($folder['id']); |
|---|
| 1064 | $html_name = Q($foldername . ($unread ? " ($unread)" : '')); |
|---|
| 1065 | $link_attrib = $folder['virtual'] ? array() : array( |
|---|
| 1066 | 'href' => rcmail_url('', array('_mbox' => $folder['id'])), |
|---|
| 1067 | 'onclick' => sprintf("return %s.command('list','%s',this)", JS_OBJECT_NAME, $js_name), |
|---|
| 1068 | 'title' => $title, |
|---|
| 1069 | ); |
|---|
| 1070 | |
|---|
| 1071 | $out .= html::tag('li', array( |
|---|
| 1072 | 'id' => "rcmli".$folder_id, |
|---|
| 1073 | 'class' => join(' ', $classes), |
|---|
| 1074 | 'noclose' => true), |
|---|
| 1075 | html::a($link_attrib, $html_name) . |
|---|
| 1076 | (!empty($folder['folders']) ? html::div(array( |
|---|
| 1077 | 'class' => ($collapsed ? 'collapsed' : 'expanded'), |
|---|
| 1078 | 'style' => "position:absolute", |
|---|
| 1079 | 'onclick' => sprintf("%s.command('collapse-folder', '%s')", JS_OBJECT_NAME, $js_name) |
|---|
| 1080 | ), ' ') : '')); |
|---|
| 1081 | |
|---|
| 1082 | $jslist[$folder_id] = array('id' => $folder['id'], 'name' => $foldername, 'virtual' => $folder['virtual']); |
|---|
| 1083 | |
|---|
| 1084 | if (!empty($folder['folders'])) { |
|---|
| 1085 | $out .= html::tag('ul', array('style' => ($collapsed ? "display:none;" : null)), |
|---|
| 1086 | rcmail_render_folder_tree_html($folder['folders'], $mbox_name, $jslist, $attrib, $nestLevel+1)); |
|---|
| 1087 | } |
|---|
| 1088 | |
|---|
| 1089 | $out .= "</li>\n"; |
|---|
| 1090 | $idx++; |
|---|
| 1091 | } |
|---|
| 1092 | |
|---|
| 1093 | return $out; |
|---|
| 1094 | } |
|---|
| 1095 | |
|---|
| 1096 | |
|---|
| 1097 | /** |
|---|
| 1098 | * Return html for a flat list <select> for the mailbox tree |
|---|
| 1099 | * @access private |
|---|
| 1100 | */ |
|---|
| 1101 | function rcmail_render_folder_tree_select(&$arrFolders, &$mbox_name, $maxlength, &$select, $realnames=false, $nestLevel=0) |
|---|
| 1102 | { |
|---|
| 1103 | $idx = 0; |
|---|
| 1104 | $out = ''; |
|---|
| 1105 | foreach ($arrFolders as $key=>$folder) |
|---|
| 1106 | { |
|---|
| 1107 | if (!$realnames && ($folder_class = rcmail_folder_classname($folder['id']))) |
|---|
| 1108 | $foldername = rcube_label($folder_class); |
|---|
| 1109 | else |
|---|
| 1110 | { |
|---|
| 1111 | $foldername = $folder['name']; |
|---|
| 1112 | |
|---|
| 1113 | // shorten the folder name to a given length |
|---|
| 1114 | if ($maxlength && $maxlength>1) |
|---|
| 1115 | $foldername = abbreviate_string($foldername, $maxlength); |
|---|
| 1116 | } |
|---|
| 1117 | |
|---|
| 1118 | $select->add(str_repeat(' ', $nestLevel*4) . $foldername, $folder['id']); |
|---|
| 1119 | |
|---|
| 1120 | if (!empty($folder['folders'])) |
|---|
| 1121 | $out .= rcmail_render_folder_tree_select($folder['folders'], $mbox_name, $maxlength, $select, $realnames, $nestLevel+1); |
|---|
| 1122 | |
|---|
| 1123 | $idx++; |
|---|
| 1124 | } |
|---|
| 1125 | |
|---|
| 1126 | return $out; |
|---|
| 1127 | } |
|---|
| 1128 | |
|---|
| 1129 | |
|---|
| 1130 | /** |
|---|
| 1131 | * Return internal name for the given folder if it matches the configured special folders |
|---|
| 1132 | * @access private |
|---|
| 1133 | */ |
|---|
| 1134 | function rcmail_folder_classname($folder_id) |
|---|
| 1135 | { |
|---|
| 1136 | global $CONFIG; |
|---|
| 1137 | |
|---|
| 1138 | $cname = null; |
|---|
| 1139 | $folder_lc = strtolower($folder_id); |
|---|
| 1140 | |
|---|
| 1141 | // for these mailboxes we have localized labels and css classes |
|---|
| 1142 | foreach (array('inbox', 'sent', 'drafts', 'trash', 'junk') as $smbx) |
|---|
| 1143 | { |
|---|
| 1144 | if ($folder_lc == $smbx || $folder_id == $CONFIG[$smbx.'_mbox']) |
|---|
| 1145 | $cname = $smbx; |
|---|
| 1146 | } |
|---|
| 1147 | |
|---|
| 1148 | return $cname; |
|---|
| 1149 | } |
|---|
| 1150 | |
|---|
| 1151 | |
|---|
| 1152 | /** |
|---|
| 1153 | * Try to localize the given IMAP folder name. |
|---|
| 1154 | * UTF-7 decode it in case no localized text was found |
|---|
| 1155 | * |
|---|
| 1156 | * @param string Folder name |
|---|
| 1157 | * @return string Localized folder name in UTF-8 encoding |
|---|
| 1158 | */ |
|---|
| 1159 | function rcmail_localize_foldername($name) |
|---|
| 1160 | { |
|---|
| 1161 | if ($folder_class = rcmail_folder_classname($name)) |
|---|
| 1162 | return rcube_label($folder_class); |
|---|
| 1163 | else |
|---|
| 1164 | return rcube_charset_convert($name, 'UTF-7'); |
|---|
| 1165 | } |
|---|
| 1166 | |
|---|
| 1167 | |
|---|
| 1168 | ?> |
|---|