Skip to content

Commit b1578f5

Browse files
committed
Prevent expando properties on Directory
Also start of modify_batch wrapper
1 parent 402bc60 commit b1578f5

9 files changed

+200
-0
lines changed

src/Directory.php

+37
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ public function __destruct()
6565
}
6666
}
6767

68+
/**
69+
* @param string $name
70+
*/
71+
public function __get($name)
72+
{
73+
throw new NonExistentPropertyException('Property ' . $name . ' not defined for ' . get_class($this));
74+
}
75+
76+
/**
77+
* @param string $name
78+
* @param mixed $value
79+
*/
80+
public function __set($name, $value)
81+
{
82+
throw new NonExistentPropertyException('Property ' . $name . ' not defined for ' . get_class($this));
83+
}
84+
6885
/**
6986
* @param string $dn
7087
* @param array $entry
@@ -263,6 +280,26 @@ public function modify($dn, array $entry)
263280
}
264281
}
265282

283+
/**
284+
* @param string $dn
285+
* @param array $entry
286+
* @throws FeatureUnavailableException
287+
* @throws UnavailableException
288+
* @throws WriteFailureException
289+
*/
290+
public function modifyBatch($dn, array $entry)
291+
{
292+
if (!function_exists('ldap_modify_batch')) {
293+
throw new FeatureUnavailableException('The ldap_modify_batch() function is not available on this system');
294+
}
295+
296+
$this->checkBound();
297+
298+
if (!ldap_modify_batch($this->link, $dn, $entry)) {
299+
throw new WriteFailureException(ldap_error($this->link), ldap_errno($this->link));
300+
}
301+
}
302+
266303
/**
267304
* @param string $dn
268305
* @param string $filter

src/Entry.php

+17
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ public function __construct($link, $entry)
2727
$this->entry = $entry;
2828
}
2929

30+
/**
31+
* @param string $name
32+
*/
33+
public function __get($name)
34+
{
35+
throw new NonExistentPropertyException('Property ' . $name . ' not defined for ' . get_class($this));
36+
}
37+
38+
/**
39+
* @param string $name
40+
* @param mixed $value
41+
*/
42+
public function __set($name, $value)
43+
{
44+
throw new NonExistentPropertyException('Property ' . $name . ' not defined for ' . get_class($this));
45+
}
46+
3047
/**
3148
* @return Entry|null
3249
* @throws EntryRetrievalFailureException

src/FeatureUnavailableException.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace LDAPi;
4+
5+
class FeatureUnavailableException extends \RuntimeException {}

src/InvalidValueSetException.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace LDAPi;
4+
5+
class InvalidValueSetException extends \LogicException {}

src/Modification.php

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace LDAPi;
4+
5+
use ArrayObject;
6+
7+
/**
8+
* Class Modification
9+
* @package LDAPi
10+
* @property string $attributeName
11+
* @property int $operation
12+
* @property \ArrayObject|array $data
13+
* @property mixed $value
14+
*/
15+
class Modification
16+
{
17+
const OP_ADD = LDAP_MODIFY_BATCH_ADD;
18+
const OP_REMOVE = LDAP_MODIFY_BATCH_REMOVE;
19+
const OP_REMOVE_ALL = LDAP_MODIFY_BATCH_REMOVE_ALL;
20+
const OP_REPLACE = LDAP_MODIFY_BATCH_REPLACE;
21+
22+
/**
23+
* @var array
24+
*/
25+
private $data = [
26+
'attributeName' => null,
27+
'operation' => null,
28+
'values' => null,
29+
];
30+
31+
public function __construct()
32+
{
33+
$this->data['values'] = new ArrayObject;
34+
}
35+
36+
/**
37+
* @param string $name
38+
* @param mixed $value
39+
*/
40+
public function __set($name, $value)
41+
{
42+
switch ($name) {
43+
case 'attributeName':
44+
$this->data['attributeName'] = (string)$value;
45+
break;
46+
47+
case 'operation':
48+
if (!in_array($value, [self::OP_ADD, self::OP_REMOVE, self::OP_REMOVE_ALL, self::OP_REPLACE])) {
49+
throw new InvalidModeException('Operation must be one of the Modification::OP_* constants');
50+
}
51+
52+
$this->data['operation'] = (int)$value;
53+
54+
if ($this->data['operation'] === self::OP_REMOVE_ALL) {
55+
$this->data['values'] = new ArrayObject;
56+
}
57+
break;
58+
59+
case 'values':
60+
if ($this->data['operation'] === self::OP_REMOVE_ALL) {
61+
throw new InvalidModeException('REMOVE_ALL operations cannot include a value set');
62+
}
63+
64+
if ($value instanceof ArrayObject) {
65+
$this->data['values'] = $value;
66+
} else if (is_array($value)) {
67+
$this->data['values'] = new ArrayObject($value);
68+
} else {
69+
throw new InvalidValueSetException('Value set must be specified as an array or an ArrayObject');
70+
}
71+
break;
72+
73+
default:
74+
throw new NonExistentPropertyException('Property ' . $name . ' not defined for ' . get_class($this));
75+
}
76+
}
77+
78+
/**
79+
* @param string $name
80+
* @return mixed
81+
*/
82+
public function __get($name)
83+
{
84+
if (!array_key_exists($name, $this->data)) {
85+
throw new NonExistentPropertyException('Property ' . $name . ' not defined for ' . get_class($this));
86+
}
87+
88+
return $this->data[$name];
89+
}
90+
}

src/NonExistentPropertyException.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace LDAPi;
4+
5+
class NonExistentPropertyException extends \LogicException {}

src/Reference.php

+17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ public function __construct($link, $reference)
2424
$this->reference = $reference;
2525
}
2626

27+
/**
28+
* @param string $name
29+
*/
30+
public function __get($name)
31+
{
32+
throw new NonExistentPropertyException('Property ' . $name . ' not defined for ' . get_class($this));
33+
}
34+
35+
/**
36+
* @param string $name
37+
* @param mixed $value
38+
*/
39+
public function __set($name, $value)
40+
{
41+
throw new NonExistentPropertyException('Property ' . $name . ' not defined for ' . get_class($this));
42+
}
43+
2744
/**
2845
* @return Reference|null
2946
* @throws ReferenceRetrievalFailureException

src/ResultSet.php

+17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ public function __destruct()
2929
ldap_free_result($this->result);
3030
}
3131

32+
/**
33+
* @param string $name
34+
*/
35+
public function __get($name)
36+
{
37+
throw new NonExistentPropertyException('Property ' . $name . ' not defined for ' . get_class($this));
38+
}
39+
40+
/**
41+
* @param string $name
42+
* @param mixed $value
43+
*/
44+
public function __set($name, $value)
45+
{
46+
throw new NonExistentPropertyException('Property ' . $name . ' not defined for ' . get_class($this));
47+
}
48+
3249
/**
3350
* @param int $estimated
3451
* @return string

src/functions.php

+7
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@ function ldap_escape($subject, $ignore = '', $flags = 0)
4848
return strtr($subject, $charList);
4949
}
5050
}
51+
52+
if (!function_exists('ldap_modify_batch')) {
53+
define('LDAP_MODIFY_BATCH_ADD', 1);
54+
define('LDAP_MODIFY_BATCH_REMOVE', 2);
55+
define('LDAP_MODIFY_BATCH_REMOVE_ALL', 18);
56+
define('LDAP_MODIFY_BATCH_REPLACE', 3);
57+
}
5158
}

0 commit comments

Comments
 (0)