-// create new entry in users and identities table
-function rcmail_create_user($user, $host)
- {
- global $DB, $CONFIG, $IMAP;
-
- $user_email = '';
-
- // try to resolve user in virtusertable
- if (!empty($CONFIG['virtuser_file']) && strstr($user, '@')==FALSE)
- $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 = strstr($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/', $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;
- }
-
-
-// load virtuser table in array
-function rcmail_getvirtualfile()
- {
- global $CONFIG;
- if (empty($CONFIG['virtuser_file']) || !is_file($CONFIG['virtuser_file']))
- return FALSE;
-
- // read file
- $a_lines = file($CONFIG['virtuser_file']);
- return $a_lines;
- }
-
-
-// find matches of the given pattern in virtuser table
-function rcmail_findinvirtual($pattern)
- {
- $result = array();
- $virtual = rcmail_getvirtualfile();
- if ($virtual==FALSE)
- return $result;
-
- // check each line for matches
- foreach ($virtual as $line)
- {
- $line = trim($line);
- if (empty($line) || $line{0}=='#')
- continue;
-
- if (eregi($pattern, $line))
- $result[] = $line;
- }
-
- return $result;
- }
-
-
-// resolve username with virtuser table
-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 with virtuser table
-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;
- }
-
-
-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;
- }
-
-
-// overwrite action variable
-function rcmail_overwrite_action($action)
- {
- global $OUTPUT;
- $GLOBALS['_action'] = $action;
- $OUTPUT->set_env('action', $action);
- }
-
-
-/**
- * Compose an URL for a specific action
- *
- * @param string Request action
- * @param array More URL parameters
- * @param string Request task (omit if the same)
- * @return The application URL
- */
-function rcmail_url($action, $p=array(), $task=null)
-{
- global $MAIN_TASKS, $COMM_PATH;
- $qstring = '';
- $base = $COMM_PATH;
-
- if ($task && in_array($task, $MAIN_TASKS))
- $base = ereg_replace('_task=[a-z]+', '_task='.$task, $COMM_PATH);
-
- if (is_array($p))
- foreach ($p as $key => $val)
- $qstring .= '&'.urlencode($key).'='.urlencode($val);
-
- return $base . ($action ? '&_action='.$action : '') . $qstring;
-}
-
-
-// @deprecated
-function show_message($message, $type='notice', $vars=NULL)
- {
- global $OUTPUT;
- $OUTPUT->show_message($message, $type, $vars);
- }
-
-
-// encrypt IMAP password using DES encryption
-function encrypt_passwd($pass)
- {
- $td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, "");
- $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
- mcrypt_generic_init($td, get_des_key(), $iv);
- $cypher = mcrypt_generic($td, $pass);
- mcrypt_generic_deinit($td);
- mcrypt_module_close($td);
- return base64_encode($cypher);
- }
-
-
-// decrypt IMAP password using DES encryption
-function decrypt_passwd($cypher)
- {
- $td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, "");
- $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
- mcrypt_generic_init($td, get_des_key(), $iv);
- $pass = mdecrypt_generic($td, base64_decode($cypher));
- mcrypt_generic_deinit($td);
- mcrypt_module_close($td);
- return preg_replace('/\x00/', '', $pass);
- }
-
-
-// return a 24 byte key for the DES encryption
-function get_des_key()
- {
- $key = !empty($GLOBALS['CONFIG']['des_key']) ? $GLOBALS['CONFIG']['des_key'] : 'rcmail?24BitPwDkeyF**ECB';
- $len = strlen($key);
-
- // make sure the key is exactly 24 chars long
- if ($len<24)
- $key .= str_repeat('_', 24-$len);
- else if ($len>24)
- substr($key, 0, 24);
-
- return $key;
- }
-
-
-// read directory program/localization/ and return a list of available languages
-function rcube_list_languages()
- {
- global $CONFIG, $INSTALL_PATH;
- static $sa_languages = array();
-
- if (!sizeof($sa_languages))
- {
- @include($INSTALL_PATH.'program/localization/index.inc');
-
- if ($dh = @opendir($INSTALL_PATH.'program/localization'))
- {
- while (($name = readdir($dh)) !== false)
- {
- if ($name{0}=='.' || !is_dir($INSTALL_PATH.'program/localization/'.$name))
- continue;
-
- if ($label = $rcube_languages[$name])
- $sa_languages[$name] = $label ? $label : $name;
- }
- closedir($dh);
- }
- }
- return $sa_languages;
- }
-
-
-// add a localized label to the client environment
-function rcube_add_label()
- {
- global $OUTPUT;
-
- $arg_list = func_get_args();
- foreach ($arg_list as $i => $name)
- $OUTPUT->command('add_label', $name, rcube_label($name));
- }
-
-
-// remove temp files older than two day
-function rcmail_temp_gc()
- {
- $tmp = unslashify($CONFIG['temp_dir']);
- $expire = mktime() - 172800; // expire in 48 hours
-
- if ($dir = opendir($tmp))
- {
- while (($fname = readdir($dir)) !== false)
- {
- if ($fname{0} == '.')
- continue;
-
- if (filemtime($tmp.'/'.$fname) < $expire)
- @unlink($tmp.'/'.$fname);
- }
-
- closedir($dir);
- }
- }
-
-
-// remove all expired message cache records
-function rcmail_message_cache_gc()
- {
- global $DB, $CONFIG;
-
- // no cache lifetime configured
- if (empty($CONFIG['message_cache_lifetime']))
- return;
-
- // get target timestamp
- $ts = get_offset_time($CONFIG['message_cache_lifetime'], -1);
-
- $DB->query("DELETE FROM ".get_table_name('messages')."
- WHERE created < ".$DB->fromunixtime($ts));
- }
-
-
-/**
- * Convert a string from one charset to another.
- * Uses mbstring and iconv functions if possible
- *
- * @param string Input string
- * @param string Suspected charset of the input string
- * @param string Target charset to convert to; defaults to RCMAIL_CHARSET
- * @return Converted string
- */
-function rcube_charset_convert($str, $from, $to=NULL)
- {
- global $MBSTRING;
-
- $from = strtoupper($from);
- $to = $to==NULL ? strtoupper(RCMAIL_CHARSET) : strtoupper($to);
-
- if ($from==$to || $str=='' || empty($from))
- return $str;
-
- // convert charset using mbstring module
- if ($MBSTRING)
- {
- $to = $to=="UTF-7" ? "UTF7-IMAP" : $to;
- $from = $from=="UTF-7" ? "UTF7-IMAP": $from;
-
- // return if convert succeeded
- if (($out = mb_convert_encoding($str, $to, $from)) != '')
- return $out;
- }
-
- // convert charset using iconv module
- if (function_exists('iconv') && $from!='UTF-7' && $to!='UTF-7')
- return iconv($from, $to, $str);
-
- // convert string to UTF-8
- if ($from=='UTF-7')
- $str = utf7_to_utf8($str);
- else if (($from=='ISO-8859-1') && function_exists('utf8_encode'))
- $str = utf8_encode($str);
-
- // encode string for output
- if ($to=='UTF-7')
- return utf8_to_utf7($str);
- else if ($to=='ISO-8859-1' && function_exists('utf8_decode'))
- return utf8_decode($str);
-
- // return UTF-8 string
- return $str;
- }