3 commits - lib/api lib/kolab_recipient_policy.php public_html/.htaccess

Jeroen van Meeuwen vanmeeuwen at kolabsys.com
Thu Mar 15 12:14:44 CET 2012


 lib/api/kolab_api_service_form_value.php |  111 +++++++++-------
 lib/kolab_recipient_policy.php           |  205 +++++++++++++++++++++++++++++++
 public_html/.htaccess                    |    2 
 3 files changed, 271 insertions(+), 47 deletions(-)

New commits:
commit 563c2a12f101ff0e95a419729d3663bbe3ab2df1
Merge: a15713b 78d6291
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Thu Mar 15 12:14:39 2012 +0100

    Merge branch 'dev/recipient_policy'
    
    Conflicts:
    	lib/api/kolab_api_service_form_value.php

diff --cc lib/api/kolab_api_service_form_value.php
index d6d67f9,edec4d8..752057c
--- a/lib/api/kolab_api_service_form_value.php
+++ b/lib/api/kolab_api_service_form_value.php
@@@ -99,29 -68,13 +99,33 @@@ class kolab_api_service_form_value exte
              $attribs = array();
          }
  
 -        return $this->{$method_name}($postdata, $attribs);
 +        $result = array();
 +
 +        foreach ((array)$postdata as $attr_name => $attr_value) {
 +            if (empty($attr_name)) {
 +                continue;
 +            }
 +            if (preg_match('/^[a-z]+_type_id$/i', $attr_name)) {
 +                continue;
 +            }
 +
 +            $method_name = 'validate_' . strtolower($attr_name);
 +
 +            if (!method_exists($this, $method_name)) {
 +                $result[$attr_name] = 'OK';
 +                continue;
 +            }
 +
 +            $result[$attr_name] = $this->{$method_name}($attr_value);
 +        }
 +
 +        return $result;
      }
  
+     private function generate_alias($postdata, $attribs = array())
+     {
+         return $this->generate_secondary_mail($postdata, $attribs);
+     }
  
      private function generate_cn($postdata, $attribs = array())
      {
@@@ -133,9 -86,10 +137,10 @@@
                  }
              }
  
+             // TODO: Generate using policy from configuration
              $cn = trim($postdata['givenname'] . " " . $postdata['sn']);
  
 -            return array("cn" => $cn);
 +            return $cn;
          }
      }
  
@@@ -154,7 -109,15 +160,15 @@@
                  $displayname = $postdata['sn'] . ", " . $displayname;
              }
  
+             // TODO: Figure out what may be sent as an additional comment;
+             //
+             // Examples:
+             //
+             //  - van Meeuwen, Jeroen (Kolab Systems)
+             //  - Doe, John (Contractor)
+             //
+ 
 -            return array("displayname" => $displayname);
 +            return $displayname;
          }
      }
  
@@@ -179,22 -142,11 +193,12 @@@
                  }
              }
  
-             $uid = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['sn']);
-             $uid = strtolower($uid);
-             $uid = preg_replace('/[^a-z-_]/i', '', $uid);
- 
-             $orig_uid = $uid;
- 
-             $auth = Auth::get_instance($_SESSION['user']->get_domain());
- 
-             $x = 2;
-             while ($auth->user_find_by_attribute(array('uid' => $uid))) {
-                 $uid = $orig_uid . $x;
-                 $x++;
-             }
+             // TODO: Home directory attribute to use
+             $uid = $this->generate_uid($postdata, $attribs);
  
              // TODO: Home directory base path from configuration?
 -            return array('homedirectory' => '/home/'.$uid);
++
 +            return '/home/' . $uid;
          }
      }
  
@@@ -245,15 -170,53 +222,57 @@@
  
      private function generate_password($postdata, $attribs = array())
      {
+         // TODO: Password complexity policy.
          exec("head -c 200 /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c15", $userpassword_plain);
 -        $userpassword_plain = $userpassword_plain[0];
 -        return array('password' => $userpassword_plain);
 +        return $userpassword_plain[0];
 +    }
 +
 +    private function generate_userpassword($postdata, $attribs = array())
 +    {
 +        return $this->generate_password($postdata, $attribs);
      }
  
+     private function generate_primary_mail($postdata, $attribs = array())
+     {
+         if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['mail'])) {
+             // Use Data Please
+             foreach ($attribs['auto_form_fields']['mail']['data'] as $key) {
+                 if (!isset($postdata[$key])) {
+                     throw new Exception("Key not set: " . $key, 12356);
+                 }
+             }
+ 
+             $primary_mail = kolab_recipient_policy::primary_mail($postdata);
+ 
+             return array('mail' => $primary_mail);
+         }
+     }
+ 
+     private function generate_secondary_mail($postdata, $attribs = array())
+     {
+         $secondary_mail_address = Array();
+ 
+         if (isset($attribs['auto_form_fields'])) {
+             if (isset($attribs['auto_form_fields']['alias'])) {
+                 $secondary_mail_key = 'alias';
+             } elseif (isset($attribs['auto_form_fields']['mailalternateaddress'])) {
+                 $secondary_mail_key = 'mailalternateaddress';
+             } else {
+                 throw new Exception("No valid input for secondary mail address(es)", 478);
+             }
+ 
+             foreach ($attribs['auto_form_fields'][$secondary_mail_key]['data'] as $key) {
+                 if (!isset($postdata[$key])) {
+                     throw new Exception("Key not set: " . $key, 456789);
+                 }
+             }
+ 
+             $secondary_mail = kolab_recipient_policy::secondary_mail($postdata);
+ 
+             return Array($secondary_mail_key => $secondary_mail);
+         }
+     }
+ 
      private function generate_uid($postdata, $attribs = array())
      {
          if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['uid'])) {
@@@ -288,8 -252,7 +308,7 @@@
              $auth = Auth::get_instance($_SESSION['user']->get_domain());
  
              // TODO: Actually poll $auth for users with a uidNumber set, and take the next one.
- 
 -            return array('uidnumber' => 500);
 +            return 500;
          }
      }
  


commit 78d62915289072290ac0a3c1644ceb6127694699
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Thu Mar 15 12:12:06 2012 +0100

    Add kolab_recipient_policy capabilities

diff --git a/lib/api/kolab_api_service_form_value.php b/lib/api/kolab_api_service_form_value.php
index c778e5e..edec4d8 100644
--- a/lib/api/kolab_api_service_form_value.php
+++ b/lib/api/kolab_api_service_form_value.php
@@ -71,6 +71,10 @@ class kolab_api_service_form_value extends kolab_api_service
         return $this->{$method_name}($postdata, $attribs);
     }
 
+    private function generate_alias($postdata, $attribs = array())
+    {
+        return $this->generate_secondary_mail($postdata, $attribs);
+    }
 
     private function generate_cn($postdata, $attribs = array())
     {
@@ -82,6 +86,7 @@ class kolab_api_service_form_value extends kolab_api_service
                 }
             }
 
+            // TODO: Generate using policy from configuration
             $cn = trim($postdata['givenname'] . " " . $postdata['sn']);
 
             return array("cn" => $cn);
@@ -98,11 +103,20 @@ class kolab_api_service_form_value extends kolab_api_service
                 }
             }
 
+            // TODO: Generate using policy from configuration
             $displayname = $postdata['givenname'];
             if ($postdata['sn']) {
                 $displayname = $postdata['sn'] . ", " . $displayname;
             }
 
+            // TODO: Figure out what may be sent as an additional comment;
+            //
+            // Examples:
+            //
+            //  - van Meeuwen, Jeroen (Kolab Systems)
+            //  - Doe, John (Contractor)
+            //
+
             return array("displayname" => $displayname);
         }
     }
@@ -128,61 +142,22 @@ class kolab_api_service_form_value extends kolab_api_service
                 }
             }
 
-            $uid = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['sn']);
-            $uid = strtolower($uid);
-            $uid = preg_replace('/[^a-z-_]/i', '', $uid);
-
-            $orig_uid = $uid;
-
-            $auth = Auth::get_instance($_SESSION['user']->get_domain());
-
-            $x = 2;
-            while ($auth->user_find_by_attribute(array('uid' => $uid))) {
-                $uid = $orig_uid . $x;
-                $x++;
-            }
+            // TODO: Home directory attribute to use
+            $uid = $this->generate_uid($postdata, $attribs);
 
             // TODO: Home directory base path from configuration?
