| <?php |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class LDAP |
| { |
| |
| var $ldapconn; |
| var $ldapbind; |
| |
| |
| var $filter_attrb = array(); |
| var $filter_type = array(); |
| var $filter_clean = array(); |
| |
| |
| |
| function __construct() |
| { |
| |
| $this->ldapconn = ldap_connect(LDAP_HOST,LDAP_PORT) or die("Could not connect to " . LDAP_HOST . "."); |
| |
| |
| ldap_set_option($this->ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3) or die("Could not connect to server through LDAPv3."); |
| |
| |
| $this->ldapbind = ldap_bind( $this->ldapconn, LDAP_ROOTDN, LDAP_ROOTPW ); |
| |
| |
| $this->filter_attrb['cn'] = 'cn'; |
| $this->filter_attrb['uid'] = 'uid'; |
| $this->filter_attrb['employeetype'] = ucfirst(translate('employeetype')); |
| $this->filter_attrb['preferredlanguage'] = ucfirst(translate('language')); |
| |
| |
| $this->filter_type['='] = '='; |
| $this->filter_type['~='] = '~='; |
| |
| |
| $this->filter_clean['attrb'] = 'preferredlanguage'; |
| $this->filter_clean['type'] = '='; |
| $this->filter_clean['value'] = LANGUAGE; |
| } |
| |
| |
| |
| function get_entries( $filter ) |
| { |
| |
| $search = ldap_search($this->ldapconn,LDAP_DN,$filter); |
| $entries = ldap_get_entries($this->ldapconn,$search); |
| return $entries; |
| } |
| |
| |
| |
| |
| function is_valid( $name , $value ) |
| { |
| switch ( $name ) |
| { |
| case 'uid': |
| $pattern = '/^([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+([a-z]{2,6})?$/'; |
| break; |
| |
| case 'preferredlanguage': |
| $pattern = '/^[a-zA-Z]{2}$/'; |
| break; |
| |
| case 'filtertype': |
| $pattern = '/^(=|~=)$/'; |
| break; |
| |
| case 'employeetype': |
| $pattern = '/^(writer|administrator)$/'; |
| break; |
| |
| default: |
| $pattern = '/^[a-zA-Z0-9_áéíóñúàçèé ]+$/'; |
| break; |
| } |
| |
| if ( isset( $pattern ) && preg_match( $pattern , $value )) |
| { |
| return true; |
| } |
| else |
| { |
| return false; |
| } |
| } |
| |
| |
| |
| function sanitize_filter_attribute() |
| { |
| $dirty = array(); |
| $clean = array(); |
| |
| |
| $dirty['attrb'] = $this->filter_clean['attrb']; |
| |
| |
| $clean['attrb'] = $this->filter_clean['attrb']; |
| |
| |
| if ( isset( $_POST['attribute'] ) ) |
| { |
| $dirty['attrb'] = $_POST['attribute']; |
| } |
| |
| |
| if ( array_key_exists( $dirty['attrb'], $this->filter_attrb ) ) |
| { |
| |
| $clean['attrb'] = $dirty['attrb']; |
| } |
| |
| return $clean['attrb']; |
| } |
| |
| |
| |
| function sanitize_filter_type() |
| { |
| $dirty = array(); |
| $clean = array(); |
| |
| $dirty['type'] = $this->filter_clean['type']; |
| $clear['type'] = $this->filter_clean['type']; |
| |
| |
| if ( isset( $_POST['type'] ) ) |
| { |
| $dirty['type'] = $_POST['type']; |
| } |
| else |
| { |
| $dirty['type'] = $this->filter_clean['type']; |
| } |
| |
| |
| if ( array_key_exists( $dirty['type'], $this->filter_type ) ) |
| { |
| |
| $clean['type'] = $dirty['type']; |
| } |
| |
| return $clean['type']; |
| } |
| |
| |
| |
| function sanitize_filter_value() |
| { |
| $dirty = array(); |
| $clean = array(); |
| |
| $dirty['value'] = $this->filter_clean['value']; |
| $clean['value'] = $this->filter_clean['value']; |
| |
| |
| if ( isset( $_POST['value'] ) ) |
| { |
| $dirty['value'] = $_POST['value']; |
| } |
| |
| |
| $name = $this->sanitize_filter_attribute(); |
| $value = $dirty['value']; |
| |
| if ( $this->is_valid( $name, $value ) ) |
| { |
| |
| $clean['value'] = $value; |
| } |
| |
| return $clean['value']; |
| } |
| |
| |
| |
| function show_filter() |
| { |
| $clean = array(); |
| |
| $clean['attrb'] = $this->sanitize_filter_attribute(); |
| $clean['type'] = $this->sanitize_filter_type(); |
| $clean['value'] = $this->sanitize_filter_value(); |
| |
| |
| $html = '<div class="filter">'; |
| $html .= '<form name="filter" method="post" action="">'; |
| |
| |
| $html .= ucfirst(translate('filtering by')) . ': '; |
| |
| |
| $html .= get_user_attrSelector(); |
| |
| |
| $html .= '<select name="type">'; |
| foreach ($this->filter_type as $key => $value) |
| { |
| if ($clean['type'] == $key ) |
| { |
| $html .= '<option selected value="'.$key.'">' . $value . '</option>'; |
| } |
| else |
| { |
| $html .= '<option value="'.$key.'">' . $value . '</option>'; |
| } |
| } |
| $html .= '</select>'; |
| |
| |
| $html .= '<input type="text" name="value" value="'.$clean['value'].'">'; |
| |
| |
| $html .= '<input type="submit" name="submit_filter" value="'.ucfirst(translate('filter')).'">'; |
| |
| |
| $html .= '</form>'; |
| $html .= '</div>'; |
| |
| return $html; |
| } |
| |
| |
| |
| function build_filter_string() |
| { |
| $clean['attrb'] = $this->sanitize_filter_attribute(); |
| $clean['type'] = $this->sanitize_filter_type(); |
| $clean['value'] = $this->sanitize_filter_value(); |
| |
| return $clean['attrb'] . $clean['type'] . $clean['value']; |
| |
| } |
| |
| |
| |
| function is_uid_present( $uid ) |
| { |
| |
| $filter = 'uid=' . $uid; |
| $entry = $this->get_entries($filter); |
| |
| if ( $uid != '' && $entry['count'] == 1 ) |
| { |
| return true; |
| } |
| else |
| { |
| return false; |
| } |
| } |
| |
| |
| |
| function prepare_userpassword( $userpassword ) |
| { |
| $dirty['userpassword'] = $userpassword; |
| |
| switch ( LDAP_PASSHASH ) |
| { |
| case '{MD5}': |
| $clean['userpassword'] = LDAP_PASSHASH . base64_encode( pack( 'H*', md5( $dirty['userpassword'] ) ) ); |
| break; |
| |
| case '{SHA}': |
| $clean['userpassword'] = LDAP_PASSHASH . base64_encode( pack( 'H*', sha1( $dirty['userpassword'] ) ) ); |
| break; |
| } |
| |
| return $clean['userpassword']; |
| } |
| |
| |
| |
| |
| |
| |
| function sanitize_entry( $entry ) |
| { |
| |
| $fields = array('uid', 'cn','userpassword','displayname','preferredlanguage','employeetype'); |
| |
| |
| foreach ( $fields as $key ) |
| { |
| if ( isset( $entry[$key] ) && $this->is_valid( $key, $entry[$key] ) ) |
| { |
| |
| $clean['entry'][$key] = $entry[$key]; |
| } |
| } |
| |
| |
| if ( isset( $clean['entry'] ) && is_array( $clean['entry'] ) ) |
| { |
| return $clean['entry']; |
| } |
| else |
| { |
| return false; |
| } |
| } |
| |
| |
| |
| |
| |
| function init_useradd_values( $attributes ) |
| { |
| foreach ( $attributes as $key ) |
| { |
| if ( ! isset( $_POST[$key] ) ) |
| { |
| $entry[$key] = ''; |
| } |
| else |
| { |
| if ( isset( $_POST[$key] ) && $this->is_valid($key, $_POST[$key]) ) |
| { |
| $entry[$key] = $_POST[$key]; |
| } |
| else |
| { |
| $entry[$key] = ''; |
| } |
| } |
| } |
| return $entry; |
| } |
| |
| |
| |
| |
| |
| |
| |
| function init_useradmin_values( $entry, $attributes, $action ) |
| { |
| |
| |
| for ($i = 0; $i < $entry['count']; $i++) |
| { |
| if ( isset( $_POST['uid'][$i] )) |
| { |
| |
| $entry_new['uid'] = $entry[$i]['uid'][0]; |
| |
| |
| |
| $entry_new['cn'] = $entry[$i]['cn'][0]; |
| |
| |
| foreach ( $attributes as $key ) |
| { |
| |
| if ( isset( $_POST[$key][$i] ) ) |
| { |
| |
| if ( $this->is_valid( $key, $_POST[$key][$i] ) ) |
| { |
| |
| if ( $_POST[$key][$i] != $entry[$i][$key][0] ) |
| { |
| |
| $entry_new[$key] = $_POST[$key][$i]; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if ( $key == 'userpassword' ) |
| { |
| $newpasswd = $this->prepare_userpassword($_POST[$key][$i]); |
| $entry_new[$key] = $newpasswd; |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| |
| if ( isset( $entry_new ) ) |
| { |
| $message = $this->do_action( $entry_new, $action ); |
| } |
| else |
| { |
| $message = show_message(ucfirst(translate('nothing to do')), 'orange'); |
| } |
| } |
| |
| return $message; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function do_action( $entry, $action ) |
| { |
| |
| if ( isset( $entry['uid'] ) && $this->is_valid( 'uid', $entry['uid'] ) ) |
| { |
| $dn = 'uid=' . $entry['uid'] . ',' . LDAP_DN; |
| } |
| else |
| { |
| $message = show_message(ucfirst(translate('a valid uid is required')),'orange'); |
| return $message; |
| } |
| |
| |
| $possible_actions = '/^(add|update|delete)$/'; |
| if ( ! preg_match( $possible_actions, $action ) ) |
| { |
| |
| $message = show_message(ucfirst(translate('invalid action')), 'red'); |
| return $message; |
| } |
| |
| |
| switch ( $action ) |
| { |
| case 'update': |
| |
| |
| if ( is_array( $entry ) ) |
| { |
| |
| $entry['sn'] = preg_replace('/^([a-zA-Z0-9_]+ ?)/','', $entry['cn']); |
| if ( $entry['sn'] == '' ) |
| { |
| $message = show_message(ucfirst(translate('invalid cn')), 'orange'); |
| return $message; |
| } |
| |
| if (ldap_modify($this->ldapconn, $dn, $entry)) |
| { |
| $message = show_message(ucfirst(translate('data was updated successfully')), 'green'); |
| } |
| else |
| { |
| $message = show_message(ucfirst(translate('data was not updated')), 'orange'); |
| } |
| } |
| break; |
| |
| |
| case 'delete': |
| |
| |
| if ( is_array( $entry ) ) |
| { |
| if ( ldap_delete( $this->ldapconn, $dn ) ) |
| { |
| $message = show_message(ucfirst(translate('data was deleted successfully')), 'green'); |
| } |
| else |
| { |
| $message = show_message(ucfirst(translate('data was not deleted')), 'orange'); |
| } |
| } |
| break; |
| |
| |
| case 'add': |
| |
| |
| if ( $this->is_uid_present( $entry['uid'] ) ) |
| { |
| |
| $message = show_message(ucfirst(translate('user identifier already exists')), 'orange'); |
| return $message; |
| } |
| |
| |
| $require_attrs = array('uid', 'userpassword', 'cn', 'displayname', 'preferredlanguage', 'employeetype'); |
| foreach ( $require_attrs as $key ) |
| { |
| if ( !isset($entry[$key]) || ! $this->is_valid($key, $entry[$key])) |
| { |
| $message = show_message(ucfirst(translate('the field')) .' '. translate($key) .' ' . translate('requires a valid value') , 'orange'); |
| return $message; |
| } |
| } |
| |
| |
| $entry['userpassword'] = $this->prepare_userpassword($entry['userpassword']); |
| $entry['objectclass'] = 'inetOrgPerson'; |
| $entry['sn'] = preg_replace('/^([a-zA-Z0-9_]+ ?)/','', $entry['cn']); |
| if ( $entry['sn'] == '' ) |
| { |
| $message = show_message(ucfirst(translate('invalid cn')), 'orange'); |
| return $message; |
| } |
| $entry['mail'] = $entry['uid']; |
| |
| |
| if ( ldap_add( $this->ldapconn, $dn, $entry ) ) |
| { |
| $message = show_message(ucfirst(translate('user added successfully')), 'green'); |
| } |
| else |
| { |
| $message = show_message(ucfirst(translate('user was not added')), 'orange'); |
| } |
| break; |
| } |
| |
| return $message; |
| } |
| |
| |
| |
| function rename_dn( $olddn, $newdn, $newparent, $deleteoldrdn ) |
| { |
| ldap_rename($this->ldapconn, $olddn, $newdn, $newparent, $deleteoldrdn ); |
| |
| return true; |
| } |
| |
| |
| |
| |
| |
| function show_useradmin_info( $entries ) |
| { |
| $html = '<ul>'; |
| $html .= '<li>LDAP Host: ' . LDAP_HOST . '</li>'; |
| $html .= '<li>Domain Component (dc): ' . LDAP_DN . '</li>'; |
| $html .= '<li>' . $this->show_filter() .'</li>'; |
| $html .= '<li>' . ucfirst(translate('results')) . ': '. $entries['count']; '</li>'; |
| $html .= '</ul>'; |
| |
| return $html; |
| } |
| |
| |
| |
| function __destruct() |
| { |
| if ( isset( $this->ldapconn ) ) |
| { |
| ldap_unbind( $this->ldapconn ); |
| } |
| } |
| |
| } |
| |
| $ldap = new LDAP; |
| ?> |