From 42b11351497ce67e96a0465c76694632cdfb3ecb Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Fri, 07 Oct 2005 10:17:08 -0400
Subject: [PATCH] Several bugfixes and feature improvements

---
 program/include/main.inc |  101 +++++++++++++++++++++++++++++++-------------------
 1 files changed, 62 insertions(+), 39 deletions(-)

diff --git a/program/include/main.inc b/program/include/main.inc
index 8cbc271..e2e7a00 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -6,7 +6,7 @@
  |                                                                       |
  | This file is part of the RoundCube Webmail client                     |
  | Copyright (C) 2005, RoundCube Dev, - Switzerland                      |
- | All rights reserved.                                                  |
+ | Licensed under the GNU GPL                                            |
  |                                                                       |
  | PURPOSE:                                                              |
  |   Provide basic functions for the webmail package                     |
@@ -54,23 +54,11 @@
 
 
   // prepare DB connection
-  if (strtolower($CONFIG['db_type'])=='mysql')
-    $DB = new rcube_mysql($CONFIG['db_name'], $CONFIG['db_user'], $CONFIG['db_pass'], $CONFIG['db_host']);
-    
-  // database not supported
-  else
-    {
-    raise_error(array('code' => 500,
-                      'type' => 'php',
-                      'line' => __LINE__,
-                      'file' => __FILE__,
-                      'message' => "Database not supported"), TRUE, TRUE);
-    return;
-    }  
-
+  $DB = new rcube_db($CONFIG['db_dsnw'], $CONFIG['db_dsnr']);
+  $DB->sqlite_initials = $INSTALL_PATH.'SQL/sqlite.initial.sql';
 
   // we can use the database for storing session data
-  if (is_object($DB) && $DB->connect())
+  if (is_object($DB) && $DB->db_provider!='sqlite')
     include_once('include/session.inc');
 
 
@@ -145,8 +133,12 @@
 function rcmail_imap_init($connect=FALSE)
   {
   global $CONFIG, $IMAP;
-  
+
   $IMAP = new rcube_imap();
+
+  // enable caching of imap data
+  if ($CONFIG['enable_caching']===TRUE)
+    $IMAP->set_caching(TRUE);
 
   // set root dir from config
   if (strlen($CONFIG['imap_root']))
@@ -228,7 +220,7 @@
   $javascript = "var $JS_OBJECT_NAME = new rcube_webmail();\n";
   $javascript .= "$JS_OBJECT_NAME.set_env('comm_path', '$COMM_PATH');\n";
 
-  if ($_GET['_framed'] || $_POST['_framed'])
+  if (!empty($GLOBALS['_framed']))
     $javascript .= "$JS_OBJECT_NAME.set_env('framed', true);\n";
 
   $OUTPUT->add_script($javascript);
@@ -241,26 +233,43 @@
 function rcmail_login($user, $pass, $host=NULL)
   {
   global $CONFIG, $IMAP, $DB, $sess_user_lang;
+  $user_id = NULL;
   
   if (!$host)
     $host = $CONFIG['default_host'];
 
-  // exit if IMAP login failed
-  if (!($imap_login  = $IMAP->connect($host, $user, $pass)))
-    return FALSE;
-
   // query if user already registered
-  $sql_result = $DB->query(sprintf("SELECT user_id, language, preferences
-                                    FROM %s
-                                    WHERE  username='%s' AND mail_host='%s'",
+  $sql_result = $DB->query(sprintf("SELECT user_id, username, language, preferences
+                                    FROM   %s
+                                    WHERE  mail_host='%s' AND (username='%s' OR alias='%s')",
                                    get_table_name('users'),
-                                   $user, $host));
+                                   addslashes($host),
+                                   addslashes($user),
+                                   addslashes($user)));
 
-  // user already registered
+  // user already registered -> overwrite username
   if ($sql_arr = $DB->fetch_assoc($sql_result))
     {
     $user_id = $sql_arr['user_id'];
-    
+    $user = $sql_arr['username'];
+    }
+
+  // parse $host URL
+  $a_host = parse_url($host);
+  if ($a_host['host'])
+    {
+    $host = $a_host['host'];
+    $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
+    $imap_port = isset($a_host['post']) ? $a_host['post'] : ($imap_ssl ? 993 : $CONFIG['default_port']);
+    }
+
+  // exit if IMAP login failed
+  if (!($imap_login  = $IMAP->connect($host, $user, $pass, $imap_port, $imap_ssl)))
+    return FALSE;
+
+  // user already registered
+  if ($user_id && !empty($sql_arr))
+    {
     // get user prefs
     if (strlen($sql_arr['preferences']))
       {
@@ -312,19 +321,22 @@
                       (created, last_login, username, mail_host)
                       VALUES (NOW(), NOW(), '%s', '%s')",
                      get_table_name('users'),
-                     $user, $host));
+                     addslashes($user),
+                     addslashes($host)));
 
   if ($user_id = $DB->insert_id())
     {
+    $user_email = strstr($user, '@') ? $user : sprintf('%s@%s', $user, $host);
+    $user_name = $user!=$user_email ? $user : '';
+    
     // also create a new identity record
     $DB->query(sprintf("INSERT INTO %s
                         (user_id, `default`, name, email)
-                        VALUES (%d, '1', '%s', '%s@%s')",
+                        VALUES (%d, '1', '%s', '%s')",
                        get_table_name('identities'),
                        $user_id,
-                       $user,
-                       $user,
-                       $host));
+                       addslashes($user_name),
+                       addslashes($user_email)));
                        
     // get existing mailboxes
     $a_mailboxes = $IMAP->list_mailboxes();
