Advertisement
Guest User

PhpLdapImport

a guest
Mar 14th, 2018
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. <?php
  2.  
  3. define('BASEPATH', '.'); //Make this script works with nginx
  4. $env = is_null(getenv('CI_ENV'))?'':getenv('CI_ENV');
  5. if (!defined('LDAP_OPT_DIAGNOSTIC_MESSAGE')) {
  6.     define('LDAP_OPT_DIAGNOSTIC_MESSAGE', 0x0032);
  7. }
  8.  
  9. /**
  10.  * Get a list of users from Active Directory.
  11.  */
  12. $ad_users = array();
  13. $message = "";
  14.  
  15. $ldap_password = 'XXXXX';
  16. $ldap_username = 'YYYYYY.ZZZZZ';
  17. $ldap_connection = ldap_connect('ldap.pe.local');
  18. if (FALSE === $ldap_connection){
  19.     // Uh-oh, something is wrong...
  20.         echo "not connected";
  21. }
  22.  
  23. // We have to set this option for the version of Active Directory we are using.
  24. ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
  25. ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.
  26.  
  27. if (TRUE === ldap_bind($ldap_connection, $ldap_username, $ldap_password)){
  28.     $ldap_base_dn = 'cn=users,dc=pe,dc=local';
  29.     $search_filter = '(&(objectCategory=person)(samaccountname=*))';
  30.     $attributes = array();
  31.     $attributes[] = 'givenname';
  32.     $attributes[] = 'mail';
  33.     $attributes[] = 'samaccountname';
  34.     $attributes[] = 'sn';
  35.     $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes);
  36.     if (FALSE !== $result){
  37.         $entries = ldap_get_entries($ldap_connection, $result);
  38.         echo "<pre>";
  39.         print_r($entries);
  40.         echo "</pre>";
  41.  
  42.         for ($x=0; $x<$entries['count']; $x++){
  43.                 // reject strange users
  44.                 $username = strtolower($entries[$x]['samaccountname'][0]);
  45.                 // reject 'track' and 'powertrain'
  46.                 if(stristr($username, 'track') || stristr($username, 'powertrain')) {
  47.                         continue;
  48.                 }
  49.                 // reject those not having a dot in the username
  50.                 if(stristr($username, '.') === FALSE) {
  51.                         continue;
  52.                 }
  53.                 $ad_users[$username]['dn'] = $entries[$x]['dn'];
  54.                 $ad_users[$username]['login'] = $username;
  55.                 $ad_users[$username]['firstname'] = ucfirst(stristr($username, '.', true));
  56.                 $ad_users[$username]['lastname'] = ucfirst(substr(stristr($username, '.'), 1));
  57.         }
  58.     }
  59.     ldap_unbind($ldap_connection); // Clean up after ourselves.
  60. }
  61. else echo "not binded";
  62.  
  63. $message .= "Retrieved ". count($ad_users) ." Active Directory users\n";
  64.  
  65. echo $message;
  66.  
  67. echo "<pre>";
  68. print_r($ad_users);
  69. echo "</pre>";
  70.  
  71. $dbConn = new mysqli('localhost', 'jorani', 'jorani');
  72. $dbConn->select_db('jorani');
  73. $sql = "SELECT login FROM users";
  74. $res = $dbConn->query($sql);
  75.  
  76. while ($row = $res->fetch_assoc()) {
  77.         // remove field from ad_users if username exists
  78.         if(array_key_exists($row['login'], $ad_users)) {
  79.                 echo $row['login']." already exists! Skipping\n";
  80.                 unset($ad_users[$row['login']]);
  81.         }
  82. }
  83.  
  84.  
  85. echo "<pre>";
  86. foreach($ad_users as $uname => $u) {
  87.     echo "
  88. insert into users
  89. (firstname, lastname, login, email, role, manager, organization, contract, position, datehired, ldap_path, active, timezone, identifier)
  90. values (
  91. '$u[firstname]', '$u[lastname]', '$uname', '$uname@podiumengineering.com', '2', '2', '0', '1', '1', '2018-03-12', '$u[dn]', '1', 'Europe/Rome', '');
  92. ";
  93. }
  94. echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement