Advertisement
Guest User

CodeHelp

a guest
Feb 14th, 2016
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.47 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class User_Model extends CI_Model {
  4.  
  5.     function __construct()
  6.     {
  7.         parent::__construct();
  8.     }
  9.    
  10.     function update_user_list($opco = NULL)
  11.     {
  12.         // Variables
  13.         $username = 'user@domain';
  14.         $password = 'Pwd123';
  15.         $host = 'ldap://domain.org';
  16.         $dn = "OU=".$opco.",OU=PRODUCTION,DC=domain,DC=org";
  17.         $filter = "(&(&(&(objectCategory=person)(objectClass=user))))";
  18.         $counti = 0;
  19.         $countu = 0;
  20.         $countd = 0;
  21.        
  22.         // Open the connection
  23.         $ad = ldap_connect($host) or die("Couldn't connect to Active Directory!");
  24.         ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION,3);
  25.         ldap_set_option($ad, LDAP_OPT_REFERRALS,0);
  26.         $bd = ldap_bind($ad,$username,$password) or die("Couldn't bind to Active Directory!");
  27.        
  28.        
  29.         // Launch Active directory search and fill array with results
  30.         $search = @ldap_search($ad, $dn, $filter) or die ("ldap search failed");
  31.         $entries = ldap_get_entries($ad, $search);
  32.        
  33.         // Analyze entries in array
  34.         // Check if ID exists in database
  35.         if ($entries["count"] > 0) {
  36.             for ($i=0; $i<$entries["count"]; $i++) {
  37.                                
  38.                 $this->db->where('rise_id = ', @$entries[$i]['samaccountname'][0]);
  39.                 $this->db->from('ci_user');
  40.                 $num = $this->db->count_all_results();
  41.                
  42.                 if ($num > 0) {
  43.                     $data = array(
  44.                                 'first_name' => ucwords(@$entries[$i]['givenname'][0]),
  45.                                 'last_name' => ucwords(@$entries[$i]['sn'][0]),
  46.                                 'office' => strtoupper(@$entries[$i]['physicaldeliveryofficename'][0]),
  47.                                 'email' => strtolower(@$entries[$i]['mail'][0]),
  48.                                 'telephone_number' => str_replace(' ','',@$entries[$i]['telephonenumber'][0]),
  49.                                 'mobile_number' => str_replace(' ','',@$entries[$i]['mobile'][0]),
  50.                                 'extension' => @$entries[$i]['employeenumber'][0],
  51.                                 'badge_number' => @$entries[$i]['postofficebox'][0],
  52.                                 'short_code' => strtoupper(@$entries[$i]['st'][0]),
  53.                                 'job_title' => ucwords(@$entries[$i]['title'][0]),
  54.                                 'department' => ucwords(@$entries[$i]['department'][0])
  55.                             );
  56.                     $this->db->where('id', @$entries[$i]['samaccountname'][0]);
  57.                     $this->db->update('ci_user',$data);
  58.                     $countu++;
  59.                 } else {
  60.                     $data = array(
  61.                                 'id' => strtolower(@$entries[$i]['samaccountname'][0]),
  62.                                 'first_name' => ucwords(@$entries[$i]['givenname'][0]),
  63.                                 'last_name' => ucwords(@$entries[$i]['sn'][0]),
  64.                                 'office' => strtoupper(@$entries[$i]['physicaldeliveryofficename'][0]),
  65.                                 'email' => strtolower(@$entries[$i]['mail'][0]),
  66.                                 'telephone_number' => str_replace(' ','',@$entries[$i]['telephonenumber'][0]),
  67.                                 'mobile_number' => str_replace(' ','',@$entries[$i]['mobile'][0]),
  68.                                 'extension' => @$entries[$i]['employeenumber'][0],
  69.                                 'badge_number' => @$entries[$i]['postofficebox'][0],
  70.                                 'short_code' => strtoupper(@$entries[$i]['st'][0]),
  71.                                 'job_title' => ucwords(@$entries[$i]['title'][0]),
  72.                                 'department' => ucwords(@$entries[$i]['department'][0])
  73.                             );
  74.                     $this->db->insert('ci_user',$data);
  75.                     $counti++;
  76.                 }
  77.             }
  78.             // Close the connection
  79.             ldap_unbind($ad);
  80.             $feedback = array (
  81.                             "insert" => $counti,
  82.                             "update" => $countu,
  83.                             "opco" => $opco
  84.                         );
  85.             return $feedback;
  86.         } else {
  87.             // Close the connection
  88.             ldap_unbind($ad);
  89.             // Nothing imported, nothing updated
  90.             return false;
  91.         }
  92.        
  93.     }
  94.  
  95. }
  96.  
  97. /* End of file User_Model.php */
  98. /* Location: ./application/models/User_Model.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement