alecpl
2008-04-23 ae0c82b0e4883268d977c377b9d023c658887a5d
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']);
@@ -348,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
@@ -358,20 +378,20 @@
function get_table_name($table)
  {
  global $CONFIG;
  // return table name if configured
  $config_key = 'db_table_'.$table;
  if (strlen($CONFIG[$config_key]))
    return $CONFIG[$config_key];
  return $table;
  }
/**
 * Return correct name for a specific database sequence
 * (used for Postres only)
 * (used for Postgres only)
 *
 * @param string Secuence name
 * @return string Translated sequence name
@@ -379,13 +399,13 @@
function get_sequence_name($sequence)
  {
  global $CONFIG;
  // return table name if configured
  $config_key = 'db_sequence_'.$sequence;
  if (strlen($CONFIG[$config_key]))
    return $CONFIG[$config_key];
  return $sequence;
  }
@@ -399,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]))
@@ -423,7 +442,7 @@
  if (isset($rcube_charsets[$lang]))
    $charset = $rcube_charsets[$lang];
  else
    $charset = 'UTF-8';
    $charset = 'UTF-8';
  if ($prop=='charset')
@@ -439,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);
@@ -462,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.
@@ -850,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])
@@ -876,6 +896,7 @@
/**
 * Add a localized label to the client environment
 * @deprecated
 */
function rcube_add_label()
  {
@@ -883,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);
  }
@@ -1107,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
@@ -1233,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
@@ -1332,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]))
@@ -1377,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
@@ -1386,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)))
@@ -1396,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(
@@ -1467,12 +1483,14 @@
function parse_attrib_string($str)
  {
  $attrib = array();
  preg_match_all('/\s*([-_a-z]+)=(["\'])([^"]+)\2/Ui', stripslashes($str), $regs, PREG_SET_ORDER);
  preg_match_all('/\s*([-_a-z]+)=(["\'])??(?(2)([^\2]+)\2|(\S+?))/Ui', stripslashes($str), $regs, PREG_SET_ORDER);
  // convert attributes to an associative array (name => value)
  if ($regs)
    foreach ($regs as $attr)
      $attrib[strtolower($attr[1])] = $attr[3];
      {
      $attrib[strtolower($attr[1])] = $attr[3] . $attr[4];
      }
  return $attrib;
  }
@@ -1491,12 +1509,22 @@
  global $CONFIG, $sess_user_lang;
  
  $ts = NULL;
  if (is_numeric($date))
    $ts = $date;
  else if (!empty($date))
    $ts = @strtotime($date);
    {
    while (($ts = @strtotime($date))===false)
      {
        // if we have a date in non-rfc format
   // remove token from the end and try again
        $d = explode(' ', $date);
   array_pop($d);
   if (!$d) break;
   $date = implode(' ', $d);
      }
    }
  if (empty($ts))
    return '';
   
@@ -1548,7 +1576,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);
    }
@@ -1607,12 +1635,12 @@
 * Append a line to a logfile in the logs directory.
 * Date will be added automatically to the line.
 *
 * @param $name Name of logfile
 * @param $line Line to append
 * @param $name name of log file
 * @param line Line to append
 */
function write_log($name, $line)
  {
  global $CONFIG, $INSTALL_PATH;
  global $CONFIG;
  if (!is_string($line))
    $line = var_export($line, true);
@@ -1622,7 +1650,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'))    
@@ -1794,10 +1822,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']);