From bb8721aaeb4961f0dee8ca250749906e01a8f6a8 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Thu, 03 Jun 2010 04:02:12 -0400
Subject: [PATCH] - Support dynamic hostname (%d/%n) variables in configuration options (#1485438)

---
 program/include/rcube_smtp.php   |    2 +-
 CHANGELOG                        |    1 +
 program/include/main.inc         |   16 ++++++++++++++++
 program/include/rcube_ldap.php   |    1 +
 program/include/rcmail.php       |    4 ++--
 config/main.inc.php.dist         |   20 +++++++++++++++++++-
 program/include/rcube_config.php |    8 ++++----
 7 files changed, 44 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index ebcfbd1..b4c4391 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG RoundCube Webmail
 ===========================
 
+- Support dynamic hostname (%d/%n) variables in configuration options (#1485438)
 - Add 'messages_list' hook (#1486266)
 - Add request* event triggers in http_post/http_request (#1486054)
 - Fix use RFC-compliant line-delimiter when saving messages on IMAP (#1486712)
diff --git a/config/main.inc.php.dist b/config/main.inc.php.dist
index 0b39d76..34a9763 100644
--- a/config/main.inc.php.dist
+++ b/config/main.inc.php.dist
@@ -61,6 +61,10 @@
 // leave blank to show a textbox at login, give a list of hosts
 // to display a pulldown menu or set one host as string.
 // To use SSL/TLS connection, enter hostname with prefix ssl:// or tls://
+// Supported replacement variables:
+// %n - http hostname ($_SERVER['SERVER_NAME'])
+// %d - domain (http hostname without the first part)
+// For example %n = mail.domain.tld, %d = domain.tld
 $rcmail_config['default_host'] = '';
 
 // TCP port used for IMAP connections
@@ -90,7 +94,11 @@
 // SMTP server host (for sending mails).
 // To use SSL/TLS connection, enter hostname with prefix ssl:// or tls://
 // If left blank, the PHP mail() function is used
-// Use %h variable as replacement for user's IMAP hostname
+// Supported replacement variables:
+// %h - user's IMAP hostname
+// %n - http hostname ($_SERVER['SERVER_NAME'])
+// %d - domain (http hostname without the first part)
+// For example %n = mail.domain.tld, %d = domain.tld
 $rcmail_config['smtp_server'] = '';
 
 // SMTP port (default is 25; 465 for SSL)
@@ -176,6 +184,11 @@
 
 // This domain will be used to form e-mail addresses of new users
 // Specify an array with 'host' => 'domain' values to support multiple hosts
+// Supported replacement variables:
+// %h - user's IMAP hostname
+// %n - http hostname ($_SERVER['SERVER_NAME'])
+// %d - domain (http hostname without the first part)
+// For example %n = mail.domain.tld, %d = domain.tld
 $rcmail_config['mail_domain'] = '';
 
 // Password charset.
@@ -374,6 +387,11 @@
  *
 $rcmail_config['ldap_public']['Verisign'] = array(
   'name'          => 'Verisign.com',
+  // Replacement variables supported in host names:
+  // %h - user's IMAP hostname
+  // %n - http hostname ($_SERVER['SERVER_NAME'])
+  // %d - domain (http hostname without the first part)
+  // For example %n = mail.domain.tld, %d = domain.tld
   'hosts'         => array('directory.verisign.com'),
   'port'          => 389,
   'use_tls'	    => false,
diff --git a/program/include/main.inc b/program/include/main.inc
index 6a81791..e8e9216 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -1532,6 +1532,7 @@
   return false;
 }
 
+
 // for backward compatibility
 function rcube_sess_unset($var_name=null)
 {
@@ -1541,6 +1542,21 @@
 }
 
 
+// Replaces hostname variables
+function rcube_parse_host($name)
+{
+  // %n - host
+  $n = preg_replace('/:\d+$/', '', $_SERVER['SERVER_NAME']);
+  // %d - domain name without first part, e.g. %d=mail.domain.tld, %m=domain.tld
+  $d = preg_replace('/^[^\.]+\./', '', $n);
+  // %h - IMAP host
+  $h = $_SESSION['imap_host'];
+
+  $name = str_replace(array('%n', '%d', '%h'), array($n, $d, $h), $name);
+  return $name;
+}
+
+
 /**
  * E-mail address validation
  */
diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index 652dbd0..20b7bf7 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -575,7 +575,7 @@
       if (!$allowed)
         return false;
       }
-    else if (!empty($config['default_host']) && $host != $config['default_host'])
+    else if (!empty($config['default_host']) && $host != rcube_parse_host($config['default_host']))
       return false;
 
     // parse $host URL
@@ -743,7 +743,7 @@
       $host = get_input_value('_host', RCUBE_INPUT_POST);
     }
     else
-      $host = $default_host;
+      $host = rcube_parse_host($default_host);
 
     return $host;
   }
diff --git a/program/include/rcube_config.php b/program/include/rcube_config.php
index bd53517..eb2e088 100644
--- a/program/include/rcube_config.php
+++ b/program/include/rcube_config.php
@@ -277,12 +277,12 @@
                 $domain = $this->prop['mail_domain'][$host];
         }
         else if (!empty($this->prop['mail_domain']))
-            $domain = $this->prop['mail_domain'];
-    
+            $domain = rcube_parse_host($this->prop['mail_domain']);
+
         return $domain;
     }
-  
-  
+
+
     /**
      * Getter for error state
      *
diff --git a/program/include/rcube_ldap.php b/program/include/rcube_ldap.php
index 307e43e..63c193f 100644
--- a/program/include/rcube_ldap.php
+++ b/program/include/rcube_ldap.php
@@ -99,6 +99,7 @@
 
     foreach ($this->prop['hosts'] as $host)
     {
+      $host = rcube_parse_host($host);
       $this->_debug("C: Connect [$host".($this->prop['port'] ? ':'.$this->prop['port'] : '')."]");
 
       if ($lc = @ldap_connect($host, $this->prop['port']))
diff --git a/program/include/rcube_smtp.php b/program/include/rcube_smtp.php
index 4471435..4525502 100644
--- a/program/include/rcube_smtp.php
+++ b/program/include/rcube_smtp.php
@@ -73,7 +73,7 @@
       'smtp_timeout'   => $RCMAIL->config->get('smtp_timeout'),
     ));
 
-    $smtp_host = str_replace('%h', $_SESSION['imap_host'], $CONFIG['smtp_server']);
+    $smtp_host = rcube_parse_host($CONFIG['smtp_server']);
     // when called from Installer it's possible to have empty $smtp_host here
     if (!$smtp_host) $smtp_host = 'localhost';
     $smtp_port = is_numeric($CONFIG['smtp_port']) ? $CONFIG['smtp_port'] : 25;

--
Gitblit v1.9.1