@@ -337,6 +349,14 @@
     if ($CONFIG['trash_mbox'] && !in_array_nocase($CONFIG['trash_mbox'], $a_mailboxes))
       $IMAP->create_mailbox($CONFIG['trash_mbox'], TRUE);
     }
+  else
+    {
+    raise_error(array('code' => 500,
+                      'type' => 'php',
+                      'line' => __LINE__,
+                      'file' => __FILE__,
+                      'message' => "Failed to create new user"), TRUE, FALSE);
+    }
     
   return $user_id;
   }
@@ -346,7 +366,7 @@
   {
   global $OUTPUT, $JS_OBJECT_NAME, $REMOTE_REQUEST;
 
-  $framed = ($_GET['framed'] || $_POST['_framed']);
+  $framed = $GLOBALS['_framed'];
   $command = sprintf("display_message('%s', '%s');",
                      addslashes(rep_specialchars_output(rcube_label($message))),
                      $type);
@@ -844,7 +864,7 @@
     $input = new textfield($attrib);
 
   // use value from post
-  if ($_POST[$fname])
+  if (!empty($_POST[$fname]))
     $value = $_POST[$fname];
 
   $out = $input->show($value);
@@ -887,10 +907,10 @@
   $week_secs = 60 * 60 * 24 * 7;
   $diff = $now - $timestamp;
 
-  // define daate format depending on current time  
-  if (!$format && $diff < $day_secs)
+  // define date format depending on current time  
+  if ($CONFIG['prettydate'] && !$format && $diff < $day_secs)
     return sprintf('%s %s', rcube_label('today'), date('H:i', $timestamp));
-  else if (!$format && $diff < $week_secs)
+  else if ($CONFIG['prettydate'] && !$format && $diff < $week_secs)
     $format = $CONFIG['date_short'] ? $CONFIG['date_short'] : 'D H:i';
   else if (!$format)
     $format = $CONFIG['date_long'] ? $CONFIG['date_long'] : 'd.m.Y H:i';
@@ -970,7 +990,10 @@
   if (is_array($CONFIG['default_host']))
     {
     $select_host = new select(array('name' => '_host'));
-    $select_host->add($CONFIG['default_host']);
+    
+    foreach ($CONFIG['default_host'] as $key => $value)
+      $select_host->add($value, (is_numeric($key) ? $value : $key));
+      
     $fields['host'] = $select_host->show($_POST['_host']);
     }
   else if (!strlen($CONFIG['default_host']))

--
Gitblit v1.9.1