function get_ldap_connection($config = null) { if ($config == null) { $config = $this->ldap_config; } $config_id = crc32(serialize($config)); if (array_key_exists($config_id, self::$ldap_connections)) { $ldap = self::$ldap_connections[$config_id]; } else { //cannot use Net_LDAP2::connect() as StatusNet uses //PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError'); //PEAR handling can be overridden on instance objects, so we do that. $ldap = new Net_LDAP2($config); $ldap->setErrorHandling(PEAR_ERROR_RETURN); $err = $ldap->bind(); if (Net_LDAP2::isError($err)) { // if we were called with a config, assume caller will handle // incorrect username/password (LDAP_INVALID_CREDENTIALS) if (isset($config) && $err->getCode() == 0x31) { throw new LdapInvalidCredentialsException('Could not connect to LDAP server: ' . $err->getMessage()); } throw new Exception('Could not connect to LDAP server: ' . $err->getMessage()); } $c = common_memcache(); if (!empty($c)) { $cacheObj = new MemcacheSchemaCache(array('c' => $c, 'cacheKey' => common_cache_key('ldap_schema:' . $config_id))); $ldap->registerSchemaCache($cacheObj); } self::$ldap_connections[$config_id] = $ldap; } return $ldap; }
function ldap_get_connection($config = null) { if ($config == null && isset($this->default_ldap)) { return $this->default_ldap; } //cannot use Net_LDAP2::connect() as StatusNet uses //PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError'); //PEAR handling can be overridden on instance objects, so we do that. $ldap = new Net_LDAP2(isset($config) ? $config : $this->ldap_get_config()); $ldap->setErrorHandling(PEAR_ERROR_RETURN); $err = $ldap->bind(); if (Net_LDAP2::isError($err)) { // if we were called with a config, assume caller will handle // incorrect username/password (LDAP_INVALID_CREDENTIALS) if (isset($config) && $err->getCode() == 0x31) { return null; } throw new Exception('Could not connect to LDAP server: ' . $err->getMessage()); return false; } if ($config == null) { $this->default_ldap = $ldap; } return $ldap; }