Mercurial > hg > rc2
comparison program/include/rcmail.php @ 0:4681f974d28b
vanilla 1.3.3 distro, I hope
| author | Charlie Root |
|---|---|
| date | Thu, 04 Jan 2018 15:52:31 -0500 |
| parents | |
| children | 85a746e95663 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4681f974d28b |
|---|---|
| 1 <?php | |
| 2 | |
| 3 /** | |
| 4 +-----------------------------------------------------------------------+ | |
| 5 | program/include/rcmail.php | | |
| 6 | | | |
| 7 | This file is part of the Roundcube Webmail client | | |
| 8 | Copyright (C) 2008-2014, The Roundcube Dev Team | | |
| 9 | Copyright (C) 2011-2014, Kolab Systems AG | | |
| 10 | | | |
| 11 | Licensed under the GNU General Public License version 3 or | | |
| 12 | any later version with exceptions for skins & plugins. | | |
| 13 | See the README file for a full license statement. | | |
| 14 | | | |
| 15 | PURPOSE: | | |
| 16 | Application class providing core functions and holding | | |
| 17 | instances of all 'global' objects like db- and imap-connections | | |
| 18 +-----------------------------------------------------------------------+ | |
| 19 | Author: Thomas Bruederli <roundcube@gmail.com> | | |
| 20 | Author: Aleksander Machniak <alec@alec.pl> | | |
| 21 +-----------------------------------------------------------------------+ | |
| 22 */ | |
| 23 | |
| 24 /** | |
| 25 * Application class of Roundcube Webmail | |
| 26 * implemented as singleton | |
| 27 * | |
| 28 * @package Webmail | |
| 29 */ | |
| 30 class rcmail extends rcube | |
| 31 { | |
| 32 /** | |
| 33 * Main tasks. | |
| 34 * | |
| 35 * @var array | |
| 36 */ | |
| 37 static public $main_tasks = array('mail','settings','addressbook','login','logout','utils','dummy'); | |
| 38 | |
| 39 /** | |
| 40 * Current task. | |
| 41 * | |
| 42 * @var string | |
| 43 */ | |
| 44 public $task; | |
| 45 | |
| 46 /** | |
| 47 * Current action. | |
| 48 * | |
| 49 * @var string | |
| 50 */ | |
| 51 public $action = ''; | |
| 52 public $comm_path = './'; | |
| 53 public $filename = ''; | |
| 54 | |
| 55 private $address_books = array(); | |
| 56 private $action_map = array(); | |
| 57 | |
| 58 | |
| 59 const ERROR_STORAGE = -2; | |
| 60 const ERROR_INVALID_REQUEST = 1; | |
| 61 const ERROR_INVALID_HOST = 2; | |
| 62 const ERROR_COOKIES_DISABLED = 3; | |
| 63 const ERROR_RATE_LIMIT = 4; | |
| 64 | |
| 65 | |
| 66 /** | |
| 67 * This implements the 'singleton' design pattern | |
| 68 * | |
| 69 * @param integer $mode Ignored rcube::get_instance() argument | |
| 70 * @param string $env Environment name to run (e.g. live, dev, test) | |
| 71 * | |
| 72 * @return rcmail The one and only instance | |
| 73 */ | |
| 74 static function get_instance($mode = 0, $env = '') | |
| 75 { | |
| 76 if (!self::$instance || !is_a(self::$instance, 'rcmail')) { | |
| 77 self::$instance = new rcmail($env); | |
| 78 // init AFTER object was linked with self::$instance | |
| 79 self::$instance->startup(); | |
| 80 } | |
| 81 | |
| 82 return self::$instance; | |
| 83 } | |
| 84 | |
| 85 /** | |
| 86 * Initial startup function | |
| 87 * to register session, create database and imap connections | |
| 88 */ | |
| 89 protected function startup() | |
| 90 { | |
| 91 $this->init(self::INIT_WITH_DB | self::INIT_WITH_PLUGINS); | |
| 92 | |
| 93 // set filename if not index.php | |
| 94 if (($basename = basename($_SERVER['SCRIPT_FILENAME'])) && $basename != 'index.php') { | |
| 95 $this->filename = $basename; | |
| 96 } | |
| 97 | |
| 98 // load all configured plugins | |
| 99 $plugins = (array) $this->config->get('plugins', array()); | |
| 100 $required_plugins = array('filesystem_attachments', 'jqueryui'); | |
| 101 $this->plugins->load_plugins($plugins, $required_plugins); | |
| 102 | |
| 103 // start session | |
| 104 $this->session_init(); | |
| 105 | |
| 106 // create user object | |
| 107 $this->set_user(new rcube_user($_SESSION['user_id'])); | |
| 108 | |
| 109 // set task and action properties | |
| 110 $this->set_task(rcube_utils::get_input_value('_task', rcube_utils::INPUT_GPC)); | |
| 111 $this->action = asciiwords(rcube_utils::get_input_value('_action', rcube_utils::INPUT_GPC)); | |
| 112 | |
| 113 // reset some session parameters when changing task | |
| 114 if ($this->task != 'utils') { | |
| 115 // we reset list page when switching to another task | |
| 116 // but only to the main task interface - empty action (#1489076, #1490116) | |
| 117 // this will prevent from unintentional page reset on cross-task requests | |
| 118 if ($this->session && $_SESSION['task'] != $this->task && empty($this->action)) { | |
| 119 $this->session->remove('page'); | |
| 120 | |
| 121 // set current task to session | |
| 122 $_SESSION['task'] = $this->task; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 // init output class (not in CLI mode) | |
| 127 if (!empty($_REQUEST['_remote'])) { | |
| 128 $GLOBALS['OUTPUT'] = $this->json_init(); | |
| 129 } | |
| 130 else if ($_SERVER['REMOTE_ADDR']) { | |
| 131 $GLOBALS['OUTPUT'] = $this->load_gui(!empty($_REQUEST['_framed'])); | |
| 132 } | |
| 133 | |
| 134 // run init method on all the plugins | |
| 135 $this->plugins->init($this, $this->task); | |
| 136 } | |
| 137 | |
| 138 /** | |
| 139 * Setter for application task | |
| 140 * | |
| 141 * @param string $task Task to set | |
| 142 */ | |
| 143 public function set_task($task) | |
| 144 { | |
| 145 if (php_sapi_name() == 'cli') { | |
| 146 $task = 'cli'; | |
| 147 } | |
| 148 else if (!$this->user || !$this->user->ID) { | |
| 149 $task = 'login'; | |
| 150 } | |
| 151 else { | |
| 152 $task = asciiwords($task, true) ?: 'mail'; | |
| 153 } | |
| 154 | |
| 155 $this->task = $task; | |
| 156 $this->comm_path = $this->url(array('task' => $this->task)); | |
| 157 | |
| 158 if (!empty($_REQUEST['_framed'])) { | |
| 159 $this->comm_path .= '&_framed=1'; | |
| 160 } | |
| 161 | |
| 162 if ($this->output) { | |
| 163 $this->output->set_env('task', $this->task); | |
| 164 $this->output->set_env('comm_path', $this->comm_path); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 /** | |
| 169 * Setter for system user object | |
| 170 * | |
| 171 * @param rcube_user $user Current user instance | |
| 172 */ | |
| 173 public function set_user($user) | |
| 174 { | |
| 175 parent::set_user($user); | |
| 176 | |
| 177 $lang = $this->language_prop($this->config->get('language', $_SESSION['language'])); | |
| 178 $_SESSION['language'] = $this->user->language = $lang; | |
| 179 | |
| 180 // set localization | |
| 181 setlocale(LC_ALL, $lang . '.utf8', $lang . '.UTF-8', 'en_US.utf8', 'en_US.UTF-8'); | |
| 182 | |
| 183 // Workaround for http://bugs.php.net/bug.php?id=18556 | |
| 184 // Also strtoupper/strtolower and other methods are locale-aware | |
| 185 // for these locales it is problematic (#1490519) | |
| 186 if (in_array($lang, array('tr_TR', 'ku', 'az_AZ'))) { | |
| 187 setlocale(LC_CTYPE, 'en_US.utf8', 'en_US.UTF-8', 'C'); | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 /** | |
| 192 * Return instance of the internal address book class | |
| 193 * | |
| 194 * @param string $id Address book identifier (-1 for default addressbook) | |
| 195 * @param boolean $writeable True if the address book needs to be writeable | |
| 196 * | |
| 197 * @return rcube_contacts Address book object | |
| 198 */ | |
| 199 public function get_address_book($id, $writeable = false) | |
| 200 { | |
| 201 $contacts = null; | |
| 202 $ldap_config = (array)$this->config->get('ldap_public'); | |
| 203 | |
| 204 // 'sql' is the alias for '0' used by autocomplete | |
| 205 if ($id == 'sql') | |
| 206 $id = '0'; | |
| 207 else if ($id == -1) { | |
| 208 $id = $this->config->get('default_addressbook'); | |
| 209 $default = true; | |
| 210 } | |
| 211 | |
| 212 // use existing instance | |
| 213 if (isset($this->address_books[$id]) && ($this->address_books[$id] instanceof rcube_addressbook)) { | |
| 214 $contacts = $this->address_books[$id]; | |
| 215 } | |
| 216 else if ($id && $ldap_config[$id]) { | |
| 217 $domain = $this->config->mail_domain($_SESSION['storage_host']); | |
| 218 $contacts = new rcube_ldap($ldap_config[$id], $this->config->get('ldap_debug'), $domain); | |
| 219 } | |
| 220 else if ($id === '0') { | |
| 221 $contacts = new rcube_contacts($this->db, $this->get_user_id()); | |
| 222 } | |
| 223 else { | |
| 224 $plugin = $this->plugins->exec_hook('addressbook_get', array('id' => $id, 'writeable' => $writeable)); | |
| 225 | |
| 226 // plugin returned instance of a rcube_addressbook | |
| 227 if ($plugin['instance'] instanceof rcube_addressbook) { | |
| 228 $contacts = $plugin['instance']; | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 // when user requested default writeable addressbook | |
| 233 // we need to check if default is writeable, if not we | |
| 234 // will return first writeable book (if any exist) | |
| 235 if ($contacts && $default && $contacts->readonly && $writeable) { | |
| 236 $contacts = null; | |
| 237 } | |
| 238 | |
| 239 // Get first addressbook from the list if configured default doesn't exist | |
| 240 // This can happen when user deleted the addressbook (e.g. Kolab folder) | |
| 241 if (!$contacts && (!$id || $default)) { | |
| 242 $source = reset($this->get_address_sources($writeable, !$default)); | |
| 243 if (!empty($source)) { | |
| 244 $contacts = $this->get_address_book($source['id']); | |
| 245 if ($contacts) { | |
| 246 $id = $source['id']; | |
| 247 } | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 if (!$contacts) { | |
| 252 // there's no default, just return | |
| 253 if ($default) { | |
| 254 return null; | |
| 255 } | |
| 256 | |
| 257 self::raise_error(array( | |
| 258 'code' => 700, | |
| 259 'file' => __FILE__, | |
| 260 'line' => __LINE__, | |
| 261 'message' => "Addressbook source ($id) not found!" | |
| 262 ), | |
| 263 true, true); | |
| 264 } | |
| 265 | |
| 266 // add to the 'books' array for shutdown function | |
| 267 $this->address_books[$id] = $contacts; | |
| 268 | |
| 269 if ($writeable && $contacts->readonly) { | |
| 270 return null; | |
| 271 } | |
| 272 | |
| 273 // set configured sort order | |
| 274 if ($sort_col = $this->config->get('addressbook_sort_col')) { | |
| 275 $contacts->set_sort_order($sort_col); | |
| 276 } | |
| 277 | |
| 278 return $contacts; | |
| 279 } | |
| 280 | |
| 281 /** | |
| 282 * Return identifier of the address book object | |
| 283 * | |
| 284 * @param rcube_addressbook $object Addressbook source object | |
| 285 * | |
| 286 * @return string Source identifier | |
| 287 */ | |
| 288 public function get_address_book_id($object) | |
| 289 { | |
| 290 foreach ($this->address_books as $index => $book) { | |
| 291 if ($book === $object) { | |
| 292 return $index; | |
| 293 } | |
| 294 } | |
| 295 } | |
| 296 | |
| 297 /** | |
| 298 * Return address books list | |
| 299 * | |
| 300 * @param boolean $writeable True if the address book needs to be writeable | |
| 301 * @param boolean $skip_hidden True if the address book needs to be not hidden | |
| 302 * | |
| 303 * @return array Address books array | |
| 304 */ | |
| 305 public function get_address_sources($writeable = false, $skip_hidden = false) | |
| 306 { | |
| 307 $abook_type = (string) $this->config->get('address_book_type'); | |
| 308 $ldap_config = (array) $this->config->get('ldap_public'); | |
| 309 $autocomplete = (array) $this->config->get('autocomplete_addressbooks'); | |
| 310 $list = array(); | |
| 311 | |
| 312 // We are using the DB address book or a plugin address book | |
| 313 if (!empty($abook_type) && strtolower($abook_type) != 'ldap') { | |
| 314 if (!isset($this->address_books['0'])) { | |
| 315 $this->address_books['0'] = new rcube_contacts($this->db, $this->get_user_id()); | |
| 316 } | |
| 317 | |
| 318 $list['0'] = array( | |
| 319 'id' => '0', | |
| 320 'name' => $this->gettext('personaladrbook'), | |
| 321 'groups' => $this->address_books['0']->groups, | |
| 322 'readonly' => $this->address_books['0']->readonly, | |
| 323 'undelete' => $this->address_books['0']->undelete && $this->config->get('undo_timeout'), | |
| 324 'autocomplete' => in_array('sql', $autocomplete), | |
| 325 ); | |
| 326 } | |
| 327 | |
| 328 if (!empty($ldap_config)) { | |
| 329 foreach ($ldap_config as $id => $prop) { | |
| 330 // handle misconfiguration | |
| 331 if (empty($prop) || !is_array($prop)) { | |
| 332 continue; | |
| 333 } | |
| 334 | |
| 335 $list[$id] = array( | |
| 336 'id' => $id, | |
| 337 'name' => html::quote($prop['name']), | |
| 338 'groups' => !empty($prop['groups']) || !empty($prop['group_filters']), | |
| 339 'readonly' => !$prop['writable'], | |
| 340 'hidden' => $prop['hidden'], | |
| 341 'autocomplete' => in_array($id, $autocomplete) | |
| 342 ); | |
| 343 } | |
| 344 } | |
| 345 | |
| 346 $plugin = $this->plugins->exec_hook('addressbooks_list', array('sources' => $list)); | |
| 347 $list = $plugin['sources']; | |
| 348 | |
| 349 foreach ($list as $idx => $item) { | |
| 350 // register source for shutdown function | |
| 351 if (!is_object($this->address_books[$item['id']])) { | |
| 352 $this->address_books[$item['id']] = $item; | |
| 353 } | |
| 354 // remove from list if not writeable as requested | |
| 355 if ($writeable && $item['readonly']) { | |
| 356 unset($list[$idx]); | |
| 357 } | |
| 358 // remove from list if hidden as requested | |
| 359 else if ($skip_hidden && $item['hidden']) { | |
| 360 unset($list[$idx]); | |
| 361 } | |
| 362 } | |
| 363 | |
| 364 return $list; | |
| 365 } | |
| 366 | |
| 367 /** | |
| 368 * Getter for compose responses. | |
| 369 * These are stored in local config and user preferences. | |
| 370 * | |
| 371 * @param boolean $sorted True to sort the list alphabetically | |
| 372 * @param boolean $user_only True if only this user's responses shall be listed | |
| 373 * | |
| 374 * @return array List of the current user's stored responses | |
| 375 */ | |
| 376 public function get_compose_responses($sorted = false, $user_only = false) | |
| 377 { | |
| 378 $responses = array(); | |
| 379 | |
| 380 if (!$user_only) { | |
| 381 foreach ($this->config->get('compose_responses_static', array()) as $response) { | |
| 382 if (empty($response['key'])) { | |
| 383 $response['key'] = substr(md5($response['name']), 0, 16); | |
| 384 } | |
| 385 | |
| 386 $response['static'] = true; | |
| 387 $response['class'] = 'readonly'; | |
| 388 | |
| 389 $k = $sorted ? '0000-' . mb_strtolower($response['name']) : $response['key']; | |
| 390 $responses[$k] = $response; | |
| 391 } | |
| 392 } | |
| 393 | |
| 394 foreach ($this->config->get('compose_responses', array()) as $response) { | |
| 395 if (empty($response['key'])) { | |
| 396 $response['key'] = substr(md5($response['name']), 0, 16); | |
| 397 } | |
| 398 | |
| 399 $k = $sorted ? mb_strtolower($response['name']) : $response['key']; | |
| 400 $responses[$k] = $response; | |
| 401 } | |
| 402 | |
| 403 // sort list by name | |
| 404 if ($sorted) { | |
| 405 ksort($responses, SORT_LOCALE_STRING); | |
| 406 } | |
| 407 | |
| 408 $responses = array_values($responses); | |
| 409 | |
| 410 $hook = $this->plugins->exec_hook('get_compose_responses', array( | |
| 411 'list' => $responses, | |
| 412 'sorted' => $sorted, | |
| 413 'user_only' => $user_only, | |
| 414 )); | |
| 415 | |
| 416 return $hook['list']; | |
| 417 } | |
| 418 | |
| 419 /** | |
| 420 * Init output object for GUI and add common scripts. | |
| 421 * This will instantiate a rcmail_output_html object and set | |
| 422 * environment vars according to the current session and configuration | |
| 423 * | |
| 424 * @param boolean $framed True if this request is loaded in a (i)frame | |
| 425 * | |
| 426 * @return rcube_output Reference to HTML output object | |
| 427 */ | |
| 428 public function load_gui($framed = false) | |
| 429 { | |
| 430 // init output page | |
| 431 if (!($this->output instanceof rcmail_output_html)) { | |
| 432 $this->output = new rcmail_output_html($this->task, $framed); | |
| 433 } | |
| 434 | |
| 435 // set refresh interval | |
| 436 $this->output->set_env('refresh_interval', $this->config->get('refresh_interval', 0)); | |
| 437 $this->output->set_env('session_lifetime', $this->config->get('session_lifetime', 0) * 60); | |
| 438 | |
| 439 if ($framed) { | |
| 440 $this->comm_path .= '&_framed=1'; | |
| 441 $this->output->set_env('framed', true); | |
| 442 } | |
| 443 | |
| 444 $this->output->set_env('task', $this->task); | |
| 445 $this->output->set_env('action', $this->action); | |
| 446 $this->output->set_env('comm_path', $this->comm_path); | |
| 447 $this->output->set_charset(RCUBE_CHARSET); | |
| 448 | |
| 449 if ($this->user && $this->user->ID) { | |
| 450 $this->output->set_env('user_id', $this->user->get_hash()); | |
| 451 } | |
| 452 | |
| 453 // set compose mode for all tasks (message compose step can be triggered from everywhere) | |
| 454 $this->output->set_env('compose_extwin', $this->config->get('compose_extwin',false)); | |
| 455 | |
| 456 // add some basic labels to client | |
| 457 $this->output->add_label('loading', 'servererror', 'connerror', 'requesttimedout', | |
| 458 'refreshing', 'windowopenerror', 'uploadingmany', 'close'); | |
| 459 | |
| 460 return $this->output; | |
| 461 } | |
| 462 | |
| 463 /** | |
| 464 * Create an output object for JSON responses | |
| 465 * | |
| 466 * @return rcube_output Reference to JSON output object | |
| 467 */ | |
| 468 public function json_init() | |
| 469 { | |
| 470 if (!($this->output instanceof rcmail_output_json)) { | |
| 471 $this->output = new rcmail_output_json($this->task); | |
| 472 } | |
| 473 | |
| 474 return $this->output; | |
| 475 } | |
| 476 | |
| 477 /** | |
| 478 * Create session object and start the session. | |
| 479 */ | |
| 480 public function session_init() | |
| 481 { | |
| 482 parent::session_init(); | |
| 483 | |
| 484 // set initial session vars | |
| 485 if (!$_SESSION['user_id']) { | |
| 486 $_SESSION['temp'] = true; | |
| 487 } | |
| 488 | |
| 489 // restore skin selection after logout | |
| 490 if ($_SESSION['temp'] && !empty($_SESSION['skin'])) { | |
| 491 $this->config->set('skin', $_SESSION['skin']); | |
| 492 } | |
| 493 } | |
| 494 | |
| 495 /** | |
| 496 * Perform login to the mail server and to the webmail service. | |
| 497 * This will also create a new user entry if auto_create_user is configured. | |
| 498 * | |
| 499 * @param string $username Mail storage (IMAP) user name | |
| 500 * @param string $password Mail storage (IMAP) password | |
| 501 * @param string $host Mail storage (IMAP) host | |
| 502 * @param bool $cookiecheck Enables cookie check | |
| 503 * | |
| 504 * @return boolean True on success, False on failure | |
| 505 */ | |
| 506 function login($username, $password, $host = null, $cookiecheck = false) | |
| 507 { | |
| 508 $this->login_error = null; | |
| 509 | |
| 510 if (empty($username)) { | |
| 511 return false; | |
| 512 } | |
| 513 | |
| 514 if ($cookiecheck && empty($_COOKIE)) { | |
| 515 $this->login_error = self::ERROR_COOKIES_DISABLED; | |
| 516 return false; | |
| 517 } | |
| 518 | |
| 519 $username_filter = $this->config->get('login_username_filter'); | |
| 520 $username_maxlen = $this->config->get('login_username_maxlen', 1024); | |
| 521 $password_maxlen = $this->config->get('login_password_maxlen', 1024); | |
| 522 $default_host = $this->config->get('default_host'); | |
| 523 $default_port = $this->config->get('default_port'); | |
| 524 $username_domain = $this->config->get('username_domain'); | |
| 525 $login_lc = $this->config->get('login_lc', 2); | |
| 526 | |
| 527 // check input for security (#1490500) | |
| 528 if (($username_maxlen && strlen($username) > $username_maxlen) | |
| 529 || ($username_filter && !preg_match($username_filter, $username)) | |
| 530 || ($password_maxlen && strlen($password) > $password_maxlen) | |
| 531 ) { | |
| 532 $this->login_error = self::ERROR_INVALID_REQUEST; | |
| 533 return false; | |
| 534 } | |
| 535 | |
| 536 // host is validated in rcmail::autoselect_host(), so here | |
| 537 // we'll only handle unset host (if possible) | |
| 538 if (!$host && !empty($default_host)) { | |
| 539 if (is_array($default_host)) { | |
| 540 $key = key($default_host); | |
| 541 $host = is_numeric($key) ? $default_host[$key] : $key; | |
| 542 } | |
| 543 else { | |
| 544 $host = $default_host; | |
| 545 } | |
| 546 | |
| 547 $host = rcube_utils::parse_host($host); | |
| 548 } | |
| 549 | |
| 550 if (!$host) { | |
| 551 $this->login_error = self::ERROR_INVALID_HOST; | |
| 552 return false; | |
| 553 } | |
| 554 | |
| 555 // parse $host URL | |
| 556 $a_host = parse_url($host); | |
| 557 if ($a_host['host']) { | |
| 558 $host = $a_host['host']; | |
| 559 $ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? $a_host['scheme'] : null; | |
| 560 | |
| 561 if (!empty($a_host['port'])) | |
| 562 $port = $a_host['port']; | |
| 563 else if ($ssl && $ssl != 'tls' && (!$default_port || $default_port == 143)) | |
| 564 $port = 993; | |
| 565 } | |
| 566 | |
| 567 if (!$port) { | |
| 568 $port = $default_port; | |
| 569 } | |
| 570 | |
| 571 // Check if we need to add/force domain to username | |
| 572 if (!empty($username_domain)) { | |
| 573 $domain = is_array($username_domain) ? $username_domain[$host] : $username_domain; | |
| 574 | |
| 575 if ($domain = rcube_utils::parse_host((string)$domain, $host)) { | |
| 576 $pos = strpos($username, '@'); | |
| 577 | |
| 578 // force configured domains | |
| 579 if ($pos !== false && $this->config->get('username_domain_forced')) { | |
| 580 $username = substr($username, 0, $pos) . '@' . $domain; | |
| 581 } | |
| 582 // just add domain if not specified | |
| 583 else if ($pos === false) { | |
| 584 $username .= '@' . $domain; | |
| 585 } | |
| 586 } | |
| 587 } | |
| 588 | |
| 589 // Convert username to lowercase. If storage backend | |
| 590 // is case-insensitive we need to store always the same username (#1487113) | |
| 591 if ($login_lc) { | |
| 592 if ($login_lc == 2 || $login_lc === true) { | |
| 593 $username = mb_strtolower($username); | |
| 594 } | |
| 595 else if (strpos($username, '@')) { | |
| 596 // lowercase domain name | |
| 597 list($local, $domain) = explode('@', $username); | |
| 598 $username = $local . '@' . mb_strtolower($domain); | |
| 599 } | |
| 600 } | |
| 601 | |
| 602 // try to resolve email address from virtuser table | |
| 603 if (strpos($username, '@') && ($virtuser = rcube_user::email2user($username))) { | |
| 604 $username = $virtuser; | |
| 605 } | |
| 606 | |
| 607 // Here we need IDNA ASCII | |
| 608 // Only rcube_contacts class is using domain names in Unicode | |
| 609 $host = rcube_utils::idn_to_ascii($host); | |
| 610 $username = rcube_utils::idn_to_ascii($username); | |
| 611 | |
| 612 // user already registered -> overwrite username | |
| 613 if ($user = rcube_user::query($username, $host)) { | |
| 614 $username = $user->data['username']; | |
| 615 | |
| 616 // Brute-force prevention | |
| 617 if ($user->is_locked()) { | |
| 618 $this->login_error = self::ERROR_RATE_LIMIT; | |
| 619 return false; | |
| 620 } | |
| 621 } | |
| 622 | |
| 623 $storage = $this->get_storage(); | |
| 624 | |
| 625 // try to log in | |
| 626 if (!$storage->connect($host, $username, $password, $port, $ssl)) { | |
| 627 if ($user) { | |
| 628 $user->failed_login(); | |
| 629 } | |
| 630 | |
| 631 // Wait a second to slow down brute-force attacks (#1490549) | |
| 632 sleep(1); | |
| 633 return false; | |
| 634 } | |
| 635 | |
| 636 // user already registered -> update user's record | |
| 637 if (is_object($user)) { | |
| 638 // update last login timestamp | |
| 639 $user->touch(); | |
| 640 } | |
| 641 // create new system user | |
| 642 else if ($this->config->get('auto_create_user')) { | |
| 643 if ($created = rcube_user::create($username, $host)) { | |
| 644 $user = $created; | |
| 645 } | |
| 646 else { | |
| 647 self::raise_error(array( | |
| 648 'code' => 620, | |
| 649 'file' => __FILE__, | |
| 650 'line' => __LINE__, | |
| 651 'message' => "Failed to create a user record. Maybe aborted by a plugin?" | |
| 652 ), | |
| 653 true, false); | |
| 654 } | |
| 655 } | |
| 656 else { | |
| 657 self::raise_error(array( | |
| 658 'code' => 621, | |
| 659 'file' => __FILE__, | |
| 660 'line' => __LINE__, | |
| 661 'message' => "Access denied for new user $username. 'auto_create_user' is disabled" | |
| 662 ), | |
| 663 true, false); | |
| 664 } | |
| 665 | |
| 666 // login succeeded | |
| 667 if (is_object($user) && $user->ID) { | |
| 668 // Configure environment | |
| 669 $this->set_user($user); | |
| 670 $this->set_storage_prop(); | |
| 671 | |
| 672 // set session vars | |
| 673 $_SESSION['user_id'] = $user->ID; | |
| 674 $_SESSION['username'] = $user->data['username']; | |
| 675 $_SESSION['storage_host'] = $host; | |
| 676 $_SESSION['storage_port'] = $port; | |
| 677 $_SESSION['storage_ssl'] = $ssl; | |
| 678 $_SESSION['password'] = $this->encrypt($password); | |
| 679 $_SESSION['login_time'] = time(); | |
| 680 | |
| 681 $timezone = rcube_utils::get_input_value('_timezone', rcube_utils::INPUT_GPC); | |
| 682 if ($timezone && is_string($timezone) && $timezone != '_default_') { | |
| 683 $_SESSION['timezone'] = $timezone; | |
| 684 } | |
| 685 | |
| 686 // fix some old settings according to namespace prefix | |
| 687 $this->fix_namespace_settings($user); | |
| 688 | |
| 689 // set/create special folders | |
| 690 $this->set_special_folders(); | |
| 691 | |
| 692 // clear all mailboxes related cache(s) | |
| 693 $storage->clear_cache('mailboxes', true); | |
| 694 | |
| 695 return true; | |
| 696 } | |
| 697 | |
| 698 return false; | |
| 699 } | |
| 700 | |
| 701 /** | |
| 702 * Returns error code of last login operation | |
| 703 * | |
| 704 * @return int Error code | |
| 705 */ | |
| 706 public function login_error() | |
| 707 { | |
| 708 if ($this->login_error) { | |
| 709 return $this->login_error; | |
| 710 } | |
| 711 | |
| 712 if ($this->storage && $this->storage->get_error_code() < -1) { | |
| 713 return self::ERROR_STORAGE; | |
| 714 } | |
| 715 } | |
| 716 | |
| 717 /** | |
| 718 * Auto-select IMAP host based on the posted login information | |
| 719 * | |
| 720 * @return string Selected IMAP host | |
| 721 */ | |
| 722 public function autoselect_host() | |
| 723 { | |
| 724 $default_host = $this->config->get('default_host'); | |
| 725 $host = null; | |
| 726 | |
| 727 if (is_array($default_host)) { | |
| 728 $post_host = rcube_utils::get_input_value('_host', rcube_utils::INPUT_POST); | |
| 729 $post_user = rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST); | |
| 730 | |
| 731 list(, $domain) = explode('@', $post_user); | |
| 732 | |
| 733 // direct match in default_host array | |
| 734 if ($default_host[$post_host] || in_array($post_host, array_values($default_host))) { | |
| 735 $host = $post_host; | |
| 736 } | |
| 737 // try to select host by mail domain | |
| 738 else if (!empty($domain)) { | |
| 739 foreach ($default_host as $storage_host => $mail_domains) { | |
| 740 if (is_array($mail_domains) && in_array_nocase($domain, $mail_domains)) { | |
| 741 $host = $storage_host; | |
| 742 break; | |
| 743 } | |
| 744 else if (stripos($storage_host, $domain) !== false || stripos(strval($mail_domains), $domain) !== false) { | |
| 745 $host = is_numeric($storage_host) ? $mail_domains : $storage_host; | |
| 746 break; | |
| 747 } | |
| 748 } | |
| 749 } | |
| 750 | |
| 751 // take the first entry if $host is still not set | |
| 752 if (empty($host)) { | |
| 753 $key = key($default_host); | |
| 754 $host = is_numeric($key) ? $default_host[$key] : $key; | |
| 755 } | |
| 756 } | |
| 757 else if (empty($default_host)) { | |
| 758 $host = rcube_utils::get_input_value('_host', rcube_utils::INPUT_POST); | |
| 759 } | |
| 760 else { | |
| 761 $host = rcube_utils::parse_host($default_host); | |
| 762 } | |
| 763 | |
| 764 return $host; | |
| 765 } | |
| 766 | |
| 767 /** | |
| 768 * Destroy session data and remove cookie | |
| 769 */ | |
| 770 public function kill_session() | |
| 771 { | |
| 772 $this->plugins->exec_hook('session_destroy'); | |
| 773 | |
| 774 $this->session->kill(); | |
| 775 $_SESSION = array('language' => $this->user->language, 'temp' => true, 'skin' => $this->config->get('skin')); | |
| 776 $this->user->reset(); | |
| 777 } | |
| 778 | |
| 779 /** | |
| 780 * Do server side actions on logout | |
| 781 */ | |
| 782 public function logout_actions() | |
| 783 { | |
| 784 $storage = $this->get_storage(); | |
| 785 $logout_expunge = $this->config->get('logout_expunge'); | |
| 786 $logout_purge = $this->config->get('logout_purge'); | |
| 787 $trash_mbox = $this->config->get('trash_mbox'); | |
| 788 | |
| 789 if ($logout_purge && !empty($trash_mbox)) { | |
| 790 $storage->clear_folder($trash_mbox); | |
| 791 } | |
| 792 | |
| 793 if ($logout_expunge) { | |
| 794 $storage->expunge_folder('INBOX'); | |
| 795 } | |
| 796 | |
| 797 // Try to save unsaved user preferences | |
| 798 if (!empty($_SESSION['preferences'])) { | |
| 799 $this->user->save_prefs(unserialize($_SESSION['preferences'])); | |
| 800 } | |
| 801 } | |
| 802 | |
| 803 /** | |
| 804 * Build a valid URL to this instance of Roundcube | |
| 805 * | |
| 806 * @param mixed $p Either a string with the action or | |
| 807 * url parameters as key-value pairs | |
| 808 * @param boolean $absolute Build an URL absolute to document root | |
| 809 * @param boolean $full Create fully qualified URL including http(s):// and hostname | |
| 810 * @param bool $secure Return absolute URL in secure location | |
| 811 * | |
| 812 * @return string Valid application URL | |
| 813 */ | |
| 814 public function url($p, $absolute = false, $full = false, $secure = false) | |
| 815 { | |
| 816 if (!is_array($p)) { | |
| 817 if (strpos($p, 'http') === 0) { | |
| 818 return $p; | |
| 819 } | |
| 820 | |
| 821 $p = array('_action' => @func_get_arg(0)); | |
| 822 } | |
| 823 | |
| 824 $pre = array(); | |
| 825 $task = $p['_task'] ?: ($p['task'] ?: $this->task); | |
| 826 $pre['_task'] = $task; | |
| 827 unset($p['task'], $p['_task']); | |
| 828 | |
| 829 $url = $this->filename; | |
| 830 $delm = '?'; | |
| 831 | |
| 832 foreach (array_merge($pre, $p) as $key => $val) { | |
| 833 if ($val !== '' && $val !== null) { | |
| 834 $par = $key[0] == '_' ? $key : '_'.$key; | |
| 835 $url .= $delm.urlencode($par).'='.urlencode($val); | |
| 836 $delm = '&'; | |
| 837 } | |
| 838 } | |
| 839 | |
| 840 $base_path = strval($_SERVER['REDIRECT_SCRIPT_URL'] ?: $_SERVER['SCRIPT_NAME']); | |
| 841 $base_path = preg_replace('![^/]+$!', '', $base_path); | |
| 842 | |
| 843 if ($secure && ($token = $this->get_secure_url_token(true))) { | |
| 844 // add token to the url | |
| 845 $url = $token . '/' . $url; | |
| 846 | |
| 847 // remove old token from the path | |
| 848 $base_path = rtrim($base_path, '/'); | |
| 849 $base_path = preg_replace('/\/[a-zA-Z0-9]{' . strlen($token) . '}$/', '', $base_path); | |
| 850 | |
| 851 // this need to be full url to make redirects work | |
| 852 $absolute = true; | |
| 853 } | |
| 854 else if ($secure && ($token = $this->get_request_token())) | |
| 855 $url .= $delm . '_token=' . urlencode($token); | |
| 856 | |
| 857 if ($absolute || $full) { | |
| 858 // add base path to this Roundcube installation | |
| 859 if ($base_path == '') $base_path = '/'; | |
| 860 $prefix = $base_path; | |
| 861 | |
| 862 // prepend protocol://hostname:port | |
| 863 if ($full) { | |
| 864 $prefix = rcube_utils::resolve_url($prefix); | |
| 865 } | |
| 866 | |
| 867 $prefix = rtrim($prefix, '/') . '/'; | |
| 868 } | |
| 869 else { | |
| 870 $prefix = './'; | |
| 871 } | |
| 872 | |
| 873 return $prefix . $url; | |
| 874 } | |
| 875 | |
| 876 /** | |
| 877 * Function to be executed in script shutdown | |
| 878 */ | |
| 879 public function shutdown() | |
| 880 { | |
| 881 parent::shutdown(); | |
| 882 | |
| 883 foreach ($this->address_books as $book) { | |
| 884 if (is_object($book) && is_a($book, 'rcube_addressbook')) { | |
| 885 $book->close(); | |
| 886 } | |
| 887 } | |
| 888 | |
| 889 // write performance stats to logs/console | |
| 890 if ($this->config->get('devel_mode') || $this->config->get('performance_stats')) { | |
| 891 // make sure logged numbers use unified format | |
| 892 setlocale(LC_NUMERIC, 'en_US.utf8', 'en_US.UTF-8', 'en_US', 'C'); | |
| 893 | |
| 894 if (function_exists('memory_get_usage')) { | |
| 895 $mem = $this->show_bytes(memory_get_usage()); | |
| 896 } | |
| 897 if (function_exists('memory_get_peak_usage')) { | |
| 898 $mem .= '/'.$this->show_bytes(memory_get_peak_usage()); | |
| 899 } | |
| 900 | |
| 901 $log = $this->task . ($this->action ? '/'.$this->action : '') . ($mem ? " [$mem]" : ''); | |
| 902 | |
| 903 if (defined('RCMAIL_START')) { | |
| 904 self::print_timer(RCMAIL_START, $log); | |
| 905 } | |
| 906 else { | |
| 907 self::console($log); | |
| 908 } | |
| 909 } | |
| 910 } | |
| 911 | |
| 912 /** | |
| 913 * CSRF attack prevention code. Raises error when check fails. | |
| 914 * | |
| 915 * @param int $mode Request mode | |
| 916 */ | |
| 917 public function request_security_check($mode = rcube_utils::INPUT_POST) | |
| 918 { | |
| 919 // check request token | |
| 920 if (!$this->check_request($mode)) { | |
| 921 $error = array('code' => 403, 'message' => "Request security check failed"); | |
| 922 self::raise_error($error, false, true); | |
| 923 } | |
| 924 | |
| 925 // check referer if configured | |
| 926 if ($this->config->get('referer_check') && !rcube_utils::check_referer()) { | |
| 927 $error = array('code' => 403, 'message' => "Referer check failed"); | |
| 928 self::raise_error($error, true, true); | |
| 929 } | |
| 930 } | |
| 931 | |
| 932 /** | |
| 933 * Registers action aliases for current task | |
| 934 * | |
| 935 * @param array $map Alias-to-filename hash array | |
| 936 */ | |
| 937 public function register_action_map($map) | |
| 938 { | |
| 939 if (is_array($map)) { | |
| 940 foreach ($map as $idx => $val) { | |
| 941 $this->action_map[$idx] = $val; | |
| 942 } | |
| 943 } | |
| 944 } | |
| 945 | |
| 946 /** | |
| 947 * Returns current action filename | |
| 948 * | |
| 949 * @param array $map Alias-to-filename hash array | |
| 950 */ | |
| 951 public function get_action_file() | |
| 952 { | |
| 953 if (!empty($this->action_map[$this->action])) { | |
| 954 return $this->action_map[$this->action]; | |
| 955 } | |
| 956 | |
| 957 return strtr($this->action, '-', '_') . '.inc'; | |
| 958 } | |
| 959 | |
| 960 /** | |
| 961 * Fixes some user preferences according to namespace handling change. | |
| 962 * Old Roundcube versions were using folder names with removed namespace prefix. | |
| 963 * Now we need to add the prefix on servers where personal namespace has prefix. | |
| 964 * | |
| 965 * @param rcube_user $user User object | |
| 966 */ | |
| 967 private function fix_namespace_settings($user) | |
| 968 { | |
| 969 $prefix = $this->storage->get_namespace('prefix'); | |
| 970 $prefix_len = strlen($prefix); | |
| 971 | |
| 972 if (!$prefix_len) { | |
| 973 return; | |
| 974 } | |
| 975 | |
| 976 if ($this->config->get('namespace_fixed')) { | |
| 977 return; | |
| 978 } | |
| 979 | |
| 980 $prefs = array(); | |
| 981 | |
| 982 // Build namespace prefix regexp | |
| 983 $ns = $this->storage->get_namespace(); | |
| 984 $regexp = array(); | |
| 985 | |
| 986 foreach ($ns as $entry) { | |
| 987 if (!empty($entry)) { | |
| 988 foreach ($entry as $item) { | |
| 989 if (strlen($item[0])) { | |
| 990 $regexp[] = preg_quote($item[0], '/'); | |
| 991 } | |
| 992 } | |
| 993 } | |
| 994 } | |
| 995 $regexp = '/^('. implode('|', $regexp).')/'; | |
| 996 | |
| 997 // Fix preferences | |
| 998 $opts = array('drafts_mbox', 'junk_mbox', 'sent_mbox', 'trash_mbox', 'archive_mbox'); | |
| 999 foreach ($opts as $opt) { | |
| 1000 if ($value = $this->config->get($opt)) { | |
| 1001 if ($value != 'INBOX' && !preg_match($regexp, $value)) { | |
| 1002 $prefs[$opt] = $prefix.$value; | |
| 1003 } | |
| 1004 } | |
| 1005 } | |
| 1006 | |
| 1007 if (($search_mods = $this->config->get('search_mods')) && !empty($search_mods)) { | |
| 1008 $folders = array(); | |
| 1009 foreach ($search_mods as $idx => $value) { | |
| 1010 if ($idx != 'INBOX' && $idx != '*' && !preg_match($regexp, $idx)) { | |
| 1011 $idx = $prefix.$idx; | |
| 1012 } | |
| 1013 $folders[$idx] = $value; | |
| 1014 } | |
| 1015 | |
| 1016 $prefs['search_mods'] = $folders; | |
| 1017 } | |
| 1018 | |
| 1019 if (($threading = $this->config->get('message_threading')) && !empty($threading)) { | |
| 1020 $folders = array(); | |
| 1021 foreach ($threading as $idx => $value) { | |
| 1022 if ($idx != 'INBOX' && !preg_match($regexp, $idx)) { | |
| 1023 $idx = $prefix.$idx; | |
| 1024 } | |
| 1025 $folders[$prefix.$idx] = $value; | |
| 1026 } | |
| 1027 | |
| 1028 $prefs['message_threading'] = $folders; | |
| 1029 } | |
| 1030 | |
| 1031 if ($collapsed = $this->config->get('collapsed_folders')) { | |
| 1032 $folders = explode('&&', $collapsed); | |
| 1033 $count = count($folders); | |
| 1034 $folders_str = ''; | |
| 1035 | |
| 1036 if ($count) { | |
| 1037 $folders[0] = substr($folders[0], 1); | |
| 1038 $folders[$count-1] = substr($folders[$count-1], 0, -1); | |
| 1039 } | |
| 1040 | |
| 1041 foreach ($folders as $value) { | |
| 1042 if ($value != 'INBOX' && !preg_match($regexp, $value)) { | |
| 1043 $value = $prefix.$value; | |
| 1044 } | |
| 1045 $folders_str .= '&'.$value.'&'; | |
| 1046 } | |
| 1047 | |
| 1048 $prefs['collapsed_folders'] = $folders_str; | |
| 1049 } | |
| 1050 | |
| 1051 $prefs['namespace_fixed'] = true; | |
| 1052 | |
| 1053 // save updated preferences and reset imap settings (default folders) | |
| 1054 $user->save_prefs($prefs); | |
| 1055 $this->set_storage_prop(); | |
| 1056 } | |
| 1057 | |
| 1058 /** | |
| 1059 * Overwrite action variable | |
| 1060 * | |
| 1061 * @param string $action New action value | |
| 1062 */ | |
| 1063 public function overwrite_action($action) | |
| 1064 { | |
| 1065 $this->action = $action; | |
| 1066 $this->output->set_env('action', $action); | |
| 1067 } | |
| 1068 | |
| 1069 /** | |
| 1070 * Set environment variables for specified config options | |
| 1071 * | |
| 1072 * @param array $options List of configuration option names | |
| 1073 */ | |
| 1074 public function set_env_config($options) | |
| 1075 { | |
| 1076 foreach ((array) $options as $option) { | |
| 1077 if ($this->config->get($option)) { | |
| 1078 $this->output->set_env($option, true); | |
| 1079 } | |
| 1080 } | |
| 1081 } | |
| 1082 | |
| 1083 /** | |
| 1084 * Returns RFC2822 formatted current date in user's timezone | |
| 1085 * | |
| 1086 * @return string Date | |
| 1087 */ | |
| 1088 public function user_date() | |
| 1089 { | |
| 1090 // get user's timezone | |
| 1091 try { | |
| 1092 $tz = new DateTimeZone($this->config->get('timezone')); | |
| 1093 $date = new DateTime('now', $tz); | |
| 1094 } | |
| 1095 catch (Exception $e) { | |
| 1096 $date = new DateTime(); | |
| 1097 } | |
| 1098 | |
| 1099 return $date->format('r'); | |
| 1100 } | |
| 1101 | |
| 1102 /** | |
| 1103 * Write login data (name, ID, IP address) to the 'userlogins' log file. | |
| 1104 */ | |
| 1105 public function log_login($user = null, $failed_login = false, $error_code = 0) | |
| 1106 { | |
| 1107 if (!$this->config->get('log_logins')) { | |
| 1108 return; | |
| 1109 } | |
| 1110 | |
| 1111 // failed login | |
| 1112 if ($failed_login) { | |
| 1113 // don't fill the log with complete input, which could | |
| 1114 // have been prepared by a hacker | |
| 1115 if (strlen($user) > 256) { | |
| 1116 $user = substr($user, 0, 256) . '...'; | |
| 1117 } | |
| 1118 | |
| 1119 $message = sprintf('Failed login for %s from %s in session %s (error: %d)', | |
| 1120 $user, rcube_utils::remote_ip(), session_id(), $error_code); | |
| 1121 } | |
| 1122 // successful login | |
| 1123 else { | |
| 1124 $user_name = $this->get_user_name(); | |
| 1125 $user_id = $this->get_user_id(); | |
| 1126 | |
| 1127 if (!$user_id) { | |
| 1128 return; | |
| 1129 } | |
| 1130 | |
| 1131 $message = sprintf('Successful login for %s (ID: %d) from %s in session %s', | |
| 1132 $user_name, $user_id, rcube_utils::remote_ip(), session_id()); | |
| 1133 } | |
| 1134 | |
| 1135 // log login | |
| 1136 self::write_log('userlogins', $message); | |
| 1137 } | |
| 1138 | |
| 1139 /** | |
| 1140 * Create a HTML table based on the given data | |
| 1141 * | |
| 1142 * @param array $attrib Named table attributes | |
| 1143 * @param mixed $table_data Table row data. Either a two-dimensional array | |
| 1144 * or a valid SQL result set | |
| 1145 * @param array $show_cols List of cols to show | |
| 1146 * @param string $id_col Name of the identifier col | |
| 1147 * | |
| 1148 * @return string HTML table code | |
| 1149 */ | |
| 1150 public function table_output($attrib, $table_data, $show_cols, $id_col) | |
| 1151 { | |
| 1152 $table = new html_table($attrib); | |
| 1153 | |
| 1154 // add table header | |
| 1155 if (!$attrib['noheader']) { | |
| 1156 foreach ($show_cols as $col) { | |
| 1157 $table->add_header($col, $this->Q($this->gettext($col))); | |
| 1158 } | |
| 1159 } | |
| 1160 | |
| 1161 if (!is_array($table_data)) { | |
| 1162 $db = $this->get_dbh(); | |
| 1163 while ($table_data && ($sql_arr = $db->fetch_assoc($table_data))) { | |
| 1164 $table->add_row(array('id' => 'rcmrow' . rcube_utils::html_identifier($sql_arr[$id_col]))); | |
| 1165 | |
| 1166 // format each col | |
| 1167 foreach ($show_cols as $col) { | |
| 1168 $table->add($col, $this->Q($sql_arr[$col])); | |
| 1169 } | |
| 1170 } | |
| 1171 } | |
| 1172 else { | |
| 1173 foreach ($table_data as $row_data) { | |
| 1174 $class = !empty($row_data['class']) ? $row_data['class'] : null; | |
| 1175 if (!empty($attrib['rowclass'])) | |
| 1176 $class = trim($class . ' ' . $attrib['rowclass']); | |
| 1177 $rowid = 'rcmrow' . rcube_utils::html_identifier($row_data[$id_col]); | |
| 1178 | |
| 1179 $table->add_row(array('id' => $rowid, 'class' => $class)); | |
| 1180 | |
| 1181 // format each col | |
| 1182 foreach ($show_cols as $col) { | |
| 1183 $val = is_array($row_data[$col]) ? $row_data[$col][0] : $row_data[$col]; | |
| 1184 $table->add($col, empty($attrib['ishtml']) ? $this->Q($val) : $val); | |
| 1185 } | |
| 1186 } | |
| 1187 } | |
| 1188 | |
| 1189 return $table->show($attrib); | |
| 1190 } | |
| 1191 | |
| 1192 /** | |
| 1193 * Convert the given date to a human readable form | |
| 1194 * This uses the date formatting properties from config | |
| 1195 * | |
| 1196 * @param mixed $date Date representation (string, timestamp or DateTime object) | |
| 1197 * @param string $format Date format to use | |
| 1198 * @param bool $convert Enables date conversion according to user timezone | |
| 1199 * | |
| 1200 * @return string Formatted date string | |
| 1201 */ | |
| 1202 public function format_date($date, $format = null, $convert = true) | |
| 1203 { | |
| 1204 if (is_object($date) && is_a($date, 'DateTime')) { | |
| 1205 $timestamp = $date->format('U'); | |
| 1206 } | |
| 1207 else { | |
| 1208 if (!empty($date)) { | |
| 1209 $timestamp = rcube_utils::strtotime($date); | |
| 1210 } | |
| 1211 | |
| 1212 if (empty($timestamp)) { | |
| 1213 return ''; | |
| 1214 } | |
| 1215 | |
| 1216 try { | |
| 1217 $date = new DateTime("@".$timestamp); | |
| 1218 } | |
| 1219 catch (Exception $e) { | |
| 1220 return ''; | |
| 1221 } | |
| 1222 } | |
| 1223 | |
| 1224 if ($convert) { | |
| 1225 try { | |
| 1226 // convert to the right timezone | |
| 1227 $stz = date_default_timezone_get(); | |
| 1228 $tz = new DateTimeZone($this->config->get('timezone')); | |
| 1229 $date->setTimezone($tz); | |
| 1230 date_default_timezone_set($tz->getName()); | |
| 1231 | |
| 1232 $timestamp = $date->format('U'); | |
| 1233 } | |
| 1234 catch (Exception $e) { | |
| 1235 } | |
| 1236 } | |
| 1237 | |
| 1238 // define date format depending on current time | |
| 1239 if (!$format) { | |
| 1240 $now = time(); | |
| 1241 $now_date = getdate($now); | |
| 1242 $today_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday'], $now_date['year']); | |
| 1243 $week_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday']-6, $now_date['year']); | |
| 1244 $pretty_date = $this->config->get('prettydate'); | |
| 1245 | |
| 1246 if ($pretty_date && $timestamp > $today_limit && $timestamp <= $now) { | |
| 1247 $format = $this->config->get('date_today', $this->config->get('time_format', 'H:i')); | |
| 1248 $today = true; | |
| 1249 } | |
| 1250 else if ($pretty_date && $timestamp > $week_limit && $timestamp <= $now) { | |
| 1251 $format = $this->config->get('date_short', 'D H:i'); | |
| 1252 } | |
| 1253 else { | |
| 1254 $format = $this->config->get('date_long', 'Y-m-d H:i'); | |
| 1255 } | |
| 1256 } | |
| 1257 | |
| 1258 // strftime() format | |
| 1259 if (preg_match('/%[a-z]+/i', $format)) { | |
| 1260 $format = strftime($format, $timestamp); | |
| 1261 if ($stz) { | |
| 1262 date_default_timezone_set($stz); | |
| 1263 } | |
| 1264 return $today ? ($this->gettext('today') . ' ' . $format) : $format; | |
| 1265 } | |
| 1266 | |
| 1267 // parse format string manually in order to provide localized weekday and month names | |
| 1268 // an alternative would be to convert the date() format string to fit with strftime() | |
| 1269 $out = ''; | |
| 1270 for ($i=0; $i<strlen($format); $i++) { | |
| 1271 if ($format[$i] == "\\") { // skip escape chars | |
| 1272 continue; | |
| 1273 } | |
| 1274 | |
| 1275 // write char "as-is" | |
| 1276 if ($format[$i] == ' ' || $format[$i-1] == "\\") { | |
| 1277 $out .= $format[$i]; | |
| 1278 } | |
| 1279 // weekday (short) | |
| 1280 else if ($format[$i] == 'D') { | |
| 1281 $out .= $this->gettext(strtolower(date('D', $timestamp))); | |
| 1282 } | |
| 1283 // weekday long | |
| 1284 else if ($format[$i] == 'l') { | |
| 1285 $out .= $this->gettext(strtolower(date('l', $timestamp))); | |
| 1286 } | |
| 1287 // month name (short) | |
| 1288 else if ($format[$i] == 'M') { | |
| 1289 $out .= $this->gettext(strtolower(date('M', $timestamp))); | |
| 1290 } | |
| 1291 // month name (long) | |
| 1292 else if ($format[$i] == 'F') { | |
| 1293 $out .= $this->gettext('long'.strtolower(date('M', $timestamp))); | |
| 1294 } | |
| 1295 else if ($format[$i] == 'x') { | |
| 1296 $out .= strftime('%x %X', $timestamp); | |
| 1297 } | |
| 1298 else { | |
| 1299 $out .= date($format[$i], $timestamp); | |
| 1300 } | |
| 1301 } | |
| 1302 | |
| 1303 if ($today) { | |
| 1304 $label = $this->gettext('today'); | |
| 1305 // replcae $ character with "Today" label (#1486120) | |
| 1306 if (strpos($out, '$') !== false) { | |
| 1307 $out = preg_replace('/\$/', $label, $out, 1); | |
| 1308 } | |
| 1309 else { | |
| 1310 $out = $label . ' ' . $out; | |
| 1311 } | |
| 1312 } | |
| 1313 | |
| 1314 if ($stz) { | |
| 1315 date_default_timezone_set($stz); | |
| 1316 } | |
| 1317 | |
| 1318 return $out; | |
| 1319 } | |
| 1320 | |
| 1321 /** | |
| 1322 * Return folders list in HTML | |
| 1323 * | |
| 1324 * @param array $attrib Named parameters | |
| 1325 * | |
| 1326 * @return string HTML code for the gui object | |
| 1327 */ | |
| 1328 public function folder_list($attrib) | |
| 1329 { | |
| 1330 static $a_mailboxes; | |
| 1331 | |
| 1332 $attrib += array('maxlength' => 100, 'realnames' => false, 'unreadwrap' => ' (%s)'); | |
| 1333 | |
| 1334 $type = $attrib['type'] ? $attrib['type'] : 'ul'; | |
| 1335 unset($attrib['type']); | |
| 1336 | |
| 1337 if ($type == 'ul' && !$attrib['id']) { | |
| 1338 $attrib['id'] = 'rcmboxlist'; | |
| 1339 } | |
| 1340 | |
| 1341 if (empty($attrib['folder_name'])) { | |
| 1342 $attrib['folder_name'] = '*'; | |
| 1343 } | |
| 1344 | |
| 1345 // get current folder | |
| 1346 $storage = $this->get_storage(); | |
| 1347 $mbox_name = $storage->get_folder(); | |
| 1348 | |
| 1349 // build the folders tree | |
| 1350 if (empty($a_mailboxes)) { | |
| 1351 // get mailbox list | |
| 1352 $a_folders = $storage->list_folders_subscribed( | |
| 1353 '', $attrib['folder_name'], $attrib['folder_filter']); | |
| 1354 $delimiter = $storage->get_hierarchy_delimiter(); | |
| 1355 $a_mailboxes = array(); | |
| 1356 | |
| 1357 foreach ($a_folders as $folder) { | |
| 1358 $this->build_folder_tree($a_mailboxes, $folder, $delimiter); | |
| 1359 } | |
| 1360 } | |
| 1361 | |
| 1362 // allow plugins to alter the folder tree or to localize folder names | |
| 1363 $hook = $this->plugins->exec_hook('render_mailboxlist', array( | |
| 1364 'list' => $a_mailboxes, | |
| 1365 'delimiter' => $delimiter, | |
| 1366 'type' => $type, | |
| 1367 'attribs' => $attrib, | |
| 1368 )); | |
| 1369 | |
| 1370 $a_mailboxes = $hook['list']; | |
| 1371 $attrib = $hook['attribs']; | |
| 1372 | |
| 1373 if ($type == 'select') { | |
| 1374 $attrib['is_escaped'] = true; | |
| 1375 $select = new html_select($attrib); | |
| 1376 | |
| 1377 // add no-selection option | |
| 1378 if ($attrib['noselection']) { | |
| 1379 $select->add(html::quote($this->gettext($attrib['noselection'])), ''); | |
| 1380 } | |
| 1381 | |
| 1382 $this->render_folder_tree_select($a_mailboxes, $mbox_name, $attrib['maxlength'], $select, $attrib['realnames']); | |
| 1383 $out = $select->show($attrib['default']); | |
| 1384 } | |
| 1385 else { | |
| 1386 $js_mailboxlist = array(); | |
| 1387 $tree = $this->render_folder_tree_html($a_mailboxes, $mbox_name, $js_mailboxlist, $attrib); | |
| 1388 | |
| 1389 if ($type != 'js') { | |
| 1390 $out = html::tag('ul', $attrib, $tree, html::$common_attrib); | |
| 1391 | |
| 1392 $this->output->include_script('treelist.js'); | |
| 1393 $this->output->add_gui_object('mailboxlist', $attrib['id']); | |
| 1394 $this->output->set_env('unreadwrap', $attrib['unreadwrap']); | |
| 1395 $this->output->set_env('collapsed_folders', (string) $this->config->get('collapsed_folders')); | |
| 1396 } | |
| 1397 | |
| 1398 $this->output->set_env('mailboxes', $js_mailboxlist); | |
| 1399 | |
| 1400 // we can't use object keys in javascript because they are unordered | |
| 1401 // we need sorted folders list for folder-selector widget | |
| 1402 $this->output->set_env('mailboxes_list', array_keys($js_mailboxlist)); | |
| 1403 } | |
| 1404 | |
| 1405 // add some labels to client | |
| 1406 $this->output->add_label('purgefolderconfirm', 'deletemessagesconfirm'); | |
| 1407 | |
| 1408 return $out; | |
| 1409 } | |
| 1410 | |
| 1411 /** | |
| 1412 * Return folders list as html_select object | |
| 1413 * | |
| 1414 * @param array $p Named parameters | |
| 1415 * | |
| 1416 * @return html_select HTML drop-down object | |
| 1417 */ | |
| 1418 public function folder_selector($p = array()) | |
| 1419 { | |
| 1420 $realnames = $this->config->get('show_real_foldernames'); | |
| 1421 $p += array('maxlength' => 100, 'realnames' => $realnames, 'is_escaped' => true); | |
| 1422 $a_mailboxes = array(); | |
| 1423 $storage = $this->get_storage(); | |
| 1424 | |
| 1425 if (empty($p['folder_name'])) { | |
| 1426 $p['folder_name'] = '*'; | |
| 1427 } | |
| 1428 | |
| 1429 if ($p['unsubscribed']) { | |
| 1430 $list = $storage->list_folders('', $p['folder_name'], $p['folder_filter'], $p['folder_rights']); | |
| 1431 } | |
| 1432 else { | |
| 1433 $list = $storage->list_folders_subscribed('', $p['folder_name'], $p['folder_filter'], $p['folder_rights']); | |
| 1434 } | |
| 1435 | |
| 1436 $delimiter = $storage->get_hierarchy_delimiter(); | |
| 1437 | |
| 1438 if (!empty($p['exceptions'])) { | |
| 1439 $list = array_diff($list, (array) $p['exceptions']); | |
| 1440 } | |
| 1441 | |
| 1442 if (!empty($p['additional'])) { | |
| 1443 foreach ($p['additional'] as $add_folder) { | |
| 1444 $add_items = explode($delimiter, $add_folder); | |
| 1445 $folder = ''; | |
| 1446 while (count($add_items)) { | |
| 1447 $folder .= array_shift($add_items); | |
| 1448 | |
| 1449 // @TODO: sorting | |
| 1450 if (!in_array($folder, $list)) { | |
| 1451 $list[] = $folder; | |
| 1452 } | |
| 1453 | |
| 1454 $folder .= $delimiter; | |
| 1455 } | |
| 1456 } | |
| 1457 } | |
| 1458 | |
| 1459 foreach ($list as $folder) { | |
| 1460 $this->build_folder_tree($a_mailboxes, $folder, $delimiter); | |
| 1461 } | |
| 1462 | |
| 1463 $select = new html_select($p); | |
| 1464 | |
| 1465 if ($p['noselection']) { | |
| 1466 $select->add(html::quote($p['noselection']), ''); | |
| 1467 } | |
| 1468 | |
| 1469 $this->render_folder_tree_select($a_mailboxes, $mbox, $p['maxlength'], $select, $p['realnames'], 0, $p); | |
| 1470 | |
| 1471 return $select; | |
| 1472 } | |
| 1473 | |
| 1474 /** | |
| 1475 * Create a hierarchical array of the mailbox list | |
| 1476 */ | |
| 1477 public function build_folder_tree(&$arrFolders, $folder, $delm = '/', $path = '') | |
| 1478 { | |
| 1479 // Handle namespace prefix | |
| 1480 $prefix = ''; | |
| 1481 if (!$path) { | |
| 1482 $n_folder = $folder; | |
| 1483 $folder = $this->storage->mod_folder($folder); | |
| 1484 | |
| 1485 if ($n_folder != $folder) { | |
| 1486 $prefix = substr($n_folder, 0, -strlen($folder)); | |
| 1487 } | |
| 1488 } | |
| 1489 | |
| 1490 $pos = strpos($folder, $delm); | |
| 1491 | |
| 1492 if ($pos !== false) { | |
| 1493 $subFolders = substr($folder, $pos+1); | |
| 1494 $currentFolder = substr($folder, 0, $pos); | |
| 1495 | |
| 1496 // sometimes folder has a delimiter as the last character | |
| 1497 if (!strlen($subFolders)) { | |
| 1498 $virtual = false; | |
| 1499 } | |
| 1500 else if (!isset($arrFolders[$currentFolder])) { | |
| 1501 $virtual = true; | |
| 1502 } | |
| 1503 else { | |
| 1504 $virtual = $arrFolders[$currentFolder]['virtual']; | |
| 1505 } | |
| 1506 } | |
| 1507 else { | |
| 1508 $subFolders = false; | |
| 1509 $currentFolder = $folder; | |
| 1510 $virtual = false; | |
| 1511 } | |
| 1512 | |
| 1513 $path .= $prefix . $currentFolder; | |
| 1514 | |
| 1515 if (!isset($arrFolders[$currentFolder])) { | |
| 1516 $arrFolders[$currentFolder] = array( | |
| 1517 'id' => $path, | |
| 1518 'name' => rcube_charset::convert($currentFolder, 'UTF7-IMAP'), | |
| 1519 'virtual' => $virtual, | |
| 1520 'folders' => array() | |
| 1521 ); | |
| 1522 } | |
| 1523 else { | |
| 1524 $arrFolders[$currentFolder]['virtual'] = $virtual; | |
| 1525 } | |
| 1526 | |
| 1527 if (strlen($subFolders)) { | |
| 1528 $this->build_folder_tree($arrFolders[$currentFolder]['folders'], $subFolders, $delm, $path.$delm); | |
| 1529 } | |
| 1530 } | |
| 1531 | |
| 1532 /** | |
| 1533 * Return html for a structured list <ul> for the mailbox tree | |
| 1534 */ | |
| 1535 public function render_folder_tree_html(&$arrFolders, &$mbox_name, &$jslist, $attrib, $nestLevel = 0) | |
| 1536 { | |
| 1537 $maxlength = intval($attrib['maxlength']); | |
| 1538 $realnames = (bool)$attrib['realnames']; | |
| 1539 $msgcounts = $this->storage->get_cache('messagecount'); | |
| 1540 $collapsed = $this->config->get('collapsed_folders'); | |
| 1541 $realnames = $this->config->get('show_real_foldernames'); | |
| 1542 | |
| 1543 $out = ''; | |
| 1544 foreach ($arrFolders as $folder) { | |
| 1545 $title = null; | |
| 1546 $folder_class = $this->folder_classname($folder['id']); | |
| 1547 $is_collapsed = strpos($collapsed, '&'.rawurlencode($folder['id']).'&') !== false; | |
| 1548 $unread = $msgcounts ? intval($msgcounts[$folder['id']]['UNSEEN']) : 0; | |
| 1549 | |
| 1550 if ($folder_class && !$realnames) { | |
| 1551 $foldername = $this->gettext($folder_class); | |
| 1552 } | |
| 1553 else { | |
| 1554 $foldername = $folder['name']; | |
| 1555 | |
| 1556 // shorten the folder name to a given length | |
| 1557 if ($maxlength && $maxlength > 1) { | |
| 1558 $fname = abbreviate_string($foldername, $maxlength); | |
| 1559 if ($fname != $foldername) { | |
| 1560 $title = $foldername; | |
| 1561 } | |
| 1562 $foldername = $fname; | |
| 1563 } | |
| 1564 } | |
| 1565 | |
| 1566 // make folder name safe for ids and class names | |
| 1567 $folder_id = rcube_utils::html_identifier($folder['id'], true); | |
| 1568 $classes = array('mailbox'); | |
| 1569 | |
| 1570 // set special class for Sent, Drafts, Trash and Junk | |
| 1571 if ($folder_class) { | |
| 1572 $classes[] = $folder_class; | |
| 1573 } | |
| 1574 | |
| 1575 if ($folder['id'] == $mbox_name) { | |
| 1576 $classes[] = 'selected'; | |
| 1577 } | |
| 1578 | |
| 1579 if ($folder['virtual']) { | |
| 1580 $classes[] = 'virtual'; | |
| 1581 } | |
| 1582 else if ($unread) { | |
| 1583 $classes[] = 'unread'; | |
| 1584 } | |
| 1585 | |
| 1586 $js_name = $this->JQ($folder['id']); | |
| 1587 $html_name = $this->Q($foldername) . ($unread ? html::span('unreadcount', sprintf($attrib['unreadwrap'], $unread)) : ''); | |
| 1588 $link_attrib = $folder['virtual'] ? array() : array( | |
| 1589 'href' => $this->url(array('_mbox' => $folder['id'])), | |
| 1590 'onclick' => sprintf("return %s.command('list','%s',this,event)", rcmail_output::JS_OBJECT_NAME, $js_name), | |
| 1591 'rel' => $folder['id'], | |
| 1592 'title' => $title, | |
| 1593 ); | |
| 1594 | |
| 1595 $out .= html::tag('li', array( | |
| 1596 'id' => "rcmli" . $folder_id, | |
| 1597 'class' => join(' ', $classes), | |
| 1598 'noclose' => true | |
| 1599 ), | |
| 1600 html::a($link_attrib, $html_name)); | |
| 1601 | |
| 1602 if (!empty($folder['folders'])) { | |
| 1603 $out .= html::div('treetoggle ' . ($is_collapsed ? 'collapsed' : 'expanded'), ' '); | |
| 1604 } | |
| 1605 | |
| 1606 $jslist[$folder['id']] = array( | |
| 1607 'id' => $folder['id'], | |
| 1608 'name' => $foldername, | |
| 1609 'virtual' => $folder['virtual'], | |
| 1610 ); | |
| 1611 | |
| 1612 if (!empty($folder_class)) { | |
| 1613 $jslist[$folder['id']]['class'] = $folder_class; | |
| 1614 } | |
| 1615 | |
| 1616 if (!empty($folder['folders'])) { | |
| 1617 $out .= html::tag('ul', array('style' => ($is_collapsed ? "display:none;" : null)), | |
| 1618 $this->render_folder_tree_html($folder['folders'], $mbox_name, $jslist, $attrib, $nestLevel+1)); | |
| 1619 } | |
| 1620 | |
| 1621 $out .= "</li>\n"; | |
| 1622 } | |
| 1623 | |
| 1624 return $out; | |
| 1625 } | |
| 1626 | |
| 1627 /** | |
| 1628 * Return html for a flat list <select> for the mailbox tree | |
| 1629 */ | |
| 1630 public function render_folder_tree_select(&$arrFolders, &$mbox_name, $maxlength, &$select, $realnames = false, $nestLevel = 0, $opts = array()) | |
| 1631 { | |
| 1632 $out = ''; | |
| 1633 | |
| 1634 foreach ($arrFolders as $folder) { | |
| 1635 // skip exceptions (and its subfolders) | |
| 1636 if (!empty($opts['exceptions']) && in_array($folder['id'], $opts['exceptions'])) { | |
| 1637 continue; | |
| 1638 } | |
| 1639 | |
| 1640 // skip folders in which it isn't possible to create subfolders | |
| 1641 if (!empty($opts['skip_noinferiors'])) { | |
| 1642 $attrs = $this->storage->folder_attributes($folder['id']); | |
| 1643 if ($attrs && in_array_nocase('\\Noinferiors', $attrs)) { | |
| 1644 continue; | |
| 1645 } | |
| 1646 } | |
| 1647 | |
| 1648 if (!$realnames && ($folder_class = $this->folder_classname($folder['id']))) { | |
| 1649 $foldername = $this->gettext($folder_class); | |
| 1650 } | |
| 1651 else { | |
| 1652 $foldername = $folder['name']; | |
| 1653 | |
| 1654 // shorten the folder name to a given length | |
| 1655 if ($maxlength && $maxlength > 1) { | |
| 1656 $foldername = abbreviate_string($foldername, $maxlength); | |
| 1657 } | |
| 1658 } | |
| 1659 | |
| 1660 $select->add(str_repeat(' ', $nestLevel*4) . html::quote($foldername), $folder['id']); | |
| 1661 | |
| 1662 if (!empty($folder['folders'])) { | |
| 1663 $out .= $this->render_folder_tree_select($folder['folders'], $mbox_name, $maxlength, | |
| 1664 $select, $realnames, $nestLevel+1, $opts); | |
| 1665 } | |
| 1666 } | |
| 1667 | |
| 1668 return $out; | |
| 1669 } | |
| 1670 | |
| 1671 /** | |
| 1672 * Return internal name for the given folder if it matches the configured special folders | |
| 1673 */ | |
| 1674 public function folder_classname($folder_id) | |
| 1675 { | |
| 1676 if ($folder_id == 'INBOX') { | |
| 1677 return 'inbox'; | |
| 1678 } | |
| 1679 | |
| 1680 // for these mailboxes we have localized labels and css classes | |
| 1681 foreach (array('sent', 'drafts', 'trash', 'junk') as $smbx) | |
| 1682 { | |
| 1683 if ($folder_id === $this->config->get($smbx.'_mbox')) { | |
| 1684 return $smbx; | |
| 1685 } | |
| 1686 } | |
| 1687 } | |
| 1688 | |
| 1689 /** | |
| 1690 * Try to localize the given IMAP folder name. | |
| 1691 * UTF-7 decode it in case no localized text was found | |
| 1692 * | |
| 1693 * @param string $name Folder name | |
| 1694 * @param bool $with_path Enable path localization | |
| 1695 * @param bool $path_remove Remove the path | |
| 1696 * | |
| 1697 * @return string Localized folder name in UTF-8 encoding | |
| 1698 */ | |
| 1699 public function localize_foldername($name, $with_path = false, $path_remove = false) | |
| 1700 { | |
| 1701 $realnames = $this->config->get('show_real_foldernames'); | |
| 1702 | |
| 1703 if (!$realnames && ($folder_class = $this->folder_classname($name))) { | |
| 1704 return $this->gettext($folder_class); | |
| 1705 } | |
| 1706 | |
| 1707 $storage = $this->get_storage(); | |
| 1708 $delimiter = $storage->get_hierarchy_delimiter(); | |
| 1709 | |
| 1710 // Remove the path | |
| 1711 if ($path_remove) { | |
| 1712 if (strpos($name, $delimiter)) { | |
| 1713 $path = explode($delimiter, $name); | |
| 1714 $name = array_pop($path); | |
| 1715 } | |
| 1716 } | |
| 1717 // try to localize path of the folder | |
| 1718 else if ($with_path && !$realnames) { | |
| 1719 $path = explode($delimiter, $name); | |
| 1720 $count = count($path); | |
| 1721 | |
| 1722 if ($count > 1) { | |
| 1723 for ($i = 1; $i < $count; $i++) { | |
| 1724 $folder = implode($delimiter, array_slice($path, 0, -$i)); | |
| 1725 if ($folder_class = $this->folder_classname($folder)) { | |
| 1726 $name = implode($delimiter, array_slice($path, $count - $i)); | |
| 1727 $name = rcube_charset::convert($name, 'UTF7-IMAP'); | |
| 1728 | |
| 1729 return $this->gettext($folder_class) . $delimiter . $name; | |
| 1730 } | |
| 1731 } | |
| 1732 } | |
| 1733 } | |
| 1734 | |
| 1735 return rcube_charset::convert($name, 'UTF7-IMAP'); | |
| 1736 } | |
| 1737 | |
| 1738 /** | |
| 1739 * Localize folder path | |
| 1740 */ | |
| 1741 public function localize_folderpath($path) | |
| 1742 { | |
| 1743 $protect_folders = $this->config->get('protect_default_folders'); | |
| 1744 $delimiter = $this->storage->get_hierarchy_delimiter(); | |
| 1745 $path = explode($delimiter, $path); | |
| 1746 $result = array(); | |
| 1747 | |
| 1748 foreach ($path as $idx => $dir) { | |
| 1749 $directory = implode($delimiter, array_slice($path, 0, $idx+1)); | |
| 1750 if ($protect_folders && $this->storage->is_special_folder($directory)) { | |
| 1751 unset($result); | |
| 1752 $result[] = $this->localize_foldername($directory); | |
| 1753 } | |
| 1754 else { | |
| 1755 $result[] = rcube_charset::convert($dir, 'UTF7-IMAP'); | |
| 1756 } | |
| 1757 } | |
| 1758 | |
| 1759 return implode($delimiter, $result); | |
| 1760 } | |
| 1761 | |
| 1762 /** | |
| 1763 * Return HTML for quota indicator object | |
| 1764 * | |
| 1765 * @param array $attrib Named parameters | |
| 1766 * | |
| 1767 * @return string HTML code for the quota indicator object | |
| 1768 */ | |
| 1769 public static function quota_display($attrib) | |
| 1770 { | |
| 1771 $rcmail = rcmail::get_instance(); | |
| 1772 | |
| 1773 if (!$attrib['id']) { | |
| 1774 $attrib['id'] = 'rcmquotadisplay'; | |
| 1775 } | |
| 1776 | |
| 1777 $_SESSION['quota_display'] = !empty($attrib['display']) ? $attrib['display'] : 'text'; | |
| 1778 | |
| 1779 $rcmail->output->add_gui_object('quotadisplay', $attrib['id']); | |
| 1780 | |
| 1781 $quota = $rcmail->quota_content($attrib); | |
| 1782 | |
| 1783 $rcmail->output->add_script('rcmail.set_quota('.rcube_output::json_serialize($quota).');', 'docready'); | |
| 1784 | |
| 1785 return html::span($attrib, ' '); | |
| 1786 } | |
| 1787 | |
| 1788 /** | |
| 1789 * Return (parsed) quota information | |
| 1790 * | |
| 1791 * @param array $attrib Named parameters | |
| 1792 * @param array $folder Current folder | |
| 1793 * | |
| 1794 * @return array Quota information | |
| 1795 */ | |
| 1796 public function quota_content($attrib = null, $folder = null) | |
| 1797 { | |
| 1798 $quota = $this->storage->get_quota($folder); | |
| 1799 $quota = $this->plugins->exec_hook('quota', $quota); | |
| 1800 | |
| 1801 $quota_result = (array) $quota; | |
| 1802 $quota_result['type'] = isset($_SESSION['quota_display']) ? $_SESSION['quota_display'] : ''; | |
| 1803 $quota_result['folder'] = $folder !== null && $folder !== '' ? $folder : 'INBOX'; | |
| 1804 | |
| 1805 if ($quota['total'] > 0) { | |
| 1806 if (!isset($quota['percent'])) { | |
| 1807 $quota_result['percent'] = min(100, round(($quota['used']/max(1,$quota['total']))*100)); | |
| 1808 } | |
| 1809 | |
| 1810 $title = sprintf('%s / %s (%.0f%%)', | |
| 1811 $this->show_bytes($quota['used'] * 1024), | |
| 1812 $this->show_bytes($quota['total'] * 1024), | |
| 1813 $quota_result['percent'] | |
| 1814 ); | |
| 1815 | |
| 1816 $quota_result['title'] = $title; | |
| 1817 | |
| 1818 if ($attrib['width']) { | |
| 1819 $quota_result['width'] = $attrib['width']; | |
| 1820 } | |
| 1821 if ($attrib['height']) { | |
| 1822 $quota_result['height'] = $attrib['height']; | |
| 1823 } | |
| 1824 | |
| 1825 // build a table of quota types/roots info | |
| 1826 if (($root_cnt = count($quota_result['all'])) > 1 || count($quota_result['all'][key($quota_result['all'])]) > 1) { | |
| 1827 $table = new html_table(array('cols' => 3, 'class' => 'quota-info')); | |
| 1828 | |
| 1829 $table->add_header(null, self::Q($this->gettext('quotatype'))); | |
| 1830 $table->add_header(null, self::Q($this->gettext('quotatotal'))); | |
| 1831 $table->add_header(null, self::Q($this->gettext('quotaused'))); | |
| 1832 | |
| 1833 foreach ($quota_result['all'] as $root => $data) { | |
| 1834 if ($root_cnt > 1 && $root) { | |
| 1835 $table->add(array('colspan' => 3, 'class' => 'root'), self::Q($root)); | |
| 1836 } | |
| 1837 | |
| 1838 if ($storage = $data['storage']) { | |
| 1839 $percent = min(100, round(($storage['used']/max(1,$storage['total']))*100)); | |
| 1840 | |
| 1841 $table->add('name', self::Q($this->gettext('quotastorage'))); | |
| 1842 $table->add(null, $this->show_bytes($storage['total'] * 1024)); | |
| 1843 $table->add(null, sprintf('%s (%.0f%%)', $this->show_bytes($storage['used'] * 1024), $percent)); | |
| 1844 } | |
| 1845 if ($message = $data['message']) { | |
| 1846 $percent = min(100, round(($message['used']/max(1,$message['total']))*100)); | |
| 1847 | |
| 1848 $table->add('name', self::Q($this->gettext('quotamessage'))); | |
| 1849 $table->add(null, intval($message['total'])); | |
| 1850 $table->add(null, sprintf('%d (%.0f%%)', $message['used'], $percent)); | |
| 1851 } | |
| 1852 } | |
| 1853 | |
| 1854 $quota_result['table'] = $table->show(); | |
| 1855 } | |
| 1856 } | |
| 1857 else { | |
| 1858 $unlimited = $this->config->get('quota_zero_as_unlimited'); | |
| 1859 $quota_result['title'] = $this->gettext($unlimited ? 'unlimited' : 'unknown'); | |
| 1860 $quota_result['percent'] = 0; | |
| 1861 } | |
| 1862 | |
| 1863 // cleanup | |
| 1864 unset($quota_result['abort']); | |
| 1865 if (empty($quota_result['table'])) { | |
| 1866 unset($quota_result['all']); | |
| 1867 } | |
| 1868 | |
| 1869 return $quota_result; | |
| 1870 } | |
| 1871 | |
| 1872 /** | |
| 1873 * Outputs error message according to server error/response codes | |
| 1874 * | |
| 1875 * @param string $fallback Fallback message label | |
| 1876 * @param array $fallback_args Fallback message label arguments | |
| 1877 * @param string $suffix Message label suffix | |
| 1878 * @param array $params Additional parameters (type, prefix) | |
| 1879 */ | |
| 1880 public function display_server_error($fallback = null, $fallback_args = null, $suffix = '', $params = array()) | |
| 1881 { | |
| 1882 $err_code = $this->storage->get_error_code(); | |
| 1883 $res_code = $this->storage->get_response_code(); | |
| 1884 $args = array(); | |
| 1885 | |
| 1886 if ($res_code == rcube_storage::NOPERM) { | |
| 1887 $error = 'errornoperm'; | |
| 1888 } | |
| 1889 else if ($res_code == rcube_storage::READONLY) { | |
| 1890 $error = 'errorreadonly'; | |
| 1891 } | |
| 1892 else if ($res_code == rcube_storage::OVERQUOTA) { | |
| 1893 $error = 'erroroverquota'; | |
| 1894 } | |
| 1895 else if ($err_code && ($err_str = $this->storage->get_error_str())) { | |
| 1896 // try to detect access rights problem and display appropriate message | |
| 1897 if (stripos($err_str, 'Permission denied') !== false) { | |
| 1898 $error = 'errornoperm'; | |
| 1899 } | |
| 1900 // try to detect full mailbox problem and display appropriate message | |
| 1901 // there can be e.g. "Quota exceeded" / "quotum would exceed" / "Over quota" | |
| 1902 else if (stripos($err_str, 'quot') !== false && preg_match('/exceed|over/i', $err_str)) { | |
| 1903 $error = 'erroroverquota'; | |
| 1904 } | |
| 1905 else { | |
| 1906 $error = 'servererrormsg'; | |
| 1907 $args = array('msg' => rcube::Q($err_str)); | |
| 1908 } | |
| 1909 } | |
| 1910 else if ($err_code < 0) { | |
| 1911 $error = 'storageerror'; | |
| 1912 } | |
| 1913 else if ($fallback) { | |
| 1914 $error = $fallback; | |
| 1915 $args = $fallback_args; | |
| 1916 $params['prefix'] = false; | |
| 1917 } | |
| 1918 | |
| 1919 if ($error) { | |
| 1920 if ($suffix && $this->text_exists($error . $suffix)) { | |
| 1921 $error .= $suffix; | |
| 1922 } | |
| 1923 | |
| 1924 $msg = $this->gettext(array('name' => $error, 'vars' => $args)); | |
| 1925 | |
| 1926 if ($params['prefix'] && $fallback) { | |
| 1927 $msg = $this->gettext(array('name' => $fallback, 'vars' => $fallback_args)) . ' ' . $msg; | |
| 1928 } | |
| 1929 | |
| 1930 $this->output->show_message($msg, $params['type'] ?: 'error'); | |
| 1931 } | |
| 1932 } | |
| 1933 | |
| 1934 /** | |
| 1935 * Output HTML editor scripts | |
| 1936 * | |
| 1937 * @param string $mode Editor mode | |
| 1938 */ | |
| 1939 public function html_editor($mode = '') | |
| 1940 { | |
| 1941 $spellcheck = intval($this->config->get('enable_spellcheck')); | |
| 1942 $spelldict = intval($this->config->get('spellcheck_dictionary')); | |
| 1943 $disabled_plugins = array(); | |
| 1944 $disabled_buttons = array(); | |
| 1945 $extra_plugins = array(); | |
| 1946 $extra_buttons = array(); | |
| 1947 | |
| 1948 if (!$spellcheck) { | |
| 1949 $disabled_plugins[] = 'spellchecker'; | |
| 1950 } | |
| 1951 | |
| 1952 $hook = $this->plugins->exec_hook('html_editor', array( | |
| 1953 'mode' => $mode, | |
| 1954 'disabled_plugins' => $disabled_plugins, | |
| 1955 'disabled_buttons' => $disabled_buttons, | |
| 1956 'extra_plugins' => $extra_plugins, | |
| 1957 'extra_buttons' => $extra_buttons, | |
| 1958 )); | |
| 1959 | |
| 1960 if ($hook['abort']) { | |
| 1961 return; | |
| 1962 } | |
| 1963 | |
| 1964 $lang_codes = array($_SESSION['language']); | |
| 1965 $assets_dir = $this->config->get('assets_dir') ?: INSTALL_PATH; | |
| 1966 | |
| 1967 if ($pos = strpos($_SESSION['language'], '_')) { | |
| 1968 $lang_codes[] = substr($_SESSION['language'], 0, $pos); | |
| 1969 } | |
| 1970 | |
| 1971 foreach ($lang_codes as $code) { | |
| 1972 if (file_exists("$assets_dir/program/js/tinymce/langs/$code.js")) { | |
| 1973 $lang = $code; | |
| 1974 break; | |
| 1975 } | |
| 1976 } | |
| 1977 | |
| 1978 if (empty($lang)) { | |
| 1979 $lang = 'en'; | |
| 1980 } | |
| 1981 | |
| 1982 $config = array( | |
| 1983 'mode' => $mode, | |
| 1984 'lang' => $lang, | |
| 1985 'skin_path' => $this->output->get_skin_path(), | |
| 1986 'spellcheck' => $spellcheck, // deprecated | |
| 1987 'spelldict' => $spelldict, | |
| 1988 'disabled_plugins' => $hook['disabled_plugins'], | |
| 1989 'disabled_buttons' => $hook['disabled_buttons'], | |
| 1990 'extra_plugins' => $hook['extra_plugins'], | |
| 1991 'extra_buttons' => $hook['extra_buttons'], | |
| 1992 ); | |
| 1993 | |
| 1994 $this->output->add_label('selectimage', 'addimage', 'selectmedia', 'addmedia'); | |
| 1995 $this->output->set_env('editor_config', $config); | |
| 1996 $this->output->include_css('program/resources/tinymce/browser.css'); | |
| 1997 $this->output->include_script('tinymce/tinymce.min.js'); | |
| 1998 $this->output->include_script('editor.js'); | |
| 1999 } | |
| 2000 | |
| 2001 /** | |
| 2002 * File upload progress handler. | |
| 2003 */ | |
| 2004 public function upload_progress() | |
| 2005 { | |
| 2006 $params = array( | |
| 2007 'action' => $this->action, | |
| 2008 'name' => rcube_utils::get_input_value('_progress', rcube_utils::INPUT_GET), | |
| 2009 ); | |
| 2010 | |
| 2011 if (function_exists('uploadprogress_get_info')) { | |
| 2012 $status = uploadprogress_get_info($params['name']); | |
| 2013 | |
| 2014 if (!empty($status)) { | |
| 2015 $params['current'] = $status['bytes_uploaded']; | |
| 2016 $params['total'] = $status['bytes_total']; | |
| 2017 } | |
| 2018 } | |
| 2019 | |
| 2020 if (!isset($status) && filter_var(ini_get('apc.rfc1867'), FILTER_VALIDATE_BOOLEAN) | |
| 2021 && ini_get('apc.rfc1867_name') | |
| 2022 ) { | |
| 2023 $prefix = ini_get('apc.rfc1867_prefix'); | |
| 2024 $status = apc_fetch($prefix . $params['name']); | |
| 2025 | |
| 2026 if (!empty($status)) { | |
| 2027 $params['current'] = $status['current']; | |
| 2028 $params['total'] = $status['total']; | |
| 2029 } | |
| 2030 } | |
| 2031 | |
| 2032 if (!isset($status) && filter_var(ini_get('session.upload_progress.enabled'), FILTER_VALIDATE_BOOLEAN) | |
| 2033 && ini_get('session.upload_progress.name') | |
| 2034 ) { | |
| 2035 $key = ini_get('session.upload_progress.prefix') . $params['name']; | |
| 2036 | |
| 2037 $params['total'] = $_SESSION[$key]['content_length']; | |
| 2038 $params['current'] = $_SESSION[$key]['bytes_processed']; | |
| 2039 } | |
| 2040 | |
| 2041 if (!empty($params['total'])) { | |
| 2042 $total = $this->show_bytes($params['total'], $unit); | |
| 2043 switch ($unit) { | |
| 2044 case 'GB': | |
| 2045 $gb = $params['current']/1073741824; | |
| 2046 $current = sprintf($gb >= 10 ? "%d" : "%.1f", $gb); | |
| 2047 break; | |
| 2048 case 'MB': | |
| 2049 $mb = $params['current']/1048576; | |
| 2050 $current = sprintf($mb >= 10 ? "%d" : "%.1f", $mb); | |
| 2051 break; | |
| 2052 case 'KB': | |
| 2053 $current = round($params['current']/1024); | |
| 2054 break; | |
| 2055 case 'B': | |
| 2056 default: | |
| 2057 $current = $params['current']; | |
| 2058 break; | |
| 2059 } | |
| 2060 | |
| 2061 $params['percent'] = round($params['current']/$params['total']*100); | |
| 2062 $params['text'] = $this->gettext(array( | |
| 2063 'name' => 'uploadprogress', | |
| 2064 'vars' => array( | |
| 2065 'percent' => $params['percent'] . '%', | |
| 2066 'current' => $current, | |
| 2067 'total' => $total | |
| 2068 ) | |
| 2069 )); | |
| 2070 } | |
| 2071 | |
| 2072 $this->output->command('upload_progress_update', $params); | |
| 2073 $this->output->send(); | |
| 2074 } | |
| 2075 | |
| 2076 /** | |
| 2077 * Initializes file uploading interface. | |
| 2078 * | |
| 2079 * @param int $max_size Optional maximum file size in bytes | |
| 2080 * | |
| 2081 * @return string Human-readable file size limit | |
| 2082 */ | |
| 2083 public function upload_init($max_size = null) | |
| 2084 { | |
| 2085 // Enable upload progress bar | |
| 2086 if ($seconds = $this->config->get('upload_progress')) { | |
| 2087 if (function_exists('uploadprogress_get_info')) { | |
| 2088 $field_name = 'UPLOAD_IDENTIFIER'; | |
| 2089 } | |
| 2090 if (!$field_name && filter_var(ini_get('apc.rfc1867'), FILTER_VALIDATE_BOOLEAN)) { | |
| 2091 $field_name = ini_get('apc.rfc1867_name'); | |
| 2092 } | |
| 2093 if (!$field_name && filter_var(ini_get('session.upload_progress.enabled'), FILTER_VALIDATE_BOOLEAN)) { | |
| 2094 $field_name = ini_get('session.upload_progress.name'); | |
| 2095 } | |
| 2096 | |
| 2097 if ($field_name) { | |
| 2098 $this->output->set_env('upload_progress_name', $field_name); | |
| 2099 $this->output->set_env('upload_progress_time', (int) $seconds); | |
| 2100 } | |
| 2101 } | |
| 2102 | |
| 2103 // find max filesize value | |
| 2104 $max_filesize = rcube_utils::max_upload_size(); | |
| 2105 if ($max_size && $max_size < $max_filesize) { | |
| 2106 $max_filesize = $max_size; | |
| 2107 } | |
| 2108 | |
| 2109 $max_filesize_txt = $this->show_bytes($max_filesize); | |
| 2110 $this->output->set_env('max_filesize', $max_filesize); | |
| 2111 $this->output->set_env('filesizeerror', $this->gettext(array( | |
| 2112 'name' => 'filesizeerror', 'vars' => array('size' => $max_filesize_txt)))); | |
| 2113 | |
| 2114 if ($max_filecount = ini_get('max_file_uploads')) { | |
| 2115 $this->output->set_env('max_filecount', $max_filecount); | |
| 2116 $this->output->set_env('filecounterror', $this->gettext(array( | |
| 2117 'name' => 'filecounterror', 'vars' => array('count' => $max_filecount)))); | |
| 2118 } | |
| 2119 | |
| 2120 return $max_filesize_txt; | |
| 2121 } | |
| 2122 | |
| 2123 /** | |
| 2124 * Upload form object | |
| 2125 * | |
| 2126 * @param array $attrib Object attributes | |
| 2127 * @param string $name Form object name | |
| 2128 * @param string $action Form action name | |
| 2129 * @param array $input_attr File input attributes | |
| 2130 * | |
| 2131 * @return string HTML output | |
| 2132 */ | |
| 2133 public function upload_form($attrib, $name, $action, $input_attr = array()) | |
| 2134 { | |
| 2135 // Get filesize, enable upload progress bar | |
| 2136 $max_filesize = $this->upload_init(); | |
| 2137 | |
| 2138 $hint = html::div('hint', $this->gettext(array('name' => 'maxuploadsize', 'vars' => array('size' => $max_filesize)))); | |
| 2139 | |
| 2140 if ($attrib['mode'] == 'hint') { | |
| 2141 return $hint; | |
| 2142 } | |
| 2143 | |
| 2144 // set defaults | |
| 2145 $attrib += array('id' => 'rcmUploadbox', 'buttons' => 'yes'); | |
| 2146 | |
| 2147 $event = rcmail_output::JS_OBJECT_NAME . ".command('$action', this.form)"; | |
| 2148 $form_id = $attrib['id'] . 'Frm'; | |
| 2149 | |
| 2150 // Default attributes of file input and form | |
| 2151 $input_attr += array( | |
| 2152 'id' => $attrib['id'] . 'Input', | |
| 2153 'type' => 'file', | |
| 2154 'name' => '_attachments[]', | |
| 2155 ); | |
| 2156 | |
| 2157 $form_attr = array( | |
| 2158 'id' => $form_id, | |
| 2159 'name' => $name, | |
| 2160 'method' => 'post', | |
| 2161 'enctype' => 'multipart/form-data' | |
| 2162 ); | |
| 2163 | |
| 2164 if ($attrib['mode'] == 'smart') { | |
| 2165 unset($attrib['buttons']); | |
| 2166 $form_attr['class'] = 'smart-upload'; | |
| 2167 $input_attr = array_merge($input_attr, array( | |
| 2168 // #5854: Chrome does not execute onchange when selecting the same file. | |
| 2169 // To fix this we reset the input using null value. | |
| 2170 'onchange' => "$event; this.value=null", | |
| 2171 'class' => 'smart-upload', | |
| 2172 'tabindex' => '-1', | |
| 2173 )); | |
| 2174 } | |
| 2175 | |
| 2176 $input = new html_inputfield($input_attr); | |
| 2177 $content = $attrib['prefix'] . $input->show(); | |
| 2178 | |
| 2179 if ($attrib['mode'] != 'smart') { | |
| 2180 $content = html::div(null, $content); | |
| 2181 $content .= $hint; | |
| 2182 } | |
| 2183 | |
| 2184 if (rcube_utils::get_boolean($attrib['buttons'])) { | |
| 2185 $button = new html_inputfield(array('type' => 'button')); | |
| 2186 $content .= html::div('buttons', | |
| 2187 $button->show($this->gettext('close'), array('class' => 'button', 'onclick' => "$('#{$attrib['id']}').hide()")) . ' ' . | |
| 2188 $button->show($this->gettext('upload'), array('class' => 'button mainaction', 'onclick' => $event)) | |
| 2189 ); | |
| 2190 } | |
| 2191 | |
| 2192 $this->output->add_gui_object($name, $form_id); | |
| 2193 | |
| 2194 return html::div($attrib, $this->output->form_tag($form_attr, $content)); | |
| 2195 } | |
| 2196 | |
| 2197 /** | |
| 2198 * Outputs uploaded file content (with image thumbnails support | |
| 2199 * | |
| 2200 * @param array $file Upload file data | |
| 2201 */ | |
| 2202 public function display_uploaded_file($file) | |
| 2203 { | |
| 2204 if (empty($file)) { | |
| 2205 return; | |
| 2206 } | |
| 2207 | |
| 2208 $file = $this->plugins->exec_hook('attachment_display', $file); | |
| 2209 | |
| 2210 if ($file['status']) { | |
| 2211 if (empty($file['size'])) { | |
| 2212 $file['size'] = $file['data'] ? strlen($file['data']) : @filesize($file['path']); | |
| 2213 } | |
| 2214 | |
| 2215 // generate image thumbnail for file browser in HTML editor | |
| 2216 if (!empty($_GET['_thumbnail'])) { | |
| 2217 $temp_dir = $this->config->get('temp_dir'); | |
| 2218 $thumbnail_size = 80; | |
| 2219 $mimetype = $file['mimetype']; | |
| 2220 $file_ident = $file['id'] . ':' . $file['mimetype'] . ':' . $file['size']; | |
| 2221 $cache_basename = $temp_dir . '/' . md5($file_ident . ':' . $this->user->ID . ':' . $thumbnail_size); | |
| 2222 $cache_file = $cache_basename . '.thumb'; | |
| 2223 | |
| 2224 // render thumbnail image if not done yet | |
| 2225 if (!is_file($cache_file)) { | |
| 2226 if (!$file['path']) { | |
| 2227 $orig_name = $filename = $cache_basename . '.tmp'; | |
| 2228 file_put_contents($orig_name, $file['data']); | |
| 2229 } | |
| 2230 else { | |
| 2231 $filename = $file['path']; | |
| 2232 } | |
| 2233 | |
| 2234 $image = new rcube_image($filename); | |
| 2235 if ($imgtype = $image->resize($thumbnail_size, $cache_file, true)) { | |
| 2236 $mimetype = 'image/' . $imgtype; | |
| 2237 | |
| 2238 if ($orig_name) { | |
| 2239 unlink($orig_name); | |
| 2240 } | |
| 2241 } | |
| 2242 } | |
| 2243 | |
| 2244 if (is_file($cache_file)) { | |
| 2245 // cache for 1h | |
| 2246 $this->output->future_expire_header(3600); | |
| 2247 header('Content-Type: ' . $mimetype); | |
| 2248 header('Content-Length: ' . filesize($cache_file)); | |
| 2249 | |
| 2250 readfile($cache_file); | |
| 2251 exit; | |
| 2252 } | |
| 2253 } | |
| 2254 | |
| 2255 header('Content-Type: ' . $file['mimetype']); | |
| 2256 header('Content-Length: ' . $file['size']); | |
| 2257 | |
| 2258 if ($file['data']) { | |
| 2259 echo $file['data']; | |
| 2260 } | |
| 2261 else if ($file['path']) { | |
| 2262 readfile($file['path']); | |
| 2263 } | |
| 2264 } | |
| 2265 } | |
| 2266 | |
| 2267 /** | |
| 2268 * Initializes client-side autocompletion. | |
| 2269 */ | |
| 2270 public function autocomplete_init() | |
| 2271 { | |
| 2272 static $init; | |
| 2273 | |
| 2274 if ($init) { | |
| 2275 return; | |
| 2276 } | |
| 2277 | |
| 2278 $init = 1; | |
| 2279 | |
| 2280 if (($threads = (int)$this->config->get('autocomplete_threads')) > 0) { | |
| 2281 $book_types = (array) $this->config->get('autocomplete_addressbooks', 'sql'); | |
| 2282 if (count($book_types) > 1) { | |
| 2283 $this->output->set_env('autocomplete_threads', $threads); | |
| 2284 $this->output->set_env('autocomplete_sources', $book_types); | |
| 2285 } | |
| 2286 } | |
| 2287 | |
| 2288 $this->output->set_env('autocomplete_max', (int)$this->config->get('autocomplete_max', 15)); | |
| 2289 $this->output->set_env('autocomplete_min_length', $this->config->get('autocomplete_min_length')); | |
| 2290 $this->output->add_label('autocompletechars', 'autocompletemore'); | |
| 2291 } | |
| 2292 | |
| 2293 /** | |
| 2294 * Returns supported font-family specifications | |
| 2295 * | |
| 2296 * @param string $font Font name | |
| 2297 * | |
| 2298 * @param string|array Font-family specification array or string (if $font is used) | |
| 2299 */ | |
| 2300 public static function font_defs($font = null) | |
| 2301 { | |
| 2302 $fonts = array( | |
| 2303 'Andale Mono' => '"Andale Mono",Times,monospace', | |
| 2304 'Arial' => 'Arial,Helvetica,sans-serif', | |
| 2305 'Arial Black' => '"Arial Black","Avant Garde",sans-serif', | |
| 2306 'Book Antiqua' => '"Book Antiqua",Palatino,serif', | |
| 2307 'Courier New' => '"Courier New",Courier,monospace', | |
| 2308 'Georgia' => 'Georgia,Palatino,serif', | |
| 2309 'Helvetica' => 'Helvetica,Arial,sans-serif', | |
| 2310 'Impact' => 'Impact,Chicago,sans-serif', | |
| 2311 'Tahoma' => 'Tahoma,Arial,Helvetica,sans-serif', | |
| 2312 'Terminal' => 'Terminal,Monaco,monospace', | |
| 2313 'Times New Roman' => '"Times New Roman",Times,serif', | |
| 2314 'Trebuchet MS' => '"Trebuchet MS",Geneva,sans-serif', | |
| 2315 'Verdana' => 'Verdana,Geneva,sans-serif', | |
| 2316 ); | |
| 2317 | |
| 2318 if ($font) { | |
| 2319 return $fonts[$font]; | |
| 2320 } | |
| 2321 | |
| 2322 return $fonts; | |
| 2323 } | |
| 2324 | |
| 2325 /** | |
| 2326 * Create a human readable string for a number of bytes | |
| 2327 * | |
| 2328 * @param int $bytes Number of bytes | |
| 2329 * @param string &$unit Size unit | |
| 2330 * | |
| 2331 * @return string Byte string | |
| 2332 */ | |
| 2333 public function show_bytes($bytes, &$unit = null) | |
| 2334 { | |
| 2335 if ($bytes >= 1073741824) { | |
| 2336 $unit = 'GB'; | |
| 2337 $gb = $bytes/1073741824; | |
| 2338 $str = sprintf($gb >= 10 ? "%d " : "%.1f ", $gb) . $this->gettext($unit); | |
| 2339 } | |
| 2340 else if ($bytes >= 1048576) { | |
| 2341 $unit = 'MB'; | |
| 2342 $mb = $bytes/1048576; | |
| 2343 $str = sprintf($mb >= 10 ? "%d " : "%.1f ", $mb) . $this->gettext($unit); | |
| 2344 } | |
| 2345 else if ($bytes >= 1024) { | |
| 2346 $unit = 'KB'; | |
| 2347 $str = sprintf("%d ", round($bytes/1024)) . $this->gettext($unit); | |
| 2348 } | |
| 2349 else { | |
| 2350 $unit = 'B'; | |
| 2351 $str = sprintf('%d ', $bytes) . $this->gettext($unit); | |
| 2352 } | |
| 2353 | |
| 2354 return $str; | |
| 2355 } | |
| 2356 | |
| 2357 /** | |
| 2358 * Returns real size (calculated) of the message part | |
| 2359 * | |
| 2360 * @param rcube_message_part $part Message part | |
| 2361 * | |
| 2362 * @return string Part size (and unit) | |
| 2363 */ | |
| 2364 public function message_part_size($part) | |
| 2365 { | |
| 2366 if (isset($part->d_parameters['size'])) { | |
| 2367 $size = $this->show_bytes((int)$part->d_parameters['size']); | |
| 2368 } | |
| 2369 else { | |
| 2370 $size = $part->size; | |
| 2371 | |
| 2372 if ($size === 0) { | |
| 2373 $part->exact_size = true; | |
| 2374 } | |
| 2375 | |
| 2376 if ($part->encoding == 'base64') { | |
| 2377 $size = $size / 1.33; | |
| 2378 } | |
| 2379 | |
| 2380 $size = $this->show_bytes($size); | |
| 2381 } | |
| 2382 | |
| 2383 if (!$part->exact_size) { | |
| 2384 $size = '~' . $size; | |
| 2385 } | |
| 2386 | |
| 2387 return $size; | |
| 2388 } | |
| 2389 | |
| 2390 /** | |
| 2391 * Returns message UID(s) and IMAP folder(s) from GET/POST data | |
| 2392 * | |
| 2393 * @param string $uids UID value to decode | |
| 2394 * @param string $mbox Default mailbox value (if not encoded in UIDs) | |
| 2395 * @param bool $is_multifolder Will be set to True if multi-folder request | |
| 2396 * | |
| 2397 * @return array List of message UIDs per folder | |
| 2398 */ | |
| 2399 public static function get_uids($uids = null, $mbox = null, &$is_multifolder = false) | |
| 2400 { | |
| 2401 // message UID (or comma-separated list of IDs) is provided in | |
| 2402 // the form of <ID>-<MBOX>[,<ID>-<MBOX>]* | |
| 2403 | |
| 2404 $_uid = $uids ?: rcube_utils::get_input_value('_uid', rcube_utils::INPUT_GPC); | |
| 2405 $_mbox = $mbox ?: (string) rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_GPC); | |
| 2406 | |
| 2407 // already a hash array | |
| 2408 if (is_array($_uid) && !isset($_uid[0])) { | |
| 2409 return $_uid; | |
| 2410 } | |
| 2411 | |
| 2412 $result = array(); | |
| 2413 | |
| 2414 // special case: * | |
| 2415 if ($_uid == '*' && is_object($_SESSION['search'][1]) && $_SESSION['search'][1]->multi) { | |
| 2416 $is_multifolder = true; | |
| 2417 // extract the full list of UIDs per folder from the search set | |
| 2418 foreach ($_SESSION['search'][1]->sets as $subset) { | |
| 2419 $mbox = $subset->get_parameters('MAILBOX'); | |
| 2420 $result[$mbox] = $subset->get(); | |
| 2421 } | |
| 2422 } | |
| 2423 else { | |
| 2424 if (is_string($_uid)) | |
| 2425 $_uid = explode(',', $_uid); | |
| 2426 | |
| 2427 // create a per-folder UIDs array | |
| 2428 foreach ((array)$_uid as $uid) { | |
| 2429 list($uid, $mbox) = explode('-', $uid, 2); | |
| 2430 if (!strlen($mbox)) { | |
| 2431 $mbox = $_mbox; | |
| 2432 } | |
| 2433 else { | |
| 2434 $is_multifolder = true; | |
| 2435 } | |
| 2436 | |
| 2437 if ($uid == '*') { | |
| 2438 $result[$mbox] = $uid; | |
| 2439 } | |
| 2440 else { | |
| 2441 $result[$mbox][] = $uid; | |
| 2442 } | |
| 2443 } | |
| 2444 } | |
| 2445 | |
| 2446 return $result; | |
| 2447 } | |
| 2448 | |
| 2449 /** | |
| 2450 * Get resource file content (with assets_dir support) | |
| 2451 * | |
| 2452 * @param string $name File name | |
| 2453 * | |
| 2454 * @return string File content | |
| 2455 */ | |
| 2456 public function get_resource_content($name) | |
| 2457 { | |
| 2458 if (!strpos($name, '/')) { | |
| 2459 $name = "program/resources/$name"; | |
| 2460 } | |
| 2461 | |
| 2462 $assets_dir = $this->config->get('assets_dir'); | |
| 2463 | |
| 2464 if ($assets_dir) { | |
| 2465 $path = slashify($assets_dir) . $name; | |
| 2466 if (@file_exists($path)) { | |
| 2467 $name = $path; | |
| 2468 } | |
| 2469 } | |
| 2470 | |
| 2471 return file_get_contents($name, false); | |
| 2472 } | |
| 2473 | |
| 2474 /** | |
| 2475 * Converts HTML content into plain text | |
| 2476 * | |
| 2477 * @param string $html HTML content | |
| 2478 * @param array $options Conversion parameters (width, links, charset) | |
| 2479 * | |
| 2480 * @return string Plain text | |
| 2481 */ | |
| 2482 public function html2text($html, $options = array()) | |
| 2483 { | |
| 2484 $default_options = array( | |
| 2485 'links' => true, | |
| 2486 'width' => 75, | |
| 2487 'body' => $html, | |
| 2488 'charset' => RCUBE_CHARSET, | |
| 2489 ); | |
| 2490 | |
| 2491 $options = array_merge($default_options, (array) $options); | |
| 2492 | |
| 2493 // Plugins may want to modify HTML in another/additional way | |
| 2494 $options = $this->plugins->exec_hook('html2text', $options); | |
| 2495 | |
| 2496 // Convert to text | |
| 2497 if (!$options['abort']) { | |
| 2498 $converter = new rcube_html2text($options['body'], | |
| 2499 false, $options['links'], $options['width'], $options['charset']); | |
| 2500 | |
| 2501 $options['body'] = rtrim($converter->get_text()); | |
| 2502 } | |
| 2503 | |
| 2504 return $options['body']; | |
| 2505 } | |
| 2506 | |
| 2507 /** | |
| 2508 * Connect to the mail storage server with stored session data | |
| 2509 * | |
| 2510 * @return bool True on success, False on error | |
| 2511 */ | |
| 2512 public function storage_connect() | |
| 2513 { | |
| 2514 $storage = $this->get_storage(); | |
| 2515 | |
| 2516 if ($_SESSION['storage_host'] && !$storage->is_connected()) { | |
| 2517 $host = $_SESSION['storage_host']; | |
| 2518 $user = $_SESSION['username']; | |
| 2519 $port = $_SESSION['storage_port']; | |
| 2520 $ssl = $_SESSION['storage_ssl']; | |
| 2521 $pass = $this->decrypt($_SESSION['password']); | |
| 2522 | |
| 2523 if (!$storage->connect($host, $user, $pass, $port, $ssl)) { | |
| 2524 if (is_object($this->output)) { | |
| 2525 $this->output->show_message('storageerror', 'error'); | |
| 2526 } | |
| 2527 } | |
| 2528 else { | |
| 2529 $this->set_storage_prop(); | |
| 2530 } | |
| 2531 } | |
| 2532 | |
| 2533 return $storage->is_connected(); | |
| 2534 } | |
| 2535 } |
