thomascube
2008-07-31 62e54249df84f7d8a1d9af2f6d5a978bbba78bb5
program/include/main.inc
@@ -76,14 +76,15 @@
  if (!empty($opt))
    {
    $db = &rcmail::get_instance()->db;
    if($db->db_provider=='pgsql') // just for sure
    $dbclass = 'rcube_mdb2';
    if ($db->db_provider=='pgsql' && ($db instanceof $dbclass))
      {
      $db->db_handle->setOption('disable_smart_seqname', true);
      $db->db_handle->setOption('seqname_format', '%s');
      }
      }
  
    return $CONFIG[$opt];
    return $opt;
    }
    
  return $sequence;
@@ -102,51 +103,6 @@
{
  return rcmail::get_instance()->gettext($p);
}
/**
 * Load virtuser table in array
 *
 * @return array Virtuser table entries
 */
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
 *
 * @param string Regular expression to search for
 * @return array Matching entries
 */
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;
  }
/**
@@ -173,18 +129,7 @@
function rcmail_url($action, $p=array(), $task=null)
{
  $app = rcmail::get_instance();
  $qstring = '';
  $base = $app->comm_path;
  if ($task && in_array($task, rcmail::$main_tasks))
    $base = ereg_replace('_task=[a-z]+', '_task='.$task, $app->comm_path);
  if (is_array($p))
    foreach ($p as $key => $val)
      $qstring .= '&'.urlencode($key).'='.urlencode($val);
  return $base . ($action ? '&_action='.$action : '') . $qstring;
  return $app->url((array)$p + array('_action' => $action, 'task' => $task));
}
@@ -518,9 +463,10 @@
 * Remove all non-ascii and non-word chars
 * except . and -
 */
function asciiwords($str)
function asciiwords($str, $css_id = false)
{
  return preg_replace('/[^a-z0-9.-_]/i', '', $str);
  $allowed = 'a-z0-9\_\-' . (!$css_id ? '\.' : '');
  return preg_replace("/[^$allowed]/i", '', $str);
}
/**
@@ -548,22 +494,6 @@
/**
 * Check if a specific template exists
 *
 * @param string Template name
 * @return boolean True if template exists
 */
function template_exists($name)
  {
  global $CONFIG;
  $skin_path = $CONFIG['skin_path'];
  // check template file
  return is_file("$skin_path/templates/$name.html");
  }
/**
 * Create a HTML table based on the given data
 *
 * @param  array  Named table attributes
@@ -574,65 +504,46 @@
 */
function rcube_table_output($attrib, $table_data, $a_show_cols, $id_col)
  {
  global $DB;
  global $RCMAIL;
  
  // allow the following attributes to be added to the <table> tag
  $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
  $table = '<table' . $attrib_str . ">\n";
  $table = new html_table(/*array('cols' => count($a_show_cols))*/);
    
  // add table title
  $table .= "<thead><tr>\n";
  // add table header
  foreach ($a_show_cols as $col)
    $table .= '<td class="'.$col.'">' . Q(rcube_label($col)) . "</td>\n";
  $table .= "</tr></thead>\n<tbody>\n";
    $table->add_header($col, Q(rcube_label($col)));
  
  $c = 0;
  if (!is_array($table_data)) 
  {
    $db = $RCMAIL->get_dbh();
    while ($table_data && ($sql_arr = $db->fetch_assoc($table_data)))
    {
    while ($table_data && ($sql_arr = $DB->fetch_assoc($table_data)))
      {
      $zebra_class = $c%2 ? 'even' : 'odd';
      $table .= sprintf('<tr id="rcmrow%d" class="contact '.$zebra_class.'">'."\n", $sql_arr[$id_col]);
      $zebra_class = $c % 2 ? 'even' : 'odd';
      $table->add_row(array('id' => 'rcmrow' . $sql_arr[$id_col], 'class' => "contact $zebra_class"));
      // format each col
      foreach ($a_show_cols as $col)
        {
        $cont = Q($sql_arr[$col]);
        $table .= '<td class="'.$col.'">' . $cont . "</td>\n";
        }
      $table .= "</tr>\n";
        $table->add($col, Q($sql_arr[$col]));
      $c++;
      }
    }
  }
  else 
    {
  {
    foreach ($table_data as $row_data)
      {
      $zebra_class = $c%2 ? 'even' : 'odd';
      $table .= sprintf('<tr id="rcmrow%s" class="contact '.$zebra_class.'">'."\n", $row_data[$id_col]);
    {
      $zebra_class = $c % 2 ? 'even' : 'odd';
      $table->add_row(array('id' => 'rcmrow' . $row_data[$id_col], 'class' => "contact $zebra_class"));
      // format each col
      foreach ($a_show_cols as $col)
        {
        $cont = Q($row_data[$col]);
        $table .= '<td class="'.$col.'">' . $cont . "</td>\n";
        }
      $table .= "</tr>\n";
        $table->add($col, Q($row_data[$col]));
      $c++;
      }
    }
  }
  // complete message table
  $table .= "</tbody></table>\n";
  return $table;
  return $table->show($attrib);
  }
@@ -670,29 +581,6 @@
  $out = $input->show($value);
         
  return $out;
  }
