Author Topic: Server.inc.php for Server configuration  (Read 6749 times)

Offline RolfXXL

  • Newbie
  • *
  • Posts: 9
Server.inc.php for Server configuration
« on: September 15, 2006, 06:04:18 AM »
Hey ya!

Since i worked with Horde/IMP before, i was used to define multiple Server in a seperated file, specifying alsl the server-specific option
in an array.
Looking at RoundCube the server configuration was really drivin me nuts.

So i created a new server-configuration model like

$rcmail_server['IMAP1'] = array(
    'name' => 'IMAP1',
    'imap_host' => 'host.imap.com',
    'imap_port' => '143',
    'mail_domain' => 'imap.com',
    'smtp_host' => 'smtp.imap.com',
    'smtp_port' => '25',
    'smtp_user' => '%u',
    'smtp_pass' => '%p',
    'smtp_auth_type' => 'LOGIN',
    'imap_root' => 'mail/'
);

giving u the opportunity to specify all the options in good-readable style for each server in a new config file server.inc.php.

If the community (so u ;-) ) is interested in it, i would code it clear and free off my debug-code and make a diff, too.

So, interested?

Offline desertadmin

  • Jr. Member
  • **
  • Posts: 36
Re: Server.inc.php for Server configuration
« Reply #1 on: September 15, 2006, 07:52:14 PM »
I think it would be great do you have any examples?

Offline RolfXXL

  • Newbie
  • *
  • Posts: 9
Re: Server.inc.php for Server configuration
« Reply #2 on: September 17, 2006, 04:39:21 PM »
Don`t know what u mean with "example", but here is my patch.
The values used in here can be deleted out of main.inc.php, but i didn`t want to patch your config ;-)

try this:

Code: [Select]
diff -u --recursive --new-file roundcubemail-0.1beta2.orig/config/server.inc.php round.new/config/server.inc.php
--- roundcubemail-0.1beta2.orig/config/server.inc.php 1970-01-01 01:00:00.000000000 +0100
+++ round.new/config/server.inc.php 2006-09-15 00:11:19.000000000 +0200
@@ -0,0 +1,48 @@
+<?php
+
+
/*
+ +-----------------------------------------------------------------------+
+ | Server configuration file                       |
+ |                                    |
+ | This file is part of the RoundCube Webmail client           |
+ | Copyright (C) 2005, RoundCube Dev. - Switzerland           |
+ | Licensed under the GNU GPL                      |
+ |                                    | 
+ | Multiple Static Server with server.inc.php              |
+ | Implemented by S.B.                          |
+ |                                    |
+ +-----------------------------------------------------------------------+
+
+*/
+
+
$rcmail_server = array();
+
+
$rcmail_server['IMAP1'] = array(
+
'name' => 'IMAP1',
+
'imap_host' => 'imap1.host.de',
+
'imap_port' => '143',
+
'mail_domain' => 'host.de',
+
'smtp_host' => 'smtp.host.de',
+
'smtp_port' => '25',
+
'smtp_user' => '%u',
+
'smtp_pass' => '%p',
+
'smtp_auth_type' => 'LOGIN',
+
'imap_root' => 'mail/'
+);
+
+
$rcmail_server['IMAP2'] = array(
+
'name' => 'IMAP2',
+
'imap_host' => 'imap2.server.de',
+
'imap_port' => '143',
+
'mail_domain' => 'server.de',
+
'smtp_host' => 'smtp.server.de',
+
'smtp_port' => '25',
+
'smtp_user' => '%u',
+
'smtp_pass' => '%p',
+
'smtp_auth_type' => 'LOGIN',
+
'imap_root' => ''
+);
+
+
+
// end of config file
+?>

