diff options
Diffstat (limited to 'program/include')
-rwxr-xr-x | program/include/iniset.php | 2 | ||||
-rw-r--r-- | program/include/main.inc | 525 | ||||
-rw-r--r-- | program/include/rcmail.php | 462 | ||||
-rw-r--r-- | program/include/rcube_config.php | 152 | ||||
-rw-r--r-- | program/include/rcube_imap.php | 2 | ||||
-rw-r--r-- | program/include/rcube_json_output.php | 4 | ||||
-rw-r--r-- | program/include/rcube_shared.inc | 4 | ||||
-rwxr-xr-x | program/include/rcube_template.php | 67 | ||||
-rw-r--r-- | program/include/rcube_user.php | 125 |
9 files changed, 744 insertions, 599 deletions
diff --git a/program/include/iniset.php b/program/include/iniset.php index 926b2826f..5072d636c 100755 --- a/program/include/iniset.php +++ b/program/include/iniset.php @@ -2,7 +2,7 @@ /* +-----------------------------------------------------------------------+ - | program/include/iniset.inc | + | program/include/iniset.php | | | | This file is part of the RoundCube Webmail client | | Copyright (C) 2008, RoundCube Dev, - Switzerland | diff --git a/program/include/main.inc b/program/include/main.inc index 853ca686c..e15215015 100644 --- a/program/include/main.inc +++ b/program/include/main.inc @@ -40,153 +40,6 @@ define('RCUBE_INPUT_GPC', 0x0103); /** - * Initial startup function - * to register session, create database and imap connections - * - * @param string Current task - */ -function rcmail_startup($task='mail') - { - global $sess_id, $sess_user_lang; - global $CONFIG, $OUTPUT, $IMAP, $DB, $USER; - - // start output buffering, we don't need any output yet, - // it'll be cleared after reading of config files, etc. - ob_start(); - - // load configuration - $CONFIG = rcmail_load_config(); - - // set session domain - if (isset($CONFIG['session_domain']) && !empty($CONFIG['session_domain'])) { - ini_set('session.cookie_domain', $CONFIG['session_domain']); - } - - // set session garbage collecting time according to session_lifetime - if (!empty($CONFIG['session_lifetime'])) - ini_set('session.gc_maxlifetime', ($CONFIG['session_lifetime']) * 120); - - // prepare DB connection - $dbwrapper = empty($CONFIG['db_backend']) ? 'db' : $CONFIG['db_backend']; - $dbclass = "rcube_" . $dbwrapper; - - $DB = new $dbclass($CONFIG['db_dsnw'], $CONFIG['db_dsnr'], $CONFIG['db_persistent']); - $DB->sqlite_initials = INSTALL_PATH.'SQL/sqlite.initial.sql'; - $DB->set_debug((bool)$CONFIG['sql_debug']); - $DB->db_connect('w'); - - // use database for storing session data - include_once('include/session.inc'); - - // clear output buffer - ob_end_clean(); - - // init session - session_start(); - $sess_id = session_id(); - - // create session and set session vars - if (!isset($_SESSION['auth_time'])) - { - $_SESSION['user_lang'] = rcube_language_prop($CONFIG['locale_string']); - $_SESSION['auth_time'] = time(); - $_SESSION['temp'] = true; - } - - // set session vars global - $sess_user_lang = rcube_language_prop($_SESSION['user_lang']); - - // create user object - $USER = new rcube_user($_SESSION['user_id']); - - // overwrite config with user preferences - $CONFIG = array_merge($CONFIG, (array)$USER->get_prefs()); - - // reset some session parameters when changing task - if ($_SESSION['task'] != $task) - unset($_SESSION['page']); - - // set current task to session - $_SESSION['task'] = $task; - - // create IMAP object - if ($task=='mail') - rcmail_imap_init(); - - // set localization - if ($CONFIG['locale_string']) - setlocale(LC_ALL, $CONFIG['locale_string']); - else if ($sess_user_lang) - setlocale(LC_ALL, $sess_user_lang); - - register_shutdown_function('rcmail_shutdown'); - } - - -/** - * Load roundcube configuration array - * - * @return array Named configuration parameters - */ -function rcmail_load_config() - { - // load config file - include_once('config/main.inc.php'); - $conf = is_array($rcmail_config) ? $rcmail_config : array(); - - // load host-specific configuration - rcmail_load_host_config($conf); - - $conf['skin_path'] = $conf['skin_path'] ? unslashify($conf['skin_path']) : 'skins/default'; - - // load db conf - include_once('config/db.inc.php'); - $conf = array_merge($conf, $rcmail_config); - - if (empty($conf['log_dir'])) - $conf['log_dir'] = INSTALL_PATH.'logs'; - else - $conf['log_dir'] = unslashify($conf['log_dir']); - - // set PHP error logging according to config - if ($conf['debug_level'] & 1) - { - ini_set('log_errors', 1); - ini_set('error_log', $conf['log_dir'].'/errors'); - } - if ($conf['debug_level'] & 4) - ini_set('display_errors', 1); - else - ini_set('display_errors', 0); - - return $conf; - } - - -/** - * Load a host-specific config file if configured - * This will merge the host specific configuration with the given one - * - * @param array Global configuration parameters - */ -function rcmail_load_host_config(&$config) - { - $fname = NULL; - - if (is_array($config['include_host_config'])) - $fname = $config['include_host_config'][$_SERVER['HTTP_HOST']]; - else if (!empty($config['include_host_config'])) - $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php'; - - if ($fname && is_file('config/'.$fname)) - { - include('config/'.$fname); - $config = array_merge($config, $rcmail_config); - } - } - - -/** * Create unique authorization hash * * @param string Session ID @@ -246,84 +99,6 @@ function rcmail_authenticate_session() } -/** - * Create global IMAP object and connect to server - * - * @param boolean True if connection should be established - */ -function rcmail_imap_init($connect=FALSE) - { - global $CONFIG, $DB, $IMAP, $OUTPUT; - - $IMAP = new rcube_imap($DB); - $IMAP->debug_level = $CONFIG['debug_level']; - $IMAP->skip_deleted = $CONFIG['skip_deleted']; - - - // connect with stored session data - if ($connect) - { - if (!($conn = $IMAP->connect($_SESSION['imap_host'], $_SESSION['username'], decrypt_passwd($_SESSION['password']), $_SESSION['imap_port'], $_SESSION['imap_ssl']))) - $OUTPUT->show_message('imaperror', 'error'); - - rcmail_set_imap_prop(); - } - - // enable caching of imap data - if ($CONFIG['enable_caching']===TRUE) - $IMAP->set_caching(TRUE); - - // set pagesize from config - if (isset($CONFIG['pagesize'])) - $IMAP->set_pagesize($CONFIG['pagesize']); - } - - -/** - * Set root dir and last stored mailbox - * This must be done AFTER connecting to the server! - */ -function rcmail_set_imap_prop() - { - global $CONFIG, $IMAP; - - if (!empty($CONFIG['default_charset'])) - $IMAP->set_charset($CONFIG['default_charset']); - - // set root dir from config - if (!empty($CONFIG['imap_root'])) - $IMAP->set_rootdir($CONFIG['imap_root']); - - if (is_array($CONFIG['default_imap_folders'])) - $IMAP->set_default_mailboxes($CONFIG['default_imap_folders']); - - if (!empty($_SESSION['mbox'])) - $IMAP->set_mailbox($_SESSION['mbox']); - if (isset($_SESSION['page'])) - $IMAP->set_page($_SESSION['page']); - } - - -/** - * Do these things on script shutdown - */ -function rcmail_shutdown() - { - global $IMAP, $CONTACTS; - - if (is_object($IMAP)) - { - $IMAP->close(); - $IMAP->write_cache(); - } - - if (is_object($CONTACTS)) - $CONTACTS->close(); - - // before closing the database connection, write session data - session_write_close(); - } - /** * Destroy session data and remove cookie @@ -339,17 +114,18 @@ function rcmail_kill_session() $USER->save_prefs($a_user_prefs); } - $_SESSION = array('user_lang' => $GLOBALS['sess_user_lang'], 'auth_time' => time(), 'temp' => true); + $_SESSION = array('language' => $USER->language, 'auth_time' => time(), 'temp' => true); setcookie('sessauth', '-del-', time()-60); $USER->reset(); } + /** * Do server side actions on logout */ function rcmail_logout_actions() { - global $CONFIG, $IMAP; + global $CONFIG, $IMAP, $RCMAIL; // on logout action we're not connected to imap server if (($CONFIG['logout_purge'] && !empty($CONFIG['trash_mbox'])) @@ -358,7 +134,7 @@ function rcmail_logout_actions() if (!rcmail_authenticate_session()) return; - rcmail_imap_init(true); + $RCMAIL->imap_init(true); } if ($CONFIG['logout_purge'] && !empty($CONFIG['trash_mbox'])) @@ -411,114 +187,6 @@ function get_sequence_name($sequence) /** - * Check the given string and returns language properties - * - * @param string Language code - * @param string Peropert name - * @return string Property value - */ -function rcube_language_prop($lang, $prop='lang') - { - static $rcube_languages, $rcube_language_aliases, $rcube_charsets; - - if (empty($rcube_languages)) - @include(INSTALL_PATH.'program/localization/index.inc'); - - // check if we have an alias for that language - if (!isset($rcube_languages[$lang]) && isset($rcube_language_aliases[$lang])) - $lang = $rcube_language_aliases[$lang]; - - // try the first two chars - if (!isset($rcube_languages[$lang]) && strlen($lang)>2) - { - $lang = substr($lang, 0, 2); - $lang = rcube_language_prop($lang); - } - - if (!isset($rcube_languages[$lang])) - $lang = 'en_US'; - - // language has special charset configured - if (isset($rcube_charsets[$lang])) - $charset = $rcube_charsets[$lang]; - else - $charset = 'UTF-8'; - - - if ($prop=='charset') - return $charset; - else - return $lang; - } - - -/** - * Init output object for GUI and add common scripts. - * This will instantiate a rcmail_template object and set - * environment vars according to the current session and configuration - */ -function rcmail_load_gui() -{ - global $CONFIG, $OUTPUT, $sess_user_lang; - - // init output page - $OUTPUT = new rcube_template($CONFIG, $GLOBALS['_task']); - $OUTPUT->set_env('comm_path', $GLOBALS['COMM_PATH']); - - foreach (array('flag_for_deletion') as $js_config_var) - $OUTPUT->set_env($js_config_var, $CONFIG[$js_config_var]); - - if (!empty($GLOBALS['_framed'])) - $OUTPUT->set_env('framed', true); - - // set locale setting - rcmail_set_locale($sess_user_lang); - - // set user-selected charset - if (!empty($CONFIG['charset'])) - $OUTPUT->set_charset($CONFIG['charset']); - - // add some basic label to client - $OUTPUT->add_label('loading'); -} - -/** - * Create an output object for JSON responses - */ -function rcmail_init_json() -{ - global $CONFIG, $OUTPUT; - - // init output object - $OUTPUT = new rcube_json_output($CONFIG, $GLOBALS['_task']); - - // set locale setting - rcmail_set_locale($sess_user_lang); -} - -/** - * Set localization charset based on the given language. - * This also creates a global property for mbstring usage. - */ -function rcmail_set_locale($lang) - { - global $OUTPUT, $MBSTRING; - static $s_mbstring_loaded = NULL; - - // settings for mbstring module (by Tadashi Jokagi) - if (is_null($s_mbstring_loaded)) - $MBSTRING = $s_mbstring_loaded = extension_loaded("mbstring"); - else - $MBSTRING = $s_mbstring_loaded = FALSE; - - if ($MBSTRING) - mb_internal_encoding(RCMAIL_CHARSET); - - $OUTPUT->set_charset(rcube_language_prop($lang, 'charset')); - } - - -/** * Auto-select IMAP host based on the posted login information * * @return string Selected IMAP host @@ -551,149 +219,6 @@ function rcmail_autoselect_host() /** - * Perfom login to the IMAP server and to the webmail service. - * This will also create a new user entry if auto_create_user is configured. - * - * @param string IMAP user name - * @param string IMAP password - * @param string IMAP host - * @return boolean True on success, False on failure - */ -function rcmail_login($user, $pass, $host=NULL) - { - global $CONFIG, $IMAP, $DB, $USER, $sess_user_lang; - $user_id = NULL; - - if (!$host) - $host = $CONFIG['default_host']; - - // Validate that selected host is in the list of configured hosts - if (is_array($CONFIG['default_host'])) - { - $allowed = FALSE; - foreach ($CONFIG['default_host'] as $key => $host_allowed) - { - if (!is_numeric($key)) - $host_allowed = $key; - if ($host == $host_allowed) - { - $allowed = TRUE; - break; - } - } - if (!$allowed) - return FALSE; - } - else if (!empty($CONFIG['default_host']) && $host != $CONFIG['default_host']) - return FALSE; - - // parse $host URL - $a_host = parse_url($host); - if ($a_host['host']) - { - $host = $a_host['host']; - $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? $a_host['scheme'] : null; - $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : $CONFIG['default_port']); - } - else - $imap_port = $CONFIG['default_port']; - - - /* Modify username with domain if required - Inspired by Marco <P0L0_notspam_binware.org> - */ - // Check if we need to add domain - if (!empty($CONFIG['username_domain']) && !strpos($user, '@')) - { - if (is_array($CONFIG['username_domain']) && isset($CONFIG['username_domain'][$host])) - $user .= '@'.$CONFIG['username_domain'][$host]; - else if (is_string($CONFIG['username_domain'])) - $user .= '@'.$CONFIG['username_domain']; - } - - // try to resolve email address from virtuser table - if (!empty($CONFIG['virtuser_file']) && strpos($user, '@')) - $user = rcube_user::email2user($user); - - // lowercase username if it's an e-mail address (#1484473) - if (strpos($user, '@')) - $user = strtolower($user); - - // query if user already registered - if ($existing = rcube_user::query($user, $host)) - $USER = $existing; - - // user already registered -> overwrite username - if ($USER->ID) - { - $user_id = $USER->ID; - $user = $USER->data['username']; - } - - // exit if IMAP login failed - if (!($imap_login = $IMAP->connect($host, $user, $pass, $imap_port, $imap_ssl))) - return false; - - // user already registered - if ($USER->ID) - { - // get user prefs - $CONFIG = array_merge($CONFIG, (array)$USER->get_prefs()); - - // set user specific language - if (!empty($USER->data['language'])) - $sess_user_lang = $_SESSION['user_lang'] = $USER->data['language']; - - // update user's record - $USER->touch(); - } - // create new system user - else if ($CONFIG['auto_create_user']) - { - if ($created = rcube_user::create($user, $host)) - { - $USER = $created; - - // get existing mailboxes - $a_mailboxes = $IMAP->list_mailboxes(); - } - } - else - { - raise_error(array( - 'code' => 600, - 'type' => 'php', - 'file' => "config/main.inc.php", - 'message' => "Acces denied for new user $user. 'auto_create_user' is disabled" - ), true, false); - } - - if ($USER->ID) - { - $_SESSION['user_id'] = $USER->ID; - $_SESSION['username'] = $USER->data['username']; - $_SESSION['imap_host'] = $host; - $_SESSION['imap_port'] = $imap_port; - $_SESSION['imap_ssl'] = $imap_ssl; - $_SESSION['user_lang'] = $sess_user_lang; - $_SESSION['password'] = encrypt_passwd($pass); - $_SESSION['login_time'] = mktime(); - - // force reloading complete list of subscribed mailboxes - rcmail_set_imap_prop(); - $IMAP->clear_cache('mailboxes'); - - if ($CONFIG['create_default_folders']) - $IMAP->create_default_folders(); - - return TRUE; - } - - return FALSE; - } - - -/** * Load virtuser table in array * * @return array Virtuser table entries @@ -745,9 +270,9 @@ function rcmail_findinvirtual($pattern) */ function rcmail_overwrite_action($action) { - global $OUTPUT; - $GLOBALS['_action'] = $action; - $OUTPUT->set_env('action', $action); + $app = rcmail::get_instance(); + $app->action = $action; + $app->output->set_env('action', $action); } @@ -761,12 +286,13 @@ function rcmail_overwrite_action($action) */ function rcmail_url($action, $p=array(), $task=null) { - global $MAIN_TASKS, $COMM_PATH; + $app = rcmail::get_instance(); + $qstring = ''; - $base = $COMM_PATH; + $base = $app->comm_path; - if ($task && in_array($task, $MAIN_TASKS)) - $base = ereg_replace('_task=[a-z]+', '_task='.$task, $COMM_PATH); + if ($task && in_array($task, rcmail::$main_tasks)) + $base = ereg_replace('_task=[a-z]+', '_task='.$task, $app->comm_path); if (is_array($p)) foreach ($p as $key => $val) @@ -964,8 +490,7 @@ function rcmail_message_cache_gc() */ function rcube_charset_convert($str, $from, $to=NULL) { - global $MBSTRING; - static $convert_warning = false; + static $mbstring_loaded = null, $convert_warning = false; $from = strtoupper($from); $to = $to==NULL ? strtoupper(RCMAIL_CHARSET) : strtoupper($to); @@ -989,8 +514,14 @@ function rcube_charset_convert($str, $from, $to=NULL) return iconv(($aliases[$from] ? $aliases[$from] : $from), ($aliases[$to] ? $aliases[$to] : $to) . "//IGNORE", $str); } - // convert charset using mbstring module - if ($MBSTRING) + // settings for mbstring module (by Tadashi Jokagi) + if (is_null($mbstring_loaded)) { + if ($mbstring_loaded = extension_loaded("mbstring")) + mb_internal_encoding(RCMAIL_CHARSET); + } + + // convert charset using mbstring module + if ($mbstring_loaded) { $aliases['UTF-7'] = 'UTF7-IMAP'; $aliases['WINDOWS-1257'] = 'ISO-8859-13'; @@ -1059,7 +590,7 @@ function rcube_charset_convert($str, $from, $to=NULL) */ function rep_specialchars_output($str, $enctype='', $mode='', $newlines=TRUE) { - global $OUTPUT_TYPE, $OUTPUT; + global $OUTPUT; static $html_encode_arr = false; static $js_rep_table = false; static $xml_rep_table = false; @@ -1513,7 +1044,7 @@ function parse_attrib_string($str) */ function format_date($date, $format=NULL) { - global $CONFIG, $sess_user_lang; + global $CONFIG; $ts = NULL; @@ -1524,11 +1055,11 @@ function format_date($date, $format=NULL) while (($ts = @strtotime($date))===false) { // if we have a date in non-rfc format - // remove token from the end and try again + // remove token from the end and try again $d = explode(' ', $date); - array_pop($d); - if (!$d) break; - $date = implode(' ', $d); + array_pop($d); + if (!$d) break; + $date = implode(' ', $d); } } @@ -1627,7 +1158,7 @@ function console($msg) if (!($GLOBALS['CONFIG']['debug_level'] & 4)) write_log('console', $msg); - else if ($GLOBALS['REMOTE_REQUEST']) + else if ($GLOBALS['OUTPUT']->ajax_call) print "/*\n $msg \n*/\n"; else { diff --git a/program/include/rcmail.php b/program/include/rcmail.php new file mode 100644 index 000000000..27ff18256 --- /dev/null +++ b/program/include/rcmail.php @@ -0,0 +1,462 @@ +<?php + +/* + +-----------------------------------------------------------------------+ + | program/include/rcmail.php | + | | + | This file is part of the RoundCube Webmail client | + | Copyright (C) 2008, RoundCube Dev. - Switzerland | + | Licensed under the GNU GPL | + | | + | PURPOSE: | + | Application class providing core functions and holding | + | instances of all 'global' objects like db- and imap-connections | + +-----------------------------------------------------------------------+ + | Author: Thomas Bruederli <roundcube@gmail.com> | + +-----------------------------------------------------------------------+ + + $Id: rcube_browser.php 328 2006-08-30 17:41:21Z thomasb $ + +*/ + + +/** + * Application class of RoundCube Webmail + * implemented as singleton + * + * @package Core + */ +class rcmail +{ + static public $main_tasks = array('mail','settings','addressbook','logout'); + + static private $instance; + + public $config; + public $user; + public $db; + public $imap; + public $output; + public $task = 'mail'; + public $action = ''; + public $comm_path = './'; + + private $texts; + + + /** + * This implements the 'singleton' design pattern + * + * @return object qvert The one and only instance + */ + static function get_instance() + { + if (!self::$instance) { + self::$instance = new rcmail(); + self::$instance->startup(); // init AFTER object was linked with self::$instance + } + + return self::$instance; + } + + + /** + * Private constructor + * + * @todo Remove global $CONFIG + */ + private function __construct() + { + // load configuration + $this->config = new rcube_config(); + $GLOBALS['CONFIG'] = $this->config->all(); + + register_shutdown_function(array($this, 'shutdown')); + } + + + /** + * Initial startup function + * to register session, create database and imap connections + * + * @todo Remove global vars $DB, $USER + */ + private function startup() + { + $config_all = $this->config->all(); + + // set task and action properties + $this->set_task(strip_quotes(get_input_value('_task', RCUBE_INPUT_GPC))); + $this->action = strip_quotes(get_input_value('_action', RCUBE_INPUT_GPC)); + + // connect to database + $GLOBALS['DB'] = $this->get_dbh(); + + // use database for storing session data + include_once('include/session.inc'); + + // set session domain + if (!empty($config_all['session_domain'])) { + ini_set('session.cookie_domain', $config_all['session_domain']); + } + // set session garbage collecting time according to session_lifetime + if (!empty($config_all['session_lifetime'])) { + ini_set('session.gc_maxlifetime', ($config_all['session_lifetime']) * 120); + } + + // start PHP session + session_start(); + + // set initial session vars + if (!isset($_SESSION['auth_time'])) { + $_SESSION['auth_time'] = time(); + $_SESSION['temp'] = true; + } + + + // create user object + $this->set_user(new rcube_user($_SESSION['user_id'])); + + // reset some session parameters when changing task + if ($_SESSION['task'] != $this->task) + unset($_SESSION['page']); + + // set current task to session + $_SESSION['task'] = $this->task; + + // create IMAP object + if ($this->task == 'mail') + $this->imap_init(); + } + + + /** + * Setter for application task + * + * @param string Task to set + */ + public function set_task($task) + { + if (!in_array($task, self::$main_tasks)) + $task = 'mail'; + + $this->task = $task; + $this->comm_path = './?_task=' . $task; + + if ($this->output) + $this->output->set_env('task', $task); + } + + + /** + * Setter for system user object + * + * @param object rcube_user Current user instance + */ + public function set_user($user) + { + if (is_object($user)) { + $this->user = $user; + $GLOBALS['USER'] = $this->user; + + // overwrite config with user preferences + $this->config->merge((array)$this->user->get_prefs()); + } + + $_SESSION['language'] = $this->user->language = $this->language_prop($this->config->get('language')); + + // set localization + setlocale(LC_ALL, $_SESSION['language']); + } + + + /** + * Check the given string and return a valid language code + * + * @param string Language code + * @return string Valid language code + */ + private function language_prop($lang) + { + static $rcube_languages, $rcube_language_aliases; + + if (empty($rcube_languages)) { + @include(INSTALL_PATH . 'program/localization/index.inc'); + } + + // check if we have an alias for that language + if (!isset($rcube_languages[$lang]) && isset($rcube_language_aliases[$lang])) { + $lang = $rcube_language_aliases[$lang]; + } + + // try the first two chars + if (!isset($rcube_languages[$lang]) && strlen($lang)>2) { + $lang = $this->language_prop(substr($lang, 0, 2)); + } + + if (!isset($rcube_languages[$lang])) { + $lang = 'en_US'; + } + + return $lang; + } + + + /** + * Get the current database connection + * + * @return object rcube_db Database connection object + */ + public function get_dbh() + { + if (!$this->db) { + $dbclass = "rcube_" . $this->config->get('db_backend', 'mdb2'); + $config_all = $this->config->all(); + + $this->db = new $dbclass($config_all['db_dsnw'], $config_all['db_dsnr'], $config_all['db_persistent']); + $this->db->sqlite_initials = INSTALL_PATH . 'SQL/sqlite.initial.sql'; + $this->db->set_debug((bool)$config_all['sql_debug']); + $this->db->db_connect('w'); + } + + return $this->db; + } + + + /** + * Init output object for GUI and add common scripts. + * This will instantiate a rcmail_template object and set + * environment vars according to the current session and configuration + */ + public function load_gui($framed = false) + { + // init output page + $this->output = new rcube_template($this->task, $framed); + + foreach (array('flag_for_deletion') as $js_config_var) { + $this->output->set_env($js_config_var, $this->config->get($js_config_var)); + } + + if ($framed) { + $this->comm_path .= '&_framed=1'; + $this->output->set_env('framed', true); + } + + $this->output->set_env('task', $this->task); + $this->output->set_env('action', $this->action); + $this->output->set_env('comm_path', $this->comm_path); + $this->output->set_charset($this->config->get('charset', RCMAIL_CHARSET)); + + // add some basic label to client + $this->output->add_label('loading'); + + return $this->output; + } + + + /** + * Create an output object for JSON responses + */ + public function init_json() + { + $this->output = new rcube_json_output($this->task); + + return $this->output; + } + + + /** + * Create global IMAP object and connect to server + * + * @param boolean True if connection should be established + * @todo Remove global $IMAP + */ + function imap_init($connect = false) + { + $this->imap = new rcube_imap($this->db); + $this->imap->debug_level = $this->config->get('debug_level'); + $this->imap->skip_deleted = $this->config->get('skip_deleted'); + + // connect with stored session data + if ($connect && $_SESSION['imap_host']) { + if (!($conn = $this->imap->connect($_SESSION['imap_host'], $_SESSION['username'], decrypt_passwd($_SESSION['password']), $_SESSION['imap_port'], $_SESSION['imap_ssl']))) + ; #$OUTPUT->show_message('imaperror', 'error'); + + $this->set_imap_prop(); + } + + // enable caching of imap data + if ($this->config->get('enable_caching')) { + $this->imap->set_caching(true); + } + + // set pagesize from config + $this->imap->set_pagesize($this->config->get('pagesize', 50)); + + // set global object for backward compatibility + $GLOBALS['IMAP'] = $this->imap; + } + + + /** + * Perfom login to the IMAP server and to the webmail service. + * This will also create a new user entry if auto_create_user is configured. + * + * @param string IMAP user name + * @param string IMAP password + * @param string IMAP host + * @return boolean True on success, False on failure + */ + function login($username, $pass, $host=NULL) + { + $user = NULL; + $config = $this->config->all(); + + if (!$host) + $host = $config['default_host']; + + // Validate that selected host is in the list of configured hosts + if (is_array($config['default_host'])) { + $allowed = false; + foreach ($config['default_host'] as $key => $host_allowed) { + if (!is_numeric($key)) + $host_allowed = $key; + if ($host == $host_allowed) { + $allowed = true; + break; + } + } + if (!$allowed) + return false; + } + else if (!empty($config['default_host']) && $host != $config['default_host']) + return false; + + // parse $host URL + $a_host = parse_url($host); + if ($a_host['host']) { + $host = $a_host['host']; + $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? $a_host['scheme'] : null; + $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : $config['default_port']); + } + else + $imap_port = $config['default_port']; + + + /* Modify username with domain if required + Inspired by Marco <P0L0_notspam_binware.org> + */ + // Check if we need to add domain + if (!empty($config['username_domain']) && !strpos($username, '@')) { + if (is_array($config['username_domain']) && isset($config['username_domain'][$host])) + $username .= '@'.$config['username_domain'][$host]; + else if (is_string($config['username_domain'])) + $username .= '@'.$config['username_domain']; + } + + // try to resolve email address from virtuser table + if (!empty($config['virtuser_file']) && strpos($username, '@')) + $username = rcube_user::email2user($username); + + // lowercase username if it's an e-mail address (#1484473) + if (strpos($username, '@')) + $username = strtolower($username); + + // user already registered -> overwrite username + if ($user = rcube_user::query($username, $host)) + $username = $user->data['username']; + + // exit if IMAP login failed + if (!($imap_login = $this->imap->connect($host, $username, $pass, $imap_port, $imap_ssl))) + return false; + + // user already registered -> update user's record + if (is_object($user)) { + $user->touch(); + } + // create new system user + else if ($config['auto_create_user']) { + if ($created = rcube_user::create($username, $host)) { + $user = $created; + + // get existing mailboxes (but why?) + // $a_mailboxes = $this->imap->list_mailboxes(); + } + } + else { + raise_error(array( + 'code' => 600, + 'type' => 'php', + 'file' => "config/main.inc.php", + 'message' => "Acces denied for new user $username. 'auto_create_user' is disabled" + ), true, false); + } + + // login succeeded + if (is_object($user) && $user->ID) { + $this->set_user($user); + + // set session vars + $_SESSION['user_id'] = $user->ID; + $_SESSION['username'] = $user->data['username']; + $_SESSION['imap_host'] = $host; + $_SESSION['imap_port'] = $imap_port; + $_SESSION['imap_ssl'] = $imap_ssl; + $_SESSION['password'] = encrypt_passwd($pass); + $_SESSION['login_time'] = mktime(); + + // force reloading complete list of subscribed mailboxes + $this->set_imap_prop(); + $this->imap->clear_cache('mailboxes'); + + if ($config['create_default_folders']) + $this->imap->create_default_folders(); + + return true; + } + + return false; + } + + + /** + * Set root dir and last stored mailbox + * This must be done AFTER connecting to the server! + */ + public function set_imap_prop() + { + $this->imap->set_charset($this->config->get('default_charset', RCMAIL_CHARSET)); + + // set root dir from config + if ($imap_root = $this->config->get('imap_root')) { + $this->imap->set_rootdir($imap_root); + } + if ($default_folders = $this->config->get('default_imap_folders')) { + $this->imap->set_default_mailboxes($default_folders); + } + if (!empty($_SESSION['mbox'])) { + $this->imap->set_mailbox($_SESSION['mbox']); + } + if (isset($_SESSION['page'])) { + $this->imap->set_page($_SESSION['page']); + } + } + + + public function shutdown() + { + if (is_object($this->imap)) { + $this->imap->close(); + $this->imap->write_cache(); + } + + if (is_object($this->contacts)) + $this->contacts->close(); + + // before closing the database connection, write session data + session_write_close(); + } + +} + + diff --git a/program/include/rcube_config.php b/program/include/rcube_config.php new file mode 100644 index 000000000..541767a00 --- /dev/null +++ b/program/include/rcube_config.php @@ -0,0 +1,152 @@ +<?php + +/* + +-----------------------------------------------------------------------+ + | program/include/rcube_config.php | + | | + | This file is part of the RoundCube Webmail client | + | Copyright (C) 2008, RoundCube Dev. - Switzerland | + | Licensed under the GNU GPL | + | | + | PURPOSE: | + | Class to read configuration settings | + | | + +-----------------------------------------------------------------------+ + | Author: Thomas Bruederli <roundcube@gmail.com> | + +-----------------------------------------------------------------------+ + + $Id: $ + +*/ + +/** + * Configuration class for RoundCube + * + * @package Core + */ +class rcube_config +{ + private $prop = array(); + + + /** + * Object constructor + */ + public function __construct() + { + $this->load(); + } + + + /** + * Load config from local config file + */ + private function load() + { + // start output buffering, we don't need any output yet, + // it'll be cleared after reading of config files, etc. + ob_start(); + + // load main config file + include_once(INSTALL_PATH . 'config/main.inc.php'); + $this->prop = (array)$rcmail_config; + + // load database config + include_once(INSTALL_PATH . 'config/db.inc.php'); + $this->prop += (array)$rcmail_config; + + // fix paths + $this->prop['skin_path'] = $this->prop['skin_path'] ? unslashify($this->prop['skin_path']) : 'skins/default'; + $this->prop['log_dir'] = $this->prop['log_dir'] ? unslashify($this->prop['log_dir']) : INSTALL_PATH . 'logs'; + + // handle aliases + if (isset($this->prop['locale_string']) && empty($this->prop['language'])) + $this->prop['language'] = $this->prop['locale_string']; + + // set PHP error logging according to config + if ($this->prop['debug_level'] & 1) { + ini_set('log_errors', 1); + ini_set('error_log', $this->prop['log_dir'] . '/errors'); + } + if ($this->prop['debug_level'] & 4) { + ini_set('display_errors', 1); + } + else { + ini_set('display_errors', 0); + } + + // clear output buffer + ob_end_clean(); + } + + + /** + * Load a host-specific config file if configured + * This will merge the host specific configuration with the given one + */ + private function load_host_config() + { + $fname = null; + + if (is_array($this->prop['include_host_config'])) { + $fname = $this->prop['include_host_config'][$_SERVER['HTTP_HOST']]; + } + else if (!empty($this->prop['include_host_config'])) { + $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php'; + } + + if ($fname && is_file(INSTALL_PATH . 'config/' . $fname)) { + include(INSTALL_PATH . 'config/' . $fname); + $this->prop = array_merge($this->prop, (array)$rcmail_config); + } + } + + + /** + * Getter for a specific config parameter + * + * @param string Parameter name + * @param mixed Default value if not set + * @return mixed The requested config value + */ + public function get($name, $def = null) + { + return isset($this->prop[$name]) ? $this->prop[$name] : $def; + } + + + /** + * Setter for a config parameter + * + * @param string Parameter name + * @param mixed Parameter value + */ + public function set($name, $value) + { + $this->prop[$name] = $value; + } + + + /** + * Override config options with the given values (eg. user prefs) + * + * @param array Hash array with config props to merge over + */ + public function merge($prefs) + { + $this->prop = array_merge($this->prop, $prefs); + } + + + /** + * Getter for all config options + * + * @return array Hash array containg all config properties + */ + public function all() + { + return $this->prop; + } + +} + diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php index 2c6d14297..4ac033a08 100644 --- a/program/include/rcube_imap.php +++ b/program/include/rcube_imap.php @@ -465,7 +465,7 @@ class rcube_imap if (!$force && is_array($a_mailbox_cache[$mailbox]) && isset($a_mailbox_cache[$mailbox][$mode])) return $a_mailbox_cache[$mailbox][$mode]; - // RECENT count is fetched abit different + // RECENT count is fetched a bit different if ($mode == 'RECENT') $count = iil_C_CheckForRecent($this->conn, $mailbox); diff --git a/program/include/rcube_json_output.php b/program/include/rcube_json_output.php index 802da2413..19a454321 100644 --- a/program/include/rcube_json_output.php +++ b/program/include/rcube_json_output.php @@ -41,10 +41,10 @@ class rcube_json_output /** * Constructor */ - public function __construct(&$config, $task) + public function __construct($task) { $this->task = $task; - $this->config = $config; + $this->config = rcmail::get_instance()->config; } diff --git a/program/include/rcube_shared.inc b/program/include/rcube_shared.inc index eed9662f0..03525b30f 100644 --- a/program/include/rcube_shared.inc +++ b/program/include/rcube_shared.inc @@ -91,9 +91,11 @@ function rcube_browser() */ function rcube_label($attrib) { - global $sess_user_lang, $OUTPUT; + global $OUTPUT; static $sa_text_data = false; static $s_language, $utf8_decode; + + $sess_user_lang = $_SESSION['language']; // extract attributes if (is_string($attrib)) diff --git a/program/include/rcube_template.php b/program/include/rcube_template.php index a76e378e1..df6c3ccfc 100755 --- a/program/include/rcube_template.php +++ b/program/include/rcube_template.php @@ -30,6 +30,7 @@ */ class rcube_template extends rcube_html_page { + var $app; var $config; var $task = ''; var $framed = false; @@ -45,13 +46,17 @@ class rcube_template extends rcube_html_page * Constructor * * @todo Use jQuery's $(document).ready() here. + * @todo Replace $this->config with the real rcube_config object */ - public function __construct(&$config, $task) + public function __construct($task, $framed = false) { parent::__construct(); + $this->app = rcmail::get_instance(); + $this->config = $this->app->config->all(); + + //$this->framed = $framed; $this->task = $task; - $this->config = $config; // add common javascripts $javascript = 'var '.JS_OBJECT_NAME.' = new rcube_webmail();'; @@ -348,7 +353,7 @@ class rcube_template extends rcube_html_page */ private function parse_with_globals($input) { - $GLOBALS['__comm_path'] = Q($GLOBALS['COMM_PATH']); + $GLOBALS['__comm_path'] = Q($this->app->comm_path); return preg_replace('/\$(__[a-z0-9_\-]+)/e', '$GLOBALS["\\1"]', $input); } @@ -609,7 +614,6 @@ class rcube_template extends rcube_html_page */ private function button($attrib) { - global $CONFIG, $OUTPUT, $MAIN_TASKS; static $sa_buttons = array(); static $s_button_count = 100; @@ -692,7 +696,7 @@ class rcube_template extends rcube_html_page )); // make valid href to specific buttons - if (in_array($attrib['command'], $MAIN_TASKS)) { + if (in_array($attrib['command'], rcmail::$main_tasks)) { $attrib['href'] = Q(rcmail_url(null, null, $attrib['command'])); } else if (in_array($attrib['command'], $a_static_commands)) { @@ -795,13 +799,35 @@ class rcube_template extends rcube_html_page /** + * Create a form tag with the necessary hidden fields + * + * @param array Named tag parameters + * @return string HTML code for the form + */ + public function form_tag($attrib, $content = null) + { + if ($this->framed) { + $hiddenfield = new html_hiddenfield(array('name' => '_framed', 'value' => '1')); + $hidden = $hiddenfield->show(); + } + + if (!$content) + $attrib['noclose'] = true; + + return html::tag('form', + $attrib + array('action' => "./", 'method' => "get"), + $hidden . $content); + } + + + /** * GUI object 'username' * Showing IMAP username of the current session * * @param array Named tag parameters (currently not used) * @return string HTML code for the gui object */ - static function current_username($attrib) + public function current_username($attrib) { global $USER; static $username; @@ -835,8 +861,7 @@ class rcube_template extends rcube_html_page */ private function login_form($attrib) { - global $CONFIG, $SESS_HIDDEN_FIELD; - $default_host = $CONFIG['default_host']; + $default_host = $this->config['default_host']; $_SESSION['temp'] = true; @@ -880,20 +905,12 @@ class rcube_template extends rcube_html_page $table->add(null, $input_host->show(get_input_value('_host', RCUVE_INPUT_POST))); } - $out = $SESS_HIDDEN_FIELD; - $out .= $input_action->show(); + $out = $input_action->show(); $out .= $table->show(); // surround html output with a form tag if (empty($attrib['form'])) { - $out = html::tag( - 'form', - array( - 'name' => $form_name, - 'action' => "./", - 'method' => "post" - ), - $out); + $out = $this->form_tag(array('name' => $form_name, 'method' => "post"), $out); } return $out; @@ -924,15 +941,11 @@ class rcube_template extends rcube_html_page // add form tag around text field if (empty($attrib['form'])) { - $out = html::tag( - 'form', - array( - 'name' => "rcmqsearchform", - 'action' => "./", - 'onsubmit' => JS_OBJECT_NAME . ".command('search');return false;", - 'style' => "display:inline", - ), - $out); + $out = $this->form_tag(array( + 'name' => "rcmqsearchform", + 'onsubmit' => JS_OBJECT_NAME . ".command('search');return false;", + 'style' => "display:inline"), + $out); } return $out; diff --git a/program/include/rcube_user.php b/program/include/rcube_user.php index e748758a9..e125f6309 100644 --- a/program/include/rcube_user.php +++ b/program/include/rcube_user.php @@ -29,8 +29,11 @@ */ class rcube_user { - var $ID = null; - var $data = null; + public $ID = null; + public $data = null; + public $language = 'en_US'; + + private $db = null; /** @@ -40,18 +43,19 @@ class rcube_user */ function __construct($id = null, $sql_arr = null) { - global $DB; + $this->db = rcmail::get_instance()->get_dbh(); if ($id && !$sql_arr) { - $sql_result = $DB->query("SELECT * FROM ".get_table_name('users')." WHERE user_id=?", $id); - $sql_arr = $DB->fetch_assoc($sql_result); + $sql_result = $this->db->query("SELECT * FROM ".get_table_name('users')." WHERE user_id=?", $id); + $sql_arr = $this->db->fetch_assoc($sql_result); } if (!empty($sql_arr)) { $this->ID = $sql_arr['user_id']; $this->data = $sql_arr; + $this->language = $sql_arr['language']; } } @@ -85,7 +89,7 @@ class rcube_user function get_prefs() { if ($this->ID && $this->data['preferences']) - return unserialize($this->data['preferences']); + return array('language' => $this->language) + unserialize($this->data['preferences']); else return array(); } @@ -99,26 +103,26 @@ class rcube_user */ function save_prefs($a_user_prefs) { - global $DB, $CONFIG, $sess_user_lang; - if (!$this->ID) return false; // merge (partial) prefs array with existing settings $a_user_prefs += (array)$this->get_prefs(); + unset($a_user_prefs['language']); - $DB->query( + $this->db->query( "UPDATE ".get_table_name('users')." SET preferences=?, language=? WHERE user_id=?", serialize($a_user_prefs), - $sess_user_lang, + $_SESSION['language'], $this->ID); - if ($DB->affected_rows()) + $this->language = $_SESSION['language']; + if ($this->db->affected_rows()) { - $CONFIG = array_merge($CONFIG, $a_user_prefs); + rcmail::get_instance()->config->merge($a_user_prefs); return true; } @@ -134,10 +138,8 @@ class rcube_user */ function get_identity($id = null) { - global $DB; - $sql_result = $this->list_identities($id ? sprintf('AND identity_id=%d', $id) : ''); - return $DB->fetch_assoc($sql_result); + return $this->db->fetch_assoc($sql_result); } @@ -148,15 +150,13 @@ class rcube_user */ function list_identities($sql_add = '') { - global $DB; - // get contacts from DB - $sql_result = $DB->query( + $sql_result = $this->db->query( "SELECT * FROM ".get_table_name('identities')." WHERE del<>1 AND user_id=? $sql_add - ORDER BY ".$DB->quoteIdentifier('standard')." DESC, name ASC", + ORDER BY ".$this->db->quoteIdentifier('standard')." DESC, name ASC", $this->ID); return $sql_result; @@ -172,8 +172,6 @@ class rcube_user */ function update_identity($iid, $data) { - global $DB; - if (!$this->ID) return false; @@ -182,11 +180,11 @@ class rcube_user foreach ((array)$data as $col => $value) { $write_sql[] = sprintf("%s=%s", - $DB->quoteIdentifier($col), - $DB->quote($value)); + $this->db->quoteIdentifier($col), + $this->db->quote($value)); } - $DB->query( + $this->db->query( "UPDATE ".get_table_name('identities')." SET ".join(', ', $write_sql)." WHERE identity_id=? @@ -195,7 +193,7 @@ class rcube_user $iid, $this->ID); - return $DB->affected_rows(); + return $this->db->affected_rows(); } @@ -207,25 +205,23 @@ class rcube_user */ function insert_identity($data) { - global $DB; - if (!$this->ID) return false; $insert_cols = $insert_values = array(); foreach ((array)$data as $col => $value) { - $insert_cols[] = $DB->quoteIdentifier($col); - $insert_values[] = $DB->quote($value); + $insert_cols[] = $this->db->quoteIdentifier($col); + $insert_values[] = $this->db->quote($value); } - $DB->query( + $this->db->query( "INSERT INTO ".get_table_name('identities')." (user_id, ".join(', ', $insert_cols).") VALUES (?, ".join(', ', $insert_values).")", $this->ID); - return $DB->insert_id(get_sequence_name('identities')); + return $this->db->insert_id(get_sequence_name('identities')); } @@ -237,24 +233,22 @@ class rcube_user */ function delete_identity($iid) { - global $DB; - if (!$this->ID) return false; if (!$this->ID || $this->ID == '') return false; - $sql_result = $DB->query("SELECT count(*) AS ident_count FROM " . + $sql_result = $this->db->query("SELECT count(*) AS ident_count FROM " . get_table_name('identities') . " WHERE user_id = ? AND del <> 1", $this->ID); - $sql_arr = $DB->fetch_assoc($sql_result); + $sql_arr = $this->db->fetch_assoc($sql_result); if ($sql_arr['ident_count'] <= 1) return false; - $DB->query( + $this->db->query( "UPDATE ".get_table_name('identities')." SET del=1 WHERE user_id=? @@ -262,7 +256,7 @@ class rcube_user $this->ID, $iid); - return $DB->affected_rows(); + return $this->db->affected_rows(); } @@ -273,13 +267,11 @@ class rcube_user */ function set_default($iid) { - global $DB; - if ($this->ID && $iid) { - $DB->query( + $this->db->query( "UPDATE ".get_table_name('identities')." - SET ".$DB->quoteIdentifier('standard')."='0' + SET ".$this->db->quoteIdentifier('standard')."='0' WHERE user_id=? AND identity_id<>? AND del<>1", @@ -294,13 +286,11 @@ class rcube_user */ function touch() { - global $DB; - if ($this->ID) { - $DB->query( + $this->db->query( "UPDATE ".get_table_name('users')." - SET last_login=".$DB->now()." + SET last_login=".$this->db->now()." WHERE user_id=?", $this->ID); } @@ -323,14 +313,13 @@ class rcube_user * @param string IMAP user name * @param string IMAP host name * @return object rcube_user New user instance - * @static */ - function query($user, $host) + static function query($user, $host) { - global $DB; + $dbh = rcmail::get_instance()->get_dbh(); // query if user already registered - $sql_result = $DB->query( + $sql_result = $dbh->query( "SELECT * FROM ".get_table_name('users')." WHERE mail_host=? AND (username=? OR alias=?)", $host, @@ -338,7 +327,7 @@ class rcube_user $user); // user already registered -> overwrite username - if ($sql_arr = $DB->fetch_assoc($sql_result)) + if ($sql_arr = $dbh->fetch_assoc($sql_result)) return new rcube_user($sql_arr['user_id'], $sql_arr); else return false; @@ -351,28 +340,27 @@ class rcube_user * @param string IMAP user name * @param string IMAP host * @return object rcube_user New user instance - * @static */ - function create($user, $host) + static function create($user, $host) { - global $DB, $CONFIG; - $user_email = ''; + $rcmail = rcmail::get_instance(); + $dbh = $rcmail->get_dbh(); // try to resolve user in virtusertable - if (!empty($CONFIG['virtuser_file']) && !strpos($user, '@')) + if ($rcmail->config->get('virtuser_file') && !strpos($user, '@')) $user_email = rcube_user::user2email($user); - $DB->query( + $dbh->query( "INSERT INTO ".get_table_name('users')." (created, last_login, username, mail_host, alias, language) - VALUES (".$DB->now().", ".$DB->now().", ?, ?, ?, ?)", + VALUES (".$dbh->now().", ".$dbh->now().", ?, ?, ?, ?)", strip_newlines($user), strip_newlines($host), strip_newlines($user_email), - $_SESSION['user_lang']); + $_SESSION['language']); - if ($user_id = $DB->insert_id(get_sequence_name('users'))) + if ($user_id = $dbh->insert_id(get_sequence_name('users'))) { $mail_domain = rcmail_mail_domain($host); @@ -382,13 +370,13 @@ class rcube_user $user_name = $user != $user_email ? $user : ''; // try to resolve the e-mail address from the virtuser table - if (!empty($CONFIG['virtuser_query']) && - ($sql_result = $DB->query(preg_replace('/%u/', $DB->escapeSimple($user), $CONFIG['virtuser_query']))) && - ($DB->num_rows()>0)) + if ($virtuser_query = $rcmail->config->get('virtuser_query') && + ($sql_result = $dbh->query(preg_replace('/%u/', $dbh->escapeSimple($user), $virtuser_query))) && + ($dbh->num_rows() > 0)) { - while ($sql_arr = $DB->fetch_array($sql_result)) + while ($sql_arr = $dbh->fetch_array($sql_result)) { - $DB->query( + $dbh->query( "INSERT INTO ".get_table_name('identities')." (user_id, del, standard, name, email) VALUES (?, 0, 1, ?, ?)", @@ -400,7 +388,7 @@ class rcube_user else { // also create new identity records - $DB->query( + $dbh->query( "INSERT INTO ".get_table_name('identities')." (user_id, del, standard, name, email) VALUES (?, 0, 1, ?, ?)", @@ -428,9 +416,8 @@ class rcube_user * * @param string E-mail address to resolve * @return string Resolved IMAP username - * @static */ - function email2user($email) + static function email2user($email) { $user = $email; $r = rcmail_findinvirtual("^$email"); @@ -455,9 +442,8 @@ class rcube_user * * @param string User name * @return string Resolved e-mail address - * @static */ - function user2email($user) + static function user2email($user) { $email = ""; $r = rcmail_findinvirtual("$user$"); @@ -479,4 +465,3 @@ class rcube_user } -?> |