From c8c4cafb1f8ae3bd50ea7106063ab51236f205fa Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Fri, 09 Oct 2015 07:44:45 -0400
Subject: [PATCH] Add possibility to define date format in write operations for ldap attributes (#1488741)

---
 CHANGELOG                            |    1 +
 program/lib/Roundcube/rcube_ldap.php |   51 ++++++++++++++++++++++++++++++++++++++++-----------
 config/defaults.inc.php              |    7 +++++--
 3 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 03af71e..e19b976 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Add possibility to define date format in write operations for ldap attributes (#1488741)
 - Display attachment size in compose (#1484774)
 - Added possibility to drag-n-drop attachments from mail preview to compose window
 - Implemented mail messages searching with predefined date interval
diff --git a/config/defaults.inc.php b/config/defaults.inc.php
index 161eee6..d53b700 100644
--- a/config/defaults.inc.php
+++ b/config/defaults.inc.php
@@ -853,8 +853,11 @@
   'required_fields' => array('cn', 'sn', 'mail'),
   'search_fields'   => array('mail', 'cn'),  // fields to search in
   // mapping of contact fields to directory attributes
-  //   for every attribute one can specify the number of values (limit) allowed.
-  //   default is 1, a wildcard * means unlimited
+  //   1. for every attribute one can specify the number of values (limit) allowed.
+  //      default is 1, a wildcard * means unlimited
+  //   2. another possible parameter is separator character for composite fields
+  //   3. it's possible to define field format for write operations, e.g. for date fields
+  //      example: 'birthday:date[YmdHis\\Z]'
   'fieldmap' => array(
     // Roundcube  => LDAP:limit
     'name'        => 'cn',
diff --git a/program/lib/Roundcube/rcube_ldap.php b/program/lib/Roundcube/rcube_ldap.php
index f1ebea0..208588c 100644
--- a/program/lib/Roundcube/rcube_ldap.php
+++ b/program/lib/Roundcube/rcube_ldap.php
@@ -38,6 +38,7 @@
 
     // private properties
     protected $ldap;
+    protected $formats  = array();
     protected $prop     = array();
     protected $fieldmap = array();
     protected $filter   = '';
@@ -45,7 +46,7 @@
     protected $result;
     protected $ldap_result;
     protected $mail_domain = '';
-    protected $debug = false;
+    protected $debug       = false;
 
     /**
      * Group objectclass (lowercase) to member attribute mapping
@@ -119,22 +120,38 @@
         if (is_array($p['fieldmap'])) {
             $p['fieldmap'] = array_filter($p['fieldmap']);
             foreach ($p['fieldmap'] as $rf => $lf)
-                $this->fieldmap[$rf] = $this->_attr_name(strtolower($lf));
+                $this->fieldmap[$rf] = $this->_attr_name($lf);
         }
         else if (!empty($p)) {
             // read deprecated *_field properties to remain backwards compatible
             foreach ($p as $prop => $value)
                 if (!empty($value) && preg_match('/^(.+)_field$/', $prop, $matches))
-                    $this->fieldmap[$matches[1]] = $this->_attr_name(strtolower($value));
+                    $this->fieldmap[$matches[1]] = $this->_attr_name($value);
         }
 
         // use fieldmap to advertise supported coltypes to the application
         foreach ($this->fieldmap as $colv => $lfv) {
             list($col, $type) = explode(':', $colv);
-            list($lf, $limit, $delim) = explode(':', $lfv);
+            $params           = explode(':', $lfv);
 
-            if ($limit == '*') $limit = null;
-            else               $limit = max(1, intval($limit));
+            $lf    = array_shift($params);
+            $limit = 1;
+
+            foreach ($params as $idx => $param) {
+                // field format specification
+                if (preg_match('/^(date)\[(.+)\]$/i', $param, $m)) {
+                    $this->formats[$lf] = array('type' => strtolower($m[1]), 'format' => $m[2]);
+                }
+                // first argument is a limit
+                else if ($idx === 0) {
+                    if ($param == '*') $limit = null;
+                    else               $limit = max(1, intval($param));
+                }
+                // second is a composite field separator
+                else if ($idx === 1 && $param) {
+                    $this->coltypes[$col]['serialized'][$type] = $param;
+                }
+            }
 
             if (!is_array($this->coltypes[$col])) {
                 $subtypes = $type ? array($type) : null;
@@ -146,10 +163,7 @@
                 $this->coltypes[$col]['limit'] += $limit;
             }
 
-            if ($delim)
-               $this->coltypes[$col]['serialized'][$type] = $delim;
-
-           $this->fieldmap[$colv] = $lf;
+            $this->fieldmap[$colv] = $lf;
         }
 
         // support for composite address
@@ -195,7 +209,7 @@
         }
 
         foreach ($this->prop['required_fields'] as $key => $val) {
-            $this->prop['required_fields'][$key] = $this->_attr_name(strtolower($val));
+            $this->prop['required_fields'][$key] = $this->_attr_name($val);
         }
 
         // Build sub_fields filter
@@ -1536,6 +1550,20 @@
             }
         }
 
+        foreach ($this->formats as $fld => $format) {
+            if (empty($ldap_data[$fld])) {
+                continue;
+            }
+
+            switch ($format['type']) {
+            case 'date':
+                if ($dt = rcube_utils::anytodatetime($ldap_data[$fld])) {
+                    $ldap_data[$fld] = $dt->format($format['format']);
+                }
+                break;
+            }
+        }
+
         return $ldap_data;
     }
 
@@ -1555,6 +1583,7 @@
 
         list($name, $limit) = explode(':', $namev, 2);
         $suffix = $limit ? ':'.$limit : '';
+        $name   = strtolower($name);
 
         return (isset($aliases[$name]) ? $aliases[$name] : $name) . $suffix;
     }

--
Gitblit v1.9.1