| 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-2011, The Roundcube Dev Team | |
|---|
| 9 | | | |
|---|
| 10 | | Licensed under the GNU General Public License version 3 or | |
|---|
| 11 | | any later version with exceptions for skins & plugins. | |
|---|
| 12 | | See the README file for a full license statement. | |
|---|
| 13 | | | |
|---|
| 14 | | PURPOSE: | |
|---|
| 15 | | Provide basic functions for the webmail package | |
|---|
| 16 | | | |
|---|
| 17 | +-----------------------------------------------------------------------+ |
|---|
| 18 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 19 | +-----------------------------------------------------------------------+ |
|---|
| 20 | |
|---|
| 21 | $Id$ |
|---|
| 22 | |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Roundcube Webmail common functions |
|---|
| 27 | * |
|---|
| 28 | * @package Core |
|---|
| 29 | * @author Thomas Bruederli <roundcube@gmail.com> |
|---|
| 30 | */ |
|---|
| 31 | |
|---|
| 32 | require_once INSTALL_PATH . 'program/include/rcube_shared.inc'; |
|---|
| 33 | |
|---|
| 34 | // define constannts for input reading |
|---|
| 35 | define('RCUBE_INPUT_GET', 0x0101); |
|---|
| 36 | define('RCUBE_INPUT_POST', 0x0102); |
|---|
| 37 | define('RCUBE_INPUT_GPC', 0x0103); |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * Return correct name for a specific database table |
|---|
| 43 | * |
|---|
| 44 | * @param string Table name |
|---|
| 45 | * @return string Translated table name |
|---|
| 46 | */ |
|---|
| 47 | function get_table_name($table) |
|---|
| 48 | { |
|---|
| 49 | global $CONFIG; |
|---|
| 50 | |
|---|
| 51 | // return table name if configured |
|---|
| 52 | $config_key = 'db_table_'.$table; |
|---|
| 53 | |
|---|
| 54 | if (strlen($CONFIG[$config_key])) |
|---|
| 55 | return $CONFIG[$config_key]; |
|---|
| 56 | |
|---|
| 57 | return $table; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * Return correct name for a specific database sequence |
|---|
| 63 | * (used for Postgres only) |
|---|
| 64 | * |
|---|
| 65 | * @param string Secuence name |
|---|
| 66 | * @return string Translated sequence name |
|---|
| 67 | */ |
|---|
| 68 | function get_sequence_name($sequence) |
|---|
| 69 | { |
|---|
| 70 | // return sequence name if configured |
|---|
| 71 | $config_key = 'db_sequence_'.$sequence; |
|---|
| 72 | $opt = rcmail::get_instance()->config->get($config_key); |
|---|
| 73 | |
|---|
| 74 | if (!empty($opt)) |
|---|
| 75 | return $opt; |
|---|
| 76 | |
|---|
| 77 | return $sequence; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | * Get localized text in the desired language |
|---|
| 83 | * It's a global wrapper for rcmail::gettext() |
|---|
| 84 | * |
|---|
| 85 | * @param mixed Named parameters array or label name |
|---|
| 86 | * @param string Domain to search in (e.g. plugin name) |
|---|
| 87 | * @return string Localized text |
|---|
| 88 | * @see rcmail::gettext() |
|---|
| 89 | */ |
|---|
| 90 | function rcube_label($p, $domain=null) |
|---|
| 91 | { |
|---|
| 92 | return rcmail::get_instance()->gettext($p, $domain); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * Global wrapper of rcmail::text_exists() |
|---|
| 98 | * to check whether a text label is defined |
|---|
| 99 | * |
|---|
| 100 | * @see rcmail::text_exists() |
|---|
| 101 | */ |
|---|
| 102 | function rcube_label_exists($name, $domain=null, &$ref_domain = null) |
|---|
| 103 | { |
|---|
| 104 | return rcmail::get_instance()->text_exists($name, $domain, $ref_domain); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * Overwrite action variable |
|---|
| 110 | * |
|---|
| 111 | * @param string New action value |
|---|
| 112 | */ |
|---|
| 113 | function rcmail_overwrite_action($action) |
|---|
| 114 | { |
|---|
| 115 | $app = rcmail::get_instance(); |
|---|
| 116 | $app->action = $action; |
|---|
| 117 | $app->output->set_env('action', $action); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | * Compose an URL for a specific action |
|---|
| 123 | * |
|---|
| 124 | * @param string Request action |
|---|
| 125 | * @param array More URL parameters |
|---|
| 126 | * @param string Request task (omit if the same) |
|---|
| 127 | * @return The application URL |
|---|
| 128 | */ |
|---|
| 129 | function rcmail_url($action, $p=array(), $task=null) |
|---|
| 130 | { |
|---|
| 131 | $app = rcmail::get_instance(); |
|---|
| 132 | return $app->url((array)$p + array('_action' => $action, 'task' => $task)); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | * Garbage collector function for temp files. |
|---|
| 138 | * Remove temp files older than two days |
|---|
| 139 | */ |
|---|
| 140 | function rcmail_temp_gc() |
|---|
| 141 | { |
|---|
| 142 | $rcmail = rcmail::get_instance(); |
|---|
| 143 | |
|---|
| 144 | $tmp = unslashify($rcmail->config->get('temp_dir')); |
|---|
| 145 | $expire = mktime() - 172800; // expire in 48 hours |
|---|
| 146 | |
|---|
| 147 | if ($dir = opendir($tmp)) { |
|---|
| 148 | while (($fname = readdir($dir)) !== false) { |
|---|
| 149 | if ($fname{0} == '.') |
|---|
| 150 | continue; |
|---|
| 151 | |
|---|
| 152 | if (filemtime($tmp.'/'.$fname) < $expire) |
|---|
| 153 | @unlink($tmp.'/'.$fname); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | closedir($dir); |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | // Deprecated |
|---|
| 162 | function rcube_charset_convert($str, $from, $to=NULL) |
|---|
| 163 | { |
|---|
| 164 | return rcube_charset::convert($str, $from, $to); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | // Deprecated |
|---|
| 169 | function rc_detect_encoding($string, $failover='') |
|---|
| 170 | { |
|---|
| 171 | return rcube_charset::detect($string, $failover); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | // Deprecated |
|---|
| 176 | function rc_utf8_clean($input) |
|---|
| 177 | { |
|---|
| 178 | return rcube_charset::clean($input); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | * Convert a variable into a javascript object notation |
|---|
| 184 | * |
|---|
| 185 | * @param mixed Input value |
|---|
| 186 | * @return string Serialized JSON string |
|---|
| 187 | */ |
|---|
| 188 | function json_serialize($input) |
|---|
| 189 | { |
|---|
| 190 | $input = rcube_charset::clean($input); |
|---|
| 191 | |
|---|
| 192 | // sometimes even using rcube_charset::clean() the input contains invalid UTF-8 sequences |
|---|
| 193 | // that's why we have @ here |
|---|
| 194 | return @json_encode($input); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | /** |
|---|
| 199 | * Replacing specials characters to a specific encoding type |
|---|
| 200 | * |
|---|
| 201 | * @param string Input string |
|---|
| 202 | * @param string Encoding type: text|html|xml|js|url |
|---|
| 203 | * @param string Replace mode for tags: show|replace|remove |
|---|
| 204 | * @param boolean Convert newlines |
|---|
| 205 | * @return string The quoted string |
|---|
| 206 | */ |
|---|
| 207 | function rep_specialchars_output($str, $enctype='', $mode='', $newlines=TRUE) |
|---|
| 208 | { |
|---|
| 209 | static $html_encode_arr = false; |
|---|
| 210 | static $js_rep_table = false; |
|---|
| 211 | static $xml_rep_table = false; |
|---|
| 212 | |
|---|
| 213 | if (!$enctype) |
|---|
| 214 | $enctype = $OUTPUT->type; |
|---|
| 215 | |
|---|
| 216 | // encode for HTML output |
|---|
| 217 | if ($enctype=='html') |
|---|
| 218 | { |
|---|
| 219 | if (!$html_encode_arr) |
|---|
| 220 | { |
|---|
| 221 | $html_encode_arr = get_html_translation_table(HTML_SPECIALCHARS); |
|---|
| 222 | unset($html_encode_arr['?']); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | $ltpos = strpos($str, '<'); |
|---|
| 226 | $encode_arr = $html_encode_arr; |
|---|
| 227 | |
|---|
| 228 | // don't replace quotes and html tags |
|---|
| 229 | if (($mode=='show' || $mode=='') && $ltpos!==false && strpos($str, '>', $ltpos)!==false) |
|---|
| 230 | { |
|---|
| 231 | unset($encode_arr['"']); |
|---|
| 232 | unset($encode_arr['<']); |
|---|
| 233 | unset($encode_arr['>']); |
|---|
| 234 | unset($encode_arr['&']); |
|---|
| 235 | } |
|---|
| 236 | else if ($mode=='remove') |
|---|
| 237 | $str = strip_tags($str); |
|---|
| 238 | |
|---|
| 239 | $out = strtr($str, $encode_arr); |
|---|
| 240 | |
|---|
| 241 | // avoid douple quotation of & |
|---|
| 242 | $out = preg_replace('/&([A-Za-z]{2,6}|#[0-9]{2,4});/', '&\\1;', $out); |
|---|
| 243 | |
|---|
| 244 | return $newlines ? nl2br($out) : $out; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | // if the replace tables for XML and JS are not yet defined |
|---|
| 248 | if ($js_rep_table===false) |
|---|
| 249 | { |
|---|
| 250 | $js_rep_table = $xml_rep_table = array(); |
|---|
| 251 | $xml_rep_table['&'] = '&'; |
|---|
| 252 | |
|---|
| 253 | for ($c=160; $c<256; $c++) // can be increased to support more charsets |
|---|
| 254 | $xml_rep_table[chr($c)] = "&#$c;"; |
|---|
| 255 | |
|---|
| 256 | $xml_rep_table['"'] = '"'; |
|---|
| 257 | $js_rep_table['"'] = '\\"'; |
|---|
| 258 | $js_rep_table["'"] = "\\'"; |
|---|
| 259 | $js_rep_table["\\"] = "\\\\"; |
|---|
| 260 | // Unicode line and paragraph separators (#1486310) |
|---|
| 261 | $js_rep_table[chr(hexdec(E2)).chr(hexdec(80)).chr(hexdec(A8))] = '
'; |
|---|
| 262 | $js_rep_table[chr(hexdec(E2)).chr(hexdec(80)).chr(hexdec(A9))] = '
'; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | // encode for javascript use |
|---|
| 266 | if ($enctype=='js') |
|---|
| 267 | return preg_replace(array("/\r?\n/", "/\r/", '/<\\//'), array('\n', '\n', '<\\/'), strtr($str, $js_rep_table)); |
|---|
| 268 | |
|---|
| 269 | // encode for plaintext |
|---|
| 270 | if ($enctype=='text') |
|---|
| 271 | return str_replace("\r\n", "\n", $mode=='remove' ? strip_tags($str) : $str); |
|---|
| 272 | |
|---|
| 273 | if ($enctype=='url') |
|---|
| 274 | return rawurlencode($str); |
|---|
| 275 | |
|---|
| 276 | // encode for XML |
|---|
| 277 | if ($enctype=='xml') |
|---|
| 278 | return strtr($str, $xml_rep_table); |
|---|
| 279 | |
|---|
| 280 | // no encoding given -> return original string |
|---|
| 281 | return $str; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | /** |
|---|
| 285 | * Quote a given string. |
|---|
| 286 | * Shortcut function for rep_specialchars_output |
|---|
| 287 | * |
|---|
| 288 | * @return string HTML-quoted string |
|---|
| 289 | * @see rep_specialchars_output() |
|---|
| 290 | */ |
|---|
| 291 | function Q($str, $mode='strict', $newlines=TRUE) |
|---|
| 292 | { |
|---|
| 293 | return rep_specialchars_output($str, 'html', $mode, $newlines); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | /** |
|---|
| 297 | * Quote a given string for javascript output. |
|---|
| 298 | * Shortcut function for rep_specialchars_output |
|---|
| 299 | * |
|---|
| 300 | * @return string JS-quoted string |
|---|
| 301 | * @see rep_specialchars_output() |
|---|
| 302 | */ |
|---|
| 303 | function JQ($str) |
|---|
| 304 | { |
|---|
| 305 | return rep_specialchars_output($str, 'js'); |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | /** |
|---|
| 310 | * Read input value and convert it for internal use |
|---|
| 311 | * Performs stripslashes() and charset conversion if necessary |
|---|
| 312 | * |
|---|
| 313 | * @param string Field name to read |
|---|
| 314 | * @param int Source to get value from (GPC) |
|---|
| 315 | * @param boolean Allow HTML tags in field value |
|---|
| 316 | * @param string Charset to convert into |
|---|
| 317 | * @return string Field value or NULL if not available |
|---|
| 318 | */ |
|---|
| 319 | function get_input_value($fname, $source, $allow_html=FALSE, $charset=NULL) |
|---|
| 320 | { |
|---|
| 321 | $value = NULL; |
|---|
| 322 | |
|---|
| 323 | if ($source == RCUBE_INPUT_GET) { |
|---|
| 324 | if (isset($_GET[$fname])) |
|---|
| 325 | $value = $_GET[$fname]; |
|---|
| 326 | } |
|---|
| 327 | else if ($source == RCUBE_INPUT_POST) { |
|---|
| 328 | if (isset($_POST[$fname])) |
|---|
| 329 | $value = $_POST[$fname]; |
|---|
| 330 | } |
|---|
| 331 | else if ($source == RCUBE_INPUT_GPC) { |
|---|
| 332 | if (isset($_POST[$fname])) |
|---|
| 333 | $value = $_POST[$fname]; |
|---|
| 334 | else if (isset($_GET[$fname])) |
|---|
| 335 | $value = $_GET[$fname]; |
|---|
| 336 | else if (isset($_COOKIE[$fname])) |
|---|
| 337 | $value = $_COOKIE[$fname]; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | return parse_input_value($value, $allow_html, $charset); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | /** |
|---|
| 344 | * Parse/validate input value. See get_input_value() |
|---|
| 345 | * Performs stripslashes() and charset conversion if necessary |
|---|
| 346 | * |
|---|
| 347 | * @param string Input value |
|---|
| 348 | * @param boolean Allow HTML tags in field value |
|---|
| 349 | * @param string Charset to convert into |
|---|
| 350 | * @return string Parsed value |
|---|
| 351 | */ |
|---|
| 352 | function parse_input_value($value, $allow_html=FALSE, $charset=NULL) |
|---|
| 353 | { |
|---|
| 354 | global $OUTPUT; |
|---|
| 355 | |
|---|
| 356 | if (empty($value)) |
|---|
| 357 | return $value; |
|---|
| 358 | |
|---|
| 359 | if (is_array($value)) { |
|---|
| 360 | foreach ($value as $idx => $val) |
|---|
| 361 | $value[$idx] = parse_input_value($val, $allow_html, $charset); |
|---|
| 362 | return $value; |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | // strip single quotes if magic_quotes_sybase is enabled |
|---|
| 366 | if (ini_get('magic_quotes_sybase')) |
|---|
| 367 | $value = str_replace("''", "'", $value); |
|---|
| 368 | // strip slashes if magic_quotes enabled |
|---|
| 369 | else if (get_magic_quotes_gpc() || get_magic_quotes_runtime()) |
|---|
| 370 | $value = stripslashes($value); |
|---|
| 371 | |
|---|
| 372 | // remove HTML tags if not allowed |
|---|
| 373 | if (!$allow_html) |
|---|
| 374 | $value = strip_tags($value); |
|---|
| 375 | |
|---|
| 376 | $output_charset = is_object($OUTPUT) ? $OUTPUT->get_charset() : null; |
|---|
| 377 | |
|---|
| 378 | // remove invalid characters (#1488124) |
|---|
| 379 | if ($output_charset == 'UTF-8') |
|---|
| 380 | $value = rc_utf8_clean($value); |
|---|
| 381 | |
|---|
| 382 | // convert to internal charset |
|---|
| 383 | if ($charset && $output_charset) |
|---|
| 384 | $value = rcube_charset_convert($value, $output_charset, $charset); |
|---|
| 385 | |
|---|
| 386 | return $value; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | /** |
|---|
| 390 | * Convert array of request parameters (prefixed with _) |
|---|
| 391 | * to a regular array with non-prefixed keys. |
|---|
| 392 | * |
|---|
| 393 | * @param int Source to get value from (GPC) |
|---|
| 394 | * @return array Hash array with all request parameters |
|---|
| 395 | */ |
|---|
| 396 | function request2param($mode = RCUBE_INPUT_GPC, $ignore = 'task|action') |
|---|
| 397 | { |
|---|
| 398 | $out = array(); |
|---|
| 399 | $src = $mode == RCUBE_INPUT_GET ? $_GET : ($mode == RCUBE_INPUT_POST ? $_POST : $_REQUEST); |
|---|
| 400 | foreach ($src as $key => $value) { |
|---|
| 401 | $fname = $key[0] == '_' ? substr($key, 1) : $key; |
|---|
| 402 | if ($ignore && !preg_match('/^(' . $ignore . ')$/', $fname)) |
|---|
| 403 | $out[$fname] = get_input_value($key, $mode); |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | return $out; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | /** |
|---|
| 410 | * Remove all non-ascii and non-word chars |
|---|
| 411 | * except ., -, _ |
|---|
| 412 | */ |
|---|
| 413 | function asciiwords($str, $css_id = false, $replace_with = '') |
|---|
| 414 | { |
|---|
| 415 | $allowed = 'a-z0-9\_\-' . (!$css_id ? '\.' : ''); |
|---|
| 416 | return preg_replace("/[^$allowed]/i", $replace_with, $str); |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | /** |
|---|
| 420 | * Convert the given string into a valid HTML identifier |
|---|
| 421 | * Same functionality as done in app.js with rcube_webmail.html_identifier() |
|---|
| 422 | */ |
|---|
| 423 | function html_identifier($str, $encode=false) |
|---|
| 424 | { |
|---|
| 425 | if ($encode) |
|---|
| 426 | return rtrim(strtr(base64_encode($str), '+/', '-_'), '='); |
|---|
| 427 | else |
|---|
| 428 | return asciiwords($str, true, '_'); |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | /** |
|---|
| 432 | * Remove single and double quotes from given string |
|---|
| 433 | * |
|---|
| 434 | * @param string Input value |
|---|
| 435 | * @return string Dequoted string |
|---|
| 436 | */ |
|---|
| 437 | function strip_quotes($str) |
|---|
| 438 | { |
|---|
| 439 | return str_replace(array("'", '"'), '', $str); |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | /** |
|---|
| 444 | * Remove new lines characters from given string |
|---|
| 445 | * |
|---|
| 446 | * @param string Input value |
|---|
| 447 | * @return string Stripped string |
|---|
| 448 | */ |
|---|
| 449 | function strip_newlines($str) |
|---|
| 450 | { |
|---|
| 451 | return preg_replace('/[\r\n]/', '', $str); |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | /** |
|---|
| 456 | * Create a HTML table based on the given data |
|---|
| 457 | * |
|---|
| 458 | * @param array Named table attributes |
|---|
| 459 | * @param mixed Table row data. Either a two-dimensional array or a valid SQL result set |
|---|
| 460 | * @param array List of cols to show |
|---|
| 461 | * @param string Name of the identifier col |
|---|
| 462 | * @return string HTML table code |
|---|
| 463 | */ |
|---|
| 464 | function rcube_table_output($attrib, $table_data, $a_show_cols, $id_col) |
|---|
| 465 | { |
|---|
| 466 | global $RCMAIL; |
|---|
| 467 | |
|---|
| 468 | $table = new html_table(/*array('cols' => count($a_show_cols))*/); |
|---|
| 469 | |
|---|
| 470 | // add table header |
|---|
| 471 | if (!$attrib['noheader']) |
|---|
| 472 | foreach ($a_show_cols as $col) |
|---|
| 473 | $table->add_header($col, Q(rcube_label($col))); |
|---|
| 474 | |
|---|
| 475 | $c = 0; |
|---|
| 476 | if (!is_array($table_data)) |
|---|
| 477 | { |
|---|
| 478 | $db = $RCMAIL->get_dbh(); |
|---|
| 479 | while ($table_data && ($sql_arr = $db->fetch_assoc($table_data))) |
|---|
| 480 | { |
|---|
| 481 | $table->add_row(array('id' => 'rcmrow' . html_identifier($sql_arr[$id_col]))); |
|---|
| 482 | |
|---|
| 483 | // format each col |
|---|
| 484 | foreach ($a_show_cols as $col) |
|---|
| 485 | $table->add($col, Q($sql_arr[$col])); |
|---|
| 486 | |
|---|
| 487 | $c++; |
|---|
| 488 | } |
|---|
| 489 | } |
|---|
| 490 | else { |
|---|
| 491 | foreach ($table_data as $row_data) |
|---|
| 492 | { |
|---|
| 493 | $class = !empty($row_data['class']) ? $row_data['class'] : ''; |
|---|
| 494 | |
|---|
| 495 | $table->add_row(array('id' => 'rcmrow' . html_identifier($row_data[$id_col]), 'class' => $class)); |
|---|
| 496 | |
|---|
| 497 | // format each col |
|---|
| 498 | foreach ($a_show_cols as $col) |
|---|
| 499 | $table->add($col, Q(is_array($row_data[$col]) ? $row_data[$col][0] : $row_data[$col])); |
|---|
| 500 | |
|---|
| 501 | $c++; |
|---|
| 502 | } |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | return $table->show($attrib); |
|---|
| 506 | } |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | /** |
|---|
| 510 | * Create an edit field for inclusion on a form |
|---|
| 511 | * |
|---|
| 512 | * @param string col field name |
|---|
| 513 | * @param string value field value |
|---|
| 514 | * @param array attrib HTML element attributes for field |
|---|
| 515 | * @param string type HTML element type (default 'text') |
|---|
| 516 | * @return string HTML field definition |
|---|
| 517 | */ |
|---|
| 518 | function rcmail_get_edit_field($col, $value, $attrib, $type='text') |
|---|
| 519 | { |
|---|
| 520 | static $colcounts = array(); |
|---|
| 521 | |
|---|
| 522 | $fname = '_'.$col; |
|---|
| 523 | $attrib['name'] = $fname . ($attrib['array'] ? '[]' : ''); |
|---|
| 524 | $attrib['class'] = trim($attrib['class'] . ' ff_' . $col); |
|---|
| 525 | |
|---|
| 526 | if ($type == 'checkbox') { |
|---|
| 527 | $attrib['value'] = '1'; |
|---|
| 528 | $input = new html_checkbox($attrib); |
|---|
| 529 | } |
|---|
| 530 | else if ($type == 'textarea') { |
|---|
| 531 | $attrib['cols'] = $attrib['size']; |
|---|
| 532 | $input = new html_textarea($attrib); |
|---|
| 533 | } |
|---|
| 534 | else if ($type == 'select') { |
|---|
| 535 | $input = new html_select($attrib); |
|---|
| 536 | $input->add('---', ''); |
|---|
| 537 | $input->add(array_values($attrib['options']), array_keys($attrib['options'])); |
|---|
| 538 | } |
|---|
| 539 | else if ($attrib['type'] == 'password') { |
|---|
| 540 | $input = new html_passwordfield($attrib); |
|---|
| 541 | } |
|---|
| 542 | else { |
|---|
| 543 | if ($attrib['type'] != 'text' && $attrib['type'] != 'hidden') |
|---|
| 544 | $attrib['type'] = 'text'; |
|---|
| 545 | $input = new html_inputfield($attrib); |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | // use value from post |
|---|
| 549 | if (isset($_POST[$fname])) { |
|---|
| 550 | $postvalue = get_input_value($fname, RCUBE_INPUT_POST, true); |
|---|
| 551 | $value = $attrib['array'] ? $postvalue[intval($colcounts[$col]++)] : $postvalue; |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | $out = $input->show($value); |
|---|
| 555 | |
|---|
| 556 | return $out; |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | /** |
|---|
| 561 | * Replace all css definitions with #container [def] |
|---|
| 562 | * and remove css-inlined scripting |
|---|
| 563 | * |
|---|
| 564 | * @param string CSS source code |
|---|
| 565 | * @param string Container ID to use as prefix |
|---|
| 566 | * @return string Modified CSS source |
|---|
| 567 | */ |
|---|
| 568 | function rcmail_mod_css_styles($source, $container_id, $allow_remote=false) |
|---|
| 569 | { |
|---|
| 570 | $last_pos = 0; |
|---|
| 571 | $replacements = new rcube_string_replacer; |
|---|
| 572 | |
|---|
| 573 | // ignore the whole block if evil styles are detected |
|---|
| 574 | $source = rcmail_xss_entity_decode($source); |
|---|
| 575 | $stripped = preg_replace('/[^a-z\(:;]/i', '', $source); |
|---|
| 576 | $evilexpr = 'expression|behavior|javascript:|import[^a]' . (!$allow_remote ? '|url\(' : ''); |
|---|
| 577 | if (preg_match("/$evilexpr/i", $stripped)) |
|---|
| 578 | return '/* evil! */'; |
|---|
| 579 | |
|---|
| 580 | // cut out all contents between { and } |
|---|
| 581 | while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos))) { |
|---|
| 582 | $styles = substr($source, $pos+1, $pos2-($pos+1)); |
|---|
| 583 | |
|---|
| 584 | // check every line of a style block... |
|---|
| 585 | if ($allow_remote) { |
|---|
| 586 | $a_styles = preg_split('/;[\r\n]*/', $styles, -1, PREG_SPLIT_NO_EMPTY); |
|---|
| 587 | foreach ($a_styles as $line) { |
|---|
| 588 | $stripped = preg_replace('/[^a-z\(:;]/i', '', $line); |
|---|
| 589 | // ... and only allow strict url() values |
|---|
| 590 | if (stripos($stripped, 'url(') && !preg_match('!url\s*\([ "\'](https?:)//[a-z0-9/._+-]+["\' ]\)!Uims', $line)) { |
|---|
| 591 | $a_styles = array('/* evil! */'); |
|---|
| 592 | break; |
|---|
| 593 | } |
|---|
| 594 | } |
|---|
| 595 | $styles = join(";\n", $a_styles); |
|---|
| 596 | } |
|---|
| 597 | |
|---|
| 598 | $key = $replacements->add($styles); |
|---|
| 599 | $source = substr($source, 0, $pos+1) . $replacements->get_replacement($key) . substr($source, $pos2, strlen($source)-$pos2); |
|---|
| 600 | $last_pos = $pos+2; |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | // remove html comments and add #container to each tag selector. |
|---|
| 604 | // also replace body definition because we also stripped off the <body> tag |
|---|
| 605 | $styles = preg_replace( |
|---|
| 606 | array( |
|---|
| 607 | '/(^\s*<!--)|(-->\s*$)/', |
|---|
| 608 | '/(^\s*|,\s*|\}\s*)([a-z0-9\._#\*][a-z0-9\.\-_]*)/im', |
|---|
| 609 | '/'.preg_quote($container_id, '/').'\s+body/i', |
|---|
| 610 | ), |
|---|
| 611 | array( |
|---|
| 612 | '', |
|---|
| 613 | "\\1#$container_id \\2", |
|---|
| 614 | $container_id, |
|---|
| 615 | ), |
|---|
| 616 | $source); |
|---|
| 617 | |
|---|
| 618 | // put block contents back in |
|---|
| 619 | $styles = $replacements->resolve($styles); |
|---|
| 620 | |
|---|
| 621 | return $styles; |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | |
|---|
| 625 | /** |
|---|
| 626 | * Decode escaped entities used by known XSS exploits. |
|---|
| 627 | * See http://downloads.securityfocus.com/vulnerabilities/exploits/26800.eml for examples |
|---|
| 628 | * |
|---|
| 629 | * @param string CSS content to decode |
|---|
| 630 | * @return string Decoded string |
|---|
| 631 | */ |
|---|
| 632 | function rcmail_xss_entity_decode($content) |
|---|
| 633 | { |
|---|
| 634 | $out = html_entity_decode(html_entity_decode($content)); |
|---|
| 635 | $out = preg_replace_callback('/\\\([0-9a-f]{4})/i', 'rcmail_xss_entity_decode_callback', $out); |
|---|
| 636 | $out = preg_replace('#/\*.*\*/#Ums', '', $out); |
|---|
| 637 | return $out; |
|---|
| 638 | } |
|---|
| 639 | |
|---|
| 640 | |
|---|
| 641 | /** |
|---|
| 642 | * preg_replace_callback callback for rcmail_xss_entity_decode_callback |
|---|
| 643 | * |
|---|
| 644 | * @param array matches result from preg_replace_callback |
|---|
| 645 | * @return string decoded entity |
|---|
| 646 | */ |
|---|
| 647 | function rcmail_xss_entity_decode_callback($matches) |
|---|
| 648 | { |
|---|
| 649 | return chr(hexdec($matches[1])); |
|---|
| 650 | } |
|---|
| 651 | |
|---|
| 652 | /** |
|---|
| 653 | * Compose a valid attribute string for HTML tags |
|---|
| 654 | * |
|---|
| 655 | * @param array Named tag attributes |
|---|
| 656 | * @param array List of allowed attributes |
|---|
| 657 | * @return string HTML formatted attribute string |
|---|
| 658 | */ |
|---|
| 659 | function create_attrib_string($attrib, $allowed_attribs=array('id', 'class', 'style')) |
|---|
| 660 | { |
|---|
| 661 | // allow the following attributes to be added to the <iframe> tag |
|---|
| 662 | $attrib_str = ''; |
|---|
| 663 | foreach ($allowed_attribs as $a) |
|---|
| 664 | if (isset($attrib[$a])) |
|---|
| 665 | $attrib_str .= sprintf(' %s="%s"', $a, str_replace('"', '"', $attrib[$a])); |
|---|
| 666 | |
|---|
| 667 | return $attrib_str; |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | /** |
|---|
| 672 | * Convert a HTML attribute string attributes to an associative array (name => value) |
|---|
| 673 | * |
|---|
| 674 | * @param string Input string |
|---|
| 675 | * @return array Key-value pairs of parsed attributes |
|---|
| 676 | */ |
|---|
| 677 | function parse_attrib_string($str) |
|---|
| 678 | { |
|---|
| 679 | $attrib = array(); |
|---|
| 680 | preg_match_all('/\s*([-_a-z]+)=(["\'])??(?(2)([^\2]*)\2|(\S+?))/Ui', stripslashes($str), $regs, PREG_SET_ORDER); |
|---|
| 681 | |
|---|
| 682 | // convert attributes to an associative array (name => value) |
|---|
| 683 | if ($regs) { |
|---|
| 684 | foreach ($regs as $attr) { |
|---|
| 685 | $attrib[strtolower($attr[1])] = html_entity_decode($attr[3] . $attr[4]); |
|---|
| 686 | } |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | return $attrib; |
|---|
| 690 | } |
|---|
| 691 | |
|---|
| 692 | |
|---|
| 693 | /** |
|---|
| 694 | * Improved equivalent to strtotime() |
|---|
| 695 | * |
|---|
| 696 | * @param string Date string |
|---|
| 697 | * @return int |
|---|
| 698 | */ |
|---|
| 699 | function rcube_strtotime($date) |
|---|
| 700 | { |
|---|
| 701 | // check for MS Outlook vCard date format YYYYMMDD |
|---|
| 702 | if (preg_match('/^([12][90]\d\d)([01]\d)(\d\d)$/', trim($date), $matches)) { |
|---|
| 703 | return mktime(0,0,0, intval($matches[2]), intval($matches[3]), intval($matches[1])); |
|---|
| 704 | } |
|---|
| 705 | else if (is_numeric($date)) |
|---|
| 706 | return $date; |
|---|
| 707 | |
|---|
| 708 | // support non-standard "GMTXXXX" literal |
|---|
| 709 | $date = preg_replace('/GMT\s*([+-][0-9]+)/', '\\1', $date); |
|---|
| 710 | |
|---|
| 711 | // if date parsing fails, we have a date in non-rfc format. |
|---|
| 712 | // remove token from the end and try again |
|---|
| 713 | while ((($ts = @strtotime($date)) === false) || ($ts < 0)) { |
|---|
| 714 | $d = explode(' ', $date); |
|---|
| 715 | array_pop($d); |
|---|
| 716 | if (!$d) break; |
|---|
| 717 | $date = implode(' ', $d); |
|---|
| 718 | } |
|---|
| 719 | |
|---|
| 720 | return $ts; |
|---|
| 721 | } |
|---|
| 722 | |
|---|
| 723 | |
|---|
| 724 | /** |
|---|
| 725 | * Convert the given date to a human readable form |
|---|
| 726 | * This uses the date formatting properties from config |
|---|
| 727 | * |
|---|
| 728 | * @param mixed Date representation (string, timestamp or DateTime object) |
|---|
| 729 | * @param string Date format to use |
|---|
| 730 | * @param bool Enables date convertion according to user timezone |
|---|
| 731 | * |
|---|
| 732 | * @return string Formatted date string |
|---|
| 733 | */ |
|---|
| 734 | function format_date($date, $format=NULL, $convert=true) |
|---|
| 735 | { |
|---|
| 736 | global $RCMAIL, $CONFIG; |
|---|
| 737 | |
|---|
| 738 | if (is_object($date) && is_a($date, 'DateTime')) { |
|---|
| 739 | $timestamp = $date->format('U'); |
|---|
| 740 | } |
|---|
| 741 | else { |
|---|
| 742 | if (!empty($date)) |
|---|
| 743 | $timestamp = rcube_strtotime($date); |
|---|
| 744 | |
|---|
| 745 | if (empty($timestamp)) |
|---|
| 746 | return ''; |
|---|
| 747 | |
|---|
| 748 | try { |
|---|
| 749 | $date = new DateTime("@".$timestamp); |
|---|
| 750 | } |
|---|
| 751 | catch (Exception $e) { |
|---|
| 752 | return ''; |
|---|
| 753 | } |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | if ($convert) { |
|---|
| 757 | try { |
|---|
| 758 | // convert to the right timezone |
|---|
| 759 | $stz = date_default_timezone_get(); |
|---|
| 760 | $tz = new DateTimeZone($RCMAIL->config->get('timezone')); |
|---|
| 761 | $date->setTimezone($tz); |
|---|
| 762 | date_default_timezone_set($tz->getName()); |
|---|
| 763 | |
|---|
| 764 | $timestamp = $date->format('U'); |
|---|
| 765 | } |
|---|
| 766 | catch (Exception $e) { |
|---|
| 767 | } |
|---|
| 768 | } |
|---|
| 769 | |
|---|
| 770 | // define date format depending on current time |
|---|
| 771 | if (!$format) { |
|---|
| 772 | $now = time(); |
|---|
| 773 | $now_date = getdate($now); |
|---|
| 774 | $today_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday'], $now_date['year']); |
|---|
| 775 | $week_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday']-6, $now_date['year']); |
|---|
| 776 | |
|---|
| 777 | if ($CONFIG['prettydate'] && $timestamp > $today_limit && $timestamp < $now) { |
|---|
| 778 | $format = $RCMAIL->config->get('date_today', $RCMAIL->config->get('time_format', 'H:i')); |
|---|
| 779 | $today = true; |
|---|
| 780 | } |
|---|
| 781 | else if ($CONFIG['prettydate'] && $timestamp > $week_limit && $timestamp < $now) |
|---|
| 782 | $format = $RCMAIL->config->get('date_short', 'D H:i'); |
|---|
| 783 | else |
|---|
| 784 | $format = $RCMAIL->config->get('date_long', 'Y-m-d H:i'); |
|---|
| 785 | } |
|---|
| 786 | |
|---|
| 787 | // strftime() format |
|---|
| 788 | if (preg_match('/%[a-z]+/i', $format)) { |
|---|
| 789 | $format = strftime($format, $timestamp); |
|---|
| 790 | |
|---|
| 791 | if ($convert && $stz) { |
|---|
| 792 | date_default_timezone_set($stz); |
|---|
| 793 | } |
|---|
| 794 | |
|---|
| 795 | return $today ? (rcube_label('today') . ' ' . $format) : $format; |
|---|
| 796 | } |
|---|
| 797 | |
|---|
| 798 | // parse format string manually in order to provide localized weekday and month names |
|---|
| 799 | // an alternative would be to convert the date() format string to fit with strftime() |
|---|
| 800 | $out = ''; |
|---|
| 801 | for ($i=0; $i<strlen($format); $i++) { |
|---|
| 802 | if ($format[$i]=='\\') // skip escape chars |
|---|
| 803 | continue; |
|---|
| 804 | |
|---|
| 805 | // write char "as-is" |
|---|
| 806 | if ($format[$i]==' ' || $format{$i-1}=='\\') |
|---|
| 807 | $out .= $format[$i]; |
|---|
| 808 | // weekday (short) |
|---|
| 809 | else if ($format[$i]=='D') |
|---|
| 810 | $out .= rcube_label(strtolower(date('D', $timestamp))); |
|---|
| 811 | // weekday long |
|---|
| 812 | else if ($format[$i]=='l') |
|---|
| 813 | $out .= rcube_label(strtolower(date('l', $timestamp))); |
|---|
| 814 | // month name (short) |
|---|
| 815 | else if ($format[$i]=='M') |
|---|
| 816 | $out .= rcube_label(strtolower(date('M', $timestamp))); |
|---|
| 817 | // month name (long) |
|---|
| 818 | else if ($format[$i]=='F') |
|---|
| 819 | $out .= rcube_label('long'.strtolower(date('M', $timestamp))); |
|---|
| 820 | else if ($format[$i]=='x') |
|---|
| 821 | $out .= strftime('%x %X', $timestamp); |
|---|
| 822 | else |
|---|
| 823 | $out .= date($format[$i], $timestamp); |
|---|
| 824 | } |
|---|
| 825 | |
|---|
| 826 | if ($today) { |
|---|
| 827 | $label = rcube_label('today'); |
|---|
| 828 | // replcae $ character with "Today" label (#1486120) |
|---|
| 829 | if (strpos($out, '$') !== false) { |
|---|
| 830 | $out = preg_replace('/\$/', $label, $out, 1); |
|---|
| 831 | } |
|---|
| 832 | else { |
|---|
| 833 | $out = $label . ' ' . $out; |
|---|
| 834 | } |
|---|
| 835 | } |
|---|
| 836 | |
|---|
| 837 | if ($convert && $stz) { |
|---|
| 838 | date_default_timezone_set($stz); |
|---|
| 839 | } |
|---|
| 840 | |
|---|
| 841 | return $out; |
|---|
| 842 | } |
|---|
| 843 | |
|---|
| 844 | |
|---|
| 845 | /** |
|---|
| 846 | * Compose a valid representation of name and e-mail address |
|---|
| 847 | * |
|---|
| 848 | * @param string E-mail address |
|---|
| 849 | * @param string Person name |
|---|
| 850 | * @return string Formatted string |
|---|
| 851 | */ |
|---|
| 852 | function format_email_recipient($email, $name='') |
|---|
| 853 | { |
|---|
| 854 | if ($name && $name != $email) { |
|---|
| 855 | // Special chars as defined by RFC 822 need to in quoted string (or escaped). |
|---|
| 856 | return sprintf('%s <%s>', preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $name) ? '"'.addcslashes($name, '"').'"' : $name, trim($email)); |
|---|
| 857 | } |
|---|
| 858 | |
|---|
| 859 | return trim($email); |
|---|
| 860 | } |
|---|
| 861 | |
|---|
| 862 | |
|---|
| 863 | /** |
|---|
| 864 | * Return the mailboxlist in HTML |
|---|
| 865 | * |
|---|
| 866 | * @param array Named parameters |
|---|
| 867 | * @return string HTML code for the gui object |
|---|
| 868 | */ |
|---|
| 869 | function rcmail_mailbox_list($attrib) |
|---|
| 870 | { |
|---|
| 871 | global $RCMAIL; |
|---|
| 872 | static $a_mailboxes; |
|---|
| 873 | |
|---|
| 874 | $attrib += array('maxlength' => 100, 'realnames' => false, 'unreadwrap' => ' (%s)'); |
|---|
| 875 | |
|---|
| 876 | // add some labels to client |
|---|
| 877 | $RCMAIL->output->add_label('purgefolderconfirm', 'deletemessagesconfirm'); |
|---|
| 878 | |
|---|
| 879 | $type = $attrib['type'] ? $attrib['type'] : 'ul'; |
|---|
| 880 | unset($attrib['type']); |
|---|
| 881 | |
|---|
| 882 | if ($type=='ul' && !$attrib['id']) |
|---|
| 883 | $attrib['id'] = 'rcmboxlist'; |
|---|
| 884 | |
|---|
| 885 | if (empty($attrib['folder_name'])) |
|---|
| 886 | $attrib['folder_name'] = '*'; |
|---|
| 887 | |
|---|
| 888 | // get mailbox list |
|---|
| 889 | $mbox_name = $RCMAIL->storage->get_folder(); |
|---|
| 890 | |
|---|
| 891 | // build the folders tree |
|---|
| 892 | if (empty($a_mailboxes)) { |
|---|
| 893 | // get mailbox list |
|---|
| 894 | $a_folders = $RCMAIL->storage->list_folders_subscribed('', $attrib['folder_name'], $attrib['folder_filter']); |
|---|
| 895 | $delimiter = $RCMAIL->storage->get_hierarchy_delimiter(); |
|---|
| 896 | $a_mailboxes = array(); |
|---|
| 897 | |
|---|
| 898 | foreach ($a_folders as $folder) |
|---|
| 899 | rcmail_build_folder_tree($a_mailboxes, $folder, $delimiter); |
|---|
| 900 | } |
|---|
| 901 | |
|---|
| 902 | // allow plugins to alter the folder tree or to localize folder names |
|---|
| 903 | $hook = $RCMAIL->plugins->exec_hook('render_mailboxlist', array( |
|---|
| 904 | 'list' => $a_mailboxes, |
|---|
| 905 | 'delimiter' => $delimiter, |
|---|
| 906 | 'type' => $type, |
|---|
| 907 | 'attribs' => $attrib, |
|---|
| 908 | )); |
|---|
| 909 | |
|---|
| 910 | $a_mailboxes = $hook['list']; |
|---|
| 911 | $attrib = $hook['attribs']; |
|---|
| 912 | |
|---|
| 913 | if ($type == 'select') { |
|---|
| 914 | $select = new html_select($attrib); |
|---|
| 915 | |
|---|
| 916 | // add no-selection option |
|---|
| 917 | if ($attrib['noselection']) |
|---|
| 918 | $select->add(rcube_label($attrib['noselection']), ''); |
|---|
| 919 | |
|---|
| 920 | rcmail_render_folder_tree_select($a_mailboxes, $mbox_name, $attrib['maxlength'], $select, $attrib['realnames']); |
|---|
| 921 | $out = $select->show($attrib['default']); |
|---|
| 922 | } |
|---|
| 923 | else { |
|---|
| 924 | $js_mailboxlist = array(); |
|---|
| 925 | $out = html::tag('ul', $attrib, rcmail_render_folder_tree_html($a_mailboxes, $mbox_name, $js_mailboxlist, $attrib), html::$common_attrib); |
|---|
| 926 | |
|---|
| 927 | $RCMAIL->output->add_gui_object('mailboxlist', $attrib['id']); |
|---|
| 928 | $RCMAIL->output->set_env('mailboxes', $js_mailboxlist); |
|---|
| 929 | $RCMAIL->output->set_env('unreadwrap', $attrib['unreadwrap']); |
|---|
| 930 | $RCMAIL->output->set_env('collapsed_folders', (string)$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 html_select HTML drop-down object |
|---|
| 942 | */ |
|---|
| 943 | function rcmail_mailbox_select($p = array()) |
|---|
| 944 | { |
|---|
| 945 | global $RCMAIL; |
|---|
| 946 | |
|---|
| 947 | $p += array('maxlength' => 100, 'realnames' => false); |
|---|
| 948 | $a_mailboxes = array(); |
|---|
| 949 | $storage = $RCMAIL->get_storage(); |
|---|
| 950 | |
|---|
| 951 | if (empty($p['folder_name'])) { |
|---|
| 952 | $p['folder_name'] = '*'; |
|---|
| 953 | } |
|---|
| 954 | |
|---|
| 955 | if ($p['unsubscribed']) |
|---|
| 956 | $list = $storage->list_folders('', $p['folder_name'], $p['folder_filter'], $p['folder_rights']); |
|---|
| 957 | else |
|---|
| 958 | $list = $storage->list_folders_subscribed('', $p['folder_name'], $p['folder_filter'], $p['folder_rights']); |
|---|
| 959 | |
|---|
| 960 | $delimiter = $storage->get_hierarchy_delimiter(); |
|---|
| 961 | |
|---|
| 962 | foreach ($list as $folder) { |
|---|
| 963 | if (empty($p['exceptions']) || !in_array($folder, $p['exceptions'])) |
|---|
| 964 | rcmail_build_folder_tree($a_mailboxes, $folder, $delimiter); |
|---|
| 965 | } |
|---|
| 966 | |
|---|
| 967 | $select = new html_select($p); |
|---|
| 968 | |
|---|
| 969 | if ($p['noselection']) |
|---|
| 970 | $select->add($p['noselection'], ''); |
|---|
| 971 | |
|---|
| 972 | rcmail_render_folder_tree_select($a_mailboxes, $mbox, $p['maxlength'], $select, $p['realnames'], 0, $p); |
|---|
| 973 | |
|---|
| 974 | return $select; |
|---|
| 975 | } |
|---|
| 976 | |
|---|
| 977 | |
|---|
| 978 | /** |
|---|
| 979 | * Create a hierarchical array of the mailbox list |
|---|
| 980 | * @access private |
|---|
| 981 | * @return void |
|---|
| 982 | */ |
|---|
| 983 | function rcmail_build_folder_tree(&$arrFolders, $folder, $delm='/', $path='') |
|---|
| 984 | { |
|---|
| 985 | global $RCMAIL; |
|---|
| 986 | |
|---|
| 987 | // Handle namespace prefix |
|---|
| 988 | $prefix = ''; |
|---|
| 989 | if (!$path) { |
|---|
| 990 | $n_folder = $folder; |
|---|
| 991 | $folder = $RCMAIL->storage->mod_folder($folder); |
|---|
| 992 | |
|---|
| 993 | if ($n_folder != $folder) { |
|---|
| 994 | $prefix = substr($n_folder, 0, -strlen($folder)); |
|---|
| 995 | } |
|---|
| 996 | } |
|---|
| 997 | |
|---|
| 998 | $pos = strpos($folder, $delm); |
|---|
| 999 | |
|---|
| 1000 | if ($pos !== false) { |
|---|
| 1001 | $subFolders = substr($folder, $pos+1); |
|---|
| 1002 | $currentFolder = substr($folder, 0, $pos); |
|---|
| 1003 | |
|---|
| 1004 | // sometimes folder has a delimiter as the last character |
|---|
| 1005 | if (!strlen($subFolders)) |
|---|
| 1006 | $virtual = false; |
|---|
| 1007 | else if (!isset($arrFolders[$currentFolder])) |
|---|
| 1008 | $virtual = true; |
|---|
| 1009 | else |
|---|
| 1010 | $virtual = $arrFolders[$currentFolder]['virtual']; |
|---|
| 1011 | } |
|---|
| 1012 | else { |
|---|
| 1013 | $subFolders = false; |
|---|
| 1014 | $currentFolder = $folder; |
|---|
| 1015 | $virtual = false; |
|---|
| 1016 | } |
|---|
| 1017 | |
|---|
| 1018 | $path .= $prefix.$currentFolder; |
|---|
| 1019 | |
|---|
| 1020 | if (!isset($arrFolders[$currentFolder])) { |
|---|
| 1021 | $arrFolders[$currentFolder] = array( |
|---|
| 1022 | 'id' => $path, |
|---|
| 1023 | 'name' => rcube_charset_convert($currentFolder, 'UTF7-IMAP'), |
|---|
| 1024 | 'virtual' => $virtual, |
|---|
| 1025 | 'folders' => array()); |
|---|
| 1026 | } |
|---|
| 1027 | else |
|---|
| 1028 | $arrFolders[$currentFolder]['virtual'] = $virtual; |
|---|
| 1029 | |
|---|
| 1030 | if (strlen($subFolders)) |
|---|
| 1031 | rcmail_build_folder_tree($arrFolders[$currentFolder]['folders'], $subFolders, $delm, $path.$delm); |
|---|
| 1032 | } |
|---|
| 1033 | |
|---|
| 1034 | |
|---|
| 1035 | /** |
|---|
| 1036 | * Return html for a structured list <ul> for the mailbox tree |
|---|
| 1037 | * @access private |
|---|
| 1038 | * @return string |
|---|
| 1039 | */ |
|---|
| 1040 | function rcmail_render_folder_tree_html(&$arrFolders, &$mbox_name, &$jslist, $attrib, $nestLevel=0) |
|---|
| 1041 | { |
|---|
| 1042 | global $RCMAIL, $CONFIG; |
|---|
| 1043 | |
|---|
| 1044 | $maxlength = intval($attrib['maxlength']); |
|---|
| 1045 | $realnames = (bool)$attrib['realnames']; |
|---|
| 1046 | $msgcounts = $RCMAIL->storage->get_cache('messagecount'); |
|---|
| 1047 | |
|---|
| 1048 | $out = ''; |
|---|
| 1049 | foreach ($arrFolders as $key => $folder) { |
|---|
| 1050 | $title = null; |
|---|
| 1051 | $folder_class = rcmail_folder_classname($folder['id']); |
|---|
| 1052 | $collapsed = strpos($CONFIG['collapsed_folders'], '&'.rawurlencode($folder['id']).'&') !== false; |
|---|
| 1053 | $unread = $msgcounts ? intval($msgcounts[$folder['id']]['UNSEEN']) : 0; |
|---|
| 1054 | |
|---|
| 1055 | if ($folder_class && !$realnames) { |
|---|
| 1056 | $foldername = rcube_label($folder_class); |
|---|
| 1057 | } |
|---|
| 1058 | else { |
|---|
| 1059 | $foldername = $folder['name']; |
|---|
| 1060 | |
|---|
| 1061 | // shorten the folder name to a given length |
|---|
| 1062 | if ($maxlength && $maxlength > 1) { |
|---|
| 1063 | $fname = abbreviate_string($foldername, $maxlength); |
|---|
| 1064 | if ($fname != $foldername) |
|---|
| 1065 | $title = $foldername; |
|---|
| 1066 | $foldername = $fname; |
|---|
| 1067 | } |
|---|
| 1068 | } |
|---|
| 1069 | |
|---|
| 1070 | // make folder name safe for ids and class names |
|---|
| 1071 | $folder_id = html_identifier($folder['id'], true); |
|---|
| 1072 | $classes = array('mailbox'); |
|---|
| 1073 | |
|---|
| 1074 | // set special class for Sent, Drafts, Trash and Junk |
|---|
| 1075 | if ($folder_class) |
|---|
| 1076 | $classes[] = $folder_class; |
|---|
| 1077 | |
|---|
| 1078 | if ($folder['id'] == $mbox_name) |
|---|
| 1079 | $classes[] = 'selected'; |
|---|
| 1080 | |
|---|
| 1081 | if ($folder['virtual']) |
|---|
| 1082 | $classes[] = 'virtual'; |
|---|
| 1083 | else if ($unread) |
|---|
| 1084 | $classes[] = 'unread'; |
|---|
| 1085 | |
|---|
| 1086 | $js_name = JQ($folder['id']); |
|---|
| 1087 | $html_name = Q($foldername) . ($unread ? html::span('unreadcount', sprintf($attrib['unreadwrap'], $unread)) : ''); |
|---|
| 1088 | $link_attrib = $folder['virtual'] ? array() : array( |
|---|
| 1089 | 'href' => rcmail_url('', array('_mbox' => $folder['id'])), |
|---|
| 1090 | 'onclick' => sprintf("return %s.command('list','%s',this)", JS_OBJECT_NAME, $js_name), |
|---|
| 1091 | 'rel' => $folder['id'], |
|---|
| 1092 | 'title' => $title, |
|---|
| 1093 | ); |
|---|
| 1094 | |
|---|
| 1095 | $out .= html::tag('li', array( |
|---|
| 1096 | 'id' => "rcmli".$folder_id, |
|---|
| 1097 | 'class' => join(' ', $classes), |
|---|
| 1098 | 'noclose' => true), |
|---|
| 1099 | html::a($link_attrib, $html_name) . |
|---|
| 1100 | (!empty($folder['folders']) ? html::div(array( |
|---|
| 1101 | 'class' => ($collapsed ? 'collapsed' : 'expanded'), |
|---|
| 1102 | 'style' => "position:absolute", |
|---|
| 1103 | 'onclick' => sprintf("%s.command('collapse-folder', '%s')", JS_OBJECT_NAME, $js_name) |
|---|
| 1104 | ), ' ') : '')); |
|---|
| 1105 | |
|---|
| 1106 | $jslist[$folder_id] = array('id' => $folder['id'], 'name' => $foldername, 'virtual' => $folder['virtual']); |
|---|
| 1107 | |
|---|
| 1108 | if (!empty($folder['folders'])) { |
|---|
| 1109 | $out .= html::tag('ul', array('style' => ($collapsed ? "display:none;" : null)), |
|---|
| 1110 | rcmail_render_folder_tree_html($folder['folders'], $mbox_name, $jslist, $attrib, $nestLevel+1)); |
|---|
| 1111 | } |
|---|
| 1112 | |
|---|
| 1113 | $out .= "</li>\n"; |
|---|
| 1114 | } |
|---|
| 1115 | |
|---|
| 1116 | return $out; |
|---|
| 1117 | } |
|---|
| 1118 | |
|---|
| 1119 | |
|---|
| 1120 | /** |
|---|
| 1121 | * Return html for a flat list <select> for the mailbox tree |
|---|
| 1122 | * @access private |
|---|
| 1123 | * @return string |
|---|
| 1124 | */ |
|---|
| 1125 | function rcmail_render_folder_tree_select(&$arrFolders, &$mbox_name, $maxlength, &$select, $realnames=false, $nestLevel=0, $opts=array()) |
|---|
| 1126 | { |
|---|
| 1127 | global $RCMAIL; |
|---|
| 1128 | |
|---|
| 1129 | $out = ''; |
|---|
| 1130 | |
|---|
| 1131 | foreach ($arrFolders as $key => $folder) { |
|---|
| 1132 | // skip exceptions (and its subfolders) |
|---|
| 1133 | if (!empty($opts['exceptions']) && in_array($folder['id'], $opts['exceptions'])) { |
|---|
| 1134 | continue; |
|---|
| 1135 | } |
|---|
| 1136 | |
|---|
| 1137 | // skip folders in which it isn't possible to create subfolders |
|---|
| 1138 | if (!empty($opts['skip_noinferiors']) && ($attrs = $RCMAIL->storage->folder_attributes($folder['id'])) |
|---|
| 1139 | && in_array('\\Noinferiors', $attrs) |
|---|
| 1140 | ) { |
|---|
| 1141 | continue; |
|---|
| 1142 | } |
|---|
| 1143 | |
|---|
| 1144 | if (!$realnames && ($folder_class = rcmail_folder_classname($folder['id']))) |
|---|
| 1145 | $foldername = rcube_label($folder_class); |
|---|
| 1146 | else { |
|---|
| 1147 | $foldername = $folder['name']; |
|---|
| 1148 | |
|---|
| 1149 | // shorten the folder name to a given length |
|---|
| 1150 | if ($maxlength && $maxlength>1) |
|---|
| 1151 | $foldername = abbreviate_string($foldername, $maxlength); |
|---|
| 1152 | } |
|---|
| 1153 | |
|---|
| 1154 | $select->add(str_repeat(' ', $nestLevel*4) . $foldername, $folder['id']); |
|---|
| 1155 | |
|---|
| 1156 | if (!empty($folder['folders'])) |
|---|
| 1157 | $out .= rcmail_render_folder_tree_select($folder['folders'], $mbox_name, $maxlength, |
|---|
| 1158 | $select, $realnames, $nestLevel+1, $opts); |
|---|
| 1159 | } |
|---|
| 1160 | |
|---|
| 1161 | return $out; |
|---|
| 1162 | } |
|---|
| 1163 | |
|---|
| 1164 | |
|---|
| 1165 | /** |
|---|
| 1166 | * Return internal name for the given folder if it matches the configured special folders |
|---|
| 1167 | * @access private |
|---|
| 1168 | * @return string |
|---|
| 1169 | */ |
|---|
| 1170 | function rcmail_folder_classname($folder_id) |
|---|
| 1171 | { |
|---|
| 1172 | global $CONFIG; |
|---|
| 1173 | |
|---|
| 1174 | if ($folder_id == 'INBOX') |
|---|
| 1175 | return 'inbox'; |
|---|
| 1176 | |
|---|
| 1177 | // for these mailboxes we have localized labels and css classes |
|---|
| 1178 | foreach (array('sent', 'drafts', 'trash', 'junk') as $smbx) |
|---|
| 1179 | { |
|---|
| 1180 | if ($folder_id == $CONFIG[$smbx.'_mbox']) |
|---|
| 1181 | return $smbx; |
|---|
| 1182 | } |
|---|
| 1183 | } |
|---|
| 1184 | |
|---|
| 1185 | |
|---|
| 1186 | /** |
|---|
| 1187 | * Try to localize the given IMAP folder name. |
|---|
| 1188 | * UTF-7 decode it in case no localized text was found |
|---|
| 1189 | * |
|---|
| 1190 | * @param string Folder name |
|---|
| 1191 | * @return string Localized folder name in UTF-8 encoding |
|---|
| 1192 | */ |
|---|
| 1193 | function rcmail_localize_foldername($name) |
|---|
| 1194 | { |
|---|
| 1195 | if ($folder_class = rcmail_folder_classname($name)) |
|---|
| 1196 | return rcube_label($folder_class); |
|---|
| 1197 | else |
|---|
| 1198 | return rcube_charset_convert($name, 'UTF7-IMAP'); |
|---|
| 1199 | } |
|---|
| 1200 | |
|---|
| 1201 | |
|---|
| 1202 | function rcmail_localize_folderpath($path) |
|---|
| 1203 | { |
|---|
| 1204 | global $RCMAIL; |
|---|
| 1205 | |
|---|
| 1206 | $protect_folders = $RCMAIL->config->get('protect_default_folders'); |
|---|
| 1207 | $default_folders = (array) $RCMAIL->config->get('default_folders'); |
|---|
| 1208 | $delimiter = $RCMAIL->storage->get_hierarchy_delimiter(); |
|---|
| 1209 | $path = explode($delimiter, $path); |
|---|
| 1210 | $result = array(); |
|---|
| 1211 | |
|---|
| 1212 | foreach ($path as $idx => $dir) { |
|---|
| 1213 | $directory = implode($delimiter, array_slice($path, 0, $idx+1)); |
|---|
| 1214 | if ($protect_folders && in_array($directory, $default_folders)) { |
|---|
| 1215 | unset($result); |
|---|
| 1216 | $result[] = rcmail_localize_foldername($directory); |
|---|
| 1217 | } |
|---|
| 1218 | else { |
|---|
| 1219 | $result[] = rcube_charset_convert($dir, 'UTF7-IMAP'); |
|---|
| 1220 | } |
|---|
| 1221 | } |
|---|
| 1222 | |
|---|
| 1223 | return implode($delimiter, $result); |
|---|
| 1224 | } |
|---|
| 1225 | |
|---|
| 1226 | |
|---|
| 1227 | function rcmail_quota_display($attrib) |
|---|
| 1228 | { |
|---|
| 1229 | global $OUTPUT; |
|---|
| 1230 | |
|---|
| 1231 | if (!$attrib['id']) |
|---|
| 1232 | $attrib['id'] = 'rcmquotadisplay'; |
|---|
| 1233 | |
|---|
| 1234 | $_SESSION['quota_display'] = !empty($attrib['display']) ? $attrib['display'] : 'text'; |
|---|
| 1235 | |
|---|
| 1236 | $OUTPUT->add_gui_object('quotadisplay', $attrib['id']); |
|---|
| 1237 | |
|---|
| 1238 | $quota = rcmail_quota_content($attrib); |
|---|
| 1239 | |
|---|
| 1240 | $OUTPUT->add_script('rcmail.set_quota('.json_serialize($quota).');', 'docready'); |
|---|
| 1241 | |
|---|
| 1242 | return html::span($attrib, ''); |
|---|
| 1243 | } |
|---|
| 1244 | |
|---|
| 1245 | |
|---|
| 1246 | function rcmail_quota_content($attrib=NULL) |
|---|
| 1247 | { |
|---|
| 1248 | global $RCMAIL; |
|---|
| 1249 | |
|---|
| 1250 | $quota = $RCMAIL->storage->get_quota(); |
|---|
| 1251 | $quota = $RCMAIL->plugins->exec_hook('quota', $quota); |
|---|
| 1252 | |
|---|
| 1253 | $quota_result = (array) $quota; |
|---|
| 1254 | $quota_result['type'] = isset($_SESSION['quota_display']) ? $_SESSION['quota_display'] : ''; |
|---|
| 1255 | |
|---|
| 1256 | if (!$quota['total'] && $RCMAIL->config->get('quota_zero_as_unlimited')) { |
|---|
| 1257 | $quota_result['title'] = rcube_label('unlimited'); |
|---|
| 1258 | $quota_result['percent'] = 0; |
|---|
| 1259 | } |
|---|
| 1260 | else if ($quota['total']) { |
|---|
| 1261 | if (!isset($quota['percent'])) |
|---|
| 1262 | $quota_result['percent'] = min(100, round(($quota['used']/max(1,$quota['total']))*100)); |
|---|
| 1263 | |
|---|
| 1264 | $title = sprintf('%s / %s (%.0f%%)', |
|---|
| 1265 | show_bytes($quota['used'] * 1024), show_bytes($quota['total'] * 1024), |
|---|
| 1266 | $quota_result['percent']); |
|---|
| 1267 | |
|---|
| 1268 | $quota_result['title'] = $title; |
|---|
| 1269 | |
|---|
| 1270 | if ($attrib['width']) |
|---|
| 1271 | $quota_result['width'] = $attrib['width']; |
|---|
| 1272 | if ($attrib['height']) |
|---|
| 1273 | $quota_result['height'] = $attrib['height']; |
|---|
| 1274 | } |
|---|
| 1275 | else { |
|---|
| 1276 | $quota_result['title'] = rcube_label('unknown'); |
|---|
| 1277 | $quota_result['percent'] = 0; |
|---|
| 1278 | } |
|---|
| 1279 | |
|---|
| 1280 | return $quota_result; |
|---|
| 1281 | } |
|---|
| 1282 | |
|---|
| 1283 | |
|---|
| 1284 | /** |
|---|
| 1285 | * Outputs error message according to server error/response codes |
|---|
| 1286 | * |
|---|
| 1287 | * @param string Fallback message label |
|---|
| 1288 | * @param string Fallback message label arguments |
|---|
| 1289 | * |
|---|
| 1290 | * @return void |
|---|
| 1291 | */ |
|---|
| 1292 | function rcmail_display_server_error($fallback=null, $fallback_args=null) |
|---|
| 1293 | { |
|---|
| 1294 | global $RCMAIL; |
|---|
| 1295 | |
|---|
| 1296 | $err_code = $RCMAIL->storage->get_error_code(); |
|---|
| 1297 | $res_code = $RCMAIL->storage->get_response_code(); |
|---|
| 1298 | |
|---|
| 1299 | if ($err_code < 0) { |
|---|
| 1300 | $RCMAIL->output->show_message('storageerror', 'error'); |
|---|
| 1301 | } |
|---|
| 1302 | else if ($res_code == rcube_storage::NOPERM) { |
|---|
| 1303 | $RCMAIL->output->show_message('errornoperm', 'error'); |
|---|
| 1304 | } |
|---|
| 1305 | else if ($res_code == rcube_storage::READONLY) { |
|---|
| 1306 | $RCMAIL->output->show_message('errorreadonly', 'error'); |
|---|
| 1307 | } |
|---|
| 1308 | else if ($err_code && ($err_str = $RCMAIL->storage->get_error_str())) { |
|---|
| 1309 | // try to detect access rights problem and display appropriate message |
|---|
| 1310 | if (stripos($err_str, 'Permission denied') !== false) |
|---|
| 1311 | $RCMAIL->output->show_message('errornoperm', 'error'); |
|---|
| 1312 | else |
|---|
| 1313 | $RCMAIL->output->show_message('servererrormsg', 'error', array('msg' => $err_str)); |
|---|
| 1314 | } |
|---|
| 1315 | else if ($fallback) { |
|---|
| 1316 | $RCMAIL->output->show_message($fallback, 'error', $fallback_args); |
|---|
| 1317 | } |
|---|
| 1318 | |
|---|
| 1319 | return true; |
|---|
| 1320 | } |
|---|
| 1321 | |
|---|
| 1322 | |
|---|
| 1323 | /** |
|---|
| 1324 | * Generate CSS classes from mimetype and filename extension |
|---|
| 1325 | * |
|---|
| 1326 | * @param string Mimetype |
|---|
| 1327 | * @param string The filename |
|---|
| 1328 | * @return string CSS classes separated by space |
|---|
| 1329 | */ |
|---|
| 1330 | function rcmail_filetype2classname($mimetype, $filename) |
|---|
| 1331 | { |
|---|
| 1332 | list($primary, $secondary) = explode('/', $mimetype); |
|---|
| 1333 | |
|---|
| 1334 | $classes = array($primary ? $primary : 'unknown'); |
|---|
| 1335 | if ($secondary) { |
|---|
| 1336 | $classes[] = $secondary; |
|---|
| 1337 | } |
|---|
| 1338 | if (preg_match('/\.([a-z0-9]+)$/i', $filename, $m)) { |
|---|
| 1339 | $classes[] = $m[1]; |
|---|
| 1340 | } |
|---|
| 1341 | |
|---|
| 1342 | return strtolower(join(" ", $classes)); |
|---|
| 1343 | } |
|---|
| 1344 | |
|---|
| 1345 | /** |
|---|
| 1346 | * Output HTML editor scripts |
|---|
| 1347 | * |
|---|
| 1348 | * @param string Editor mode |
|---|
| 1349 | * @return void |
|---|
| 1350 | */ |
|---|
| 1351 | function rcube_html_editor($mode='') |
|---|
| 1352 | { |
|---|
| 1353 | global $RCMAIL; |
|---|
| 1354 | |
|---|
| 1355 | $hook = $RCMAIL->plugins->exec_hook('html_editor', array('mode' => $mode)); |
|---|
| 1356 | |
|---|
| 1357 | if ($hook['abort']) |
|---|
| 1358 | return; |
|---|
| 1359 | |
|---|
| 1360 | $lang = strtolower($_SESSION['language']); |
|---|
| 1361 | |
|---|
| 1362 | // TinyMCE uses two-letter lang codes, with exception of Chinese |
|---|
| 1363 | if (strpos($lang, 'zh_') === 0) |
|---|
| 1364 | $lang = str_replace('_', '-', $lang); |
|---|
| 1365 | else |
|---|
| 1366 | $lang = substr($lang, 0, 2); |
|---|
| 1367 | |
|---|
| 1368 | if (!file_exists(INSTALL_PATH . 'program/js/tiny_mce/langs/'.$lang.'.js')) |
|---|
| 1369 | $lang = 'en'; |
|---|
| 1370 | |
|---|
| 1371 | $RCMAIL->output->include_script('tiny_mce/tiny_mce.js'); |
|---|
| 1372 | $RCMAIL->output->include_script('editor.js'); |
|---|
| 1373 | $RCMAIL->output->add_script(sprintf("rcmail_editor_init(%s)", |
|---|
| 1374 | json_encode(array( |
|---|
| 1375 | 'mode' => $mode, |
|---|
| 1376 | 'lang' => $lang, |
|---|
| 1377 | 'skin_path' => $RCMAIL->output->get_skin_path(), |
|---|
| 1378 | 'spellcheck' => intval($RCMAIL->config->get('enable_spellcheck')), |
|---|
| 1379 | 'spelldict' => intval($RCMAIL->config->get('spellcheck_dictionary')), |
|---|
| 1380 | ))), 'foot'); |
|---|
| 1381 | } |
|---|
| 1382 | |
|---|
| 1383 | |
|---|
| 1384 | /** |
|---|
| 1385 | * Replaces TinyMCE's emoticon images with plain-text representation |
|---|
| 1386 | * |
|---|
| 1387 | * @param string HTML content |
|---|
| 1388 | * @return string HTML content |
|---|
| 1389 | */ |
|---|
| 1390 | function rcmail_replace_emoticons($html) |
|---|
| 1391 | { |
|---|
| 1392 | $emoticons = array( |
|---|
| 1393 | '8-)' => 'smiley-cool', |
|---|
| 1394 | ':-#' => 'smiley-foot-in-mouth', |
|---|
| 1395 | ':-*' => 'smiley-kiss', |
|---|
| 1396 | ':-X' => 'smiley-sealed', |
|---|
| 1397 | ':-P' => 'smiley-tongue-out', |
|---|
| 1398 | ':-@' => 'smiley-yell', |
|---|
| 1399 | ":'(" => 'smiley-cry', |
|---|
| 1400 | ':-(' => 'smiley-frown', |
|---|
| 1401 | ':-D' => 'smiley-laughing', |
|---|
| 1402 | ':-)' => 'smiley-smile', |
|---|
| 1403 | ':-S' => 'smiley-undecided', |
|---|
| 1404 | ':-$' => 'smiley-embarassed', |
|---|
| 1405 | 'O:-)' => 'smiley-innocent', |
|---|
| 1406 | ':-|' => 'smiley-money-mouth', |
|---|
| 1407 | ':-O' => 'smiley-surprised', |
|---|
| 1408 | ';-)' => 'smiley-wink', |
|---|
| 1409 | ); |
|---|
| 1410 | |
|---|
| 1411 | foreach ($emoticons as $idx => $file) { |
|---|
| 1412 | // <img title="Cry" src="http://.../program/js/tiny_mce/plugins/emotions/img/smiley-cry.gif" border="0" alt="Cry" /> |
|---|
| 1413 | $search[] = '/<img title="[a-z ]+" src="https?:\/\/[a-z0-9_.\/-]+\/tiny_mce\/plugins\/emotions\/img\/'.$file.'.gif"[^>]+\/>/i'; |
|---|
| 1414 | $replace[] = $idx; |
|---|
| 1415 | } |
|---|
| 1416 | |
|---|
| 1417 | return preg_replace($search, $replace, $html); |
|---|
| 1418 | } |
|---|
| 1419 | |
|---|
| 1420 | |
|---|
| 1421 | /** |
|---|
| 1422 | * Send the given message using the configured method |
|---|
| 1423 | * |
|---|
| 1424 | * @param object $message Reference to Mail_MIME object |
|---|
| 1425 | * @param string $from Sender address string |
|---|
| 1426 | * @param array $mailto Array of recipient address strings |
|---|
| 1427 | * @param array $smtp_error SMTP error array (reference) |
|---|
| 1428 | * @param string $body_file Location of file with saved message body (reference), |
|---|
| 1429 | * used when delay_file_io is enabled |
|---|
| 1430 | * @param array $smtp_opts SMTP options (e.g. DSN request) |
|---|
| 1431 | * |
|---|
| 1432 | * @return boolean Send status. |
|---|
| 1433 | */ |
|---|
| 1434 | function rcmail_deliver_message(&$message, $from, $mailto, &$smtp_error, &$body_file=null, $smtp_opts=null) |
|---|
| 1435 | { |
|---|
| 1436 | global $CONFIG, $RCMAIL; |
|---|
| 1437 | |
|---|
| 1438 | $headers = $message->headers(); |
|---|
| 1439 | |
|---|
| 1440 | // send thru SMTP server using custom SMTP library |
|---|
| 1441 | if ($CONFIG['smtp_server']) { |
|---|
| 1442 | // generate list of recipients |
|---|
| 1443 | $a_recipients = array($mailto); |
|---|
| 1444 | |
|---|
| 1445 | if (strlen($headers['Cc'])) |
|---|
| 1446 | $a_recipients[] = $headers['Cc']; |
|---|
| 1447 | if (strlen($headers['Bcc'])) |
|---|
| 1448 | $a_recipients[] = $headers['Bcc']; |
|---|
| 1449 | |
|---|
| 1450 | // clean Bcc from header for recipients |
|---|
| 1451 | $send_headers = $headers; |
|---|
| 1452 | unset($send_headers['Bcc']); |
|---|
| 1453 | // here too, it because txtHeaders() below use $message->_headers not only $send_headers |
|---|
| 1454 | unset($message->_headers['Bcc']); |
|---|
| 1455 | |
|---|
| 1456 | $smtp_headers = $message->txtHeaders($send_headers, true); |
|---|
| 1457 | |
|---|
| 1458 | if ($message->getParam('delay_file_io')) { |
|---|
| 1459 | // use common temp dir |
|---|
| 1460 | $temp_dir = $RCMAIL->config->get('temp_dir'); |
|---|
| 1461 | $body_file = tempnam($temp_dir, 'rcmMsg'); |
|---|
| 1462 | if (PEAR::isError($mime_result = $message->saveMessageBody($body_file))) { |
|---|
| 1463 | raise_error(array('code' => 650, 'type' => 'php', |
|---|
| 1464 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 1465 | 'message' => "Could not create message: ".$mime_result->getMessage()), |
|---|
| 1466 | TRUE, FALSE); |
|---|
| 1467 | return false; |
|---|
| 1468 | } |
|---|
| 1469 | $msg_body = fopen($body_file, 'r'); |
|---|
| 1470 | } else { |
|---|
| 1471 | $msg_body = $message->get(); |
|---|
| 1472 | } |
|---|
| 1473 | |
|---|
| 1474 | // send message |
|---|
| 1475 | if (!is_object($RCMAIL->smtp)) |
|---|
| 1476 | $RCMAIL->smtp_init(true); |
|---|
| 1477 | |
|---|
| 1478 | $sent = $RCMAIL->smtp->send_mail($from, $a_recipients, $smtp_headers, $msg_body, $smtp_opts); |
|---|
| 1479 | $smtp_response = $RCMAIL->smtp->get_response(); |
|---|
| 1480 | $smtp_error = $RCMAIL->smtp->get_error(); |
|---|
| 1481 | |
|---|
| 1482 | // log error |
|---|
| 1483 | if (!$sent) |
|---|
| 1484 | raise_error(array('code' => 800, 'type' => 'smtp', 'line' => __LINE__, 'file' => __FILE__, |
|---|
| 1485 | 'message' => "SMTP error: ".join("\n", $smtp_response)), TRUE, FALSE); |
|---|
| 1486 | } |
|---|
| 1487 | // send mail using PHP's mail() function |
|---|
| 1488 | else { |
|---|
| 1489 | // unset some headers because they will be added by the mail() function |
|---|
| 1490 | $headers_enc = $message->headers($headers); |
|---|
| 1491 | $headers_php = $message->_headers; |
|---|
| 1492 | unset($headers_php['To'], $headers_php['Subject']); |
|---|
| 1493 | |
|---|
| 1494 | // reset stored headers and overwrite |
|---|
| 1495 | $message->_headers = array(); |
|---|
| 1496 | $header_str = $message->txtHeaders($headers_php); |
|---|
| 1497 | |
|---|
| 1498 | // #1485779 |
|---|
| 1499 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
|---|
| 1500 | if (preg_match_all('/<([^@]+@[^>]+)>/', $headers_enc['To'], $m)) { |
|---|
| 1501 | $headers_enc['To'] = implode(', ', $m[1]); |
|---|
| 1502 | } |
|---|
| 1503 | } |
|---|
| 1504 | |
|---|
| 1505 | $msg_body = $message->get(); |
|---|
| 1506 | |
|---|
| 1507 | if (PEAR::isError($msg_body)) |
|---|
| 1508 | raise_error(array('code' => 650, 'type' => 'php', |
|---|
| 1509 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 1510 | 'message' => "Could not create message: ".$msg_body->getMessage()), |
|---|
| 1511 | TRUE, FALSE); |
|---|
| 1512 | else { |
|---|
| 1513 | $delim = $RCMAIL->config->header_delimiter(); |
|---|
| 1514 | $to = $headers_enc['To']; |
|---|
| 1515 | $subject = $headers_enc['Subject']; |
|---|
| 1516 | $header_str = rtrim($header_str); |
|---|
| 1517 | |
|---|
| 1518 | if ($delim != "\r\n") { |
|---|
| 1519 | $header_str = str_replace("\r\n", $delim, $header_str); |
|---|
| 1520 | $msg_body = str_replace("\r\n", $delim, $msg_body); |
|---|
| 1521 | $to = str_replace("\r\n", $delim, $to); |
|---|
| 1522 | $subject = str_replace("\r\n", $delim, $subject); |
|---|
| 1523 | } |
|---|
| 1524 | |
|---|
| 1525 | if (ini_get('safe_mode')) |
|---|
| 1526 | $sent = mail($to, $subject, $msg_body, $header_str); |
|---|
| 1527 | else |
|---|
| 1528 | $sent = mail($to, $subject, $msg_body, $header_str, "-f$from"); |
|---|
| 1529 | } |
|---|
| 1530 | } |
|---|
| 1531 | |
|---|
| 1532 | if ($sent) { |
|---|
| 1533 | $RCMAIL->plugins->exec_hook('message_sent', array('headers' => $headers, 'body' => $msg_body)); |
|---|
| 1534 | |
|---|
| 1535 | // remove MDN headers after sending |
|---|
| 1536 | unset($headers['Return-Receipt-To'], $headers['Disposition-Notification-To']); |
|---|
| 1537 | |
|---|
| 1538 | // get all recipients |
|---|
| 1539 | if ($headers['Cc']) |
|---|
| 1540 | $mailto .= $headers['Cc']; |
|---|
| 1541 | if ($headers['Bcc']) |
|---|
| 1542 | $mailto .= $headers['Bcc']; |
|---|
| 1543 | if (preg_match_all('/<([^@]+@[^>]+)>/', $mailto, $m)) |
|---|
| 1544 | $mailto = implode(', ', array_unique($m[1])); |
|---|
| 1545 | |
|---|
| 1546 | if ($CONFIG['smtp_log']) { |
|---|
| 1547 | write_log('sendmail', sprintf("User %s [%s]; Message for %s; %s", |
|---|
| 1548 | $RCMAIL->user->get_username(), |
|---|
| 1549 | $_SERVER['REMOTE_ADDR'], |
|---|
| 1550 | $mailto, |
|---|
| 1551 | !empty($smtp_response) ? join('; ', $smtp_response) : '')); |
|---|
| 1552 | } |
|---|
| 1553 | } |
|---|
| 1554 | |
|---|
| 1555 | if (is_resource($msg_body)) { |
|---|
| 1556 | fclose($msg_body); |
|---|
| 1557 | } |
|---|
| 1558 | |
|---|
| 1559 | $message->_headers = array(); |
|---|
| 1560 | $message->headers($headers); |
|---|
| 1561 | |
|---|
| 1562 | return $sent; |
|---|
| 1563 | } |
|---|
| 1564 | |
|---|
| 1565 | |
|---|
| 1566 | // Returns unique Message-ID |
|---|
| 1567 | function rcmail_gen_message_id() |
|---|
| 1568 | { |
|---|
| 1569 | global $RCMAIL; |
|---|
| 1570 | |
|---|
| 1571 | $local_part = md5(uniqid('rcmail'.mt_rand(),true)); |
|---|
| 1572 | $domain_part = $RCMAIL->user->get_username('domain'); |
|---|
| 1573 | |
|---|
| 1574 | // Try to find FQDN, some spamfilters doesn't like 'localhost' (#1486924) |
|---|
| 1575 | if (!preg_match('/\.[a-z]+$/i', $domain_part)) { |
|---|
| 1576 | if (($host = preg_replace('/:[0-9]+$/', '', $_SERVER['HTTP_HOST'])) |
|---|
| 1577 | && preg_match('/\.[a-z]+$/i', $host)) { |
|---|
| 1578 | $domain_part = $host; |
|---|
| 1579 | } |
|---|
| 1580 | else if (($host = preg_replace('/:[0-9]+$/', '', $_SERVER['SERVER_NAME'])) |
|---|
| 1581 | && preg_match('/\.[a-z]+$/i', $host)) { |
|---|
| 1582 | $domain_part = $host; |
|---|
| 1583 | } |
|---|
| 1584 | } |
|---|
| 1585 | |
|---|
| 1586 | return sprintf('<%s@%s>', $local_part, $domain_part); |
|---|
| 1587 | } |
|---|
| 1588 | |
|---|
| 1589 | |
|---|
| 1590 | // Returns RFC2822 formatted current date in user's timezone |
|---|
| 1591 | function rcmail_user_date() |
|---|
| 1592 | { |
|---|
| 1593 | global $RCMAIL; |
|---|
| 1594 | |
|---|
| 1595 | // get user's timezone |
|---|
| 1596 | try { |
|---|
| 1597 | $tz = new DateTimeZone($RCMAIL->config->get('timezone')); |
|---|
| 1598 | $date = new DateTime('now', $tz); |
|---|
| 1599 | } |
|---|
| 1600 | catch (Exception $e) { |
|---|
| 1601 | $date = new DateTime(); |
|---|
| 1602 | } |
|---|
| 1603 | |
|---|
| 1604 | return $date->format('r'); |
|---|
| 1605 | } |
|---|
| 1606 | |
|---|
| 1607 | |
|---|
| 1608 | /** |
|---|
| 1609 | * Check if we can process not exceeding memory_limit |
|---|
| 1610 | * |
|---|
| 1611 | * @param integer Required amount of memory |
|---|
| 1612 | * @return boolean |
|---|
| 1613 | */ |
|---|
| 1614 | function rcmail_mem_check($need) |
|---|
| 1615 | { |
|---|
| 1616 | $mem_limit = parse_bytes(ini_get('memory_limit')); |
|---|
| 1617 | $memory = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB |
|---|
| 1618 | |
|---|
| 1619 | return $mem_limit > 0 && $memory + $need > $mem_limit ? false : true; |
|---|
| 1620 | } |
|---|
| 1621 | |
|---|
| 1622 | |
|---|
| 1623 | /** |
|---|
| 1624 | * Check if working in SSL mode |
|---|
| 1625 | * |
|---|
| 1626 | * @param integer HTTPS port number |
|---|
| 1627 | * @param boolean Enables 'use_https' option checking |
|---|
| 1628 | * @return boolean |
|---|
| 1629 | */ |
|---|
| 1630 | function rcube_https_check($port=null, $use_https=true) |
|---|
| 1631 | { |
|---|
| 1632 | global $RCMAIL; |
|---|
| 1633 | |
|---|
| 1634 | if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') |
|---|
| 1635 | return true; |
|---|
| 1636 | if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https') |
|---|
| 1637 | return true; |
|---|
| 1638 | if ($port && $_SERVER['SERVER_PORT'] == $port) |
|---|
| 1639 | return true; |
|---|
| 1640 | if ($use_https && isset($RCMAIL) && $RCMAIL->config->get('use_https')) |
|---|
| 1641 | return true; |
|---|
| 1642 | |
|---|
| 1643 | return false; |
|---|
| 1644 | } |
|---|
| 1645 | |
|---|
| 1646 | |
|---|
| 1647 | /** |
|---|
| 1648 | * For backward compatibility. |
|---|
| 1649 | * |
|---|
| 1650 | * @global rcmail $RCMAIL |
|---|
| 1651 | * @param string $var_name Variable name. |
|---|
| 1652 | * @return void |
|---|
| 1653 | */ |
|---|
| 1654 | function rcube_sess_unset($var_name=null) |
|---|
| 1655 | { |
|---|
| 1656 | global $RCMAIL; |
|---|
| 1657 | |
|---|
| 1658 | $RCMAIL->session->remove($var_name); |
|---|
| 1659 | } |
|---|
| 1660 | |
|---|
| 1661 | |
|---|
| 1662 | /** |
|---|
| 1663 | * Replaces hostname variables |
|---|
| 1664 | * |
|---|
| 1665 | * @param string $name Hostname |
|---|
| 1666 | * @param string $host Optional IMAP hostname |
|---|
| 1667 | * @return string |
|---|
| 1668 | */ |
|---|
| 1669 | function rcube_parse_host($name, $host='') |
|---|
| 1670 | { |
|---|
| 1671 | // %n - host |
|---|
| 1672 | $n = preg_replace('/:\d+$/', '', $_SERVER['SERVER_NAME']); |
|---|
| 1673 | // %d - domain name without first part, e.g. %n=mail.domain.tld, %d=domain.tld |
|---|
| 1674 | $d = preg_replace('/^[^\.]+\./', '', $n); |
|---|
| 1675 | // %h - IMAP host |
|---|
| 1676 | $h = $_SESSION['storage_host'] ? $_SESSION['storage_host'] : $host; |
|---|
| 1677 | // %z - IMAP domain without first part, e.g. %h=imap.domain.tld, %z=domain.tld |
|---|
| 1678 | $z = preg_replace('/^[^\.]+\./', '', $h); |
|---|
| 1679 | // %s - domain name after the '@' from e-mail address provided at login screen. Returns FALSE if an invalid email is provided |
|---|
| 1680 | if ( strpos($name, '%s') !== false ){ |
|---|
| 1681 | $user_email = rcube_idn_convert(get_input_value('_user', RCUBE_INPUT_POST), true); |
|---|
| 1682 | if ( preg_match('/(.*)@([a-z0-9\.\-\[\]\:]+)/i', $user_email, $s) < 1 || filter_var($s[1]."@".$s[2], FILTER_VALIDATE_EMAIL) === false ) |
|---|
| 1683 | return false; |
|---|
| 1684 | } |
|---|
| 1685 | |
|---|
| 1686 | $name = str_replace(array('%n', '%d', '%h', '%z', '%s'), array($n, $d, $h, $z, $s[2]), $name); |
|---|
| 1687 | return $name; |
|---|
| 1688 | } |
|---|
| 1689 | |
|---|
| 1690 | |
|---|
| 1691 | /** |
|---|
| 1692 | * E-mail address validation |
|---|
| 1693 | * |
|---|
| 1694 | * @param string $email Email address |
|---|
| 1695 | * @param boolean $dns_check True to check dns |
|---|
| 1696 | * @return boolean |
|---|
| 1697 | */ |
|---|
| 1698 | function check_email($email, $dns_check=true) |
|---|
| 1699 | { |
|---|
| 1700 | // Check for invalid characters |
|---|
| 1701 | if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $email)) |
|---|
| 1702 | return false; |
|---|
| 1703 | |
|---|
| 1704 | // Check for length limit specified by RFC 5321 (#1486453) |
|---|
| 1705 | if (strlen($email) > 254) |
|---|
| 1706 | return false; |
|---|
| 1707 | |
|---|
| 1708 | $email_array = explode('@', $email); |
|---|
| 1709 | |
|---|
| 1710 | // Check that there's one @ symbol |
|---|
| 1711 | if (count($email_array) < 2) |
|---|
| 1712 | return false; |
|---|
| 1713 | |
|---|
| 1714 | $domain_part = array_pop($email_array); |
|---|
| 1715 | $local_part = implode('@', $email_array); |
|---|
| 1716 | |
|---|
| 1717 | // from PEAR::Validate |
|---|
| 1718 | $regexp = '&^(?: |
|---|
| 1719 | ("\s*(?:[^"\f\n\r\t\v\b\s]+\s*)+")| #1 quoted name |
|---|
| 1720 | ([-\w!\#\$%\&\'*+~/^`|{}=]+(?:\.[-\w!\#\$%\&\'*+~/^`|{}=]+)*)) #2 OR dot-atom (RFC5322) |
|---|
| 1721 | $&xi'; |
|---|
| 1722 | |
|---|
| 1723 | if (!preg_match($regexp, $local_part)) |
|---|
| 1724 | return false; |
|---|
| 1725 | |
|---|
| 1726 | // Check domain part |
|---|
| 1727 | if (preg_match('/^\[*(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\]*$/', $domain_part)) |
|---|
| 1728 | return true; // IP address |
|---|
| 1729 | else { |
|---|
| 1730 | // If not an IP address |
|---|
| 1731 | $domain_array = explode('.', $domain_part); |
|---|
| 1732 | if (sizeof($domain_array) < 2) |
|---|
| 1733 | return false; // Not enough parts to be a valid domain |
|---|
| 1734 | |
|---|
| 1735 | foreach ($domain_array as $part) |
|---|
| 1736 | if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]))$/', $part)) |
|---|
| 1737 | return false; |
|---|
| 1738 | |
|---|
| 1739 | if (!$dns_check || !rcmail::get_instance()->config->get('email_dns_check')) |
|---|
| 1740 | return true; |
|---|
| 1741 | |
|---|
| 1742 | if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' && version_compare(PHP_VERSION, '5.3.0', '<')) { |
|---|
| 1743 | $lookup = array(); |
|---|
| 1744 | @exec("nslookup -type=MX " . escapeshellarg($domain_part) . " 2>&1", $lookup); |
|---|
| 1745 | foreach ($lookup as $line) { |
|---|
| 1746 | if (strpos($line, 'MX preference')) |
|---|
| 1747 | return true; |
|---|
| 1748 | } |
|---|
| 1749 | return false; |
|---|
| 1750 | } |
|---|
| 1751 | |
|---|
| 1752 | // find MX record(s) |
|---|
| 1753 | if (getmxrr($domain_part, $mx_records)) |
|---|
| 1754 | return true; |
|---|
| 1755 | |
|---|
| 1756 | // find any DNS record |
|---|
| 1757 | if (checkdnsrr($domain_part, 'ANY')) |
|---|
| 1758 | return true; |
|---|
| 1759 | } |
|---|
| 1760 | |
|---|
| 1761 | return false; |
|---|
| 1762 | } |
|---|
| 1763 | |
|---|
| 1764 | /* |
|---|
| 1765 | * Idn_to_ascii wrapper. |
|---|
| 1766 | * Intl/Idn modules version of this function doesn't work with e-mail address |
|---|
| 1767 | */ |
|---|
| 1768 | function rcube_idn_to_ascii($str) |
|---|
| 1769 | { |
|---|
| 1770 | return rcube_idn_convert($str, true); |
|---|
| 1771 | } |
|---|
| 1772 | |
|---|
| 1773 | /* |
|---|
| 1774 | * Idn_to_ascii wrapper. |
|---|
| 1775 | * Intl/Idn modules version of this function doesn't work with e-mail address |
|---|
| 1776 | */ |
|---|
| 1777 | function rcube_idn_to_utf8($str) |
|---|
| 1778 | { |
|---|
| 1779 | return rcube_idn_convert($str, false); |
|---|
| 1780 | } |
|---|
| 1781 | |
|---|
| 1782 | function rcube_idn_convert($input, $is_utf=false) |
|---|
| 1783 | { |
|---|
| 1784 | if ($at = strpos($input, '@')) { |
|---|
| 1785 | $user = substr($input, 0, $at); |
|---|
| 1786 | $domain = substr($input, $at+1); |
|---|
| 1787 | } |
|---|
| 1788 | else { |
|---|
| 1789 | $domain = $input; |
|---|
| 1790 | } |
|---|
| 1791 | |
|---|
| 1792 | $domain = $is_utf ? idn_to_ascii($domain) : idn_to_utf8($domain); |
|---|
| 1793 | |
|---|
| 1794 | if ($domain === false) { |
|---|
| 1795 | return ''; |
|---|
| 1796 | } |
|---|
| 1797 | |
|---|
| 1798 | return $at ? $user . '@' . $domain : $domain; |
|---|
| 1799 | } |
|---|
| 1800 | |
|---|
| 1801 | |
|---|
| 1802 | /** |
|---|
| 1803 | * Helper class to turn relative urls into absolute ones |
|---|
| 1804 | * using a predefined base |
|---|
| 1805 | */ |
|---|
| 1806 | class rcube_base_replacer |
|---|
| 1807 | { |
|---|
| 1808 | private $base_url; |
|---|
| 1809 | |
|---|
| 1810 | public function __construct($base) |
|---|
| 1811 | { |
|---|
| 1812 | $this->base_url = $base; |
|---|
| 1813 | } |
|---|
| 1814 | |
|---|
| 1815 | public function callback($matches) |
|---|
| 1816 | { |
|---|
| 1817 | return $matches[1] . '="' . self::absolute_url($matches[3], $this->base_url) . '"'; |
|---|
| 1818 | } |
|---|
| 1819 | |
|---|
| 1820 | public function replace($body) |
|---|
| 1821 | { |
|---|
| 1822 | return preg_replace_callback(array( |
|---|
| 1823 | '/(src|background|href)=(["\']?)([^"\'\s]+)(\2|\s|>)/Ui', |
|---|
| 1824 | '/(url\s*\()(["\']?)([^"\'\)\s]+)(\2)\)/Ui', |
|---|
| 1825 | ), |
|---|
| 1826 | array($this, 'callback'), $body); |
|---|
| 1827 | } |
|---|
| 1828 | |
|---|
| 1829 | /** |
|---|
| 1830 | * Convert paths like ../xxx to an absolute path using a base url |
|---|
| 1831 | * |
|---|
| 1832 | * @param string $path Relative path |
|---|
| 1833 | * @param string $base_url Base URL |
|---|
| 1834 | * |
|---|
| 1835 | * @return string Absolute URL |
|---|
| 1836 | */ |
|---|
| 1837 | public static function absolute_url($path, $base_url) |
|---|
| 1838 | { |
|---|
| 1839 | $host_url = $base_url; |
|---|
| 1840 | $abs_path = $path; |
|---|
| 1841 | |
|---|
| 1842 | // check if path is an absolute URL |
|---|
| 1843 | if (preg_match('/^[fhtps]+:\/\//', $path)) { |
|---|
| 1844 | return $path; |
|---|
| 1845 | } |
|---|
| 1846 | |
|---|
| 1847 | // check if path is a content-id scheme |
|---|
| 1848 | if (strpos($path, 'cid:') === 0) { |
|---|
| 1849 | return $path; |
|---|
| 1850 | } |
|---|
| 1851 | |
|---|
| 1852 | // cut base_url to the last directory |
|---|
| 1853 | if (strrpos($base_url, '/') > 7) { |
|---|
| 1854 | $host_url = substr($base_url, 0, strpos($base_url, '/', 7)); |
|---|
| 1855 | $base_url = substr($base_url, 0, strrpos($base_url, '/')); |
|---|
| 1856 | } |
|---|
| 1857 | |
|---|
| 1858 | // $path is absolute |
|---|
| 1859 | if ($path[0] == '/') { |
|---|
| 1860 | $abs_path = $host_url.$path; |
|---|
| 1861 | } |
|---|
| 1862 | else { |
|---|
| 1863 | // strip './' because its the same as '' |
|---|
| 1864 | $path = preg_replace('/^\.\//', '', $path); |
|---|
| 1865 | |
|---|
| 1866 | if (preg_match_all('/\.\.\//', $path, $matches, PREG_SET_ORDER)) { |
|---|
| 1867 | foreach ($matches as $a_match) { |
|---|
| 1868 | if (strrpos($base_url, '/')) { |
|---|
| 1869 | $base_url = substr($base_url, 0, strrpos($base_url, '/')); |
|---|
| 1870 | } |
|---|
| 1871 | $path = substr($path, 3); |
|---|
| 1872 | } |
|---|
| 1873 | } |
|---|
| 1874 | |
|---|
| 1875 | $abs_path = $base_url.'/'.$path; |
|---|
| 1876 | } |
|---|
| 1877 | |
|---|
| 1878 | return $abs_path; |
|---|
| 1879 | } |
|---|
| 1880 | } |
|---|
| 1881 | |
|---|
| 1882 | |
|---|
| 1883 | /****** debugging and logging functions ********/ |
|---|
| 1884 | |
|---|
| 1885 | /** |
|---|
| 1886 | * Print or write debug messages |
|---|
| 1887 | * |
|---|
| 1888 | * @param mixed Debug message or data |
|---|
| 1889 | * @return void |
|---|
| 1890 | */ |
|---|
| 1891 | function console() |
|---|
| 1892 | { |
|---|
| 1893 | $args = func_get_args(); |
|---|
| 1894 | |
|---|
| 1895 | if (class_exists('rcmail', false)) { |
|---|
| 1896 | $rcmail = rcmail::get_instance(); |
|---|
| 1897 | if (is_object($rcmail->plugins)) { |
|---|
| 1898 | $plugin = $rcmail->plugins->exec_hook('console', array('args' => $args)); |
|---|
| 1899 | if ($plugin['abort']) |
|---|
| 1900 | return; |
|---|
| 1901 | $args = $plugin['args']; |
|---|
| 1902 | } |
|---|
| 1903 | } |
|---|
| 1904 | |
|---|
| 1905 | $msg = array(); |
|---|
| 1906 | foreach ($args as $arg) |
|---|
| 1907 | $msg[] = !is_string($arg) ? var_export($arg, true) : $arg; |
|---|
| 1908 | |
|---|
| 1909 | write_log('console', join(";\n", $msg)); |
|---|
| 1910 | } |
|---|
| 1911 | |
|---|
| 1912 | |
|---|
| 1913 | /** |
|---|
| 1914 | * Append a line to a logfile in the logs directory. |
|---|
| 1915 | * Date will be added automatically to the line. |
|---|
| 1916 | * |
|---|
| 1917 | * @param $name name of log file |
|---|
| 1918 | * @param line Line to append |
|---|
| 1919 | * @return void |
|---|
| 1920 | */ |
|---|
| 1921 | function write_log($name, $line) |
|---|
| 1922 | { |
|---|
| 1923 | global $CONFIG, $RCMAIL; |
|---|
| 1924 | |
|---|
| 1925 | if (!is_string($line)) |
|---|
| 1926 | $line = var_export($line, true); |
|---|
| 1927 | |
|---|
| 1928 | if (empty($CONFIG['log_date_format'])) |
|---|
| 1929 | $CONFIG['log_date_format'] = 'd-M-Y H:i:s O'; |
|---|
| 1930 | |
|---|
| 1931 | $date = date($CONFIG['log_date_format']); |
|---|
| 1932 | |
|---|
| 1933 | // trigger logging hook |
|---|
| 1934 | if (is_object($RCMAIL) && is_object($RCMAIL->plugins)) { |
|---|
| 1935 | $log = $RCMAIL->plugins->exec_hook('write_log', array('name' => $name, 'date' => $date, 'line' => $line)); |
|---|
| 1936 | $name = $log['name']; |
|---|
| 1937 | $line = $log['line']; |
|---|
| 1938 | $date = $log['date']; |
|---|
| 1939 | if ($log['abort']) |
|---|
| 1940 | return true; |
|---|
| 1941 | } |
|---|
| 1942 | |
|---|
| 1943 | if ($CONFIG['log_driver'] == 'syslog') { |
|---|
| 1944 | $prio = $name == 'errors' ? LOG_ERR : LOG_INFO; |
|---|
| 1945 | syslog($prio, $line); |
|---|
| 1946 | return true; |
|---|
| 1947 | } |
|---|
| 1948 | else { |
|---|
| 1949 | $line = sprintf("[%s]: %s\n", $date, $line); |
|---|
| 1950 | |
|---|
| 1951 | // log_driver == 'file' is assumed here |
|---|
| 1952 | if (empty($CONFIG['log_dir'])) |
|---|
| 1953 | $CONFIG['log_dir'] = INSTALL_PATH.'logs'; |
|---|
| 1954 | |
|---|
| 1955 | // try to open specific log file for writing |
|---|
| 1956 | $logfile = $CONFIG['log_dir'].'/'.$name; |
|---|
| 1957 | if ($fp = @fopen($logfile, 'a')) { |
|---|
| 1958 | fwrite($fp, $line); |
|---|
| 1959 | fflush($fp); |
|---|
| 1960 | fclose($fp); |
|---|
| 1961 | return true; |
|---|
| 1962 | } |
|---|
| 1963 | else |
|---|
| 1964 | trigger_error("Error writing to log file $logfile; Please check permissions", E_USER_WARNING); |
|---|
| 1965 | } |
|---|
| 1966 | |
|---|
| 1967 | return false; |
|---|
| 1968 | } |
|---|
| 1969 | |
|---|
| 1970 | |
|---|
| 1971 | /** |
|---|
| 1972 | * Write login data (name, ID, IP address) to the 'userlogins' log file. |
|---|
| 1973 | * |
|---|
| 1974 | * @return void |
|---|
| 1975 | */ |
|---|
| 1976 | function rcmail_log_login() |
|---|
| 1977 | { |
|---|
| 1978 | global $RCMAIL; |
|---|
| 1979 | |
|---|
| 1980 | if (!$RCMAIL->config->get('log_logins') || !$RCMAIL->user) |
|---|
| 1981 | return; |
|---|
| 1982 | |
|---|
| 1983 | write_log('userlogins', sprintf('Successful login for %s (ID: %d) from %s in session %s', |
|---|
| 1984 | $RCMAIL->user->get_username(), $RCMAIL->user->ID, rcmail_remote_ip(), session_id())); |
|---|
| 1985 | } |
|---|
| 1986 | |
|---|
| 1987 | |
|---|
| 1988 | /** |
|---|
| 1989 | * Returns remote IP address and forwarded addresses if found |
|---|
| 1990 | * |
|---|
| 1991 | * @return string Remote IP address(es) |
|---|
| 1992 | */ |
|---|
| 1993 | function rcmail_remote_ip() |
|---|
| 1994 | { |
|---|
| 1995 | $address = $_SERVER['REMOTE_ADDR']; |
|---|
| 1996 | |
|---|
| 1997 | // append the NGINX X-Real-IP header, if set |
|---|
| 1998 | if (!empty($_SERVER['HTTP_X_REAL_IP'])) { |
|---|
| 1999 | $remote_ip[] = 'X-Real-IP: ' . $_SERVER['HTTP_X_REAL_IP']; |
|---|
| 2000 | } |
|---|
| 2001 | // append the X-Forwarded-For header, if set |
|---|
| 2002 | if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
|---|
| 2003 | $remote_ip[] = 'X-Forwarded-For: ' . $_SERVER['HTTP_X_FORWARDED_FOR']; |
|---|
| 2004 | } |
|---|
| 2005 | |
|---|
| 2006 | if (!empty($remote_ip)) |
|---|
| 2007 | $address .= '(' . implode(',', $remote_ip) . ')'; |
|---|
| 2008 | |
|---|
| 2009 | return $address; |
|---|
| 2010 | } |
|---|
| 2011 | |
|---|
| 2012 | |
|---|
| 2013 | /** |
|---|
| 2014 | * Check whether the HTTP referer matches the current request |
|---|
| 2015 | * |
|---|
| 2016 | * @return boolean True if referer is the same host+path, false if not |
|---|
| 2017 | */ |
|---|
| 2018 | function rcube_check_referer() |
|---|
| 2019 | { |
|---|
| 2020 | $uri = parse_url($_SERVER['REQUEST_URI']); |
|---|
| 2021 | $referer = parse_url(rc_request_header('Referer')); |
|---|
| 2022 | return $referer['host'] == rc_request_header('Host') && $referer['path'] == $uri['path']; |
|---|
| 2023 | } |
|---|
| 2024 | |
|---|
| 2025 | |
|---|
| 2026 | /** |
|---|
| 2027 | * @access private |
|---|
| 2028 | * @return mixed |
|---|
| 2029 | */ |
|---|
| 2030 | function rcube_timer() |
|---|
| 2031 | { |
|---|
| 2032 | return microtime(true); |
|---|
| 2033 | } |
|---|
| 2034 | |
|---|
| 2035 | |
|---|
| 2036 | /** |
|---|
| 2037 | * @access private |
|---|
| 2038 | * @return void |
|---|
| 2039 | */ |
|---|
| 2040 | function rcube_print_time($timer, $label='Timer', $dest='console') |
|---|
| 2041 | { |
|---|
| 2042 | static $print_count = 0; |
|---|
| 2043 | |
|---|
| 2044 | $print_count++; |
|---|
| 2045 | $now = rcube_timer(); |
|---|
| 2046 | $diff = $now-$timer; |
|---|
| 2047 | |
|---|
| 2048 | if (empty($label)) |
|---|
| 2049 | $label = 'Timer '.$print_count; |
|---|
| 2050 | |
|---|
| 2051 | write_log($dest, sprintf("%s: %0.4f sec", $label, $diff)); |
|---|
| 2052 | } |
|---|
| 2053 | |
|---|
| 2054 | |
|---|
| 2055 | /** |
|---|
| 2056 | * Throw system error and show error page |
|---|
| 2057 | * |
|---|
| 2058 | * @param array Named parameters |
|---|
| 2059 | * - code: Error code (required) |
|---|
| 2060 | * - type: Error type [php|db|imap|javascript] (required) |
|---|
| 2061 | * - message: Error message |
|---|
| 2062 | * - file: File where error occured |
|---|
| 2063 | * - line: Line where error occured |
|---|
| 2064 | * @param boolean True to log the error |
|---|
| 2065 | * @param boolean Terminate script execution |
|---|
| 2066 | */ |
|---|
| 2067 | // may be defined in Installer |
|---|
| 2068 | if (!function_exists('raise_error')) { |
|---|
| 2069 | function raise_error($arg=array(), $log=false, $terminate=false) |
|---|
| 2070 | { |
|---|
| 2071 | global $__page_content, $CONFIG, $OUTPUT, $ERROR_CODE, $ERROR_MESSAGE; |
|---|
| 2072 | |
|---|
| 2073 | // report bug (if not incompatible browser) |
|---|
| 2074 | if ($log && $arg['type'] && $arg['message']) |
|---|
| 2075 | rcube_log_bug($arg); |
|---|
| 2076 | |
|---|
| 2077 | // display error page and terminate script |
|---|
| 2078 | if ($terminate) { |
|---|
| 2079 | $ERROR_CODE = $arg['code']; |
|---|
| 2080 | $ERROR_MESSAGE = $arg['message']; |
|---|
| 2081 | include INSTALL_PATH . 'program/steps/utils/error.inc'; |
|---|
| 2082 | exit; |
|---|
| 2083 | } |
|---|
| 2084 | } |
|---|
| 2085 | } |
|---|
| 2086 | |
|---|
| 2087 | |
|---|
| 2088 | /** |
|---|
| 2089 | * Report error according to configured debug_level |
|---|
| 2090 | * |
|---|
| 2091 | * @param array Named parameters |
|---|
| 2092 | * @return void |
|---|
| 2093 | * @see raise_error() |
|---|
| 2094 | */ |
|---|
| 2095 | function rcube_log_bug($arg_arr) |
|---|
| 2096 | { |
|---|
| 2097 | global $CONFIG; |
|---|
| 2098 | |
|---|
| 2099 | $program = strtoupper($arg_arr['type']); |
|---|
| 2100 | $level = $CONFIG['debug_level']; |
|---|
| 2101 | |
|---|
| 2102 | // disable errors for ajax requests, write to log instead (#1487831) |
|---|
| 2103 | if (($level & 4) && !empty($_REQUEST['_remote'])) { |
|---|
| 2104 | $level = ($level ^ 4) | 1; |
|---|
| 2105 | } |
|---|
| 2106 | |
|---|
| 2107 | // write error to local log file |
|---|
| 2108 | if ($level & 1) { |
|---|
| 2109 | $post_query = ($_SERVER['REQUEST_METHOD'] == 'POST' ? '?_task='.urlencode($_POST['_task']).'&_action='.urlencode($_POST['_action']) : ''); |
|---|
| 2110 | $log_entry = sprintf("%s Error: %s%s (%s %s)", |
|---|
| 2111 | $program, |
|---|
| 2112 | $arg_arr['message'], |
|---|
| 2113 | $arg_arr['file'] ? sprintf(' in %s on line %d', $arg_arr['file'], $arg_arr['line']) : '', |
|---|
| 2114 | $_SERVER['REQUEST_METHOD'], |
|---|
| 2115 | $_SERVER['REQUEST_URI'] . $post_query); |
|---|
| 2116 | |
|---|
| 2117 | if (!write_log('errors', $log_entry)) { |
|---|
| 2118 | // send error to PHPs error handler if write_log didn't succeed |
|---|
| 2119 | trigger_error($arg_arr['message']); |
|---|
| 2120 | } |
|---|
| 2121 | } |
|---|
| 2122 | |
|---|
| 2123 | // report the bug to the global bug reporting system |
|---|
| 2124 | if ($level & 2) { |
|---|
| 2125 | // TODO: Send error via HTTP |
|---|
| 2126 | } |
|---|
| 2127 | |
|---|
| 2128 | // show error if debug_mode is on |
|---|
| 2129 | if ($level & 4) { |
|---|
| 2130 | print "<b>$program Error"; |
|---|
| 2131 | |
|---|
| 2132 | if (!empty($arg_arr['file']) && !empty($arg_arr['line'])) |
|---|
| 2133 | print " in $arg_arr[file] ($arg_arr[line])"; |
|---|
| 2134 | |
|---|
| 2135 | print ':</b> '; |
|---|
| 2136 | print nl2br($arg_arr['message']); |
|---|
| 2137 | print '<br />'; |
|---|
| 2138 | flush(); |
|---|
| 2139 | } |
|---|
| 2140 | } |
|---|
| 2141 | |
|---|
| 2142 | function rcube_upload_progress() |
|---|
| 2143 | { |
|---|
| 2144 | global $RCMAIL; |
|---|
| 2145 | |
|---|
| 2146 | $prefix = ini_get('apc.rfc1867_prefix'); |
|---|
| 2147 | $params = array( |
|---|
| 2148 | 'action' => $RCMAIL->action, |
|---|
| 2149 | 'name' => get_input_value('_progress', RCUBE_INPUT_GET), |
|---|
| 2150 | ); |
|---|
| 2151 | |
|---|
| 2152 | if (function_exists('apc_fetch')) { |
|---|
| 2153 | $status = apc_fetch($prefix . $params['name']); |
|---|
| 2154 | |
|---|
| 2155 | if (!empty($status)) { |
|---|
| 2156 | $status['percent'] = round($status['current']/$status['total']*100); |
|---|
| 2157 | $params = array_merge($status, $params); |
|---|
| 2158 | } |
|---|
| 2159 | } |
|---|
| 2160 | |
|---|
| 2161 | if (isset($params['percent'])) |
|---|
| 2162 | $params['text'] = rcube_label(array('name' => 'uploadprogress', 'vars' => array( |
|---|
| 2163 | 'percent' => $params['percent'] . '%', |
|---|
| 2164 | 'current' => show_bytes($params['current']), |
|---|
| 2165 | 'total' => show_bytes($params['total']) |
|---|
| 2166 | ))); |
|---|
| 2167 | |
|---|
| 2168 | $RCMAIL->output->command('upload_progress_update', $params); |
|---|
| 2169 | $RCMAIL->output->send(); |
|---|
| 2170 | } |
|---|
| 2171 | |
|---|
| 2172 | function rcube_upload_init() |
|---|
| 2173 | { |
|---|
| 2174 | global $RCMAIL; |
|---|
| 2175 | |
|---|
| 2176 | // Enable upload progress bar |
|---|
| 2177 | if (($seconds = $RCMAIL->config->get('upload_progress')) && ini_get('apc.rfc1867')) { |
|---|
| 2178 | if ($field_name = ini_get('apc.rfc1867_name')) { |
|---|
| 2179 | $RCMAIL->output->set_env('upload_progress_name', $field_name); |
|---|
| 2180 | $RCMAIL->output->set_env('upload_progress_time', (int) $seconds); |
|---|
| 2181 | } |
|---|
| 2182 | } |
|---|
| 2183 | |
|---|
| 2184 | // find max filesize value |
|---|
| 2185 | $max_filesize = parse_bytes(ini_get('upload_max_filesize')); |
|---|
| 2186 | $max_postsize = parse_bytes(ini_get('post_max_size')); |
|---|
| 2187 | if ($max_postsize && $max_postsize < $max_filesize) |
|---|
| 2188 | $max_filesize = $max_postsize; |
|---|
| 2189 | |
|---|
| 2190 | $RCMAIL->output->set_env('max_filesize', $max_filesize); |
|---|
| 2191 | $max_filesize = show_bytes($max_filesize); |
|---|
| 2192 | $RCMAIL->output->set_env('filesizeerror', rcube_label(array( |
|---|
| 2193 | 'name' => 'filesizeerror', 'vars' => array('size' => $max_filesize)))); |
|---|
| 2194 | |
|---|
| 2195 | return $max_filesize; |
|---|
| 2196 | } |
|---|
| 2197 | |
|---|
| 2198 | /** |
|---|
| 2199 | * Initializes client-side autocompletion |
|---|
| 2200 | */ |
|---|
| 2201 | function rcube_autocomplete_init() |
|---|
| 2202 | { |
|---|
| 2203 | global $RCMAIL; |
|---|
| 2204 | static $init; |
|---|
| 2205 | |
|---|
| 2206 | if ($init) |
|---|
| 2207 | return; |
|---|
| 2208 | |
|---|
| 2209 | $init = 1; |
|---|
| 2210 | |
|---|
| 2211 | if (($threads = (int)$RCMAIL->config->get('autocomplete_threads')) > 0) { |
|---|
| 2212 | $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql'); |
|---|
| 2213 | if (count($book_types) > 1) { |
|---|
| 2214 | $RCMAIL->output->set_env('autocomplete_threads', $threads); |
|---|
| 2215 | $RCMAIL->output->set_env('autocomplete_sources', $book_types); |
|---|
| 2216 | } |
|---|
| 2217 | } |
|---|
| 2218 | |
|---|
| 2219 | $RCMAIL->output->set_env('autocomplete_max', (int)$RCMAIL->config->get('autocomplete_max', 15)); |
|---|
| 2220 | $RCMAIL->output->set_env('autocomplete_min_length', $RCMAIL->config->get('autocomplete_min_length')); |
|---|
| 2221 | $RCMAIL->output->add_label('autocompletechars', 'autocompletemore'); |
|---|
| 2222 | } |
|---|
| 2223 | |
|---|
| 2224 | function rcube_fontdefs($font = null) |
|---|
| 2225 | { |
|---|
| 2226 | $fonts = array( |
|---|
| 2227 | 'Andale Mono' => '"Andale Mono",Times,monospace', |
|---|
| 2228 | 'Arial' => 'Arial,Helvetica,sans-serif', |
|---|
| 2229 | 'Arial Black' => '"Arial Black","Avant Garde",sans-serif', |
|---|
| 2230 | 'Book Antiqua' => '"Book Antiqua",Palatino,serif', |
|---|
| 2231 | 'Courier New' => '"Courier New",Courier,monospace', |
|---|
| 2232 | 'Georgia' => 'Georgia,Palatino,serif', |
|---|
| 2233 | 'Helvetica' => 'Helvetica,Arial,sans-serif', |
|---|
| 2234 | 'Impact' => 'Impact,Chicago,sans-serif', |
|---|
| 2235 | 'Tahoma' => 'Tahoma,Arial,Helvetica,sans-serif', |
|---|
| 2236 | 'Terminal' => 'Terminal,Monaco,monospace', |
|---|
| 2237 | 'Times New Roman' => '"Times New Roman",Times,serif', |
|---|
| 2238 | 'Trebuchet MS' => '"Trebuchet MS",Geneva,sans-serif', |
|---|
| 2239 | 'Verdana' => 'Verdana,Geneva,sans-serif', |
|---|
| 2240 | ); |
|---|
| 2241 | |
|---|
| 2242 | if ($font) |
|---|
| 2243 | return $fonts[$font]; |
|---|
| 2244 | |
|---|
| 2245 | return $fonts; |
|---|
| 2246 | } |
|---|