123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803 |
- <?php
- require_once 'PEAR.php';
- require_once 'Net/LDAP2/RootDSE.php';
- require_once 'Net/LDAP2/Schema.php';
- require_once 'Net/LDAP2/Entry.php';
- require_once 'Net/LDAP2/Search.php';
- require_once 'Net/LDAP2/Util.php';
- require_once 'Net/LDAP2/Filter.php';
- require_once 'Net/LDAP2/LDIF.php';
- require_once 'Net/LDAP2/SchemaCache.interface.php';
- require_once 'Net/LDAP2/SimpleFileSchemaCache.php';
- define('NET_LDAP2_ERROR', 1000);
- define('NET_LDAP2_VERSION', '2.1.0');
- class Net_LDAP2 extends PEAR
- {
-
- protected $_config = array('host' => 'localhost',
- 'port' => 389,
- 'version' => 3,
- 'starttls' => false,
- 'binddn' => '',
- 'bindpw' => '',
- 'basedn' => '',
- 'options' => array(),
- 'filter' => '(objectClass=*)',
- 'scope' => 'sub',
- 'auto_reconnect' => false,
- 'min_backoff' => 1,
- 'current_backoff' => 1,
- 'max_backoff' => 32);
-
- protected $_host_list = array();
-
- protected $_down_host_list = array();
-
- protected $_link = false;
-
- protected $_schema = null;
-
- protected $_schema_cache = null;
-
- protected $_schemaAttrs = array();
-
- protected $_rootDSE_cache = array();
-
- public static function getVersion()
- {
- return NET_LDAP2_VERSION;
- }
-
- public static function connect($config = array())
- {
- $ldap_check = self::checkLDAPExtension();
- if (self::iserror($ldap_check)) {
- return $ldap_check;
- }
- @$obj = new Net_LDAP2($config);
-
-
- $err = $obj->bind();
- if (self::isError($err)) {
- return $err;
- }
- return $obj;
- }
-
- public function __construct($config = array())
- {
- parent::__construct('Net_LDAP2_Error');
- $this->setConfig($config);
- }
-
- protected function setConfig($config)
- {
-
-
-
-
- if (! is_array($config)) {
- return;
- }
- foreach ($config as $k => $v) {
- if (isset($this->_config[$k])) {
- $this->_config[$k] = $v;
- } else {
-
- switch($k) {
- case "dn":
- $this->_config["binddn"] = $v;
- break;
- case "password":
- $this->_config["bindpw"] = $v;
- break;
- case "tls":
- $this->_config["starttls"] = $v;
- break;
- case "base":
- $this->_config["basedn"] = $v;
- break;
- }
- }
- }
-
-
-
- if (is_array($this->_config['host'])) {
- $this->_host_list = $this->_config['host'];
- } else {
- if (strlen($this->_config['host']) > 0) {
- $this->_host_list = array($this->_config['host']);
- } else {
- $this->_host_list = array();
-
-
- }
- }
-
-
-
-
- $this->_down_host_list = array();
- }
-
- public function bind($dn = null, $password = null)
- {
-
- if (is_null($dn)) {
- $dn = $this->_config["binddn"];
- }
- if (is_null($password)) {
- $password = $this->_config["bindpw"];
- }
-
-
- if ($this->_link === false) {
-
-
- $olddn = $this->_config["binddn"];
- $oldpw = $this->_config["bindpw"];
-
-
- $this->_config["binddn"] = $dn;
- $this->_config["bindpw"] = $password;
-
- $msg = $this->performConnect();
-
- $this->_config["binddn"] = $olddn;
- $this->_config["bindpw"] = $oldpw;
-
- if (self::isError($msg)) {
- return $msg;
- }
- } else {
-
-
- if (is_null($dn)) {
-
- $msg = @ldap_bind($this->_link);
- } else {
-
- $msg = @ldap_bind($this->_link, $dn, $password);
- }
- if (false === $msg) {
- return PEAR::raiseError("Bind failed: " .
- @ldap_error($this->_link),
- @ldap_errno($this->_link));
- }
- }
- return true;
- }
-
- protected function performConnect()
- {
-
-
-
-
-
-
-
-
-
-
- if ($this->_link !== false) {
- return true;
- }
-
-
-
-
-
-
-
-
- $current_error = new PEAR_Error('Unknown connection error');
-
- if (!is_array($this->_host_list) || count($this->_host_list) == 0) {
- $current_error = PEAR::raiseError('No Servers configured! Please '.
- 'pass in an array of servers to Net_LDAP2');
- return $current_error;
- }
-
- foreach ($this->_host_list as $host) {
-
- if (is_array($host)) {
- $current_error = PEAR::raiseError('No Servers configured! '.
- 'Please pass in an one dimensional array of servers to '.
- 'Net_LDAP2! (multidimensional array detected!)');
- continue;
- }
-
- if (in_array($host, $this->_down_host_list)) {
- continue;
- }
-
-
- $this->_config['host'] = $host;
-
- $this->_link = @ldap_connect($host, $this->_config['port']);
- if (false === $this->_link) {
- $current_error = PEAR::raiseError('Could not connect to ' .
- $host . ':' . $this->_config['port']);
- $this->_down_host_list[] = $host;
- continue;
- }
-
-
- if ($this->_config["starttls"] === true) {
- if (self::isError($msg = $this->startTLS())) {
- $current_error = $msg;
- $this->_link = false;
- $this->_down_host_list[] = $host;
- continue;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- $version_set = false;
- $ignored_err = $this->setLDAPVersion(0, true);
- if (!self::isError($ignored_err)) {
- $version_set = true;
- }
-
-
-
-
-
-
-
-
- $msg = $this->bind();
- if (self::isError($msg)) {
-
-
- if ($msg->getCode() == 0x02 && !$version_set) {
-
-
- $msg = new Net_LDAP2_Error($msg->getMessage().
- " (could not set LDAP protocol version to ".
- $this->_config['version'].")",
- $msg->getCode());
- }
- $this->_link = false;
- $current_error = $msg;
- $this->_down_host_list[] = $host;
- continue;
- }
-
-
-
-
-
-
- if (!$version_set) {
- if (self::isError($msg = $this->setLDAPVersion())) {
- $current_error = $msg;
- $this->_link = false;
- $this->_down_host_list[] = $host;
- continue;
- }
- }
-
- if (isset($this->_config['options']) &&
- is_array($this->_config['options']) &&
- count($this->_config['options'])) {
- foreach ($this->_config['options'] as $opt => $val) {
- $err = $this->setOption($opt, $val);
- if (self::isError($err)) {
- $current_error = $err;
- $this->_link = false;
- $this->_down_host_list[] = $host;
- continue 2;
- }
- }
- }
-
-
- return true;
- }
-
- return $current_error;
- }
-
- protected function performReconnect()
- {
-
- if ($this->_link !== false) {
- return true;
- }
-
-
- $current_error = new PEAR_Error('Unknown connection error');
-
- sleep($this->_config['current_backoff']);
-
- $this->_down_host_list = array();
- $msg = $this->performConnect();
-
- if (self::isError($msg)) {
- $this->_config['current_backoff'] =
- $this->_config['current_backoff'] * 2;
- if ($this->_config['current_backoff'] > $this->_config['max_backoff']) {
- $this->_config['current_backoff'] = $this->_config['max_backoff'];
- }
- return $msg;
- }
-
- $msg = $this->bind();
- if (self::isError($msg)) {
- $this->_config['current_backoff'] = $this->_config['current_backoff'] * 2;
- if ($this->_config['current_backoff'] > $this->_config['max_backoff']) {
- $this->_config['current_backoff'] = $this->_config['max_backoff'];
- }
-
-
-
- $this->_down_host_list[] = $this->_config['host'];
- return $msg;
- }
-
-
- $this->_config['current_backoff'] = $this->_config['min_backoff'];
- return true;
- }
-
- public function startTLS()
- {
-
- $rootDSE = $this->rootDse();
- if (self::isError($rootDSE)) {
-
-
-
- } else {
-
- $supported_extensions = $rootDSE->getValue('supportedExtension');
- if (self::isError($supported_extensions)) {
-
-
-
- } else {
-
-
-
- if (!in_array('1.3.6.1.4.1.1466.20037', $supported_extensions)) {
- return $this->raiseError("Server reports that it does not support TLS.");
- }
- }
- }
-
- if (false === @ldap_start_tls($this->_link)) {
-
-
-
- return $this->raiseError("TLS could not be started: " .
- @ldap_error($this->_link),
- @ldap_errno($this->_link));
- } else {
- return true;
- }
- }
-
- public function start_tls()
- {
- $args = func_get_args();
- return call_user_func_array(array( $this, 'startTLS' ), $args);
- }
-
- public function done()
- {
- $this->_Net_LDAP2();
- }
-
- public function disconnect()
- {
- $this->done();
- }
-
- public function _Net_LDAP2()
- {
- @ldap_close($this->_link);
- }
-
- public function add($entry)
- {
- if (!$entry instanceof Net_LDAP2_Entry) {
- return PEAR::raiseError('Parameter to Net_LDAP2::add() must be a Net_LDAP2_Entry object.');
- }
-
-
- $foo = 0;
- while (true) {
- $link = $this->getLink();
- if ($link === false) {
-
-
-
- return PEAR::raiseError("Could not add entry " . $entry->dn() .
- " no valid LDAP connection could be found.");
- }
- if (@ldap_add($link, $entry->dn(), $entry->getValues())) {
-
-
- if (!$entry->getLDAP() instanceof Net_LDAP2) {
- $entry->setLDAP($this);
- }
-
- $entry->markAsNew(false);
- return true;
- } else {
-
-
- $error_code = @ldap_errno($link);
- $error_name = Net_LDAP2::errorMessage($error_code);
- if (($error_name === 'LDAP_OPERATIONS_ERROR') &&
- ($this->_config['auto_reconnect'])) {
-
-
-
- $this->_link = false;
- $this->performReconnect();
- } else {
-
-
- return PEAR::raiseError("Could not add entry " . $entry->dn() . " " .
- $error_name,
- $error_code);
- }
- }
- }
- }
-
- public function delete($dn, $recursive = false)
- {
- if ($dn instanceof Net_LDAP2_Entry) {
- $dn = $dn->dn();
- }
- if (false === is_string($dn)) {
- return PEAR::raiseError("Parameter is not a string nor an entry object!");
- }
-
- if ($recursive) {
- $result = @ldap_list($this->_link, $dn, '(objectClass=*)', array(null), 0, 0);
- if (@ldap_count_entries($this->_link, $result)) {
- $subentry = @ldap_first_entry($this->_link, $result);
- $this->delete(@ldap_get_dn($this->_link, $subentry), true);
- while ($subentry = @ldap_next_entry($this->_link, $subentry)) {
- $this->delete(@ldap_get_dn($this->_link, $subentry), true);
- }
- }
- }
-
-
- while (true) {
- $link = $this->getLink();
- if ($link === false) {
-
-
-
- return PEAR::raiseError("Could not add entry " . $dn .
- " no valid LDAP connection could be found.");
- }
- if (@ldap_delete($link, $dn)) {
-
- return true;
- } else {
-
-
- $error_code = @ldap_errno($link);
- $error_name = Net_LDAP2::errorMessage($error_code);
- if ((Net_LDAP2::errorMessage($error_code) === 'LDAP_OPERATIONS_ERROR') &&
- ($this->_config['auto_reconnect'])) {
-
-
-
- $this->_link = false;
- $this->performReconnect();
- } elseif ($error_code == 66) {
-
-
-
-
-
- return PEAR::raiseError("Could not delete entry $dn because of subentries. Use the recursive parameter to delete them.");
- } else {
-
-
- return PEAR::raiseError("Could not delete entry " . $dn . " " .
- $error_name,
- $error_code);
- }
- }
- }
- }
-
- public function modify($entry, $parms = array())
- {
- if (is_string($entry)) {
- $entry = $this->getEntry($entry);
- if (self::isError($entry)) {
- return $entry;
- }
- }
- if (!$entry instanceof Net_LDAP2_Entry) {
- return PEAR::raiseError("Parameter is not a string nor an entry object!");
- }
-
- foreach (array('add', 'delete', 'replace') as $action) {
- if (isset($parms[$action])) {
- $msg = $entry->$action($parms[$action]);
- if (self::isError($msg)) {
- return $msg;
- }
- $entry->setLDAP($this);
-
-
-
- while (true) {
- $msg = $entry->update();
- if (self::isError($msg)) {
-
-
- $error_code = $msg->getCode();
- $error_name = Net_LDAP2::errorMessage($error_code);
- if ((Net_LDAP2::errorMessage($error_code) === 'LDAP_OPERATIONS_ERROR') &&
- ($this->_config['auto_reconnect'])) {
-
-
-
- $this->_link = false;
- $this->performReconnect();
- } else {
-
-
- return PEAR::raiseError("Could not modify entry: ".$msg->getMessage());
- }
- } else {
-
- break;
- }
- }
- }
- }
-
- if (isset($parms['changes']) && is_array($parms['changes'])) {
- foreach ($parms['changes'] as $action => $value) {
-
-
-
- while (true) {
- $msg = $this->modify($entry, array($action => $value));
- if (self::isError($msg)) {
-
-
- $error_code = $msg->getCode();
- $error_name = Net_LDAP2::errorMessage($error_code);
- if ((Net_LDAP2::errorMessage($error_code) === 'LDAP_OPERATIONS_ERROR') &&
- ($this->_config['auto_reconnect'])) {
-
-
-
- $this->_link = false;
- $this->performReconnect();
- } else {
-
-
- return $msg;
- }
- } else {
-
- break;
- }
- }
- }
- }
- return true;
- }
-
- public function search($base = null, $filter = null, $params = array())
- {
- if (is_null($base)) {
- $base = $this->_config['basedn'];
- }
- if ($base instanceof Net_LDAP2_Entry) {
- $base = $base->dn();
- }
- if (is_null($filter)) {
- $filter = $this->_config['filter'];
- }
- if ($filter instanceof Net_LDAP2_Filter) {
- $filter = $filter->asString();
- }
- if (PEAR::isError($filter)) {
- return $filter;
- }
- if (PEAR::isError($base)) {
- return $base;
- }
-
- (isset($params['sizelimit'])) ? $sizelimit = $params['sizelimit'] : $sizelimit = 0;
- (isset($params['timelimit'])) ? $timelimit = $params['timelimit'] : $timelimit = 0;
- (isset($params['attrsonly'])) ? $attrsonly = $params['attrsonly'] : $attrsonly = 0;
- (isset($params['attributes'])) ? $attributes = $params['attributes'] : $attributes = array();
-
-
- if (!is_array($attributes)) {
- $attributes = array($attributes);
- }
-
-
- $attributes = array_values($attributes);
-
- $scope = (isset($params['scope']) ? $params['scope'] : $this->_config['scope']);
- switch ($scope) {
- case 'one':
- $search_function = 'ldap_list';
- break;
- case 'base':
- $search_function = 'ldap_read';
- break;
- default:
- $search_function = 'ldap_search';
- }
-
-
- while (true) {
- $link = $this->getLink();
- $search = @call_user_func($search_function,
- $link,
- $base,
- $filter,
- $attributes,
- $attrsonly,
- $sizelimit,
- $timelimit);
- if ($err = @ldap_errno($link)) {
- if ($err == 32) {
-
- return $obj = new Net_LDAP2_Search ($search, $this, $attributes);
- } elseif ($err == 4) {
-
- return $obj = new Net_LDAP2_Search ($search, $this, $attributes);
- } elseif ($err == 87) {
-
- return $this->raiseError(Net_LDAP2::errorMessage($err) . "($filter)", $err);
- } elseif (($err == 1) && ($this->_config['auto_reconnect'])) {
-
- $this->_link = false;
- $this->performReconnect();
- } else {
- $msg = "\nParameters:\nBase: $base\nFilter: $filter\nScope: $scope";
- return $this->raiseError(Net_LDAP2::errorMessage($err) . $msg, $err);
- }
- } else {
- return $obj = new Net_LDAP2_Search($search, $this, $attributes);
- }
- }
- }
-
- public function setOption($option, $value)
- {
- if ($this->_link) {
- if (defined($option)) {
- if (@ldap_set_option($this->_link, constant($option), $value)) {
- return true;
- } else {
- $err = @ldap_errno($this->_link);
- if ($err) {
- $msg = @ldap_err2str($err);
- } else {
- $err = NET_LDAP2_ERROR;
- $msg = Net_LDAP2::errorMessage($err);
- }
- return $this->raiseError($msg, $err);
- }
- } else {
- return $this->raiseError("Unkown Option requested");
- }
- } else {
- return $this->raiseError("Could not set LDAP option: No LDAP connection");
- }
- }
-
- public function getOption($option)
- {
- if ($this->_link) {
- if (defined($option)) {
- if (@ldap_get_option($this->_link, constant($option), $value)) {
- return $value;
- } else {
- $err = @ldap_errno($this->_link);
- if ($err) {
- $msg = @ldap_err2str($err);
- } else {
- $err = NET_LDAP2_ERROR;
- $msg = Net_LDAP2::errorMessage($err);
- }
- return $this->raiseError($msg, $err);
- }
- } else {
- $this->raiseError("Unkown Option requested");
- }
- } else {
- $this->raiseError("No LDAP connection");
- }
- }
-
- public function getLDAPVersion()
- {
- if ($this->_link) {
- $version = $this->getOption("LDAP_OPT_PROTOCOL_VERSION");
- } else {
- $version = $this->_config['version'];
- }
- return $version;
- }
-
- public function setLDAPVersion($version = 0, $force = false)
- {
- if (!$version) {
- $version = $this->_config['version'];
- }
-
-
-
-
-
-
-
-
- if (!$force) {
- $rootDSE = $this->rootDse();
- if ($rootDSE instanceof Net_LDAP2_Error) {
- return $rootDSE;
- } else {
- $supported_versions = $rootDSE->getValue('supportedLDAPVersion');
- if (is_string($supported_versions)) {
- $supported_versions = array($supported_versions);
- }
- $check_ok = in_array($version, $supported_versions);
- }
- }
- if ($force || $check_ok) {
- return $this->setOption("LDAP_OPT_PROTOCOL_VERSION", $version);
- } else {
- return $this->raiseError("LDAP Server does not support protocol version " . $version);
- }
- }
-
- public function dnExists($dn)
- {
- if (PEAR::isError($dn)) {
- return $dn;
- }
- if ($dn instanceof Net_LDAP2_Entry) {
- $dn = $dn->dn();
- }
- if (false === is_string($dn)) {
- return PEAR::raiseError('Parameter $dn is not a string nor an entry object!');
- }
-
-
- $s_opts = array(
- 'scope' => 'base',
- 'sizelimit' => 1,
- 'attributes' => '1.1'
- );
- $search = $this->search($dn, '(objectClass=*)', $s_opts);
- if (self::isError($search)) {
- return $search;
- }
-
- return ($search->count() == 0)? false : true;
- }
-
- public function getEntry($dn, $attr = array())
- {
- if (!is_array($attr)) {
- $attr = array($attr);
- }
- $result = $this->search($dn, '(objectClass=*)',
- array('scope' => 'base', 'attributes' => $attr));
- if (self::isError($result)) {
- return $result;
- } elseif ($result->count() == 0) {
- return PEAR::raiseError('Could not fetch entry '.$dn.': no entry found');
- }
- $entry = $result->shiftEntry();
- if (false == $entry) {
- return PEAR::raiseError('Could not fetch entry (error retrieving entry from search result)');
- }
- return $entry;
- }
-
- public function move($entry, $newdn, $target_ldap = null)
- {
- if (is_string($entry)) {
- $entry_o = $this->getEntry($entry);
- } else {
- $entry_o = $entry;
- }
- if (!$entry_o instanceof Net_LDAP2_Entry) {
- return PEAR::raiseError('Parameter $entry is expected to be a Net_LDAP2_Entry object! (If DN was passed, conversion failed)');
- }
- if (null !== $target_ldap && !$target_ldap instanceof Net_LDAP2) {
- return PEAR::raiseError('Parameter $target_ldap is expected to be a Net_LDAP2 object!');
- }
- if ($target_ldap && $target_ldap !== $this) {
-
- if (is_string($entry)) {
- return PEAR::raiseError('Unable to perform cross directory move: operation requires a Net_LDAP2_Entry object');
- }
- if ($target_ldap->dnExists($newdn)) {
- return PEAR::raiseError('Unable to perform cross directory move: entry does exist in target directory');
- }
- $entry_o->dn($newdn);
- $res = $target_ldap->add($entry_o);
- if (self::isError($res)) {
- return PEAR::raiseError('Unable to perform cross directory move: '.$res->getMessage().' in target directory');
- }
- $res = $this->delete($entry_o->currentDN());
- if (self::isError($res)) {
- $res2 = $target_ldap->delete($entry_o);
- if (self::isError($res2)) {
- $add_error_string = 'Additionally, the deletion (undo add) of $entry in target directory failed.';
- }
- return PEAR::raiseError('Unable to perform cross directory move: '.$res->getMessage().' in source directory. '.$add_error_string);
- }
- $entry_o->setLDAP($target_ldap);
- return true;
- } else {
-
- $entry_o->dn($newdn);
- $entry_o->setLDAP($this);
- return $entry_o->update();
- }
- }
-
- public function copy($entry, $newdn)
- {
- if (!$entry instanceof Net_LDAP2_Entry) {
- return PEAR::raiseError('Parameter $entry is expected to be a Net_LDAP2_Entry object!');
- }
- $newentry = Net_LDAP2_Entry::createFresh($newdn, $entry->getValues());
- $result = $this->add($newentry);
- if ($result instanceof Net_LDAP2_Error) {
- return $result;
- } else {
- return $newentry;
- }
- }
-
- public static function errorMessage($errorcode)
- {
- $errorMessages = array(
- 0x00 => "LDAP_SUCCESS",
- 0x01 => "LDAP_OPERATIONS_ERROR",
- 0x02 => "LDAP_PROTOCOL_ERROR",
- 0x03 => "LDAP_TIMELIMIT_EXCEEDED",
- 0x04 => "LDAP_SIZELIMIT_EXCEEDED",
- 0x05 => "LDAP_COMPARE_FALSE",
- 0x06 => "LDAP_COMPARE_TRUE",
- 0x07 => "LDAP_AUTH_METHOD_NOT_SUPPORTED",
- 0x08 => "LDAP_STRONG_AUTH_REQUIRED",
- 0x09 => "LDAP_PARTIAL_RESULTS",
- 0x0a => "LDAP_REFERRAL",
- 0x0b => "LDAP_ADMINLIMIT_EXCEEDED",
- 0x0c => "LDAP_UNAVAILABLE_CRITICAL_EXTENSION",
- 0x0d => "LDAP_CONFIDENTIALITY_REQUIRED",
- 0x0e => "LDAP_SASL_BIND_INPROGRESS",
- 0x10 => "LDAP_NO_SUCH_ATTRIBUTE",
- 0x11 => "LDAP_UNDEFINED_TYPE",
- 0x12 => "LDAP_INAPPROPRIATE_MATCHING",
- 0x13 => "LDAP_CONSTRAINT_VIOLATION",
- 0x14 => "LDAP_TYPE_OR_VALUE_EXISTS",
- 0x15 => "LDAP_INVALID_SYNTAX",
- 0x20 => "LDAP_NO_SUCH_OBJECT",
- 0x21 => "LDAP_ALIAS_PROBLEM",
- 0x22 => "LDAP_INVALID_DN_SYNTAX",
- 0x23 => "LDAP_IS_LEAF",
- 0x24 => "LDAP_ALIAS_DEREF_PROBLEM",
- 0x30 => "LDAP_INAPPROPRIATE_AUTH",
- 0x31 => "LDAP_INVALID_CREDENTIALS",
- 0x32 => "LDAP_INSUFFICIENT_ACCESS",
- 0x33 => "LDAP_BUSY",
- 0x34 => "LDAP_UNAVAILABLE",
- 0x35 => "LDAP_UNWILLING_TO_PERFORM",
- 0x36 => "LDAP_LOOP_DETECT",
- 0x3C => "LDAP_SORT_CONTROL_MISSING",
- 0x3D => "LDAP_INDEX_RANGE_ERROR",
- 0x40 => "LDAP_NAMING_VIOLATION",
- 0x41 => "LDAP_OBJECT_CLASS_VIOLATION",
- 0x42 => "LDAP_NOT_ALLOWED_ON_NONLEAF",
- 0x43 => "LDAP_NOT_ALLOWED_ON_RDN",
- 0x44 => "LDAP_ALREADY_EXISTS",
- 0x45 => "LDAP_NO_OBJECT_CLASS_MODS",
- 0x46 => "LDAP_RESULTS_TOO_LARGE",
- 0x47 => "LDAP_AFFECTS_MULTIPLE_DSAS",
- 0x50 => "LDAP_OTHER",
- 0x51 => "LDAP_SERVER_DOWN",
- 0x52 => "LDAP_LOCAL_ERROR",
- 0x53 => "LDAP_ENCODING_ERROR",
- 0x54 => "LDAP_DECODING_ERROR",
- 0x55 => "LDAP_TIMEOUT",
- 0x56 => "LDAP_AUTH_UNKNOWN",
- 0x57 => "LDAP_FILTER_ERROR",
- 0x58 => "LDAP_USER_CANCELLED",
- 0x59 => "LDAP_PARAM_ERROR",
- 0x5a => "LDAP_NO_MEMORY",
- 0x5b => "LDAP_CONNECT_ERROR",
- 0x5c => "LDAP_NOT_SUPPORTED",
- 0x5d => "LDAP_CONTROL_NOT_FOUND",
- 0x5e => "LDAP_NO_RESULTS_RETURNED",
- 0x5f => "LDAP_MORE_RESULTS_TO_RETURN",
- 0x60 => "LDAP_CLIENT_LOOP",
- 0x61 => "LDAP_REFERRAL_LIMIT_EXCEEDED",
- 1000 => "Unknown Net_LDAP2 Error"
- );
- return isset($errorMessages[$errorcode]) ?
- $errorMessages[$errorcode] :
- $errorMessages[NET_LDAP2_ERROR] . ' (' . $errorcode . ')';
- }
-
- public function rootDse($attrs = null)
- {
- if ($attrs !== null && !is_array($attrs)) {
- return PEAR::raiseError('Parameter $attr is expected to be an array!');
- }
- $attrs_signature = serialize($attrs);
-
-
- if (true || !array_key_exists($attrs_signature, $this->_rootDSE_cache)) {
- $rootdse = Net_LDAP2_RootDSE::fetch($this, $attrs);
- if ($rootdse instanceof Net_LDAP2_Error) {
- return $rootdse;
- }
-
- $this->_rootDSE_cache[$attrs_signature] = $rootdse;
- }
- return $this->_rootDSE_cache[$attrs_signature];
- }
-
- public function root_dse()
- {
- $args = func_get_args();
- return call_user_func_array(array($this, 'rootDse'), $args);
- }
-
- public function schema($dn = null)
- {
-
-
-
-
- if ($this->_schema === null) {
- if ($this->_schema_cache) {
- $cached_schema = $this->_schema_cache->loadSchema();
- if ($cached_schema instanceof Net_LDAP2_Error) {
- return $cached_schema;
- } else {
- if ($cached_schema instanceof Net_LDAP2_Schema) {
- $this->_schema = $cached_schema;
- }
- }
- }
- }
-
-
- if ($this->_schema === null) {
-
-
-
- $this->_schema = new Net_LDAP2_Error('Schema not initialized');
- $this->_schema = Net_LDAP2_Schema::fetch($this, $dn);
-
- if ($this->_schema_cache) {
- $caching_result = $this->_schema_cache->storeSchema($this->_schema);
- if ($caching_result instanceof Net_LDAP2_Error) {
- return $caching_result;
- }
- }
- }
- return $this->_schema;
- }
-
- public function registerSchemaCache($cache) {
- if (is_null($cache)
- || (is_object($cache) && in_array('Net_LDAP2_SchemaCache', class_implements($cache))) ) {
- $this->_schema_cache = $cache;
- return true;
- } else {
- return new Net_LDAP2_Error('Custom schema caching object is either no '.
- 'valid object or does not implement the Net_LDAP2_SchemaCache interface!');
- }
- }
-
- public static function checkLDAPExtension()
- {
- if (!extension_loaded('ldap') && !@dl('ldap.' . PHP_SHLIB_SUFFIX)) {
- return new Net_LDAP2_Error("It seems that you do not have the ldap-extension installed. Please install it before using the Net_LDAP2 package.");
- } else {
- return true;
- }
- }
-
- public function utf8Encode($attributes)
- {
- return $this->utf8($attributes, 'utf8_encode');
- }
-
- public function utf8Decode($attributes)
- {
- return $this->utf8($attributes, 'utf8_decode');
- }
-
- protected function utf8($attributes, $function)
- {
- if (!is_array($attributes) || array_key_exists(0, $attributes)) {
- return PEAR::raiseError('Parameter $attributes is expected to be an associative array');
- }
- if (!$this->_schema) {
- $this->_schema = $this->schema();
- }
- if (!$this->_link || self::isError($this->_schema) || !function_exists($function)) {
- return $attributes;
- }
- if (is_array($attributes) && count($attributes) > 0) {
- foreach ($attributes as $k => $v) {
- if (!isset($this->_schemaAttrs[$k])) {
- $attr = $this->_schema->get('attribute', $k);
- if (self::isError($attr)) {
- continue;
- }
-
-
-
- if (!array_key_exists('syntax', $attr) || false !== strpos($attr['syntax'], '1.3.6.1.4.1.1466.115.121.1.15')) {
- $encode = true;
- } else {
- $encode = false;
- }
- $this->_schemaAttrs[$k] = $encode;
- } else {
- $encode = $this->_schemaAttrs[$k];
- }
- if ($encode) {
- if (is_array($v)) {
- foreach ($v as $ak => $av) {
- $v[$ak] = call_user_func($function, $av);
- }
- } else {
- $v = call_user_func($function, $v);
- }
- }
- $attributes[$k] = $v;
- }
- }
- return $attributes;
- }
-
- public function getLink()
- {
- if ($this->_config['auto_reconnect']) {
- while (true) {
-
-
-
-
- if ($this->_link !== false) {
- return $this->_link;
- } else {
- $this->performReconnect();
- }
- }
- }
- return $this->_link;
- }
- }
- class Net_LDAP2_Error extends PEAR_Error
- {
-
- public function __construct($message = 'Net_LDAP2_Error', $code = NET_LDAP2_ERROR, $mode = PEAR_ERROR_RETURN,
- $level = E_USER_NOTICE, $debuginfo = null)
- {
- if (is_int($code)) {
- parent::__construct($message . ': ' . Net_LDAP2::errorMessage($code), $code, $mode, $level, $debuginfo);
- } else {
- parent::__construct("$message: $code", NET_LDAP2_ERROR, $mode, $level, $debuginfo);
- }
- }
- }
- ?>
|