From fba1f5ab813f2eb4bedc5d9c4a75e77bbaa90131 Mon Sep 17 00:00:00 2001 From: thomascube <thomas@roundcube.net> Date: Mon, 10 Dec 2007 10:27:19 -0500 Subject: [PATCH] New class rcube_user + send message disposition notification --- program/include/main.inc | 335 +++++++++++++++++++++---------------------------------- 1 files changed, 128 insertions(+), 207 deletions(-) diff --git a/program/include/main.inc b/program/include/main.inc index 854458a..7892d09 100644 --- a/program/include/main.inc +++ b/program/include/main.inc @@ -29,6 +29,8 @@ require_once('lib/des.inc'); require_once('lib/utf7.inc'); require_once('lib/utf8.class.php'); +require_once('include/rcube_user.inc'); +require_once('include/rcube_shared.inc'); require_once('include/rcmail_template.inc'); @@ -47,7 +49,7 @@ function rcmail_startup($task='mail') { global $sess_id, $sess_user_lang; - global $CONFIG, $INSTALL_PATH, $BROWSER, $OUTPUT, $_SESSION, $IMAP, $DB; + global $CONFIG, $INSTALL_PATH, $BROWSER, $OUTPUT, $_SESSION, $IMAP, $DB, $USER; // check client $BROWSER = rcube_browser(); @@ -86,6 +88,8 @@ // set session vars global $sess_user_lang = rcube_language_prop($_SESSION['user_lang']); + // create user object + $USER = new rcube_user($_SESSION['user_id']); // overwrite config with user preferences if (is_array($_SESSION['user_prefs'])) @@ -320,21 +324,18 @@ */ function rcmail_kill_session() { - // save user preferences - $a_user_prefs = $_SESSION['user_prefs']; - if (!is_array($a_user_prefs)) - $a_user_prefs = array(); - + global $USER; + if ((isset($_SESSION['sort_col']) && $_SESSION['sort_col']!=$a_user_prefs['message_sort_col']) || (isset($_SESSION['sort_order']) && $_SESSION['sort_order']!=$a_user_prefs['message_sort_order'])) { - $a_user_prefs['message_sort_col'] = $_SESSION['sort_col']; - $a_user_prefs['message_sort_order'] = $_SESSION['sort_order']; - rcmail_save_user_prefs($a_user_prefs); + $a_user_prefs = array('message_sort_col' => $_SESSION['sort_col'], 'message_sort_order' => $_SESSION['sort_order']); + $USER->save_prefs($a_user_prefs); } $_SESSION = array('user_lang' => $GLOBALS['sess_user_lang'], 'auth_time' => time(), 'temp' => true); setcookie('sessauth', '-del-', time()-60); + $USER->reset(); } @@ -530,7 +531,7 @@ */ function rcmail_login($user, $pass, $host=NULL) { - global $CONFIG, $IMAP, $DB, $sess_user_lang; + global $CONFIG, $IMAP, $DB, $USER, $sess_user_lang; $user_id = NULL; if (!$host) @@ -580,69 +581,71 @@ $user .= '@'.$CONFIG['username_domain']; } + // try to resolve email address from virtuser table + if (!empty($CONFIG['virtuser_file']) && strpos($user, '@')) + $user = rcube_user::email2user($user); + // lowercase username if it's an e-mail address (#1484473) if (strpos($user, '@')) $user = strtolower($user); // query if user already registered - $sql_result = $DB->query("SELECT user_id, username, language, preferences - FROM ".get_table_name('users')." - WHERE mail_host=? AND (username=? OR alias=?)", - $host, - $user, - $user); + if ($existing = rcube_user::query($user, $host)) + $USER = $existing; // user already registered -> overwrite username - if ($sql_arr = $DB->fetch_assoc($sql_result)) + if ($USER->ID) { - $user_id = $sql_arr['user_id']; - $user = $sql_arr['username']; + $user_id = $USER->ID; + $user = $USER->data['username']; } - - // try to resolve email address from virtuser table - if (!empty($CONFIG['virtuser_file']) && strpos($user, '@')) - $user = rcmail_email2user($user); - // exit if IMAP login failed if (!($imap_login = $IMAP->connect($host, $user, $pass, $imap_port, $imap_ssl))) - return FALSE; + return false; // user already registered - if ($user_id && !empty($sql_arr)) + if ($USER->ID) { // get user prefs - if (strlen($sql_arr['preferences'])) - { - $user_prefs = unserialize($sql_arr['preferences']); - $_SESSION['user_prefs'] = $user_prefs; - array_merge($CONFIG, $user_prefs); - } - + $_SESSION['user_prefs'] = $USER->get_prefs(); + array_merge($CONFIG, $_SESSION['user_prefs']); // set user specific language - if (strlen($sql_arr['language'])) - $sess_user_lang = $_SESSION['user_lang'] = $sql_arr['language']; + if (!empty($USER->data['language'])) + $sess_user_lang = $_SESSION['user_lang'] = $USER->data['language']; // update user's record - $DB->query("UPDATE ".get_table_name('users')." - SET last_login=".$DB->now()." - WHERE user_id=?", - $user_id); + $USER->touch(); } // create new system user else if ($CONFIG['auto_create_user']) { - $user_id = rcmail_create_user($user, $host); + if ($created = rcube_user::create($user, $host)) + { + $USER = $created; + + // get existing mailboxes + $a_mailboxes = $IMAP->list_mailboxes(); + } + } + else + { + raise_error(array( + 'code' => 600, + 'type' => 'php', + 'file' => "config/main.inc.php", + 'message' => "Acces denied for new user $user. 'auto_create_user' is disabled" + ), true, false); } - if ($user_id) + if ($USER->ID) { - $_SESSION['user_id'] = $user_id; + $_SESSION['user_id'] = $USER->ID; + $_SESSION['username'] = $USER->data['username']; $_SESSION['imap_host'] = $host; $_SESSION['imap_port'] = $imap_port; $_SESSION['imap_ssl'] = $imap_ssl; - $_SESSION['username'] = $user; $_SESSION['user_lang'] = $sess_user_lang; $_SESSION['password'] = encrypt_passwd($pass); $_SESSION['login_time'] = mktime(); @@ -650,90 +653,15 @@ // force reloading complete list of subscribed mailboxes rcmail_set_imap_prop(); $IMAP->clear_cache('mailboxes'); - $IMAP->create_default_folders(); + + if ($CONFIG['create_default_folders']) + $IMAP->create_default_folders(); return TRUE; } return FALSE; } - - -/** - * Create new entry in users and identities table - * - * @param string User name - * @param string IMAP host - * @return mixed New user ID or False on failure - */ -function rcmail_create_user($user, $host) -{ - global $DB, $CONFIG, $IMAP; - - $user_email = ''; - - // try to resolve user in virtusertable - if (!empty($CONFIG['virtuser_file']) && !strpos($user, '@')) - $user_email = rcmail_user2email($user); - - $DB->query("INSERT INTO ".get_table_name('users')." - (created, last_login, username, mail_host, alias, language) - VALUES (".$DB->now().", ".$DB->now().", ?, ?, ?, ?)", - strip_newlines($user), - strip_newlines($host), - strip_newlines($user_email), - $_SESSION['user_lang']); - - if ($user_id = $DB->insert_id(get_sequence_name('users'))) - { - $mail_domain = rcmail_mail_domain($host); - - if ($user_email=='') - $user_email = strpos($user, '@') ? $user : sprintf('%s@%s', $user, $mail_domain); - - $user_name = $user!=$user_email ? $user : ''; - - // try to resolve the e-mail address from the virtuser table - if (!empty($CONFIG['virtuser_query']) && - ($sql_result = $DB->query(preg_replace('/%u/', $DB->quote($user), $CONFIG['virtuser_query']))) && - ($DB->num_rows()>0)) - { - while ($sql_arr = $DB->fetch_array($sql_result)) - { - $DB->query("INSERT INTO ".get_table_name('identities')." - (user_id, del, standard, name, email) - VALUES (?, 0, 1, ?, ?)", - $user_id, - strip_newlines($user_name), - preg_replace('/^@/', $user . '@', $sql_arr[0])); - } - } - else - { - // also create new identity records - $DB->query("INSERT INTO ".get_table_name('identities')." - (user_id, del, standard, name, email) - VALUES (?, 0, 1, ?, ?)", - $user_id, - strip_newlines($user_name), - strip_newlines($user_email)); - } - - // get existing mailboxes - $a_mailboxes = $IMAP->list_mailboxes(); - } - else - { - raise_error(array( - 'code' => 500, - 'type' => 'php', - 'line' => __LINE__, - 'file' => __FILE__, - 'message' => "Failed to create new user"), TRUE, FALSE); - } - - return $user_id; -} /** @@ -778,87 +706,6 @@ } return $result; - } - - -/** - * Resolve username using a virtuser table - * - * @param string E-mail address to resolve - * @return string Resolved IMAP username - */ -function rcmail_email2user($email) - { - $user = $email; - $r = rcmail_findinvirtual("^$email"); - - for ($i=0; $i<count($r); $i++) - { - $data = $r[$i]; - $arr = preg_split('/\s+/', $data); - if(count($arr)>0) - { - $user = trim($arr[count($arr)-1]); - break; - } - } - - return $user; - } - - -/** - * Resolve e-mail address from virtuser table - * - * @param string User name - * @return string Resolved e-mail address - */ -function rcmail_user2email($user) - { - $email = ""; - $r = rcmail_findinvirtual("$user$"); - - for ($i=0; $i<count($r); $i++) - { - $data=$r[$i]; - $arr = preg_split('/\s+/', $data); - if (count($arr)>0) - { - $email = trim($arr[0]); - break; - } - } - - return $email; - } - - -/** - * Write the given user prefs to the user's record - * - * @param mixed User prefs to save - * @return boolean True on success, False on failure - */ -function rcmail_save_user_prefs($a_user_prefs) - { - global $DB, $CONFIG, $sess_user_lang; - - $DB->query("UPDATE ".get_table_name('users')." - SET preferences=?, - language=? - WHERE user_id=?", - serialize($a_user_prefs), - $sess_user_lang, - $_SESSION['user_id']); - - if ($DB->affected_rows()) - { - $_SESSION['user_prefs'] = $a_user_prefs; - $CONFIG = array_merge($CONFIG, $a_user_prefs); - return TRUE; - } - - return FALSE; } @@ -1064,16 +911,18 @@ // convert charset using iconv module if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') - return iconv($from, $to . "//IGNORE", $str); + { + $iconv_map = array('KS_C_5601-1987' => 'EUC-KR'); + return iconv(($iconv_map[$from] ? $iconv_map[$from] : $from), ($iconv_map[$to] ? $iconv_map[$to] : $to) . "//IGNORE", $str); + } // convert charset using mbstring module if ($MBSTRING) { - $to = $to=="UTF-7" ? "UTF7-IMAP" : $to; - $from = $from=="UTF-7" ? "UTF7-IMAP": $from; + $mb_map = array('UTF-7' => 'UTF7-IMAP', 'KS_C_5601-1987' => 'EUC-KR'); // return if convert succeeded - if (($out = mb_convert_encoding($str, $to, $from)) != '') + if (($out = mb_convert_encoding($str, ($mb_map[$to] ? $mb_map[$to] : $to), ($mb_map[$from] ? $mb_map[$from] : $from))) != '') return $out; } @@ -1262,6 +1111,14 @@ return $value; } +/** + * Remove all non-ascii and non-word chars + * except . and - + */ +function asciiwords($str) +{ + return preg_replace('/[^a-z0-9.-_]/i', '', $str); +} /** * Remove single and double quotes from given string @@ -1415,7 +1272,7 @@ // use value from post if (!empty($_POST[$fname])) - $value = $_POST[$fname]; + $value = get_input_value($fname, RCUBE_INPUT_POST); $out = $input->show($value); @@ -1447,6 +1304,70 @@ /** + * Replace all css definitions with #container [def] + * + * @param string CSS source code + * @param string Container ID to use as prefix + * @return string Modified CSS source + */ +function rcmail_mod_css_styles($source, $container_id, $base_url = '') + { + $a_css_values = array(); + $last_pos = 0; + + // cut out all contents between { and } + while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos))) + { + $key = sizeof($a_css_values); + $a_css_values[$key] = substr($source, $pos+1, $pos2-($pos+1)); + $source = substr($source, 0, $pos+1) . "<<str_replacement[$key]>>" . substr($source, $pos2, strlen($source)-$pos2); + $last_pos = $pos+2; + } + + // remove html commends and add #container to each tag selector. + // also replace body definition because we also stripped off the <body> tag + $styles = preg_replace( + array( + '/(^\s*<!--)|(-->\s*$)/', + '/(^\s*|,\s*|\}\s*)([a-z0-9\._#][a-z0-9\.\-_]*)/im', + '/@import\s+(url\()?[\'"]?([^\)\'"]+)[\'"]?(\))?/ime', + '/<<str_replacement\[([0-9]+)\]>>/e', + "/$container_id\s+body/i" + ), + array( + '', + "\\1#$container_id \\2", + "sprintf(\"@import url('./bin/modcss.php?u=%s&c=%s')\", urlencode(make_absolute_url('\\2','$base_url')), urlencode($container_id))", + "\$a_css_values[\\1]", + "$container_id div.rcmBody" + ), + $source); + + return $styles; + } + +/** + * Try to autodetect operating system and find the correct line endings + * + * @return string The appropriate mail header delimiter + */ +function rcmail_header_delm() +{ + global $CONFIG; + + // use the configured delimiter for headers + if (!empty($CONFIG['mail_header_delimiter'])) + return $CONFIG['mail_header_delimiter']; + else if (strtolower(substr(PHP_OS, 0, 3)=='win')) + return "\r\n"; + else if (strtolower(substr(PHP_OS, 0, 3)=='mac')) + return "\r\n"; + else + return "\n"; +} + + +/** * Compose a valid attribute string for HTML tags * * @param array Named tag attributes -- Gitblit v1.9.1