| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 1 | <?php | 
 | 2 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 3 | function debug($msg) | 
 | 4 | { | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 5 |         include 'config.php'; | 
 | 6 |         if ($DEBUG) | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 7 |                 echo $msg . "\n"; | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 8 | } | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 9 | function generateSalt($length = 10) | 
 | 10 | { | 
 | 11 |         $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 12 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 13 |         $string = ""; | 
 | 14 |         for ($i = 0; $i < $length; $i++) { | 
 | 15 |                 $string .= substr($chars, rand(0, strlen($chars) - 1), 1); | 
 | 16 |         } | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 17 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 18 |         return $string; | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 19 | } | 
 | 20 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 21 | class User | 
 | 22 | { | 
| Marc Kupietz | a19f307 | 2023-02-25 14:16:40 +0100 | [diff] [blame] | 23 |         function __construct(string $user_name, string $first_name, string $last_name, string $email, string $password, string $organization) | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 24 |         { | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 25 |                 $this->user_name = $user_name; | 
 | 26 |                 $this->name = $first_name; | 
 | 27 |                 $this->first_name = $first_name; | 
 | 28 |                 $this->last_name = $last_name; | 
 | 29 |                 $this->email = $email; | 
| Marc Kupietz | a19f307 | 2023-02-25 14:16:40 +0100 | [diff] [blame] | 30 |                 $this->organization = $organization; | 
| Marc Kupietz | 92e4866 | 2023-02-23 10:04:21 +0100 | [diff] [blame] | 31 |                 if ($ENCRYPT_PASSWORDS) { | 
 | 32 |                         $this->user_hash = "{crypt}" . crypt($password, '$6$' . generateSalt(10) . '$'); | 
 | 33 |                         # $this->user_hash = "{SHA}" .  base64_encode(sha1($password, true)); | 
 | 34 |                 } else { | 
 | 35 |                         $this->user_hash = "{CLEAR}" . $password; | 
 | 36 |                 } | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 37 |                 $this->password = $this->user_hash; | 
| Marc Kupietz | 92e4866 | 2023-02-23 10:04:21 +0100 | [diff] [blame] | 38 |  | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 39 |         } | 
 | 40 | } | 
 | 41 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 42 | function ldap_search_query($query, $filter = "cn") | 
 | 43 | { | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 44 |         include 'config.php'; | 
 | 45 |         $ldap_host = $HOST; | 
 | 46 |         $ldap_port = $PORT; | 
 | 47 |         $ldaptree = explode("{},", $BASE_DN)[1]; | 
 | 48 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 49 |         $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1)); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 50 |         $ldap_pass = $PASSWORD; | 
 | 51 |  | 
 | 52 |         //First: Connect to  LDAP Server | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 53 |         $connect = ldap_connect($ldap_host, $ldap_port) | 
 | 54 |                 or debug(">>Could not connect to LDAP server to add user<<"); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 55 |         ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); | 
 | 56 |         ldap_set_option($connect, LDAP_OPT_REFERRALS, 0); | 
 | 57 |  | 
 | 58 |         //Login to LDAP | 
 | 59 |         ldap_bind($connect, $ldap_user, $ldap_pass) | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 60 |                 or debug(">>Could not bind to $ldap_host to add user<<"); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 61 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 62 |  | 
 | 63 |         $result = ldap_search($connect, $ldaptree, "(" . $filter . "=" . $query . ")") or die("Error in search query: " . ldap_error($connect)); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 64 |         $data = ldap_get_entries($connect, $result); | 
 | 65 |         return $data; | 
 | 66 | } | 
 | 67 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 68 | function ldap_add_user($user) | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 69 | { | 
 | 70 |         include 'config.php'; | 
 | 71 |         $ldap_host = $HOST; | 
 | 72 |         $ldap_port = $PORT; | 
 | 73 |         $base_dn = str_replace('{}', $user->user_name, $BASE_DN); | 
 | 74 |         $ldaptree = explode("{},", $BASE_DN)[1]; | 
 | 75 |  | 
 | 76 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 77 |         $info["givenName"] = $user->first_name; | 
 | 78 |         $info["sn"] = $user->last_name; | 
 | 79 |         $info["uid"] = $user->user_name; | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 80 |         #$info["homeDirectory"]="/home/"; | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 81 |         $info["mail"] = $user->email; | 
| Marc Kupietz | a19f307 | 2023-02-25 14:16:40 +0100 | [diff] [blame] | 82 |         $info["o"] = $user->organization; | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 83 |         $info["displayName"] = $user->first_name . " " . $user->last_name; | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 84 |         #$info["departmentNumber"]=$user->id; | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 85 |         $info["cn"] = $user->user_name; | 
 | 86 |         $info["userPassword"] = $user->user_hash; | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 87 |         $info["objectclass"][0] = "top"; | 
 | 88 |         $info["objectclass"][1] = "person"; | 
 | 89 |         $info["objectclass"][2] = "inetOrgPerson"; | 
 | 90 |         $info["objectclass"][3] = "organizationalPerson"; | 
 | 91 |  | 
 | 92 |  | 
 | 93 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 94 |         $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1)); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 95 |         $ldap_pass = $PASSWORD; | 
 | 96 |  | 
 | 97 |         //First: Connect to  LDAP Server | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 98 |         $connect = ldap_connect($ldap_host, $ldap_port) | 
 | 99 |                 or debug(">>Could not connect to LDAP server to add user<<"); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 100 |         ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); | 
 | 101 |         ldap_set_option($connect, LDAP_OPT_REFERRALS, 0); | 
 | 102 |  | 
 | 103 |         //Login to LDAP | 
 | 104 |         ldap_bind($connect, $ldap_user, $ldap_pass) | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 105 |                 or debug(">>Could not bind to $ldap_host to add user<<"); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 106 |  | 
 | 107 |         // Adding new user | 
 | 108 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 109 |         $add = ldap_add($connect, $base_dn, $info) | 
 | 110 |                 or debug(">>Not able to load user <<"); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 111 |  | 
 | 112 |         // Close connection | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 113 |         ldap_close($connect); | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 114 |  | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 115 |         // Return value of operation | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 116 |  | 
 | 117 |         return $add; | 
 | 118 | } | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 119 | function ldap_user_count($user) | 
 | 120 | { | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 121 |         return ldap_search_query($user)["count"]; | 
 | 122 | } | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 123 | function ldap_mail_count($email) | 
 | 124 | { | 
| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 125 |         return ldap_search_query($email, "mail")["count"]; | 
 | 126 | } | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 127 |  | 
 | 128 | function change_password($email, $new_password) | 
 | 129 | { | 
 | 130 |         include 'config.php'; | 
 | 131 |         $ldap_host = $HOST; | 
 | 132 |         $ldap_port = $PORT; | 
 | 133 |         $ldaptree = explode("{},", $BASE_DN)[1]; | 
 | 134 |  | 
 | 135 |         $ldap_user = "cn=" . $USER . "," . join(",", array_slice(explode(",", $ldaptree), 1)); | 
 | 136 |         $ldap_pass = $PASSWORD; | 
 | 137 |  | 
 | 138 |         //First: Connect to  LDAP Server | 
 | 139 |         $connect = ldap_connect($ldap_host, $ldap_port) | 
 | 140 |                 or debug(">>Could not connect to LDAP server to add user<<"); | 
 | 141 |         ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); | 
 | 142 |         ldap_set_option($connect, LDAP_OPT_REFERRALS, 0); | 
 | 143 |  | 
 | 144 |         //Login to LDAP | 
 | 145 |         ldap_bind($connect, $ldap_user, $ldap_pass) | 
 | 146 |                 or debug(">>Could not bind to $ldap_host to add user<<"); | 
 | 147 |  | 
 | 148 |  | 
 | 149 |         $result = ldap_search($connect, $ldaptree, "(mail=" . $email . ")") or die("Error in search query: " . ldap_error($connect)); | 
 | 150 |         $data = ldap_get_entries($connect, $result); | 
 | 151 |         if (!$data['count'] || !isset($data[0]["dn"]) || empty($data[0]["dn"])) { | 
 | 152 |                 return false; | 
 | 153 |         } | 
 | 154 |         $dn = $data[0]["dn"]; | 
 | 155 |  | 
| Marc Kupietz | 92e4866 | 2023-02-23 10:04:21 +0100 | [diff] [blame] | 156 |         if ($ENCRYPT_PASSWORDS) { | 
 | 157 |                 $newEntry = ['userPassword' => "{crypt}" . crypt($new_password, '$6$' . generateSalt(10) . '$')]; | 
 | 158 |                 # $newEntry = ['userPassword' => "{SHA}" .  base64_encode(sha1($new_password, true))]; | 
 | 159 |         } else { | 
 | 160 |                 $newEntry = ['userPassword' => "{CLEAR}" .  $new_password]; | 
 | 161 |         } | 
| matheusfillipe | f43dd96 | 2021-05-13 23:27:01 -0300 | [diff] [blame] | 162 |         if (ldap_mod_replace($connect, $dn, $newEntry)) | 
 | 163 |                 return true; | 
 | 164 |         else | 
 | 165 |                 return false; | 
 | 166 | } |