conf = $config; } else { return false; } return true; } /** * Connects to the server using the given username and password * * @param string $user user name * @param string $passwd password * @param boolean $allowAnonymous specifies if anonymous binds are allowed * @throws LAMException unable to connect */ public function connect($user, $passwd, $allowAnonymous=false) { // close any prior connection @$this->close(); // do not allow anonymous bind if (!$allowAnonymous && ((!$user)||($user == "")||(!$passwd))) { throw new LAMException(_("Cannot connect to specified LDAP server. Please try again.")); } // save password und username encrypted $this->encrypt_login($user, $passwd); $startTLS = $this->conf->getUseTLS(); $startTLS = ($startTLS === 'yes'); $this->server = connectToLDAP($this->conf->get_ServerURL(), $startTLS); if ($this->server != null) { // referral following $followReferrals = ($this->conf->getFollowReferrals() === 'true') ? 1 : 0; ldap_set_option($this->server,LDAP_OPT_REFERRALS, $followReferrals); $bind = @ldap_bind($this->server, $user, $passwd); if ($bind) { $return = ldap_errno($this->server); $this->is_connected = true; return; } // return error number $errorNumber = ldap_errno($this->server); $clientSource = empty($_SERVER['REMOTE_ADDR']) ? '' : $_SERVER['REMOTE_ADDR']; if (($errorNumber === False) || ($errorNumber == 81)) { // connection failed logNewMessage(LOG_ERR, 'User ' . $user . ' (' . $clientSource . ') failed to log in (LDAP error: ' . getDefaultLDAPErrorString($this->server) . ').'); throw new LAMException(_("Cannot connect to specified LDAP server. Please try again.")); } elseif ($errorNumber == 49) { // user name/password invalid. Return to login page. logNewMessage(LOG_ERR, 'User ' . $user . ' (' . $clientSource . ') failed to log in (wrong password). ' . getDefaultLDAPErrorString($this->server)); throw new LAMException(_("Wrong password/user name combination. Please try again."), getDefaultLDAPErrorString($this->server)); } else { // other errors logNewMessage(LOG_ERR, 'User ' . $user . ' (' . $clientSource . ') failed to log in (LDAP error: ' . getDefaultLDAPErrorString($this->server) . ').'); throw new LAMException(_("LDAP error, server says:"), "($errorNumber) " . getDefaultLDAPErrorString($this->server)); } } throw new LAMException(_("Cannot connect to specified LDAP server. Please try again.")); } /** Closes connection to server */ public function close() { if ($this->server != null) { @ldap_close($this->server); } } /** * Returns the LDAP connection handle * * @return object connection handle */ public function server() { if (!$this->is_connected) { try { $this->connect($this->getUserName(), $this->getPassword()); $this->is_connected = true; } catch (LAMException $e) { logNewMessage(LOG_ERR, $e->getTitle() . ' ' . $e->getMessage()); } } return $this->server; } /** Closes connection to LDAP server before serialization */ public function __sleep() { $this->close(); // define which attributes to save return array("conf", "username", "password"); } /** Reconnects to LDAP server when deserialized */ public function __wakeup() { $this->is_connected = false; // delete PDF files and images which are older than 15 min $tmpDir = dirname(__FILE__) . '/../tmp/'; $time = time(); $dir = @opendir($tmpDir); $file = @readdir($dir); while ($file) { $path = $tmpDir . $file; if ((substr($file, 0, 1) != '.') && !is_dir($path) && ($time - filemtime($path) > 900)) { @unlink($path); } $file = @readdir($dir); } @closedir($dir); // clean internal files that are older than 24 hours $tmpDir = dirname(__FILE__) . '/../tmp/internal/'; $time = time(); $dir = @opendir($tmpDir); $file = @readdir($dir); while ($file) { if (substr($file, -4) == '.tmp') { $path = $tmpDir . $file; if ($time - filemtime($path) > (3600 * 24)) { @unlink($path); } } $file = @readdir($dir); } @closedir($dir); } /** * Encrypts username and password * * @param string $username LDAP user name * @param string $password LDAP password */ public function encrypt_login($username, $password) { // encrypt username and password $this->username = base64_encode(lamEncrypt($username)); $this->password = base64_encode(lamEncrypt($password)); } /** * Returns the LDAP user name. * * @return string user name */ public function getUserName() { return lamDecrypt(base64_decode($this->username)); } /** * Returns the LDAP password. * * @return string password */ public function getPassword() { return lamDecrypt(base64_decode($this->password)); } /** Closes connection to LDAP server and deletes encrypted username/password */ public function destroy() { $this->close(); $this->username="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; $this->password="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; } } ?>