-
             return array('homedirectory' => '/home/'.$uid);
         }
     }
 
     private function generate_mail($postdata, $attribs = array())
     {
-        if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['mail'])) {
-            // Use Data Please
-            foreach ($attribs['auto_form_fields']['mail']['data'] as $key) {
-                if (!isset($postdata[$key])) {
-                    throw new Exception("Key not set: " . $key, 12356);
-                }
-            }
-
-            $givenname = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['givenname']);
-            $sn        = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['sn']);
-
-            $givenname = strtolower($givenname);
-            $sn        = strtolower($sn);
-
-            $givenname = preg_replace('/[^a-z-_]/i', '', $givenname);
-            $sn        = preg_replace('/[^a-z-_]/i', '', $sn);
-
-            $local = trim($givenname . '.' . $sn, '.');
-            $mail  = $local . '@' . $_SESSION['user']->get_domain();
-
-            $orig_mail = $mail;
-
-            $auth = Auth::get_instance($_SESSION['user']->get_domain());
-
-            $x = 2;
-            while ($auth->user_find_by_attribute(array('mail' => $mail))) {
-                list($mail_local, $mail_domain) = explode('@', $orig_mail);
-                $mail = $mail_local . $x . '@' . $mail_domain;
-                $x++;
-            }
+        return $this->generate_primary_mail($postdata, $attribs);
+    }
 
-            return array('mail' => $mail);
-        }
+    private function generate_mailalternateaddress($postdata, $attribs = array())
+    {
+        return $this->generate_secondary_mail($postdata, $attribs);
     }
 
     private function generate_mailhost($postdata, $attribs = array())
@@ -195,11 +170,53 @@ class kolab_api_service_form_value extends kolab_api_service
 
     private function generate_password($postdata, $attribs = array())
     {
+        // TODO: Password complexity policy.
         exec("head -c 200 /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c15", $userpassword_plain);
         $userpassword_plain = $userpassword_plain[0];
         return array('password' => $userpassword_plain);
     }
 
+    private function generate_primary_mail($postdata, $attribs = array())
+    {
+        if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['mail'])) {
+            // Use Data Please
+            foreach ($attribs['auto_form_fields']['mail']['data'] as $key) {
+                if (!isset($postdata[$key])) {
+                    throw new Exception("Key not set: " . $key, 12356);
+                }
+            }
+
+            $primary_mail = kolab_recipient_policy::primary_mail($postdata);
+
+            return array('mail' => $primary_mail);
+        }
+    }
+
+    private function generate_secondary_mail($postdata, $attribs = array())
+    {
+        $secondary_mail_address = Array();
+
+        if (isset($attribs['auto_form_fields'])) {
+            if (isset($attribs['auto_form_fields']['alias'])) {
+                $secondary_mail_key = 'alias';
+            } elseif (isset($attribs['auto_form_fields']['mailalternateaddress'])) {
+                $secondary_mail_key = 'mailalternateaddress';
+            } else {
+                throw new Exception("No valid input for secondary mail address(es)", 478);
+            }
+
+            foreach ($attribs['auto_form_fields'][$secondary_mail_key]['data'] as $key) {
+                if (!isset($postdata[$key])) {
+                    throw new Exception("Key not set: " . $key, 456789);
+                }
+            }
+
+            $secondary_mail = kolab_recipient_policy::secondary_mail($postdata);
+
+            return Array($secondary_mail_key => $secondary_mail);
+        }
+    }
+
     private function generate_uid($postdata, $attribs = array())
     {
         if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['uid'])) {
@@ -210,6 +227,7 @@ class kolab_api_service_form_value extends kolab_api_service
                 }
             }
 
+            // TODO: Use preferredlanguage
             $uid = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['sn']);
             $uid = strtolower($uid);
             $uid = preg_replace('/[^a-z-_]/i', '', $uid);
@@ -234,7 +252,6 @@ class kolab_api_service_form_value extends kolab_api_service
             $auth = Auth::get_instance($_SESSION['user']->get_domain());
 
             // TODO: Actually poll $auth for users with a uidNumber set, and take the next one.
-
             return array('uidnumber' => 500);
         }
     }
