alecpl
2008-04-13 875a482d3c47218b8d191bb07fdb16373a80b1d4
program/include/main.inc
@@ -27,9 +27,7 @@
 */
require_once('lib/utf7.inc');
require_once('include/rcube_user.inc');
require_once('include/rcube_shared.inc');
require_once('include/rcmail_template.inc');
// fallback if not PHP modules are available
@include_once('lib/des.inc');
@@ -50,10 +48,11 @@
function rcmail_startup($task='mail')
  {
  global $sess_id, $sess_user_lang;
  global $CONFIG, $INSTALL_PATH, $BROWSER, $OUTPUT, $_SESSION, $IMAP, $DB, $USER;
  global $CONFIG, $OUTPUT, $IMAP, $DB, $USER;
  // check client
  $BROWSER = rcube_browser();
  // start output buffering, we don't need any output yet,
  // it'll be cleared after reading of config files, etc.
  ob_start();
  // load configuration
  $CONFIG = rcmail_load_config();
@@ -70,15 +69,17 @@
  // prepare DB connection
  $dbwrapper = empty($CONFIG['db_backend']) ? 'db' : $CONFIG['db_backend'];
  $dbclass = "rcube_" . $dbwrapper;
  require_once("include/$dbclass.inc");
  
  $DB = new $dbclass($CONFIG['db_dsnw'], $CONFIG['db_dsnr'], $CONFIG['db_persistent']);
  $DB->sqlite_initials = $INSTALL_PATH.'SQL/sqlite.initial.sql';
  $DB->sqlite_initials = INSTALL_PATH.'SQL/sqlite.initial.sql';
  $DB->set_debug((bool)$CONFIG['sql_debug']);
  $DB->db_connect('w');
  // use database for storing session data
  include_once('include/session.inc');
  // clear output buffer
  ob_end_clean();
  // init session
  session_start();
@@ -101,7 +102,6 @@
  // overwrite config with user preferences
  $CONFIG = array_merge($CONFIG, (array)$USER->get_prefs());
  // reset some session parameters when changing task
  if ($_SESSION['task'] != $task)
    unset($_SESSION['page']);
@@ -113,13 +113,11 @@
  if ($task=='mail')
    rcmail_imap_init();
  // set localization
  if ($CONFIG['locale_string'])
    setlocale(LC_ALL, $CONFIG['locale_string']);
  else if ($sess_user_lang)
    setlocale(LC_ALL, $sess_user_lang);
  register_shutdown_function('rcmail_shutdown');
  }
@@ -132,8 +130,6 @@
 */
