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

189 lines
4.5 KiB
PHP
Raw Normal View History

2013-07-12 00:35:54 +00:00
<?php defined('SYSPATH') OR die('No direct script access.');
2013-08-16 02:21:17 +00:00
2013-07-12 00:35:54 +00:00
/**
* LDAP database result. See [Results](/database/results) for usage and examples.
*
2014-02-11 00:26:11 +00:00
* @package Kohana/Database
2013-07-12 00:35:54 +00:00
* @category Query/Result
* @author Deon George
* @copyright (c) 2013 phpLDAPadmin Development Team
* @license http://dev.phpldapadmin.org/license.html
*/
2013-08-16 02:21:17 +00:00
abstract class Kohana_Database_LDAP_Result extends Database_Result {
2013-07-12 00:35:54 +00:00
private $_current_entry = NULL;
2013-08-16 02:21:17 +00:00
/** SeekableIterator **/
2013-07-13 12:42:02 +00:00
2013-08-16 02:21:17 +00:00
/**
* Implements [Iterator::current], returns the current LDAP Leaf Entry.
*
* @return mixed LDAP leaf array of values
*/
2013-07-12 00:35:54 +00:00
public function current() {
2013-07-13 12:42:02 +00:00
if (! $this->_current_entry)
return array();
// 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));
2013-07-12 00:35:54 +00:00
while ($x = ldap_next_attribute($this->_result['l'],$this->_current_entry))
array_push($attrs,$x);
2013-07-12 00:35:54 +00:00
foreach ($attrs as $attr) {
$vals[strtolower($attr)] = ldap_get_values($this->_result['l'],$this->_current_entry,$attr);
2013-07-12 00:35:54 +00:00
// We dont need the count entry
unset($vals[strtolower($attr)]['count']);
}
2013-07-12 00:35:54 +00:00
$vals['dn'] = $this->key();
2013-07-12 00:35:54 +00:00
if ($this->_as_object === TRUE) {
// Return an stdClass
throw HTTP_Exception::factory(501,'Not implemented',array(':method'=>__METHOD__));
} elseif (is_string($this->_as_object)) {
$x = new $this->_as_object;
2013-07-12 00:35:54 +00:00
return $x->result_load_values($vals);
} else {
2013-07-12 00:35:54 +00:00
return $vals;
}
}
/**
* Implements [Iterator::key], returns the dn.
*
2013-08-16 02:21:17 +00:00
* echo key($result);
2013-07-12 00:35:54 +00:00
*
2013-08-16 02:21:17 +00:00
* @return string LDAP Distringuised Name
2013-07-12 00:35:54 +00:00
*/
public function key() {
return ldap_get_dn($this->_result['l'],$this->_current_entry);
}
2013-07-13 12:42:02 +00:00
/**
* Implements [Iterator::next], moves to the next row.
*
2013-08-16 02:21:17 +00:00
* next($result);
2013-07-13 12:42:02 +00:00
*
2013-08-16 02:21:17 +00:00
* @return $this
2013-07-13 12:42:02 +00:00
*/
public function next() {
$this->_current_entry = ldap_next_entry($this->_result['l'],$this->_current_entry);
++$this->_current_row;
return $this;
}
2013-08-16 02:21:17 +00:00
/**
* @override We cant go backwards!
*/
2013-07-13 12:42:02 +00:00
public function prev() {
2014-04-29 04:26:56 +00:00
throw HTTP_Exception::factory(501,'Cant go backwards :method',array(':method'=>__METHOD__));
2013-07-13 12:42:02 +00:00
}
2013-08-16 02:21:17 +00:00
/**
* Implements [Iterator::rewind], moves to the first entry.
*
* reset($result);
*
* @return void
*/
public function rewind() {
if ($this->_total_rows)
$this->_current_entry = ldap_first_entry($this->_result['l'],$this->_result['r']);
$this->_current_row = 0;
}
/**
* Implements [SeekableIterator::seek], to seek to an entry.
*
* [!!] This method is only used internally.
*
* @return boolean
*/
2013-07-12 00:35:54 +00:00
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;
}
2013-07-13 12:42:02 +00:00
/**
* Implements [Iterator::valid], checks if the current row exists.
*
* [!!] This method is only used internally.
*
2013-08-16 02:21:17 +00:00
* @return boolean
2013-07-13 12:42:02 +00:00
*/
public function valid() {
return (boolean)$this->_current_entry;
}
2013-08-16 02:21:17 +00:00
/** ArrayAccess **/
public function offsetExists($offset) {
return $this->valid();
}
/** Custom Methods **/
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 as_array($key=NULL,$value=NULL) {
$result = array();
if ($key === NULL AND $value === NULL) {
// Indexed rows
foreach ($this as $dn => $row)
2014-04-29 04:26:56 +00:00
array_push($result,$row);
2013-08-16 02:21:17 +00:00
} elseif ($key === NULL) {
throw HTTP_Exception::factory(501,'Not implemented :method',array(':method'=>__METHOD__));
2013-08-16 02:21:17 +00:00
// Indexed columns
} elseif ($value === NULL) {
throw HTTP_Exception::factory(501,'Not implemented :method',array(':method'=>__METHOD__));
2013-08-16 02:21:17 +00:00
// Associative rows
} else {
throw HTTP_Exception::factory(501,'Not implemented :method',array(':method'=>__METHOD__));
2013-08-16 02:21:17 +00:00
// Associative columns
}
return $result;
}
2013-07-12 00:35:54 +00:00
} // End Database_LDAP_Result
2013-08-16 02:21:17 +00:00
?>