PMF_Ldap::connect PHP Method

connect() public method

Connects to given LDAP server with given credentials
public connect ( string $ldapServer, integer $ldapPort, string $ldapBase, string $ldapUser = '', string $ldapPassword = '' ) : boolean
$ldapServer string
$ldapPort integer
$ldapBase string
$ldapUser string
$ldapPassword string
return boolean
    public function connect($ldapServer, $ldapPort, $ldapBase, $ldapUser = '', $ldapPassword = '')
    {
        // Sanity checks
        if ('' === $ldapServer || '' === $ldapPort || '' === $ldapBase) {
            return false;
        }
        $this->base = $ldapBase;
        $this->ds = ldap_connect($ldapServer, $ldapPort);
        if (!$this->ds) {
            $this->error = sprintf('Unable to connect to LDAP server (Error: %s)', ldap_error($this->ds));
            $this->errno = ldap_errno($this->ds);
            return false;
        }
        // optionally set Bind version
        if (isset($this->_ldapConfig['ldap_options'])) {
            foreach ($this->_ldapConfig['ldap_options'] as $key => $value) {
                if (!ldap_set_option($this->ds, constant($key), $value)) {
                    $this->errno = ldap_errno($this->ds);
                    $this->error = sprintf('Unable to set LDAP option "%s" to "%s" (Error: %s).', $key, $value, ldap_error($this->ds));
                }
            }
        }
        if (isset($this->_ldapConfig['ldap_use_anonymous_login']) && $this->_ldapConfig['ldap_use_anonymous_login']) {
            $ldapBind = ldap_bind($this->ds);
            // Anonymous LDAP login
        } else {
            $ldapBind = ldap_bind($this->ds, $ldapUser, $ldapPassword);
        }
        if (!$ldapBind) {
            $this->errno = ldap_errno($this->ds);
            $this->error = sprintf('Unable to bind to LDAP server (Error: %s).', ldap_error($this->ds));
            $this->ds = false;
            return false;
        }
        return true;
    }

Usage Example

 /**
  * Checks the password for the given user account.
  *
  * Returns true if the given password for the user account specified by
  * is correct, otherwise false.
  * Error messages are added to the array errors.
  *
  * This function is only called when local authentication has failed, so
  * we are about to create user account.
  *
  * @param string $login        Loginname
  * @param string $password     Password
  * @param array  $optionalData Optional data
  *
  * @return boolean
  */
 public function checkPassword($login, $password, array $optionalData = null)
 {
     if ('' === trim($password)) {
         $this->errors[] = PMF_User::ERROR_USER_INCORRECT_PASSWORD;
         return false;
     }
     $bindLogin = $login;
     if ($this->_ldapConfig['ldap_use_domain_prefix']) {
         if (array_key_exists('domain', $optionalData)) {
             $bindLogin = $optionalData['domain'] . '\\' . $login;
         }
     } else {
         $this->ldap = new PMF_Ldap($this->_config);
         $this->ldap->connect($this->_ldapConfig['ldap_server'], $this->_ldapConfig['ldap_port'], $this->_ldapConfig['ldap_base'], $this->_ldapConfig['ldap_user'], $this->_ldapConfig['ldap_password']);
         if ($this->ldap->error) {
             $this->errors[] = $this->ldap->error;
         }
         $bindLogin = $this->ldap->getDn($login);
     }
     // Check user in LDAP
     $this->ldap = new PMF_Ldap($this->_config);
     $this->ldap->connect($this->_ldapConfig['ldap_server'], $this->_ldapConfig['ldap_port'], $this->_ldapConfig['ldap_base'], $bindLogin, $password);
     if (!$this->ldap->bind($bindLogin, $password)) {
         $this->errors[] = $this->ldap->error;
         return false;
     } else {
         $this->add($login, $password);
         return true;
     }
 }
All Usage Examples Of PMF_Ldap::connect