📄 ldap.php
字号:
<?php//// Pear DB LDAP - Database independent query interface definition// for PHP's LDAP extension.//// Copyright (C) 2002 Ludovico Magnocavallo <ludo@sumatrasolutions.com>//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//// Contributors// - Piotr Roszatycki <Piotr_Roszatycki@netia.net.pl>// DB_ldap::base() method, support for LDAP sequences, various fixes//// $Id: ldap.php,v 1.2 2004/04/26 20:44:37 rurban Exp $//// Based on DB 1.3 from the pear.php.net repository. // The only modifications made have been modification of the include paths. //rcs_id('$Id: ldap.php,v 1.2 2004/04/26 20:44:37 rurban Exp $');rcs_id('From Pear CVS: Id: ldap.php,v 1.9 2002/02/11 12:59:37 mj Exp');require_once 'DB/common.php';define("DB_ERROR_BIND_FAILED", -26);define("DB_ERROR_UNKNOWN_LDAP_ACTION", -27);/** * LDAP result class * * LDAP_result extends DB_result to provide specific LDAP * result methods. * * @version 1.0 * @author Ludovico Magnocavallo <ludo@sumatrasolutions.com> * @package DB */class LDAP_result extends DB_result{ // {{{ properties /** * data returned from ldap_entries() * @access private */ var $_entries = null; /** * result rows as hash of records * @access private */ var $_recordset = null; /** * current record as hash * @access private */ var $_record = null; // }}} // {{{ constructor /** * class constructor, calls DB_result constructor * @param ref $dbh reference to the db instance * @param resource $result ldap command result */ function LDAP_result(&$dbh, $result) { $this->DB_result($dbh, $result); } /** * fetch rows of data into $this->_recordset * * called once as soon as something needs to be returned * @access private * @param resource $result ldap command result * @return boolean true */ function getRows() { if ($this->_recordset === null) { // begin processing result into recordset $this->_entries = ldap_get_entries($this->dbh->connection, $this->result); $this->row_counter = $this->_entries['count']; $i = 1; $rs_template = array(); if (count($this->dbh->attributes) > 0) { reset($this->dbh->attributes); while (list($a_index, $a_name) = each($this->dbh->attributes)) $rs_template[$a_name] = ''; } while (list($entry_idx, $entry) = each($this->_entries)) { // begin first loop, iterate through entries if (!empty($this->dbh->limit_from) && ($i < $this->dbh->limit_from)) continue; if (!empty($this->dbh->limit_count) && ($i > $this->dbh->limit_count)) break; $rs = $rs_template; if (!is_array($entry)) continue; while (list($attr, $attr_values) = each($entry)) { // begin second loop, iterate through attributes if (is_int($attr) || $attr == 'count') continue; if (is_string($attr_values)) $rs[$attr] = $attr_values; else { $value = ''; while (list($value_idx, $attr_value) = each($attr_values)) { // begin third loop, iterate through attribute values if (!is_int($value_idx)) continue; if (empty($value)) $value = $attr_value; else { if (is_array($value)) $value[] = $attr_value; else $value = array($value, $attr_value); }// else $value .= "\n$attr_value"; // end third loop } $rs[$attr] = $value; } // end second loop } reset($rs); $this->_recordset[$entry_idx] = $rs; $i++; // end first loop } $this->_entries = null; if (!is_array($this->_recordset)) $this->_recordset = array(); if (!empty($this->dbh->sorting)) { $sorting_method = (!empty($this->dbh->sorting_method) ? $this->dbh->sorting_method : 'cmp'); uksort($this->_recordset, array(&$this, $sorting_method)); } reset($this->_recordset); // end processing result into recordset } return DB_OK; } /** * Fetch and return a row of data (it uses driver->fetchInto for that) * @param int $fetchmode format of fetched row * @param int $rownum the row number to fetch * * @return array a row of data, NULL on no more rows or PEAR_Error on error * * @access public */ function &fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) { $this->getRows(); if (count($this->_recordset) == 0) return null; if ($this->_record !== null) $this->_record = next($this->_recordset); else $this->_record = current($this->_recordset); $row = $this->_record; return $row; } /** * Fetch a row of data into an existing variable. * * @param mixed $arr reference to data containing the row * @param integer $fetchmode format of fetched row * @param integer $rownum the row number to fetch * * @return mixed DB_OK on success, NULL on no more rows or * a DB_Error object on error * * @access public */ function fetchInto(&$ar, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) { $this->getRows(); if ($this->_record !== null) $this->_record = next($this->_recordset); else $this->_record = current($this->_recordset); $ar = $this->_record; if (!$ar) { return null; } return DB_OK; } /** * return all records * * returns a hash of all records, basically returning * a copy of $this->_recordset * @param integer $fetchmode format of fetched row * @param integer $rownum the row number to fetch (not used, here for interface compatibility) * * @return mixed DB_OK on success, NULL on no more rows or * a DB_Error object on error * * @access public */ function fetchAll($fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) { $this->getRows(); return($this->_recordset); } /** * Get the the number of columns in a result set. * * @return int the number of columns, or a DB error * * @access public */ function numCols($result) { $this->getRows(); return(count(array_keys($this->_record))); } function cmp($a, $b) { return(strcmp(strtolower($this->_recordset[$a][$this->dbh->sorting]), strtolower($this->_recordset[$b][$this->dbh->sorting]))); } /** * Get the number of rows in a result set. * * @return int the number of rows, or a DB error * * @access public */ function numRows() { $this->getRows(); return $this->row_counter; } /** * Get the next result if a batch of queries was executed. * * @return bool true if a new result is available or false if not. * * @access public */ function nextResult() { return $this->dbh->nextResult($this->result); } /** * Frees the resources allocated for this result set. * @return int error code * * @access public */ function free() { $this->_recordset = null; $this->_record = null; ldap_free_result($this->result); $this->result = null; return true; } /** * @deprecated */ function tableInfo($mode = null) { return $this->dbh->tableInfo($this->result, $mode); } /** * returns the actual rows number * @return integer */ function getRowCounter() { $this->getRows(); return $this->row_counter; }}/** * LDAP DB interface class * * LDAP extends DB_common to provide DB compliant * access to LDAP servers * * @version 1.0 * @author Ludovico Magnocavallo <ludo@sumatrasolutions.com> * @package DB */class DB_ldap extends DB_common{ // {{{ properties /** * LDAP connection * @access private
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -