Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class User_Model extends CI_Model {
- function __construct()
- {
- parent::__construct();
- }
- function update_user_list($opco = NULL)
- {
- // Variables
- $username = 'user@domain';
- $password = 'Pwd123';
- $host = 'ldap://domain.org';
- $dn = "OU=".$opco.",OU=PRODUCTION,DC=domain,DC=org";
- $filter = "(&(&(&(objectCategory=person)(objectClass=user))))";
- $counti = 0;
- $countu = 0;
- $countd = 0;
- // Open the connection
- $ad = ldap_connect($host) or die("Couldn't connect to Active Directory!");
- ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION,3);
- ldap_set_option($ad, LDAP_OPT_REFERRALS,0);
- $bd = ldap_bind($ad,$username,$password) or die("Couldn't bind to Active Directory!");
- // Launch Active directory search and fill array with results
- $search = @ldap_search($ad, $dn, $filter) or die ("ldap search failed");
- $entries = ldap_get_entries($ad, $search);
- // Analyze entries in array
- // Check if ID exists in database
- if ($entries["count"] > 0) {
- for ($i=0; $i<$entries["count"]; $i++) {
- $this->db->where('rise_id = ', @$entries[$i]['samaccountname'][0]);
- $this->db->from('ci_user');
- $num = $this->db->count_all_results();
- if ($num > 0) {
- $data = array(
- 'first_name' => ucwords(@$entries[$i]['givenname'][0]),
- 'last_name' => ucwords(@$entries[$i]['sn'][0]),
- 'office' => strtoupper(@$entries[$i]['physicaldeliveryofficename'][0]),
- 'email' => strtolower(@$entries[$i]['mail'][0]),
- 'telephone_number' => str_replace(' ','',@$entries[$i]['telephonenumber'][0]),
- 'mobile_number' => str_replace(' ','',@$entries[$i]['mobile'][0]),
- 'extension' => @$entries[$i]['employeenumber'][0],
- 'badge_number' => @$entries[$i]['postofficebox'][0],
- 'short_code' => strtoupper(@$entries[$i]['st'][0]),
- 'job_title' => ucwords(@$entries[$i]['title'][0]),
- 'department' => ucwords(@$entries[$i]['department'][0])
- );
- $this->db->where('id', @$entries[$i]['samaccountname'][0]);
- $this->db->update('ci_user',$data);
- $countu++;
- } else {
- $data = array(
- 'id' => strtolower(@$entries[$i]['samaccountname'][0]),
- 'first_name' => ucwords(@$entries[$i]['givenname'][0]),
- 'last_name' => ucwords(@$entries[$i]['sn'][0]),
- 'office' => strtoupper(@$entries[$i]['physicaldeliveryofficename'][0]),
- 'email' => strtolower(@$entries[$i]['mail'][0]),
- 'telephone_number' => str_replace(' ','',@$entries[$i]['telephonenumber'][0]),
- 'mobile_number' => str_replace(' ','',@$entries[$i]['mobile'][0]),
- 'extension' => @$entries[$i]['employeenumber'][0],
- 'badge_number' => @$entries[$i]['postofficebox'][0],
- 'short_code' => strtoupper(@$entries[$i]['st'][0]),
- 'job_title' => ucwords(@$entries[$i]['title'][0]),
- 'department' => ucwords(@$entries[$i]['department'][0])
- );
- $this->db->insert('ci_user',$data);
- $counti++;
- }
- }
- // Close the connection
- ldap_unbind($ad);
- $feedback = array (
- "insert" => $counti,
- "update" => $countu,
- "opco" => $opco
- );
- return $feedback;
- } else {
- // Close the connection
- ldap_unbind($ad);
- // Nothing imported, nothing updated
- return false;
- }
- }
- }
- /* End of file User_Model.php */
- /* Location: ./application/models/User_Model.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement