3 commits - lib/api lib/Auth lib/kolab_api_controller.php lib/kolab_api_service.php lib/User.php public_html/api

Jeroen van Meeuwen vanmeeuwen at kolabsys.com
Fri May 25 12:58:14 CEST 2012


 lib/Auth/LDAP.php                        |   60 +++---
 lib/User.php                             |    2 
 lib/api/kolab_api_service_form_value.php |  276 ++++++++++++++++++++++++-------
 lib/api/kolab_api_service_group.php      |    2 
 lib/kolab_api_controller.php             |    4 
 lib/kolab_api_service.php                |  130 +++++++-------
 public_html/api/index.php                |    2 
 7 files changed, 324 insertions(+), 152 deletions(-)

New commits:
commit 216ff1b095ea1f2a09b1ee4cd45bacbd9e934922
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Fri May 25 11:58:10 2012 +0100

    Replace uses of error_log(), with console() and disable them

diff --git a/lib/Auth/LDAP.php b/lib/Auth/LDAP.php
index 799dfa9..7146f71 100644
--- a/lib/Auth/LDAP.php
+++ b/lib/Auth/LDAP.php
@@ -72,7 +72,7 @@ class LDAP
                     $domain = $_SESSION['user']->get_domain();
                 } catch (Exception $e) {
                     // TODO: Debug logging
-                    error_log("Warning, user not authenticated yet");
+                    //console("Warning, user not authenticated yet");
                 }
             }
         }
@@ -128,7 +128,7 @@ class LDAP
      */
     public function authenticate($username, $password)
     {
-        error_log("LDAP authentication request for $username");
+        //console("LDAP authentication request for $username");
 
         if (!$this->_connect()) {
             return false;
@@ -189,17 +189,17 @@ class LDAP
                 $_SESSION['user']->user_root_dn = $root_dn;
                 $_SESSION['user']->user_bind_dn = $subject_dn;
                 $_SESSION['user']->user_bind_pw = $password;
-                error_log("Successfully bound with User DN: " . $_SESSION['user']->user_bind_dn);
+                //console("Successfully bound with User DN: " . $_SESSION['user']->user_bind_dn);
             }
             else {
-                error_log("Successfully bound with User DN: " . $subject_dn . " but not saving it to the session");
+                //console("Successfully bound with User DN: " . $subject_dn . " but not saving it to the session");
             }
 
             // @TODO: return unique attribute
             return $subject_dn;
         }
         else {
-            error_log("LDAP Error: " . $this->_errstr());
+            //console("LDAP Error: " . $this->_errstr());
             return false;
         }
     }
@@ -236,7 +236,7 @@ class LDAP
                     }
                 }
             } else {
-                error_log("No schema details exist for attribute $attribute (which is strange)");
+                //console("No schema details exist for attribute $attribute (which is strange)");
             }
 
             // The relevant parts only, please
@@ -364,7 +364,7 @@ class LDAP
         $supported_controls = $this->supported_controls();
 
         if (!in_array($effective_rights_control_oid, $supported_controls)) {
-            error_log("No getEffectiveRights control in supportedControls");
+            //console("No getEffectiveRights control in supportedControls");
             return $this->legacy_rights($subject);
         }
 
@@ -461,7 +461,7 @@ class LDAP
 
     public function find_user_groups($member_dn)
     {
-        error_log(__FILE__ . "(" . __LINE__ . "): " .  $member_dn);
+        //console(__FILE__ . "(" . __LINE__ . "): " .  $member_dn);
 
         $groups = array();
 
@@ -935,11 +935,11 @@ class LDAP
             return false;
         }
 
-        error_log("Searching for domain $domain");
-        error_log("From domain to root dn");
+        //console("Searching for domain $domain");
+        //console("From domain to root dn");
 
         if (($this->_bind($conf->get('ldap', 'bind_dn'), $conf->get('ldap', 'bind_pw'))) == false) {
-            error_log("WARNING: Invalid Service bind credentials supplied");
+            //console("WARNING: Invalid Service bind credentials supplied");
             $this->_bind($conf->manager_bind_dn, $conf->manager_bind_pw);
         }
 
@@ -967,7 +967,7 @@ class LDAP
 
         $this->_unbind();
 
