From 3c9919e475484e6c25536fc006c7039d1576d350 Mon Sep 17 00:00:00 2001
From: Arthur Schiwon <blizzz@owncloud.com>
Date: Wed, 25 Jul 2012 18:21:16 +0200
Subject: [PATCH] LDAP: check if php-ldap is installed. If not, give an error
 output. FIX: blank Users page when the module is not installed.

---
 apps/user_ldap/lib_ldap.php | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/apps/user_ldap/lib_ldap.php b/apps/user_ldap/lib_ldap.php
index f3b0b993ce..39992e81e0 100644
--- a/apps/user_ldap/lib_ldap.php
+++ b/apps/user_ldap/lib_ldap.php
@@ -440,6 +440,10 @@ class OC_LDAP {
 	 */
 	static public function readAttribute($dn, $attr) {
 		$cr = self::getConnectionResource();
+		if(!is_resource($cr)) {
+			//LDAP not available
+			return false;
+		}
 		$rr = ldap_read($cr, $dn, 'objectClass=*', array($attr));
 		$er = ldap_first_entry($cr, $rr);
 		//LDAP attributes are not case sensitive
@@ -495,6 +499,11 @@ class OC_LDAP {
 		if(!is_null($attr) && !is_array($attr)) {
 			$attr = array(strtolower($attr));
 		}
+		$cr = self::getConnectionResource();
+		if(!is_resource($cr)) {
+			//LDAP not available
+			return array();
+		}
 		$sr = @ldap_search(self::getConnectionResource(), $base, $filter, $attr);
 		$findings = @ldap_get_entries(self::getConnectionResource(), $sr );
 		// if we're here, probably no connection ressource is returned.
@@ -686,11 +695,22 @@ class OC_LDAP {
 	 * Connects and Binds to LDAP
 	 */
 	static private function establishConnection() {
+		static $phpLDAPinstalled = true;
+		if(!$phpLDAPinstalled) {
+			return false;
+		}
 		if(!self::$configured) {
 			OCP\Util::writeLog('ldap', 'Configuration is invalid, cannot connect', OCP\Util::INFO);
 			return false;
 		}
 		if(!self::$ldapConnectionRes) {
+			//check if php-ldap is installed
+			if(!function_exists('ldap_connect')) {
+				$phpLDAPinstalled = false;
+				OCP\Util::writeLog('user_ldap', 'function ldap_connect is not available. Make sure that the PHP ldap module is installed.', OCP\Util::ERROR);
+
+				return false;
+			}
 			self::$ldapConnectionRes = ldap_connect(self::$ldapHost, self::$ldapPort);
 			if(ldap_set_option(self::$ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) {
 					if(ldap_set_option(self::$ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) {
-- 
GitLab