@@ -11,14 +11,14 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class MatchingRule |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Compare attribute value to assertion. |
|
| 16 | - * |
|
| 17 | - * @param mixed $assertion Value to assert |
|
| 18 | - * @param mixed $value Attribute value |
|
| 19 | - * |
|
| 20 | - * @return null|bool True if value matches. Null shall be returned if match |
|
| 21 | - * evaluates to Undefined. |
|
| 22 | - */ |
|
| 23 | - abstract public function compare($assertion, $value): ?bool; |
|
| 14 | + /** |
|
| 15 | + * Compare attribute value to assertion. |
|
| 16 | + * |
|
| 17 | + * @param mixed $assertion Value to assert |
|
| 18 | + * @param mixed $value Attribute value |
|
| 19 | + * |
|
| 20 | + * @return null|bool True if value matches. Null shall be returned if match |
|
| 21 | + * evaluates to Undefined. |
|
| 22 | + */ |
|
| 23 | + abstract public function compare($assertion, $value): ?bool; |
|
| 24 | 24 | } |
@@ -11,30 +11,30 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class StringPrepMatchingRule extends MatchingRule |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * String preparer. |
|
| 16 | - * |
|
| 17 | - * @var StringPreparer |
|
| 18 | - */ |
|
| 19 | - protected $_prep; |
|
| 14 | + /** |
|
| 15 | + * String preparer. |
|
| 16 | + * |
|
| 17 | + * @var StringPreparer |
|
| 18 | + */ |
|
| 19 | + protected $_prep; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Constructor. |
|
| 23 | - * |
|
| 24 | - * @param StringPreparer $preparer |
|
| 25 | - */ |
|
| 26 | - public function __construct(StringPreparer $preparer) |
|
| 27 | - { |
|
| 28 | - $this->_prep = $preparer; |
|
| 29 | - } |
|
| 21 | + /** |
|
| 22 | + * Constructor. |
|
| 23 | + * |
|
| 24 | + * @param StringPreparer $preparer |
|
| 25 | + */ |
|
| 26 | + public function __construct(StringPreparer $preparer) |
|
| 27 | + { |
|
| 28 | + $this->_prep = $preparer; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * {@inheritdoc} |
|
| 33 | - */ |
|
| 34 | - public function compare($assertion, $value): ?bool |
|
| 35 | - { |
|
| 36 | - $assertion = $this->_prep->prepare($assertion); |
|
| 37 | - $value = $this->_prep->prepare($value); |
|
| 38 | - return 0 == strcmp($assertion, $value); |
|
| 39 | - } |
|
| 31 | + /** |
|
| 32 | + * {@inheritdoc} |
|
| 33 | + */ |
|
| 34 | + public function compare($assertion, $value): ?bool |
|
| 35 | + { |
|
| 36 | + $assertion = $this->_prep->prepare($assertion); |
|
| 37 | + $value = $this->_prep->prepare($value); |
|
| 38 | + return 0 == strcmp($assertion, $value); |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -15,414 +15,414 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class DNParser |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * RFC 2253 special characters. |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - const SPECIAL_CHARS = ',=+<>#;'; |
|
| 18 | + /** |
|
| 19 | + * RFC 2253 special characters. |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + const SPECIAL_CHARS = ',=+<>#;'; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * DN string. |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - private $_dn; |
|
| 25 | + /** |
|
| 26 | + * DN string. |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + private $_dn; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * DN string length. |
|
| 34 | - * |
|
| 35 | - * @var int |
|
| 36 | - */ |
|
| 37 | - private $_len; |
|
| 32 | + /** |
|
| 33 | + * DN string length. |
|
| 34 | + * |
|
| 35 | + * @var int |
|
| 36 | + */ |
|
| 37 | + private $_len; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Constructor. |
|
| 41 | - * |
|
| 42 | - * @param string $dn Distinguised name |
|
| 43 | - */ |
|
| 44 | - protected function __construct(string $dn) |
|
| 45 | - { |
|
| 46 | - $this->_dn = $dn; |
|
| 47 | - $this->_len = strlen($dn); |
|
| 48 | - } |
|
| 39 | + /** |
|
| 40 | + * Constructor. |
|
| 41 | + * |
|
| 42 | + * @param string $dn Distinguised name |
|
| 43 | + */ |
|
| 44 | + protected function __construct(string $dn) |
|
| 45 | + { |
|
| 46 | + $this->_dn = $dn; |
|
| 47 | + $this->_len = strlen($dn); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Parse distinguished name string to name-components. |
|
| 52 | - * |
|
| 53 | - * @param string $dn |
|
| 54 | - * |
|
| 55 | - * @return array |
|
| 56 | - */ |
|
| 57 | - public static function parseString(string $dn): array |
|
| 58 | - { |
|
| 59 | - $parser = new self($dn); |
|
| 60 | - return $parser->parse(); |
|
| 61 | - } |
|
| 50 | + /** |
|
| 51 | + * Parse distinguished name string to name-components. |
|
| 52 | + * |
|
| 53 | + * @param string $dn |
|
| 54 | + * |
|
| 55 | + * @return array |
|
| 56 | + */ |
|
| 57 | + public static function parseString(string $dn): array |
|
| 58 | + { |
|
| 59 | + $parser = new self($dn); |
|
| 60 | + return $parser->parse(); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Escape a AttributeValue string conforming to RFC 2253. |
|
| 65 | - * |
|
| 66 | - * @see https://tools.ietf.org/html/rfc2253#section-2.4 |
|
| 67 | - * |
|
| 68 | - * @param string $str |
|
| 69 | - * |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - public static function escapeString(string $str): string |
|
| 73 | - { |
|
| 74 | - // one of the characters ",", "+", """, "\", "<", ">" or ";" |
|
| 75 | - $str = preg_replace('/([,\+"\\\<\>;])/u', '\\\\$1', $str); |
|
| 76 | - // a space character occurring at the end of the string |
|
| 77 | - $str = preg_replace('/( )$/u', '\\\\$1', $str); |
|
| 78 | - // a space or "#" character occurring at the beginning of the string |
|
| 79 | - $str = preg_replace('/^([ #])/u', '\\\\$1', $str); |
|
| 80 | - // implementation specific special characters |
|
| 81 | - $str = preg_replace_callback('/([\pC])/u', |
|
| 82 | - function ($m) { |
|
| 83 | - $octets = str_split(bin2hex($m[1]), 2); |
|
| 84 | - return implode('', |
|
| 85 | - array_map( |
|
| 86 | - function ($octet) { |
|
| 87 | - return '\\' . strtoupper($octet); |
|
| 88 | - }, $octets)); |
|
| 89 | - }, $str); |
|
| 90 | - return $str; |
|
| 91 | - } |
|
| 63 | + /** |
|
| 64 | + * Escape a AttributeValue string conforming to RFC 2253. |
|
| 65 | + * |
|
| 66 | + * @see https://tools.ietf.org/html/rfc2253#section-2.4 |
|
| 67 | + * |
|
| 68 | + * @param string $str |
|
| 69 | + * |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + public static function escapeString(string $str): string |
|
| 73 | + { |
|
| 74 | + // one of the characters ",", "+", """, "\", "<", ">" or ";" |
|
| 75 | + $str = preg_replace('/([,\+"\\\<\>;])/u', '\\\\$1', $str); |
|
| 76 | + // a space character occurring at the end of the string |
|
| 77 | + $str = preg_replace('/( )$/u', '\\\\$1', $str); |
|
| 78 | + // a space or "#" character occurring at the beginning of the string |
|
| 79 | + $str = preg_replace('/^([ #])/u', '\\\\$1', $str); |
|
| 80 | + // implementation specific special characters |
|
| 81 | + $str = preg_replace_callback('/([\pC])/u', |
|
| 82 | + function ($m) { |
|
| 83 | + $octets = str_split(bin2hex($m[1]), 2); |
|
| 84 | + return implode('', |
|
| 85 | + array_map( |
|
| 86 | + function ($octet) { |
|
| 87 | + return '\\' . strtoupper($octet); |
|
| 88 | + }, $octets)); |
|
| 89 | + }, $str); |
|
| 90 | + return $str; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Parse DN to name-components. |
|
| 95 | - * |
|
| 96 | - * @throws \RuntimeException |
|
| 97 | - * |
|
| 98 | - * @return array |
|
| 99 | - */ |
|
| 100 | - protected function parse(): array |
|
| 101 | - { |
|
| 102 | - $offset = 0; |
|
| 103 | - $name = $this->_parseName($offset); |
|
| 104 | - if ($offset < $this->_len) { |
|
| 105 | - $remains = substr($this->_dn, $offset); |
|
| 106 | - throw new \UnexpectedValueException( |
|
| 107 | - 'Parser finished before the end of string' . |
|
| 108 | - ", remaining: '{$remains}'."); |
|
| 109 | - } |
|
| 110 | - return $name; |
|
| 111 | - } |
|
| 93 | + /** |
|
| 94 | + * Parse DN to name-components. |
|
| 95 | + * |
|
| 96 | + * @throws \RuntimeException |
|
| 97 | + * |
|
| 98 | + * @return array |
|
| 99 | + */ |
|
| 100 | + protected function parse(): array |
|
| 101 | + { |
|
| 102 | + $offset = 0; |
|
| 103 | + $name = $this->_parseName($offset); |
|
| 104 | + if ($offset < $this->_len) { |
|
| 105 | + $remains = substr($this->_dn, $offset); |
|
| 106 | + throw new \UnexpectedValueException( |
|
| 107 | + 'Parser finished before the end of string' . |
|
| 108 | + ", remaining: '{$remains}'."); |
|
| 109 | + } |
|
| 110 | + return $name; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Parse 'name'. |
|
| 115 | - * |
|
| 116 | - * name-component *("," name-component) |
|
| 117 | - * |
|
| 118 | - * @param int $offset |
|
| 119 | - * |
|
| 120 | - * @return array Array of name-components |
|
| 121 | - */ |
|
| 122 | - private function _parseName(int &$offset): array |
|
| 123 | - { |
|
| 124 | - $idx = $offset; |
|
| 125 | - $names = []; |
|
| 126 | - while ($idx < $this->_len) { |
|
| 127 | - $names[] = $this->_parseNameComponent($idx); |
|
| 128 | - if ($idx >= $this->_len) { |
|
| 129 | - break; |
|
| 130 | - } |
|
| 131 | - $this->_skipWs($idx); |
|
| 132 | - if (',' != $this->_dn[$idx] && ';' != $this->_dn[$idx]) { |
|
| 133 | - break; |
|
| 134 | - } |
|
| 135 | - ++$idx; |
|
| 136 | - $this->_skipWs($idx); |
|
| 137 | - } |
|
| 138 | - $offset = $idx; |
|
| 139 | - return array_reverse($names); |
|
| 140 | - } |
|
| 113 | + /** |
|
| 114 | + * Parse 'name'. |
|
| 115 | + * |
|
| 116 | + * name-component *("," name-component) |
|
| 117 | + * |
|
| 118 | + * @param int $offset |
|
| 119 | + * |
|
| 120 | + * @return array Array of name-components |
|
| 121 | + */ |
|
| 122 | + private function _parseName(int &$offset): array |
|
| 123 | + { |
|
| 124 | + $idx = $offset; |
|
| 125 | + $names = []; |
|
| 126 | + while ($idx < $this->_len) { |
|
| 127 | + $names[] = $this->_parseNameComponent($idx); |
|
| 128 | + if ($idx >= $this->_len) { |
|
| 129 | + break; |
|
| 130 | + } |
|
| 131 | + $this->_skipWs($idx); |
|
| 132 | + if (',' != $this->_dn[$idx] && ';' != $this->_dn[$idx]) { |
|
| 133 | + break; |
|
| 134 | + } |
|
| 135 | + ++$idx; |
|
| 136 | + $this->_skipWs($idx); |
|
| 137 | + } |
|
| 138 | + $offset = $idx; |
|
| 139 | + return array_reverse($names); |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Parse 'name-component'. |
|
| 144 | - * |
|
| 145 | - * attributeTypeAndValue *("+" attributeTypeAndValue) |
|
| 146 | - * |
|
| 147 | - * @param int $offset |
|
| 148 | - * |
|
| 149 | - * @return array Array of [type, value] tuples |
|
| 150 | - */ |
|
| 151 | - private function _parseNameComponent(int &$offset): array |
|
| 152 | - { |
|
| 153 | - $idx = $offset; |
|
| 154 | - $tvpairs = []; |
|
| 155 | - while ($idx < $this->_len) { |
|
| 156 | - $tvpairs[] = $this->_parseAttrTypeAndValue($idx); |
|
| 157 | - $this->_skipWs($idx); |
|
| 158 | - if ($idx >= $this->_len || '+' != $this->_dn[$idx]) { |
|
| 159 | - break; |
|
| 160 | - } |
|
| 161 | - ++$idx; |
|
| 162 | - $this->_skipWs($idx); |
|
| 163 | - } |
|
| 164 | - $offset = $idx; |
|
| 165 | - return $tvpairs; |
|
| 166 | - } |
|
| 142 | + /** |
|
| 143 | + * Parse 'name-component'. |
|
| 144 | + * |
|
| 145 | + * attributeTypeAndValue *("+" attributeTypeAndValue) |
|
| 146 | + * |
|
| 147 | + * @param int $offset |
|
| 148 | + * |
|
| 149 | + * @return array Array of [type, value] tuples |
|
| 150 | + */ |
|
| 151 | + private function _parseNameComponent(int &$offset): array |
|
| 152 | + { |
|
| 153 | + $idx = $offset; |
|
| 154 | + $tvpairs = []; |
|
| 155 | + while ($idx < $this->_len) { |
|
| 156 | + $tvpairs[] = $this->_parseAttrTypeAndValue($idx); |
|
| 157 | + $this->_skipWs($idx); |
|
| 158 | + if ($idx >= $this->_len || '+' != $this->_dn[$idx]) { |
|
| 159 | + break; |
|
| 160 | + } |
|
| 161 | + ++$idx; |
|
| 162 | + $this->_skipWs($idx); |
|
| 163 | + } |
|
| 164 | + $offset = $idx; |
|
| 165 | + return $tvpairs; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * Parse 'attributeTypeAndValue'. |
|
| 170 | - * |
|
| 171 | - * attributeType "=" attributeValue |
|
| 172 | - * |
|
| 173 | - * @param int $offset |
|
| 174 | - * |
|
| 175 | - * @throws \UnexpectedValueException |
|
| 176 | - * |
|
| 177 | - * @return array A tuple of [type, value]. Value may be either a string or |
|
| 178 | - * an Element, if it's encoded as hexstring. |
|
| 179 | - */ |
|
| 180 | - private function _parseAttrTypeAndValue(int &$offset): array |
|
| 181 | - { |
|
| 182 | - $idx = $offset; |
|
| 183 | - $type = $this->_parseAttrType($idx); |
|
| 184 | - $this->_skipWs($idx); |
|
| 185 | - if ($idx >= $this->_len || '=' != $this->_dn[$idx++]) { |
|
| 186 | - throw new \UnexpectedValueException('Invalid type and value pair.'); |
|
| 187 | - } |
|
| 188 | - $this->_skipWs($idx); |
|
| 189 | - // hexstring |
|
| 190 | - if ($idx < $this->_len && '#' == $this->_dn[$idx]) { |
|
| 191 | - ++$idx; |
|
| 192 | - $data = $this->_parseAttrHexValue($idx); |
|
| 193 | - try { |
|
| 194 | - $value = Element::fromDER($data); |
|
| 195 | - } catch (DecodeException $e) { |
|
| 196 | - throw new \UnexpectedValueException( |
|
| 197 | - 'Invalid DER encoding from hexstring.', 0, $e); |
|
| 198 | - } |
|
| 199 | - } else { |
|
| 200 | - $value = $this->_parseAttrStringValue($idx); |
|
| 201 | - } |
|
| 202 | - $offset = $idx; |
|
| 203 | - return [$type, $value]; |
|
| 204 | - } |
|
| 168 | + /** |
|
| 169 | + * Parse 'attributeTypeAndValue'. |
|
| 170 | + * |
|
| 171 | + * attributeType "=" attributeValue |
|
| 172 | + * |
|
| 173 | + * @param int $offset |
|
| 174 | + * |
|
| 175 | + * @throws \UnexpectedValueException |
|
| 176 | + * |
|
| 177 | + * @return array A tuple of [type, value]. Value may be either a string or |
|
| 178 | + * an Element, if it's encoded as hexstring. |
|
| 179 | + */ |
|
| 180 | + private function _parseAttrTypeAndValue(int &$offset): array |
|
| 181 | + { |
|
| 182 | + $idx = $offset; |
|
| 183 | + $type = $this->_parseAttrType($idx); |
|
| 184 | + $this->_skipWs($idx); |
|
| 185 | + if ($idx >= $this->_len || '=' != $this->_dn[$idx++]) { |
|
| 186 | + throw new \UnexpectedValueException('Invalid type and value pair.'); |
|
| 187 | + } |
|
| 188 | + $this->_skipWs($idx); |
|
| 189 | + // hexstring |
|
| 190 | + if ($idx < $this->_len && '#' == $this->_dn[$idx]) { |
|
| 191 | + ++$idx; |
|
| 192 | + $data = $this->_parseAttrHexValue($idx); |
|
| 193 | + try { |
|
| 194 | + $value = Element::fromDER($data); |
|
| 195 | + } catch (DecodeException $e) { |
|
| 196 | + throw new \UnexpectedValueException( |
|
| 197 | + 'Invalid DER encoding from hexstring.', 0, $e); |
|
| 198 | + } |
|
| 199 | + } else { |
|
| 200 | + $value = $this->_parseAttrStringValue($idx); |
|
| 201 | + } |
|
| 202 | + $offset = $idx; |
|
| 203 | + return [$type, $value]; |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * Parse 'attributeType'. |
|
| 208 | - * |
|
| 209 | - * (ALPHA 1*keychar) / oid |
|
| 210 | - * |
|
| 211 | - * @param int $offset |
|
| 212 | - * |
|
| 213 | - * @throws \UnexpectedValueException |
|
| 214 | - * |
|
| 215 | - * @return string |
|
| 216 | - */ |
|
| 217 | - private function _parseAttrType(int &$offset): string |
|
| 218 | - { |
|
| 219 | - $idx = $offset; |
|
| 220 | - // dotted OID |
|
| 221 | - $type = $this->_regexMatch('/^(?:oid\.)?([0-9]+(?:\.[0-9]+)*)/i', $idx); |
|
| 222 | - if (null === $type) { |
|
| 223 | - // name |
|
| 224 | - $type = $this->_regexMatch('/^[a-z][a-z0-9\-]*/i', $idx); |
|
| 225 | - if (null === $type) { |
|
| 226 | - throw new \UnexpectedValueException('Invalid attribute type.'); |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - $offset = $idx; |
|
| 230 | - return $type; |
|
| 231 | - } |
|
| 206 | + /** |
|
| 207 | + * Parse 'attributeType'. |
|
| 208 | + * |
|
| 209 | + * (ALPHA 1*keychar) / oid |
|
| 210 | + * |
|
| 211 | + * @param int $offset |
|
| 212 | + * |
|
| 213 | + * @throws \UnexpectedValueException |
|
| 214 | + * |
|
| 215 | + * @return string |
|
| 216 | + */ |
|
| 217 | + private function _parseAttrType(int &$offset): string |
|
| 218 | + { |
|
| 219 | + $idx = $offset; |
|
| 220 | + // dotted OID |
|
| 221 | + $type = $this->_regexMatch('/^(?:oid\.)?([0-9]+(?:\.[0-9]+)*)/i', $idx); |
|
| 222 | + if (null === $type) { |
|
| 223 | + // name |
|
| 224 | + $type = $this->_regexMatch('/^[a-z][a-z0-9\-]*/i', $idx); |
|
| 225 | + if (null === $type) { |
|
| 226 | + throw new \UnexpectedValueException('Invalid attribute type.'); |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + $offset = $idx; |
|
| 230 | + return $type; |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - /** |
|
| 234 | - * Parse 'attributeValue' of string type. |
|
| 235 | - * |
|
| 236 | - * @param int $offset |
|
| 237 | - * |
|
| 238 | - * @throws \UnexpectedValueException |
|
| 239 | - * |
|
| 240 | - * @return string |
|
| 241 | - */ |
|
| 242 | - private function _parseAttrStringValue(int &$offset): string |
|
| 243 | - { |
|
| 244 | - $idx = $offset; |
|
| 245 | - if ($idx >= $this->_len) { |
|
| 246 | - return ''; |
|
| 247 | - } |
|
| 248 | - if ('"' == $this->_dn[$idx]) { // quoted string |
|
| 249 | - $val = $this->_parseQuotedAttrString($idx); |
|
| 250 | - } else { // string |
|
| 251 | - $val = $this->_parseAttrString($idx); |
|
| 252 | - } |
|
| 253 | - $offset = $idx; |
|
| 254 | - return $val; |
|
| 255 | - } |
|
| 233 | + /** |
|
| 234 | + * Parse 'attributeValue' of string type. |
|
| 235 | + * |
|
| 236 | + * @param int $offset |
|
| 237 | + * |
|
| 238 | + * @throws \UnexpectedValueException |
|
| 239 | + * |
|
| 240 | + * @return string |
|
| 241 | + */ |
|
| 242 | + private function _parseAttrStringValue(int &$offset): string |
|
| 243 | + { |
|
| 244 | + $idx = $offset; |
|
| 245 | + if ($idx >= $this->_len) { |
|
| 246 | + return ''; |
|
| 247 | + } |
|
| 248 | + if ('"' == $this->_dn[$idx]) { // quoted string |
|
| 249 | + $val = $this->_parseQuotedAttrString($idx); |
|
| 250 | + } else { // string |
|
| 251 | + $val = $this->_parseAttrString($idx); |
|
| 252 | + } |
|
| 253 | + $offset = $idx; |
|
| 254 | + return $val; |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - /** |
|
| 258 | - * Parse plain 'attributeValue' string. |
|
| 259 | - * |
|
| 260 | - * @param int $offset |
|
| 261 | - * |
|
| 262 | - * @throws \UnexpectedValueException |
|
| 263 | - * |
|
| 264 | - * @return string |
|
| 265 | - */ |
|
| 266 | - private function _parseAttrString(int &$offset): string |
|
| 267 | - { |
|
| 268 | - $idx = $offset; |
|
| 269 | - $val = ''; |
|
| 270 | - $wsidx = null; |
|
| 271 | - while ($idx < $this->_len) { |
|
| 272 | - $c = $this->_dn[$idx]; |
|
| 273 | - // pair (escape sequence) |
|
| 274 | - if ('\\' == $c) { |
|
| 275 | - ++$idx; |
|
| 276 | - $val .= $this->_parsePairAfterSlash($idx); |
|
| 277 | - $wsidx = null; |
|
| 278 | - continue; |
|
| 279 | - } |
|
| 280 | - if ('"' == $c) { |
|
| 281 | - throw new \UnexpectedValueException('Unexpected quotation.'); |
|
| 282 | - } |
|
| 283 | - if (false !== strpos(self::SPECIAL_CHARS, $c)) { |
|
| 284 | - break; |
|
| 285 | - } |
|
| 286 | - // keep track of the first consecutive whitespace |
|
| 287 | - if (' ' == $c) { |
|
| 288 | - if (null === $wsidx) { |
|
| 289 | - $wsidx = $idx; |
|
| 290 | - } |
|
| 291 | - } else { |
|
| 292 | - $wsidx = null; |
|
| 293 | - } |
|
| 294 | - // stringchar |
|
| 295 | - $val .= $c; |
|
| 296 | - ++$idx; |
|
| 297 | - } |
|
| 298 | - // if there was non-escaped whitespace in the end of the value |
|
| 299 | - if (null !== $wsidx) { |
|
| 300 | - $val = substr($val, 0, -($idx - $wsidx)); |
|
| 301 | - } |
|
| 302 | - $offset = $idx; |
|
| 303 | - return $val; |
|
| 304 | - } |
|
| 257 | + /** |
|
| 258 | + * Parse plain 'attributeValue' string. |
|
| 259 | + * |
|
| 260 | + * @param int $offset |
|
| 261 | + * |
|
| 262 | + * @throws \UnexpectedValueException |
|
| 263 | + * |
|
| 264 | + * @return string |
|
| 265 | + */ |
|
| 266 | + private function _parseAttrString(int &$offset): string |
|
| 267 | + { |
|
| 268 | + $idx = $offset; |
|
| 269 | + $val = ''; |
|
| 270 | + $wsidx = null; |
|
| 271 | + while ($idx < $this->_len) { |
|
| 272 | + $c = $this->_dn[$idx]; |
|
| 273 | + // pair (escape sequence) |
|
| 274 | + if ('\\' == $c) { |
|
| 275 | + ++$idx; |
|
| 276 | + $val .= $this->_parsePairAfterSlash($idx); |
|
| 277 | + $wsidx = null; |
|
| 278 | + continue; |
|
| 279 | + } |
|
| 280 | + if ('"' == $c) { |
|
| 281 | + throw new \UnexpectedValueException('Unexpected quotation.'); |
|
| 282 | + } |
|
| 283 | + if (false !== strpos(self::SPECIAL_CHARS, $c)) { |
|
| 284 | + break; |
|
| 285 | + } |
|
| 286 | + // keep track of the first consecutive whitespace |
|
| 287 | + if (' ' == $c) { |
|
| 288 | + if (null === $wsidx) { |
|
| 289 | + $wsidx = $idx; |
|
| 290 | + } |
|
| 291 | + } else { |
|
| 292 | + $wsidx = null; |
|
| 293 | + } |
|
| 294 | + // stringchar |
|
| 295 | + $val .= $c; |
|
| 296 | + ++$idx; |
|
| 297 | + } |
|
| 298 | + // if there was non-escaped whitespace in the end of the value |
|
| 299 | + if (null !== $wsidx) { |
|
| 300 | + $val = substr($val, 0, -($idx - $wsidx)); |
|
| 301 | + } |
|
| 302 | + $offset = $idx; |
|
| 303 | + return $val; |
|
| 304 | + } |
|
| 305 | 305 | |
| 306 | - /** |
|
| 307 | - * Parse quoted 'attributeValue' string. |
|
| 308 | - * |
|
| 309 | - * @param int $offset Offset to starting quote |
|
| 310 | - * |
|
| 311 | - * @throws \UnexpectedValueException |
|
| 312 | - * |
|
| 313 | - * @return string |
|
| 314 | - */ |
|
| 315 | - private function _parseQuotedAttrString(int &$offset): string |
|
| 316 | - { |
|
| 317 | - $idx = $offset + 1; |
|
| 318 | - $val = ''; |
|
| 319 | - while ($idx < $this->_len) { |
|
| 320 | - $c = $this->_dn[$idx]; |
|
| 321 | - if ('\\' == $c) { // pair |
|
| 322 | - ++$idx; |
|
| 323 | - $val .= $this->_parsePairAfterSlash($idx); |
|
| 324 | - continue; |
|
| 325 | - } |
|
| 326 | - if ('"' == $c) { |
|
| 327 | - ++$idx; |
|
| 328 | - break; |
|
| 329 | - } |
|
| 330 | - $val .= $c; |
|
| 331 | - ++$idx; |
|
| 332 | - } |
|
| 333 | - $offset = $idx; |
|
| 334 | - return $val; |
|
| 335 | - } |
|
| 306 | + /** |
|
| 307 | + * Parse quoted 'attributeValue' string. |
|
| 308 | + * |
|
| 309 | + * @param int $offset Offset to starting quote |
|
| 310 | + * |
|
| 311 | + * @throws \UnexpectedValueException |
|
| 312 | + * |
|
| 313 | + * @return string |
|
| 314 | + */ |
|
| 315 | + private function _parseQuotedAttrString(int &$offset): string |
|
| 316 | + { |
|
| 317 | + $idx = $offset + 1; |
|
| 318 | + $val = ''; |
|
| 319 | + while ($idx < $this->_len) { |
|
| 320 | + $c = $this->_dn[$idx]; |
|
| 321 | + if ('\\' == $c) { // pair |
|
| 322 | + ++$idx; |
|
| 323 | + $val .= $this->_parsePairAfterSlash($idx); |
|
| 324 | + continue; |
|
| 325 | + } |
|
| 326 | + if ('"' == $c) { |
|
| 327 | + ++$idx; |
|
| 328 | + break; |
|
| 329 | + } |
|
| 330 | + $val .= $c; |
|
| 331 | + ++$idx; |
|
| 332 | + } |
|
| 333 | + $offset = $idx; |
|
| 334 | + return $val; |
|
| 335 | + } |
|
| 336 | 336 | |
| 337 | - /** |
|
| 338 | - * Parse 'attributeValue' of binary type. |
|
| 339 | - * |
|
| 340 | - * @param int $offset |
|
| 341 | - * |
|
| 342 | - * @throws \UnexpectedValueException |
|
| 343 | - * |
|
| 344 | - * @return string |
|
| 345 | - */ |
|
| 346 | - private function _parseAttrHexValue(int &$offset): string |
|
| 347 | - { |
|
| 348 | - $idx = $offset; |
|
| 349 | - $hexstr = $this->_regexMatch('/^(?:[0-9a-f]{2})+/i', $idx); |
|
| 350 | - if (null === $hexstr) { |
|
| 351 | - throw new \UnexpectedValueException('Invalid hexstring.'); |
|
| 352 | - } |
|
| 353 | - $data = hex2bin($hexstr); |
|
| 354 | - $offset = $idx; |
|
| 355 | - return $data; |
|
| 356 | - } |
|
| 337 | + /** |
|
| 338 | + * Parse 'attributeValue' of binary type. |
|
| 339 | + * |
|
| 340 | + * @param int $offset |
|
| 341 | + * |
|
| 342 | + * @throws \UnexpectedValueException |
|
| 343 | + * |
|
| 344 | + * @return string |
|
| 345 | + */ |
|
| 346 | + private function _parseAttrHexValue(int &$offset): string |
|
| 347 | + { |
|
| 348 | + $idx = $offset; |
|
| 349 | + $hexstr = $this->_regexMatch('/^(?:[0-9a-f]{2})+/i', $idx); |
|
| 350 | + if (null === $hexstr) { |
|
| 351 | + throw new \UnexpectedValueException('Invalid hexstring.'); |
|
| 352 | + } |
|
| 353 | + $data = hex2bin($hexstr); |
|
| 354 | + $offset = $idx; |
|
| 355 | + return $data; |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - /** |
|
| 359 | - * Parse 'pair' after leading slash. |
|
| 360 | - * |
|
| 361 | - * @param int $offset |
|
| 362 | - * |
|
| 363 | - * @throws \UnexpectedValueException |
|
| 364 | - * |
|
| 365 | - * @return string |
|
| 366 | - */ |
|
| 367 | - private function _parsePairAfterSlash(int &$offset): string |
|
| 368 | - { |
|
| 369 | - $idx = $offset; |
|
| 370 | - if ($idx >= $this->_len) { |
|
| 371 | - throw new \UnexpectedValueException( |
|
| 372 | - 'Unexpected end of escape sequence.'); |
|
| 373 | - } |
|
| 374 | - $c = $this->_dn[$idx++]; |
|
| 375 | - // special | \ | " | SPACE |
|
| 376 | - if (false !== strpos(self::SPECIAL_CHARS . '\\" ', $c)) { |
|
| 377 | - $val = $c; |
|
| 378 | - } else { // hexpair |
|
| 379 | - if ($idx >= $this->_len) { |
|
| 380 | - throw new \UnexpectedValueException('Unexpected end of hexpair.'); |
|
| 381 | - } |
|
| 382 | - $val = @hex2bin($c . $this->_dn[$idx++]); |
|
| 383 | - if (false === $val) { |
|
| 384 | - throw new \UnexpectedValueException('Invalid hexpair.'); |
|
| 385 | - } |
|
| 386 | - } |
|
| 387 | - $offset = $idx; |
|
| 388 | - return $val; |
|
| 389 | - } |
|
| 358 | + /** |
|
| 359 | + * Parse 'pair' after leading slash. |
|
| 360 | + * |
|
| 361 | + * @param int $offset |
|
| 362 | + * |
|
| 363 | + * @throws \UnexpectedValueException |
|
| 364 | + * |
|
| 365 | + * @return string |
|
| 366 | + */ |
|
| 367 | + private function _parsePairAfterSlash(int &$offset): string |
|
| 368 | + { |
|
| 369 | + $idx = $offset; |
|
| 370 | + if ($idx >= $this->_len) { |
|
| 371 | + throw new \UnexpectedValueException( |
|
| 372 | + 'Unexpected end of escape sequence.'); |
|
| 373 | + } |
|
| 374 | + $c = $this->_dn[$idx++]; |
|
| 375 | + // special | \ | " | SPACE |
|
| 376 | + if (false !== strpos(self::SPECIAL_CHARS . '\\" ', $c)) { |
|
| 377 | + $val = $c; |
|
| 378 | + } else { // hexpair |
|
| 379 | + if ($idx >= $this->_len) { |
|
| 380 | + throw new \UnexpectedValueException('Unexpected end of hexpair.'); |
|
| 381 | + } |
|
| 382 | + $val = @hex2bin($c . $this->_dn[$idx++]); |
|
| 383 | + if (false === $val) { |
|
| 384 | + throw new \UnexpectedValueException('Invalid hexpair.'); |
|
| 385 | + } |
|
| 386 | + } |
|
| 387 | + $offset = $idx; |
|
| 388 | + return $val; |
|
| 389 | + } |
|
| 390 | 390 | |
| 391 | - /** |
|
| 392 | - * Match DN to pattern and extract the last capture group. |
|
| 393 | - * |
|
| 394 | - * Updates offset to fully matched pattern. |
|
| 395 | - * |
|
| 396 | - * @param string $pattern |
|
| 397 | - * @param int $offset |
|
| 398 | - * |
|
| 399 | - * @return null|string Null if pattern doesn't match |
|
| 400 | - */ |
|
| 401 | - private function _regexMatch(string $pattern, int &$offset): ?string |
|
| 402 | - { |
|
| 403 | - $idx = $offset; |
|
| 404 | - if (!preg_match($pattern, substr($this->_dn, $idx), $match)) { |
|
| 405 | - return null; |
|
| 406 | - } |
|
| 407 | - $idx += strlen($match[0]); |
|
| 408 | - $offset = $idx; |
|
| 409 | - return end($match); |
|
| 410 | - } |
|
| 391 | + /** |
|
| 392 | + * Match DN to pattern and extract the last capture group. |
|
| 393 | + * |
|
| 394 | + * Updates offset to fully matched pattern. |
|
| 395 | + * |
|
| 396 | + * @param string $pattern |
|
| 397 | + * @param int $offset |
|
| 398 | + * |
|
| 399 | + * @return null|string Null if pattern doesn't match |
|
| 400 | + */ |
|
| 401 | + private function _regexMatch(string $pattern, int &$offset): ?string |
|
| 402 | + { |
|
| 403 | + $idx = $offset; |
|
| 404 | + if (!preg_match($pattern, substr($this->_dn, $idx), $match)) { |
|
| 405 | + return null; |
|
| 406 | + } |
|
| 407 | + $idx += strlen($match[0]); |
|
| 408 | + $offset = $idx; |
|
| 409 | + return end($match); |
|
| 410 | + } |
|
| 411 | 411 | |
| 412 | - /** |
|
| 413 | - * Skip consecutive spaces. |
|
| 414 | - * |
|
| 415 | - * @param int $offset |
|
| 416 | - */ |
|
| 417 | - private function _skipWs(int &$offset): void |
|
| 418 | - { |
|
| 419 | - $idx = $offset; |
|
| 420 | - while ($idx < $this->_len) { |
|
| 421 | - if (' ' != $this->_dn[$idx]) { |
|
| 422 | - break; |
|
| 423 | - } |
|
| 424 | - ++$idx; |
|
| 425 | - } |
|
| 426 | - $offset = $idx; |
|
| 427 | - } |
|
| 412 | + /** |
|
| 413 | + * Skip consecutive spaces. |
|
| 414 | + * |
|
| 415 | + * @param int $offset |
|
| 416 | + */ |
|
| 417 | + private function _skipWs(int &$offset): void |
|
| 418 | + { |
|
| 419 | + $idx = $offset; |
|
| 420 | + while ($idx < $this->_len) { |
|
| 421 | + if (' ' != $this->_dn[$idx]) { |
|
| 422 | + break; |
|
| 423 | + } |
|
| 424 | + ++$idx; |
|
| 425 | + } |
|
| 426 | + $offset = $idx; |
|
| 427 | + } |
|
| 428 | 428 | } |