-        error_log("Using $domain_rootdn");
+        //console("Using $domain_rootdn");
 
         return $domain_rootdn;
     }
@@ -1059,11 +1059,11 @@ class LDAP
         $result = self::normalize_result($this->_search($base_dn, $filter, array_keys($attribute)));
 
         if (count($result) > 0) {
-            error_log("Results found: " . implode(', ', array_keys($result)));
+            //console("Results found: " . implode(', ', array_keys($result)));
             return $result;
         }
         else {
-            error_log("No result");
+            //console("No result");
             return false;
         }
     }
@@ -1435,7 +1435,7 @@ class LDAP
         }
 
         if (!$result) {
-            error_log("LDAP Error: " . $this->_errstr());
+            //console("LDAP Error: " . $this->_errstr());
             return false;
         }
 
@@ -1783,13 +1783,13 @@ class LDAP
         }
 
         // TODO: Debug logging
-        error_log("->_bind() Binding with $dn");
+        //console("->_bind() Binding with $dn");
 
         $this->bind_dn = $dn;
         $this->bind_pw = $pw;
 
         if (($bind_ok = ldap_bind($this->conn, $dn, $pw)) == false) {
-            error_log("LDAP Error: " . $this->_errstr());
+            //console("LDAP Error: " . $this->_errstr());
             // Issue error message
             return false;
         }
@@ -1809,13 +1809,13 @@ class LDAP
         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 9);
 
         // TODO: Debug logging
-        error_log("Connecting to " . $this->_ldap_server . " on port " . $this->_ldap_port);
+        //console("Connecting to " . $this->_ldap_server . " on port " . $this->_ldap_port);
         $connection = ldap_connect($this->_ldap_server, $this->_ldap_port);
 
         if ($connection == false) {
             $this->conn = null;
             // TODO: Debug logging
-            error_log("Not connected: " . ldap_err2str() .  "(no.) " . ldap_errno());
+            //console("Not connected: " . ldap_err2str() .  "(no.) " . ldap_errno());
             return false;
         }
 
@@ -1824,7 +1824,7 @@ class LDAP
         ldap_set_option($this->conn, LDAP_OPT_PROTOCOL_VERSION, 3);
 
         // TODO: Debug logging
-        error_log("Connected!");
+        //console("Connected!");
 
         return true;
     }
@@ -2038,13 +2038,13 @@ class LDAP
      */
     private function _probe_root_dn($entry_root_dn)
     {
-        error_log("Running for entry root dn: " . $entry_root_dn);
+        //console("Running for entry root dn: " . $entry_root_dn);
         if (($tmpconn = ldap_connect($this->_ldap_server)) == false) {
             //message("LDAP Error: " . $this->_errstr());
             return false;
         }
 
-        error_log("User DN: " . $_SESSION['user']->user_bind_dn);
+        //console("User DN: " . $_SESSION['user']->user_bind_dn);
 
         if (($bind_success = ldap_bind($tmpconn, $_SESSION['user']->user_bind_dn, $_SESSION['user']->user_bind_pw)) == false) {
             //message("LDAP Error: " . $this->_errstr());
@@ -2105,7 +2105,7 @@ class LDAP
             $this->_bind($this->conf->get('manager_bind_dn'), $this->conf->get('manager_bind_pw'));
         }
 
-        error_log("Searching for a group dn in $root_dn, with search filter: $search_filter");
+        //console("Searching for a group dn in $root_dn, with search filter: $search_filter");
 
         $search_results = ldap_search($this->conn, $root_dn, $search_filter);
 
@@ -2129,7 +2129,7 @@ class LDAP
             $this->_bind($this->conf->get('manager_bind_dn'), $this->conf->get('manager_bind_pw'));
         }
 
-        error_log("Searching for a user dn in $root_dn, with search filter: $search_filter");
+        //console("Searching for a user dn in $root_dn, with search filter: $search_filter");
 
         $search_results = ldap_search($this->conn, $root_dn, $search_filter);
 
