247 lines
5.8 KiB
PHP
247 lines
5.8 KiB
PHP
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||
|
|
||
|
/**
|
||
|
* This class takes care of searching within LDAP
|
||
|
*
|
||
|
* @package Kohana/LDAP
|
||
|
* @subpackage LDAP/Search
|
||
|
* @category Helpers
|
||
|
* @author Deon George
|
||
|
* @copyright (c) phpLDAPadmin Development Team
|
||
|
* @license http://dev.phpldapadmin.org/license.html
|
||
|
*/
|
||
|
abstract class Kohana_Database_LDAP_Search {
|
||
|
private $_connection; // Our LDAP Server to query
|
||
|
|
||
|
private $_attrs = array('*','+'); // LDAP Attributes to Return
|
||
|
private $_base = array(); // LDAP Search Base
|
||
|
private $_deref = LDAP_DEREF_NEVER; // LDAP Search Default DEREF
|
||
|
private $_filter = '(objectclass=*)'; // LDAP Search Filter
|
||
|
private $_size_limit = '500'; // LDAP Search Size Limit
|
||
|
private $_scope = 'base'; // LDAP Search Scope
|
||
|
private $_time_limit = '60'; // LDAP Search Time Limit
|
||
|
private $_db_pending = array(); // LDAP Query Filter to compile
|
||
|
|
||
|
/**
|
||
|
* Callable database methods
|
||
|
* @var array
|
||
|
*/
|
||
|
protected static $_db_methods = array(
|
||
|
'where', 'and_where', 'or_where', 'where_open', 'and_where_open', 'or_where_open', 'where_close',
|
||
|
'and_where_close', 'or_where_close',
|
||
|
);
|
||
|
|
||
|
/**
|
||
|
* Members that have access methods
|
||
|
* @var array
|
||
|
*/
|
||
|
protected static $_properties = array(
|
||
|
);
|
||
|
|
||
|
public function __construct($resource,$base=array()) {
|
||
|
$this->_connection = $resource;
|
||
|
|
||
|
$this->_base = is_null($base) ? $this->base() : $base;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Handles pass-through to database methods. Calls to query methods
|
||
|
* (query, get, insert, update) are not allowed. Query builder methods
|
||
|
* are chainable.
|
||
|
*
|
||
|
* @param string $method Method name
|
||
|
* @param array $args Method arguments
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function __call($method,array $args) {
|
||
|
if (in_array($method,Database_LDAP_Search::$_properties)) {
|
||
|
/*
|
||
|
// @todo To Implement
|
||
|
if ($method === 'validation')
|
||
|
{
|
||
|
if ( ! isset($this->_validation))
|
||
|
{
|
||
|
// Initialize the validation object
|
||
|
$this->_validation();
|
||
|
}
|
||
|
}
|
||
|
*/
|
||
|
|
||
|
// Return the property
|
||
|
return $this->{'_'.$method};
|
||
|
}
|
||
|
elseif (in_array($method,Database_LDAP_Search::$_db_methods))
|
||
|
{
|
||
|
// Add pending database call which is executed after query type is determined
|
||
|
$this->_db_pending[] = array('name' => $method,'args' => $args);
|
||
|
|
||
|
return $this;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
throw new Kohana_Exception('Invalid method :method called in :class',
|
||
|
array(':method' => $method,':class' => get_class($this)));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function _build() {
|
||
|
$search = new Database_LDAP_Search_Builder_Query();
|
||
|
|
||
|
// Process pending database method calls
|
||
|
foreach ($this->_db_pending as $method) {
|
||
|
$name = $method['name'];
|
||
|
$args = $method['args'];
|
||
|
|
||
|
$this->_db_applied[$name] = $name;
|
||
|
|
||
|
call_user_func_array(array($search,$name),$args);
|
||
|
}
|
||
|
|
||
|
return $search;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Figure out the bases
|
||
|
*/
|
||
|
public function base() {
|
||
|
// If the base is set in the configuration file, then just return that.
|
||
|
if (! is_null($x=Kohana::$config->load('database.default.connection.database')))
|
||
|
return $x;
|
||
|
|
||
|
$x = Database_LDAP::factory('auth');
|
||
|
|
||
|
$u = $x->search(array(''))
|
||
|
->scope('base')
|
||
|
->run();
|
||
|
|
||
|
// Remove the '' base
|
||
|
$u = array_pop($u);
|
||
|
$u = array_pop($u);
|
||
|
|
||
|
return isset($u['namingcontexts']) ? $u['namingcontexts'] : array();
|
||
|
}
|
||
|
|
||
|
public static function instance($resource) {
|
||
|
return new Database_LDAP_Search($resource);
|
||
|
}
|
||
|
|
||
|
public function deref($val) {
|
||
|
$this->_deref = $val;
|
||
|
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Search the LDAP database
|
||
|
*/
|
||
|
public function run() {
|
||
|
$query = array();
|
||
|
|
||
|
// Query Defaults
|
||
|
$attrs_only = 0;
|
||
|
|
||
|
// Compile our query
|
||
|
if ($this->_db_pending)
|
||
|
$this->_filter = $this->_build();
|
||
|
|
||
|
$result = array();
|
||
|
foreach ($this->_base as $base) {
|
||
|
switch ($this->_scope) {
|
||
|
case 'base':
|
||
|
$search = ldap_read($this->_connection,$base,$this->_filter,$this->_attrs,$attrs_only,$this->_size_limit,$this->_time_limit,$this->_deref);
|
||
|
break;
|
||
|
|
||
|
case 'one':
|
||
|
$search = ldap_list($this->_connection,$base,$this->_filter,$this->_attrs,$attrs_only,$this->_size_limit,$this->_time_limit,$this->_deref);
|
||
|
break;
|
||
|
|
||
|
case 'sub':
|
||
|
default:
|
||
|
$search = ldap_search($this->_connection,$base,$this->_filter,$this->_attrs,$attrs_only,$this->_size_limit,$this->_time_limit,$this->_deref);
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (! $search) {
|
||
|
$result[$base] = array();
|
||
|
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// Get the first entry identifier
|
||
|
if ($entries = ldap_get_entries($this->_connection,$search)) {
|
||
|
|
||
|
# Remove the count
|
||
|
if (isset($entries['count']))
|
||
|
unset($entries['count']);
|
||
|
|
||
|
// Iterate over the entries
|
||
|
foreach ($entries as $a => $entry) {
|
||
|
if (! isset($entry['dn']))
|
||
|
throw HTTP_Exception::factory(501,'No DN?');
|
||
|
|
||
|
// Remove the none entry references.
|
||
|
if (! is_array($entry)) {
|
||
|
unset($entries[$a]);
|
||
|
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
$dn = $entry['dn'];
|
||
|
unset($entry['dn']);
|
||
|
|
||
|
// Iterate over the attributes
|
||
|
foreach ($entry as $b => $attrs) {
|
||
|
// Remove the none entry references.
|
||
|
if (! is_array($attrs)) {
|
||
|
unset($entry[$b]);
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// Remove the count
|
||
|
if (isset($entry[$b]['count']))
|
||
|
unset($entry[$b]['count']);
|
||
|
}
|
||
|
|
||
|
// Our queries always include the DN (the only value not an array).
|
||
|
$entry['dn'] = $dn;
|
||
|
$result[$base][$dn] = $entry;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Sort our results
|
||
|
if (isset($result[$base]))
|
||
|
foreach ($result[$base] as $key => $values)
|
||
|
ksort($result[$base][$key]);
|
||
|
}
|
||
|
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function size_limit($val) {
|
||
|
$this->_size_limit = $val;
|
||
|
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
public function scope($val) {
|
||
|
switch ($val) {
|
||
|
case 'base':
|
||
|
case 'sub':
|
||
|
case 'one': $this->_scope = $val;
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
throw new Kohana_Exception('Unknown search scope :scope',array(':scope',$val));
|
||
|
}
|
||
|
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
public function time_limit($val) {
|
||
|
$this->_time_limit = $val;
|
||
|
|
||
|
return $this;
|
||
|
}
|
||
|
}
|
||
|
?>
|