| 43 | | global $sess_id, $sess_user_lang; |
| 44 | | global $CONFIG, $INSTALL_PATH, $BROWSER, $OUTPUT, $_SESSION, $IMAP, $DB; |
| 45 | | |
| 46 | | tfk_debug('Start me up.'); |
| 47 | | |
| 48 | | // check client |
| 49 | | $BROWSER = rcube_browser(); |
| 50 | | |
| 51 | | tfk_debug('// load browser'); |
| 52 | | |
| 53 | | // load configuration |
| 54 | | $CONFIG = rcmail_load_config(); |
| 55 | | |
| 56 | | tfk_debug('// load config'); |
| 57 | | |
| 58 | | // set session garbage collecting time according to session_lifetime |
| 59 | | if (empty($CONFIG['session_lifetime']) === FALSE) { |
| 60 | | ini_set('session.gc_maxlifetime', ($CONFIG['session_lifetime']) * 120); |
| 61 | | } |
| 62 | | // prepare DB connection |
| 63 | | require_once 'include/rcube_'.(empty($CONFIG['db_backend']) ? 'db' : $CONFIG['db_backend']).'.inc'; |
| 64 | | |
| 65 | | $DB = new rcube_db($CONFIG['db_dsnw'], $CONFIG['db_dsnr'], $CONFIG['db_persistent']); |
| 66 | | $DB->sqlite_initials = $INSTALL_PATH.'SQL/sqlite.initial.sql'; |
| 67 | | $DB->db_connect('w'); |
| 68 | | |
| 69 | | tfk_debug('// init db'); |
| 70 | | |
| 71 | | // use database for storing session data |
| 72 | | include_once 'include/session.inc'; |
| 73 | | |
| 74 | | // init session |
| 75 | | session_start(); |
| 76 | | $sess_id = session_id(); |
| 77 | | |
| 78 | | // create session and set session vars |
| 79 | | if (!isset($_SESSION['auth_time'])) { |
| 80 | | $_SESSION['user_lang'] = rcube_language_prop($CONFIG['locale_string']); |
| 81 | | $_SESSION['auth_time'] = time(); |
| 82 | | $_SESSION['temp'] = true; |
| 83 | | } |
| 84 | | |
| 85 | | // set session vars global |
| 86 | | $sess_user_lang = rcube_language_prop($_SESSION['user_lang']); |
| 87 | | |
| 88 | | |
| 89 | | // overwrite config with user preferences |
| 90 | | if (is_array($_SESSION['user_prefs'])) { |
| 91 | | $CONFIG = array_merge($CONFIG, $_SESSION['user_prefs']); |
| 92 | | } |
| 93 | | |
| 94 | | // reset some session parameters when changing task |
| 95 | | if ($_SESSION['task'] != $task) { |
| 96 | | unset($_SESSION['page']); |
| 97 | | } |
| 98 | | // set current task to session |
| 99 | | $_SESSION['task'] = $task; |
| 100 | | |
| 101 | | // create IMAP object |
| 102 | | if ($task=='mail') { |
| 103 | | rcmail_imap_init(); |
| 104 | | } |
| 105 | | |
| 106 | | // set localization |
| 107 | | if ($CONFIG['locale_string']) { |
| 108 | | setlocale(LC_ALL, $CONFIG['locale_string']); |
| 109 | | } |
| 110 | | else if ($sess_user_lang) { |
| 111 | | setlocale(LC_ALL, $sess_user_lang); |
| 112 | | } |
| 113 | | register_shutdown_function('rcmail_shutdown'); |
| 114 | | } |
| 115 | | |
| 116 | | |
| 117 | | // load roundcube configuration into global var |
| 118 | | function rcmail_load_config() |
| 119 | | { |
| 120 | | global $INSTALL_PATH; |
| 121 | | |
| 122 | | // load config file |
| 123 | | $status = @include_once 'config/main.inc.php'; |
| 124 | | if ($status === FALSE) { |
| 125 | | die('Could not open config/main.inc.php.'); |
| 126 | | } |
| 127 | | $conf = is_array($rcmail_config) ? $rcmail_config : array(); |
| 128 | | |
| 129 | | // load host-specific configuration |
| 130 | | rcmail_load_host_config($conf); |
| 131 | | |
| 132 | | $conf['skin_path'] = $conf['skin_path'] ? unslashify($conf['skin_path']) : 'skins/default'; |
| 133 | | |
| 134 | | // load db conf |
| 135 | | @include_once 'config/db.inc.php'; |
| 136 | | $conf = array_merge($conf, $rcmail_config); |
| 137 | | |
| 138 | | if (empty($conf['log_dir'])) { |
| 139 | | $conf['log_dir'] = $INSTALL_PATH.'logs'; |
| 140 | | } |
| 141 | | else { |
| 142 | | $conf['log_dir'] = unslashify($conf['log_dir']); |
| 143 | | } |
| 144 | | // set PHP error logging according to config |
| 145 | | if ($conf['debug_level'] & 1) { |
| 146 | | ini_set('log_errors', 1); |
| 147 | | ini_set('error_log', $conf['log_dir'].'/errors'); |
| 148 | | } |
| 149 | | if ($conf['debug_level'] & 4) { |
| 150 | | ini_set('display_errors', 1); |
| 151 | | } |
| 152 | | else { |
| 153 | | ini_set('display_errors', 0); |
| 154 | | } |
| 155 | | return $conf; |
| 156 | | } |
| 157 | | |
| 158 | | |
| 159 | | // load a host-specific config file if configured |
| 160 | | function rcmail_load_host_config(&$config) |
| 161 | | { |
| 162 | | $fname = NULL; |
| 163 | | |
| 164 | | if (is_array($config['include_host_config'])) { |
| 165 | | $fname = $config['include_host_config'][$_SERVER['HTTP_HOST']]; |
| 166 | | } |
| 167 | | else if (!empty($config['include_host_config'])) { |
| 168 | | $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php'; |
| 169 | | } |
| 170 | | if ($fname && is_file('config/'.$fname)) { |
| 171 | | include('config/'.$fname); |
| 172 | | $config = array_merge($config, $rcmail_config); |
| 173 | | } |
| 174 | | } |
| 175 | | |
| 176 | | |
| 177 | | // create authorization hash |
| 178 | | function rcmail_auth_hash($sess_id, $ts) |
| 179 | | { |
| 180 | | global $CONFIG; |
| 181 | | |
| 182 | | $auth_string = sprintf( |
| 183 | | 'rcmail*sess%sR%s*Chk:%s;%s', |
| 184 | | $sess_id, |
| 185 | | $ts, |
| 186 | | $CONFIG['ip_check'] ? $_SERVER['REMOTE_ADDR'] : '***.***.***.***', |
| 187 | | $_SERVER['HTTP_USER_AGENT'] |
| 188 | | ); |
| 189 | | |
| 190 | | if (function_exists('sha1')) { |
| 191 | | return sha1($auth_string); |
| 192 | | } |
| 193 | | return md5($auth_string); |
| 194 | | } |
| 195 | | |
| 196 | | |
| 197 | | // compare the auth hash sent by the client with the local session credentials |
| 198 | | function rcmail_authenticate_session() |
| 199 | | { |
| 200 | | global $CONFIG, $SESS_CLIENT_IP, $SESS_CHANGED; |
| 201 | | |
| 202 | | // advanced session authentication |
| 203 | | if ($CONFIG['double_auth']) { |
| 204 | | $now = time(); |
| 205 | | $valid = ($_COOKIE['sessauth'] == rcmail_auth_hash(session_id(), $_SESSION['auth_time']) || |
| 206 | | $_COOKIE['sessauth'] == rcmail_auth_hash(session_id(), $_SESSION['last_auth'])); |
| 207 | | |
| 208 | | // renew auth cookie every 5 minutes (only for GET requests) |
| 209 | | if (!$valid || ($_SERVER['REQUEST_METHOD']!='POST' && $now-$_SESSION['auth_time'] > 300)) { |
| 210 | | $_SESSION['last_auth'] = $_SESSION['auth_time']; |
| 211 | | $_SESSION['auth_time'] = $now; |
| 212 | | setcookie('sessauth', rcmail_auth_hash(session_id(), $now)); |
| 213 | | } |
| 214 | | } |
| 215 | | else { |
| 216 | | $valid = $CONFIG['ip_check'] ? $_SERVER['REMOTE_ADDR'] == $SESS_CLIENT_IP : true; |
| 217 | | } |
| 218 | | // check session filetime |
| 219 | | if (!empty($CONFIG['session_lifetime']) && isset($SESS_CHANGED) && $SESS_CHANGED + $CONFIG['session_lifetime']*60 < time()) { |
| 220 | | $valid = false; |
| 221 | | } |
| 222 | | return $valid; |
| 223 | | } |
| 224 | | |
| 225 | | |
| 226 | | // create IMAP object and connect to server |
| 227 | | function rcmail_imap_init($connect=FALSE) |
| 228 | | { |
| 229 | | global $CONFIG, $DB, $IMAP, $OUTPUT; |
| 230 | | |
| 231 | | $IMAP = new rcube_imap($DB); |
| 232 | | $IMAP->debug_level = $CONFIG['debug_level']; |
| 233 | | $IMAP->skip_deleted = $CONFIG['skip_deleted']; |
| 234 | | |
| 235 | | |
| 236 | | // connect with stored session data |
| 237 | | if ($connect) { |
| 238 | | $conn = $IMAP->connect( |
| 239 | | $_SESSION['imap_host'], |
| 240 | | $_SESSION['username'], |
| 241 | | decrypt_passwd($_SESSION['password']), |
| 242 | | $_SESSION['imap_port'], |
| 243 | | $_SESSION['imap_ssl'] |
| | 34 | |
| | 35 | private function __construct() |
| | 36 | { |
| | 37 | |
| | 38 | } |
| | 39 | /** |
| | 40 | * tfk_debug |
| | 41 | * |
| | 42 | * @param string $str |
| | 43 | * @ignore |
| | 44 | */ |
| | 45 | static function tfk_debug($str) |
| | 46 | { |
| | 47 | $str = "\n\n" . @date('Y-m-d H:i:s') . "\n" . $str; |
| | 48 | $fp = @fopen(dirname(__FILE__) . '/../../logs/debug.tfk', 'a'); |
| | 49 | if ($fp !== false) { |
| | 50 | @fwrite($fp, $str); |
| | 51 | @fclose($fp); |
| | 52 | } else { |
| | 53 | die('Could not open logs/debug.tfk.'); |
| | 54 | } |
| | 55 | } |
| | 56 | |
| | 57 | /** |
| | 58 | * register session and connect to server |
| | 59 | * |
| | 60 | * @access public |
| | 61 | * @todo Remove this include from here. This is not good for bytecode caching. |
| | 62 | * @param string $task |
| | 63 | */ |
| | 64 | static function rcmail_startup($task = 'mail') |
| | 65 | { |
| | 66 | //global $sess_id, $sess_user_lang; |
| | 67 | //global $CONFIG, $INSTALL_PATH, $BROWSER, $OUTPUT, $_SESSION, $IMAP, $DB; |
| | 68 | |
| | 69 | $registry = rc_registry::getInstance(); |
| | 70 | $INSTALL_PATH = $registry->get('INSTALL_PATH', 'core'); |
| | 71 | |
| | 72 | self::tfk_debug('Start me up.'); |
| | 73 | |
| | 74 | // check client |
| | 75 | $BROWSER = rcube_browser(); |
| | 76 | |
| | 77 | self::tfk_debug('// load browser'); |
| | 78 | |
| | 79 | // load configuration |
| | 80 | $CONFIG = self::rcmail_load_config(); |
| | 81 | $registry->set('CONFIG', $CONFIG, 'core'); |
| | 82 | |
| | 83 | self::tfk_debug('// load config'); |
| | 84 | |
| | 85 | // set session garbage collecting time according to session_lifetime |
| | 86 | if (empty($CONFIG['session_lifetime']) === FALSE) { |
| | 87 | ini_set('session.gc_maxlifetime', ($CONFIG['session_lifetime']) * 120); |
| | 88 | } |
| | 89 | // prepare DB connection |
| | 90 | require_once 'include/rcube_'.(empty($CONFIG['db_backend']) ? 'db' : $CONFIG['db_backend']).'.inc'; |
| | 91 | |
| | 92 | $DB = new rcube_db($CONFIG['db_dsnw'], $CONFIG['db_dsnr'], $CONFIG['db_persistent']); |
| | 93 | $DB->sqlite_initials = $INSTALL_PATH.'SQL/sqlite.initial.sql'; |
| | 94 | $DB->db_connect('w'); |
| | 95 | |
| | 96 | $registry->set('DB', $DB, 'core'); |
| | 97 | |
| | 98 | self::tfk_debug('// init db'); |
| | 99 | |
| | 100 | // use database for storing session data |
| | 101 | include_once 'include/session.inc'; |
| | 102 | |
| | 103 | // init session |
| | 104 | session_start(); |
| | 105 | $sess_id = session_id(); |
| | 106 | |
| | 107 | // create session and set session vars |
| | 108 | if (!isset($_SESSION['auth_time'])) { |
| | 109 | $_SESSION['user_lang'] = self::rcube_language_prop($CONFIG['locale_string']); |
| | 110 | $_SESSION['auth_time'] = time(); |
| | 111 | $_SESSION['temp'] = true; |
| | 112 | } |
| | 113 | |
| | 114 | // set session vars global |
| | 115 | $sess_user_lang = self::rcube_language_prop($_SESSION['user_lang']); |
| | 116 | |
| | 117 | |
| | 118 | // overwrite config with user preferences |
| | 119 | if (is_array($_SESSION['user_prefs'])) { |
| | 120 | $CONFIG = array_merge($CONFIG, $_SESSION['user_prefs']); |
| | 121 | } |
| | 122 | |
| | 123 | // reset some session parameters when changing task |
| | 124 | if ($_SESSION['task'] != $task) { |
| | 125 | unset($_SESSION['page']); |
| | 126 | } |
| | 127 | // set current task to session |
| | 128 | $_SESSION['task'] = $task; |
| | 129 | |
| | 130 | // create IMAP object |
| | 131 | if ($task=='mail') { |
| | 132 | self::rcmail_imap_init(); |
| | 133 | } |
| | 134 | |
| | 135 | // set localization |
| | 136 | if ($CONFIG['locale_string']) { |
| | 137 | setlocale(LC_ALL, $CONFIG['locale_string']); |
| | 138 | } |
| | 139 | else if ($sess_user_lang) { |
| | 140 | setlocale(LC_ALL, $sess_user_lang); |
| | 141 | } |
| | 142 | register_shutdown_function(array('rc_main', 'rcmail_shutdown')); |
| | 143 | |
| | 144 | $registry->set('sess_id', $sess_id, 'core'); |
| | 145 | $registry->set('sess_user_lang', $sess_user_lang, 'core'); |
| | 146 | $registry->set('CONFIG', $CONFIG, 'core'); |
| | 147 | $registry->set('BROWSER', $BROWSER, 'core'); |
| | 148 | |
| | 149 | //global $OUTPUT, $IMAP |
| | 150 | } |
| | 151 | |
| | 152 | |
| | 153 | // load roundcube configuration into global var |
| | 154 | static function rcmail_load_config() |
| | 155 | { |
| | 156 | $registry = rc_registry::getInstance(); |
| | 157 | $INSTALL_PATH = $registry->get('INSTALL_PATH', 'core'); |
| | 158 | |
| | 159 | // load config file |
| | 160 | $status = @include_once 'config/main.inc.php'; |
| | 161 | if ($status === FALSE) { |
| | 162 | die('Could not open config/main.inc.php.'); |
| | 163 | } |
| | 164 | $conf = is_array($rcmail_config) ? $rcmail_config : array(); |
| | 165 | |
| | 166 | // load host-specific configuration |
| | 167 | $conf = self::rcmail_load_host_config($conf); |
| | 168 | |
| | 169 | $conf['skin_path'] = $conf['skin_path'] ? unslashify($conf['skin_path']) : 'skins/default'; |
| | 170 | |
| | 171 | // load db conf |
| | 172 | @include_once 'config/db.inc.php'; |
| | 173 | $conf = array_merge($conf, $rcmail_config); |
| | 174 | |
| | 175 | if (empty($conf['log_dir'])) { |
| | 176 | $conf['log_dir'] = $INSTALL_PATH . 'logs'; |
| | 177 | } |
| | 178 | else { |
| | 179 | $conf['log_dir'] = unslashify($conf['log_dir']); |
| | 180 | } |
| | 181 | // set PHP error logging according to config |
| | 182 | if ($conf['debug_level'] & 1) { |
| | 183 | ini_set('log_errors', 1); |
| | 184 | ini_set('error_log', $conf['log_dir'].'/errors'); |
| | 185 | } |
| | 186 | if ($conf['debug_level'] & 4) { |
| | 187 | ini_set('display_errors', 1); |
| | 188 | } |
| | 189 | else { |
| | 190 | ini_set('display_errors', 0); |
| | 191 | } |
| | 192 | return $conf; |
| | 193 | } |
| | 194 | |
| | 195 | |
| | 196 | // load a host-specific config file if configured |
| | 197 | static function rcmail_load_host_config($config) |
| | 198 | { |
| | 199 | $fname = NULL; |
| | 200 | |
| | 201 | if (is_array($config['include_host_config'])) { |
| | 202 | $fname = $config['include_host_config'][$_SERVER['HTTP_HOST']]; |
| | 203 | } |
| | 204 | else if (!empty($config['include_host_config'])) { |
| | 205 | $fname = preg_replace('/[^a-z0-9\.\-_]/i', '', $_SERVER['HTTP_HOST']) . '.inc.php'; |
| | 206 | } |
| | 207 | if ($fname && is_file('config/'.$fname)) { |
| | 208 | include('config/'.$fname); |
| | 209 | $config = array_merge($config, $rcmail_config); |
| | 210 | } |
| | 211 | return $config; |
| | 212 | } |
| | 213 | |
| | 214 | |
| | 215 | // create authorization hash |
| | 216 | static function rcmail_auth_hash($sess_id, $ts) |
| | 217 | { |
| | 218 | $registry = rc_registry::getInstance(); |
| | 219 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 220 | |
| | 221 | $auth_string = sprintf( |
| | 222 | 'rcmail*sess%sR%s*Chk:%s;%s', |
| | 223 | $sess_id, |
| | 224 | $ts, |
| | 225 | $CONFIG['ip_check'] ? $_SERVER['REMOTE_ADDR'] : '***.***.***.***', |
| | 226 | $_SERVER['HTTP_USER_AGENT'] |
| 245 | | if ($conn === false) { |
| 246 | | $OUTPUT->show_message('imaperror', 'error'); |
| 247 | | } |
| 248 | | rcmail_set_imap_prop(); |
| 249 | | } |
| 250 | | |
| 251 | | // enable caching of imap data |
| 252 | | if ($CONFIG['enable_caching']===TRUE) { |
| 253 | | $IMAP->set_caching(TRUE); |
| 254 | | } |
| 255 | | // set pagesize from config |
| 256 | | if (isset($CONFIG['pagesize'])) { |
| 257 | | $IMAP->set_pagesize($CONFIG['pagesize']); |
| 258 | | } |
| 259 | | } |
| 260 | | |
| 261 | | |
| 262 | | // set root dir and last stored mailbox |
| 263 | | // this must be done AFTER connecting to the server |
| 264 | | function rcmail_set_imap_prop() |
| 265 | | { |
| 266 | | global $CONFIG, $IMAP; |
| 267 | | |
| 268 | | // set root dir from config |
| 269 | | if (!empty($CONFIG['imap_root'])) { |
| 270 | | $IMAP->set_rootdir($CONFIG['imap_root']); |
| 271 | | } |
| 272 | | if (is_array($CONFIG['default_imap_folders'])) { |
| 273 | | $IMAP->set_default_mailboxes($CONFIG['default_imap_folders']); |
| 274 | | } |
| 275 | | if (!empty($_SESSION['mbox'])) { |
| 276 | | $IMAP->set_mailbox($_SESSION['mbox']); |
| 277 | | } |
| 278 | | if (isset($_SESSION['page'])) { |
| 279 | | $IMAP->set_page($_SESSION['page']); |
| 280 | | } |
| 281 | | } |
| 282 | | |
| 283 | | |
| 284 | | // do these things on script shutdown |
| 285 | | function rcmail_shutdown() |
| 286 | | { |
| 287 | | global $IMAP; |
| 288 | | |
| 289 | | if (is_object($IMAP)) { |
| 290 | | $IMAP->close(); |
| 291 | | $IMAP->write_cache(); |
| 292 | | } |
| 293 | | |
| 294 | | // before closing the database connection, write session data |
| 295 | | session_write_close(); |
| 296 | | } |
| 297 | | |
| 298 | | // destroy session data and remove cookie |
| 299 | | function rcmail_kill_session() |
| 300 | | { |
| 301 | | // save user preferences |
| 302 | | $a_user_prefs = $_SESSION['user_prefs']; |
| 303 | | if (!is_array($a_user_prefs)) { |
| 304 | | $a_user_prefs = array(); |
| 305 | | } |
| 306 | | if ( |
| 307 | | ( |
| 308 | | isset($_SESSION['sort_col']) |
| 309 | | && $_SESSION['sort_col'] != $a_user_prefs['message_sort_col'] |
| 310 | | ) |
| 311 | | || |
| 312 | | ( |
| 313 | | isset($_SESSION['sort_order']) |
| 314 | | && $_SESSION['sort_order'] != $a_user_prefs['message_sort_order'] |
| 315 | | ) |
| 316 | | ) { |
| 317 | | $a_user_prefs['message_sort_col'] = $_SESSION['sort_col']; |
| 318 | | $a_user_prefs['message_sort_order'] = $_SESSION['sort_order']; |
| 319 | | rcmail_save_user_prefs($a_user_prefs); |
| 320 | | } |
| 321 | | |
| 322 | | $_SESSION = array( |
| 323 | | 'user_lang' => $GLOBALS['sess_user_lang'], |
| 324 | | 'auth_time' => time(), |
| 325 | | 'temp' => true |
| 326 | | ); |
| 327 | | setcookie('sessauth', '-del-', time()-60); |
| 328 | | } |
| 329 | | |
| 330 | | |
| 331 | | // return correct name for a specific database table |
| 332 | | function get_table_name($table) |
| 333 | | { |
| 334 | | global $CONFIG; |
| 335 | | |
| 336 | | // return table name if configured |
| 337 | | $config_key = 'db_table_'.$table; |
| 338 | | |
| 339 | | if (strlen($CONFIG[$config_key])) { |
| 340 | | return $CONFIG[$config_key]; |
| 341 | | } |
| 342 | | return $table; |
| 343 | | } |
| 344 | | |
| 345 | | |
| 346 | | // return correct name for a specific database sequence |
| 347 | | // (used for Postres only) |
| 348 | | function get_sequence_name($sequence) |
| 349 | | { |
| 350 | | global $CONFIG; |
| 351 | | |
| 352 | | // return table name if configured |
| 353 | | $config_key = 'db_sequence_'.$sequence; |
| 354 | | |
| 355 | | if (strlen($CONFIG[$config_key])) { |
| 356 | | return $CONFIG[$config_key]; |
| 357 | | } |
| 358 | | return $table; |
| 359 | | } |
| 360 | | |
| 361 | | |
| 362 | | // check the given string and returns language properties |
| 363 | | function rcube_language_prop($lang, $prop='lang') |
| 364 | | { |
| 365 | | global $INSTALL_PATH; |
| 366 | | static $rcube_languages, $rcube_language_aliases, $rcube_charsets; |
| 367 | | |
| 368 | | if (empty($rcube_languages)) { |
| 369 | | @include($INSTALL_PATH.'program/localization/index.inc'); |
| 370 | | } |
| 371 | | // check if we have an alias for that language |
| 372 | | if (!isset($rcube_languages[$lang]) && isset($rcube_language_aliases[$lang])) { |
| 373 | | $lang = $rcube_language_aliases[$lang]; |
| 374 | | } |
| 375 | | // try the first two chars |
| 376 | | if (!isset($rcube_languages[$lang]) && strlen($lang)>2) { |
| 377 | | $lang = substr($lang, 0, 2); |
| 378 | | $lang = rcube_language_prop($lang); |
| 379 | | } |
| 380 | | |
| 381 | | if (!isset($rcube_languages[$lang])) { |
| 382 | | $lang = 'en_US'; |
| 383 | | } |
| 384 | | // language has special charset configured |
| 385 | | if (isset($rcube_charsets[$lang])) { |
| 386 | | $charset = $rcube_charsets[$lang]; |
| 387 | | } |
| 388 | | else { |
| 389 | | $charset = 'UTF-8'; |
| 390 | | } |
| 391 | | if ($prop=='charset') { |
| 392 | | return $charset; |
| 393 | | } |
| 394 | | return $lang; |
| 395 | | } |
| 396 | | |
| 397 | | |
| 398 | | // init output object for GUI and add common scripts |
| 399 | | function rcmail_load_gui() |
| 400 | | { |
| 401 | | global $CONFIG, $OUTPUT, $sess_user_lang; |
| 402 | | |
| 403 | | // init output page |
| 404 | | $OUTPUT = new rcmail_template($CONFIG, $GLOBALS['_task']); |
| 405 | | $OUTPUT->set_env('comm_path', $GLOBALS['COMM_PATH']); |
| 406 | | |
| 407 | | if (is_array($CONFIG['javascript_config'])) { |
| 408 | | foreach ($CONFIG['javascript_config'] as $js_config_var) { |
| 409 | | $OUTPUT->set_env($js_config_var, $CONFIG[$js_config_var]); |
| 410 | | } |
| 411 | | } |
| 412 | | |
| 413 | | if (!empty($GLOBALS['_framed'])) { |
| 414 | | $OUTPUT->set_env('framed', true); |
| 415 | | } |
| 416 | | // set locale setting |
| 417 | | rcmail_set_locale($sess_user_lang); |
| 418 | | |
| 419 | | // set user-selected charset |
| 420 | | if (!empty($CONFIG['charset'])) { |
| 421 | | $OUTPUT->set_charset($CONFIG['charset']); |
| 422 | | } |
| 423 | | // register common UI objects |
| 424 | | $OUTPUT->add_handlers( |
| 425 | | array( |
| 426 | | 'loginform' => 'rcmail_login_form', |
| 427 | | 'username' => 'rcmail_current_username', |
| 428 | | 'message' => 'rcmail_message_container', |
| 429 | | 'charsetselector' => 'rcmail_charset_selector', |
| | 228 | |
| | 229 | if (function_exists('sha1')) { |
| | 230 | return sha1($auth_string); |
| | 231 | } |
| | 232 | return md5($auth_string); |
| | 233 | } |
| | 234 | |
| | 235 | |
| | 236 | // compare the auth hash sent by the client with the local session credentials |
| | 237 | function rcmail_authenticate_session() |
| | 238 | { |
| | 239 | $registry = rc_registry::getInstance(); |
| | 240 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 241 | $SESS_CLIENT_IP = $registry->get('SESS_CLIENT_IP', 'core'); |
| | 242 | $SESS_CHANGED = $registry->get('SESS_CHANGED', 'core'); |
| | 243 | |
| | 244 | // advanced session authentication |
| | 245 | if ($CONFIG['double_auth']) { |
| | 246 | $now = time(); |
| | 247 | $valid = ($_COOKIE['sessauth'] == self::rcmail_auth_hash(session_id(), $_SESSION['auth_time']) || |
| | 248 | $_COOKIE['sessauth'] == self::rcmail_auth_hash(session_id(), $_SESSION['last_auth'])); |
| | 249 | |
| | 250 | // renew auth cookie every 5 minutes (only for GET requests) |
| | 251 | if (!$valid || ($_SERVER['REQUEST_METHOD']!='POST' && $now-$_SESSION['auth_time'] > 300)) { |
| | 252 | $_SESSION['last_auth'] = $_SESSION['auth_time']; |
| | 253 | $_SESSION['auth_time'] = $now; |
| | 254 | setcookie('sessauth', self::rcmail_auth_hash(session_id(), $now)); |
| | 255 | } |
| | 256 | } |
| | 257 | else { |
| | 258 | $valid = $CONFIG['ip_check'] ? $_SERVER['REMOTE_ADDR'] == $SESS_CLIENT_IP : true; |
| | 259 | } |
| | 260 | // check session filetime |
| | 261 | if (!empty($CONFIG['session_lifetime']) && isset($SESS_CHANGED) && $SESS_CHANGED + $CONFIG['session_lifetime']*60 < time()) { |
| | 262 | $valid = false; |
| | 263 | } |
| | 264 | return $valid; |
| | 265 | } |
| | 266 | |
| | 267 | |
| | 268 | // create IMAP object and connect to server |
| | 269 | static function rcmail_imap_init($connect=FALSE) |
| | 270 | { |
| | 271 | $registry = rc_registry::getInstance(); |
| | 272 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 273 | $DB = $registry->get('DB', 'core'); |
| | 274 | $OUTPUT = $registry->get('OUTPUT', 'core'); |
| | 275 | |
| | 276 | $IMAP = new rcube_imap($DB); |
| | 277 | $IMAP->debug_level = $CONFIG['debug_level']; |
| | 278 | $IMAP->skip_deleted = $CONFIG['skip_deleted']; |
| | 279 | |
| | 280 | // connect with stored session data |
| | 281 | if ($connect) { |
| | 282 | $conn = $IMAP->connect( |
| | 283 | $_SESSION['imap_host'], |
| | 284 | $_SESSION['username'], |
| | 285 | decrypt_passwd($_SESSION['password']), |
| | 286 | $_SESSION['imap_port'], |
| | 287 | $_SESSION['imap_ssl'] |
| | 288 | ); |
| | 289 | $registry->set('IMAP', $IMAP, 'core'); |
| | 290 | if ($conn === false) { |
| | 291 | $OUTPUT->show_message('imaperror', 'error'); |
| | 292 | } |
| | 293 | self::rcmail_set_imap_prop(); |
| | 294 | } |
| | 295 | |
| | 296 | // enable caching of imap data |
| | 297 | if ($CONFIG['enable_caching']===TRUE) { |
| | 298 | $IMAP->set_caching(TRUE); |
| | 299 | } |
| | 300 | // set pagesize from config |
| | 301 | if (isset($CONFIG['pagesize'])) { |
| | 302 | $IMAP->set_pagesize($CONFIG['pagesize']); |
| | 303 | } |
| | 304 | $registry->set('IMAP', $IMAP, 'core'); |
| | 305 | } |
| | 306 | |
| | 307 | |
| | 308 | // set root dir and last stored mailbox |
| | 309 | // this must be done AFTER connecting to the server |
| | 310 | static function rcmail_set_imap_prop() |
| | 311 | { |
| | 312 | $registry = rc_registry::getInstance(); |
| | 313 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 314 | $IMAP = $registry->get('IMAP', 'core'); |
| | 315 | |
| | 316 | // set root dir from config |
| | 317 | if (!empty($CONFIG['imap_root'])) { |
| | 318 | $IMAP->set_rootdir($CONFIG['imap_root']); |
| | 319 | } |
| | 320 | if (is_array($CONFIG['default_imap_folders'])) { |
| | 321 | $IMAP->set_default_mailboxes($CONFIG['default_imap_folders']); |
| | 322 | } |
| | 323 | if (!empty($_SESSION['mbox'])) { |
| | 324 | $IMAP->set_mailbox($_SESSION['mbox']); |
| | 325 | } |
| | 326 | if (isset($_SESSION['page'])) { |
| | 327 | $IMAP->set_page($_SESSION['page']); |
| | 328 | } |
| | 329 | $registry->set('IMAP', $IMAP, 'core'); |
| | 330 | } |
| | 331 | |
| | 332 | |
| | 333 | // do these things on script shutdown |
| | 334 | static function rcmail_shutdown() |
| | 335 | { |
| | 336 | $registry = rc_registry::getInstance(); |
| | 337 | $IMAP = $registry->get('IMAP', 'core'); |
| | 338 | |
| | 339 | if (is_object($IMAP)) { |
| | 340 | $IMAP->close(); |
| | 341 | $IMAP->write_cache(); |
| | 342 | } |
| | 343 | |
| | 344 | // before closing the database connection, write session data |
| | 345 | session_write_close(); |
| | 346 | } |
| | 347 | |
| | 348 | // destroy session data and remove cookie |
| | 349 | static function rcmail_kill_session() |
| | 350 | { |
| | 351 | // save user preferences |
| | 352 | $a_user_prefs = $_SESSION['user_prefs']; |
| | 353 | if (!is_array($a_user_prefs)) { |
| | 354 | $a_user_prefs = array(); |
| | 355 | } |
| | 356 | if ( |
| | 357 | ( |
| | 358 | isset($_SESSION['sort_col']) |
| | 359 | && $_SESSION['sort_col'] != $a_user_prefs['message_sort_col'] |
| 431 | | ); |
| 432 | | |
| 433 | | // add some basic label to client |
| 434 | | if (!$OUTPUT->ajax_call) { |
| 435 | | rcube_add_label('loading'); |
| 436 | | } |
| 437 | | } |
| 438 | | |
| 439 | | |
| 440 | | // set localization charset based on the given language |
| 441 | | function rcmail_set_locale($lang) |
| 442 | | { |
| 443 | | global $OUTPUT, $MBSTRING; |
| 444 | | static $s_mbstring_loaded = NULL; |
| 445 | | |
| 446 | | // settings for mbstring module (by Tadashi Jokagi) |
| 447 | | if (is_null($s_mbstring_loaded)) { |
| 448 | | $MBSTRING = $s_mbstring_loaded = extension_loaded("mbstring"); |
| 449 | | } |
| 450 | | else { |
| 451 | | $MBSTRING = $s_mbstring_loaded = FALSE; |
| 452 | | } |
| 453 | | if ($MBSTRING) { |
| 454 | | mb_internal_encoding(RCMAIL_CHARSET); |
| 455 | | } |
| 456 | | $OUTPUT->set_charset(rcube_language_prop($lang, 'charset')); |
| 457 | | } |
| 458 | | |
| 459 | | |
| 460 | | // auto-select IMAP host based on the posted login information |
| 461 | | function rcmail_autoselect_host() |
| 462 | | { |
| 463 | | global $CONFIG; |
| 464 | | |
| 465 | | $host = isset($_POST['_host']) ? get_input_value('_host', RCUBE_INPUT_POST) : $CONFIG['default_host']; |
| 466 | | if (is_array($host)) { |
| 467 | | list($user, $domain) = explode('@', get_input_value('_user', RCUBE_INPUT_POST)); |
| 468 | | if (!empty($domain)) { |
| 469 | | foreach ($host as $imap_host => $mail_domains) { |
| 470 | | if (is_array($mail_domains) && in_array($domain, $mail_domains)) { |
| 471 | | $host = $imap_host; |
| | 361 | || |
| | 362 | ( |
| | 363 | isset($_SESSION['sort_order']) |
| | 364 | && $_SESSION['sort_order'] != $a_user_prefs['message_sort_order'] |
| | 365 | ) |
| | 366 | ) { |
| | 367 | $a_user_prefs['message_sort_col'] = $_SESSION['sort_col']; |
| | 368 | $a_user_prefs['message_sort_order'] = $_SESSION['sort_order']; |
| | 369 | rcmail_save_user_prefs($a_user_prefs); |
| | 370 | } |
| | 371 | |
| | 372 | $_SESSION = array( |
| | 373 | 'user_lang' => $GLOBALS['sess_user_lang'], |
| | 374 | 'auth_time' => time(), |
| | 375 | 'temp' => true |
| | 376 | ); |
| | 377 | setcookie('sessauth', '-del-', time()-60); |
| | 378 | } |
| | 379 | |
| | 380 | |
| | 381 | // return correct name for a specific database table |
| | 382 | static function get_table_name($table) |
| | 383 | { |
| | 384 | $registry = rc_registry::getInstance(); |
| | 385 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 386 | |
| | 387 | // return table name if configured |
| | 388 | $config_key = 'db_table_'.$table; |
| | 389 | |
| | 390 | if (strlen($CONFIG[$config_key])) { |
| | 391 | return $CONFIG[$config_key]; |
| | 392 | } |
| | 393 | return $table; |
| | 394 | } |
| | 395 | |
| | 396 | |
| | 397 | // return correct name for a specific database sequence |
| | 398 | // (used for Postres only) |
| | 399 | static function get_sequence_name($sequence) |
| | 400 | { |
| | 401 | $registry = rc_registry::getInstance(); |
| | 402 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 403 | |
| | 404 | // return table name if configured |
| | 405 | $config_key = 'db_sequence_'.$sequence; |
| | 406 | |
| | 407 | if (strlen($CONFIG[$config_key])) { |
| | 408 | return $CONFIG[$config_key]; |
| | 409 | } |
| | 410 | return $table; |
| | 411 | } |
| | 412 | |
| | 413 | |
| | 414 | // check the given string and returns language properties |
| | 415 | static function rcube_language_prop($lang, $prop='lang') |
| | 416 | { |
| | 417 | $registry = rc_registry::getInstance(); |
| | 418 | $INSTALL_PATH = $registry->get('INSTALL_PATH', 'core'); |
| | 419 | $rcube_languages = $registry->get('rcube_languages', 'core'); |
| | 420 | $rcube_language_aliases = $registry->get('rcube_language_aliases', 'core'); |
| | 421 | $rcube_charsets = $registry->get('rcube_charsets', 'core'); |
| | 422 | |
| | 423 | if (empty($rcube_languages)) { |
| | 424 | $status = @include($INSTALL_PATH.'program/localization/index.inc'); |
| | 425 | if ($status === false) { |
| | 426 | self::tfk_debug("Couldn't include localization/index.inc"); |
| | 427 | } |
| | 428 | } |
| | 429 | // check if we have an alias for that language |
| | 430 | if (!isset($rcube_languages[$lang]) && isset($rcube_language_aliases[$lang])) { |
| | 431 | $lang = $rcube_language_aliases[$lang]; |
| | 432 | } |
| | 433 | // try the first two chars |
| | 434 | if (!isset($rcube_languages[$lang]) && strlen($lang)>2) { |
| | 435 | $registry->set('rcube_languages', $rcube_languages, 'core'); |
| | 436 | $registry->set('rcube_language_aliases', $rcube_language_aliases, 'core'); |
| | 437 | $registry->set('rcube_charsets', $rcube_charsets, 'core'); |
| | 438 | |
| | 439 | $lang = substr($lang, 0, 2); |
| | 440 | $lang = self::rcube_language_prop($lang); |
| | 441 | } |
| | 442 | |
| | 443 | if (!isset($rcube_languages[$lang])) { |
| | 444 | $lang = 'en_US'; |
| | 445 | } |
| | 446 | // language has special charset configured |
| | 447 | if (isset($rcube_charsets[$lang])) { |
| | 448 | $charset = $rcube_charsets[$lang]; |
| | 449 | } |
| | 450 | else { |
| | 451 | $charset = 'UTF-8'; |
| | 452 | } |
| | 453 | |
| | 454 | $registry->set('rcube_languages', $rcube_languages, 'core'); |
| | 455 | $registry->set('rcube_language_aliases', $rcube_language_aliases, 'core'); |
| | 456 | $registry->set('rcube_charsets', $rcube_charsets, 'core'); |
| | 457 | |
| | 458 | if ($prop=='charset') { |
| | 459 | return $charset; |
| | 460 | } |
| | 461 | |
| | 462 | return $lang; |
| | 463 | } |
| | 464 | |
| | 465 | |
| | 466 | // init output object for GUI and add common scripts |
| | 467 | static function rcmail_load_gui() |
| | 468 | { |
| | 469 | $registry = rc_registry::getInstance(); |
| | 470 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 471 | $sess_user_lang = $registry->get('sess_user_lang', 'core'); |
| | 472 | |
| | 473 | // init output page |
| | 474 | $OUTPUT = new rcmail_template($CONFIG, $GLOBALS['_task']); |
| | 475 | $OUTPUT->set_env('comm_path', $GLOBALS['COMM_PATH']); |
| | 476 | |
| | 477 | if (is_array($CONFIG['javascript_config'])) { |
| | 478 | foreach ($CONFIG['javascript_config'] as $js_config_var) { |
| | 479 | $OUTPUT->set_env($js_config_var, $CONFIG[$js_config_var]); |
| | 480 | } |
| | 481 | } |
| | 482 | |
| | 483 | if (!empty($GLOBALS['_framed'])) { |
| | 484 | $OUTPUT->set_env('framed', true); |
| | 485 | } |
| | 486 | $registry->set('OUTPUT', $OUTPUT, 'core'); |
| | 487 | // set locale setting |
| | 488 | self::rcmail_set_locale($sess_user_lang); |
| | 489 | |
| | 490 | // set user-selected charset |
| | 491 | if (!empty($CONFIG['charset'])) { |
| | 492 | $OUTPUT->set_charset($CONFIG['charset']); |
| | 493 | } |
| | 494 | $registry->set('OUTPUT', $OUTPUT, 'core'); |
| | 495 | // register common UI objects |
| | 496 | $OUTPUT->add_handlers( |
| | 497 | array( |
| | 498 | 'loginform' => 'rcmail_login_form', |
| | 499 | 'username' => 'rcmail_current_username', |
| | 500 | 'message' => 'rcmail_message_container', |
| | 501 | 'charsetselector' => 'rcmail_charset_selector', |
| | 502 | ) |
| | 503 | ); |
| | 504 | // add some basic label to client |
| | 505 | if (!$OUTPUT->ajax_call) { |
| | 506 | self::rcube_add_label('loading'); |
| | 507 | } |
| | 508 | } |
| | 509 | |
| | 510 | |
| | 511 | // set localization charset based on the given language |
| | 512 | static function rcmail_set_locale($lang) |
| | 513 | { |
| | 514 | $registry = rc_registry::getInstance(); |
| | 515 | $OUTPUT = $registry->get('OUTPUT', 'core'); |
| | 516 | $MBSTRING = $registry->get('MBSTRING', 'core'); |
| | 517 | $s_mbstring_loaded = $registry->get('s_mbstring_loaded', 'core'); |
| | 518 | |
| | 519 | // settings for mbstring module (by Tadashi Jokagi) |
| | 520 | if (is_null($s_mbstring_loaded)) { |
| | 521 | $MBSTRING = $s_mbstring_loaded = extension_loaded("mbstring"); |
| | 522 | } |
| | 523 | else { |
| | 524 | $MBSTRING = $s_mbstring_loaded = FALSE; |
| | 525 | } |
| | 526 | $registry->set('MBSTRING', $MBSTRING, 'core'); |
| | 527 | $registry->set('s_mbstring_loaded', $s_mbstring_loaded, 'core'); |
| | 528 | |
| | 529 | if ($MBSTRING) { |
| | 530 | mb_internal_encoding(RCMAIL_CHARSET); |
| | 531 | } |
| | 532 | $OUTPUT->set_charset(self::rcube_language_prop($lang, 'charset')); |
| | 533 | } |
| | 534 | |
| | 535 | |
| | 536 | // auto-select IMAP host based on the posted login information |
| | 537 | static function rcmail_autoselect_host() |
| | 538 | { |
| | 539 | $registry = rc_registry::getInstance(); |
| | 540 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 541 | |
| | 542 | $host = isset($_POST['_host']) ? self::get_input_value('_host', RCUBE_INPUT_POST) : $CONFIG['default_host']; |
| | 543 | if (is_array($host)) { |
| | 544 | list($user, $domain) = explode('@', self::get_input_value('_user', RCUBE_INPUT_POST)); |
| | 545 | if (!empty($domain)) { |
| | 546 | foreach ($host as $imap_host => $mail_domains) { |
| | 547 | if (is_array($mail_domains) && in_array($domain, $mail_domains)) { |
| | 548 | $host = $imap_host; |
| | 549 | break; |
| | 550 | } |
| | 551 | } |
| | 552 | } |
| | 553 | |
| | 554 | // take the first entry if $host is still an array |
| | 555 | if (is_array($host)) { |
| | 556 | $host = array_shift($host); |
| | 557 | } |
| | 558 | } |
| | 559 | return $host; |
| | 560 | } |
| | 561 | |
| | 562 | |
| | 563 | // perfom login to the IMAP server and to the webmail service |
| | 564 | static function rcmail_login($user, $pass, $host=NULL) |
| | 565 | { |
| | 566 | $user_id = NULL; |
| | 567 | |
| | 568 | $registry = rc_registry::getInstance(); |
| | 569 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 570 | $IMAP = $registry->get('IMAP', 'core'); |
| | 571 | $DB = $registry->get('DB', 'core'); |
| | 572 | $sess_user_lang = $registry->get('sess_user_lang', 'core'); |
| | 573 | |
| | 574 | if (is_null($host) === true) { |
| | 575 | $host = $CONFIG['default_host']; |
| | 576 | } |
| | 577 | // Validate that selected host is in the list of configured hosts |
| | 578 | if (is_array($CONFIG['default_host'])) { |
| | 579 | $allowed = FALSE; |
| | 580 | foreach ($CONFIG['default_host'] as $key => $host_allowed) { |
| | 581 | if (!is_numeric($key)) { |
| | 582 | $host_allowed = $key; |
| | 583 | } |
| | 584 | if ($host == $host_allowed) { |
| | 585 | $allowed = TRUE; |
| 475 | | } |
| 476 | | |
| 477 | | // take the first entry if $host is still an array |
| 478 | | if (is_array($host)) { |
| 479 | | $host = array_shift($host); |
| 480 | | } |
| 481 | | } |
| 482 | | return $host; |
| | 589 | if (!$allowed) { |
| | 590 | return FALSE; |
| | 591 | } |
| | 592 | } |
| | 593 | else if (!empty($CONFIG['default_host']) && $host != $CONFIG['default_host']) { |
| | 594 | return FALSE; |
| | 595 | } |
| | 596 | |
| | 597 | // parse $host URL |
| | 598 | $a_host = parse_url($host); |
| | 599 | if ($a_host['host']) { |
| | 600 | $host = $a_host['host']; |
| | 601 | $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE; |
| | 602 | $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : $CONFIG['default_port']); |
| | 603 | } |
| | 604 | else { |
| | 605 | $imap_port = $CONFIG['default_port']; |
| | 606 | } |
| | 607 | |
| | 608 | /** |
| | 609 | * Modify username with domain if required |
| | 610 | * Inspired by Marco <P0L0_notspam_binware.org> |
| | 611 | */ |
| | 612 | // Check if we need to add domain |
| | 613 | //tfk_debug('User #1: ' . $user); |
| | 614 | //tfk_debug('Host: ' . $CONFIG['username_domain']); |
| | 615 | if (!empty($CONFIG['username_domain']) && !strstr($user, '@')) { |
| | 616 | if (is_array($CONFIG['username_domain']) && isset($CONFIG['username_domain'][$host])) { |
| | 617 | $user .= '@' . $CONFIG['username_domain'][$host]; |
| | 618 | } |
| | 619 | else if (is_string($CONFIG['username_domain'])) { |
| | 620 | $user .= '@' . $CONFIG['username_domain']; |
| | 621 | } |
| | 622 | } |
| | 623 | //tfk_debug('User #2: ' . $user); |
| | 624 | |
| | 625 | // query if user already registered |
| | 626 | $_query = "SELECT user_id, username, language, preferences"; |
| | 627 | $_query.= " FROM " . self::get_table_name('users'); |
| | 628 | $_query.= " WHERE mail_host=?"; |
| | 629 | $_query.= " AND (username=? OR alias=?)"; |
| | 630 | |
| | 631 | $sql_result = $DB->query( |
| | 632 | $_query, |
| | 633 | $host, |
| | 634 | $user, |
| | 635 | $user |
| | 636 | ); |
| | 637 | if ($DB->db_error === true) { |
| | 638 | return FALSE; |
| | 639 | } |
| | 640 | // user already registered -> overwrite username |
| | 641 | if ($sql_arr = $DB->fetch_assoc($sql_result)) { |
| | 642 | $user_id = $sql_arr['user_id']; |
| | 643 | $user = $sql_arr['username']; |
| | 644 | } |
| | 645 | |
| | 646 | // try to resolve email address from virtuser table |
| | 647 | if (!empty($CONFIG['virtuser_file']) && strstr($user, '@')) { |
| | 648 | $user = rcmail_email2user($user); |
| | 649 | } |
| | 650 | |
| | 651 | // exit if IMAP login failed |
| | 652 | if (!($imap_login = $IMAP->connect($host, $user, $pass, $imap_port, $imap_ssl))) { |
| | 653 | return FALSE; |
| | 654 | } |
| | 655 | // user already registered |
| | 656 | if ($user_id && !empty($sql_arr)) { |
| | 657 | // get user prefs |
| | 658 | if (strlen($sql_arr['preferences'])) { |
| | 659 | $user_prefs = unserialize($sql_arr['preferences']); |
| | 660 | $_SESSION['user_prefs'] = $user_prefs; |
| | 661 | array_merge($CONFIG, $user_prefs); |
| | 662 | } |
| | 663 | |
| | 664 | |
| | 665 | // set user specific language |
| | 666 | if (strlen($sql_arr['language'])) { |
| | 667 | $sess_user_lang = $_SESSION['user_lang'] = $sql_arr['language']; |
| | 668 | } |
| | 669 | // update user's record |
| | 670 | $_query = "UPDATE " . self::get_table_name('users'); |
| | 671 | $_query.= " SET last_login=" . $DB->now(); |
| | 672 | $_query.= " WHERE user_id=?"; |
| | 673 | $DB->query($_query, $user_id); |
| | 674 | } |
| | 675 | // create new system user |
| | 676 | else if ($CONFIG['auto_create_user']) { |
| | 677 | $user_id = self::rcmail_create_user($user, $host); |
| | 678 | } |
| | 679 | |
| | 680 | //tfk_debug('User id: ' . $user_id); |
| | 681 | |
| | 682 | if (empty($user_id) === true) { |
| | 683 | return false; |
| | 684 | } |
| | 685 | $_SESSION['user_id'] = $user_id; |
| | 686 | $_SESSION['imap_host'] = $host; |
| | 687 | $_SESSION['imap_port'] = $imap_port; |
| | 688 | $_SESSION['imap_ssl'] = $imap_ssl; |
| | 689 | $_SESSION['username'] = $user; |
| | 690 | $_SESSION['user_lang'] = $sess_user_lang; |
| | 691 | $_SESSION['password'] = self::encrypt_passwd($pass); |
| | 692 | $_SESSION['login_time'] = mktime(); |
| | 693 | |
| | 694 | /** |
| | 695 | * If multi-SMTP servers, use correct one |
| | 696 | * Otherwise, use the general SMTP server |
| | 697 | * or use phpMail function |
| | 698 | * |
| | 699 | * @author Brett Patterson <brett@bpatterson.net> |
| | 700 | * @author Till Klampaeckel <till@php.net> |
| | 701 | */ |
| | 702 | if(is_array($CONFIG['smtp_server']) === true) { |
| | 703 | if (isset($CONFIG['smtp_server'][$host]) === true) { |
| | 704 | $_SESSION['smtp_server'] = $CONFIG['smtp_server'][$host]; |
| | 705 | } |
| | 706 | else { |
| | 707 | $_SESSION['smtp_server'] = 'phpMail'; |
| | 708 | } |
| | 709 | } |
| | 710 | else { |
| | 711 | if (empty($CONFIG['smtp_server']) === false) { |
| | 712 | $_SESSION['smtp_server'] = $CONFIG['smtp_server']; |
| | 713 | } |
| | 714 | else { |
| | 715 | $_SESSION['smtp_server'] = 'phpMail'; |
| | 716 | } |
| | 717 | } |
| | 718 | |
| | 719 | // force reloading complete list of subscribed mailboxes |
| | 720 | self::rcmail_set_imap_prop(); |
| | 721 | $IMAP->clear_cache('mailboxes'); |
| | 722 | $IMAP->create_default_folders(); |
| | 723 | |
| | 724 | return TRUE; |
| | 725 | } |
| | 726 | |
| | 727 | |
| | 728 | // create new entry in users and identities table |
| | 729 | static function rcmail_create_user($user, $host) |
| | 730 | { |
| | 731 | $registry = rc_registry::getInstance(); |
| | 732 | $DB = $registry->get('DB', 'core'); |
| | 733 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 734 | $IMAP = $registry->get('IMAP', 'core'); |
| | 735 | |
| | 736 | $user_email = ''; |
| | 737 | |
| | 738 | // try to resolve user in virtusertable |
| | 739 | if (!empty($CONFIG['virtuser_file']) && strstr($user, '@')==FALSE) { |
| | 740 | $user_email = self::rcmail_user2email($user); |
| | 741 | } |
| | 742 | $_query = "INSERT INTO " . self::get_table_name('users'); |
| | 743 | $_query.= " (created, last_login, username, mail_host, alias, language)"; |
| | 744 | $_query.= " VALUES (" . $DB->now() . ", " . $DB->now() . ", ?, ?, ?, ?)"; |
| | 745 | $DB->query( |
| | 746 | $_query, |
| | 747 | self::strip_newlines($user), |
| | 748 | self::strip_newlines($host), |
| | 749 | self::strip_newlines($user_email), |
| | 750 | $_SESSION['user_lang'] |
| | 751 | ); |
| | 752 | |
| | 753 | if ($user_id = $DB->insert_id(self::get_sequence_name('users'))) { |
| | 754 | $mail_domain = self::rcmail_mail_domain($host); |
| | 755 | |
| | 756 | if ($user_email=='') { |
| | 757 | $user_email = strstr($user, '@') ? $user : sprintf('%s@%s', $user, $mail_domain); |
| | 758 | } |
| | 759 | $user_name = $user!=$user_email ? $user : ''; |
| | 760 | |
| | 761 | // try to resolve the e-mail address from the virtuser table |
| | 762 | if ( |
| | 763 | !empty($CONFIG['virtuser_query']) |
| | 764 | && ($sql_result = $DB->query(preg_replace('/%u/', $user, $CONFIG['virtuser_query']))) |
| | 765 | && ($DB->num_rows()>0) |
| | 766 | ) { |
| | 767 | while ($sql_arr = $DB->fetch_array($sql_result)) { |
| | 768 | $_query = "INSERT INTO " . self::get_table_name('identities'); |
| | 769 | $_query.= " (user_id, del, standard, name, email)"; |
| | 770 | $_query.= " VALUES (?, 0, 1, ?, ?)"; |
| | 771 | $DB->query( |
| | 772 | $_query, |
| | 773 | $user_id, |
| | 774 | self::strip_newlines($user_name), |
| | 775 | preg_replace('/^@/', $user . '@', $sql_arr[0]) |
| | 776 | ); |
| | 777 | } |
| | 778 | } |
| | 779 | else { |
| | 780 | // also create new identity records |
| | 781 | $_query = "INSERT INTO " . self::get_table_name('identities'); |
| | 782 | $_query.= " (user_id, del, standard, name, email)"; |
| | 783 | $_query.= " VALUES (?, 0, 1, ?, ?)"; |
| | 784 | $DB->query( |
| | 785 | $_query, |
| | 786 | $user_id, |
| | 787 | self::strip_newlines($user_name), |
| | 788 | self::strip_newlines($user_email) |
| | 789 | ); |
| | 790 | } |
| | 791 | |
| | 792 | // get existing mailboxes |
| | 793 | $a_mailboxes = $IMAP->list_mailboxes(); |
| | 794 | } |
| | 795 | else { |
| | 796 | raise_error( |
| | 797 | array( |
| | 798 | 'code' => 500, |
| | 799 | 'type' => 'php', |
| | 800 | 'line' => __LINE__, |
| | 801 | 'file' => __FILE__, |
| | 802 | 'message' => "Failed to create new user" |
| | 803 | ), |
| | 804 | TRUE, |
| | 805 | FALSE |
| | 806 | ); |
| | 807 | } |
| | 808 | return $user_id; |
| | 809 | } |
| | 810 | |
| | 811 | |
| | 812 | // load virtuser table in array |
| | 813 | function rcmail_getvirtualfile() |
| | 814 | { |
| | 815 | $registry = rc_registry::getInstance(); |
| | 816 | $registry->get('CONFIG', 'core'); |
| | 817 | if (empty($CONFIG['virtuser_file']) || !is_file($CONFIG['virtuser_file'])) { |
| | 818 | return FALSE; |
| | 819 | } |
| | 820 | // read file |
| | 821 | $a_lines = file($CONFIG['virtuser_file']); |
| | 822 | return $a_lines; |
| | 823 | } |
| | 824 | |
| | 825 | |
| | 826 | // find matches of the given pattern in virtuser table |
| | 827 | static function rcmail_findinvirtual($pattern) |
| | 828 | { |
| | 829 | $result = array(); |
| | 830 | $virtual = self::rcmail_getvirtualfile(); |
| | 831 | if ($virtual==FALSE) { |
| | 832 | return $result; |
| | 833 | } |
| | 834 | // check each line for matches |
| | 835 | foreach ($virtual as $line) { |
| | 836 | $line = trim($line); |
| | 837 | if (empty($line) || $line{0}=='#') { |
| | 838 | continue; |
| | 839 | } |
| | 840 | if (eregi($pattern, $line)) { |
| | 841 | $result[] = $line; |
| | 842 | } |
| | 843 | } |
| | 844 | return $result; |
| | 845 | } |
| | 846 | |
| | 847 | |
| | 848 | // resolve username with virtuser table |
| | 849 | static function rcmail_email2user($email) |
| | 850 | { |
| | 851 | $user = $email; |
| | 852 | $r = self::rcmail_findinvirtual("^$email"); |
| | 853 | |
| | 854 | for ($i=0; $i<count($r); $i++) { |
| | 855 | $data = $r[$i]; |
| | 856 | $arr = preg_split('/\s+/', $data); |
| | 857 | if (count($arr)>0) { |
| | 858 | $user = trim($arr[count($arr)-1]); |
| | 859 | break; |
| | 860 | } |
| | 861 | } |
| | 862 | return $user; |
| | 863 | } |
| | 864 | |
| | 865 | |
| | 866 | // resolve e-mail address with virtuser table |
| | 867 | static function rcmail_user2email($user) |
| | 868 | { |
| | 869 | $email = ""; |
| | 870 | $r = self::rcmail_findinvirtual("$user$"); |
| | 871 | |
| | 872 | for ($i=0; $i<count($r); $i++) { |
| | 873 | $data=$r[$i]; |
| | 874 | $arr = preg_split('/\s+/', $data); |
| | 875 | if (count($arr)>0) { |
| | 876 | $email = trim($arr[0]); |
| | 877 | break; |
| | 878 | } |
| | 879 | } |
| | 880 | return $email; |
| | 881 | } |
| | 882 | |
| | 883 | |
| | 884 | function rcmail_save_user_prefs($a_user_prefs) |
| | 885 | { |
| | 886 | $registry = rc_registry::getInstance(); |
| | 887 | $DB = $registry->get('DB', 'core'); |
| | 888 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 889 | $sess_user_lang = $registry->get('sess_user_lang', 'core'); |
| | 890 | |
| | 891 | $_query = "UPDATE " . self::get_table_name('users'); |
| | 892 | $_query.= " SET preferences=?,"; |
| | 893 | $_query.= " language=?"; |
| | 894 | $_query.= " WHERE user_id=?"; |
| | 895 | $DB->query( |
| | 896 | $_query, |
| | 897 | serialize($a_user_prefs), |
| | 898 | $sess_user_lang, |
| | 899 | $_SESSION['user_id'] |
| | 900 | ); |
| | 901 | |
| | 902 | if ($DB->affected_rows()) { |
| | 903 | $_SESSION['user_prefs'] = $a_user_prefs; |
| | 904 | $CONFIG = array_merge($CONFIG, $a_user_prefs); |
| | 905 | $registry->set('CONFIG', $CONFIG, 'core'); |
| | 906 | return TRUE; |
| | 907 | } |
| | 908 | |
| | 909 | return FALSE; |
| | 910 | } |
| | 911 | |
| | 912 | |
| | 913 | // overwrite action variable |
| | 914 | static function rcmail_overwrite_action($action) |
| | 915 | { |
| | 916 | $registry = rc_registry::getInstance(); |
| | 917 | $OUTPUT = $registry->get('OUTPUT', 'core'); |
| | 918 | $GLOBALS['_action'] = $action; |
| | 919 | $OUTPUT->set_env('action', $action); |
| | 920 | } |
| | 921 | |
| | 922 | |
| | 923 | /** |
| | 924 | * Compose an URL for a specific action |
| | 925 | * |
| | 926 | * @param string Request action |
| | 927 | * @param array More URL parameters |
| | 928 | * @param string Request task (omit if the same) |
| | 929 | * @return The application URL |
| | 930 | */ |
| | 931 | static function rcmail_url($action, $p=array(), $task=null) |
| | 932 | { |
| | 933 | $registry = rc_registry::getInstance(); |
| | 934 | $COMM_PATH = $registry->get('COMM_PATH', 'core'); |
| | 935 | $MAIN_TASKS = $registry->get('MAIN_TASKS', 'core'); |
| | 936 | |
| | 937 | $qstring = ''; |
| | 938 | $base = $COMM_PATH; |
| | 939 | |
| | 940 | if ($task && in_array($task, $MAIN_TASKS)) { |
| | 941 | $base = ereg_replace('_task=[a-z]+', '_task='.$task, $COMM_PATH); |
| | 942 | } |
| | 943 | if (is_array($p)) { |
| | 944 | foreach ($p as $key => $val) { |
| | 945 | $qstring .= '&'.urlencode($key).'='.urlencode($val); |
| | 946 | } |
| | 947 | } |
| | 948 | return $base . ($action ? '&_action='.$action : '') . $qstring; |
| | 949 | } |
| | 950 | |
| | 951 | |
| | 952 | /** |
| | 953 | *@deprecated |
| | 954 | */ |
| | 955 | |
| | 956 | function show_message($message, $type='notice', $vars=NULL) |
| | 957 | { |
| | 958 | $registry = rc_registry::getInstance(); |
| | 959 | $OUTPUT = $registry->get('OUTPUT', 'core'); |
| | 960 | $OUTPUT->show_message($message, $type, $vars); |
| | 961 | |
| | 962 | } |
| | 963 | |
| | 964 | |
| | 965 | |
| | 966 | // encrypt IMAP password using DES encryption |
| | 967 | static function encrypt_passwd($pass) |
| | 968 | { |
| | 969 | $cypher = des(self::get_des_key(), $pass, 1, 0, NULL); |
| | 970 | return base64_encode($cypher); |
| | 971 | } |
| | 972 | |
| | 973 | // decrypt IMAP password using DES encryption |
| | 974 | static function decrypt_passwd($cypher) |
| | 975 | { |
| | 976 | $pass = des(self::get_des_key(), base64_decode($cypher), 0, 0, NULL); |
| | 977 | return preg_replace('/\x00/', '', $pass); |
| | 978 | } |
| | 979 | |
| | 980 | |
| | 981 | // return a 24 byte key for the DES encryption |
| | 982 | static function get_des_key() |
| | 983 | { |
| | 984 | $registry = rc_registry::getInstance(); |
| | 985 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 986 | $key = !empty($CONFIG['des_key']) ? $CONFIG['des_key'] : 'rcmail?24BitPwDkeyF**ECB'; |
| | 987 | $len = strlen($key); |
| | 988 | |
| | 989 | // make sure the key is exactly 24 chars long |
| | 990 | if ($len<24) { |
| | 991 | $key .= str_repeat('_', 24-$len); |
| | 992 | } |
| | 993 | else if ($len>24) { |
| | 994 | substr($key, 0, 24); |
| | 995 | } |
| | 996 | return $key; |
| | 997 | } |
| | 998 | |
| | 999 | |
| | 1000 | // read directory program/localization/ and return a list of available languages |
| | 1001 | function rcube_list_languages() |
| | 1002 | { |
| | 1003 | $registry = rc_registry::getInstance(); |
| | 1004 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 1005 | $INSTALL_PATH = $registry->get('INSTALL_PATH', 'core'); |
| | 1006 | $sa_languages = $registry->get('sa_languages', 'core'); |
| | 1007 | |
| | 1008 | if (is_null($sa_languages)) { |
| | 1009 | @include($INSTALL_PATH.'program/localization/index.inc'); |
| | 1010 | |
| | 1011 | if ($dh = @opendir($INSTALL_PATH.'program/localization')) { |
| | 1012 | while (($name = readdir($dh)) !== false) { |
| | 1013 | if ($name{0}=='.' || !is_dir($INSTALL_PATH.'program/localization/'.$name)) |
| | 1014 | continue; |
| | 1015 | |
| | 1016 | if ($label = $rcube_languages[$name]) |
| | 1017 | $sa_languages[$name] = $label ? $label : $name; |
| | 1018 | } |
| | 1019 | closedir($dh); |
| | 1020 | } |
| | 1021 | $registry->set('sa_languages', $sa_languages, 'core'); |
| | 1022 | } |
| | 1023 | return $sa_languages; |
| | 1024 | } |
| | 1025 | |
| | 1026 | |
| | 1027 | // add a localized label to the client environment |
| | 1028 | static function rcube_add_label() |
| | 1029 | { |
| | 1030 | $registry = rc_registry::getInstance(); |
| | 1031 | $OUTPUT = $registry->get('OUTPUT', 'core'); |
| | 1032 | $arg_list = func_get_args(); |
| | 1033 | foreach ($arg_list as $i => $name) { |
| | 1034 | $OUTPUT->command('add_label', $name, rcube_label($name)); |
| | 1035 | } |
| | 1036 | } |
| | 1037 | |
| | 1038 | |
| | 1039 | // remove temp files older than two day |
| | 1040 | function rcmail_temp_gc() |
| | 1041 | { |
| | 1042 | $registry = rc_registry::getInstance(); |
| | 1043 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 1044 | $tmp = self::unslashify($CONFIG['temp_dir']); |
| | 1045 | $expire = mktime() - 172800; // expire in 48 hours |
| | 1046 | |
| | 1047 | if (($dir = opendir($tmp)) === false) { |
| | 1048 | return false; |
| | 1049 | } |
| | 1050 | while (($fname = readdir($dir)) !== false) { |
| | 1051 | if ($fname{0} == '.') |
| | 1052 | continue; |
| | 1053 | |
| | 1054 | if (filemtime($tmp.'/'.$fname) < $expire) |
| | 1055 | @unlink($tmp.'/'.$fname); |
| | 1056 | } |
| | 1057 | closedir($dir); |
| | 1058 | } |
| | 1059 | |
| | 1060 | |
| | 1061 | // remove all expired message cache records |
| | 1062 | static function rcmail_message_cache_gc() |
| | 1063 | { |
| | 1064 | $registry = rc_registry::getInstance(); |
| | 1065 | $DB = $registry->get('DB', 'core'); |
| | 1066 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 1067 | |
| | 1068 | // no cache lifetime configured |
| | 1069 | if (empty($CONFIG['message_cache_lifetime'])) { |
| | 1070 | return; |
| | 1071 | } |
| | 1072 | // get target timestamp |
| | 1073 | $ts = get_offset_time($CONFIG['message_cache_lifetime'], -1); |
| | 1074 | |
| | 1075 | $_query = "DELETE FROM " . self::get_table_name('messages'); |
| | 1076 | $_query.= " WHERE created < " . $DB->fromunixtime($ts); |
| | 1077 | $DB->query($_query); |
| | 1078 | } |
| | 1079 | |
| | 1080 | |
| | 1081 | /** |
| | 1082 | * Convert a string from one charset to another. |
| | 1083 | * Uses mbstring and iconv functions if possible |
| | 1084 | * |
| | 1085 | * @param string Input string |
| | 1086 | * @param string Suspected charset of the input string |
| | 1087 | * @param string Target charset to convert to; defaults to RCMAIL_CHARSET |
| | 1088 | * @return Converted string |
| | 1089 | */ |
| | 1090 | static function rcube_charset_convert($str, $from, $to=NULL) |
| | 1091 | { |
| | 1092 | $registry = rc_registry::getInstance(); |
| | 1093 | $MBSTRING = $registry->get('MBSTRING', 'core'); |
| | 1094 | |
| | 1095 | $from = strtoupper($from); |
| | 1096 | $to = $to==NULL ? strtoupper(RCMAIL_CHARSET) : strtoupper($to); |
| | 1097 | |
| | 1098 | if ($from==$to || $str=='' || empty($from)) { |
| | 1099 | return $str; |
| | 1100 | } |
| | 1101 | // convert charset using mbstring module |
| | 1102 | if ($MBSTRING) { |
| | 1103 | $to = $to=="UTF-7" ? "UTF7-IMAP" : $to; |
| | 1104 | $from = $from=="UTF-7" ? "UTF7-IMAP": $from; |
| | 1105 | |
| | 1106 | // return if convert succeeded |
| | 1107 | if (($out = mb_convert_encoding($str, $to, $from)) != '') { |
| | 1108 | return $out; |
| | 1109 | } |
| | 1110 | } |
| | 1111 | |
| | 1112 | // convert charset using iconv module |
| | 1113 | if (function_exists('iconv') && $from!='UTF-7' && $to!='UTF-7') |
| | 1114 | return iconv($from, $to, $str); |
| | 1115 | |
| | 1116 | $conv = new utf8(); |
| | 1117 | |
| | 1118 | // convert string to UTF-8 |
| | 1119 | if ($from=='UTF-7') { |
| | 1120 | $str = utf7_to_utf8($str); |
| | 1121 | } |
| | 1122 | else if (($from=='ISO-8859-1') && function_exists('utf8_encode')) { |
| | 1123 | $str = utf8_encode($str); |
| | 1124 | } |
| | 1125 | else if ($from!='UTF-8') { |
| | 1126 | $conv->loadCharset($from); |
| | 1127 | $str = $conv->strToUtf8($str); |
| | 1128 | } |
| | 1129 | |
| | 1130 | // encode string for output |
| | 1131 | if ($to=='UTF-7') { |
| | 1132 | return utf8_to_utf7($str); |
| | 1133 | } |
| | 1134 | else if ($to=='ISO-8859-1' && function_exists('utf8_decode')) { |
| | 1135 | return utf8_decode($str); |
| | 1136 | } |
| | 1137 | else if ($to!='UTF-8') { |
| | 1138 | $conv->loadCharset($to); |
| | 1139 | return $conv->utf8ToStr($str); |
| | 1140 | } |
| | 1141 | |
| | 1142 | // return UTF-8 string |
| | 1143 | return $str; |
| | 1144 | } |
| | 1145 | |
| | 1146 | |
| | 1147 | /** |
| | 1148 | * Replacing specials characters to a specific encoding type |
| | 1149 | * |
| | 1150 | * @param string Input string |
| | 1151 | * @param string Encoding type: text|html|xml|js|url |
| | 1152 | * @param string Replace mode for tags: show|replace|remove |
| | 1153 | * @param boolean Convert newlines |
| | 1154 | * @return The quoted string |
| | 1155 | */ |
| | 1156 | static function rep_specialchars_output($str, $enctype='', $mode='', $newlines=TRUE) |
| | 1157 | { |
| | 1158 | $registry = rc_registry::getInstance(); |
| | 1159 | $OUTPUT_TYPE = $registry->get('OUTPUT_TYPE', 'core'); |
| | 1160 | $OUTPUT = $registry->get('OUTPUT', 'core'); |
| | 1161 | $html_encode_arr = $registry->get('html_encode_arr', 'core'); |
| | 1162 | $js_rep_table = $registry->get('js_rep_table', 'core'); |
| | 1163 | $xml_rep_table = $registry->get('xml_rep_table', 'core'); |
| | 1164 | |
| | 1165 | if (!$enctype) { |
| | 1166 | $enctype = $OUTPUT_TYPE; |
| | 1167 | } |
| | 1168 | // convert nbsps back to normal spaces if not html |
| | 1169 | if ($enctype!='html') { |
| | 1170 | $str = str_replace(chr(160), ' ', $str); |
| | 1171 | } |
| | 1172 | // encode for plaintext |
| | 1173 | if ($enctype=='text') { |
| | 1174 | return str_replace( |
| | 1175 | "\r\n", |
| | 1176 | "\n", |
| | 1177 | $mode=='remove' ? strip_tags($str) : $str |
| | 1178 | ); |
| | 1179 | } |
| | 1180 | // encode for HTML output |
| | 1181 | if ($enctype=='html') { |
| | 1182 | if (is_null($html_encode_arr)) { |
| | 1183 | $html_encode_arr = get_html_translation_table(HTML_SPECIALCHARS); |
| | 1184 | unset($html_encode_arr['?']); |
| | 1185 | $registry->set('html_encode_arr', $html_encode_arr, 'core'); |
| | 1186 | } |
| | 1187 | |
| | 1188 | $ltpos = strpos($str, '<'); |
| | 1189 | $encode_arr = $html_encode_arr; |
| | 1190 | |
| | 1191 | // don't replace quotes and html tags |
| | 1192 | if ( |
| | 1193 | ($mode=='show' || $mode=='') |
| | 1194 | && $ltpos!==false |
| | 1195 | && strpos($str, '>', $ltpos)!==false |
| | 1196 | ) { |
| | 1197 | unset($encode_arr['"']); |
| | 1198 | unset($encode_arr['<']); |
| | 1199 | unset($encode_arr['>']); |
| | 1200 | unset($encode_arr['&']); |
| | 1201 | } |
| | 1202 | else if ($mode=='remove') { |
| | 1203 | $str = strip_tags($str); |
| | 1204 | } |
| | 1205 | // avoid douple quotation of & |
| | 1206 | $out = preg_replace( |
| | 1207 | '/&([a-z]{2,5}|#[0-9]{2,4});/', |
| | 1208 | '&\\1;', |
| | 1209 | strtr($str, $encode_arr) |
| | 1210 | ); |
| | 1211 | return $newlines ? nl2br($out) : $out; |
| | 1212 | } |
| | 1213 | |
| | 1214 | if ($enctype=='url') { |
| | 1215 | return rawurlencode($str); |
| | 1216 | } |
| | 1217 | // if the replace tables for XML and JS are not yet defined |
| | 1218 | if (is_null($js_rep_table)) { |
| | 1219 | $js_rep_table = $xml_rep_table = array(); |
| | 1220 | $xml_rep_table['&'] = '&'; |
| | 1221 | |
| | 1222 | for ($c=160; $c<256; $c++) // can be increased to support more charsets |
| | 1223 | { |
| | 1224 | $hex = dechex($c); |
| | 1225 | $xml_rep_table[Chr($c)] = "&#$c;"; |
| | 1226 | |
| | 1227 | if ($OUTPUT->get_charset()=='ISO-8859-1') { |
| | 1228 | $js_rep_table[Chr($c)] = sprintf( |
| | 1229 | "\u%s%s", |
| | 1230 | str_repeat('0', 4-strlen($hex)), |
| | 1231 | $hex |
| | 1232 | ); |
| | 1233 | } |
| | 1234 | } |
| | 1235 | $xml_rep_table['"'] = '"'; |
| | 1236 | $registry->set('js_rep_table', $js_rep_table, 'core'); |
| | 1237 | } |
| | 1238 | |
| | 1239 | // encode for XML |
| | 1240 | if ($enctype=='xml') { |
| | 1241 | return strtr($str, $xml_rep_table); |
| | 1242 | } |
| | 1243 | // encode for javascript use |
| | 1244 | if ($enctype=='js') { |
| | 1245 | if ($OUTPUT->get_charset()!='UTF-8') { |
| | 1246 | $str = self::rcube_charset_convert( |
| | 1247 | $str, |
| | 1248 | RCMAIL_CHARSET, |
| | 1249 | $OUTPUT->get_charset() |
| | 1250 | ); |
| | 1251 | } |
| | 1252 | return preg_replace( |
| | 1253 | array("/\r?\n/", "/\r/"), |
| | 1254 | array('\n', '\n'), |
| | 1255 | addslashes(strtr($str, $js_rep_table)) |
| | 1256 | ); |
| | 1257 | } |
| | 1258 | // no encoding given -> return original string |
| | 1259 | return $str; |
| | 1260 | } |
| | 1261 | |
| | 1262 | /** |
| | 1263 | * Quote a given string. Alias function for rep_specialchars_output |
| | 1264 | * @see rep_specialchars_output |
| | 1265 | */ |
| | 1266 | static function Q($str, $mode='strict', $newlines=TRUE) |
| | 1267 | { |
| | 1268 | return self::rep_specialchars_output($str, 'html', $mode, $newlines); |
| | 1269 | } |
| | 1270 | |
| | 1271 | /** |
| | 1272 | * Quote a given string. Alias function for rep_specialchars_output |
| | 1273 | * @see rep_specialchars_output |
| | 1274 | */ |
| | 1275 | static function JQ($str) |
| | 1276 | { |
| | 1277 | return self::rep_specialchars_output($str, 'js'); |
| | 1278 | } |
| | 1279 | |
| | 1280 | |
| | 1281 | /** |
| | 1282 | * Read input value and convert it for internal use |
| | 1283 | * Performs stripslashes() and charset conversion if necessary |
| | 1284 | * |
| | 1285 | * @param string Field name to read |
| | 1286 | * @param int Source to get value from (GPC) |
| | 1287 | * @param boolean Allow HTML tags in field value |
| | 1288 | * @param string Charset to convert into |
| | 1289 | * @return string Field value or NULL if not available |
| | 1290 | */ |
| | 1291 | static function get_input_value($fname, $source, $allow_html=FALSE, $charset=NULL) |
| | 1292 | { |
| | 1293 | $registry = rc_registry::getInstance(); |
| | 1294 | $OUTPUT = $registry->get('OUTPUT', 'core'); |
| | 1295 | $value = NULL; |
| | 1296 | |
| | 1297 | if ($source == RCUBE_INPUT_GET && isset($_GET[$fname])) { |
| | 1298 | $value = $_GET[$fname]; |
| | 1299 | } |
| | 1300 | else if ($source == RCUBE_INPUT_POST && isset($_POST[$fname])) { |
| | 1301 | $value = $_POST[$fname]; |
| | 1302 | } |
| | 1303 | else if ($source == RCUBE_INPUT_GPC) { |
| | 1304 | if (isset($_POST[$fname])) |
| | 1305 | $value = $_POST[$fname]; |
| | 1306 | else if (isset($_GET[$fname])) |
| | 1307 | $value = $_GET[$fname]; |
| | 1308 | else if (isset($_COOKIE[$fname])) |
| | 1309 | $value = $_COOKIE[$fname]; |
| | 1310 | } |
| | 1311 | |
| | 1312 | // strip slashes if magic_quotes enabled |
| | 1313 | if ((bool)get_magic_quotes_gpc()) |
| | 1314 | $value = stripslashes($value); |
| | 1315 | |
| | 1316 | // remove HTML tags if not allowed |
| | 1317 | if (!$allow_html) { |
| | 1318 | $value = strip_tags($value); |
| | 1319 | } |
| | 1320 | // convert to internal charset |
| | 1321 | if (is_object($OUTPUT)) { |
| | 1322 | return self::rcube_charset_convert($value, $OUTPUT->get_charset(), $charset); |
| | 1323 | } |
| | 1324 | return $value; |
| | 1325 | } |
| | 1326 | |
| | 1327 | /** |
| | 1328 | * Remove single and double quotes from given string |
| | 1329 | */ |
| | 1330 | static function strip_quotes($str) |
| | 1331 | { |
| | 1332 | return preg_replace('/[\'"]/', '', $str); |
| | 1333 | } |
| | 1334 | |
| | 1335 | /** |
| | 1336 | * Remove new lines characters from given string |
| | 1337 | */ |
| | 1338 | static function strip_newlines($str) |
| | 1339 | { |
| | 1340 | return preg_replace('/[\r\n]/', '', $str); |
| | 1341 | } |
| | 1342 | |
| | 1343 | |
| | 1344 | // return boolean if a specific template exists |
| | 1345 | static function template_exists($name) |
| | 1346 | { |
| | 1347 | $registry = rc_registry::getInstance(); |
| | 1348 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 1349 | $skin_path = $CONFIG['skin_path']; |
| | 1350 | |
| | 1351 | // check template file |
| | 1352 | return is_file("$skin_path/templates/$name.html"); |
| | 1353 | } |
| | 1354 | |
| | 1355 | |
| | 1356 | // Wrapper for rcmail_template::parse() |
| | 1357 | // @deprecated |
| | 1358 | static function parse_template($name='main', $exit=true) |
| | 1359 | { |
| | 1360 | $registry = rc_registry::getInstance(); |
| | 1361 | $OUTPUT = $registry->get('OUTPUT', 'core'); |
| | 1362 | $OUTPUT->parse($name, $exit); |
| | 1363 | } |
| | 1364 | |
| | 1365 | static function rcube_table_output($attrib, $table_data, $a_show_cols, $id_col) |
| | 1366 | { |
| | 1367 | $registry = rc_registry::getInstance(); |
| | 1368 | $DB = $registry->get('DB', 'core'); |
| | 1369 | |
| | 1370 | // allow the following attributes to be added to the <table> tag |
| | 1371 | $attrib_str = self::create_attrib_string( |
| | 1372 | $attrib, |
| | 1373 | array( |
| | 1374 | 'style', |
| | 1375 | 'class', |
| | 1376 | 'id', |
| | 1377 | 'cellpadding', |
| | 1378 | 'cellspacing', |
| | 1379 | 'border', |
| | 1380 | 'summary' |
| | 1381 | ) |
| | 1382 | ); |
| | 1383 | $table = '<table' . $attrib_str . ">\n"; |
| | 1384 | |
| | 1385 | // add table title |
| | 1386 | $table .= "<thead><tr>\n"; |
| | 1387 | |
| | 1388 | foreach ($a_show_cols as $col) { |
| | 1389 | $table .= '<td class="'.$col.'">' . self::Q(rcube_label($col)) . "</td>\n"; |
| | 1390 | } |
| | 1391 | $table .= "</tr></thead>\n<tbody>\n"; |
| | 1392 | |
| | 1393 | $c = 0; |
| | 1394 | if (!is_array($table_data)) { |
| | 1395 | while ($table_data && ($sql_arr = $DB->fetch_assoc($table_data))) { |
| | 1396 | $zebra_class = $c%2 ? 'even' : 'odd'; |
| | 1397 | |
| | 1398 | $table .= sprintf( |
| | 1399 | '<tr id="rcmrow%d" class="contact '.$zebra_class.'">'."\n", |
| | 1400 | $sql_arr[$id_col] |
| | 1401 | ); |
| | 1402 | |
| | 1403 | // format each col |
| | 1404 | foreach ($a_show_cols as $col) { |
| | 1405 | $cont = self::Q($sql_arr[$col]); |
| | 1406 | $table .= '<td class="'.$col.'">' . $cont . "</td>\n"; |
| | 1407 | } |
| | 1408 | |
| | 1409 | $table .= "</tr>\n"; |
| | 1410 | $c++; |
| | 1411 | } |
| | 1412 | } |
| | 1413 | else { |
| | 1414 | foreach ($table_data as $row_data) { |
| | 1415 | $zebra_class = $c%2 ? 'even' : 'odd'; |
| | 1416 | |
| | 1417 | $table .= sprintf( |
| | 1418 | '<tr id="rcmrow%d" class="contact '.$zebra_class.'">'."\n", |
| | 1419 | $row_data[$id_col] |
| | 1420 | ); |
| | 1421 | |
| | 1422 | // format each col |
| | 1423 | foreach ($a_show_cols as $col) { |
| | 1424 | $cont = self::Q($row_data[$col]); |
| | 1425 | $table .= '<td class="'.$col.'">' . $cont . "</td>\n"; |
| | 1426 | } |
| | 1427 | |
| | 1428 | $table .= "</tr>\n"; |
| | 1429 | $c++; |
| | 1430 | } |
| | 1431 | } |
| | 1432 | |
| | 1433 | // complete message table |
| | 1434 | $table .= "</tbody></table>\n"; |
| | 1435 | |
| | 1436 | return $table; |
| | 1437 | } |
| | 1438 | |
| | 1439 | |
| | 1440 | /** |
| | 1441 | * Create an edit field for inclusion on a form |
| | 1442 | * |
| | 1443 | * @param string col field name |
| | 1444 | * @param string value field value |
| | 1445 | * @param array attrib HTML element attributes for field |
| | 1446 | * @param string type HTML element type (default 'text') |
| | 1447 | * @return string HTML field definition |
| | 1448 | */ |
| | 1449 | static function rcmail_get_edit_field($col, $value, $attrib, $type='text') |
| | 1450 | { |
| | 1451 | $fname = '_'.$col; |
| | 1452 | $attrib['name'] = $fname; |
| | 1453 | |
| | 1454 | if ($type=='checkbox') { |
| | 1455 | $attrib['value'] = '1'; |
| | 1456 | $input = new checkbox($attrib); |
| | 1457 | } |
| | 1458 | else if ($type=='textarea') { |
| | 1459 | $attrib['cols'] = $attrib['size']; |
| | 1460 | $input = new textarea($attrib); |
| | 1461 | } |
| | 1462 | else { |
| | 1463 | $input = new textfield($attrib); |
| | 1464 | } |
| | 1465 | // use value from post |
| | 1466 | if (!empty($_POST[$fname])) { |
| | 1467 | $value = $_POST[$fname]; |
| | 1468 | } |
| | 1469 | $out = $input->show($value); |
| | 1470 | |
| | 1471 | return $out; |
| | 1472 | } |
| | 1473 | |
| | 1474 | |
| | 1475 | // return the mail domain configured for the given host |
| | 1476 | static function rcmail_mail_domain($host) |
| | 1477 | { |
| | 1478 | $registry = rc_registry::getInstance(); |
| | 1479 | $CONFIG = $regisry->get('CONFIG', 'core'); |
| | 1480 | |
| | 1481 | $domain = $host; |
| | 1482 | if (is_array($CONFIG['mail_domain'])) { |
| | 1483 | if (isset($CONFIG['mail_domain'][$host])) { |
| | 1484 | $domain = $CONFIG['mail_domain'][$host]; |
| | 1485 | } |
| | 1486 | } |
| | 1487 | else if (!empty($CONFIG['mail_domain'])) { |
| | 1488 | $domain = $CONFIG['mail_domain']; |
| | 1489 | } |
| | 1490 | return $domain; |
| | 1491 | } |
| | 1492 | |
| | 1493 | |
| | 1494 | // compose a valid attribute string for HTML tags |
| | 1495 | static function create_attrib_string($attrib, $allowed_attribs=array('id', 'class', 'style')) |
| | 1496 | { |
| | 1497 | // allow the following attributes to be added to the <iframe> tag |
| | 1498 | $attrib_str = ''; |
| | 1499 | foreach ($allowed_attribs as $a) { |
| | 1500 | if (isset($attrib[$a])) { |
| | 1501 | $attrib_str .= sprintf( |
| | 1502 | ' %s="%s"', |
| | 1503 | $a, str_replace('"', '"', $attrib[$a]) |
| | 1504 | ); |
| | 1505 | } |
| | 1506 | } |
| | 1507 | return $attrib_str; |
| | 1508 | } |
| | 1509 | |
| | 1510 | |
| | 1511 | // convert a HTML attribute string attributes to an associative array (name => value) |
| | 1512 | function parse_attrib_string($str) |
| | 1513 | { |
| | 1514 | $attrib = array(); |
| | 1515 | preg_match_all( |
| | 1516 | '/\s*([-_a-z]+)=(["\'])([^"]+)\2/Ui', |
| | 1517 | stripslashes($str), |
| | 1518 | $regs, |
| | 1519 | PREG_SET_ORDER |
| | 1520 | ); |
| | 1521 | |
| | 1522 | // convert attributes to an associative array (name => value) |
| | 1523 | if ($regs) { |
| | 1524 | foreach ($regs as $attr) { |
| | 1525 | $attrib[strtolower($attr[1])] = $attr[3]; |
| | 1526 | } |
| | 1527 | } |
| | 1528 | return $attrib; |
| | 1529 | } |
| | 1530 | |
| | 1531 | |
| | 1532 | static function format_date($date, $format=NULL) |
| | 1533 | { |
| | 1534 | $registry = rc_registry::getInstance(); |
| | 1535 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 1536 | $sess_user_lang = $registry->get('sess_user_lang', 'core'); |
| | 1537 | |
| | 1538 | $ts = NULL; |
| | 1539 | |
| | 1540 | if (is_numeric($date)) { |
| | 1541 | $ts = $date; |
| | 1542 | } |
| | 1543 | else if (!empty($date)) { |
| | 1544 | $ts = @strtotime($date); |
| | 1545 | } |
| | 1546 | if (empty($ts)) { |
| | 1547 | return ''; |
| | 1548 | } |
| | 1549 | // get user's timezone |
| | 1550 | $tz = $CONFIG['timezone']; |
| | 1551 | if ($CONFIG['dst_active']) { |
| | 1552 | $tz++; |
| | 1553 | } |
| | 1554 | // convert time to user's timezone |
| | 1555 | $timestamp = $ts - date('Z', $ts) + ($tz * 3600); |
| | 1556 | |
| | 1557 | // get current timestamp in user's timezone |
| | 1558 | $now = time(); // local time |
| | 1559 | $now -= (int)date('Z'); // make GMT time |
| | 1560 | $now += ($tz * 3600); // user's time |
| | 1561 | $now_date = getdate($now); |
| | 1562 | |
| | 1563 | $today_limit = mktime( |
| | 1564 | 0, 0, 0, |
| | 1565 | $now_date['mon'], |
| | 1566 | $now_date['mday'], |
| | 1567 | $now_date['year'] |
| | 1568 | ); |
| | 1569 | $week_limit = mktime( |
| | 1570 | 0, 0, 0, |
| | 1571 | $now_date['mon'], |
| | 1572 | $now_date['mday']-6, |
| | 1573 | $now_date['year'] |
| | 1574 | ); |
| | 1575 | |
| | 1576 | // define date format depending on current time |
| | 1577 | if ( |
| | 1578 | $CONFIG['prettydate'] |
| | 1579 | && !$format |
| | 1580 | && $timestamp > $today_limit |
| | 1581 | && $timestamp < $now |
| | 1582 | ) { |
| | 1583 | return sprintf( |
| | 1584 | '%s %s', |
| | 1585 | rcube_label('today'), |
| | 1586 | date( |
| | 1587 | $CONFIG['date_today'] ? $CONFIG['date_today'] : 'H:i', |
| | 1588 | $timestamp |
| | 1589 | ) |
| | 1590 | ); |
| | 1591 | } |
| | 1592 | elseif ( |
| | 1593 | $CONFIG['prettydate'] |
| | 1594 | && !$format |
| | 1595 | && $timestamp > $week_limit |
| | 1596 | && $timestamp < $now |
| | 1597 | ) { |
| | 1598 | $format = $CONFIG['date_short'] ? $CONFIG['date_short'] : 'D H:i'; |
| | 1599 | } |
| | 1600 | elseif (!$format) { |
| | 1601 | $format = $CONFIG['date_long'] ? $CONFIG['date_long'] : 'd.m.Y H:i'; |
| | 1602 | } |
| | 1603 | |
| | 1604 | // parse format string manually in order to provide localized weekday and month names |
| | 1605 | // an alternative would be to convert the date() format string to fit with strftime() |
| | 1606 | $out = ''; |
| | 1607 | for($i=0; $i<strlen($format); $i++) { |
| | 1608 | if ($format{$i}=='\\') { // skip escape chars |
| | 1609 | continue; |
| | 1610 | } |
| | 1611 | // write char "as-is" |
| | 1612 | if ($format{$i}==' ' || $format{$i-1}=='\\') { |
| | 1613 | $out .= $format{$i}; |
| | 1614 | // weekday (short) |
| | 1615 | } |
| | 1616 | elseif ($format{$i}=='D') { |
| | 1617 | $out .= rcube_label(strtolower(date('D', $timestamp))); |
| | 1618 | // weekday long |
| | 1619 | } |
| | 1620 | elseif ($format{$i}=='l') { |
| | 1621 | $out .= rcube_label(strtolower(date('l', $timestamp))); |
| | 1622 | // month name (short) |
| | 1623 | } |
| | 1624 | elseif ($format{$i}=='M') { |
| | 1625 | $out .= rcube_label(strtolower(date('M', $timestamp))); |
| | 1626 | // month name (long) |
| | 1627 | } |
| | 1628 | elseif ($format{$i}=='F') { |
| | 1629 | $out .= rcube_label(strtolower(date('F', $timestamp))); |
| | 1630 | } |
| | 1631 | else { |
| | 1632 | $out .= date($format{$i}, $timestamp); |
| | 1633 | } |
| | 1634 | } |
| | 1635 | return $out; |
| | 1636 | } |
| | 1637 | |
| | 1638 | |
| | 1639 | static function format_email_recipient($email, $name='') |
| | 1640 | { |
| | 1641 | if ($name && $name != $email) { |
| | 1642 | return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email); |
| | 1643 | } |
| | 1644 | else { |
| | 1645 | return $email; |
| | 1646 | } |
| | 1647 | } |
| | 1648 | |
| | 1649 | |
| | 1650 | |
| | 1651 | // ************** functions delivering gui objects ************** |
| | 1652 | |
| | 1653 | |
| | 1654 | |
| | 1655 | static function rcmail_message_container($attrib) |
| | 1656 | { |
| | 1657 | $registry = rc_registry::getInstance(); |
| | 1658 | $OUPUT = $registry->get('OUTPUT', 'core'); |
| | 1659 | |
| | 1660 | if (!$attrib['id']) { |
| | 1661 | $attrib['id'] = 'rcmMessageContainer'; |
| | 1662 | } |
| | 1663 | // allow the following attributes to be added to the <table> tag |
| | 1664 | $attrib_str = self::create_attrib_string( |
| | 1665 | $attrib, |
| | 1666 | array('style', 'class', 'id') |
| | 1667 | ); |
| | 1668 | $out = '<div' . $attrib_str . "></div>"; |
| | 1669 | |
| | 1670 | $OUTPUT->add_gui_object('message', $attrib['id']); |
| | 1671 | |
| | 1672 | return $out; |
| | 1673 | } |
| | 1674 | |
| | 1675 | |
| | 1676 | // return the IMAP username of the current session |
| | 1677 | static function rcmail_current_username($attrib) |
| | 1678 | { |
| | 1679 | $registry = rc_registry::getInstance(); |
| | 1680 | $DB = $registry->get('DB', 'core'); |
| | 1681 | $s_username = $registry->get('s_username', 'core'); |
| | 1682 | |
| | 1683 | // alread fetched |
| | 1684 | if (!empty($s_username)) { |
| | 1685 | return $s_username; |
| | 1686 | } |
| | 1687 | // get e-mail address form default identity |
| | 1688 | $_query = "SELECT email AS mailto"; |
| | 1689 | $_query.= " FROM " . self::get_table_name('identities'); |
| | 1690 | $_query.= " WHERE user_id=?"; |
| | 1691 | $_query.= " AND standard=1"; |
| | 1692 | $_query.= " AND del<>1"; |
| | 1693 | $sql_result = $DB->query( |
| | 1694 | $_query, |
| | 1695 | $_SESSION['user_id'] |
| | 1696 | ); |
| | 1697 | |
| | 1698 | if ($DB->num_rows($sql_result)) { |
| | 1699 | $sql_arr = $DB->fetch_assoc($sql_result); |
| | 1700 | $s_username = $sql_arr['mailto']; |
| | 1701 | } |
| | 1702 | elseif (strstr($_SESSION['username'], '@')) { |
| | 1703 | $s_username = $_SESSION['username']; |
| | 1704 | } |
| | 1705 | else { |
| | 1706 | $s_username = $_SESSION['username'].'@'.$_SESSION['imap_host']; |
| | 1707 | } |
| | 1708 | return $s_username; |
| | 1709 | } |
| | 1710 | |
| | 1711 | |
| | 1712 | // return code for the webmail login form |
| | 1713 | function rcmail_login_form($attrib) |
| | 1714 | { |
| | 1715 | $registry = rc_registry::getInstance(); |
| | 1716 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 1717 | $OUTPUT = $registry->get('OUTPUT', 'core'); |
| | 1718 | $SESS_HIDDEN_FIELD = $registry->get('SESS_HIDDEN_FIELD', 'core'); |
| | 1719 | |
| | 1720 | $_SESSION['temp'] = true; |
| | 1721 | |
| | 1722 | $labels = array(); |
| | 1723 | $labels['user'] = rcube_label('username'); |
| | 1724 | $labels['pass'] = rcube_label('password'); |
| | 1725 | $labels['host'] = rcube_label('server'); |
| | 1726 | |
| | 1727 | $input_user = new textfield(array('name' => '_user', 'id' => 'rcmloginuser', 'size' => 30, 'autocomplete' => 'off')); |
| | 1728 | $input_pass = new passwordfield(array('name' => '_pass', 'id' => 'rcmloginpwd', 'size' => 30)); |
| | 1729 | $input_action = new hiddenfield(array('name' => '_action', 'value' => 'login')); |
| | 1730 | |
| | 1731 | $fields = array(); |
| | 1732 | $fields['user'] = $input_user->show(self::get_input_value('_user', RCUBE_INPUT_POST)); |
| | 1733 | $fields['pass'] = $input_pass->show(); |
| | 1734 | $fields['action'] = $input_action->show(); |
| | 1735 | |
| | 1736 | if (is_array($CONFIG['default_host'])) { |
| | 1737 | $select_host = new select(array('name' => '_host', 'id' => 'rcmloginhost')); |
| | 1738 | |
| | 1739 | foreach ($CONFIG['default_host'] as $key => $value) { |
| | 1740 | if (!is_array($value)) { |
| | 1741 | $select_host->add($value, (is_numeric($key) ? $value : $key)); |
| | 1742 | } |
| | 1743 | else { |
| | 1744 | unset($select_host); |
| | 1745 | break; |
| | 1746 | } |
| | 1747 | } |
| | 1748 | $fields['host'] = isset($select_host) ? $select_host->show($_POST['_host']) : null; |
| | 1749 | } |
| | 1750 | else if (!strlen($CONFIG['default_host'])) { |
| | 1751 | $input_host = new textfield(array('name' => '_host', 'id' => 'rcmloginhost', 'size' => 30)); |
| | 1752 | $fields['host'] = $input_host->show($_POST['_host']); |
| | 1753 | } |
| | 1754 | |
| | 1755 | $form_name = strlen($attrib['form']) ? $attrib['form'] : 'form'; |
| | 1756 | $form_start = !strlen($attrib['form']) ? '<form name="form" action="./" method="post">' : ''; |
| | 1757 | $form_end = !strlen($attrib['form']) ? '</form>' : ''; |
| | 1758 | $form_host = ''; |
| | 1759 | |
| | 1760 | if ($fields['host']) { |
| | 1761 | $form_host.= "</tr><tr>"; |
| | 1762 | $form_host.= "<td class=\"title\"><label for=\"rcmloginhost\">$labels[host]</label></td>"; |
| | 1763 | $form_host.= "<td>$fields[host]</td>"; |
| | 1764 | |
| | 1765 | } |
| | 1766 | $OUTPUT->add_gui_object('loginform', $form_name); |
| | 1767 | |
| | 1768 | $out = ''; |
| | 1769 | $out.= $form_start; |
| | 1770 | $out.= $SESS_HIDDEN_FIELD; |
| | 1771 | $out.= $fields[action]; |
| | 1772 | $out.= '<table><tr>'; |
| | 1773 | $out.= '<td class="title"><label for="rcmloginuser">'; |
| | 1774 | $out.= "$labels[user]</label></td>"; |
| | 1775 | $out.= "<td>$fields[user]</td>"; |
| | 1776 | $out.= '</tr><tr>'; |
| | 1777 | $out.= '<td class="title"><label for="rcmloginpwd">'; |
| | 1778 | $out.= "$labels[pass]</label></td>"; |
| | 1779 | $out.= "<td>$fields[pass]</td>"; |
| | 1780 | $out.= $form_host; |
| | 1781 | $out.= '</tr></table>'; |
| | 1782 | $out.= $form_end; |
| | 1783 | |
| | 1784 | return $out; |
| | 1785 | } |
| | 1786 | |
| | 1787 | |
| | 1788 | static function rcmail_charset_selector($attrib) |
| | 1789 | { |
| | 1790 | $registry = rc_registry::getInstance(); |
| | 1791 | $OUTPUT = $registry->get('OUTPUT', 'core'); |
| | 1792 | |
| | 1793 | // pass the following attributes to the form class |
| | 1794 | $field_attrib = array('name' => '_charset'); |
| | 1795 | foreach ($attrib as $attr => $value) { |
| | 1796 | if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex'))) { |
| | 1797 | $field_attrib[$attr] = $value; |
| | 1798 | } |
| | 1799 | } |
| | 1800 | $charsets = array( |
| | 1801 | 'US-ASCII' => 'ASCII (English)', |
| | 1802 | 'EUC-JP' => 'EUC-JP (Japanese)', |
| | 1803 | 'EUC-KR' => 'EUC-KR (Korean)', |
| | 1804 | 'BIG5' => 'BIG5 (Chinese)', |
| | 1805 | 'GB2312' => 'GB2312 (Chinese)', |
| | 1806 | 'ISO-2022-JP' => 'ISO-2022-JP (Japanese)', |
| | 1807 | 'ISO-8859-1' => 'ISO-8859-1 (Latin-1)', |
| | 1808 | 'ISO-8859-2' => 'ISO-8895-2 (Central European)', |
| | 1809 | 'ISO-8859-7' => 'ISO-8859-7 (Greek)', |
| | 1810 | 'ISO-8859-9' => 'ISO-8859-9 (Turkish)', |
| | 1811 | 'Windows-1251' => 'Windows-1251 (Cyrillic)', |
| | 1812 | 'Windows-1252' => 'Windows-1252 (Western)', |
| | 1813 | 'Windows-1255' => 'Windows-1255 (Hebrew)', |
| | 1814 | 'Windows-1256' => 'Windows-1256 (Arabic)', |
| | 1815 | 'Windows-1257' => 'Windows-1257 (Baltic)', |
| | 1816 | 'UTF-8' => 'UTF-8' |
| | 1817 | ); |
| | 1818 | |
| | 1819 | $select = new select($field_attrib); |
| | 1820 | $select->add(array_values($charsets), array_keys($charsets)); |
| | 1821 | |
| | 1822 | $set = $_POST['_charset'] ? $_POST['_charset'] : $OUTPUT->get_charset(); |
| | 1823 | return $select->show($set); |
| | 1824 | } |
| | 1825 | |
| | 1826 | |
| | 1827 | // return code for search function |
| | 1828 | static function rcmail_search_form($attrib) |
| | 1829 | { |
| | 1830 | $registry = rc_registry::getInstance(); |
| | 1831 | $OUTPUT = $registry->get('OUTPUT', 'core'); |
| | 1832 | |
| | 1833 | // add some labels to client |
| | 1834 | self::rcube_add_label('searching'); |
| | 1835 | |
| | 1836 | $attrib['name'] = '_q'; |
| | 1837 | |
| | 1838 | if (empty($attrib['id'])) { |
| | 1839 | $attrib['id'] = 'rcmqsearchbox'; |
| | 1840 | } |
| | 1841 | $input_q = new textfield($attrib); |
| | 1842 | $out = $input_q->show(); |
| | 1843 | |
| | 1844 | $OUTPUT->add_gui_object('qsearchbox', $attrib['id']); |
| | 1845 | |
| | 1846 | // add form tag around text field |
| | 1847 | if (empty($attrib['form'])) { |
| | 1848 | $_html = '<form name="rcmqsearchform" action="./" '; |
| | 1849 | $_html.= 'onsubmit="%s.command(\'search\');return false" '; |
| | 1850 | $_html.= ' style="display:inline;">%s</form>'; |
| | 1851 | $out = sprintf( |
| | 1852 | $_html, |
| | 1853 | JS_OBJECT_NAME, |
| | 1854 | $out |
| | 1855 | ); |
| | 1856 | } |
| | 1857 | return $out; |
| | 1858 | } |
| | 1859 | |
| | 1860 | |
| | 1861 | /****** debugging functions ********/ |
| | 1862 | |
| | 1863 | |
| | 1864 | /** |
| | 1865 | * Print or write debug messages |
| | 1866 | * |
| | 1867 | * @param mixed Debug message or data |
| | 1868 | */ |
| | 1869 | function console($msg) |
| | 1870 | { |
| | 1871 | $registry = rc_registry::getInstance(); |
| | 1872 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 1873 | if (!is_string($msg)) { |
| | 1874 | $msg = var_export($msg, true); |
| | 1875 | } |
| | 1876 | if (!($CONFIG['debug_level'] & 4)) { |
| | 1877 | write_log('console', $msg); |
| | 1878 | } |
| | 1879 | elseif ($GLOBALS['REMOTE_REQUEST']) { |
| | 1880 | echo "/*\n $msg \n*/\n"; |
| | 1881 | } |
| | 1882 | else { |
| | 1883 | echo '<div style="background:#eee; border:1px solid #ccc; '; |
| | 1884 | echo 'margin-bottom:3px; padding:6px"><pre>'; |
| | 1885 | echo $msg; |
| | 1886 | echo "</pre></div>\n"; |
| | 1887 | } |
| | 1888 | } |
| | 1889 | |
| | 1890 | |
| | 1891 | /** |
| | 1892 | * Append a line to a logfile in the logs directory. |
| | 1893 | * Date will be added automatically to the line. |
| | 1894 | * |
| | 1895 | * @param $name Name of logfile |
| | 1896 | * @param $line Line to append |
| | 1897 | */ |
| | 1898 | function write_log($name, $line) |
| | 1899 | { |
| | 1900 | $registry = rc_registry::getInstance(); |
| | 1901 | $CONFIG = $registry->get('CONFIG', 'core'); |
| | 1902 | |
| | 1903 | if (!is_string($line)) { |
| | 1904 | $line = var_export($line, true); |
| | 1905 | } |
| | 1906 | $log_entry = sprintf("[%s]: %s\n", |
| | 1907 | date("d-M-Y H:i:s O", mktime()), |
| | 1908 | $line); |
| | 1909 | |
| | 1910 | if (empty($CONFIG['log_dir'])) { |
| | 1911 | $CONFIG['log_dir'] = $INSTALL_PATH.'logs'; |
| | 1912 | } |
| | 1913 | // try to open specific log file for writing |
| | 1914 | if ($fp = @fopen($CONFIG['log_dir'].'/'.$name, 'a')) { |
| | 1915 | fwrite($fp, $log_entry); |
| | 1916 | fclose($fp); |
| | 1917 | } |
| | 1918 | } |
| | 1919 | |
| | 1920 | |
| | 1921 | function rcube_timer() |
| | 1922 | { |
| | 1923 | list($usec, $sec) = explode(" ", microtime()); |
| | 1924 | return ((float)$usec + (float)$sec); |
| | 1925 | } |
| | 1926 | |
| | 1927 | |
| | 1928 | function rcube_print_time($timer, $label='Timer') |
| | 1929 | { |
| | 1930 | static $print_count = 0; |
| | 1931 | |
| | 1932 | $print_count++; |
| | 1933 | $now = rcube_timer(); |
| | 1934 | $diff = $now-$timer; |
| | 1935 | |
| | 1936 | if (empty($label)) { |
| | 1937 | $label = 'Timer '.$print_count; |
| | 1938 | } |
| | 1939 | console(sprintf("%s: %0.4f sec", $label, $diff)); |
| | 1940 | } |
| 484 | | |
| 485 | | |
| 486 | | // perfom login to the IMAP server and to the webmail service |
| 487 | | function rcmail_login($user, $pass, $host=NULL) |
| 488 | | { |
| 489 | | global $CONFIG, $IMAP, $DB, $sess_user_lang; |
| 490 | | $user_id = NULL; |
| 491 | | |
| 492 | | if (!$host) { |
| 493 | | $host = $CONFIG['default_host']; |
| 494 | | } |
| 495 | | // Validate that selected host is in the list of configured hosts |
| 496 | | if (is_array($CONFIG['default_host'])) { |
| 497 | | $allowed = FALSE; |
| 498 | | foreach ($CONFIG['default_host'] as $key => $host_allowed) { |
| 499 | | if (!is_numeric($key)) { |
| 500 | | $host_allowed = $key; |
| 501 | | } |
| 502 | | if ($host == $host_allowed) { |
| 503 | | $allowed = TRUE; |
| 504 | | break; |
| 505 | | } |
| 506 | | } |
| 507 | | if (!$allowed) { |
| 508 | | return FALSE; |
| 509 | | } |
| 510 | | } |
| 511 | | else if (!empty($CONFIG['default_host']) && $host != $CONFIG['default_host']) { |
| 512 | | return FALSE; |
| 513 | | } |
| 514 | | |
| 515 | | // parse $host URL |
| 516 | | $a_host = parse_url($host); |
| 517 | | if ($a_host['host']) { |
| 518 | | $host = $a_host['host']; |
| 519 | | $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE; |
| 520 | | $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : $CONFIG['default_port']); |
| 521 | | } |
| 522 | | else { |
| 523 | | $imap_port = $CONFIG['default_port']; |
| 524 | | } |
| 525 | | |
| 526 | | /** |
| 527 | | * Modify username with domain if required |
| 528 | | * Inspired by Marco <P0L0_notspam_binware.org> |
| 529 | | */ |
| 530 | | // Check if we need to add domain |
| 531 | | //tfk_debug('User #1: ' . $user); |
| 532 | | //tfk_debug('Host: ' . $CONFIG['username_domain']); |
| 533 | | if (!empty($CONFIG['username_domain']) && !strstr($user, '@')) { |
| 534 | | if (is_array($CONFIG['username_domain']) && isset($CONFIG['username_domain'][$host])) { |
| 535 | | $user .= '@' . $CONFIG['username_domain'][$host]; |
| 536 | | } |
| 537 | | else if (is_string($CONFIG['username_domain'])) { |
| 538 | | $user .= '@' . $CONFIG['username_domain']; |
| 539 | | } |
| 540 | | } |
| 541 | | //tfk_debug('User #2: ' . $user); |
| 542 | | |
| 543 | | // query if user already registered |
| 544 | | $_query = "SELECT user_id, username, language, preferences"; |
| 545 | | $_query.= " FROM " . get_table_name('users'); |
| 546 | | $_query.= " WHERE mail_host=?"; |
| 547 | | $_query.= " AND (username=? OR alias=?)"; |
| 548 | | |
| 549 | | $sql_result = $DB->query( |
| 550 | | $_query, |
| 551 | | $host, |
| 552 | | $user, |
| 553 | | $user |
| 554 | | ); |
| 555 | | if ($DB->db_error === true) { |
| 556 | | return FALSE; |
| 557 | | } |
| 558 | | // user already registered -> overwrite username |
| 559 | | if ($sql_arr = $DB->fetch_assoc($sql_result)) { |
| 560 | | $user_id = $sql_arr['user_id']; |
| 561 | | $user = $sql_arr['username']; |
| 562 | | } |
| 563 | | |
| 564 | | // try to resolve email address from virtuser table |
| 565 | | if (!empty($CONFIG['virtuser_file']) && strstr($user, '@')) { |
| 566 | | $user = rcmail_email2user($user); |
| 567 | | } |
| 568 | | |
| 569 | | // exit if IMAP login failed |
| 570 | | if (!($imap_login = $IMAP->connect($host, $user, $pass, $imap_port, $imap_ssl))) { |
| 571 | | return FALSE; |
| 572 | | } |
| 573 | | // user already registered |
| 574 | | if ($user_id && !empty($sql_arr)) { |
| 575 | | // get user prefs |
| 576 | | if (strlen($sql_arr['preferences'])) { |
| 577 | | $user_prefs = unserialize($sql_arr['preferences']); |
| 578 | | $_SESSION['user_prefs'] = $user_prefs; |
| 579 | | array_merge($CONFIG, $user_prefs); |
| 580 | | } |
| 581 | | |
| 582 | | |
| 583 | | // set user specific language |
| 584 | | if (strlen($sql_arr['language'])) { |
| 585 | | $sess_user_lang = $_SESSION['user_lang'] = $sql_arr['language']; |
| 586 | | } |
| 587 | | // update user's record |
| 588 | | $_query = "UPDATE ".get_table_name('users'); |
| 589 | | $_query.= " SET last_login=".$DB->now(); |
| 590 | | $_query.= " WHERE user_id=?"; |
| 591 | | $DB->query($_query, $user_id); |
| 592 | | } |
| 593 | | // create new system user |
| 594 | | else if ($CONFIG['auto_create_user']) { |
| 595 | | $user_id = rcmail_create_user($user, $host); |
| 596 | | } |
| 597 | | |
| 598 | | //tfk_debug('User id: ' . $user_id); |
| 599 | | |
| 600 | | if (empty($user_id) === true) { |
| 601 | | return false; |
| 602 | | } |
| 603 | | $_SESSION['user_id'] = $user_id; |
| 604 | | $_SESSION['imap_host'] = $host; |
| 605 | | $_SESSION['imap_port'] = $imap_port; |
| 606 | | $_SESSION['imap_ssl'] = $imap_ssl; |
| 607 | | $_SESSION['username'] = $user; |
| 608 | | $_SESSION['user_lang'] = $sess_user_lang; |
| 609 | | $_SESSION['password'] = encrypt_passwd($pass); |
| 610 | | $_SESSION['login_time'] = mktime(); |
| 611 | | |
| 612 | | /** |
| 613 | | * If multi-SMTP servers, use correct one |
| 614 | | * Otherwise, use the general SMTP server |
| 615 | | * or use phpMail function |
| 616 | | * |
| 617 | | * @author Brett Patterson <brett@bpatterson.net> |
| 618 | | * @author Till Klampaeckel <till@php.net> |
| 619 | | */ |
| 620 | | if(is_array($CONFIG['smtp_server']) === true) { |
| 621 | | if (isset($CONFIG['smtp_server'][$host]) === true) { |
| 622 | | $_SESSION['smtp_server'] = $CONFIG['smtp_server'][$host]; |
| 623 | | } |
| 624 | | else { |
| 625 | | $_SESSION['smtp_server'] = 'phpMail'; |
| 626 | | } |
| 627 | | } |
| 628 | | else { |
| 629 | | if (empty($CONFIG['smtp_server']) === false) { |
| 630 | | $_SESSION['smtp_server'] = $CONFIG['smtp_server']; |
| 631 | | } |
| 632 | | else { |
| 633 | | $_SESSION['smtp_server'] = 'phpMail'; |
| 634 | | } |
| 635 | | } |
| 636 | | |
| 637 | | // force reloading complete list of subscribed mailboxes |
| 638 | | rcmail_set_imap_prop(); |
| 639 | | $IMAP->clear_cache('mailboxes'); |
| 640 | | $IMAP->create_default_folders(); |
| 641 | | |
| 642 | | return TRUE; |
| 643 | | } |
| 644 | | |
| 645 | | |
| 646 | | // create new entry in users and identities table |
| 647 | | function rcmail_create_user($user, $host) |
| 648 | | { |
| 649 | | global $DB, $CONFIG, $IMAP; |
| 650 | | |
| 651 | | $user_email = ''; |
| 652 | | |
| 653 | | // try to resolve user in virtusertable |
| 654 | | if (!empty($CONFIG['virtuser_file']) && strstr($user, '@')==FALSE) |
| 655 | | $user_email = rcmail_user2email($user); |
| 656 | | |
| 657 | | $DB->query("INSERT INTO ".get_table_name('users')." |
| 658 | | (created, last_login, username, mail_host, alias, language) |
| 659 | | VALUES (".$DB->now().", ".$DB->now().", ?, ?, ?, ?)", |
| 660 | | strip_newlines($user), |
| 661 | | strip_newlines($host), |
| 662 | | strip_newlines($user_email), |
| 663 | | $_SESSION['user_lang']); |
| 664 | | |
| 665 | | if ($user_id = $DB->insert_id(get_sequence_name('users'))) |
| 666 | | { |
| 667 | | $mail_domain = rcmail_mail_domain($host); |
| 668 | | |
| 669 | | if ($user_email=='') |
| 670 | | $user_email = strstr($user, '@') ? $user : sprintf('%s@%s', $user, $mail_domain); |
| 671 | | |
| 672 | | $user_name = $user!=$user_email ? $user : ''; |
| 673 | | |
| 674 | | // try to resolve the e-mail address from the virtuser table |
| 675 | | if (!empty($CONFIG['virtuser_query']) && |
| 676 | | ($sql_result = $DB->query(preg_replace('/%u/', $user, $CONFIG['virtuser_query']))) && |
| 677 | | ($DB->num_rows()>0)) |
| 678 | | while ($sql_arr = $DB->fetch_array($sql_result)) |
| 679 | | { |
| 680 | | $DB->query("INSERT INTO ".get_table_name('identities')." |
| 681 | | (user_id, del, standard, name, email) |
| 682 | | VALUES (?, 0, 1, ?, ?)", |
| 683 | | $user_id, |
| 684 | | strip_newlines($user_name), |
| 685 | | preg_replace('/^@/', $user . '@', $sql_arr[0])); |
| 686 | | } |
| 687 | | else |
| 688 | | { |
| 689 | | // also create new identity records |
| 690 | | $DB->query("INSERT INTO ".get_table_name('identities')." |
| 691 | | (user_id, del, standard, name, email) |
| 692 | | VALUES (?, 0, 1, ?, ?)", |
| 693 | | $user_id, |
| 694 | | strip_newlines($user_name), |
| 695 | | strip_newlines($user_email)); |
| 696 | | } |
| 697 | | |
| 698 | | // get existing mailboxes |
| 699 | | $a_mailboxes = $IMAP->list_mailboxes(); |
| 700 | | } |
| 701 | | else |
| 702 | | { |
| 703 | | raise_error(array('code' => 500, |
| 704 | | 'type' => 'php', |
| 705 | | 'line' => __LINE__, |
| 706 | | 'file' => __FILE__, |
| 707 | | 'message' => "Failed to create new user"), TRUE, FALSE); |
| 708 | | } |
| 709 | | |
| 710 | | return $user_id; |
| 711 | | } |
| 712 | | |
| 713 | | |
| 714 | | // load virtuser table in array |
| 715 | | function rcmail_getvirtualfile() |
| 716 | | { |
| 717 | | global $CONFIG; |
| 718 | | if (empty($CONFIG['virtuser_file']) || !is_file($CONFIG['virtuser_file'])) |
| 719 | | return FALSE; |
| 720 | | |
| 721 | | // read file |
| 722 | | $a_lines = file($CONFIG['virtuser_file']); |
| 723 | | return $a_lines; |
| 724 | | } |
| 725 | | |
| 726 | | |
| 727 | | // find matches of the given pattern in virtuser table |
| 728 | | function rcmail_findinvirtual($pattern) |
| 729 | | { |
| 730 | | $result = array(); |
| 731 | | $virtual = rcmail_getvirtualfile(); |
| 732 | | if ($virtual==FALSE) { |
| 733 | | return $result; |
| 734 | | } |
| 735 | | // check each line for matches |
| 736 | | foreach ($virtual as $line) { |
| 737 | | $line = trim($line); |
| 738 | | if (empty($line) || $line{0}=='#') { |
| 739 | | continue; |
| 740 | | } |
| 741 | | if (eregi($pattern, $line)) { |
| 742 | | $result[] = $line; |
| 743 | | } |
| 744 | | } |
| 745 | | return $result; |
| 746 | | } |
| 747 | | |
| 748 | | |
| 749 | | // resolve username with virtuser table |
| 750 | | function rcmail_email2user($email) |
| 751 | | { |
| 752 | | $user = $email; |
| 753 | | $r = rcmail_findinvirtual("^$email"); |
| 754 | | |
| 755 | | for ($i=0; $i<count($r); $i++) |
| 756 | | { |
| 757 | | $data = $r[$i]; |
| 758 | | $arr = preg_split('/\s+/', $data); |
| 759 | | if(count($arr)>0) |
| 760 | | { |
| 761 | | $user = trim($arr[count($arr)-1]); |
| 762 | | break; |
| 763 | | } |
| 764 | | } |
| 765 | | |
| 766 | | return $user; |
| 767 | | } |
| 768 | | |
| 769 | | |
| 770 | | // resolve e-mail address with virtuser table |
| 771 | | function rcmail_user2email($user) |
| 772 | | { |
| 773 | | $email = ""; |
| 774 | | $r = rcmail_findinvirtual("$user$"); |
| 775 | | |
| 776 | | for ($i=0; $i<count($r); $i++) |
| 777 | | { |
| 778 | | $data=$r[$i]; |
| 779 | | $arr = preg_split('/\s+/', $data); |
| 780 | | if (count($arr)>0) |
| 781 | | { |
| 782 | | $email = trim($arr[0]); |
| 783 | | break; |
| 784 | | } |
| 785 | | } |
| 786 | | |
| 787 | | return $email; |
| 788 | | } |
| 789 | | |
| 790 | | |
| 791 | | function rcmail_save_user_prefs($a_user_prefs) |
| 792 | | { |
| 793 | | global $DB, $CONFIG, $sess_user_lang; |
| 794 | | |
| 795 | | $DB->query("UPDATE ".get_table_name('users')." |
| 796 | | SET preferences=?, |
| 797 | | language=? |
| 798 | | WHERE user_id=?", |
| 799 | | serialize($a_user_prefs), |
| 800 | | $sess_user_lang, |
| 801 | | $_SESSION['user_id']); |
| 802 | | |
| 803 | | if ($DB->affected_rows()) |
| 804 | | { |
| 805 | | $_SESSION['user_prefs'] = $a_user_prefs; |
| 806 | | $CONFIG = array_merge($CONFIG, $a_user_prefs); |
| 807 | | return TRUE; |
| 808 | | } |
| 809 | | |
| 810 | | return FALSE; |
| 811 | | } |
| 812 | | |
| 813 | | |
| 814 | | // overwrite action variable |
| 815 | | function rcmail_overwrite_action($action) |
| 816 | | { |
| 817 | | global $OUTPUT; |
| 818 | | $GLOBALS['_action'] = $action; |
| 819 | | $OUTPUT->set_env('action', $action); |
| 820 | | } |
| 821 | | |
| 822 | | |
| 823 | | /** |
| 824 | | * Compose an URL for a specific action |
| 825 | | * |
| 826 | | * @param string Request action |
| 827 | | * @param array More URL parameters |
| 828 | | * @param string Request task (omit if the same) |
| 829 | | * @return The application URL |
| 830 | | */ |
| 831 | | function rcmail_url($action, $p=array(), $task=null) |
| 832 | | { |
| 833 | | global $MAIN_TASKS, $COMM_PATH; |
| 834 | | $qstring = ''; |
| 835 | | $base = $COMM_PATH; |
| 836 | | |
| 837 | | if ($task && in_array($task, $MAIN_TASKS)) |
| 838 | | $base = ereg_replace('_task=[a-z]+', '_task='.$task, $COMM_PATH); |
| 839 | | |
| 840 | | if (is_array($p)) |
| 841 | | foreach ($p as $key => $val) |
| 842 | | $qstring .= '&'.urlencode($key).'='.urlencode($val); |
| 843 | | |
| 844 | | return $base . ($action ? '&_action='.$action : '') . $qstring; |
| 845 | | } |
| 846 | | |
| 847 | | |
| 848 | | // @deprecated |
| 849 | | function show_message($message, $type='notice', $vars=NULL) |
| 850 | | { |
| 851 | | global $OUTPUT; |
| 852 | | $OUTPUT->show_message($message, $type, $vars); |
| 853 | | } |
| 854 | | |
| 855 | | |
| 856 | | // encrypt IMAP password using DES encryption |
| 857 | | function encrypt_passwd($pass) |
| 858 | | { |
| 859 | | $cypher = des(get_des_key(), $pass, 1, 0, NULL); |
| 860 | | return base64_encode($cypher); |
| 861 | | } |
| 862 | | |
| 863 | | |
| 864 | | // decrypt IMAP password using DES encryption |
| 865 | | function decrypt_passwd($cypher) |
| 866 | | { |
| 867 | | $pass = des(get_des_key(), base64_decode($cypher), 0, 0, NULL); |
| 868 | | return preg_replace('/\x00/', '', $pass); |
| 869 | | } |
| 870 | | |
| 871 | | |
| 872 | | // return a 24 byte key for the DES encryption |
| 873 | | function get_des_key() |
| 874 | | { |
| 875 | | $key = !empty($GLOBALS['CONFIG']['des_key']) ? $GLOBALS['CONFIG']['des_key'] : 'rcmail?24BitPwDkeyF**ECB'; |
| 876 | | $len = strlen($key); |
| 877 | | |
| 878 | | // make sure the key is exactly 24 chars long |
| 879 | | if ($len<24) |
| 880 | | $key .= str_repeat('_', 24-$len); |
| 881 | | else if ($len>24) |
| 882 | | substr($key, 0, 24); |
| 883 | | |
| 884 | | return $key; |
| 885 | | } |
| 886 | | |
| 887 | | |
| 888 | | // read directory program/localization/ and return a list of available languages |
| 889 | | function rcube_list_languages() |
| 890 | | { |
| 891 | | global $CONFIG, $INSTALL_PATH; |
| 892 | | static $sa_languages = array(); |
| 893 | | |
| 894 | | if (!sizeof($sa_languages)) |
| 895 | | { |
| 896 | | @include($INSTALL_PATH.'program/localization/index.inc'); |
| 897 | | |
| 898 | | if ($dh = @opendir($INSTALL_PATH.'program/localization')) |
| 899 | | { |
| 900 | | while (($name = readdir($dh)) !== false) |
| 901 | | { |
| 902 | | if ($name{0}=='.' || !is_dir($INSTALL_PATH.'program/localization/'.$name)) |
| 903 | | continue; |
| 904 | | |
| 905 | | if ($label = $rcube_languages[$name]) |
| 906 | | $sa_languages[$name] = $label ? $label : $name; |
| 907 | | } |
| 908 | | closedir($dh); |
| 909 | | } |
| 910 | | } |
| 911 | | return $sa_languages; |
| 912 | | } |
| 913 | | |
| 914 | | |
| 915 | | // add a localized label to the client environment |
| 916 | | function rcube_add_label() |
| 917 | | { |
| 918 | | global $OUTPUT; |
| 919 | | |
| 920 | | $arg_list = func_get_args(); |
| 921 | | foreach ($arg_list as $i => $name) |
| 922 | | $OUTPUT->command('add_label', $name, rcube_label($name)); |
| 923 | | } |
| 924 | | |
| 925 | | |
| 926 | | // remove temp files older than two day |
| 927 | | function rcmail_temp_gc() |
| 928 | | { |
| 929 | | $tmp = unslashify($CONFIG['temp_dir']); |
| 930 | | $expire = mktime() - 172800; // expire in 48 hours |
| 931 | | |
| 932 | | if ($dir = opendir($tmp)) |
| 933 | | { |
| 934 | | while (($fname = readdir($dir)) !== false) |
| 935 | | { |
| 936 | | if ($fname{0} == '.') |
| 937 | | continue; |
| 938 | | |
| 939 | | if (filemtime($tmp.'/'.$fname) < $expire) |
| 940 | | @unlink($tmp.'/'.$fname); |
| 941 | | } |
| 942 | | |
| 943 | | closedir($dir); |
| 944 | | } |
| 945 | | } |
| 946 | | |
| 947 | | |
| 948 | | // remove all expired message cache records |
| 949 | | function rcmail_message_cache_gc() |
| 950 | | { |
| 951 | | global $DB, $CONFIG; |
| 952 | | |
| 953 | | // no cache lifetime configured |
| 954 | | if (empty($CONFIG['message_cache_lifetime'])) |
| 955 | | return; |
| 956 | | |
| 957 | | // get target timestamp |
| 958 | | $ts = get_offset_time($CONFIG['message_cache_lifetime'], -1); |
| 959 | | |
| 960 | | $DB->query("DELETE FROM ".get_table_name('messages')." |
| 961 | | WHERE created < ".$DB->fromunixtime($ts)); |
| 962 | | } |
| 963 | | |
| 964 | | |
| 965 | | /** |
| 966 | | * Convert a string from one charset to another. |
| 967 | | * Uses mbstring and iconv functions if possible |
| 968 | | * |
| 969 | | * @param string Input string |
| 970 | | * @param string Suspected charset of the input string |
| 971 | | * @param string Target charset to convert to; defaults to RCMAIL_CHARSET |
| 972 | | * @return Converted string |
| 973 | | */ |
| 974 | | function rcube_charset_convert($str, $from, $to=NULL) |
| 975 | | { |
| 976 | | global $MBSTRING; |
| 977 | | |
| 978 | | $from = strtoupper($from); |
| 979 | | $to = $to==NULL ? strtoupper(RCMAIL_CHARSET) : strtoupper($to); |
| 980 | | |
| 981 | | if ($from==$to || $str=='' || empty($from)) |
| 982 | | return $str; |
| 983 | | |
| 984 | | // convert charset using mbstring module |
| 985 | | if ($MBSTRING) |
| 986 | | { |
| 987 | | $to = $to=="UTF-7" ? "UTF7-IMAP" : $to; |
| 988 | | $from = $from=="UTF-7" ? "UTF7-IMAP": $from; |
| 989 | | |
| 990 | | // return if convert succeeded |
| 991 | | if (($out = mb_convert_encoding($str, $to, $from)) != '') |
| 992 | | return $out; |
| 993 | | } |
| 994 | | |
| 995 | | // convert charset using iconv module |
| 996 | | if (function_exists('iconv') && $from!='UTF-7' && $to!='UTF-7') |
| 997 | | return iconv($from, $to, $str); |
| 998 | | |
| 999 | | $conv = new utf8(); |
| 1000 | | |
| 1001 | | // convert string to UTF-8 |
| 1002 | | if ($from=='UTF-7') |
| 1003 | | $str = utf7_to_utf8($str); |
| 1004 | | else if (($from=='ISO-8859-1') && function_exists('utf8_encode')) |
| 1005 | | $str = utf8_encode($str); |
| 1006 | | else if ($from!='UTF-8') |
| 1007 | | { |
| 1008 | | $conv->loadCharset($from); |
| 1009 | | $str = $conv->strToUtf8($str); |
| 1010 | | } |
| 1011 | | |
| 1012 | | // encode string for output |
| 1013 | | if ($to=='UTF-7') |
| 1014 | | return utf8_to_utf7($str); |
| 1015 | | else if ($to=='ISO-8859-1' && function_exists('utf8_decode')) |
| 1016 | | return utf8_decode($str); |
| 1017 | | else if ($to!='UTF-8') |
| 1018 | | { |
| 1019 | | $conv->loadCharset($to); |
| 1020 | | return $conv->utf8ToStr($str); |
| 1021 | | } |
| 1022 | | |
| 1023 | | // return UTF-8 string |
| 1024 | | return $str; |
| 1025 | | } |
| 1026 | | |
| 1027 | | |
| 1028 | | /** |
| 1029 | | * Replacing specials characters to a specific encoding type |
| 1030 | | * |
| 1031 | | * @param string Input string |
| 1032 | | * @param string Encoding type: text|html|xml|js|url |
| 1033 | | * @param string Replace mode for tags: show|replace|remove |
| 1034 | | * @param boolean Convert newlines |
| 1035 | | * @return The quoted string |
| 1036 | | */ |
| 1037 | | function rep_specialchars_output($str, $enctype='', $mode='', $newlines=TRUE) |
| 1038 | | { |
| 1039 | | global $OUTPUT_TYPE, $OUTPUT; |
| 1040 | | static $html_encode_arr, $js_rep_table, $xml_rep_table; |
| 1041 | | |
| 1042 | | if (!$enctype) |
| 1043 | | $enctype = $GLOBALS['OUTPUT_TYPE']; |
| 1044 | | |
| 1045 | | // convert nbsps back to normal spaces if not html |
| 1046 | | if ($enctype!='html') |
| 1047 | | $str = str_replace(chr(160), ' ', $str); |
| 1048 | | |
| 1049 | | // encode for plaintext |
| 1050 | | if ($enctype=='text') |
| 1051 | | return str_replace("\r\n", "\n", $mode=='remove' ? strip_tags($str) : $str); |
| 1052 | | |
| 1053 | | // encode for HTML output |
| 1054 | | if ($enctype=='html') |
| 1055 | | { |
| 1056 | | if (!$html_encode_arr) |
| 1057 | | { |
| 1058 | | $html_encode_arr = get_html_translation_table(HTML_SPECIALCHARS); |
| 1059 | | unset($html_encode_arr['?']); |
| 1060 | | } |
| 1061 | | |
| 1062 | | $ltpos = strpos($str, '<'); |
| 1063 | | $encode_arr = $html_encode_arr; |
| 1064 | | |
| 1065 | | // don't replace quotes and html tags |
| 1066 | | if (($mode=='show' || $mode=='') && $ltpos!==false && strpos($str, '>', $ltpos)!==false) |
| 1067 | | { |
| 1068 | | unset($encode_arr['"']); |
| 1069 | | unset($encode_arr['<']); |
| 1070 | | unset($encode_arr['>']); |
| 1071 | | unset($encode_arr['&']); |
| 1072 | | } |
| 1073 | | else if ($mode=='remove') |
| 1074 | | $str = strip_tags($str); |
| 1075 | | |
| 1076 | | // avoid douple quotation of & |
| 1077 | | $out = preg_replace('/&([a-z]{2,5}|#[0-9]{2,4});/', '&\\1;', strtr($str, $encode_arr)); |
| 1078 | | |
| 1079 | | return $newlines ? nl2br($out) : $out; |
| 1080 | | } |
| 1081 | | |
| 1082 | | if ($enctype=='url') |
| 1083 | | return rawurlencode($str); |
| 1084 | | |
| 1085 | | // if the replace tables for XML and JS are not yet defined |
| 1086 | | if (!$js_rep_table) |
| 1087 | | { |
| 1088 | | $js_rep_table = $xml_rep_table = array(); |
| 1089 | | $xml_rep_table['&'] = '&'; |
| 1090 | | |
| 1091 | | for ($c=160; $c<256; $c++) // can be increased to support more charsets |
| 1092 | | { |
| 1093 | | $hex = dechex($c); |
| 1094 | | $xml_rep_table[Chr($c)] = "&#$c;"; |
| 1095 | | |
| 1096 | | if ($OUTPUT->get_charset()=='ISO-8859-1') |
| 1097 | | $js_rep_table[Chr($c)] = sprintf("\u%s%s", str_repeat('0', 4-strlen($hex)), $hex); |
| 1098 | | } |
| 1099 | | |
| 1100 | | $xml_rep_table['"'] = '"'; |
| 1101 | | } |
| 1102 | | |
| 1103 | | // encode for XML |
| 1104 | | if ($enctype=='xml') |
| 1105 | | return strtr($str, $xml_rep_table); |
| 1106 | | |
| 1107 | | // encode for javascript use |
| 1108 | | if ($enctype=='js') |
| 1109 | | { |
| 1110 | | if ($OUTPUT->get_charset()!='UTF-8') |
| 1111 | | $str = rcube_charset_convert($str, RCMAIL_CHARSET, $OUTPUT->get_charset()); |
| 1112 | | |
| 1113 | | return preg_replace(array("/\r?\n/", "/\r/"), array('\n', '\n'), addslashes(strtr($str, $js_rep_table))); |
| 1114 | | } |
| 1115 | | |
| 1116 | | // no encoding given -> return original string |
| 1117 | | return $str; |
| 1118 | | } |
| 1119 | | |
| 1120 | | /** |
| 1121 | | * Quote a given string. Alias function for rep_specialchars_output |
| 1122 | | * @see rep_specialchars_output |
| 1123 | | */ |
| 1124 | | function Q($str, $mode='strict', $newlines=TRUE) |
| 1125 | | { |
| 1126 | | return rep_specialchars_output($str, 'html', $mode, $newlines); |
| 1127 | | } |
| 1128 | | |
| 1129 | | /** |
| 1130 | | * Quote a given string. Alias function for rep_specialchars_output |
| 1131 | | * @see rep_specialchars_output |
| 1132 | | */ |
| 1133 | | function JQ($str) |
| 1134 | | { |
| 1135 | | return rep_specialchars_output($str, 'js'); |
| 1136 | | } |
| 1137 | | |
| 1138 | | |
| 1139 | | /** |
| 1140 | | * Read input value and convert it for internal use |
| 1141 | | * Performs stripslashes() and charset conversion if necessary |
| 1142 | | * |
| 1143 | | * @param string Field name to read |
| 1144 | | * @param int Source to get value from (GPC) |
| 1145 | | * @param boolean Allow HTML tags in field value |
| 1146 | | * @param string Charset to convert into |
| 1147 | | * @return string Field value or NULL if not available |
| 1148 | | */ |
| 1149 | | function get_input_value($fname, $source, $allow_html=FALSE, $charset=NULL) |
| 1150 | | { |
| 1151 | | global $OUTPUT; |
| 1152 | | $value = NULL; |
| 1153 | | |
| 1154 | | if ($source==RCUBE_INPUT_GET && isset($_GET[$fname])) |
| 1155 | | $value = $_GET[$fname]; |
| 1156 | | else if ($source==RCUBE_INPUT_POST && isset($_POST[$fname])) |
| 1157 | | $value = $_POST[$fname]; |
| 1158 | | else if ($source==RCUBE_INPUT_GPC) |
| 1159 | | { |
| 1160 | | if (isset($_POST[$fname])) |
| 1161 | | $value = $_POST[$fname]; |
| 1162 | | else if (isset($_GET[$fname])) |
| 1163 | | $value = $_GET[$fname]; |
| 1164 | | else if (isset($_COOKIE[$fname])) |
| 1165 | | $value = $_COOKIE[$fname]; |
| 1166 | | } |
| 1167 | | |
| 1168 | | // strip slashes if magic_quotes enabled |
| 1169 | | if ((bool)get_magic_quotes_gpc()) |
| 1170 | | $value = stripslashes($value); |
| 1171 | | |
| 1172 | | // remove HTML tags if not allowed |
| 1173 | | if (!$allow_html) |
| 1174 | | $value = strip_tags($value); |
| 1175 | | |
| 1176 | | // convert to internal charset |
| 1177 | | if (is_object($OUTPUT)) |
| 1178 | | return rcube_charset_convert($value, $OUTPUT->get_charset(), $charset); |
| 1179 | | else |
| 1180 | | return $value; |
| 1181 | | } |
| 1182 | | |
| 1183 | | /** |
| 1184 | | * Remove single and double quotes from given string |
| 1185 | | */ |
| 1186 | | function strip_quotes($str) |
| 1187 | | { |
| 1188 | | return preg_replace('/[\'"]/', '', $str); |
| 1189 | | } |
| 1190 | | |
| 1191 | | /** |
| 1192 | | * Remove new lines characters from given string |
| 1193 | | */ |
| 1194 | | function strip_newlines($str) |
| 1195 | | { |
| 1196 | | return preg_replace('/[\r\n]/', '', $str); |
| 1197 | | } |
| 1198 | | |
| 1199 | | |
| 1200 | | // return boolean if a specific template exists |
| 1201 | | function template_exists($name) |
| 1202 | | { |
| 1203 | | global $CONFIG; |
| 1204 | | $skin_path = $CONFIG['skin_path']; |
| 1205 | | |
| 1206 | | // check template file |
| 1207 | | return is_file("$skin_path/templates/$name.html"); |
| 1208 | | } |
| 1209 | | |
| 1210 | | |
| 1211 | | // Wrapper for rcmail_template::parse() |
| 1212 | | // @deprecated |
| 1213 | | function parse_template($name='main', $exit=true) |
| 1214 | | { |
| 1215 | | $GLOBALS['OUTPUT']->parse($name, $exit); |
| 1216 | | } |
| 1217 | | |
| 1218 | | |
| 1219 | | |
| 1220 | | function rcube_table_output($attrib, $table_data, $a_show_cols, $id_col) |
| 1221 | | { |
| 1222 | | global $DB; |
| 1223 | | |
| 1224 | | // allow the following attributes to be added to the <table> tag |
| 1225 | | $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary')); |
| 1226 | | |
| 1227 | | $table = '<table' . $attrib_str . ">\n"; |
| 1228 | | |
| 1229 | | // add table title |
| 1230 | | $table .= "<thead><tr>\n"; |
| 1231 | | |
| 1232 | | foreach ($a_show_cols as $col) |
| 1233 | | $table .= '<td class="'.$col.'">' . Q(rcube_label($col)) . "</td>\n"; |
| 1234 | | |
| 1235 | | $table .= "</tr></thead>\n<tbody>\n"; |
| 1236 | | |
| 1237 | | $c = 0; |
| 1238 | | if (!is_array($table_data)) |
| 1239 | | { |
| 1240 | | while ($table_data && ($sql_arr = $DB->fetch_assoc($table_data))) |
| 1241 | | { |
| 1242 | | $zebra_class = $c%2 ? 'even' : 'odd'; |
| 1243 | | |
| 1244 | | $table .= sprintf('<tr id="rcmrow%d" class="contact '.$zebra_class.'">'."\n", $sql_arr[$id_col]); |
| 1245 | | |
| 1246 | | // format each col |
| 1247 | | foreach ($a_show_cols as $col) |
| 1248 | | { |
| 1249 | | $cont = Q($sql_arr[$col]); |
| 1250 | | $table .= '<td class="'.$col.'">' . $cont . "</td>\n"; |
| 1251 | | } |
| 1252 | | |
| 1253 | | $table .= "</tr>\n"; |
| 1254 | | $c++; |
| 1255 | | } |
| 1256 | | } |
| 1257 | | else |
| 1258 | | { |
| 1259 | | foreach ($table_data as $row_data) |
| 1260 | | { |
| 1261 | | $zebra_class = $c%2 ? 'even' : 'odd'; |
| 1262 | | |
| 1263 | | $table .= sprintf('<tr id="rcmrow%d" class="contact '.$zebra_class.'">'."\n", $row_data[$id_col]); |
| 1264 | | |
| 1265 | | // format each col |
| 1266 | | foreach ($a_show_cols as $col) |
| 1267 | | { |
| 1268 | | $cont = Q($row_data[$col]); |
| 1269 | | $table .= '<td class="'.$col.'">' . $cont . "</td>\n"; |
| 1270 | | } |
| 1271 | | |
| 1272 | | $table .= "</tr>\n"; |
| 1273 | | $c++; |
| 1274 | | } |
| 1275 | | } |
| 1276 | | |
| 1277 | | // complete message table |
| 1278 | | $table .= "</tbody></table>\n"; |
| 1279 | | |
| 1280 | | return $table; |
| 1281 | | } |
| 1282 | | |
| 1283 | | |
| 1284 | | /** |
| 1285 | | * Create an edit field for inclusion on a form |
| 1286 | | * |
| 1287 | | * @param string col field name |
| 1288 | | * @param string value field value |
| 1289 | | * @param array attrib HTML element attributes for field |
| 1290 | | * @param string type HTML element type (default 'text') |
| 1291 | | * @return string HTML field definition |
| 1292 | | */ |
| 1293 | | function rcmail_get_edit_field($col, $value, $attrib, $type='text') |
| 1294 | | { |
| 1295 | | $fname = '_'.$col; |
| 1296 | | $attrib['name'] = $fname; |
| 1297 | | |
| 1298 | | if ($type=='checkbox') |
| 1299 | | { |
| 1300 | | $attrib['value'] = '1'; |
| 1301 | | $input = new checkbox($attrib); |
| 1302 | | } |
| 1303 | | else if ($type=='textarea') |
| 1304 | | { |
| 1305 | | $attrib['cols'] = $attrib['size']; |
| 1306 | | $input = new textarea($attrib); |
| 1307 | | } |
| 1308 | | else |
| 1309 | | $input = new textfield($attrib); |
| 1310 | | |
| 1311 | | // use value from post |
| 1312 | | if (!empty($_POST[$fname])) |
| 1313 | | $value = $_POST[$fname]; |
| 1314 | | |
| 1315 | | $out = $input->show($value); |
| 1316 | | |
| 1317 | | return $out; |
| 1318 | | } |
| 1319 | | |
| 1320 | | |
| 1321 | | // return the mail domain configured for the given host |
| 1322 | | function rcmail_mail_domain($host) |
| 1323 | | { |
| 1324 | | global $CONFIG; |
| 1325 | | |
| 1326 | | $domain = $host; |
| 1327 | | if (is_array($CONFIG['mail_domain'])) |
| 1328 | | { |
| 1329 | | if (isset($CONFIG['mail_domain'][$host])) |
| 1330 | | $domain = $CONFIG['mail_domain'][$host]; |
| 1331 | | } |
| 1332 | | else if (!empty($CONFIG['mail_domain'])) |
| 1333 | | $domain = $CONFIG['mail_domain']; |
| 1334 | | |
| 1335 | | return $domain; |
| 1336 | | } |
| 1337 | | |
| 1338 | | |
| 1339 | | // compose a valid attribute string for HTML tags |
| 1340 | | function create_attrib_string($attrib, $allowed_attribs=array('id', 'class', 'style')) |
| 1341 | | { |
| 1342 | | // allow the following attributes to be added to the <iframe> tag |
| 1343 | | $attrib_str = ''; |
| 1344 | | foreach ($allowed_attribs as $a) |
| 1345 | | if (isset($attrib[$a])) |
| 1346 | | $attrib_str .= sprintf(' %s="%s"', $a, str_replace('"', '"', $attrib[$a])); |
| 1347 | | |
| 1348 | | return $attrib_str; |
| 1349 | | } |
| 1350 | | |
| 1351 | | |
| 1352 | | // convert a HTML attribute string attributes to an associative array (name => value) |
| 1353 | | function parse_attrib_string($str) |
| 1354 | | { |
| 1355 | | $attrib = array(); |
| 1356 | | preg_match_all('/\s*([-_a-z]+)=(["\'])([^"]+)\2/Ui', stripslashes($str), $regs, PREG_SET_ORDER); |
| 1357 | | |
| 1358 | | // convert attributes to an associative array (name => value) |
| 1359 | | if ($regs) |
| 1360 | | foreach ($regs as $attr) |
| 1361 | | $attrib[strtolower($attr[1])] = $attr[3]; |
| 1362 | | |
| 1363 | | return $attrib; |
| 1364 | | } |
| 1365 | | |
| 1366 | | |
| 1367 | | function format_date($date, $format=NULL) |
| 1368 | | { |
| 1369 | | global $CONFIG, $sess_user_lang; |
| 1370 | | |
| 1371 | | $ts = NULL; |
| 1372 | | |
| 1373 | | if (is_numeric($date)) |
| 1374 | | $ts = $date; |
| 1375 | | else if (!empty($date)) |
| 1376 | | $ts = @strtotime($date); |
| 1377 | | |
| 1378 | | if (empty($ts)) |
| 1379 | | return ''; |
| 1380 | | |
| 1381 | | // get user's timezone |
| 1382 | | $tz = $CONFIG['timezone']; |
| 1383 | | if ($CONFIG['dst_active']) |
| 1384 | | $tz++; |
| 1385 | | |
| 1386 | | // convert time to user's timezone |
| 1387 | | $timestamp = $ts - date('Z', $ts) + ($tz * 3600); |
| 1388 | | |
| 1389 | | // get current timestamp in user's timezone |
| 1390 | | $now = time(); // local time |
| 1391 | | $now -= (int)date('Z'); // make GMT time |
| 1392 | | $now += ($tz * 3600); // user's time |
| 1393 | | $now_date = getdate($now); |
| 1394 | | |
| 1395 | | $today_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday'], $now_date['year']); |
| 1396 | | $week_limit = mktime(0, 0, 0, $now_date['mon'], $now_date['mday']-6, $now_date['year']); |
| 1397 | | |
| 1398 | | // define date format depending on current time |
| 1399 | | if ($CONFIG['prettydate'] && !$format && $timestamp > $today_limit && $timestamp < $now) |
| 1400 | | return sprintf('%s %s', rcube_label('today'), date($CONFIG['date_today'] ? $CONFIG['date_today'] : 'H:i', $timestamp)); |
| 1401 | | else if ($CONFIG['prettydate'] && !$format && $timestamp > $week_limit && $timestamp < $now) |
| 1402 | | $format = $CONFIG['date_short'] ? $CONFIG['date_short'] : 'D H:i'; |
| 1403 | | else if (!$format) |
| 1404 | | $format = $CONFIG['date_long'] ? $CONFIG['date_long'] : 'd.m.Y H:i'; |
| 1405 | | |
| 1406 | | |
| 1407 | | // parse format string manually in order to provide localized weekday and month names |
| 1408 | | // an alternative would be to convert the date() format string to fit with strftime() |
| 1409 | | $out = ''; |
| 1410 | | for($i=0; $i<strlen($format); $i++) |
| 1411 | | { |
| 1412 | | if ($format{$i}=='\\') // skip escape chars |
| 1413 | | continue; |
| 1414 | | |
| 1415 | | // write char "as-is" |
| 1416 | | if ($format{$i}==' ' || $format{$i-1}=='\\') |
| 1417 | | $out .= $format{$i}; |
| 1418 | | // weekday (short) |
| 1419 | | else if ($format{$i}=='D') |
| 1420 | | $out .= rcube_label(strtolower(date('D', $timestamp))); |
| 1421 | | // weekday long |
| 1422 | | else if ($format{$i}=='l') |
| 1423 | | $out .= rcube_label(strtolower(date('l', $timestamp))); |
| 1424 | | // month name (short) |
| 1425 | | else if ($format{$i}=='M') |
| 1426 | | $out .= rcube_label(strtolower(date('M', $timestamp))); |
| 1427 | | // month name (long) |
| 1428 | | else if ($format{$i}=='F') |
| 1429 | | $out .= rcube_label(strtolower(date('F', $timestamp))); |
| 1430 | | else |
| 1431 | | $out .= date($format{$i}, $timestamp); |
| 1432 | | } |
| 1433 | | |
| 1434 | | return $out; |
| 1435 | | } |
| 1436 | | |
| 1437 | | |
| 1438 | | function format_email_recipient($email, $name='') |
| 1439 | | { |
| 1440 | | if ($name && $name != $email) |
| 1441 | | return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email); |
| 1442 | | else |
| 1443 | | return $email; |
| 1444 | | } |
| 1445 | | |
| 1446 | | |
| 1447 | | |
| 1448 | | // ************** functions delivering gui objects ************** |
| 1449 | | |
| 1450 | | |
| 1451 | | |
| 1452 | | function rcmail_message_container($attrib) |
| 1453 | | { |
| 1454 | | global $OUTPUT; |
| 1455 | | |
| 1456 | | if (!$attrib['id']) { |
| 1457 | | $attrib['id'] = 'rcmMessageContainer'; |
| 1458 | | } |
| 1459 | | // allow the following attributes to be added to the <table> tag |
| 1460 | | $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id')); |
| 1461 | | $out = '<div' . $attrib_str . "></div>"; |
| 1462 | | |
| 1463 | | $OUTPUT->add_gui_object('message', $attrib['id']); |
| 1464 | | |
| 1465 | | return $out; |
| 1466 | | } |
| 1467 | | |
| 1468 | | |
| 1469 | | // return the IMAP username of the current session |
| 1470 | | function rcmail_current_username($attrib) |
| 1471 | | { |
| 1472 | | global $DB; |
| 1473 | | static $s_username; |
| 1474 | | |
| 1475 | | // alread fetched |
| 1476 | | if (!empty($s_username)) { |
| 1477 | | return $s_username; |
| 1478 | | } |
| 1479 | | // get e-mail address form default identity |
| 1480 | | $_query = "SELECT email AS mailto"; |
| 1481 | | $_query.= " FROM " . get_table_name('identities'); |
| 1482 | | $_query.= " WHERE user_id=?"; |
| 1483 | | $_query.= " AND standard=1"; |
| 1484 | | $_query.= " AND del<>1"; |
| 1485 | | $sql_result = $DB->query( |
| 1486 | | $_query, |
| 1487 | | $_SESSION['user_id'] |
| 1488 | | ); |
| 1489 | | |
| 1490 | | if ($DB->num_rows($sql_result)) { |
| 1491 | | $sql_arr = $DB->fetch_assoc($sql_result); |
| 1492 | | $s_username = $sql_arr['mailto']; |
| 1493 | | } |
| 1494 | | elseif (strstr($_SESSION['username'], '@')) { |
| 1495 | | $s_username = $_SESSION['username']; |
| 1496 | | } |
| 1497 | | else { |
| 1498 | | $s_username = $_SESSION['username'].'@'.$_SESSION['imap_host']; |
| 1499 | | } |
| 1500 | | return $s_username; |
| 1501 | | } |
| 1502 | | |
| 1503 | | |
| 1504 | | // return code for the webmail login form |
| 1505 | | function rcmail_login_form($attrib) |
| 1506 | | { |
| 1507 | | global $CONFIG, $OUTPUT, $SESS_HIDDEN_FIELD; |
| 1508 | | |
| 1509 | | $_SESSION['temp'] = true; |
| 1510 | | |
| 1511 | | $labels = array(); |
| 1512 | | $labels['user'] = rcube_label('username'); |
| 1513 | | $labels['pass'] = rcube_label('password'); |
| 1514 | | $labels['host'] = rcube_label('server'); |
| 1515 | | |
| 1516 | | $input_user = new textfield(array('name' => '_user', 'id' => 'rcmloginuser', 'size' => 30, 'autocomplete' => 'off')); |
| 1517 | | $input_pass = new passwordfield(array('name' => '_pass', 'id' => 'rcmloginpwd', 'size' => 30)); |
| 1518 | | $input_action = new hiddenfield(array('name' => '_action', 'value' => 'login')); |
| 1519 | | |
| 1520 | | $fields = array(); |
| 1521 | | $fields['user'] = $input_user->show(get_input_value('_user', RCUBE_INPUT_POST)); |
| 1522 | | $fields['pass'] = $input_pass->show(); |
| 1523 | | $fields['action'] = $input_action->show(); |
| 1524 | | |
| 1525 | | if (is_array($CONFIG['default_host'])) { |
| 1526 | | $select_host = new select(array('name' => '_host', 'id' => 'rcmloginhost')); |
| 1527 | | |
| 1528 | | foreach ($CONFIG['default_host'] as $key => $value) { |
| 1529 | | if (!is_array($value)) { |
| 1530 | | $select_host->add($value, (is_numeric($key) ? $value : $key)); |
| 1531 | | } |
| 1532 | | else { |
| 1533 | | unset($select_host); |
| 1534 | | break; |
| 1535 | | } |
| 1536 | | } |
| 1537 | | $fields['host'] = isset($select_host) ? $select_host->show($_POST['_host']) : null; |
| 1538 | | } |
| 1539 | | else if (!strlen($CONFIG['default_host'])) { |
| 1540 | | $input_host = new textfield(array('name' => '_host', 'id' => 'rcmloginhost', 'size' => 30)); |
| 1541 | | $fields['host'] = $input_host->show($_POST['_host']); |
| 1542 | | } |
| 1543 | | |
| 1544 | | $form_name = strlen($attrib['form']) ? $attrib['form'] : 'form'; |
| 1545 | | $form_start = !strlen($attrib['form']) ? '<form name="form" action="./" method="post">' : ''; |
| 1546 | | $form_end = !strlen($attrib['form']) ? '</form>' : ''; |
| 1547 | | $form_host = ''; |
| 1548 | | |
| 1549 | | if ($fields['host']) { |
| 1550 | | $form_host.= "</tr><tr>"; |
| 1551 | | $form_host.= "<td class=\"title\"><label for=\"rcmloginhost\">$labels[host]</label></td>"; |
| 1552 | | $form_host.= "<td>$fields[host]</td>"; |
| 1553 | | |
| 1554 | | } |
| 1555 | | $OUTPUT->add_gui_object('loginform', $form_name); |
| 1556 | | |
| 1557 | | $out = ''; |
| 1558 | | $out.= $form_start; |
| 1559 | | $out.= $SESS_HIDDEN_FIELD; |
| 1560 | | $out.= $fields[action]; |
| 1561 | | $out.= '<table><tr>'; |
| 1562 | | $out.= '<td class="title"><label for="rcmloginuser">'; |
| 1563 | | $out.= "$labels[user]</label></td>"; |
| 1564 | | $out.= "<td>$fields[user]</td>"; |
| 1565 | | $out.= '</tr><tr>'; |
| 1566 | | $out.= '<td class="title"><label for="rcmloginpwd">'; |
| 1567 | | $out.= "$labels[pass]</label></td>"; |
| 1568 | | $out.= "<td>$fields[pass]</td>"; |
| 1569 | | $out.= $form_host; |
| 1570 | | $out.= '</tr></table>'; |
| 1571 | | $out.= $form_end; |
| 1572 | | |
| 1573 | | return $out; |
| 1574 | | } |
| 1575 | | |
| 1576 | | |
| 1577 | | function rcmail_charset_selector($attrib) |
| 1578 | | { |
| 1579 | | global $OUTPUT; |
| 1580 | | |
| 1581 | | // pass the following attributes to the form class |
| 1582 | | $field_attrib = array('name' => '_charset'); |
| 1583 | | foreach ($attrib as $attr => $value) { |
| 1584 | | if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex'))) { |
| 1585 | | $field_attrib[$attr] = $value; |
| 1586 | | } |
| 1587 | | } |
| 1588 | | $charsets = array( |
| 1589 | | 'US-ASCII' => 'ASCII (English)', |
| 1590 | | 'EUC-JP' => 'EUC-JP (Japanese)', |
| 1591 | | 'EUC-KR' => 'EUC-KR (Korean)', |
| 1592 | | 'BIG5' => 'BIG5 (Chinese)', |
| 1593 | | 'GB2312' => 'GB2312 (Chinese)', |
| 1594 | | 'ISO-2022-JP' => 'ISO-2022-JP (Japanese)', |
| 1595 | | 'ISO-8859-1' => 'ISO-8859-1 (Latin-1)', |
| 1596 | | 'ISO-8859-2' => 'ISO-8895-2 (Central European)', |
| 1597 | | 'ISO-8859-7' => 'ISO-8859-7 (Greek)', |
| 1598 | | 'ISO-8859-9' => 'ISO-8859-9 (Turkish)', |
| 1599 | | 'Windows-1251' => 'Windows-1251 (Cyrillic)', |
| 1600 | | 'Windows-1252' => 'Windows-1252 (Western)', |
| 1601 | | 'Windows-1255' => 'Windows-1255 (Hebrew)', |
| 1602 | | 'Windows-1256' => 'Windows-1256 (Arabic)', |
| 1603 | | 'Windows-1257' => 'Windows-1257 (Baltic)', |
| 1604 | | 'UTF-8' => 'UTF-8' |
| 1605 | | ); |
| 1606 | | |
| 1607 | | $select = new select($field_attrib); |
| 1608 | | $select->add(array_values($charsets), array_keys($charsets)); |
| 1609 | | |
| 1610 | | $set = $_POST['_charset'] ? $_POST['_charset'] : $OUTPUT->get_charset(); |
| 1611 | | return $select->show($set); |
| 1612 | | } |
| 1613 | | |
| 1614 | | |
| 1615 | | // return code for search function |
| 1616 | | function rcmail_search_form($attrib) |
| 1617 | | { |
| 1618 | | global $OUTPUT; |
| 1619 | | |
| 1620 | | // add some labels to client |
| 1621 | | rcube_add_label('searching'); |
| 1622 | | |
| 1623 | | $attrib['name'] = '_q'; |
| 1624 | | |
| 1625 | | if (empty($attrib['id'])) { |
| 1626 | | $attrib['id'] = 'rcmqsearchbox'; |
| 1627 | | } |
| 1628 | | $input_q = new textfield($attrib); |
| 1629 | | $out = $input_q->show(); |
| 1630 | | |
| 1631 | | $OUTPUT->add_gui_object('qsearchbox', $attrib['id']); |
| 1632 | | |
| 1633 | | // add form tag around text field |
| 1634 | | if (empty($attrib['form'])) { |
| 1635 | | $_html = '<form name="rcmqsearchform" action="./" '; |
| 1636 | | $_html.= 'onsubmit="%s.command(\'search\');return false" '; |
| 1637 | | $_html.= ' style="display:inline;">%s</form>'; |
| 1638 | | $out = sprintf( |
| 1639 | | $_html, |
| 1640 | | JS_OBJECT_NAME, |
| 1641 | | $out |
| 1642 | | ); |
| 1643 | | } |
| 1644 | | return $out; |
| 1645 | | } |
| 1646 | | |
| 1647 | | |
| 1648 | | /****** debugging functions ********/ |
| 1649 | | |
| 1650 | | |
| 1651 | | /** |
| 1652 | | * Print or write debug messages |
| 1653 | | * |
| 1654 | | * @param mixed Debug message or data |
| 1655 | | */ |
| 1656 | | function console($msg) |
| 1657 | | { |
| 1658 | | if (!is_string($msg)) { |
| 1659 | | $msg = var_export($msg, true); |
| 1660 | | } |
| 1661 | | if (!($GLOBALS['CONFIG']['debug_level'] & 4)) { |
| 1662 | | write_log('console', $msg); |
| 1663 | | } |
| 1664 | | elseif ($GLOBALS['REMOTE_REQUEST']) { |
| 1665 | | echo "/*\n $msg \n*/\n"; |
| 1666 | | } |
| 1667 | | else { |
| 1668 | | echo '<div style="background:#eee; border:1px solid #ccc; '; |
| 1669 | | echo 'margin-bottom:3px; padding:6px"><pre>'; |
| 1670 | | echo $msg; |
| 1671 | | echo "</pre></div>\n"; |
| 1672 | | } |
| 1673 | | } |
| 1674 | | |
| 1675 | | |
| 1676 | | /** |
| 1677 | | * Append a line to a logfile in the logs directory. |
| 1678 | | * Date will be added automatically to the line. |
| 1679 | | * |
| 1680 | | * @param $name Name of logfile |
| 1681 | | * @param $line Line to append |
| 1682 | | */ |
| 1683 | | function write_log($name, $line) |
| 1684 | | { |
| 1685 | | global $CONFIG; |
| 1686 | | |
| 1687 | | if (!is_string($line)) { |
| 1688 | | $line = var_export($line, true); |
| 1689 | | } |
| 1690 | | $log_entry = sprintf("[%s]: %s\n", |
| 1691 | | date("d-M-Y H:i:s O", mktime()), |
| 1692 | | $line); |
| 1693 | | |
| 1694 | | if (empty($CONFIG['log_dir'])) { |
| 1695 | | $CONFIG['log_dir'] = $INSTALL_PATH.'logs'; |
| 1696 | | } |
| 1697 | | // try to open specific log file for writing |
| 1698 | | if ($fp = @fopen($CONFIG['log_dir'].'/'.$name, 'a')) { |
| 1699 | | fwrite($fp, $log_entry); |
| 1700 | | fclose($fp); |
| 1701 | | } |
| 1702 | | } |
| 1703 | | |
| 1704 | | |
| 1705 | | function rcube_timer() |
| 1706 | | { |
| 1707 | | list($usec, $sec) = explode(" ", microtime()); |
| 1708 | | return ((float)$usec + (float)$sec); |
| 1709 | | } |
| 1710 | | |
| 1711 | | |
| 1712 | | function rcube_print_time($timer, $label='Timer') |
| 1713 | | { |
| 1714 | | static $print_count = 0; |
| 1715 | | |
| 1716 | | $print_count++; |
| 1717 | | $now = rcube_timer(); |
| 1718 | | $diff = $now-$timer; |
| 1719 | | |
| 1720 | | if (empty($label)) { |
| 1721 | | $label = 'Timer '.$print_count; |
| 1722 | | } |
| 1723 | | console(sprintf("%s: %0.4f sec", $label, $diff)); |
| 1724 | | } |
| 1725 | | |
| 1726 | | ?> |