Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('BASEPATH', '.'); //Make this script works with nginx
- $env = is_null(getenv('CI_ENV'))?'':getenv('CI_ENV');
- if (!defined('LDAP_OPT_DIAGNOSTIC_MESSAGE')) {
- define('LDAP_OPT_DIAGNOSTIC_MESSAGE', 0x0032);
- }
- /**
- * Get a list of users from Active Directory.
- */
- $ad_users = array();
- $message = "";
- $ldap_password = 'XXXXX';
- $ldap_username = 'YYYYYY.ZZZZZ';
- $ldap_connection = ldap_connect('ldap.pe.local');
- if (FALSE === $ldap_connection){
- // Uh-oh, something is wrong...
- echo "not connected";
- }
- // We have to set this option for the version of Active Directory we are using.
- ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
- ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.
- if (TRUE === ldap_bind($ldap_connection, $ldap_username, $ldap_password)){
- $ldap_base_dn = 'cn=users,dc=pe,dc=local';
- $search_filter = '(&(objectCategory=person)(samaccountname=*))';
- $attributes = array();
- $attributes[] = 'givenname';
- $attributes[] = 'mail';
- $attributes[] = 'samaccountname';
- $attributes[] = 'sn';
- $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes);
- if (FALSE !== $result){
- $entries = ldap_get_entries($ldap_connection, $result);
- echo "<pre>";
- print_r($entries);
- echo "</pre>";
- for ($x=0; $x<$entries['count']; $x++){
- // reject strange users
- $username = strtolower($entries[$x]['samaccountname'][0]);
- // reject 'track' and 'powertrain'
- if(stristr($username, 'track') || stristr($username, 'powertrain')) {
- continue;
- }
- // reject those not having a dot in the username
- if(stristr($username, '.') === FALSE) {
- continue;
- }
- $ad_users[$username]['dn'] = $entries[$x]['dn'];
- $ad_users[$username]['login'] = $username;
- $ad_users[$username]['firstname'] = ucfirst(stristr($username, '.', true));
- $ad_users[$username]['lastname'] = ucfirst(substr(stristr($username, '.'), 1));
- }
- }
- ldap_unbind($ldap_connection); // Clean up after ourselves.
- }
- else echo "not binded";
- $message .= "Retrieved ". count($ad_users) ." Active Directory users\n";
- echo $message;
- echo "<pre>";
- print_r($ad_users);
- echo "</pre>";
- $dbConn = new mysqli('localhost', 'jorani', 'jorani');
- $dbConn->select_db('jorani');
- $sql = "SELECT login FROM users";
- $res = $dbConn->query($sql);
- while ($row = $res->fetch_assoc()) {
- // remove field from ad_users if username exists
- if(array_key_exists($row['login'], $ad_users)) {
- echo $row['login']." already exists! Skipping\n";
- unset($ad_users[$row['login']]);
- }
- }
- echo "<pre>";
- foreach($ad_users as $uname => $u) {
- echo "
- insert into users
- (firstname, lastname, login, email, role, manager, organization, contract, position, datehired, ldap_path, active, timezone, identifier)
- values (
- '$u[firstname]', '$u[lastname]', '$uname', '$uname@podiumengineering.com', '2', '2', '0', '1', '1', '2018-03-12', '$u[dn]', '1', 'Europe/Rome', '');
- ";
- }
- echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement