Skip to content
Open
75 changes: 43 additions & 32 deletions src/PHPOpenLDAPer/LDAPEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct(Connection $conn, string $dn)
$this->conn = $conn;
$this->dn = $dn;
$this->pullObject();
$this->mods = null;
}

/**
Expand All @@ -52,7 +53,7 @@ private function pullObject(): bool
}
LDAPConn::stripCount($entries);
if (count($entries) > 1) {
throw new \Exception("FATAL: Call to ldapObject with non-unique DN.");
throw new RuntimeException("FATAL: Call to ldapObject with non-unique DN.");
} else {
$this->object = $entries[0];
return true;
Expand Down Expand Up @@ -315,11 +316,7 @@ public function numChildren(bool $recursive = false): int
public function setAttribute(string $attr, mixed $value): void
{
$attr = strtolower($attr);
if (is_array($value)) {
$this->mods[$attr] = $value;
} else {
$this->mods[$attr] = array($value);
}
$this->mods[$attr] = $this->convertToArray($value);
}

/**
Expand All @@ -331,21 +328,8 @@ public function setAttribute(string $attr, mixed $value): void
public function appendAttribute(string $attr, mixed $value): void
{
$attr = strtolower($attr);
$objArr = array();
if (isset($this->object[$attr])) {
$objArr = $this->object[$attr];
}

$modArr = array();
if (isset($this->mods[$attr])) {
$modArr = $this->mods[$attr];
}

if (is_array($value)) {
$this->mods[$attr] = array_merge($objArr, $modArr, $value);
} else {
$this->mods[$attr] = array_merge($objArr, $modArr, (array) $value);
}
$old_value = $this->getAttribute($attr);
$this->mods[$attr] = array_merge($old_value, $this->convertToArray($value));
}

/**
Expand All @@ -356,7 +340,8 @@ public function appendAttribute(string $attr, mixed $value): void
public function setAttributes(array $arr): void
{
$arr = array_change_key_case($arr, CASE_LOWER);
$this->mods = $arr;
$arr = array_map([$this, "convertToArray"], $arr);
$this->mods = array_merge($this->mods, $arr);
}

/**
Expand Down Expand Up @@ -392,7 +377,7 @@ public function removeAttribute(string $attr, $item = null): void
public function removeAttributeEntryByValue(string $attr, mixed $value): void
{
$attr = strtolower($attr);
$arr = $this->object[$attr];
$arr = $this->getAttribute($attr);
for ($i = 0; $i < count($arr); $i++) {
if ($arr[$i] == $value) {
unset($arr[$i]);
Expand All @@ -401,6 +386,15 @@ public function removeAttributeEntryByValue(string $attr, mixed $value): void
$this->mods[$attr] = array_values($arr);
}

private function convertToArray(mixed $x)
{
if (is_array($x)) {
return $x;
} else {
return [$x];
}
}

/**
* Returns a given attribute of the object
*
Expand All @@ -410,14 +404,18 @@ public function removeAttributeEntryByValue(string $attr, mixed $value): void
public function getAttribute(string $attr): mixed
{
$attr = strtolower($attr);
if (($this->mods != null) && (array_key_exists($attr, $this->mods))) {
return $this->convertToArray($this->mods[$attr]);
}
if (!$this->exists()) {
throw new RuntimeException("cannot get attribute from nonexistent entry");
throw new RuntimeException(
"cannot get attribute from nonexistent entry with no modifications"
);
}
if (isset($this->object[$attr])) {
return is_array($this->object[$attr]) ? $this->object[$attr] : [$this->object[$attr]];
} else {
return [];
if (array_key_exists($attr, $this->object)) {
return $this->convertToArray($this->object[$attr]);
}
return [];
}

/**
Expand All @@ -427,16 +425,29 @@ public function getAttribute(string $attr): mixed
*/
public function getAttributes(): array
{
if (!$this->exists()) {
throw new RuntimeException("cannot get attributes from nonexistent entry");
$has_mods = $this->mods != null;
$has_object = $this->object != null;
if (!$has_mods && !$has_object) {
throw new RuntimeException(
"cannot get attributes from nonexistent entry with no modifications"
);
}
if (!$has_mods && $has_object) {
$attributes = $this->object;
}
if ($has_mods && !$has_object) {
$attributes = $this->mods;
}
if ($has_mods && $has_object) {
$attributes = array_merge($this->object, $this->mods);
}
$output = [];
foreach ($this->object as $key => $val) {
foreach ($attributes as $key => $val) {
if (preg_match("/^[0-9]+$/", $key)) {
continue;
}
$key = strtolower($key);
$output[$key] = is_array($val) ? $val : [$val];
$output[$key] = $this->convertToArray($val);
}
return $output;
}
Expand Down