/**
 * Return the mail domain configured for the given host
 *
 * @param string IMAP host
 * @return string Resolved SMTP host
 */
function rcmail_mail_domain($host)
  {
  global $CONFIG;
  $domain = $host;
  if (is_array($CONFIG['mail_domain']))
    {
    if (isset($CONFIG['mail_domain'][$host]))
      $domain = $CONFIG['mail_domain'][$host];
    }
  else if (!empty($CONFIG['mail_domain']))
    $domain = $CONFIG['mail_domain'];
  return $domain;
  }
@@ -743,26 +631,6 @@
  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";
}
/**
@@ -887,6 +755,8 @@
    // month name (long)
    else if ($format{$i}=='F')
      $out .= rcube_label('long'.strtolower(date('M', $timestamp)));
    else if ($format{$i}=='x')
      $out .= strftime('%x %X', $timestamp);
    else
      $out .= date($format{$i}, $timestamp);
    }
@@ -1006,68 +876,80 @@
 * @return string HTML code for the gui object
 */
function rcmail_mailbox_list($attrib)
  {
  global $IMAP, $CONFIG, $OUTPUT, $COMM_PATH;
  static $s_added_script = FALSE;
{
  global $IMAP, $OUTPUT;
  static $a_mailboxes;
  // add some labels to client
  rcube_add_label('purgefolderconfirm');
  rcube_add_label('deletemessagesconfirm');
  
// $mboxlist_start = rcube_timer();
  $type = $attrib['type'] ? $attrib['type'] : 'ul';
  $add_attrib = $type=='select' ? array('style', 'class', 'id', 'name', 'onchange') :
                                  array('style', 'class', 'id');
  unset($attrib['type']);
  if ($type=='ul' && !$attrib['id'])
    $attrib['id'] = 'rcmboxlist';
  // allow the following attributes to be added to the <ul> tag
  $attrib_str = create_attrib_string($attrib, $add_attrib);
  $out = '<' . $type . $attrib_str . ">\n";
  // add no-selection option
  if ($type=='select' && $attrib['noselection'])
    $out .= sprintf('<option value="0">%s</option>'."\n",
                    rcube_label($attrib['noselection']));
  // get mailbox list
  $mbox_name = $IMAP->get_mailbox_name();
  
  // build the folders tree
  if (empty($a_mailboxes))
    {
  if (empty($a_mailboxes)) {
    // get mailbox list
    $a_folders = $IMAP->list_mailboxes();
    $delimiter = $IMAP->get_hierarchy_delimiter();
    $a_mailboxes = array();
// rcube_print_time($mboxlist_start, 'list_mailboxes()');
    foreach ($a_folders as $folder)
      rcmail_build_folder_tree($a_mailboxes, $folder, $delimiter);
    }
  }
// var_dump($a_mailboxes);
  if ($type=='select')
    $out .= rcmail_render_folder_tree_select($a_mailboxes, $mbox_name, $attrib['maxlength']);
   else
    $out .= rcmail_render_folder_tree_html($a_mailboxes, $mbox_name, $attrib['maxlength']);
// rcube_print_time($mboxlist_start, 'render_folder_tree()');
  if ($type=='select') {
    $select = new html_select($attrib);
    // add no-selection option
    if ($attrib['noselection'])
      $select->add(rcube_label($attrib['noselection']), '0');
    rcmail_render_folder_tree_select($a_mailboxes, $mbox_name, $attrib['maxlength'], $select);
    $out = $select->show();
  }
  else {
    $out = html::tag('ul', $attrib, rcmail_render_folder_tree_html($a_mailboxes, $mbox_name, $attrib['maxlength']), html::$common_attrib);
  }
  if ($type=='ul')
    $OUTPUT->add_gui_object('mailboxlist', $attrib['id']);
  return $out . "</$type>";
  }
  return $out;
}
/**
 * Return the mailboxlist as html_select object
 *
 * @param array Named parameters
 * @return object html_select HTML drop-down object
 */
