XOOPS  2.6.0
Ads.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 namespace Xoops\Auth;
13 
15 
27 class Ads extends Ldap
28 {
36  public function __construct(Connection $dao = null)
37  {
38  parent::__construct($dao);
39  }
40 
52  public function authenticate($uname, $pwd = null)
53  {
54  $authenticated = false;
55  if (!extension_loaded('ldap')) {
57  return $authenticated;
58  }
59  $this->ds = ldap_connect($this->ldap_server, $this->ldap_port);
60  if ($this->ds) {
61  ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, $this->ldap_version);
62  ldap_set_option($this->ds, LDAP_OPT_REFERRALS, 0);
63  if ($this->ldap_use_TLS) { // We use TLS secure connection
64  if (!ldap_start_tls($this->ds)) {
66  }
67  }
68  // remove the domain name prefix from the username
69  $uname = explode("\\", $uname);
70  $uname = (sizeof($uname) > 0) ? $uname[sizeof($uname) - 1] : $uname = $uname[0];
71  // If the uid is not in the DN we proceed to a search
72  // The uid is not always in the dn
73  $userUPN = $this->getUPN($uname);
74  if (!$userUPN) {
75  return false;
76  }
77  // We bind as user to test the credentials
78  $authenticated = ldap_bind($this->ds, $userUPN, stripslashes($pwd));
79  if ($authenticated) {
80  // We load the Xoops User database
81  $dn = $this->getUserDN($uname);
82  if ($dn) {
83  return $this->loadXoopsUser($dn, $uname, $pwd);
84  } else {
85  return false;
86  }
87  } else {
88  $this->setErrors(ldap_errno($this->ds), ldap_err2str(ldap_errno($this->ds)) . '(' . $userUPN . ')');
89  }
90  } else {
92  }
93  @ldap_close($this->ds);
94  return $authenticated;
95  }
96 
107  public function getUPN($uname)
108  {
109  $userDN = $uname . '@' . $this->ldap_domain_name;
110  return $userDN;
111  }
112 }
loadXoopsUser($userdn, $uname, $pwd=null)
Definition: Ldap.php:242
const E_CANNOT_CONNECT_TO_SERVER
Definition: en_US.php:313
authenticate($uname, $pwd=null)
Definition: Ads.php:52
__construct(Connection $dao=null)
Definition: Ads.php:36
const E_EXTENSION_PHP_LDAP_NOT_LOADED
Definition: en_US.php:325
setErrors($err_no, $err_str)
getUPN($uname)
Definition: Ads.php:107
const E_TLS_CONNECTION_NOT_OPENED
Definition: en_US.php:370
$uname
Definition: checklogin.php:37
getUserDN($uname)
Definition: Ldap.php:182