From 4fdc986185ecc41af4edf22a1fed4c64826a3ff7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.bernigaud@opensides.be>
Date: Wed, 16 Mar 2016 05:41:26 +0100
Subject: [PATCH] Fixes #4610 Respect RFC4517 for postalAddress syntax

---
 .../admin/departments/class_department.inc    |  2 +-
 plugins/personal/generic/class_user.inc       | 40 ++++++++++++++++++-
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/plugins/admin/departments/class_department.inc b/plugins/admin/departments/class_department.inc
index d0096e48b..a4d781cef 100644
--- a/plugins/admin/departments/class_department.inc
+++ b/plugins/admin/departments/class_department.inc
@@ -99,7 +99,7 @@ class department extends simplePlugin
           new StringAttribute(
             _('Country'), _('Country'), 'co', FALSE
           ),
-          new TextAreaAttribute(
+          new PostalAddressAttribute(
             _('Address'), sprintf(_('A postal address for this %s'), $name),
             'postalAddress', FALSE
           ),
diff --git a/plugins/personal/generic/class_user.inc b/plugins/personal/generic/class_user.inc
index 94e19333e..da7b9f248 100644
--- a/plugins/personal/generic/class_user.inc
+++ b/plugins/personal/generic/class_user.inc
@@ -233,6 +233,42 @@ class UserPasswordAttribute extends CompositeAttribute
   }
 }
 
+class PostalAddressAttribute extends TextAreaAttribute
+{
+  function inputValue ($ldapValue)
+  {
+    $string = $ldapValue;
+    $offset = 0;
+    $lastslash = NULL;
+    while (preg_match('/[\\\$]/', $string, $m, PREG_OFFSET_CAPTURE, $offset)) {
+      $offset   = $m[0][1] + 1;
+      if ($m[0][0] == '\\') {
+        if ($lastslash == $m[0][1] - 1) {
+          $string = substr_replace($string, '\\', $m[0][1] - 1, 2);
+          $offset = $m[0][1];
+          $lastslash = NULL;
+        } else {
+          $lastslash = $m[0][1];
+        }
+      } elseif ($m[0][0] == '$') {
+        if ($lastslash == $m[0][1] - 1) {
+          $string = substr_replace($string, '$', $m[0][1] - 1, 2);
+          $offset = $m[0][1];
+        } else {
+          $string = substr_replace($string, "\n", $m[0][1], 1);
+          $offset = $m[0][1];
+        }
+      }
+    }
+    return $string;
+  }
+
+  function computeLdapValue ()
+  {
+    return preg_replace("/\n/", '$', addcslashes($this->getValue(), '$\\'));
+  }
+}
+
 class user extends simplePlugin
 {
   var $objectclasses  = array('inetOrgPerson','organizationalPerson','person');
@@ -308,7 +344,7 @@ class user extends simplePlugin
             _('State'), _('State'),
             'st', FALSE
           ),
-          new TextAreaAttribute (
+          new PostalAddressAttribute (
             _('Address'), _('Business postal address'),
             'postalAddress', FALSE
           ),
@@ -366,7 +402,7 @@ class user extends simplePlugin
             _('Display name'), _('Name this user should appear as. Used by Exchange.'),
             'displayName', FALSE
           ),
-          new TextAreaAttribute (
+          new PostalAddressAttribute (
             _('Home address'), _('Home postal address'),
             'homePostalAddress', FALSE
           ),
-- 
GitLab