@@ -2153,10 +2153,10 @@ class LDAP
 
         if (is_array($entry) && in_array('objectclass', $entry)) {
             if (!in_array(array('groupofnames', 'groupofuniquenames', 'groupofurls'), $entry['objectclass'])) {
-                error_log("Called _list_groups_members on a non-group!");
+                //console("Called _list_groups_members on a non-group!");
             }
             else {
-                error_log("Called list_group_members(" . $dn . ")");
+                //console("Called list_group_members(" . $dn . ")");
             }
         }
 
@@ -2189,7 +2189,7 @@ class LDAP
 
     private function _list_group_member($dn, $members, $recurse = true)
     {
-        error_log("Called _list_group_member(" . $dn . ")");
+        //console("Called _list_group_member(" . $dn . ")");
 
         $group_members = array();
 
@@ -2266,7 +2266,7 @@ class LDAP
 
     private function _list_group_memberurl($dn, $memberurls, $recurse = true)
     {
-        error_log("Called _list_group_memberurl(" . $dn . ")");
+        //console("Called _list_group_memberurl(" . $dn . ")");
 
         // Use the member attributes to return an array of member ldap objects
         // NOTE that the member attribute is supposed to contain a DN
@@ -2280,7 +2280,7 @@ class LDAP
 
             foreach ($entries as $entry_dn => $_entry) {
                 $group_members[$entry_dn] = $_entry;
-                error_log("Found " . $entry_dn);
+                //console("Found " . $entry_dn);
 
                 if ($recurse) {
                     // Nested group
@@ -2304,7 +2304,7 @@ class LDAP
      */
     private function _parse_memberurl($url)
     {
-        error_log("Parsing URL: " . $url);
+        //console("Parsing URL: " . $url);
         preg_match('/(.*):\/\/(.*)\/(.*)\?(.*)\?(.*)\?(.*)/', $url, $matches);
         return $matches;
     }
diff --git a/lib/User.php b/lib/User.php
index d4f8ca0..ea6ae0d 100644
--- a/lib/User.php
+++ b/lib/User.php
@@ -97,7 +97,7 @@ class User
 
     public function groups()
     {
-        //error_log("Called " . __FUNCTION__ . " on line " . __LINE__ . " of " . __FILE__);
+        //console("Called " . __FUNCTION__ . " on line " . __LINE__ . " of " . __FILE__);
         //debug_print_backtrace();
 
         if ($this->_groups || (is_array($this->_groups) && count($this->_groups) >= 1)) {
diff --git a/lib/api/kolab_api_service_group.php b/lib/api/kolab_api_service_group.php
index 6e53340..951b98c 100644
--- a/lib/api/kolab_api_service_group.php
+++ b/lib/api/kolab_api_service_group.php
@@ -179,7 +179,7 @@ class kolab_api_service_group extends kolab_api_service
         $auth = Auth::get_instance();
 
         if (empty($getdata['group'])) {
-            error_log("Empty \$getdata['group']");
+            //console("Empty \$getdata['group']");
             return FALSE;
         }
 
diff --git a/lib/kolab_api_controller.php b/lib/kolab_api_controller.php
index cd5782c..e7adbab 100644
--- a/lib/kolab_api_controller.php
+++ b/lib/kolab_api_controller.php
@@ -77,7 +77,7 @@ class kolab_api_controller
     public function add_service($service, $handler)
     {
         if ($this->services[$service]) {
-            error_log("Service $service is already registered.");
+            //console("Service $service is already registered.");
             return false;
         }
 
@@ -104,7 +104,7 @@ class kolab_api_controller
             }
         }
 
-        error_log("Unknown service $service");
+        //console("Unknown service $service");
 
         throw new Exception("Unknown service", 400);
     }
diff --git a/public_html/api/index.php b/public_html/api/index.php
index cecb2f3..b5be580 100644
--- a/public_html/api/index.php
+++ b/public_html/api/index.php
@@ -32,7 +32,7 @@ try {
     $postdata = $_SERVER['REQUEST_METHOD'] == 'POST' ? file_get_contents('php://input') : null;
     $controller->dispatch($postdata);
 } catch(Exception $e) {
-    error_log('API Error: ' . $e->getMessage());
+    //console('API Error: ' . $e->getMessage());
     $controller->output->error($e->getMessage(), $e->getCode());
 }
 


commit 2dee9bb43d2082344b1eb49dae35851406e7f472
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Fri May 25 11:56:51 2012 +0100

    Implement $form_service->validate() (#680)

diff --git a/lib/kolab_api_service.php b/lib/kolab_api_service.php
index 18e6e82..7fca044 100644
--- a/lib/kolab_api_service.php
+++ b/lib/kolab_api_service.php
@@ -208,6 +208,73 @@ abstract class kolab_api_service
     }
 
     /**
+     * Parses input (for add/edit) attributes
+     *
+     * @param string $object_name  Name of the object (user, group, etc.)
+     * @param array  $attrs        Entry attributes
+     *
+     * @return array Entry attributes
+     */
+    protected function parse_input_attributes($object_name, $attribs)
+    {
+        $type_attrs   = $this->object_type_attributes($object_name, $attribs['type_id']);
+
+        //console("parse_input_attributes", $type_attrs);
+        //console("called with \$attribs", $attribs);
+
+        $form_service = $this->controller->get_service('form_value');
+
+        // With the result, start validating the input
+        $form_service->validate(null, $attribs);
+
+        $result       = array();
+
+        if (isset($type_attrs['form_fields'])) {
+            foreach ($type_attrs['form_fields'] as $key => $value) {
+                //console("Running parse input attributes for key $key");
+
+                if (empty($attribs[$key]) && empty($value['optional'])) {
+                    //console("\$attribs['" . $key . "'] is empty, and the field is not optional");
+                    throw new Exception("Missing input value for $key", 345);
+                }
+                else {
+                    //console("Either \$attribs['" . $key . "'] is empty or the field is optional");
+                    $result[$key] = $attribs[$key];
+                }
+            }
+        }
+
+        if (isset($type_attrs['auto_form_fields'])) {
+            foreach ($type_attrs['auto_form_fields'] as $key => $value) {
+                if (empty($attribs[$key])) {
+                    if (empty($value['optional'])) {
+                        $attribs['attributes'] = array($key);
+                        $res                   = $form_service->generate(null, $attribs);
+                        $attribs[$key]         = $res[$key];
+                        $result[$key]          = $attribs[$key];
+                    }
+                } else {
+                    $result[$key] = $attribs[$key];
+                }
+            }
+        }
+
+        if (isset($type_attrs['fields'])) {
+            foreach ($type_attrs['fields'] as $key => $value) {
+                if (empty($attribs[$key])) {
+                    $result[$key] = $type_attrs['fields'][$key];
+                } else {
+                    $result[$key] = $attribs[$key];
+                }
+            }
+        }
+
+        //console("parse_input_attributes result", $result);
+
+        return $result;
+    }
+
+    /**
      * Parses result attributes
      *
      * @param string $object_name  Name of the object (user, group, etc.)
@@ -278,67 +345,4 @@ abstract class kolab_api_service
         return $attrs;
     }
 
-    /**
-     * Parses input (for add/edit) attributes
-     *
-     * @param string $object_name  Name of the object (user, group, etc.)
-     * @param array  $attrs        Entry attributes
-     *
-     * @return array Entry attributes
-     */
-    protected function parse_input_attributes($object_name, $attribs)
-    {
-        $type_attrs   = $this->object_type_attributes($object_name, $attribs['type_id']);
-
-        //console("parse_input_attributes", $type_attrs);
-        //console("called with \$attribs", $attribs);
-
-        $form_service = $this->controller->get_service('form_value');
-        $result       = array();
-
-        if (isset($type_attrs['form_fields'])) {
-            foreach ($type_attrs['form_fields'] as $key => $value) {
-                //console("Running parse input attributes for key $key");
-
-                if (empty($attribs[$key]) && empty($value['optional'])) {
-                    //console("\$attribs['" . $key . "'] is empty, and the field is not optional");
-                    throw new Exception("Missing input value for $key", 345);
-                }
-                else {
-                    //console("Either \$attribs['" . $key . "'] is empty or the field is optional");
-                    $result[$key] = $attribs[$key];
-                }
-            }
-        }
-
-        if (isset($type_attrs['auto_form_fields'])) {
-            foreach ($type_attrs['auto_form_fields'] as $key => $value) {
-                if (empty($attribs[$key])) {
-                    if (empty($value['optional'])) {
-                        $attribs['attributes'] = array($key);
-                        $res                   = $form_service->generate(null, $attribs);
-                        $attribs[$key]         = $res[$key];
-                        $result[$key]          = $attribs[$key];
-                    }
-                } else {
-                    $result[$key] = $attribs[$key];
-                }
-            }
-        }
-
-        if (isset($type_attrs['fields'])) {
-            foreach ($type_attrs['fields'] as $key => $value) {
-                if (empty($attribs[$key])) {
-                    $result[$key] = $type_attrs['fields'][$key];
-                } else {
-                    $result[$key] = $attribs[$key];
-                }
-            }
-        }
-
-        //console("parse_input_attributes result", $result);
-
-        return $result;
-    }
-
 }


commit f4e26dcfe7abc422deca8232f6fa5fff0d147390
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Fri May 25 11:04:33 2012 +0100

    Sort functions alphabetically (public first, private second, private helper functions third)
    Add validate() function, along with validate() for mail attributes

diff --git a/lib/api/kolab_api_service_form_value.php b/lib/api/kolab_api_service_form_value.php
index 8c46ba7..47a8635 100644
--- a/lib/api/kolab_api_service_form_value.php
+++ b/lib/api/kolab_api_service_form_value.php
@@ -88,35 +88,49 @@ class kolab_api_service_form_value extends kolab_api_service
     }
 
     /**
-     * Validation of field values.
+     * Generation of values for fields of type LIST.
      *
      * @param array $getdata   GET parameters
      * @param array $postdata  POST parameters. Required parameters:
+     *                         - attribute: attribute name
      *                         - type_id: Type identifier
      *                         - object_type: Object type (user, group, etc.)
      *
      * @return array Response with attribute name as a key
      */
-    public function validate($getdata, $postdata)
+    public function list_options($getdata, $postdata)
     {
-        $attribs = $this->object_type_attributes($postdata['object_type'], $postdata['type_id']);
-        $result  = array();
+        //console($postdata);
 
-        foreach ((array)$postdata as $attr_name => $attr_value) {
-            if (empty($attr_name) || $attr_name == 'type_id' || $attr_name == 'object_type') {
-                continue;
-            }
+        $attribs   = $this->object_type_attributes($postdata['object_type'], $postdata['type_id']);
+        $attr_name = $postdata['attribute'];
+        $result    = array(
+            // return search value, so client can match response to request
+            'search' => $postdata['search'],
+            'list'   => array(),
+        );
+
+        if (empty($attr_name)) {
+            return $result;
+        }
+
+
+        $method_name = 'list_options_' . strtolower($attr_name) . '_' . strtolower($postdata['object_type']);
 
-            $method_name = 'validate_' . strtolower($attr_name);
+        if (!method_exists($this, $method_name)) {
+            //console("Method $method_name doesn't exist");
+
+            $method_name = 'list_options_' . strtolower($attr_name);
 
             if (!method_exists($this, $method_name)) {
-                $result[$attr_name] = 'OK';
-                continue;
+                return $result;
             }
-
-            $result[$attr_name] = $this->{$method_name}($attr_value);
         }
 
+        //console($method_name);
+
+        $result['list'] = $this->{$method_name}($postdata, $attribs);
+
         return $result;
     }
 
@@ -158,48 +172,42 @@ class kolab_api_service_form_value extends kolab_api_service
     }
 
     /**
-     * Generation of values for fields of type LIST.
+     * Validation of field values.
      *
      * @param array $getdata   GET parameters
      * @param array $postdata  POST parameters. Required parameters:
-     *                         - attribute: attribute name
      *                         - type_id: Type identifier
      *                         - object_type: Object type (user, group, etc.)
      *
      * @return array Response with attribute name as a key
      */
-    public function list_options($getdata, $postdata)
+    public function validate($getdata, $postdata)
     {
-        //console($postdata);
+        console("Executing validate() for \$getdata, \$postdata", $getdata, $postdata);
 
-        $attribs   = $this->object_type_attributes($postdata['object_type'], $postdata['type_id']);
-        $attr_name = $postdata['attribute'];
-        $result    = array(
-            // return search value, so client can match response to request
-            'search' => $postdata['search'],
-            'list'   => array(),
-        );
-
-        if (empty($attr_name)) {
-            return $result;
-        }
+        $attribs = $this->object_type_attributes($postdata['object_type'], $postdata['type_id']);
+        $result  = array();
 
+        foreach ((array)$postdata as $attr_name => $attr_value) {
+            if (empty($attr_name) || $attr_name == 'type_id' || $attr_name == 'object_type') {
+                continue;
+            }
 
-        $method_name = 'list_options_' . strtolower($attr_name) . '_' . strtolower($postdata['object_type']);
+            $method_name = 'validate_' . strtolower($attr_name) . '_' . strtolower($postdata['object_type']);
 
-        if (!method_exists($this, $method_name)) {
-            //console("Method $method_name doesn't exist");
+            if (!method_exists($this, $method_name)) {
+                //console("Method $method_name doesn't exist");
 
-            $method_name = 'list_options_' . strtolower($attr_name);
+                $method_name = 'validate_' . strtolower($attr_name);
 
-            if (!method_exists($this, $method_name)) {
-                return $result;
+                if (!method_exists($this, $method_name)) {
+                    $result[$attr_name] = 'OK';
+                    continue;
+                }
             }
-        }
-
-        //console($method_name);
 
-        $result['list'] = $this->{$method_name}($postdata, $attribs);
+            $result[$attr_name] = $this->{$method_name}($attr_value);
+        }
 
         return $result;
     }
@@ -687,7 +695,7 @@ class kolab_api_service_form_value extends kolab_api_service
 
     private function list_options_nsrole($postdata, $attribs = array())
     {
-        error_log("Listing options for attribute 'nsrole', while the expected attribute to use is 'nsroledn'");
+        //console("Listing options for attribute 'nsrole', while the expected attribute to use is 'nsroledn'");
         return $this->list_options_nsroledn($postdata, $attribs);
     }
 
@@ -802,22 +810,83 @@ class kolab_api_service_form_value extends kolab_api_service
         return $options;
     }
 
-    private function _select_options_from_db($attribute)
+    private function validate_alias($value)
     {
+        $auth = Auth::get_instance();
+        $conf = Conf::get_instance();
+        if (!is_array($value)) {
+            $value = (array)($value);
+        }
 
-        if (empty($attribute)) {
-            return false;
+        foreach ($value as $mail_address) {
+            if (!$this->_validate_email_address($mail_address)) {
+                throw new Exception("Invalid email address '$mail_address'", 692);
+            }
+
+            // Only validate the 'alias' attribute is in any of my domain name
+            // spaces if indeed it is listed as a mail attribute.
+            if (in_array('alias', $conf->get_list('mail_attributes'))) {
+                if (!$this->_validate_email_address_in_any_of_my_domains($mail_address)) {
+                    throw new Exception("Email address '$mail_address' not in local domain", 693);
+                }
+            }
         }
 
-        $db = SQL::get_instance();
-        $result = $db->fetch_assoc($db->query("SELECT option_values FROM options WHERE attribute = ?", $attribute));
+    }
 
-        $result = json_decode($result['option_values']);
+    private function validate_mail($value)
+    {
+        $auth = Auth::get_instance();
+        $conf = Conf::get_instance();
+        if (!is_array($value)) {
+            $value = (array)($value);
+        }
 
-        if (empty($result)) {
-            return false;
+        foreach ($value as $mail_address) {
+            if (!$this->_validate_email_address($mail_address)) {
+                throw new Exception("Invalid email address '$mail_address'", 692);
+            }
+
+            // Only validate the 'mail' attribute is in any of my domain name
+            // spaces if indeed it is listed as a mail attribute.
+            if (in_array('mail', $conf->get_list('mail_attributes'))) {
+                if (!$this->_validate_email_address_in_any_of_my_domains($mail_address)) {
+                    throw new Exception("Email address '$mail_address' not in local domain", 693);
+                }
+            }
+        }
+    }
+
+    private function validate_mailalternateaddress($value)
+    {
+        $auth = Auth::get_instance();
+        $conf = Conf::get_instance();
+        if (!is_array($value)) {
+            $value = (array)($value);
+        }
+
+        foreach ($value as $mail_address) {
+            if (!$this->_validate_email_address($mail_address)) {
+                throw new Exception("Invalid email address '$mail_address'", 692);
+            }
+
+            // Only validate the 'mailalternateaddress' attribute is in any of my domain name
+            // spaces if indeed it is listed as a mail attribute.
+            if (in_array('mailalternateaddress', $conf->get_list('mail_attributes'))) {
+                if (!$this->_validate_email_address_in_any_of_my_domains($mail_address)) {
+                    throw new Exception("Email address '$mail_address' not in local domain", 693);
+                }
+            }
+        }
+    }
+
+    private function _highest_of_two($one, $two) {
+        if ($one > $two) {
+            return $one;
+        } elseif ($one == $two) {
+            return $one;
         } else {
-            return $result;
+            return $two;
         }
     }
 
@@ -916,13 +985,112 @@ class kolab_api_service_form_value extends kolab_api_service
         return $list;
     }
 
-    private function _highest_of_two($one, $two) {
-        if ($one > $two) {
-            return $one;
-        } elseif ($one == $two) {
-            return $one;
+    private function _select_options_from_db($attribute)
+    {
+
+        if (empty($attribute)) {
+            return false;
+        }
+
+        $db = SQL::get_instance();
+        $result = $db->fetch_assoc($db->query("SELECT option_values FROM options WHERE attribute = ?", $attribute));
+
+        $result = json_decode($result['option_values']);
+
+        if (empty($result)) {
+            return false;
         } else {
-            return $two;
+            return $result;
+        }
+    }
+
+    private function _validate_email_address($mail_address) {
+        $valid = true;
+
+        $at_index = strrpos($mail_address, "@");
+        if (is_bool($at_index) && !$at_index) {
+            $valid = false;
+
+        } else {
+            $domain = substr($mail_address, $at_index+1);
+            $local = substr($mail_address, 0, $at_index);
+
+            if (strlen($local) < 1 || strlen($local) > 64) {
+                // local part length exceeded
+                $valid = false;
+
+            } else if (strlen($domain) < 1 || strlen($domain) > 255) {
+                // domain part length exceeded
+                $valid = false;
+
+            } else if ($local[0] == '.' || $local[strlen($local)-1] == '.') {
+                // local part starts or ends with '.'
+                $valid = false;
+
+            } else if (preg_match('/\\.\\./', $local)) {
+                // local part has two consecutive dots
+                $valid = false;
+
+            } else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
+                // character not valid in domain part
+                $valid = false;
+
+            } else if (preg_match('/\\.\\./', $domain)) {
+                // domain part has two consecutive dots
+                $valid = false;
+
+            } else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) {
+                // character not valid in local part unless
+                // local part is quoted
+                if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) {
+                    $valid = false;
+                }
+            }
+
+            if ($valid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
+                // domain not found in DNS
+                $valid = false;
+            }
+        }
+
+        return $valid;
+    }
+
+    private function _validate_email_address_in_any_of_my_domains($mail_address) {
+        $valid = false;
+
+        $auth = Auth::get_instance();
+        $conf = Conf::get_instance();
+
+        $my_primary_domain = $_SESSION['user']->get_domain();
+        $all_domains = $auth->list_domains();
+
+        $valid_domains = array();
+
+        $dna = $conf->get('domain_name_attribute');
+
+        $at_index = strrpos($mail_address, "@");
+        if (is_bool($at_index) && !$at_index) {
+            throw new Exception("Invalid email address: No domain name space", 235);
+        } else {
+            $email_domain = substr($mail_address, $at_index+1);
         }
+
+        foreach ($all_domains as $domain_id => $domain_attrs) {
+            if (!is_array($domain_attrs[$dna])) {
+                $domain_attrs[$dna] = (array)($domain_attrs[$dna]);
+            }
+
+            if (in_array($my_primary_domain, $domain_attrs[$dna])) {
+                $valid_domains = array_merge($valid_domains, $domain_attrs[$dna]);
+            }
+        }
+
+        if (in_array($email_domain, $valid_domains)) {
+            $valid = true;
+        }
+
+        return $valid;
     }
+
 }





More information about the commits mailing list