From 40a8452fa3accd5a3e1dc550f55358315cc84bb2 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Wed, 14 Nov 2007 13:07:11 +0000 Subject: [PATCH] removed duplicate schema parsing --- lam/lib/ldap.inc | 134 +----------------------------- lam/lib/modules/inetOrgPerson.inc | 47 +++++++++-- 2 files changed, 39 insertions(+), 142 deletions(-) diff --git a/lam/lib/ldap.inc b/lam/lib/ldap.inc index a2226a75..f077d4e1 100644 --- a/lam/lib/ldap.inc +++ b/lam/lib/ldap.inc @@ -64,15 +64,6 @@ class Ldap{ /** LDAP password used for bind */ private $password; - /** Array with all objectClass strings from the LDAP server */ - private $objectClasses; - /** Array with all attribute strings from the LDAP server */ - private $attributes; - - // Capabilities of the LDAP server - /** Host attribute in inetOrgPerson */ - public $supports_unix_hosts = false; - /** Random number (changes on every page request) */ private $rand; @@ -121,16 +112,6 @@ class Ldap{ $bind = @ldap_bind($this->server, $user, $passwd); if ($bind) { $return = ldap_errno($this->server); - // read objectClasses from server and update capabilities if needed - if (! $this->objectClasses) { - $this->updateClasses(); - $this->updateCapabilities(); - // update schema - get_schema_objectclasses(); - get_schema_attributes(); - get_schema_matching_rules(); - get_schema_syntaxes(); - } // return success number return $return; } @@ -177,118 +158,6 @@ class Ldap{ return $ret; } - /** Reads the array of objectClasses from the LDAP server */ - function updateClasses() { - // read from default cn - $sr = @ldap_read($this->server, 'cn=subschema', '(objectClass=*)', array('objectclasses')); - // if default was not correct check different cn - if (!$sr) $sr = @ldap_read($this->server, 'cn=schema', '(objectClass=*)', array('objectclasses')); - if ($sr) { - // get search result and save it - $info = @ldap_get_entries($this->server,$sr); - if ($info) { - $this->objectClasses = $info[0]['objectclasses']; - if (is_array($this->objectClasses)) { - array_shift($this->objectClasses); - } - else { - $this->objectClasses = array(); - } - } - } - // if search failed save empty result - else $this->objectClasses = array(); - - // read from default cn - $sr = @ldap_read($this->server, 'cn=subschema', '(objectClass=*)', array('attributetypes')); - // if default was not correct check different cn - if (!$sr) $sr = @ldap_read($this->server, 'cn=schema', '(objectClass=*)', array('attributetypes')); - if ($sr) { - // get search result and save it - $info = @ldap_get_entries($this->server,$sr); - if ($info) { - $attributes = $info[0]['attributetypes']; - if (is_array($attributes)) { - array_shift($attributes); - } - else { - $attributes = array(); - } - } - } - else { - $attributes = array(); - } - // build Attribute list - for ($i=0; $iattributes[$name] = $values; - $start = $end + 3; - } - } - else { - $end = $start; - while ($attributes[$i][$end]!='\'') $end++; - $name = substr($attributes[$i], $start, $end-$start); - $this->attributes[$name] = $values; - } - } - } - - /** Updates the capabilities values (var $supports_*) */ - function updateCapabilities() { - for ($i = 0; $i < sizeof($this->objectClasses); $i++) { - $line = $this->objectClasses[$i]; - // search keywords - if (strpos($line, "NAME 'inetOrgPerson'") && strpos($line, " host ")) $this->supports_unix_hosts = true; - } - } - /** * Returns the LDAP connection handle * @@ -307,8 +176,7 @@ class Ldap{ function __sleep() { $this->close(); // define which attributes to save - return array("conf", "username", "password", "objectClasses", "attributes", - "supports_unix_hosts", "rand"); + return array("conf", "username", "password", "rand"); } /** Reconnects to LDAP server when deserialized */ diff --git a/lam/lib/modules/inetOrgPerson.inc b/lam/lib/modules/inetOrgPerson.inc index c262ffcd..86db23cb 100644 --- a/lam/lib/modules/inetOrgPerson.inc +++ b/lam/lib/modules/inetOrgPerson.inc @@ -36,6 +36,33 @@ $Id$ * @package modules */ class inetOrgPerson extends baseModule { + + private static $unix_hosts_supported = 'unknown'; + + /** + * Returns if the host attribute is supported by the schema. + * + * @return boolean true if supported + */ + private function supportUnixHosts() { + if (inetOrgPerson::$unix_hosts_supported === 'unknown') { + inetOrgPerson::$unix_hosts_supported = 'no'; + get_schema_objectclasses(); + $classes = get_cached_schema('objectclasses'); + if (isset($classes['inetorgperson'])) { + $mayAttrs = $classes['inetorgperson']->getMayAttrs(); + if (is_array($mayAttrs)) { + for ($i = 0; $i < sizeof($mayAttrs); $i++) { + if ($mayAttrs[$i]->getName() === 'host') { + inetOrgPerson::$unix_hosts_supported = 'yes'; + break; + } + } + } + } + } + return (inetOrgPerson::$unix_hosts_supported === 'yes'); + } /** * This function fills the message array. @@ -271,13 +298,15 @@ class inetOrgPerson extends baseModule { } } // Unix workstations for upload - if ($_SESSION['loggedIn'] && $_SESSION['ldap']->supports_unix_hosts) { - $return['upload_columns'][] = array( - 'name' => 'inetOrgPerson_hosts', - 'description' => _('Unix workstations'), - 'help' => 'workstations', - 'example' => _('pc01,pc02') - ); + if ($_SESSION['loggedIn']) { + if ($this->supportUnixHosts()) { + $return['upload_columns'][] = array( + 'name' => 'inetOrgPerson_hosts', + 'description' => _('Unix workstations'), + 'help' => 'workstations', + 'example' => _('pc01,pc02') + ); + } } // available PDF fields $return['PDF_fields'] = array( @@ -550,7 +579,7 @@ class inetOrgPerson extends baseModule { } // handle host-attribute in on epice because it's not set by default - if ($_SESSION['ldap']->supports_unix_hosts) { + if ($this->supportUnixHosts()) { $host = $_POST['host']; if (!get_preg($host,'unixhost')) { $errors[] = $this->messages['host'][0]; @@ -760,7 +789,7 @@ class inetOrgPerson extends baseModule { array('kind' => 'input', 'name' => 'businessCategory', 'type' => 'text', 'size' => '30', 'maxlength' => '255', 'value' => $businessCategory), array('kind' => 'help', 'value' => 'businessCategory')); - if ($_SESSION['ldap']->supports_unix_hosts) { + if ($this->supportUnixHosts()) { $hostvalue = ""; if (is_array($this->attributes['host'])) { $hostvalue .= implode(",", $this->attributes['host']);