Location: PHPKode > projects > phpMyOrdering > phpMyOrdering-0.1.7-alpha/scripts/classes/class.ldap.php
<?php

class ldap {
 
 function connect($servers, $port)
 {
  if ((!empty($servers)) && (!empty($port)))
  {
   $data = @ldap_connect($servers, $port);
   @ldap_set_option($data, LDAP_OPT_PROTOCOL_VERSION, 3);
   @ldap_set_option($data, LDAP_OPT_REFERRALS, 0);
   @ldap_set_option($data, LDAP_OPT_TIMELIMIT, 10);
   @ldap_set_option($data, LDAP_OPT_NETWORK_TIMEOUT, 10);
  } else {
   $data = -1;
  }
  return $data;
 }
 
 function bind($resource, $binddn, $bindpass)
 {
  if ((!empty($resource)) && (!empty($binddn)) && (!empty($bindpass))) {
   if (@ldap_bind($resource, $binddn, $bindpass) !== FALSE) {
    $data = 0;
   } else {
    $data = $this->errors($resource);
   }
  } else {
   $data = -1;
  }
  return $data;
 }
 
 function auth($user, $pass, $servers, $port)
 {
  if( ( !empty( $servers ) ) && ( !empty( $port ) ) && ( !empty( $user ) ) && ( !empty( $pass ) ) ) {
   if( ( $handle = $this->connect( $servers, $port ) ) !== -1 ) {
    if( $this->bind( $handle, $user, $pass ) === 0 ) {
     $data = 0;
    } else {
     $data = -1;
    }
   } else {
    $data = -2;
   }
  } else {
   $data = -3;
  }
  //$this->close( $resource );
  return $data;
 }
 
 function errors($resource)
 {
  return @ldap_error($resource) . " => " . @ldap_errno($resource);
 }
 
 function queryGroups($resource, $basedn)
 {
  $filter = "(&(objectCategory=group)(sAMAccountName=*))";
  $result = @ldap_search($resource, $basedn, $filter);
  if ($result === FALSE) { $result = $this->errors($resource); }
  //$this->close( $resource );
  return $result;
 }
 
 function queryUsers($resource, $basedn, $filter = "*" )
 {
  $filter = "(&(objectCategory=user)(objectCategory=person)(sAMAccountName=".$filter."))";
  $attributes = array("cn", "sn", "name", "distinguishedname", "description", "whencreated", "whenchanged", "dn", "memberof", "mail");
  $result = @ldap_search($resource, $basedn, $filter );
  if ($result === FALSE) { $result = $this->errors($resource); }
  //$this->close( $resource );
  return $result;
 }
 
 function queryUsersByName($resource, $basedn, $filter = "*" )
 {
  $filter = "(&(objectCategory=user)(objectCategory=person)(distinguishedname=".$filter."))";
  $attributes = array("cn", "sn", "name", "distinguishedname", "description", "whencreated", "whenchanged", "dn", "memberof", "mail", "userprincipalname");
  $result = @ldap_search($resource, $basedn, $filter, $attributes );
  if ($result === FALSE) { $result = $this->errors($resource); }
  //$this->close( $resource );
  return $result;
 }
 
 function queryUserByDN( $resource, $dn )
 {
  $filter = "(&(objectCategory=user)(objectCategory=person))";
  $attributes = array("cn", "sn", "name", "samaccountname", "distinguishedname", "description", "whencreated", "whenchanged", "dn", "memberof", "mail", "userprincipalname");
  $result = @ldap_read($resource, $dn, $filter, $attributes );
  if ($result === FALSE) { $result = $this->errors($resource); }
  //$this->close( $resource );
  return $result;
 }
 
 function queryObject($resource, $basedn, $filter)
 {
  $result = @ldap_search($resource, $basedn, "(cn=" . $filter . ")");
  if ($result === FALSE) { $result = $this->errors($resource); }
  //$this->close( $resource );
  return $result;
 }
 
 function queryContainers($resource, $basedn)
 {
  $filter = "(&(objectCategory=*)(sAMAccountName=*))";
  $result = @ldap_search($resource, $basedn, $filter);
  if ($result === FALSE) { $result = $this->errors($resource); }
  //$this->close( $resource ); 
  return $result;
 }
 
 function getEntries($resource, $results)
 {
  $data = @ldap_get_entries($resource, $results);
  //$this->close( $resource );
  return $data;
 }
 
 function closeConn( $resource )
 {
  @ldap_free_result( $resource );
  @ldap_unbind( $resource );
  @ldap_close( $resource );
  return;
 }
 
 function filterUserResults( $results )
 {
  if( count( $results['count'] ) > 0 ) {
   foreach( $results as $key => $value ) {
    if( $key !== "count" ) {
     $dn = $value['dn'];
     $data[$dn]['dn'] = $value['dn'];
     $data[$dn]['cn'] = $value['cn'][0];
     $data[$dn]['username'] = $value['samaccountname'][0];
     $data[$dn]['description'] = $value['description'][0];
     $data[$dn]['distinguishedname'] = $value['distinguishedname'][0];
     if( !empty( $value['mail'] ) ) { $data[$dn]['email'] = $value['mail'][0]; }
     if( !empty( $value['userprincipalname'] ) ) { $data[$dn]['email'] = $value['userprincipalname'][0]; }
     $tmp = date_parse( $value['whencreated'][0] );
     $data[$dn]['create_date'] = $tmp['year'] . "-" . $tmp['month'] . "-" . $tmp['day'];
     $data[$dn]['create_time'] = $tmp['hour'] . ":" . $tmp['minute'] . ":" . $tmp['second'] . " AM";
     if( count( $value['memberof'] ) > 0 ) {
      foreach( $value['memberof'] as $id => $group ) {
       if( $id !== "count" ) {
        $data[$dn]['groups'][$id] = $group;
       }
      }
     }
    }
   }
  } else {
   $data = -1;
  }
  return $data;
 }
 
 function filterGroupsResults( $results )
 {
  if( count( $results['count'] ) > 0 ) {
   foreach( $results as $key => $value ) {
    if( $key !== "count" ) {
     $dn = $value['dn'];
     $data[$dn]['dn'] = $value['dn'];
     $data[$dn]['cn'] = $value['cn'][0];
     $data[$dn]['username'] = $value['samaccountname'][0];
     $data[$dn]['description'] = $value['description'][0];
     $data[$dn]['distinguishedname'] = $value['distinguishedname'][0];
     $data[$dn]['create_date'] = $tmp['year'] . "-" . $tmp['month'] . "-" . $tmp['day'];
     $data[$dn]['create_time'] = $tmp['hour'] . ":" . $tmp['minute'] . ":" . $tmp['second'] . " AM";
     if( count( $value['member'] ) > 0 ) {
      foreach( $value['member'] as $id => $group ) {
       if( $id !== "count" ) {
        $data[$dn]['members'][$id] = $group;
       }
      }
     }
    }
   }
  } else {
   $data = -1;
  }
  return $data;
 }
 
}

?>
Return current item: phpMyOrdering