diff -u --recursive --new-file roundcubemail-0.1beta2.orig/index.php round.new/index.php
--- roundcubemail-0.1beta2.orig/index.php 2006-08-06 17:55:11.000000000 +0200
+++ round.new/index.php 2006-09-15 12:36:01.000000000 +0200
@@ -143,7 +143,9 @@
 // try to log in
 if ($_action=='login' && $_task=='mail')
  {
- $host = $_POST['_host'] ? $_POST['_host'] : $CONFIG['default_host'];
+  $srv_alias = $_POST['_host'];
+  $host = $SERVER[$srv_alias]['imap_host'];
+  $_SESSION['alias'] = $srv_alias;
 
  // check if client supports cookies
  if (empty($_COOKIE))
diff -u --recursive --new-file roundcubemail-0.1beta2.orig/program/include/main.inc round.new/program/include/main.inc
--- roundcubemail-0.1beta2.orig/program/include/main.inc 2006-08-06 17:55:11.000000000 +0200
+++ round.new/program/include/main.inc 2006-09-15 12:30:20.000000000 +0200
@@ -34,7 +34,7 @@
 function rcmail_startup($task='mail')
  {
  global $sess_id, $sess_auth, $sess_user_lang;
- global $CONFIG, $INSTALL_PATH, $BROWSER, $OUTPUT, $_SESSION, $IMAP, $DB, $JS_OBJECT_NAME;
+ global $CONFIG, $SERVER, $INSTALL_PATH, $BROWSER, $OUTPUT, $_SESSION, $IMAP, $DB, $JS_OBJECT_NAME;
 
  // check client
  $BROWSER = rcube_browser();
@@ -42,6 +42,10 @@
  // load config file
  include_once('config/main.inc.php');
  $CONFIG = is_array($rcmail_config) ? $rcmail_config : array();
+
+ // load server file
+ include_once('config/server.inc.php');
+ $SERVER = is_array($rcmail_server) ? $rcmail_server : array();
 
  // load host-specific configuration
  rcmail_load_host_config($CONFIG);
@@ -215,11 +219,12 @@
 // this must be done AFTER connecting to the server
 function rcmail_set_imap_prop()
  {
- global $CONFIG, $IMAP;
+ global $CONFIG, $SERVER, $IMAP;
 
  // set root dir from config
- if (!empty($CONFIG['imap_root']))
-  $IMAP->set_rootdir($CONFIG['imap_root']);
+ $srv_alias = $_SESSION['alias'];
+ if (!empty($SERVER[$srv_alias]['imap_root']))
+ $IMAP->set_rootdir($SERVER[$srv_alias]['imap_root']);
 
  if (is_array($CONFIG['default_imap_folders']))
   $IMAP->set_default_mailboxes($CONFIG['default_imap_folders']);
@@ -417,8 +422,10 @@
 // perfom login to the IMAP server and to the webmail service
 function rcmail_login($user, $pass, $host=NULL)
  {
- global $CONFIG, $IMAP, $DB, $sess_user_lang;
+ global $CONFIG, $SERVER, $IMAP, $DB, $sess_user_lang;
  $user_id = NULL;
+
+ $srv_alias = $_SESSION['alias'];
 
  if (!$host)
   $host = $CONFIG['default_host'];
@@ -429,10 +436,10 @@
   {
   $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['port']) ? $a_host['port'] : ($imap_ssl ? 993 : $CONFIG['default_port']);
+  $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : $SERVER['srv_alias']['imap_port']);
   }
  else
-  $imap_port = $CONFIG['default_port'];
+  $imap_port = $SERVER['srv_alias']['imap_port'];
 
 
  /* Modify username with domain if required
@@ -525,7 +532,7 @@
 // create new entry in users and identities table
 function rcmail_create_user($user, $host)
  {
- global $DB, $CONFIG, $IMAP;
+ global $DB, $CONFIG, $SERVER, $IMAP;
 
  $user_email = '';
 
@@ -543,14 +550,15 @@
 
  if ($user_id = $DB->insert_id(get_sequence_name('users')))
   {
+  $srv_alias = $_SESSION['alias'];
   $mail_domain = $host;
-  if (is_array($CONFIG['mail_domain']))
+  if (is_array($SERVER[$srv_alias]['mail_domain']))
    {
-   if (isset($CONFIG['mail_domain'][$host]))
-    $mail_domain = $CONFIG['mail_domain'][$host];
+   if (isset($SERVER[$srv_alias]['mail_domain'][$host]))
+    $mail_domain = $SERVER[$srv_alias]['mail_domain'][$host];
    }
-  else if (!empty($CONFIG['mail_domain']))
-   $mail_domain = $CONFIG['mail_domain'];
+  else if (!empty($SERVER[$srv_alias]['mail_domain']))
+   $mail_domain = $SERVER[$srv_alias]['mail_domain'];
 
   if ($user_email=='')
    $user_email = strstr($user, '@') ? $user : sprintf('%s@%s', $user, $mail_domain);
@@ -1686,7 +1694,7 @@
 // return code for the webmail login form
 function rcmail_login_form($attrib)
  {
- global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD;
+ global $CONFIG, $SERVER, $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD;
 
  $labels = array();
  $labels['user'] = rcube_label('username');
@@ -1701,17 +1709,24 @@
  $fields['user'] = $input_user->show(get_input_value('_user', RCUBE_INPUT_POST));
  $fields['pass'] = $input_pass->show();
  $fields['action'] = $input_action->show();
+
+ $server_list = array();
+ $val = array();
+
+ while(list($key, $val) = each($SERVER)) {
+ array_push($server_list,$key);
+ }
 
- if (is_array($CONFIG['default_host']))
+ if (is_array($server_list))
   {
   $select_host = new select(array('name' => '_host', 'id' => 'rcmloginhost'));
   
-  foreach ($CONFIG['default_host'] as $key => $value)
+  foreach ($server_list 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']))
+ else if (!strlen($server_list))
   {
  $input_host = new textfield(array('name' => '_host', 'id' => 'rcmloginhost', 'size' => 30));
  $fields['host'] = $input_host->show($_POST['_host']);
diff -u --recursive --new-file roundcubemail-0.1beta2.orig/program/include/rcube_smtp.inc round.new/program/include/rcube_smtp.inc
--- roundcubemail-0.1beta2.orig/program/include/rcube_smtp.inc 2006-06-26 20:38:03.000000000 +0200
+++ round.new/program/include/rcube_smtp.inc 2006-09-15 12:31:25.000000000 +0200
@@ -51,11 +51,12 @@
 */
 function smtp_mail($from, $recipients, &$headers, &$body)
  {
- global $SMTP_CONN, $CONFIG, $SMTP_ERROR;
+ global $SMTP_CONN, $CONFIG, $SERVER, $SMTP_ERROR;
  $smtp_timeout = null;
- $smtp_host = $CONFIG['smtp_server'];
- $smtp_port = is_numeric($CONFIG['smtp_port']) ? $CONFIG['smtp_port'] : 25;
- $smtp_host_url = parse_url($CONFIG['smtp_server']);
+ $srv_alias = $_SESSION['alias'];
+ $smtp_host = $SERVER[$srv_alias]['smtp_host'];
+ $smtp_port = is_numeric($SERVER[$srv_alias]['smtp_port']) ? $SERVER[$srv_alias]['smtp_port'] : 25;
+ $smtp_host_url = parse_url($SERVER[$srv_alias]['smtp_host']);
 
  // overwrite port
  if ($smtp_host_url['host'] && $smtp_host_url['port'])
@@ -90,19 +91,19 @@
    }
   
   // attempt to authenticate to the SMTP server
-  if ($CONFIG['smtp_user'] && $CONFIG['smtp_pass'])
+  if ($SERVER[$srv_alias]['smtp_user'] && $SERVER[$srv_alias]['smtp_pass'])
    {
-   if (strstr($CONFIG['smtp_user'], '%u'))
- $smtp_user = str_replace('%u', $_SESSION['username'], $CONFIG['smtp_user']);
+   if (strstr($SERVER[$srv_alias]['smtp_user'], '%u'))
+ $smtp_user = str_replace('%u', $_SESSION['username'], $SERVER[$srv_alias]['smtp_user']);
    else
- $smtp_user = $CONFIG['smtp_user'];
+ $smtp_user = $SERVER[$srv_alias]['smtp_user'];
 
- if (strstr($CONFIG['smtp_pass'], '%p'))
- $smtp_pass = str_replace('%p', decrypt_passwd($_SESSION['password']), $CONFIG['smtp_pass']);
+ if (strstr($SERVER[$srv_alias]['smtp_pass'], '%p'))
+ $smtp_pass = str_replace('%p', decrypt_passwd($_SESSION['password']), $SERVER[$srv_alias]['smtp_pass']);
    else
- $smtp_pass = $CONFIG['smtp_pass'];
+ $smtp_pass = $SERVER[$srv_alias]['smtp_pass'];
 
- $smtp_auth_type = empty($CONFIG['smtp_auth_type']) ? NULL : $CONFIG['smtp_auth_type'];
+ $smtp_auth_type = empty($SERVER[$srv_alias]['smtp_auth_type']) ? NULL : $SERVER[$srv_alias]['smtp_auth_type'];
  $result = $SMTP_CONN->auth($smtp_user, $smtp_pass, $smtp_auth_type);
 
    if (PEAR::isError($result))
diff -u --recursive --new-file roundcubemail-0.1beta2.orig/program/steps/mail/sendmail.inc round.new/program/steps/mail/sendmail.inc
--- roundcubemail-0.1beta2.orig/program/steps/mail/sendmail.inc 2006-08-01 00:51:23.000000000 +0200
+++ round.new/program/steps/mail/sendmail.inc 2006-09-14 23:27:23.000000000 +0200
@@ -26,6 +26,9 @@
 require_once('Mail/mime.php');
 
 
+$srv_alias = $_SESSION['alias'];
+
+
 if (!isset($_SESSION['compose']['id']))
  {
  rcmail_overwrite_action('list');
@@ -225,7 +228,7 @@
 if (!$savedraft) {
 
  // send thru SMTP server using custom SMTP library
- if ($CONFIG['smtp_server'])
+ if ($SERVER[$srv_alias]['smtp_host'])
   {
   // generate list of recipients
   $a_recipients = array($mailto);