function rcmail_mailbox_select($p = array())
{
  global $RCMAIL;
  $p += array('maxlength' => 100);
  $a_mailboxes = array();
  foreach ($RCMAIL->imap->list_mailboxes() as $folder)
    rcmail_build_folder_tree($a_mailboxes, $folder, $RCMAIL->imap->get_hierarchy_delimiter());
  $select = new html_select($p);
  if ($p['noselection'])
    $select->add($p['noselection'], '');
  rcmail_render_folder_tree_select($a_mailboxes, $mbox, $p['maxlength'], $select);
  return $select;
}
/**
@@ -1092,9 +974,10 @@
  if (!isset($arrFolders[$currentFolder]))
    {
    $arrFolders[$currentFolder] = array('id' => $path,
                                        'name' => rcube_charset_convert($currentFolder, 'UTF-7'),
                                        'folders' => array());
    $arrFolders[$currentFolder] = array(
      'id' => $path,
      'name' => rcube_charset_convert($currentFolder, 'UTF-7'),
      'folders' => array());
    }
  if (!empty($subFolders))
@@ -1114,8 +997,8 @@
  $out = '';
  foreach ($arrFolders as $key => $folder)
    {
    $zebra_class = ($nestLevel*$idx)%2 ? 'even' : 'odd';
    $title = '';
    $zebra_class = (($nestLevel+1)*$idx) % 2 == 0 ? 'even' : 'odd';
    $title = null;
    if ($folder_class = rcmail_folder_classname($folder['id']))
      $foldername = rcube_label($folder_class);
@@ -1128,47 +1011,46 @@
        {
        $fname = abbreviate_string($foldername, $maxlength);
        if ($fname != $foldername)
          $title = ' title="'.Q($foldername).'"';
          $title = $foldername;
        $foldername = $fname;
        }
      }
    // make folder name safe for ids and class names
    $folder_id = preg_replace('/[^A-Za-z0-9\-_]/', '', $folder['id']);
    $class_name = preg_replace('/[^a-z0-9\-_]/', '', $folder_class ? $folder_class : strtolower($folder['id']));
    $folder_id = asciiwords($folder['id'], true);
    $classes = array('mailbox');
    // set special class for Sent, Drafts, Trash and Junk
    if ($folder['id']==$CONFIG['sent_mbox'])
      $class_name = 'sent';
      $classes[] = 'sent';
    else if ($folder['id']==$CONFIG['drafts_mbox'])
      $class_name = 'drafts';
      $classes[] = 'drafts';
    else if ($folder['id']==$CONFIG['trash_mbox'])
      $class_name = 'trash';
      $classes[] = 'trash';
    else if ($folder['id']==$CONFIG['junk_mbox'])
      $class_name = 'junk';
      $classes[] = 'junk';
    else
      $classes[] = asciiwords($folder_class ? $folder_class : strtolower($folder['id']), true);
    $classes[] = $zebra_class;
    if ($folder['id'] == $mbox_name)
      $classes[] = 'selected';
    $js_name = htmlspecialchars(JQ($folder['id']));
    $out .= sprintf('<li id="rcmli%s" class="mailbox %s %s%s"><a href="%s"'.
                    ' onclick="return %s.command(\'list\',\'%s\',this)"'.
                    ' onmouseover="return %s.focus_folder(\'%s\')"' .
                    ' onmouseout="return %s.unfocus_folder(\'%s\')"' .
                    ' onmouseup="return %s.folder_mouse_up(\'%s\')"%s>%s</a>',
                    $folder_id,
                    $class_name,
                    $zebra_class,
                    $folder['id']==$mbox_name ? ' selected' : '',
                    Q(rcmail_url('', array('_mbox' => $folder['id']))),
                    JS_OBJECT_NAME,
                    $js_name,
                    JS_OBJECT_NAME,
                    $js_name,
                    JS_OBJECT_NAME,
                    $js_name,
                    JS_OBJECT_NAME,
                    $js_name,
                    $title,
                    Q($foldername));
    $js_name = JQ($folder['id']);
    $out .= html::tag('li', array(
        'id' => "rcmli".$folder_id,
        'class' => join(' ', $classes),
        'noclose' => true),
      html::a(array(
        'href' => rcmail_url('', array('_mbox' => $folder['id'])),
        'onclick' => sprintf("return %s.command('list','%s',this)", JS_OBJECT_NAME, $js_name),
        'onmouseover' => sprintf("return %s.focus_folder('%s')", JS_OBJECT_NAME, $js_name),
        'onmouseout' => sprintf("return %s.unfocus_folder('%s')", JS_OBJECT_NAME, $js_name),
        'onmouseup' => sprintf("return %s.folder_mouse_up('%s')", JS_OBJECT_NAME, $js_name),
        'title' => $title,
      ), Q($foldername)));
    if (!empty($folder['folders']))
      $out .= "\n<ul>\n" . rcmail_render_folder_tree_html($folder['folders'], $mbox_name, $maxlength, $nestLevel+1) . "</ul>\n";
@@ -1184,7 +1066,7 @@
 * Return html for a flat list <select> for the mailbox tree
 * @access private
 */
function rcmail_render_folder_tree_select(&$arrFolders, &$mbox_name, $maxlength, $nestLevel=0, $selected='')
function rcmail_render_folder_tree_select(&$arrFolders, &$mbox_name, $maxlength, &$select, $nestLevel=0)
  {
  global $IMAP, $OUTPUT;
@@ -1203,14 +1085,10 @@
        $foldername = abbreviate_string($foldername, $maxlength);
      }
    $out .= sprintf('<option value="%s"%s>%s%s</option>'."\n",
                    htmlspecialchars($folder['id']),
          ($selected == $foldername ? ' selected="selected"' : ''),
                    str_repeat('&nbsp;', $nestLevel*4),
                    Q($foldername));
    $select->add(str_repeat('&nbsp;', $nestLevel*4) . $foldername, $folder['id']);
    if (!empty($folder['folders']))
      $out .= rcmail_render_folder_tree_select($folder['folders'], $mbox_name, $maxlength, $nestLevel+1, $selected);
      $out .= rcmail_render_folder_tree_select($folder['folders'], $mbox_name, $maxlength, $select, $nestLevel+1);
    $idx++;
    }