From e4a643743a5a35a3cd0cd0aae9e99f0ccb9aa44a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaime=20P=C3=A9rez=20Crespo?= <jaime.perez@uninett.no>
Date: Thu, 20 Feb 2014 19:10:13 +0000
Subject: [PATCH] Add support for binary attributes in AttributeAddFromLDAP
 (Issue #620). Also, remove hardcoded attribute names, and detect binary
 attributes 'automatically'.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@3368 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Auth/LDAP.php | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/lib/SimpleSAML/Auth/LDAP.php b/lib/SimpleSAML/Auth/LDAP.php
index 23f6c051e..43efee196 100644
--- a/lib/SimpleSAML/Auth/LDAP.php
+++ b/lib/SimpleSAML/Auth/LDAP.php
@@ -361,6 +361,25 @@ class SimpleSAML_Auth_LDAP {
 			);
 		}
 
+        // parse each entry and process its attributes
+        for ($i = 0; $i < $results['count']; $i++) {
+            $entry = $results[$i];
+
+            // iterate over the attributes of the entry
+            for ($j = 0; $j < $entry['count']; $j++) {
+                $name = $entry[$j];
+                $attribute = $entry[$name];
+
+                // decide whether to base64 encode or not
+                for ($k = 0; $k < $attribute['count']; $k++) {
+                    // base64 encode binary attributes
+                    if (!ctype_print($attribute[$k])) {
+                        $results[$i][$name][$k] = base64_encode($attribute[$k]);
+                    }
+                }
+            }
+        }
+
 		// Remove the count and return
 		unset($results['count']);
 		return $results;
@@ -522,12 +541,12 @@ class SimpleSAML_Auth_LDAP {
 					continue;
 				}
 
-				// Base64 encode jpegPhoto.
-				if (strtolower($name) === 'jpegphoto') {
+                // base64 encode binary attributes
+                if (!ctype_print($value)) {
 					$values[] = base64_encode($value);
-				} else
+				} else {
 					$values[] = $value;
-
+                }
 			}
 
 			// Adding.
-- 
GitLab