diff --git a/lib/kolab_recipient_policy.php b/lib/kolab_recipient_policy.php
new file mode 100644
index 0000000..2bd647c
--- /dev/null
+++ b/lib/kolab_recipient_policy.php
@@ -0,0 +1,205 @@
+<?php
+/*
+ +--------------------------------------------------------------------------+
+ | This file is part of the Kolab Web Admin Panel                           |
+ |                                                                          |
+ | Copyright (C) 2011-2012, Kolab Systems AG                                |
+ |                                                                          |
+ | This program is free software: you can redistribute it and/or modify     |
+ | it under the terms of the GNU Affero General Public License as published |
+ | by the Free Software Foundation, either version 3 of the License, or     |
+ | (at your option) any later version.                                      |
+ |                                                                          |
+ | This program is distributed in the hope that it will be useful,          |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             |
+ | GNU Affero General Public License for more details.                      |
+ |                                                                          |
+ | You should have received a copy of the GNU Affero General Public License |
+ | along with this program. If not, see <http://www.gnu.org/licenses/>      |
+ +--------------------------------------------------------------------------+
+ | Author: Aleksander Machniak <machniak at kolabsys.com>                      |
+ | Author: Jeroen van Meeuwen <vanmeeuwen at kolabsys.com>                     |
+ +--------------------------------------------------------------------------+
+*/
+
+class kolab_recipient_policy {
+
+    static function format() {
+        $_args = func_get_args();
+
+        $args = Array();
+
+        for ($i = 0; $i < func_num_args(); $i++) {
+            #$args[$i] = preg_replace('/\./', '\.', $_args[$i]);
+            $args[$i] = $_args[$i];
+        }
+
+        return $args;
+    }
+
+    static function normalize_userdata($userdata)
+    {
+        $keymap = Array(
+                'sn' => 'surname',
+            );
+
+        foreach ($userdata as $key => $value) {
+            if (isset($keymap[$key])) {
+                $_key = $keymap[$key];
+            } else {
+                $_key = $key;
+            }
+
+            $userdata[$_key] = str_replace(' ', '', $userdata[$key]);
+        }
+
+        return $userdata;
+    }
+
+    static function primary_mail($userdata)
+    {
+        $userdata = self::normalize_userdata($userdata);
+
+        $conf = Conf::get_instance();
+
+        if (isset($userdata['domain'])) {
+            $primary_mail = $conf->get_raw($userdata['domain'], 'primary_mail');
+        } else {
+            $primary_mail = $conf->get_raw($_SESSION['user']->get_domain(), 'primary_mail');
+            // Also append the domain to the userdata
+            $userdata['domain'] = $_SESSION['user']->get_domain();
+        }
+
+        preg_match_all('/%\((\w+)\)s/', $primary_mail, $substrings);
+
+        // Update userdata array
+        for ($x = 0; $x < count($substrings[0]); $x++) {
+            if (array_key_exists($substrings[1][$x], $userdata)) {
+                if (!empty($substrings[2][$x])) {
+                    if (!empty($substrings[3][$x])) {
+                        $primary_mail = preg_replace(
+                                '/%\(' . $substrings[1][$x]. '\)s/',
+                                substr(
+                                        $userdata[$substrings[1][$x]],
+                                        $substrings[2][$x],
+                                        $substrings[3][$x]
+                                    ),
+                                $primary_mail
+                            );
+                    } else {
+                        $primary_mail = preg_replace(
+                                '/%\(' . $substrings[1][$x]. '\)s/',
+                                substr(
+                                        $userdata[$substrings[1][$x]],
+                                        $substrings[2][$x]
+                                    ),
+                                $primary_mail
+                            );
+                    }
+                } elseif (!empty($substrings[3][$x])) {
+                    $primary_mail = preg_replace(
+                            '/%\(' . $substrings[1][$x]. '\)s/',
+                            substr(
+                                    $userdata[$substrings[1][$x]],
+                                    0,
+                                    $substrings[3][$x]
+                                ),
+                            $primary_mail
+                        );
+                } else {
+                    $primary_mail = preg_replace(
+                            '/%\(' . $substrings[1][$x]. '\)s/',
+                            $userdata[$substrings[1][$x]],
+                            $primary_mail
+                        );
+                }
+            } else {
+                console("Key " . $substrings[1][$x] . " does not exist in \$userdata");
+            }
+        }
+
+        return $primary_mail;
+
+    }
+
+    static function secondary_mail($userdata)
+    {
+        $secondary_mail_addresses = Array();
+
+        $functions = Array(
+                '\'%\((\w+)\)s\'\.capitalize\(\)' => 'strtoupper(substr("%(${1})s", 0, 1)) . strtolower(substr("%(${1})s", 1))',
+                '\'%\((\w+)\)s\'\.lower\(\)'      => 'strtolower("%(${1})s")',
+                '\'%\((\w+)\)s\'\.upper\(\)'      => 'strtoupper("%(${1})s")',
+            );
+
+        $userdata = self::normalize_userdata($userdata);
+
+        $conf = Conf::get_instance();
+
+        if (isset($userdata['domain'])) {
+            $secondary_mail = $conf->get_raw($userdata['domain'], 'secondary_mail');
+        } else {
+            $secondary_mail = $conf->get_raw($_SESSION['user']->get_domain(), 'secondary_mail');
+            $userdata['domain'] = $_SESSION['user']->get_domain();
+        }
+
+        $secondary_mail = preg_replace('/^{\d:\s*/','',$secondary_mail);
+        $secondary_mail = preg_replace('/\s*}$/','',$secondary_mail);
+        $secondary_mail = preg_replace('/,\d+:\s*/',',',$secondary_mail);
+        $secondary_mail = "[" . $secondary_mail . "]";
+        $secondary_mail = json_decode($secondary_mail, true);
+
+        $orig_userdata = $userdata;
+
+        foreach ($secondary_mail as $policy_rule) {
+            foreach ($policy_rule as $format => $rule) {
+                $userdata = $orig_userdata;
+
+                $format = preg_replace('/(\{\d+\})/', '%s', $format);
+
+                preg_match_all('/\'%\((\w+)\)s\'\[(\d+):(\d+)\]/', $rule, $substrings);
+
+                // Update userdata array
+                for ($x = 0; $x < count($substrings[0]); $x++) {
+                    if (array_key_exists($substrings[1][$x], $userdata)) {
+                        $userdata[$substrings[1][$x]] = substr($userdata[$substrings[1][$x]], $substrings[2][$x], $substrings[3][$x]);
+                    } else {
+                        console("Key " . $substrings[1][$x] . " does not exist in \$userdata");
+                    }
+
+                    $rule = preg_replace(
+                            '/\'%\(' . $substrings[1][$x] . '\)s\'\[' . $substrings[2][$x] . ':' . $substrings[3][$x] . '\]/',
+                            '\'%(' . $substrings[1][$x] . ')s\'',
+                            $rule
+                        );
+
+                }
+
+                foreach ($functions as $match => $replace) {
+                    if (preg_match('/' . $match . '/', $rule, $strings)) {
+
+                        if (array_key_exists($strings[1], $userdata)) {
+                            $rule = preg_replace('/' . $match . '/', $replace, $rule);
+                        }
+
+                    }
+                }
+
+                $expanded = $conf->expand($rule, $userdata);
+
+                eval("\$result = self::" . $expanded . ";");
+
+                eval("\$result = sprintf('" . $format . "', '" . implode("', '", array_values($result)) . "');");
+
+                $secondary_mail_addresses[] = $result;
+
+            }
+
+        }
+
+        return $secondary_mail_addresses;
+
+    }
+}
+?>


commit 8cc0247145953855e4cf071203409f9f9ef9fe2e
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Tue Mar 13 12:05:27 2012 +0100

    Correct .htaccess for this instance

diff --git a/public_html/.htaccess b/public_html/.htaccess
index e646d2d..b228331 100644
--- a/public_html/.htaccess
+++ b/public_html/.htaccess
@@ -1,6 +1,6 @@
 <IfModule mod_rewrite.c>
     RewriteEngine on
-    RewriteBase /rc/admin/public_html
+    RewriteBase /~vanmeeuwen/kolab-wap/public_html
 
     # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
     RewriteCond %{REQUEST_FILENAME} !-f





More information about the commits mailing list