[Svn] r4513 - trunk/roundcubemail/program/include

trac at roundcube.net trac at roundcube.net
Wed Feb 9 12:48:27 CET 2011


Author: thomasb
Date: 2011-02-09 05:48:27 -0600 (Wed, 09 Feb 2011)
New Revision: 4513

Modified:
   trunk/roundcubemail/program/include/rcube_ldap.php
Log:
Codestyle and formatting; don't attempt to save empty array values

Modified: trunk/roundcubemail/program/include/rcube_ldap.php
===================================================================
--- trunk/roundcubemail/program/include/rcube_ldap.php	2011-02-09 11:33:49 UTC (rev 4512)
+++ trunk/roundcubemail/program/include/rcube_ldap.php	2011-02-09 11:48:27 UTC (rev 4513)
@@ -27,802 +27,835 @@
  */
 class rcube_ldap extends rcube_addressbook
 {
-  protected $conn;
-  protected $prop = array();
-  protected $fieldmap = array();
+    /** public properties */
+    public $primary_key = 'ID';
+    public $groups = false;
+    public $readonly = true;
+    public $ready = false;
+    public $group_id = 0;
+    public $list_page = 1;
+    public $page_size = 10;
+    public $coltypes = array();
 
-  protected $filter = '';
-  protected $result = null;
-  protected $ldap_result = null;
-  protected $sort_col = '';
-  protected $mail_domain = '';
-  protected $debug = false;
+    /** private properties */
+    protected $conn;
+    protected $prop = array();
+    protected $fieldmap = array();
 
-  /** public properties */
-  public $primary_key = 'ID';
-  public $readonly = true;
-  public $groups = false;
-  public $list_page = 1;
-  public $page_size = 10;
-  public $group_id = 0;
-  public $ready = false;
-  public $coltypes = array();
+    protected $filter = '';
+    protected $result = null;
+    protected $ldap_result = null;
+    protected $sort_col = '';
+    protected $mail_domain = '';
+    protected $debug = false;
 
-  private $group_cache = array();
-  private $group_members = array();
+    private $group_cache = array();
+    private $group_members = array();
 
 
-  /**
-   * Object constructor
-   *
-   * @param array 	LDAP connection properties
-   * @param boolean 	Enables debug mode
-   * @param string 	Current user mail domain name
-   * @param integer User-ID
-   */
-  function __construct($p, $debug=false, $mail_domain=NULL)
-  {
-    $this->prop = $p;
+    /**
+    * Object constructor
+    *
+    * @param array 	LDAP connection properties
+    * @param boolean 	Enables debug mode
+    * @param string 	Current user mail domain name
+    * @param integer User-ID
+    */
+    function __construct($p, $debug=false, $mail_domain=NULL)
+    {
+        $this->prop = $p;
 
-    // check if groups are configured
-    if (is_array($p['groups']))
-      $this->groups = true;
-    
-    // fieldmap property is given
-    if (is_array($p['fieldmap'])) {
-      foreach ($p['fieldmap'] as $rf => $lf)
-        $this->fieldmap[$rf] = $this->_attr_name(strtolower($lf));
-    }
-    else {
-      // read deprecated *_field properties to remain backwards compatible
-      foreach ($p as $prop => $value)
-        if (preg_match('/^(.+)_field$/', $prop, $matches))
-          $this->fieldmap[$matches[1]] = $this->_attr_name(strtolower($value));
-    }
-    
-    // use fieldmap to advertise supported coltypes to the application
-    foreach ($this->fieldmap as $col => $lf) {
-      list($col, $type) = explode(':', $col);
-      if (!is_array($this->coltypes[$col])) {
-        $subtypes = $type ? array($type) : null;
-        $this->coltypes[$col] = array('limit' => 2, 'subtypes' => $subtypes);
-      }
-      else if ($type) {
-        $this->coltypes[$col]['subtypes'][] = $type;
-        $this->coltypes[$col]['limit']++;
-      }
-      if ($type && !$this->fieldmap[$col])
-        $this->fieldmap[$col] = $lf;
-    }
-    
-    if ($this->fieldmap['street'] && $this->fieldmap['locality'])
-      $this->coltypes['address'] = array('limit' => 1);
-    else if ($this->coltypes['address'])
-      $this->coltypes['address'] = array('type' => 'textarea', 'childs' => null, 'limit' => 1, 'size' => 40);
+        // check if groups are configured
+        if (is_array($p['groups']))
+            $this->groups = true;
 
-    // make sure 'required_fields' is an array
-    if (!is_array($this->prop['required_fields']))
-      $this->prop['required_fields'] = (array) $this->prop['required_fields'];
+        // fieldmap property is given
+        if (is_array($p['fieldmap'])) {
+            foreach ($p['fieldmap'] as $rf => $lf)
+                $this->fieldmap[$rf] = $this->_attr_name(strtolower($lf));
+        }
+        else {
+            // read deprecated *_field properties to remain backwards compatible
+            foreach ($p as $prop => $value)
+                if (preg_match('/^(.+)_field$/', $prop, $matches))
+                    $this->fieldmap[$matches[1]] = $this->_attr_name(strtolower($value));
+        }
 
-    foreach ($this->prop['required_fields'] as $key => $val)
-      $this->prop['required_fields'][$key] = $this->_attr_name(strtolower($val));
+        // use fieldmap to advertise supported coltypes to the application
+        foreach ($this->fieldmap as $col => $lf) {
+            list($col, $type) = explode(':', $col);
+            if (!is_array($this->coltypes[$col])) {
+                $subtypes = $type ? array($type) : null;
+                $this->coltypes[$col] = array('limit' => 2, 'subtypes' => $subtypes);
+            }
+            elseif ($type) {
+                $this->coltypes[$col]['subtypes'][] = $type;
+                $this->coltypes[$col]['limit']++;
+            }
+            if ($type && !$this->fieldmap[$col])
+                $this->fieldmap[$col] = $lf;
+        }
 
-    $this->sort_col = $p['sort'];
-    $this->debug = $debug;
-    $this->mail_domain = $mail_domain;
+        if ($this->fieldmap['street'] && $this->fieldmap['locality'])
+            $this->coltypes['address'] = array('limit' => 1);
+        else if ($this->coltypes['address'])
+            $this->coltypes['address'] = array('type' => 'textarea', 'childs' => null, 'limit' => 1, 'size' => 40);
 
-    $this->connect();
-  }
+        // make sure 'required_fields' is an array
+        if (!is_array($this->prop['required_fields']))
+            $this->prop['required_fields'] = (array) $this->prop['required_fields'];
 
+        foreach ($this->prop['required_fields'] as $key => $val)
+            $this->prop['required_fields'][$key] = $this->_attr_name(strtolower($val));
 
-  /**
-   * Establish a connection to the LDAP server
-   */
-  function connect()
-  {
-    global $RCMAIL;
-    
-    if (!function_exists('ldap_connect'))
-      raise_error(array('code' => 100, 'type' => 'ldap',
-        'file' => __FILE__, 'line' => __LINE__,
-        'message' => "No ldap support in this installation of PHP"), true);
+        $this->sort_col = $p['sort'];
+        $this->debug = $debug;
+        $this->mail_domain = $mail_domain;
 
-    if (is_resource($this->conn))
-      return true;
+        $this->_connect();
+    }
 
-    if (!is_array($this->prop['hosts']))
-      $this->prop['hosts'] = array($this->prop['hosts']);
 
-    if (empty($this->prop['ldap_version']))
-      $this->prop['ldap_version'] = 3;
-
-    foreach ($this->prop['hosts'] as $host)
+    /**
+    * Establish a connection to the LDAP server
+    */
+    private function _connect()
     {
-      $host = rcube_idn_to_ascii(rcube_parse_host($host));
-      $this->_debug("C: Connect [$host".($this->prop['port'] ? ':'.$this->prop['port'] : '')."]");
+        global $RCMAIL;
 
-      if ($lc = @ldap_connect($host, $this->prop['port']))
-      {
-        if ($this->prop['use_tls']===true)
-          if (!ldap_start_tls($lc))
-            continue;
+        if (!function_exists('ldap_connect'))
+            raise_error(array('code' => 100, 'type' => 'ldap',
+            'file' => __FILE__, 'line' => __LINE__,
+            'message' => "No ldap support in this installation of PHP"), true);
 
-        $this->_debug("S: OK");
+        if (is_resource($this->conn))
+            return true;
 
-        ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $this->prop['ldap_version']);
-        $this->prop['host'] = $host;
-        $this->conn = $lc;
-        break;
-      }
-      $this->_debug("S: NOT OK");
-    }
-    
-    if (is_resource($this->conn))
-    {
-      $this->ready = true;
+        if (!is_array($this->prop['hosts']))
+            $this->prop['hosts'] = array($this->prop['hosts']);
 
-      // User specific access, generate the proper values to use.
-      if ($this->prop['user_specific']) {
-        // No password set, use the session password
-        if (empty($this->prop['bind_pass'])) {
-          $this->prop['bind_pass'] = $RCMAIL->decrypt($_SESSION['password']);
+        if (empty($this->prop['ldap_version']))
+            $this->prop['ldap_version'] = 3;
+
+        foreach ($this->prop['hosts'] as $host)
+        {
+            $host = idn_to_ascii(rcube_parse_host($host));
+            $this->_debug("C: Connect [$host".($this->prop['port'] ? ':'.$this->prop['port'] : '')."]");
+
+            if ($lc = @ldap_connect($host, $this->prop['port']))
+            {
+                if ($this->prop['use_tls']===true)
+                    if (!ldap_start_tls($lc))
+                        continue;
+
+                $this->_debug("S: OK");
+
+                ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $this->prop['ldap_version']);
+                $this->prop['host'] = $host;
+                $this->conn = $lc;
+                break;
+            }
+            $this->_debug("S: NOT OK");
         }
 
-        // Get the pieces needed for variable replacement.
-        $fu = $RCMAIL->user->get_username();
-        list($u, $d) = explode('@', $fu);
-        $dc = 'dc='.strtr($d, array('.' => ',dc=')); // hierarchal domain string
+        if (is_resource($this->conn))
+        {
+            $this->ready = true;
 
-        $replaces = array('%dc' => $dc, '%d' => $d, '%fu' => $fu, '%u' => $u);
+            // User specific access, generate the proper values to use.
+            if ($this->prop['user_specific']) {
+                // No password set, use the session password
+                if (empty($this->prop['bind_pass'])) {
+                    $this->prop['bind_pass'] = $RCMAIL->decrypt($_SESSION['password']);
+                }
 
-        if ($this->prop['search_base_dn'] && $this->prop['search_filter']) {
-          // Search for the dn to use to authenticate
-          $this->prop['search_base_dn'] = strtr($this->prop['search_base_dn'], $replaces);
-          $this->prop['search_filter'] = strtr($this->prop['search_filter'], $replaces);
+                // Get the pieces needed for variable replacement.
+                $fu = $RCMAIL->user->get_username();
+                list($u, $d) = explode('@', $fu);
+                $dc = 'dc='.strtr($d, array('.' => ',dc=')); // hierarchal domain string
 
-          $this->_debug("S: searching with base {$this->prop['search_base_dn']} for {$this->prop['search_filter']}");
+                $replaces = array('%dc' => $dc, '%d' => $d, '%fu' => $fu, '%u' => $u);
 
-          $res = ldap_search($this->conn, $this->prop['search_base_dn'], $this->prop['search_filter'], array('uid'));
-          if ($res && ($entry = ldap_first_entry($this->conn, $res))) {
-            $bind_dn = ldap_get_dn($this->conn, $entry);
+                if ($this->prop['search_base_dn'] && $this->prop['search_filter']) {
+                    // Search for the dn to use to authenticate
+                    $this->prop['search_base_dn'] = strtr($this->prop['search_base_dn'], $replaces);
+                    $this->prop['search_filter'] = strtr($this->prop['search_filter'], $replaces);
 
-            $this->_debug("S: search returned dn: $bind_dn");
+                    $this->_debug("S: searching with base {$this->prop['search_base_dn']} for {$this->prop['search_filter']}");
 
-            if ($bind_dn) {
-              $this->prop['bind_dn'] = $bind_dn;
-              $dn = ldap_explode_dn($bind_dn, 1);
-              $replaces['%dn'] = $dn[0];
+                    $res = ldap_search($this->conn, $this->prop['search_base_dn'], $this->prop['search_filter'], array('uid'));
+                    if ($res && ($entry = ldap_first_entry($this->conn, $res))) {
+                        $bind_dn = ldap_get_dn($this->conn, $entry);
+
+                        $this->_debug("S: search returned dn: $bind_dn");
+
+                        if ($bind_dn) {
+                            $this->prop['bind_dn'] = $bind_dn;
+                            $dn = ldap_explode_dn($bind_dn, 1);
+                            $replaces['%dn'] = $dn[0];
+                        }
+                    }
+                }
+                // Replace the bind_dn and base_dn variables.
+                $this->prop['bind_dn'] = strtr($this->prop['bind_dn'], $replaces);
+                $this->prop['base_dn'] = strtr($this->prop['base_dn'], $replaces);
             }
-          }
+
+            if (!empty($this->prop['bind_dn']) && !empty($this->prop['bind_pass']))
+                $this->ready = $this->_bind($this->prop['bind_dn'], $this->prop['bind_pass']);
         }
-        // Replace the bind_dn and base_dn variables.
-        $this->prop['bind_dn'] = strtr($this->prop['bind_dn'], $replaces);
-        $this->prop['base_dn'] = strtr($this->prop['base_dn'], $replaces);
-      }
+        else
+            raise_error(array('code' => 100, 'type' => 'ldap',
+                'file' => __FILE__, 'line' => __LINE__,
+                'message' => "Could not connect to any LDAP server, last tried $host:{$this->prop[port]}"), true);
 
-      if (!empty($this->prop['bind_dn']) && !empty($this->prop['bind_pass']))
-        $this->ready = $this->bind($this->prop['bind_dn'], $this->prop['bind_pass']);
+        // See if the directory is writeable.
+        if ($this->prop['writable']) {
+            $this->readonly = false;
+        } // end if
     }
-    else
-      raise_error(array('code' => 100, 'type' => 'ldap',
-        'file' => __FILE__, 'line' => __LINE__,
-        'message' => "Could not connect to any LDAP server, last tried $host:{$this->prop[port]}"), true);
 
-    // See if the directory is writeable.
-    if ($this->prop['writable']) {
-      $this->readonly = false;
-    } // end if
 
-  }
+    /**
+    * Bind connection with DN and password
+    *
+    * @param string Bind DN
+    * @param string Bind passwo