_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)); 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']); } $vals['dn'] = $this->key(); 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; return $x->result_load_values($vals); } else { return $vals; } } /** * Implements [Iterator::key], returns the dn. * * echo key($result); * * @return string LDAP Distringuised Name */ public function key() { return ldap_get_dn($this->_result['l'],$this->_current_entry); } /** * Implements [Iterator::next], moves to the next row. * * next($result); * * @return $this */ public function next() { $this->_current_entry = ldap_next_entry($this->_result['l'],$this->_current_entry); ++$this->_current_row; return $this; } /** * @override We cant go backwards! */ public function prev() { throw HTTP_Exception::factory(501,'Cant go backwards :method',array(':method'=>__METHOD__)); } /** * 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 */ 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; } /** * Implements [Iterator::valid], checks if the current row exists. * * [!!] This method is only used internally. * * @return boolean */ public function valid() { return (boolean)$this->_current_entry; } /** 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) array_push($result,$row); } elseif ($key === NULL) { throw HTTP_Exception::factory(501,'Not implemented :method',array(':method'=>__METHOD__)); // Indexed columns } elseif ($value === NULL) { throw HTTP_Exception::factory(501,'Not implemented :method',array(':method'=>__METHOD__)); // Associative rows } else { throw HTTP_Exception::factory(501,'Not implemented :method',array(':method'=>__METHOD__)); // Associative columns } return $result; } } // End Database_LDAP_Result ?>