Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ldap: Upgrade Net_LDAP2 #285

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/pear-pear.php.net/net_ldap2/Net/LDAP2.php
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ public function delete($dn, $recursive = false)
if ((Net_LDAP2::errorMessage($error_code) === 'LDAP_OPERATIONS_ERROR') &&
($this->_config['auto_reconnect'])) {
// The server has become disconnected before trying the
// operation. We should try again, possibly with a
// operation. We should try again, possibly with a
// different server.
$this->_link = false;
$this->performReconnect();
Expand Down Expand Up @@ -1319,10 +1319,11 @@ public function getEntry($dn, $attr = array())
* @param string|Net_LDAP2_Entry $entry Entry DN or Entry object
* @param string $newdn New location
* @param Net_LDAP2 $target_ldap (optional) Target directory for cross server move; should be passed via reference
* @param boolean $deleteoldrdn (optional) if false the old RDN value(s) is retained as non-distinguishided values of the entry.
*
* @return Net_LDAP2_Error|true
*/
public function move($entry, $newdn, $target_ldap = null)
public function move($entry, $newdn, $target_ldap = null, $deleteoldrdn = true)
{
if (is_string($entry)) {
$entry_o = $this->getEntry($entry);
Expand Down Expand Up @@ -1363,7 +1364,7 @@ public function move($entry, $newdn, $target_ldap = null)
// local move
$entry_o->dn($newdn);
$entry_o->setLDAP($this);
return $entry_o->update();
return $entry_o->update($deleteoldrdn);
}
}

Expand Down Expand Up @@ -1744,7 +1745,7 @@ protected function utf8($attributes, $function)
* auto_reconnect has been turned on (see the _config array documentation).
*
* @access public
* @return resource LDAP link
* @return resource|\LDAP\Connection LDAP link (resource on PHP < 8.1)
*/
public function getLink()
{
Expand Down
32 changes: 19 additions & 13 deletions lib/pear-pear.php.net/net_ldap2/Net/LDAP2/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function __construct($ldap, $entry = null)
parent::__construct('Net_LDAP2_Error');

// set up entry resource or DN
if ($entry !== false) {
if (is_resource($entry) || $entry instanceof \LDAP\ResultEntry) {
$this->_entry = $entry;
} else {
$this->_dn = $entry;
Expand All @@ -161,7 +161,7 @@ public function __construct($ldap, $entry = null)
if ($ldap instanceof Net_LDAP2) {
$this->_ldap = $ldap;
$this->_link = $ldap->getLink();
} elseif ($ldap !== false) {
} elseif (is_resource($ldap) || $ldap instanceof \LDAP\Connection) {
$this->_link = $ldap;
} elseif (is_array($ldap)) {
// Special case: here $ldap is an array of attributes,
Expand All @@ -173,7 +173,9 @@ public function __construct($ldap, $entry = null)

// if this is an entry existing in the directory,
// then set up as old and fetch attrs
if (($this->_entry !== false) && ($this->_link !== false)) {
if ((is_resource($this->_entry) || $this->_entry instanceof \LDAP\ResultEntry)
&& (is_resource($this->_link) || $this->_link instanceof \LDAP\Connection)
) {
$this->_new = false;
$this->_dn = @ldap_get_dn($this->_link, $this->_entry);
$this->setAttributes(); // fetch attributes from server
Expand Down Expand Up @@ -235,7 +237,7 @@ public static function createConnected($ldap, $entry)
if (!$ldap instanceof Net_LDAP2) {
return PEAR::raiseError("Unable to create connected entry: Parameter \$ldap needs to be a Net_LDAP2 object!");
}
if ($entry == false) {
if (!is_resource($entry) && !$entry instanceof \LDAP\ResultEntry) {
return PEAR::raiseError("Unable to create connected entry: Parameter \$entry needs to be a ldap entry resource!");
}

Expand Down Expand Up @@ -354,7 +356,10 @@ protected function setAttributes($attributes = null)
/*
* fetch attributes from the server
*/
if (is_null($attributes) && ($this->_entry !== false) && ($this->_link !== false)) {
if (is_null($attributes)
&& (is_resource($this->_entry) || $this->_entry instanceof \LDAP\ResultEntry)
&& (is_resource($this->_link) || $this->_link instanceof \LDAP\Connection)
) {
// fetch schema
if ($this->_ldap instanceof Net_LDAP2) {
$schema = $this->_ldap->schema();
Expand Down Expand Up @@ -489,7 +494,7 @@ public function getValue($attr, $option = null)
$value = array_shift($value);
}
}

}

return $value;
Expand Down Expand Up @@ -744,13 +749,14 @@ public function replace($attr = array(), $force = false)
* Remove the entry from the server and readd it as new in such cases.
* This also will deal with problems with setting structural object classes.
*
* @param Net_LDAP2 $ldap If passed, a call to setLDAP() is issued prior update, thus switching the LDAP-server. This is for perl-ldap interface compliance
* @param Net_LDAP2 $ldap If passed, a call to setLDAP() is issued prior update, thus switching the LDAP-server. This is for perl-ldap interface compliance
* @param boolean $deleteoldrdn (optional) if false the old RDN value(s) is retained as non-distinguishided values of the entry.
*
* @access public
* @return true|Net_LDAP2_Error
* @todo Entry rename with a DN containing special characters needs testing!
*/
public function update($ldap = null)
public function update($deleteoldrdn = true, $ldap = null)
{
if ($ldap) {
$msg = $this->setLDAP($ldap);
Expand All @@ -767,7 +773,7 @@ public function update($ldap = null)

// Get and check link
$link = $ldap->getLink();
if ($link == false) {
if (!is_resource($link) && !$link instanceof \LDAP\Connection) {
return PEAR::raiseError("Could not update entry: internal LDAP link is invalid");
}

Expand Down Expand Up @@ -825,7 +831,7 @@ public function update($ldap = null)
$parent = Net_LDAP2_Util::canonical_dn($parent);

// rename/move
if (false == @ldap_rename($link, $this->_dn, $child, $parent, false)) {
if (false == @ldap_rename($link, $this->_dn, $child, $parent, $deleteoldrdn)) {

return PEAR::raiseError("Entry not renamed: " .
@ldap_error($link), @ldap_errno($link));
Expand All @@ -850,8 +856,8 @@ public function update($ldap = null)
if ($fullEntry->exists($attr)) {
$currentValue = $fullEntry->getValue($attr, "all");
$value = array_merge( $currentValue, $value );
}
}

$modifications[$attr] = $value;
}

Expand All @@ -864,7 +870,7 @@ public function update($ldap = null)
if (!is_array($value)) {
$value = array($value);
}

// Find out what is missing from $value and exclude it
$currentValue = isset($modifications[$attr]) ? $modifications[$attr] : $fullEntry->getValue($attr, "all");
$modifications[$attr] = array_values( array_diff( $currentValue, $value ) );
Expand Down
9 changes: 8 additions & 1 deletion lib/pear-pear.php.net/net_ldap2/Net/LDAP2/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ public function shiftEntry()
}
$entry = Net_LDAP2_Entry::createConnected($this->_ldap, $this->_entry);
if ($entry instanceof PEAR_Error) $entry = false;

} else if ($this->_entry === false) {
//no more results
return false;

} else {
if (!$this->_entry = @ldap_next_entry($this->_link, $this->_entry)) {
$false = false;
Expand Down Expand Up @@ -498,7 +503,9 @@ public function getErrorCode()
*/
public function _Net_LDAP2_Search()
{
@ldap_free_result($this->_search);
if ($this->_search !== false) {
@ldap_free_result($this->_search);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/pear-pear.php.net/net_ldap2/Net/LDAP2/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public static function canonical_dn($dn, $options = array('casefold' => 'upper',

// escaping of dn-value
$val = self::escape_dn_value(array($val));
$val = str_replace('/', '\/', $val[0]);
$val = str_replace('/', '\\2f', $val[0]);

$dn[$pos] = $ocl.'='.$val;
}
Expand Down