3 Library which implement simple functions for LDAP authentication in Atutor
5 Maintainer smal (Serhiy Voyt)
11 Distributed under GPL (c)Sehiy Voyt 2005-2009
14 if (!defined('AT_INCLUDE_PATH')) { exit; }
16 function get_ldap_config($param){
18 * Get LDAP config param from DB
20 * @param var $param - LDAP config parametr
21 * @return LDAP config parametr value
26 $sql = "SELECT value FROM ".TABLE_PREFIX."config_ldap WHERE name='$param'";
27 $result = mysql_query($sql,$db);
28 if (!($row = mysql_fetch_assoc($result))) {
32 return strtolower($row['value']);
37 function ldap_bind_connect($username, $password){
41 * @param var $username
42 * @param var $password
43 * @return True if success bind to LDAP with username/password, otherwise return False
47 $ldap_server = ldap_connect(get_ldap_config('ldap_name'),get_ldap_config('ldap_port'));
52 ldap_set_option($ldap_server, LDAP_OPT_PROTOCOL_VERSION, 3);
53 ldap_start_tls($ldap_server);
54 #if (!ldap_start_tls($ldap_server)){
57 $user_dn = get_ldap_config('ldap_attr_login'). "=" . $username . "," . get_ldap_config('ldap_base_tree');
58 $ldap_server_bind = ldap_bind($ldap_server, $user_dn, $password);
59 if ($ldap_server_bind == False) {
64 ldap_close($ldap_server);
67 function get_ldap_config_attr() {
69 * Get LDAP config param's value from DB
71 * @return array of LDAP config attributes value (which not NULL) from ATutor DB
79 $sql = "SELECT value FROM ".TABLE_PREFIX."config_ldap WHERE value != '' AND name LIKE 'ldap_attr_%'";
80 if ($result_sql = mysql_query($sql, $db)) {
81 while ($row = mysql_fetch_array($result_sql)){
82 array_push($result, $row[0]);
93 function get_ldap_entry_info($username, $password, $hash_password){
95 * Get info about user entry from LDAP
97 * @param var $username
98 * @param var $password
99 * @param var $hash_password
100 * @return array of user attributes value
106 $ldap_server = ldap_connect(get_ldap_config('ldap_name'),get_ldap_config('ldap_port'));
110 $user_dn = get_ldap_config('ldap_attr_login'). "=" . $username . "," . get_ldap_config('ldap_base_tree');
111 $ldap_server_bind = ldap_bind($ldap_server, $user_dn, $password);
112 if (!$ldap_server_bind) {
115 $filter = get_ldap_config('ldap_attr_login') ."=".$username;
116 $attr = get_ldap_config_attr();
118 if(!$ldap_user= ldap_search($ldap_server, get_ldap_config('ldap_base_tree'),$filter, $attr)){
121 if(!$ldap_user_entry = ldap_first_entry($ldap_server,$ldap_user)){
124 if(!$ldap_user_attr = ldap_get_attributes($ldap_server,$ldap_user_entry)){
127 if(!$ldap_user_info=ldap_get_entries($ldap_server,$ldap_user)){
131 for ($i=0;$i<$ldap_user_attr['count'];$i++){
132 if (isset($ldap_user_info[0][strtolower($ldap_user_attr[$i])][0])) {
133 $result[strtolower($ldap_user_attr[$i])] = $ldap_user_info[0][strtolower($ldap_user_attr[$i])][0];
136 $result[get_ldap_config('ldap_attr_login')] = $username;
137 $result[get_ldap_config('ldap_attr_password')] = $hash_password;
144 ldap_close($ldap_server);
151 function add_ldap_log($ldap_source=NULL) {
153 * Function provide logging all user that's authentivated via LDAP
155 * @param var $ldap_sourse - LDAP server name, optional
156 * @return True if logging success to DB, otherwise False
162 $member_id = $_SESSION['member_id'];
166 $date = date('Y-m-d H:i:s');
168 $sql = "INSERT INTO ".TABLE_PREFIX."ldap_log VALUES($member_id,'$date', '$ldap_source')";
169 $result = mysql_query($sql,$db);
177 function insert_user_info($user_info) {
178 //function provide insert user info from $user_info array into
181 * Insert user info from LDAP to ATutor DB
183 * @param var $user_info - array of user attributes-values
184 * @return member_id of created user or False if error's occured
188 global $db, $_config;
190 $name = strtolower($user_info[get_ldap_config('ldap_attr_login')]);
191 $password = $user_info[get_ldap_config('ldap_attr_password')];
192 $email = $user_info[get_ldap_config('ldap_attr_mail')];
193 $website = $user_info[get_ldap_config('ldap_attr_website')];
195 $first_name = $user_info[get_ldap_config('ldap_attr_first_name')];
196 $second_name = $user_info[get_ldap_config('ldap_attr_second_name')];
197 $last_name = $user_info[get_ldap_config('ldap_attr_last_name')];
199 $dob = $user_info[get_ldap_config('ldap_attr_dob')];
200 //$dob = '0000-00-00';
201 $gender = $user_info[get_ldap_config('ldap_attr_gender')];
202 $address = $user_info[get_ldap_config('ldap_attr_address')];
203 $postal = $user_info[get_ldap_config('ldap_attr_postal')];
204 $city = $user_info[get_ldap_config('ldap_attr_city')];
205 $province = $user_info[get_ldap_config('ldap_attr_province')];
206 $country = $user_info[get_ldap_config('ldap_attr_country')];
207 $phone = $user_info[get_ldap_config('ldap_attr_phone')];
208 $status = AT_STATUS_STUDENT;
209 $now = date('Y-m-d H:i:s');
211 //check unique login and email
212 $sql = "SELECT login FROM ".TABLE_PREFIX."members WHERE login='$name' OR email='$email'";
213 if ($result=mysql_query($sql,$db)){
214 if(mysql_num_rows($result) > 0){
220 $sql = "INSERT INTO ".TABLE_PREFIX."members VALUES (0,'$name','$password','$email','$website','$first_name','$second_name','$last_name', '$dob', '$gender', '$address','$postal','$city','$province','$country', '$phone', $status, '$_config[pref_defaults]', '$now', '$_SESSION[lang]', 1, 1, '0000-00-00 00:00:00')";
221 $result = mysql_query($sql, $db);
226 if ($row = mysql_fetch_assoc($result)){
227 return $row['member_id'];