3 require_once 'config.inc.php';
4 require_once 'persistens.php';
5 require_once 'helper.php';
16 public function __construct($db) {
17 $this->valid_user
= FALSE;
18 $this->settings
= array();
25 public function login($uid, $pwd) {
26 $con = Persistens
::getInstance($this->db
);
27 if ($this->useLDAP() && $uid != 'admin') {
28 if ($this->authLDAP($uid, $pwd)) {
29 // check user exists. Internal password sha1 hash of uid
31 //echo "$uid:$pwd<br/>";
33 $settings = array_change_key_case(
34 $con->authenticate($uid, $pwd));
37 if (is_array($settings) && count($settings) > 0) {
39 $this->valid_user
= TRUE;
41 else if (is_array($settings) && count($settings) == 0) {
43 if ($con->getRole($uid)) {
44 // A user with this uid exists. We cannot create
45 $this->valid_user
= FALSE;
48 header('Location: ' . WEB_ROOT
. 'user_exist_error.php');
52 // User does not exist so create a normal user
53 $data = create_user_data($uid, $pwd, 2);
54 if ($con->newUser($data) === FALSE) {
55 $this->valid_user
= FALSE;
58 header('Location: ' . WEB_ROOT
. 'error.html');
61 $settings = array_change_key_case(
62 $con->authenticate($uid, $pwd));
63 if (count($settings) == 0) {
64 $this->valid_user
= FALSE;
67 $this->valid_user
= TRUE;
72 $this->valid_user
= FALSE;
75 header('Location: ' . WEB_ROOT
. 'error.html');
80 $this->valid_user
= FALSE;
84 $settings = array_change_key_case(
85 $con->authenticate($uid, $pwd));
86 if (count($settings) == 0) {
87 $this->valid_user
= FALSE;
90 $this->valid_user
= TRUE;
93 if ($this->valid_user
== TRUE) {
95 $this->settings
= array();
96 foreach ($settings as $row) {
97 $this->secKey
= $row['seckey'];
98 $this->pubKey
= $row['pubkey'];
99 foreach ($row as $key => $val) {
100 if ($key != 'seckey' ||
$key != 'pubkey')
101 $setting[$key] = $val;
103 array_push($this->settings
, $setting);
108 public function logout() {
109 $this->valid_user
= false;
113 public function validUser() {
114 return $this->valid_user
;
117 public function getSettings() {
118 return $this->settings
;
121 public function getSecretKey() {
122 return $this->secKey
;
125 public function getPublicKey() {
126 return $this->pubKey
;
129 private function useLDAP() {
130 $con = Persistens
::getInstance($this->db
);
131 $version = $con->getVersion();
132 $version = string2int($version['version']);
134 if ($version < 175) {
135 // no LDAP before 0.7.5
138 $this->ldap
= $con->getLdapConfig();
139 if (! is_array($this->ldap
) && $this->ldap
) {
143 header('Location: ' . WEB_ROOT
. 'error.html');
147 return ($this->ldap
&& $this->ldap
['enable'] !== 0);
150 private function authLDAP($uid, $pwd) {
154 // ldap_bind always accepts login if password is empty since and
155 // empty password will be considered a try to make an anonymous login
156 if ($this->ldap
&& $uid && $pwd && !empty($pwd)) {
157 $dns = $this->ldap
['dns'];
158 $dn = $this->ldap
['user_attr'] . "=$uid," . $this->ldap
['base_dn'];
159 $lc = ldap_connect($dns);
161 if (ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION
, 3) === false) {
162 if (ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION
, 2) === FALSE)
166 if ($this->ldap
['tls']) {
169 if (ldap_start_tls($lc) === false)
172 //echo "$ver: $dn\n";
173 if (@ldap_bind
($lc, $dn, $pwd))