function rcmail_load_config()
  {
  global $INSTALL_PATH;
  // load config file
  include_once('config/main.inc.php');
  $conf = is_array($rcmail_config) ? $rcmail_config : array();
@@ -148,7 +144,7 @@
  $conf = array_merge($conf, $rcmail_config);
  if (empty($conf['log_dir']))
    $conf['log_dir'] = $INSTALL_PATH.'logs';
    $conf['log_dir'] = INSTALL_PATH.'logs';
  else
    $conf['log_dir'] = unslashify($conf['log_dir']);
@@ -290,6 +286,9 @@
function rcmail_set_imap_prop()
  {
  global $CONFIG, $IMAP;
  if (!empty($CONFIG['default_charset']))
    $IMAP->set_charset($CONFIG['default_charset']);
  // set root dir from config
  if (!empty($CONFIG['imap_root']))
@@ -345,6 +344,30 @@
  $USER->reset();
  }
/**
 * Do server side actions on logout
 */
function rcmail_logout_actions()
  {
  global $CONFIG, $IMAP;
  // on logout action we're not connected to imap server
  if (($CONFIG['logout_purge'] && !empty($CONFIG['trash_mbox']))
      || $CONFIG['logout_expunge'])
    {
      if (!rcmail_authenticate_session())
        return;
      rcmail_imap_init(true);
    }
  if ($CONFIG['logout_purge'] && !empty($CONFIG['trash_mbox']))
    $IMAP->clear_mailbox($CONFIG['trash_mbox']);
  if ($CONFIG['logout_expunge'])
    $IMAP->expunge('INBOX');
  }
/**
 * Return correct name for a specific database table
@@ -396,11 +419,10 @@
 */
function rcube_language_prop($lang, $prop='lang')
  {
  global $INSTALL_PATH;
  static $rcube_languages, $rcube_language_aliases, $rcube_charsets;
  if (empty($rcube_languages))
    @include($INSTALL_PATH.'program/localization/index.inc');
    @include(INSTALL_PATH.'program/localization/index.inc');
    
  // check if we have an alias for that language
  if (!isset($rcube_languages[$lang]) && isset($rcube_language_aliases[$lang]))
@@ -420,7 +442,7 @@
  if (isset($rcube_charsets[$lang]))
    $charset = $rcube_charsets[$lang];
  else
    $charset = 'UTF-8';
    $charset = 'UTF-8';
  if ($prop=='charset')
@@ -436,18 +458,15 @@
 * environment vars according to the current session and configuration
 */
function rcmail_load_gui()
  {
{
  global $CONFIG, $OUTPUT, $sess_user_lang;
  // init output page
  $OUTPUT = new rcmail_template($CONFIG, $GLOBALS['_task']);
  $OUTPUT = new rcube_template($CONFIG, $GLOBALS['_task']);
  $OUTPUT->set_env('comm_path', $GLOBALS['COMM_PATH']);
  if (is_array($CONFIG['javascript_config']))
  {
    foreach ($CONFIG['javascript_config'] as $js_config_var)
      $OUTPUT->set_env($js_config_var, $CONFIG[$js_config_var]);
  }
  foreach (array('flag_for_deletion') as $js_config_var)
    $OUTPUT->set_env($js_config_var, $CONFIG[$js_config_var]);
  if (!empty($GLOBALS['_framed']))
    $OUTPUT->set_env('framed', true);
@@ -459,19 +478,23 @@
  if (!empty($CONFIG['charset']))
    $OUTPUT->set_charset($CONFIG['charset']);
    
  // register common UI objects
  $OUTPUT->add_handlers(array(
    'loginform' => 'rcmail_login_form',
    'username'  => 'rcmail_current_username',
    'message' => 'rcmail_message_container',
    'charsetselector' => 'rcmail_charset_selector',
  ));
  // add some basic label to client
  if (!$OUTPUT->ajax_call)
    rcube_add_label('loading', 'movingmessage');
  }
  $OUTPUT->add_label('loading', 'movingmessage');
}
/**
 * Create an output object for JSON responses
 */
function rcmail_init_json()
{
  global $CONFIG, $OUTPUT;
  // init output object
  $OUTPUT = new rcube_json_output($CONFIG, $GLOBALS['_task']);
  // set locale setting
  rcmail_set_locale($sess_user_lang);
}
/**
 * Set localization charset based on the given language.
@@ -847,18 +870,18 @@
 */
function rcube_list_languages()
  {
  global $CONFIG, $INSTALL_PATH;
  global $CONFIG;
  static $sa_languages = array();
  if (!sizeof($sa_languages))
    {
    @include($INSTALL_PATH.'program/localization/index.inc');
    @include(INSTALL_PATH.'program/localization/index.inc');
    if ($dh = @opendir($INSTALL_PATH.'program/localization'))
    if ($dh = @opendir(INSTALL_PATH.'program/localization'))
      {
      while (($name = readdir($dh)) !== false)
        {
        if ($name{0}=='.' || !is_dir($INSTALL_PATH.'program/localization/'.$name))
        if ($name{0}=='.' || !is_dir(INSTALL_PATH.'program/localization/'.$name))
          continue;
        if ($label = $rcube_languages[$name])
@@ -873,6 +896,7 @@
/**
 * Add a localized label to the client environment
 * @deprecated
 */
function rcube_add_label()
  {
@@ -880,7 +904,7 @@
  
  $arg_list = func_get_args();
  foreach ($arg_list as $i => $name)
    $OUTPUT->command('add_label', $name, rcube_label($name));
    $OUTPUT->add_label($name);
  }
@@ -956,22 +980,23 @@
    'X-USER-DEFINED' => 'ISO-8859-15',
    'ISO-8859-8-I'   => 'ISO-8859-8',
    'KS_C_5601-1987' => 'EUC-KR',
    'GB2312'         => 'GB18030'
  );
  // convert charset using iconv module  
  if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7')
    {
    $aliases['GB2312'] = 'GB18030';
    return iconv(($aliases[$from] ? $aliases[$from] : $from), ($aliases[$to] ? $aliases[$to] : $to) . "//IGNORE", $str);
    }
  // convert charset using mbstring module  
  if ($MBSTRING)
    {
    $mb_map = $aliases + array('UTF-7' => 'UTF7-IMAP');
    $aliases['UTF-7'] = 'UTF7-IMAP';
    $aliases['WINDOWS-1257'] = 'ISO-8859-13';
    
    // return if convert succeeded
    if (($out = mb_convert_encoding($str, ($mb_map[$to] ? $mb_map[$to] : $to), ($mb_map[$from] ? $mb_map[$from] : $from))) != '')
    if (($out = mb_convert_encoding($str, ($aliases[$to] ? $aliases[$to] : $to), ($aliases[$from] ? $aliases[$from] : $from))) != '')
      return $out;
    }
    
@@ -1103,7 +1128,7 @@
    if ($OUTPUT->get_charset()!='UTF-8')
      $str = rcube_charset_convert($str, RCMAIL_CHARSET, $OUTPUT->get_charset());
      
    return preg_replace(array("/\r?\n/", "/\r/"), array('\n', '\n'), addslashes(strtr($str, $js_rep_table)));
    return preg_replace(array("/\r?\n/", "/\r/", '/<\\//'), array('\n', '\n', '<\\/'), addslashes(strtr($str, $js_rep_table)));
    }
  // no encoding given -> return original string
@@ -1229,16 +1254,6 @@
/**
 * Wrapper for rcmail_template::parse()
 * @deprecated
 */
function parse_template($name='main', $exit=true)
  {
  $GLOBALS['OUTPUT']->parse($name, $exit);
  }
/**
 * Create a HTML table based on the given data
 *
 * @param  array  Named table attributes
@@ -1328,15 +1343,15 @@
  if ($type=='checkbox')
    {
    $attrib['value'] = '1';
    $input = new checkbox($attrib);
    $input = new html_checkbox($attrib);
    }
  else if ($type=='textarea')
    {
    $attrib['cols'] = $attrib['size'];
    $input = new textarea($attrib);
    $input = new html_textarea($attrib);
    }
  else
    $input = new textfield($attrib);
    $input = new html_inputfield($attrib);
  // use value from post
  if (!empty($_POST[$fname]))
@@ -1373,6 +1388,7 @@
/**
 * Replace all css definitions with #container [def]
 * and remove css-inlined scripting
 *
 * @param string CSS source code
 * @param string Container ID to use as prefix
@@ -1382,6 +1398,10 @@
  {
  $a_css_values = array();
  $last_pos = 0;
  // ignore the whole block if evil styles are detected
  if (stristr($source, 'expression') || stristr($source, 'behavior'))
    return '';
  // cut out all contents between { and }
  while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos)))
@@ -1392,7 +1412,7 @@
    $last_pos = $pos+2;
  }
  // remove html commends and add #container to each tag selector.
  // remove html comments and add #container to each tag selector.
  // also replace body definition because we also stripped off the <body> tag
  $styles = preg_replace(
    array(
@@ -1544,7 +1564,7 @@
      $out .= rcube_label(strtolower(date('M', $timestamp)));
    // month name (long)
    else if ($format{$i}=='F')
      $out .= rcube_label(strtolower(date('F', $timestamp)));
      $out .= rcube_label('long'.strtolower(date('M', $timestamp)));
    else
      $out .= date($format{$i}, $timestamp);
    }
@@ -1563,7 +1583,10 @@
function format_email_recipient($email, $name='')
  {
  if ($name && $name != $email)
    return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email);
    {
    // Special chars as defined by RFC 822 need to in quoted string (or escaped).
    return sprintf('%s <%s>', preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $name) ? '"'.addcslashes($name, '"').'"' : $name, $email);
    }
  else
    return $email;
  }
@@ -1605,7 +1628,7 @@
 */
function write_log($name, $line)
  {
  global $CONFIG, $INSTALL_PATH;
  global $CONFIG;
  if (!is_string($line))
    $line = var_export($line, true);
@@ -1615,7 +1638,7 @@
                 $line);
                 
  if (empty($CONFIG['log_dir']))
    $CONFIG['log_dir'] = $INSTALL_PATH.'logs';
    $CONFIG['log_dir'] = INSTALL_PATH.'logs';
      
  // try to open specific log file for writing
  if ($fp = @fopen($CONFIG['log_dir'].'/'.$name, 'a'))    
@@ -1787,10 +1810,6 @@
        $foldername = $fname;
        }
      }
    // add unread message count display
    if ($unread_count = $IMAP->messagecount($folder['id'], 'RECENT', ($folder['id']==$mbox_name)))
      $foldername .= sprintf(' (%d)', $unread_count);
    // make folder name safe for ids and class names
    $folder_id = preg_replace('/[^A-Za-z0-9\-_]/', '', $folder['id']);