📄 ldap.php
字号:
*/ var $connection; /** * base dn * @access private */ var $base = ''; /** * query base dn * @access private */ var $q_base = ''; /** * array of LDAP actions that only manipulate data * returning a true/false value * @access private */ var $manip = array('add', 'compare', 'delete', 'modify', 'mod_add', 'mod_del', 'mod_replace', 'rename'); /** * store the real LDAP action to perform * (ie PHP ldap function to call) for a query * @access private */ var $q_action = ''; /** * store optional parameters passed * to the real LDAP action * @access private */ var $q_params = array(); // }}} /** * Constructor, calls DB_common constructor * * @see DB_common::DB_common() */ function DB_ldap() { $this->DB_common(); $this->phptype = 'ldap'; $this->dbsyntax = 'ldap'; $this->features = array( 'prepare' => false, 'pconnect' => false, 'transactions' => false, 'limit' => false ); } /** * Connect and bind to LDAP server with either anonymous or authenticated bind depending on dsn info * * @param array $dsninfo dsn info as passed by DB::connect() * @param boolean $persistent kept for interface compatibility * @return DB_OK if successfully connected. A DB error code is returned on failure. */ function connect($dsninfo, $persistent = false) { if (!DB::assertExtension('ldap')) return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND); $this->dsn = $dsninfo; $user = $dsninfo['username']; $pw = $dsninfo['password']; $host = $dsninfo['hostspec']; $this->base = $dsninfo['database']; if ($host) { $conn = ldap_connect($host); } else { return $this->raiseError("unknown host $host"); } if (!$conn) { return $this->raiseError(DB_ERROR_CONNECT_FAILED); } if ($user && $pw) { $bind = ldap_bind($conn, "${user}," . $this->base, $pw); } else { $bind = ldap_bind($conn); } if (!$bind) { return $this->raiseError(DB_ERROR_BIND_FAILED); } $this->connection = $conn; return DB_OK; } /** * Unbinds from LDAP server * * @return int ldap_unbind() return value */ function disconnect() { $ret = @ldap_unbind($this->connection); $this->connection = null; return $ret; } /** * Performs a request against the LDAP server * * The type of request (and the corresponding PHP ldap function called) * depend on two additional parameters, added in respect to the * DB_common interface. * * @param string $filter text of the request to send to the LDAP server * @param string $action type of request to perform, defaults to search (ldap_search()) * @param array $params array of additional parameters to pass to the PHP ldap function requested * @return result from ldap function or DB Error object if no result */ function simpleQuery($filter, $action = null, $params = null) { if ($action === null) { $action = (!empty($this->q_action) ? $this->q_action : 'search'); } if ($params === null) { $params = (count($this->q_params) > 0 ? $this->q_params : array()); } if (!$this->isManip($action)) { $base = $this->q_base ? $this->q_base : $this->base; $attributes = array(); $attrsonly = 0; $sizelimit = 0; $timelimit = 0; $deref = LDAP_DEREF_NEVER; $sorting = ''; $sorting_method = ''; reset($params); while (list($k, $v) = each($params)) { if (isset(${$k})) ${$k} = $v; } $this->sorting = $sorting; $this->sorting_method = $sorting_method; $this->attributes = $attributes; if ($action == 'search') $result = @ldap_search($this->connection, $base, $filter, $attributes, $attrsonly, $sizelimit, $timelimit, $deref); else if ($action == 'list') $result = @ldap_list($this->connection, $base, $filter, $attributes, $attrsonly, $sizelimit, $timelimit, $deref); else if ($action == 'read') $result = @ldap_read($this->connection, $base, $filter, $attributes, $attrsonly, $sizelimit, $timelimit, $deref); else return $this->raiseError(DB_ERROR_UNKNOWN_LDAP_ACTION); if (!$result) { return $this->raiseError(); } } else { # If first argument is an array, it contains the entry with DN. if (is_array($filter)) { $entry = $filter; $filter = $entry["dn"]; } else { $entry = array(); } unset($entry["dn"]); $attribute = ''; $value = ''; $newrdn = ''; $newparent = ''; $deleteoldrdn = false; reset($params); while (list($k, $v) = each($params)) { if (isset(${$k})) ${$k} = $v; } if ($action == 'add') $result = @ldap_add($this->connection, $filter, $entry); else if ($action == 'compare') $result = @ldap_add($this->connection, $filter, $attribute, $value); else if ($action == 'delete') $result = @ldap_delete($this->connection, $filter); else if ($action == 'modify') $result = @ldap_modify($this->connection, $filter, $entry); else if ($action == 'mod_add') $result = @ldap_mod_add($this->connection, $filter, $entry); else if ($action == 'mod_del') $result = @ldap_mod_del($this->connection, $filter, $entry); else if ($action == 'mod_replace') $result = @ldap_mod_replace($this->connection, $filter, $entry); else if ($action == 'rename') $result = @ldap_rename($this->connection, $filter, $newrdn, $newparent, $deleteoldrdn); else return $this->raiseError(DB_ERROR_UNKNOWN_LDAP_ACTION); if (!$result) { return $this->raiseError(); } } $this->freeQuery(); return $result; } /** * Executes a query performing variables substitution in the query text * * @param string $stmt text of the request to send to the LDAP server * @param array $data query variables values to substitute * @param string $action type of request to perform, defaults to search (ldap_search()) * @param array $params array of additional parameters to pass to the PHP ldap function requested * @return LDAP_result object or DB Error object if no result * @see DB_common::executeEmulateQuery $this->simpleQuery() */ function execute($stmt, $data = false, $action = 'search', $params = array()) { $this->q_action = $action; $this->q_params = $params; $realquery = $this->executeEmulateQuery($stmt, $data); if (DB::isError($realquery)) { return $realquery; } $result = $this->simpleQuery($realquery); if (DB::isError($result) || $result === DB_OK) { return $result; } else { return new LDAP_result($this, $result); } } /** * Executes multiple queries performing variables substitution for each query * * @param string $stmt text of the request to send to the LDAP server * @param array $data query variables values to substitute * @param string $action type of request to perform, defaults to search (ldap_search()) * @param array $params array of additional parameters to pass to the PHP ldap function requested * @return LDAP_result object or DB Error object if no result * @see DB_common::executeMultiple */ function executeMultiple($stmt, &$data, $action = 'search', $params = array()) { $this->q_action = $action; $this->q_params = $params; return(parent::executeMultiple($stmt, $data)); } /** * Executes a query substituting variables if any are present * * @param string $query text of the request to send to the LDAP server * @param array $data query variables values to substitute * @param string $action type of request to perform, defaults to search (ldap_search()) * @param array $params array of additional parameters to pass to the PHP ldap function requested * @return LDAP_result object or DB Error object if no result * @see DB_common::prepare() $this->execute()$this->simpleQuery() */ function &query($query, $data = array(), $action = 'search', $params = array()) { $this->q_action = $action; $this->q_params = $params; if (sizeof($data) > 0) { $sth = $this->prepare($query); if (DB::isError($sth)) { return $sth; } return $this->execute($sth, $data); } else { $result = $this->simpleQuery($query); if (DB::isError($result) || $result === DB_OK) { return $result; } else { return new LDAP_result($this, $result); } } } /** * Modifies a query to return only a set of rows, stores $from and $count for LDAP_result * * @param string $query text of the request to send to the LDAP server * @param int $from record position from which to start returning data * @param int $count number of records to return * @return modified query text (no modifications are made, see above) */ function modifyLimitQuery($query, $from, $count) { $this->limit_from = $from; $this->limit_count = $count; return $query; } /** * Executes a query returning only a specified number of rows * * This method only saves the $from and $count parameters for LDAP_result * where the actual records processing takes place * * @param string $query text of the request to send to the LDAP server * @param int $from record position from which to start returning data * @param int $count number of records to return * @param string $action type of request to perform, defaults to search (ldap_search()) * @param array $params array of additional parameters to pass to the PHP ldap function requested * @return LDAP_result object or DB Error object if no result */ function limitQuery($query, $from, $count, $action = 'search', $params = array()) { $query = $this->modifyLimitQuery($query, $from, $count); $this->q_action = $action; $this->q_params = $params; return $this->query($query, $action, $params); } /** * Fetch the first column of the first row of data returned from * a query. Takes care of doing the query and freeing the results * when finished. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -