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