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