This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
lnldap/classes/Kohana/Database/LDAP/Result.php
2013-07-12 13:16:17 +10:00

86 lines
2.4 KiB
PHP

<?php defined('SYSPATH') OR die('No direct script access.');
/**
* LDAP database result. See [Results](/database/results) for usage and examples.
*
* @package Kohana/LDAP
* @category Query/Result
* @author Deon George
* @copyright (c) 2013 phpLDAPadmin Development Team
* @license http://dev.phpldapadmin.org/license.html
*/
class Kohana_Database_LDAP_Result extends Database_Result {
private $_current_entry = NULL;
public function __construct($result, $sql, $as_object = FALSE, array $params = NULL) {
parent::__construct($result, $sql, $as_object, $params);
// Find the number of rows in the result
$this->_total_rows = ldap_count_entries($result['l'],$result['r']);
$this->_current_entry = ldap_first_entry($result['l'],$result['r']);
if (! $this->_total_rows)
$this->__destruct();
}
public function __destruct() {
if (is_resource($this->_result['r']))
ldap_free_result($this->_result['r']);
}
public function current() {
if ($this->_as_object === TRUE) {
// Return an stdClass
throw HTTP_Exception::factory(501,'Not implemented');
} elseif (is_string($this->_as_object)) {
// Return an object of given class name
throw HTTP_Exception::factory(501,'Not implemented');
} else {
// Return an array of the row
// @todo We could probably cache this for optimisation
$attrs = $vals = array();
array_push($attrs,ldap_first_attribute($this->_result['l'],$this->_current_entry));
while ($x = ldap_next_attribute($this->_result['l'],$this->_current_entry))
array_push($attrs,$x);
foreach ($attrs as $attr) {
$vals[strtolower($attr)] = ldap_get_values($this->_result['l'],$this->_current_entry,$attr);
// We dont need the count entry
unset($vals[strtolower($attr)]['count']);
}
return $vals;
}
}
/**
* Implements [Iterator::key], returns the dn.
*
* echo key($result);
*
* @return integer
*/
public function key() {
return ldap_get_dn($this->_result['l'],$this->_current_entry);
}
public function seek($dn) {
$this->_current_entry = ldap_first_entry($this->_result['l'],$this->_result['r']);
do {
if ($dn == $this->key())
return TRUE;
// Increment internal row for optimization assuming rows are fetched in order
$this->_current_entry = ldap_next_entry($this->_result['l'],$this->_current_entry);
} while ($this->_current_entry);
// We didnt find it
return FALSE;
}
} // End Database_LDAP_Result