| matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame^] | 1 | <?php | 
|  | 2 |  | 
|  | 3 | function debug($msg) { | 
|  | 4 | include 'config.php'; | 
|  | 5 | if ($DEBUG) | 
|  | 6 | echo $msg."\n"; | 
|  | 7 | } | 
|  | 8 | function generateSalt($length=10) { | 
|  | 9 | $chars="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | 
|  | 10 |  | 
|  | 11 | $string=""; | 
|  | 12 | for($i=0;$i<$length;$i++) { | 
|  | 13 | $string.=substr($chars,rand(0,strlen($chars)-1),1); | 
|  | 14 | } | 
|  | 15 |  | 
|  | 16 | return $string; | 
|  | 17 | } | 
|  | 18 |  | 
|  | 19 | class User { | 
|  | 20 | function __construct(string $user_name, string $first_name, string $last_name, string $email, string $password){ | 
|  | 21 | $this->user_name = $user_name; | 
|  | 22 | $this->name = $first_name; | 
|  | 23 | $this->first_name = $first_name; | 
|  | 24 | $this->last_name = $last_name; | 
|  | 25 | $this->email = $email; | 
|  | 26 | $this->user_hash = "{crypt}" . crypt($password,'$6$'.generateSalt(10).'$'); | 
|  | 27 | $this->password = $this->user_hash; | 
|  | 28 | } | 
|  | 29 | } | 
|  | 30 |  | 
|  | 31 | function ldap_search_query($query, $filter="cn"){ | 
|  | 32 | include 'config.php'; | 
|  | 33 | $ldap_host = $HOST; | 
|  | 34 | $ldap_port = $PORT; | 
|  | 35 | $ldaptree = explode("{},", $BASE_DN)[1]; | 
|  | 36 |  | 
|  | 37 | $ldap_user = "cn=".$USER.",".join(",", array_slice(explode(",", $ldaptree), 1)); | 
|  | 38 | $ldap_pass = $PASSWORD; | 
|  | 39 |  | 
|  | 40 | //First: Connect to  LDAP Server | 
|  | 41 | $connect = ldap_connect( $ldap_host, $ldap_port) | 
|  | 42 | or debug(">>Could not connect to LDAP server to add user<<"); | 
|  | 43 | ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); | 
|  | 44 | ldap_set_option($connect, LDAP_OPT_REFERRALS, 0); | 
|  | 45 |  | 
|  | 46 | //Login to LDAP | 
|  | 47 | ldap_bind($connect, $ldap_user, $ldap_pass) | 
|  | 48 | or debug(">>Could not bind to $ldap_host to add user<<"); | 
|  | 49 |  | 
|  | 50 |  | 
|  | 51 | $result = ldap_search($connect,$ldaptree, "(".$filter."=".$query.")") or die ("Error in search query: ".ldap_error($connect)); | 
|  | 52 | $data = ldap_get_entries($connect, $result); | 
|  | 53 | return $data; | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | function ldap_add_user ($user) | 
|  | 57 | { | 
|  | 58 | include 'config.php'; | 
|  | 59 | $ldap_host = $HOST; | 
|  | 60 | $ldap_port = $PORT; | 
|  | 61 | $base_dn = str_replace('{}', $user->user_name, $BASE_DN); | 
|  | 62 | $ldaptree = explode("{},", $BASE_DN)[1]; | 
|  | 63 |  | 
|  | 64 |  | 
|  | 65 | $info["givenName"]=$user->first_name; | 
|  | 66 | $info["sn"]=$user->last_name; | 
|  | 67 | $info["uid"]=$user->user_name; | 
|  | 68 | #$info["homeDirectory"]="/home/"; | 
|  | 69 | $info["mail"]=$user->email; | 
|  | 70 | $info["displayName"]= $user->first_name." ".$user->last_name; | 
|  | 71 | #$info["departmentNumber"]=$user->id; | 
|  | 72 | $info["cn"] =$user->user_name; | 
|  | 73 | $info["userPassword"]=$user->user_hash; | 
|  | 74 | $info["objectclass"][0] = "top"; | 
|  | 75 | $info["objectclass"][1] = "person"; | 
|  | 76 | $info["objectclass"][2] = "inetOrgPerson"; | 
|  | 77 | $info["objectclass"][3] = "organizationalPerson"; | 
|  | 78 |  | 
|  | 79 |  | 
|  | 80 |  | 
|  | 81 | $ldap_user = "cn=".$USER.",".join(",", array_slice(explode(",", $ldaptree), 1)); | 
|  | 82 | $ldap_pass = $PASSWORD; | 
|  | 83 |  | 
|  | 84 | //First: Connect to  LDAP Server | 
|  | 85 | $connect = ldap_connect( $ldap_host, $ldap_port) | 
|  | 86 | or debug(">>Could not connect to LDAP server to add user<<"); | 
|  | 87 | ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); | 
|  | 88 | ldap_set_option($connect, LDAP_OPT_REFERRALS, 0); | 
|  | 89 |  | 
|  | 90 | //Login to LDAP | 
|  | 91 | ldap_bind($connect, $ldap_user, $ldap_pass) | 
|  | 92 | or debug(">>Could not bind to $ldap_host to add user<<"); | 
|  | 93 |  | 
|  | 94 | // Adding new user | 
|  | 95 |  | 
|  | 96 | $add = ldap_add($connect, $base_dn, $info) | 
|  | 97 | or debug(">>Not able to load user <<"); | 
|  | 98 |  | 
|  | 99 | // Close connection | 
|  | 100 | ldap_close($connect); | 
|  | 101 |  | 
|  | 102 | // Return value of operation | 
|  | 103 |  | 
|  | 104 | return $add; | 
|  | 105 | } | 
|  | 106 | function ldap_user_count($user){ | 
|  | 107 | return ldap_search_query($user)["count"]; | 
|  | 108 | } | 
|  | 109 | function ldap_mail_count($email){ | 
|  | 110 | return ldap_search_query($email, "mail")["count"]; | 
|  | 111 | } | 
|  | 112 | ?> |