@@ -16,108 +16,108 @@ |
||
16 | 16 | */ |
17 | 17 | class Available extends VObject\Component |
18 | 18 | { |
19 | - /** |
|
20 | - * Returns the 'effective start' and 'effective end' of this VAVAILABILITY |
|
21 | - * component. |
|
22 | - * |
|
23 | - * We use the DTSTART and DTEND or DURATION to determine this. |
|
24 | - * |
|
25 | - * The returned value is an array containing DateTimeImmutable instances. |
|
26 | - * If either the start or end is 'unbounded' its value will be null |
|
27 | - * instead. |
|
28 | - * |
|
29 | - * @return array |
|
30 | - */ |
|
31 | - public function getEffectiveStartEnd() |
|
32 | - { |
|
33 | - $effectiveStart = $this->DTSTART->getDateTime(); |
|
34 | - if (isset($this->DTEND)) { |
|
35 | - $effectiveEnd = $this->DTEND->getDateTime(); |
|
36 | - } else { |
|
37 | - $effectiveEnd = $effectiveStart->add(VObject\DateTimeParser::parseDuration($this->DURATION)); |
|
38 | - } |
|
19 | + /** |
|
20 | + * Returns the 'effective start' and 'effective end' of this VAVAILABILITY |
|
21 | + * component. |
|
22 | + * |
|
23 | + * We use the DTSTART and DTEND or DURATION to determine this. |
|
24 | + * |
|
25 | + * The returned value is an array containing DateTimeImmutable instances. |
|
26 | + * If either the start or end is 'unbounded' its value will be null |
|
27 | + * instead. |
|
28 | + * |
|
29 | + * @return array |
|
30 | + */ |
|
31 | + public function getEffectiveStartEnd() |
|
32 | + { |
|
33 | + $effectiveStart = $this->DTSTART->getDateTime(); |
|
34 | + if (isset($this->DTEND)) { |
|
35 | + $effectiveEnd = $this->DTEND->getDateTime(); |
|
36 | + } else { |
|
37 | + $effectiveEnd = $effectiveStart->add(VObject\DateTimeParser::parseDuration($this->DURATION)); |
|
38 | + } |
|
39 | 39 | |
40 | - return [$effectiveStart, $effectiveEnd]; |
|
41 | - } |
|
40 | + return [$effectiveStart, $effectiveEnd]; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * A simple list of validation rules. |
|
45 | - * |
|
46 | - * This is simply a list of properties, and how many times they either |
|
47 | - * must or must not appear. |
|
48 | - * |
|
49 | - * Possible values per property: |
|
50 | - * * 0 - Must not appear. |
|
51 | - * * 1 - Must appear exactly once. |
|
52 | - * * + - Must appear at least once. |
|
53 | - * * * - Can appear any number of times. |
|
54 | - * * ? - May appear, but not more than once. |
|
55 | - * |
|
56 | - * @var array |
|
57 | - */ |
|
58 | - public function getValidationRules() |
|
59 | - { |
|
60 | - return [ |
|
61 | - 'UID' => 1, |
|
62 | - 'DTSTART' => 1, |
|
63 | - 'DTSTAMP' => 1, |
|
43 | + /** |
|
44 | + * A simple list of validation rules. |
|
45 | + * |
|
46 | + * This is simply a list of properties, and how many times they either |
|
47 | + * must or must not appear. |
|
48 | + * |
|
49 | + * Possible values per property: |
|
50 | + * * 0 - Must not appear. |
|
51 | + * * 1 - Must appear exactly once. |
|
52 | + * * + - Must appear at least once. |
|
53 | + * * * - Can appear any number of times. |
|
54 | + * * ? - May appear, but not more than once. |
|
55 | + * |
|
56 | + * @var array |
|
57 | + */ |
|
58 | + public function getValidationRules() |
|
59 | + { |
|
60 | + return [ |
|
61 | + 'UID' => 1, |
|
62 | + 'DTSTART' => 1, |
|
63 | + 'DTSTAMP' => 1, |
|
64 | 64 | |
65 | - 'DTEND' => '?', |
|
66 | - 'DURATION' => '?', |
|
65 | + 'DTEND' => '?', |
|
66 | + 'DURATION' => '?', |
|
67 | 67 | |
68 | - 'CREATED' => '?', |
|
69 | - 'DESCRIPTION' => '?', |
|
70 | - 'LAST-MODIFIED' => '?', |
|
71 | - 'RECURRENCE-ID' => '?', |
|
72 | - 'RRULE' => '?', |
|
73 | - 'SUMMARY' => '?', |
|
68 | + 'CREATED' => '?', |
|
69 | + 'DESCRIPTION' => '?', |
|
70 | + 'LAST-MODIFIED' => '?', |
|
71 | + 'RECURRENCE-ID' => '?', |
|
72 | + 'RRULE' => '?', |
|
73 | + 'SUMMARY' => '?', |
|
74 | 74 | |
75 | - 'CATEGORIES' => '*', |
|
76 | - 'COMMENT' => '*', |
|
77 | - 'CONTACT' => '*', |
|
78 | - 'EXDATE' => '*', |
|
79 | - 'RDATE' => '*', |
|
75 | + 'CATEGORIES' => '*', |
|
76 | + 'COMMENT' => '*', |
|
77 | + 'CONTACT' => '*', |
|
78 | + 'EXDATE' => '*', |
|
79 | + 'RDATE' => '*', |
|
80 | 80 | |
81 | - 'AVAILABLE' => '*', |
|
82 | - ]; |
|
83 | - } |
|
81 | + 'AVAILABLE' => '*', |
|
82 | + ]; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Validates the node for correctness. |
|
87 | - * |
|
88 | - * The following options are supported: |
|
89 | - * Node::REPAIR - May attempt to automatically repair the problem. |
|
90 | - * Node::PROFILE_CARDDAV - Validate the vCard for CardDAV purposes. |
|
91 | - * Node::PROFILE_CALDAV - Validate the iCalendar for CalDAV purposes. |
|
92 | - * |
|
93 | - * This method returns an array with detected problems. |
|
94 | - * Every element has the following properties: |
|
95 | - * |
|
96 | - * * level - problem level. |
|
97 | - * * message - A human-readable string describing the issue. |
|
98 | - * * node - A reference to the problematic node. |
|
99 | - * |
|
100 | - * The level means: |
|
101 | - * 1 - The issue was repaired (only happens if REPAIR was turned on). |
|
102 | - * 2 - A warning. |
|
103 | - * 3 - An error. |
|
104 | - * |
|
105 | - * @param int $options |
|
106 | - * |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - public function validate($options = 0) |
|
110 | - { |
|
111 | - $result = parent::validate($options); |
|
85 | + /** |
|
86 | + * Validates the node for correctness. |
|
87 | + * |
|
88 | + * The following options are supported: |
|
89 | + * Node::REPAIR - May attempt to automatically repair the problem. |
|
90 | + * Node::PROFILE_CARDDAV - Validate the vCard for CardDAV purposes. |
|
91 | + * Node::PROFILE_CALDAV - Validate the iCalendar for CalDAV purposes. |
|
92 | + * |
|
93 | + * This method returns an array with detected problems. |
|
94 | + * Every element has the following properties: |
|
95 | + * |
|
96 | + * * level - problem level. |
|
97 | + * * message - A human-readable string describing the issue. |
|
98 | + * * node - A reference to the problematic node. |
|
99 | + * |
|
100 | + * The level means: |
|
101 | + * 1 - The issue was repaired (only happens if REPAIR was turned on). |
|
102 | + * 2 - A warning. |
|
103 | + * 3 - An error. |
|
104 | + * |
|
105 | + * @param int $options |
|
106 | + * |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + public function validate($options = 0) |
|
110 | + { |
|
111 | + $result = parent::validate($options); |
|
112 | 112 | |
113 | - if (isset($this->DTEND) && isset($this->DURATION)) { |
|
114 | - $result[] = [ |
|
115 | - 'level' => 3, |
|
116 | - 'message' => 'DTEND and DURATION cannot both be present', |
|
117 | - 'node' => $this, |
|
118 | - ]; |
|
119 | - } |
|
113 | + if (isset($this->DTEND) && isset($this->DURATION)) { |
|
114 | + $result[] = [ |
|
115 | + 'level' => 3, |
|
116 | + 'message' => 'DTEND and DURATION cannot both be present', |
|
117 | + 'node' => $this, |
|
118 | + ]; |
|
119 | + } |
|
120 | 120 | |
121 | - return $result; |
|
122 | - } |
|
121 | + return $result; |
|
122 | + } |
|
123 | 123 | } |
@@ -15,52 +15,52 @@ |
||
15 | 15 | */ |
16 | 16 | class UUIDUtil |
17 | 17 | { |
18 | - /** |
|
19 | - * Returns a pseudo-random v4 UUID. |
|
20 | - * |
|
21 | - * This function is based on a comment by Andrew Moore on php.net |
|
22 | - * |
|
23 | - * @see http://www.php.net/manual/en/function.uniqid.php#94959 |
|
24 | - * |
|
25 | - * @return string |
|
26 | - */ |
|
27 | - public static function getUUID() |
|
28 | - { |
|
29 | - return sprintf( |
|
30 | - '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
18 | + /** |
|
19 | + * Returns a pseudo-random v4 UUID. |
|
20 | + * |
|
21 | + * This function is based on a comment by Andrew Moore on php.net |
|
22 | + * |
|
23 | + * @see http://www.php.net/manual/en/function.uniqid.php#94959 |
|
24 | + * |
|
25 | + * @return string |
|
26 | + */ |
|
27 | + public static function getUUID() |
|
28 | + { |
|
29 | + return sprintf( |
|
30 | + '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
31 | 31 | |
32 | - // 32 bits for "time_low" |
|
33 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
32 | + // 32 bits for "time_low" |
|
33 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
34 | 34 | |
35 | - // 16 bits for "time_mid" |
|
36 | - mt_rand(0, 0xffff), |
|
35 | + // 16 bits for "time_mid" |
|
36 | + mt_rand(0, 0xffff), |
|
37 | 37 | |
38 | - // 16 bits for "time_hi_and_version", |
|
39 | - // four most significant bits holds version number 4 |
|
40 | - mt_rand(0, 0x0fff) | 0x4000, |
|
38 | + // 16 bits for "time_hi_and_version", |
|
39 | + // four most significant bits holds version number 4 |
|
40 | + mt_rand(0, 0x0fff) | 0x4000, |
|
41 | 41 | |
42 | - // 16 bits, 8 bits for "clk_seq_hi_res", |
|
43 | - // 8 bits for "clk_seq_low", |
|
44 | - // two most significant bits holds zero and one for variant DCE1.1 |
|
45 | - mt_rand(0, 0x3fff) | 0x8000, |
|
42 | + // 16 bits, 8 bits for "clk_seq_hi_res", |
|
43 | + // 8 bits for "clk_seq_low", |
|
44 | + // two most significant bits holds zero and one for variant DCE1.1 |
|
45 | + mt_rand(0, 0x3fff) | 0x8000, |
|
46 | 46 | |
47 | - // 48 bits for "node" |
|
48 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) |
|
49 | - ); |
|
50 | - } |
|
47 | + // 48 bits for "node" |
|
48 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) |
|
49 | + ); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Checks if a string is a valid UUID. |
|
54 | - * |
|
55 | - * @param string $uuid |
|
56 | - * |
|
57 | - * @return bool |
|
58 | - */ |
|
59 | - public static function validateUUID($uuid) |
|
60 | - { |
|
61 | - return 0 !== preg_match( |
|
62 | - '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i', |
|
63 | - $uuid |
|
64 | - ); |
|
65 | - } |
|
52 | + /** |
|
53 | + * Checks if a string is a valid UUID. |
|
54 | + * |
|
55 | + * @param string $uuid |
|
56 | + * |
|
57 | + * @return bool |
|
58 | + */ |
|
59 | + public static function validateUUID($uuid) |
|
60 | + { |
|
61 | + return 0 !== preg_match( |
|
62 | + '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i', |
|
63 | + $uuid |
|
64 | + ); |
|
65 | + } |
|
66 | 66 | } |
@@ -11,411 +11,411 @@ |
||
11 | 11 | */ |
12 | 12 | class VCardConverter |
13 | 13 | { |
14 | - /** |
|
15 | - * Converts a vCard object to a new version. |
|
16 | - * |
|
17 | - * targetVersion must be one of: |
|
18 | - * Document::VCARD21 |
|
19 | - * Document::VCARD30 |
|
20 | - * Document::VCARD40 |
|
21 | - * |
|
22 | - * Currently only 3.0 and 4.0 as input and output versions. |
|
23 | - * |
|
24 | - * 2.1 has some minor support for the input version, it's incomplete at the |
|
25 | - * moment though. |
|
26 | - * |
|
27 | - * If input and output version are identical, a clone is returned. |
|
28 | - * |
|
29 | - * @param int $targetVersion |
|
30 | - */ |
|
31 | - public function convert(Component\VCard $input, $targetVersion) |
|
32 | - { |
|
33 | - $inputVersion = $input->getDocumentType(); |
|
34 | - if ($inputVersion === $targetVersion) { |
|
35 | - return clone $input; |
|
36 | - } |
|
37 | - |
|
38 | - if (!in_array($inputVersion, [Document::VCARD21, Document::VCARD30, Document::VCARD40])) { |
|
39 | - throw new \InvalidArgumentException('Only vCard 2.1, 3.0 and 4.0 are supported for the input data'); |
|
40 | - } |
|
41 | - if (!in_array($targetVersion, [Document::VCARD30, Document::VCARD40])) { |
|
42 | - throw new \InvalidArgumentException('You can only use vCard 3.0 or 4.0 for the target version'); |
|
43 | - } |
|
44 | - |
|
45 | - $newVersion = Document::VCARD40 === $targetVersion ? '4.0' : '3.0'; |
|
46 | - |
|
47 | - $output = new Component\VCard([ |
|
48 | - 'VERSION' => $newVersion, |
|
49 | - ]); |
|
50 | - |
|
51 | - // We might have generated a default UID. Remove it! |
|
52 | - unset($output->UID); |
|
53 | - |
|
54 | - foreach ($input->children() as $property) { |
|
55 | - $this->convertProperty($input, $output, $property, $targetVersion); |
|
56 | - } |
|
57 | - |
|
58 | - return $output; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Handles conversion of a single property. |
|
63 | - * |
|
64 | - * @param int $targetVersion |
|
65 | - */ |
|
66 | - protected function convertProperty(Component\VCard $input, Component\VCard $output, Property $property, $targetVersion) |
|
67 | - { |
|
68 | - // Skipping these, those are automatically added. |
|
69 | - if (in_array($property->name, ['VERSION', 'PRODID'])) { |
|
70 | - return; |
|
71 | - } |
|
72 | - |
|
73 | - $parameters = $property->parameters(); |
|
74 | - $valueType = null; |
|
75 | - if (isset($parameters['VALUE'])) { |
|
76 | - $valueType = $parameters['VALUE']->getValue(); |
|
77 | - unset($parameters['VALUE']); |
|
78 | - } |
|
79 | - if (!$valueType) { |
|
80 | - $valueType = $property->getValueType(); |
|
81 | - } |
|
82 | - if (Document::VCARD30 !== $targetVersion && 'PHONE-NUMBER' === $valueType) { |
|
83 | - $valueType = null; |
|
84 | - } |
|
85 | - $newProperty = $output->createProperty( |
|
86 | - $property->name, |
|
87 | - $property->getParts(), |
|
88 | - [], // parameters will get added a bit later. |
|
89 | - $valueType |
|
90 | - ); |
|
91 | - |
|
92 | - if (Document::VCARD30 === $targetVersion) { |
|
93 | - if ($property instanceof Property\Uri && in_array($property->name, ['PHOTO', 'LOGO', 'SOUND'])) { |
|
94 | - $newProperty = $this->convertUriToBinary($output, $newProperty); |
|
95 | - } elseif ($property instanceof Property\VCard\DateAndOrTime) { |
|
96 | - // In vCard 4, the birth year may be optional. This is not the |
|
97 | - // case for vCard 3. Apple has a workaround for this that |
|
98 | - // allows applications that support Apple's extension still |
|
99 | - // omit birthyears in vCard 3, but applications that do not |
|
100 | - // support this, will just use a random birthyear. We're |
|
101 | - // choosing 1604 for the birthyear, because that's what apple |
|
102 | - // uses. |
|
103 | - $parts = DateTimeParser::parseVCardDateTime($property->getValue()); |
|
104 | - if (is_null($parts['year'])) { |
|
105 | - $newValue = '1604-'.$parts['month'].'-'.$parts['date']; |
|
106 | - $newProperty->setValue($newValue); |
|
107 | - $newProperty['X-APPLE-OMIT-YEAR'] = '1604'; |
|
108 | - } |
|
109 | - |
|
110 | - if ('ANNIVERSARY' == $newProperty->name) { |
|
111 | - // Microsoft non-standard anniversary |
|
112 | - $newProperty->name = 'X-ANNIVERSARY'; |
|
113 | - |
|
114 | - // We also need to add a new apple property for the same |
|
115 | - // purpose. This apple property needs a 'label' in the same |
|
116 | - // group, so we first need to find a groupname that doesn't |
|
117 | - // exist yet. |
|
118 | - $x = 1; |
|
119 | - while ($output->select('ITEM'.$x.'.')) { |
|
120 | - ++$x; |
|
121 | - } |
|
122 | - $output->add('ITEM'.$x.'.X-ABDATE', $newProperty->getValue(), ['VALUE' => 'DATE-AND-OR-TIME']); |
|
123 | - $output->add('ITEM'.$x.'.X-ABLABEL', '_$!<Anniversary>!$_'); |
|
124 | - } |
|
125 | - } elseif ('KIND' === $property->name) { |
|
126 | - switch (strtolower($property->getValue())) { |
|
127 | - case 'org': |
|
128 | - // vCard 3.0 does not have an equivalent to KIND:ORG, |
|
129 | - // but apple has an extension that means the same |
|
130 | - // thing. |
|
131 | - $newProperty = $output->createProperty('X-ABSHOWAS', 'COMPANY'); |
|
132 | - break; |
|
133 | - |
|
134 | - case 'individual': |
|
135 | - // Individual is implicit, so we skip it. |
|
136 | - return; |
|
137 | - |
|
138 | - case 'group': |
|
139 | - // OS X addressbook property |
|
140 | - $newProperty = $output->createProperty('X-ADDRESSBOOKSERVER-KIND', 'GROUP'); |
|
141 | - break; |
|
142 | - } |
|
143 | - } elseif ('MEMBER' === $property->name) { |
|
144 | - $newProperty = $output->createProperty('X-ADDRESSBOOKSERVER-MEMBER', $property->getValue()); |
|
145 | - } |
|
146 | - } elseif (Document::VCARD40 === $targetVersion) { |
|
147 | - // These properties were removed in vCard 4.0 |
|
148 | - if (in_array($property->name, ['NAME', 'MAILER', 'LABEL', 'CLASS'])) { |
|
149 | - return; |
|
150 | - } |
|
151 | - |
|
152 | - if ($property instanceof Property\Binary) { |
|
153 | - $newProperty = $this->convertBinaryToUri($output, $newProperty, $parameters); |
|
154 | - } elseif ($property instanceof Property\VCard\DateAndOrTime && isset($parameters['X-APPLE-OMIT-YEAR'])) { |
|
155 | - // If a property such as BDAY contained 'X-APPLE-OMIT-YEAR', |
|
156 | - // then we're stripping the year from the vcard 4 value. |
|
157 | - $parts = DateTimeParser::parseVCardDateTime($property->getValue()); |
|
158 | - if ($parts['year'] === $property['X-APPLE-OMIT-YEAR']->getValue()) { |
|
159 | - $newValue = '--'.$parts['month'].'-'.$parts['date']; |
|
160 | - $newProperty->setValue($newValue); |
|
161 | - } |
|
162 | - |
|
163 | - // Regardless if the year matched or not, we do need to strip |
|
164 | - // X-APPLE-OMIT-YEAR. |
|
165 | - unset($parameters['X-APPLE-OMIT-YEAR']); |
|
166 | - } |
|
167 | - switch ($property->name) { |
|
168 | - case 'X-ABSHOWAS': |
|
169 | - if ('COMPANY' === strtoupper($property->getValue())) { |
|
170 | - $newProperty = $output->createProperty('KIND', 'ORG'); |
|
171 | - } |
|
172 | - break; |
|
173 | - case 'X-ADDRESSBOOKSERVER-KIND': |
|
174 | - if ('GROUP' === strtoupper($property->getValue())) { |
|
175 | - $newProperty = $output->createProperty('KIND', 'GROUP'); |
|
176 | - } |
|
177 | - break; |
|
178 | - case 'X-ADDRESSBOOKSERVER-MEMBER': |
|
179 | - $newProperty = $output->createProperty('MEMBER', $property->getValue()); |
|
180 | - break; |
|
181 | - case 'X-ANNIVERSARY': |
|
182 | - $newProperty->name = 'ANNIVERSARY'; |
|
183 | - // If we already have an anniversary property with the same |
|
184 | - // value, ignore. |
|
185 | - foreach ($output->select('ANNIVERSARY') as $anniversary) { |
|
186 | - if ($anniversary->getValue() === $newProperty->getValue()) { |
|
187 | - return; |
|
188 | - } |
|
189 | - } |
|
190 | - break; |
|
191 | - case 'X-ABDATE': |
|
192 | - // Find out what the label was, if it exists. |
|
193 | - if (!$property->group) { |
|
194 | - break; |
|
195 | - } |
|
196 | - $label = $input->{$property->group.'.X-ABLABEL'}; |
|
197 | - |
|
198 | - // We only support converting anniversaries. |
|
199 | - if (!$label || '_$!<Anniversary>!$_' !== $label->getValue()) { |
|
200 | - break; |
|
201 | - } |
|
202 | - |
|
203 | - // If we already have an anniversary property with the same |
|
204 | - // value, ignore. |
|
205 | - foreach ($output->select('ANNIVERSARY') as $anniversary) { |
|
206 | - if ($anniversary->getValue() === $newProperty->getValue()) { |
|
207 | - return; |
|
208 | - } |
|
209 | - } |
|
210 | - $newProperty->name = 'ANNIVERSARY'; |
|
211 | - break; |
|
212 | - // Apple's per-property label system. |
|
213 | - case 'X-ABLABEL': |
|
214 | - if ('_$!<Anniversary>!$_' === $newProperty->getValue()) { |
|
215 | - // We can safely remove these, as they are converted to |
|
216 | - // ANNIVERSARY properties. |
|
217 | - return; |
|
218 | - } |
|
219 | - break; |
|
220 | - } |
|
221 | - } |
|
222 | - |
|
223 | - // set property group |
|
224 | - $newProperty->group = $property->group; |
|
225 | - |
|
226 | - if (Document::VCARD40 === $targetVersion) { |
|
227 | - $this->convertParameters40($newProperty, $parameters); |
|
228 | - } else { |
|
229 | - $this->convertParameters30($newProperty, $parameters); |
|
230 | - } |
|
231 | - |
|
232 | - // Lastly, we need to see if there's a need for a VALUE parameter. |
|
233 | - // |
|
234 | - // We can do that by instantiating a empty property with that name, and |
|
235 | - // seeing if the default valueType is identical to the current one. |
|
236 | - $tempProperty = $output->createProperty($newProperty->name); |
|
237 | - if ($tempProperty->getValueType() !== $newProperty->getValueType()) { |
|
238 | - $newProperty['VALUE'] = $newProperty->getValueType(); |
|
239 | - } |
|
240 | - |
|
241 | - $output->add($newProperty); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Converts a BINARY property to a URI property. |
|
246 | - * |
|
247 | - * vCard 4.0 no longer supports BINARY properties. |
|
248 | - * |
|
249 | - * @param Property\Uri $property the input property |
|
250 | - * @param $parameters list of parameters that will eventually be added to |
|
251 | - * the new property |
|
252 | - * |
|
253 | - * @return Property\Uri |
|
254 | - */ |
|
255 | - protected function convertBinaryToUri(Component\VCard $output, Property\Binary $newProperty, array &$parameters) |
|
256 | - { |
|
257 | - $value = $newProperty->getValue(); |
|
258 | - $newProperty = $output->createProperty( |
|
259 | - $newProperty->name, |
|
260 | - null, // no value |
|
261 | - [], // no parameters yet |
|
262 | - 'URI' // Forcing the BINARY type |
|
263 | - ); |
|
264 | - |
|
265 | - $mimeType = 'application/octet-stream'; |
|
266 | - |
|
267 | - // See if we can find a better mimetype. |
|
268 | - if (isset($parameters['TYPE'])) { |
|
269 | - $newTypes = []; |
|
270 | - foreach ($parameters['TYPE']->getParts() as $typePart) { |
|
271 | - if (in_array( |
|
272 | - strtoupper($typePart), |
|
273 | - ['JPEG', 'PNG', 'GIF'] |
|
274 | - )) { |
|
275 | - $mimeType = 'image/'.strtolower($typePart); |
|
276 | - } else { |
|
277 | - $newTypes[] = $typePart; |
|
278 | - } |
|
279 | - } |
|
280 | - |
|
281 | - // If there were any parameters we're not converting to a |
|
282 | - // mime-type, we need to keep them. |
|
283 | - if ($newTypes) { |
|
284 | - $parameters['TYPE']->setParts($newTypes); |
|
285 | - } else { |
|
286 | - unset($parameters['TYPE']); |
|
287 | - } |
|
288 | - } |
|
289 | - |
|
290 | - $newProperty->setValue('data:'.$mimeType.';base64,'.base64_encode($value)); |
|
291 | - |
|
292 | - return $newProperty; |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * Converts a URI property to a BINARY property. |
|
297 | - * |
|
298 | - * In vCard 4.0 attachments are encoded as data: uri. Even though these may |
|
299 | - * be valid in vCard 3.0 as well, we should convert those to BINARY if |
|
300 | - * possible, to improve compatibility. |
|
301 | - * |
|
302 | - * @param Property\Uri $property the input property |
|
303 | - * |
|
304 | - * @return Property\Binary|null |
|
305 | - */ |
|
306 | - protected function convertUriToBinary(Component\VCard $output, Property\Uri $newProperty) |
|
307 | - { |
|
308 | - $value = $newProperty->getValue(); |
|
309 | - |
|
310 | - // Only converting data: uris |
|
311 | - if ('data:' !== substr($value, 0, 5)) { |
|
312 | - return $newProperty; |
|
313 | - } |
|
314 | - |
|
315 | - $newProperty = $output->createProperty( |
|
316 | - $newProperty->name, |
|
317 | - null, // no value |
|
318 | - [], // no parameters yet |
|
319 | - 'BINARY' |
|
320 | - ); |
|
321 | - |
|
322 | - $mimeType = substr($value, 5, strpos($value, ',') - 5); |
|
323 | - if (strpos($mimeType, ';')) { |
|
324 | - $mimeType = substr($mimeType, 0, strpos($mimeType, ';')); |
|
325 | - $newProperty->setValue(base64_decode(substr($value, strpos($value, ',') + 1))); |
|
326 | - } else { |
|
327 | - $newProperty->setValue(substr($value, strpos($value, ',') + 1)); |
|
328 | - } |
|
329 | - unset($value); |
|
330 | - |
|
331 | - $newProperty['ENCODING'] = 'b'; |
|
332 | - switch ($mimeType) { |
|
333 | - case 'image/jpeg': |
|
334 | - $newProperty['TYPE'] = 'JPEG'; |
|
335 | - break; |
|
336 | - case 'image/png': |
|
337 | - $newProperty['TYPE'] = 'PNG'; |
|
338 | - break; |
|
339 | - case 'image/gif': |
|
340 | - $newProperty['TYPE'] = 'GIF'; |
|
341 | - break; |
|
342 | - } |
|
343 | - |
|
344 | - return $newProperty; |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * Adds parameters to a new property for vCard 4.0. |
|
349 | - */ |
|
350 | - protected function convertParameters40(Property $newProperty, array $parameters) |
|
351 | - { |
|
352 | - // Adding all parameters. |
|
353 | - foreach ($parameters as $param) { |
|
354 | - // vCard 2.1 allowed parameters with no name |
|
355 | - if ($param->noName) { |
|
356 | - $param->noName = false; |
|
357 | - } |
|
358 | - |
|
359 | - switch ($param->name) { |
|
360 | - // We need to see if there's any TYPE=PREF, because in vCard 4 |
|
361 | - // that's now PREF=1. |
|
362 | - case 'TYPE': |
|
363 | - foreach ($param->getParts() as $paramPart) { |
|
364 | - if ('PREF' === strtoupper($paramPart)) { |
|
365 | - $newProperty->add('PREF', '1'); |
|
366 | - } else { |
|
367 | - $newProperty->add($param->name, $paramPart); |
|
368 | - } |
|
369 | - } |
|
370 | - break; |
|
371 | - // These no longer exist in vCard 4 |
|
372 | - case 'ENCODING': |
|
373 | - case 'CHARSET': |
|
374 | - break; |
|
375 | - |
|
376 | - default: |
|
377 | - $newProperty->add($param->name, $param->getParts()); |
|
378 | - break; |
|
379 | - } |
|
380 | - } |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * Adds parameters to a new property for vCard 3.0. |
|
385 | - */ |
|
386 | - protected function convertParameters30(Property $newProperty, array $parameters) |
|
387 | - { |
|
388 | - // Adding all parameters. |
|
389 | - foreach ($parameters as $param) { |
|
390 | - // vCard 2.1 allowed parameters with no name |
|
391 | - if ($param->noName) { |
|
392 | - $param->noName = false; |
|
393 | - } |
|
394 | - |
|
395 | - switch ($param->name) { |
|
396 | - case 'ENCODING': |
|
397 | - // This value only existed in vCard 2.1, and should be |
|
398 | - // removed for anything else. |
|
399 | - if ('QUOTED-PRINTABLE' !== strtoupper($param->getValue())) { |
|
400 | - $newProperty->add($param->name, $param->getParts()); |
|
401 | - } |
|
402 | - break; |
|
403 | - |
|
404 | - /* |
|
14 | + /** |
|
15 | + * Converts a vCard object to a new version. |
|
16 | + * |
|
17 | + * targetVersion must be one of: |
|
18 | + * Document::VCARD21 |
|
19 | + * Document::VCARD30 |
|
20 | + * Document::VCARD40 |
|
21 | + * |
|
22 | + * Currently only 3.0 and 4.0 as input and output versions. |
|
23 | + * |
|
24 | + * 2.1 has some minor support for the input version, it's incomplete at the |
|
25 | + * moment though. |
|
26 | + * |
|
27 | + * If input and output version are identical, a clone is returned. |
|
28 | + * |
|
29 | + * @param int $targetVersion |
|
30 | + */ |
|
31 | + public function convert(Component\VCard $input, $targetVersion) |
|
32 | + { |
|
33 | + $inputVersion = $input->getDocumentType(); |
|
34 | + if ($inputVersion === $targetVersion) { |
|
35 | + return clone $input; |
|
36 | + } |
|
37 | + |
|
38 | + if (!in_array($inputVersion, [Document::VCARD21, Document::VCARD30, Document::VCARD40])) { |
|
39 | + throw new \InvalidArgumentException('Only vCard 2.1, 3.0 and 4.0 are supported for the input data'); |
|
40 | + } |
|
41 | + if (!in_array($targetVersion, [Document::VCARD30, Document::VCARD40])) { |
|
42 | + throw new \InvalidArgumentException('You can only use vCard 3.0 or 4.0 for the target version'); |
|
43 | + } |
|
44 | + |
|
45 | + $newVersion = Document::VCARD40 === $targetVersion ? '4.0' : '3.0'; |
|
46 | + |
|
47 | + $output = new Component\VCard([ |
|
48 | + 'VERSION' => $newVersion, |
|
49 | + ]); |
|
50 | + |
|
51 | + // We might have generated a default UID. Remove it! |
|
52 | + unset($output->UID); |
|
53 | + |
|
54 | + foreach ($input->children() as $property) { |
|
55 | + $this->convertProperty($input, $output, $property, $targetVersion); |
|
56 | + } |
|
57 | + |
|
58 | + return $output; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Handles conversion of a single property. |
|
63 | + * |
|
64 | + * @param int $targetVersion |
|
65 | + */ |
|
66 | + protected function convertProperty(Component\VCard $input, Component\VCard $output, Property $property, $targetVersion) |
|
67 | + { |
|
68 | + // Skipping these, those are automatically added. |
|
69 | + if (in_array($property->name, ['VERSION', 'PRODID'])) { |
|
70 | + return; |
|
71 | + } |
|
72 | + |
|
73 | + $parameters = $property->parameters(); |
|
74 | + $valueType = null; |
|
75 | + if (isset($parameters['VALUE'])) { |
|
76 | + $valueType = $parameters['VALUE']->getValue(); |
|
77 | + unset($parameters['VALUE']); |
|
78 | + } |
|
79 | + if (!$valueType) { |
|
80 | + $valueType = $property->getValueType(); |
|
81 | + } |
|
82 | + if (Document::VCARD30 !== $targetVersion && 'PHONE-NUMBER' === $valueType) { |
|
83 | + $valueType = null; |
|
84 | + } |
|
85 | + $newProperty = $output->createProperty( |
|
86 | + $property->name, |
|
87 | + $property->getParts(), |
|
88 | + [], // parameters will get added a bit later. |
|
89 | + $valueType |
|
90 | + ); |
|
91 | + |
|
92 | + if (Document::VCARD30 === $targetVersion) { |
|
93 | + if ($property instanceof Property\Uri && in_array($property->name, ['PHOTO', 'LOGO', 'SOUND'])) { |
|
94 | + $newProperty = $this->convertUriToBinary($output, $newProperty); |
|
95 | + } elseif ($property instanceof Property\VCard\DateAndOrTime) { |
|
96 | + // In vCard 4, the birth year may be optional. This is not the |
|
97 | + // case for vCard 3. Apple has a workaround for this that |
|
98 | + // allows applications that support Apple's extension still |
|
99 | + // omit birthyears in vCard 3, but applications that do not |
|
100 | + // support this, will just use a random birthyear. We're |
|
101 | + // choosing 1604 for the birthyear, because that's what apple |
|
102 | + // uses. |
|
103 | + $parts = DateTimeParser::parseVCardDateTime($property->getValue()); |
|
104 | + if (is_null($parts['year'])) { |
|
105 | + $newValue = '1604-'.$parts['month'].'-'.$parts['date']; |
|
106 | + $newProperty->setValue($newValue); |
|
107 | + $newProperty['X-APPLE-OMIT-YEAR'] = '1604'; |
|
108 | + } |
|
109 | + |
|
110 | + if ('ANNIVERSARY' == $newProperty->name) { |
|
111 | + // Microsoft non-standard anniversary |
|
112 | + $newProperty->name = 'X-ANNIVERSARY'; |
|
113 | + |
|
114 | + // We also need to add a new apple property for the same |
|
115 | + // purpose. This apple property needs a 'label' in the same |
|
116 | + // group, so we first need to find a groupname that doesn't |
|
117 | + // exist yet. |
|
118 | + $x = 1; |
|
119 | + while ($output->select('ITEM'.$x.'.')) { |
|
120 | + ++$x; |
|
121 | + } |
|
122 | + $output->add('ITEM'.$x.'.X-ABDATE', $newProperty->getValue(), ['VALUE' => 'DATE-AND-OR-TIME']); |
|
123 | + $output->add('ITEM'.$x.'.X-ABLABEL', '_$!<Anniversary>!$_'); |
|
124 | + } |
|
125 | + } elseif ('KIND' === $property->name) { |
|
126 | + switch (strtolower($property->getValue())) { |
|
127 | + case 'org': |
|
128 | + // vCard 3.0 does not have an equivalent to KIND:ORG, |
|
129 | + // but apple has an extension that means the same |
|
130 | + // thing. |
|
131 | + $newProperty = $output->createProperty('X-ABSHOWAS', 'COMPANY'); |
|
132 | + break; |
|
133 | + |
|
134 | + case 'individual': |
|
135 | + // Individual is implicit, so we skip it. |
|
136 | + return; |
|
137 | + |
|
138 | + case 'group': |
|
139 | + // OS X addressbook property |
|
140 | + $newProperty = $output->createProperty('X-ADDRESSBOOKSERVER-KIND', 'GROUP'); |
|
141 | + break; |
|
142 | + } |
|
143 | + } elseif ('MEMBER' === $property->name) { |
|
144 | + $newProperty = $output->createProperty('X-ADDRESSBOOKSERVER-MEMBER', $property->getValue()); |
|
145 | + } |
|
146 | + } elseif (Document::VCARD40 === $targetVersion) { |
|
147 | + // These properties were removed in vCard 4.0 |
|
148 | + if (in_array($property->name, ['NAME', 'MAILER', 'LABEL', 'CLASS'])) { |
|
149 | + return; |
|
150 | + } |
|
151 | + |
|
152 | + if ($property instanceof Property\Binary) { |
|
153 | + $newProperty = $this->convertBinaryToUri($output, $newProperty, $parameters); |
|
154 | + } elseif ($property instanceof Property\VCard\DateAndOrTime && isset($parameters['X-APPLE-OMIT-YEAR'])) { |
|
155 | + // If a property such as BDAY contained 'X-APPLE-OMIT-YEAR', |
|
156 | + // then we're stripping the year from the vcard 4 value. |
|
157 | + $parts = DateTimeParser::parseVCardDateTime($property->getValue()); |
|
158 | + if ($parts['year'] === $property['X-APPLE-OMIT-YEAR']->getValue()) { |
|
159 | + $newValue = '--'.$parts['month'].'-'.$parts['date']; |
|
160 | + $newProperty->setValue($newValue); |
|
161 | + } |
|
162 | + |
|
163 | + // Regardless if the year matched or not, we do need to strip |
|
164 | + // X-APPLE-OMIT-YEAR. |
|
165 | + unset($parameters['X-APPLE-OMIT-YEAR']); |
|
166 | + } |
|
167 | + switch ($property->name) { |
|
168 | + case 'X-ABSHOWAS': |
|
169 | + if ('COMPANY' === strtoupper($property->getValue())) { |
|
170 | + $newProperty = $output->createProperty('KIND', 'ORG'); |
|
171 | + } |
|
172 | + break; |
|
173 | + case 'X-ADDRESSBOOKSERVER-KIND': |
|
174 | + if ('GROUP' === strtoupper($property->getValue())) { |
|
175 | + $newProperty = $output->createProperty('KIND', 'GROUP'); |
|
176 | + } |
|
177 | + break; |
|
178 | + case 'X-ADDRESSBOOKSERVER-MEMBER': |
|
179 | + $newProperty = $output->createProperty('MEMBER', $property->getValue()); |
|
180 | + break; |
|
181 | + case 'X-ANNIVERSARY': |
|
182 | + $newProperty->name = 'ANNIVERSARY'; |
|
183 | + // If we already have an anniversary property with the same |
|
184 | + // value, ignore. |
|
185 | + foreach ($output->select('ANNIVERSARY') as $anniversary) { |
|
186 | + if ($anniversary->getValue() === $newProperty->getValue()) { |
|
187 | + return; |
|
188 | + } |
|
189 | + } |
|
190 | + break; |
|
191 | + case 'X-ABDATE': |
|
192 | + // Find out what the label was, if it exists. |
|
193 | + if (!$property->group) { |
|
194 | + break; |
|
195 | + } |
|
196 | + $label = $input->{$property->group.'.X-ABLABEL'}; |
|
197 | + |
|
198 | + // We only support converting anniversaries. |
|
199 | + if (!$label || '_$!<Anniversary>!$_' !== $label->getValue()) { |
|
200 | + break; |
|
201 | + } |
|
202 | + |
|
203 | + // If we already have an anniversary property with the same |
|
204 | + // value, ignore. |
|
205 | + foreach ($output->select('ANNIVERSARY') as $anniversary) { |
|
206 | + if ($anniversary->getValue() === $newProperty->getValue()) { |
|
207 | + return; |
|
208 | + } |
|
209 | + } |
|
210 | + $newProperty->name = 'ANNIVERSARY'; |
|
211 | + break; |
|
212 | + // Apple's per-property label system. |
|
213 | + case 'X-ABLABEL': |
|
214 | + if ('_$!<Anniversary>!$_' === $newProperty->getValue()) { |
|
215 | + // We can safely remove these, as they are converted to |
|
216 | + // ANNIVERSARY properties. |
|
217 | + return; |
|
218 | + } |
|
219 | + break; |
|
220 | + } |
|
221 | + } |
|
222 | + |
|
223 | + // set property group |
|
224 | + $newProperty->group = $property->group; |
|
225 | + |
|
226 | + if (Document::VCARD40 === $targetVersion) { |
|
227 | + $this->convertParameters40($newProperty, $parameters); |
|
228 | + } else { |
|
229 | + $this->convertParameters30($newProperty, $parameters); |
|
230 | + } |
|
231 | + |
|
232 | + // Lastly, we need to see if there's a need for a VALUE parameter. |
|
233 | + // |
|
234 | + // We can do that by instantiating a empty property with that name, and |
|
235 | + // seeing if the default valueType is identical to the current one. |
|
236 | + $tempProperty = $output->createProperty($newProperty->name); |
|
237 | + if ($tempProperty->getValueType() !== $newProperty->getValueType()) { |
|
238 | + $newProperty['VALUE'] = $newProperty->getValueType(); |
|
239 | + } |
|
240 | + |
|
241 | + $output->add($newProperty); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Converts a BINARY property to a URI property. |
|
246 | + * |
|
247 | + * vCard 4.0 no longer supports BINARY properties. |
|
248 | + * |
|
249 | + * @param Property\Uri $property the input property |
|
250 | + * @param $parameters list of parameters that will eventually be added to |
|
251 | + * the new property |
|
252 | + * |
|
253 | + * @return Property\Uri |
|
254 | + */ |
|
255 | + protected function convertBinaryToUri(Component\VCard $output, Property\Binary $newProperty, array &$parameters) |
|
256 | + { |
|
257 | + $value = $newProperty->getValue(); |
|
258 | + $newProperty = $output->createProperty( |
|
259 | + $newProperty->name, |
|
260 | + null, // no value |
|
261 | + [], // no parameters yet |
|
262 | + 'URI' // Forcing the BINARY type |
|
263 | + ); |
|
264 | + |
|
265 | + $mimeType = 'application/octet-stream'; |
|
266 | + |
|
267 | + // See if we can find a better mimetype. |
|
268 | + if (isset($parameters['TYPE'])) { |
|
269 | + $newTypes = []; |
|
270 | + foreach ($parameters['TYPE']->getParts() as $typePart) { |
|
271 | + if (in_array( |
|
272 | + strtoupper($typePart), |
|
273 | + ['JPEG', 'PNG', 'GIF'] |
|
274 | + )) { |
|
275 | + $mimeType = 'image/'.strtolower($typePart); |
|
276 | + } else { |
|
277 | + $newTypes[] = $typePart; |
|
278 | + } |
|
279 | + } |
|
280 | + |
|
281 | + // If there were any parameters we're not converting to a |
|
282 | + // mime-type, we need to keep them. |
|
283 | + if ($newTypes) { |
|
284 | + $parameters['TYPE']->setParts($newTypes); |
|
285 | + } else { |
|
286 | + unset($parameters['TYPE']); |
|
287 | + } |
|
288 | + } |
|
289 | + |
|
290 | + $newProperty->setValue('data:'.$mimeType.';base64,'.base64_encode($value)); |
|
291 | + |
|
292 | + return $newProperty; |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * Converts a URI property to a BINARY property. |
|
297 | + * |
|
298 | + * In vCard 4.0 attachments are encoded as data: uri. Even though these may |
|
299 | + * be valid in vCard 3.0 as well, we should convert those to BINARY if |
|
300 | + * possible, to improve compatibility. |
|
301 | + * |
|
302 | + * @param Property\Uri $property the input property |
|
303 | + * |
|
304 | + * @return Property\Binary|null |
|
305 | + */ |
|
306 | + protected function convertUriToBinary(Component\VCard $output, Property\Uri $newProperty) |
|
307 | + { |
|
308 | + $value = $newProperty->getValue(); |
|
309 | + |
|
310 | + // Only converting data: uris |
|
311 | + if ('data:' !== substr($value, 0, 5)) { |
|
312 | + return $newProperty; |
|
313 | + } |
|
314 | + |
|
315 | + $newProperty = $output->createProperty( |
|
316 | + $newProperty->name, |
|
317 | + null, // no value |
|
318 | + [], // no parameters yet |
|
319 | + 'BINARY' |
|
320 | + ); |
|
321 | + |
|
322 | + $mimeType = substr($value, 5, strpos($value, ',') - 5); |
|
323 | + if (strpos($mimeType, ';')) { |
|
324 | + $mimeType = substr($mimeType, 0, strpos($mimeType, ';')); |
|
325 | + $newProperty->setValue(base64_decode(substr($value, strpos($value, ',') + 1))); |
|
326 | + } else { |
|
327 | + $newProperty->setValue(substr($value, strpos($value, ',') + 1)); |
|
328 | + } |
|
329 | + unset($value); |
|
330 | + |
|
331 | + $newProperty['ENCODING'] = 'b'; |
|
332 | + switch ($mimeType) { |
|
333 | + case 'image/jpeg': |
|
334 | + $newProperty['TYPE'] = 'JPEG'; |
|
335 | + break; |
|
336 | + case 'image/png': |
|
337 | + $newProperty['TYPE'] = 'PNG'; |
|
338 | + break; |
|
339 | + case 'image/gif': |
|
340 | + $newProperty['TYPE'] = 'GIF'; |
|
341 | + break; |
|
342 | + } |
|
343 | + |
|
344 | + return $newProperty; |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * Adds parameters to a new property for vCard 4.0. |
|
349 | + */ |
|
350 | + protected function convertParameters40(Property $newProperty, array $parameters) |
|
351 | + { |
|
352 | + // Adding all parameters. |
|
353 | + foreach ($parameters as $param) { |
|
354 | + // vCard 2.1 allowed parameters with no name |
|
355 | + if ($param->noName) { |
|
356 | + $param->noName = false; |
|
357 | + } |
|
358 | + |
|
359 | + switch ($param->name) { |
|
360 | + // We need to see if there's any TYPE=PREF, because in vCard 4 |
|
361 | + // that's now PREF=1. |
|
362 | + case 'TYPE': |
|
363 | + foreach ($param->getParts() as $paramPart) { |
|
364 | + if ('PREF' === strtoupper($paramPart)) { |
|
365 | + $newProperty->add('PREF', '1'); |
|
366 | + } else { |
|
367 | + $newProperty->add($param->name, $paramPart); |
|
368 | + } |
|
369 | + } |
|
370 | + break; |
|
371 | + // These no longer exist in vCard 4 |
|
372 | + case 'ENCODING': |
|
373 | + case 'CHARSET': |
|
374 | + break; |
|
375 | + |
|
376 | + default: |
|
377 | + $newProperty->add($param->name, $param->getParts()); |
|
378 | + break; |
|
379 | + } |
|
380 | + } |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * Adds parameters to a new property for vCard 3.0. |
|
385 | + */ |
|
386 | + protected function convertParameters30(Property $newProperty, array $parameters) |
|
387 | + { |
|
388 | + // Adding all parameters. |
|
389 | + foreach ($parameters as $param) { |
|
390 | + // vCard 2.1 allowed parameters with no name |
|
391 | + if ($param->noName) { |
|
392 | + $param->noName = false; |
|
393 | + } |
|
394 | + |
|
395 | + switch ($param->name) { |
|
396 | + case 'ENCODING': |
|
397 | + // This value only existed in vCard 2.1, and should be |
|
398 | + // removed for anything else. |
|
399 | + if ('QUOTED-PRINTABLE' !== strtoupper($param->getValue())) { |
|
400 | + $newProperty->add($param->name, $param->getParts()); |
|
401 | + } |
|
402 | + break; |
|
403 | + |
|
404 | + /* |
|
405 | 405 | * Converting PREF=1 to TYPE=PREF. |
406 | 406 | * |
407 | 407 | * Any other PREF numbers we'll drop. |
408 | 408 | */ |
409 | - case 'PREF': |
|
410 | - if ('1' == $param->getValue()) { |
|
411 | - $newProperty->add('TYPE', 'PREF'); |
|
412 | - } |
|
413 | - break; |
|
414 | - |
|
415 | - default: |
|
416 | - $newProperty->add($param->name, $param->getParts()); |
|
417 | - break; |
|
418 | - } |
|
419 | - } |
|
420 | - } |
|
409 | + case 'PREF': |
|
410 | + if ('1' == $param->getValue()) { |
|
411 | + $newProperty->add('TYPE', 'PREF'); |
|
412 | + } |
|
413 | + break; |
|
414 | + |
|
415 | + default: |
|
416 | + $newProperty->add($param->name, $param->getParts()); |
|
417 | + break; |
|
418 | + } |
|
419 | + } |
|
420 | + } |
|
421 | 421 | } |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | } |
32 | 32 | |
33 | 33 | $base = parse($basePath); |
34 | - $pick = function ($part) use ($base, $delta) { |
|
34 | + $pick = function($part) use ($base, $delta) { |
|
35 | 35 | if ($delta[$part]) { |
36 | 36 | return $delta[$part]; |
37 | 37 | } elseif ($base[$part]) { |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | // uriencode them first. |
188 | 188 | $uri = preg_replace_callback( |
189 | 189 | '/[^[:ascii:]]/u', |
190 | - function ($matches) { |
|
190 | + function($matches) { |
|
191 | 191 | return rawurlencode($matches[0]); |
192 | 192 | }, |
193 | 193 | $uri |
@@ -316,7 +316,7 @@ discard block |
||
316 | 316 | // uriencode them first. |
317 | 317 | $uri = preg_replace_callback( |
318 | 318 | '/[^[:ascii:]]/u', |
319 | - function ($matches) { |
|
319 | + function($matches) { |
|
320 | 320 | return rawurlencode($matches[0]); |
321 | 321 | }, |
322 | 322 | $uri |
@@ -22,80 +22,80 @@ discard block |
||
22 | 22 | */ |
23 | 23 | function resolve(string $basePath, string $newPath): string |
24 | 24 | { |
25 | - $delta = parse($newPath); |
|
26 | - |
|
27 | - // If the new path defines a scheme, it's absolute and we can just return |
|
28 | - // that. |
|
29 | - if ($delta['scheme']) { |
|
30 | - return build($delta); |
|
31 | - } |
|
32 | - |
|
33 | - $base = parse($basePath); |
|
34 | - $pick = function ($part) use ($base, $delta) { |
|
35 | - if ($delta[$part]) { |
|
36 | - return $delta[$part]; |
|
37 | - } elseif ($base[$part]) { |
|
38 | - return $base[$part]; |
|
39 | - } |
|
40 | - |
|
41 | - return null; |
|
42 | - }; |
|
43 | - |
|
44 | - $newParts = []; |
|
45 | - |
|
46 | - $newParts['scheme'] = $pick('scheme'); |
|
47 | - $newParts['host'] = $pick('host'); |
|
48 | - $newParts['port'] = $pick('port'); |
|
49 | - |
|
50 | - $path = ''; |
|
51 | - if (is_string($delta['path']) and strlen($delta['path']) > 0) { |
|
52 | - // If the path starts with a slash |
|
53 | - if ('/' === $delta['path'][0]) { |
|
54 | - $path = $delta['path']; |
|
55 | - } else { |
|
56 | - // Removing last component from base path. |
|
57 | - $path = $base['path']; |
|
58 | - $length = strrpos((string) $path, '/'); |
|
59 | - if (false !== $length) { |
|
60 | - $path = substr($path, 0, $length); |
|
61 | - } |
|
62 | - $path .= '/'.$delta['path']; |
|
63 | - } |
|
64 | - } else { |
|
65 | - $path = $base['path'] ?: '/'; |
|
66 | - } |
|
67 | - // Removing .. and . |
|
68 | - $pathParts = explode('/', $path); |
|
69 | - $newPathParts = []; |
|
70 | - foreach ($pathParts as $pathPart) { |
|
71 | - switch ($pathPart) { |
|
72 | - //case '' : |
|
73 | - case '.': |
|
74 | - break; |
|
75 | - case '..': |
|
76 | - array_pop($newPathParts); |
|
77 | - break; |
|
78 | - default: |
|
79 | - $newPathParts[] = $pathPart; |
|
80 | - break; |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - $path = implode('/', $newPathParts); |
|
85 | - |
|
86 | - // If the source url ended with a /, we want to preserve that. |
|
87 | - $newParts['path'] = 0 === strpos($path, '/') ? $path : '/'.$path; |
|
88 | - if ($delta['query']) { |
|
89 | - $newParts['query'] = $delta['query']; |
|
90 | - } elseif (!empty($base['query']) && empty($delta['host']) && empty($delta['path'])) { |
|
91 | - // Keep the old query if host and path didn't change |
|
92 | - $newParts['query'] = $base['query']; |
|
93 | - } |
|
94 | - if ($delta['fragment']) { |
|
95 | - $newParts['fragment'] = $delta['fragment']; |
|
96 | - } |
|
97 | - |
|
98 | - return build($newParts); |
|
25 | + $delta = parse($newPath); |
|
26 | + |
|
27 | + // If the new path defines a scheme, it's absolute and we can just return |
|
28 | + // that. |
|
29 | + if ($delta['scheme']) { |
|
30 | + return build($delta); |
|
31 | + } |
|
32 | + |
|
33 | + $base = parse($basePath); |
|
34 | + $pick = function ($part) use ($base, $delta) { |
|
35 | + if ($delta[$part]) { |
|
36 | + return $delta[$part]; |
|
37 | + } elseif ($base[$part]) { |
|
38 | + return $base[$part]; |
|
39 | + } |
|
40 | + |
|
41 | + return null; |
|
42 | + }; |
|
43 | + |
|
44 | + $newParts = []; |
|
45 | + |
|
46 | + $newParts['scheme'] = $pick('scheme'); |
|
47 | + $newParts['host'] = $pick('host'); |
|
48 | + $newParts['port'] = $pick('port'); |
|
49 | + |
|
50 | + $path = ''; |
|
51 | + if (is_string($delta['path']) and strlen($delta['path']) > 0) { |
|
52 | + // If the path starts with a slash |
|
53 | + if ('/' === $delta['path'][0]) { |
|
54 | + $path = $delta['path']; |
|
55 | + } else { |
|
56 | + // Removing last component from base path. |
|
57 | + $path = $base['path']; |
|
58 | + $length = strrpos((string) $path, '/'); |
|
59 | + if (false !== $length) { |
|
60 | + $path = substr($path, 0, $length); |
|
61 | + } |
|
62 | + $path .= '/'.$delta['path']; |
|
63 | + } |
|
64 | + } else { |
|
65 | + $path = $base['path'] ?: '/'; |
|
66 | + } |
|
67 | + // Removing .. and . |
|
68 | + $pathParts = explode('/', $path); |
|
69 | + $newPathParts = []; |
|
70 | + foreach ($pathParts as $pathPart) { |
|
71 | + switch ($pathPart) { |
|
72 | + //case '' : |
|
73 | + case '.': |
|
74 | + break; |
|
75 | + case '..': |
|
76 | + array_pop($newPathParts); |
|
77 | + break; |
|
78 | + default: |
|
79 | + $newPathParts[] = $pathPart; |
|
80 | + break; |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + $path = implode('/', $newPathParts); |
|
85 | + |
|
86 | + // If the source url ended with a /, we want to preserve that. |
|
87 | + $newParts['path'] = 0 === strpos($path, '/') ? $path : '/'.$path; |
|
88 | + if ($delta['query']) { |
|
89 | + $newParts['query'] = $delta['query']; |
|
90 | + } elseif (!empty($base['query']) && empty($delta['host']) && empty($delta['path'])) { |
|
91 | + // Keep the old query if host and path didn't change |
|
92 | + $newParts['query'] = $base['query']; |
|
93 | + } |
|
94 | + if ($delta['fragment']) { |
|
95 | + $newParts['fragment'] = $delta['fragment']; |
|
96 | + } |
|
97 | + |
|
98 | + return build($newParts); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | /** |
@@ -111,57 +111,57 @@ discard block |
||
111 | 111 | */ |
112 | 112 | function normalize(string $uri): string |
113 | 113 | { |
114 | - $parts = parse($uri); |
|
115 | - |
|
116 | - if (!empty($parts['path'])) { |
|
117 | - $pathParts = explode('/', ltrim($parts['path'], '/')); |
|
118 | - $newPathParts = []; |
|
119 | - foreach ($pathParts as $pathPart) { |
|
120 | - switch ($pathPart) { |
|
121 | - case '.': |
|
122 | - // skip |
|
123 | - break; |
|
124 | - case '..': |
|
125 | - // One level up in the hierarchy |
|
126 | - array_pop($newPathParts); |
|
127 | - break; |
|
128 | - default: |
|
129 | - // Ensuring that everything is correctly percent-encoded. |
|
130 | - $newPathParts[] = rawurlencode(rawurldecode($pathPart)); |
|
131 | - break; |
|
132 | - } |
|
133 | - } |
|
134 | - $parts['path'] = '/'.implode('/', $newPathParts); |
|
135 | - } |
|
136 | - |
|
137 | - if ($parts['scheme']) { |
|
138 | - $parts['scheme'] = strtolower($parts['scheme']); |
|
139 | - $defaultPorts = [ |
|
140 | - 'http' => '80', |
|
141 | - 'https' => '443', |
|
142 | - ]; |
|
143 | - |
|
144 | - if (!empty($parts['port']) && isset($defaultPorts[$parts['scheme']]) && $defaultPorts[$parts['scheme']] == $parts['port']) { |
|
145 | - // Removing default ports. |
|
146 | - unset($parts['port']); |
|
147 | - } |
|
148 | - // A few HTTP specific rules. |
|
149 | - switch ($parts['scheme']) { |
|
150 | - case 'http': |
|
151 | - case 'https': |
|
152 | - if (empty($parts['path'])) { |
|
153 | - // An empty path is equivalent to / in http. |
|
154 | - $parts['path'] = '/'; |
|
155 | - } |
|
156 | - break; |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - if ($parts['host']) { |
|
161 | - $parts['host'] = strtolower($parts['host']); |
|
162 | - } |
|
163 | - |
|
164 | - return build($parts); |
|
114 | + $parts = parse($uri); |
|
115 | + |
|
116 | + if (!empty($parts['path'])) { |
|
117 | + $pathParts = explode('/', ltrim($parts['path'], '/')); |
|
118 | + $newPathParts = []; |
|
119 | + foreach ($pathParts as $pathPart) { |
|
120 | + switch ($pathPart) { |
|
121 | + case '.': |
|
122 | + // skip |
|
123 | + break; |
|
124 | + case '..': |
|
125 | + // One level up in the hierarchy |
|
126 | + array_pop($newPathParts); |
|
127 | + break; |
|
128 | + default: |
|
129 | + // Ensuring that everything is correctly percent-encoded. |
|
130 | + $newPathParts[] = rawurlencode(rawurldecode($pathPart)); |
|
131 | + break; |
|
132 | + } |
|
133 | + } |
|
134 | + $parts['path'] = '/'.implode('/', $newPathParts); |
|
135 | + } |
|
136 | + |
|
137 | + if ($parts['scheme']) { |
|
138 | + $parts['scheme'] = strtolower($parts['scheme']); |
|
139 | + $defaultPorts = [ |
|
140 | + 'http' => '80', |
|
141 | + 'https' => '443', |
|
142 | + ]; |
|
143 | + |
|
144 | + if (!empty($parts['port']) && isset($defaultPorts[$parts['scheme']]) && $defaultPorts[$parts['scheme']] == $parts['port']) { |
|
145 | + // Removing default ports. |
|
146 | + unset($parts['port']); |
|
147 | + } |
|
148 | + // A few HTTP specific rules. |
|
149 | + switch ($parts['scheme']) { |
|
150 | + case 'http': |
|
151 | + case 'https': |
|
152 | + if (empty($parts['path'])) { |
|
153 | + // An empty path is equivalent to / in http. |
|
154 | + $parts['path'] = '/'; |
|
155 | + } |
|
156 | + break; |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + if ($parts['host']) { |
|
161 | + $parts['host'] = strtolower($parts['host']); |
|
162 | + } |
|
163 | + |
|
164 | + return build($parts); |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | /** |
@@ -180,34 +180,34 @@ discard block |
||
180 | 180 | */ |
181 | 181 | function parse(string $uri): array |
182 | 182 | { |
183 | - // Normally a URI must be ASCII, however. However, often it's not and |
|
184 | - // parse_url might corrupt these strings. |
|
185 | - // |
|
186 | - // For that reason we take any non-ascii characters from the uri and |
|
187 | - // uriencode them first. |
|
188 | - $uri = preg_replace_callback( |
|
189 | - '/[^[:ascii:]]/u', |
|
190 | - function ($matches) { |
|
191 | - return rawurlencode($matches[0]); |
|
192 | - }, |
|
193 | - $uri |
|
194 | - ); |
|
195 | - |
|
196 | - $result = parse_url($uri); |
|
197 | - if (!$result) { |
|
198 | - $result = _parse_fallback($uri); |
|
199 | - } |
|
200 | - |
|
201 | - return |
|
202 | - $result + [ |
|
203 | - 'scheme' => null, |
|
204 | - 'host' => null, |
|
205 | - 'path' => null, |
|
206 | - 'port' => null, |
|
207 | - 'user' => null, |
|
208 | - 'query' => null, |
|
209 | - 'fragment' => null, |
|
210 | - ]; |
|
183 | + // Normally a URI must be ASCII, however. However, often it's not and |
|
184 | + // parse_url might corrupt these strings. |
|
185 | + // |
|
186 | + // For that reason we take any non-ascii characters from the uri and |
|
187 | + // uriencode them first. |
|
188 | + $uri = preg_replace_callback( |
|
189 | + '/[^[:ascii:]]/u', |
|
190 | + function ($matches) { |
|
191 | + return rawurlencode($matches[0]); |
|
192 | + }, |
|
193 | + $uri |
|
194 | + ); |
|
195 | + |
|
196 | + $result = parse_url($uri); |
|
197 | + if (!$result) { |
|
198 | + $result = _parse_fallback($uri); |
|
199 | + } |
|
200 | + |
|
201 | + return |
|
202 | + $result + [ |
|
203 | + 'scheme' => null, |
|
204 | + 'host' => null, |
|
205 | + 'path' => null, |
|
206 | + 'port' => null, |
|
207 | + 'user' => null, |
|
208 | + 'query' => null, |
|
209 | + 'fragment' => null, |
|
210 | + ]; |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | /** |
@@ -218,39 +218,39 @@ discard block |
||
218 | 218 | */ |
219 | 219 | function build(array $parts): string |
220 | 220 | { |
221 | - $uri = ''; |
|
222 | - |
|
223 | - $authority = ''; |
|
224 | - if (!empty($parts['host'])) { |
|
225 | - $authority = $parts['host']; |
|
226 | - if (!empty($parts['user'])) { |
|
227 | - $authority = $parts['user'].'@'.$authority; |
|
228 | - } |
|
229 | - if (!empty($parts['port'])) { |
|
230 | - $authority = $authority.':'.$parts['port']; |
|
231 | - } |
|
232 | - } |
|
233 | - |
|
234 | - if (!empty($parts['scheme'])) { |
|
235 | - // If there's a scheme, there's also a host. |
|
236 | - $uri = $parts['scheme'].':'; |
|
237 | - } |
|
238 | - if ($authority || (!empty($parts['scheme']) && 'file' === $parts['scheme'])) { |
|
239 | - // No scheme, but there is a host. |
|
240 | - $uri .= '//'.$authority; |
|
241 | - } |
|
242 | - |
|
243 | - if (!empty($parts['path'])) { |
|
244 | - $uri .= $parts['path']; |
|
245 | - } |
|
246 | - if (!empty($parts['query'])) { |
|
247 | - $uri .= '?'.$parts['query']; |
|
248 | - } |
|
249 | - if (!empty($parts['fragment'])) { |
|
250 | - $uri .= '#'.$parts['fragment']; |
|
251 | - } |
|
252 | - |
|
253 | - return $uri; |
|
221 | + $uri = ''; |
|
222 | + |
|
223 | + $authority = ''; |
|
224 | + if (!empty($parts['host'])) { |
|
225 | + $authority = $parts['host']; |
|
226 | + if (!empty($parts['user'])) { |
|
227 | + $authority = $parts['user'].'@'.$authority; |
|
228 | + } |
|
229 | + if (!empty($parts['port'])) { |
|
230 | + $authority = $authority.':'.$parts['port']; |
|
231 | + } |
|
232 | + } |
|
233 | + |
|
234 | + if (!empty($parts['scheme'])) { |
|
235 | + // If there's a scheme, there's also a host. |
|
236 | + $uri = $parts['scheme'].':'; |
|
237 | + } |
|
238 | + if ($authority || (!empty($parts['scheme']) && 'file' === $parts['scheme'])) { |
|
239 | + // No scheme, but there is a host. |
|
240 | + $uri .= '//'.$authority; |
|
241 | + } |
|
242 | + |
|
243 | + if (!empty($parts['path'])) { |
|
244 | + $uri .= $parts['path']; |
|
245 | + } |
|
246 | + if (!empty($parts['query'])) { |
|
247 | + $uri .= '?'.$parts['query']; |
|
248 | + } |
|
249 | + if (!empty($parts['fragment'])) { |
|
250 | + $uri .= '#'.$parts['fragment']; |
|
251 | + } |
|
252 | + |
|
253 | + return $uri; |
|
254 | 254 | } |
255 | 255 | |
256 | 256 | /** |
@@ -272,12 +272,12 @@ discard block |
||
272 | 272 | */ |
273 | 273 | function split(string $path): array |
274 | 274 | { |
275 | - $matches = []; |
|
276 | - if (preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u', $path, $matches)) { |
|
277 | - return [$matches[1], $matches[2]]; |
|
278 | - } |
|
275 | + $matches = []; |
|
276 | + if (preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u', $path, $matches)) { |
|
277 | + return [$matches[1], $matches[2]]; |
|
278 | + } |
|
279 | 279 | |
280 | - return [null, null]; |
|
280 | + return [null, null]; |
|
281 | 281 | } |
282 | 282 | |
283 | 283 | /** |
@@ -296,79 +296,79 @@ discard block |
||
296 | 296 | */ |
297 | 297 | function _parse_fallback(string $uri): array |
298 | 298 | { |
299 | - // Normally a URI must be ASCII, however. However, often it's not and |
|
300 | - // parse_url might corrupt these strings. |
|
301 | - // |
|
302 | - // For that reason we take any non-ascii characters from the uri and |
|
303 | - // uriencode them first. |
|
304 | - $uri = preg_replace_callback( |
|
305 | - '/[^[:ascii:]]/u', |
|
306 | - function ($matches) { |
|
307 | - return rawurlencode($matches[0]); |
|
308 | - }, |
|
309 | - $uri |
|
310 | - ); |
|
311 | - |
|
312 | - $result = [ |
|
313 | - 'scheme' => null, |
|
314 | - 'host' => null, |
|
315 | - 'port' => null, |
|
316 | - 'user' => null, |
|
317 | - 'path' => null, |
|
318 | - 'fragment' => null, |
|
319 | - 'query' => null, |
|
320 | - ]; |
|
321 | - |
|
322 | - if (preg_match('% ^([A-Za-z][A-Za-z0-9+-\.]+): %x', $uri, $matches)) { |
|
323 | - $result['scheme'] = $matches[1]; |
|
324 | - // Take what's left. |
|
325 | - $uri = substr($uri, strlen($result['scheme']) + 1); |
|
326 | - } |
|
327 | - |
|
328 | - // Taking off a fragment part |
|
329 | - if (false !== strpos($uri, '#')) { |
|
330 | - list($uri, $result['fragment']) = explode('#', $uri, 2); |
|
331 | - } |
|
332 | - // Taking off the query part |
|
333 | - if (false !== strpos($uri, '?')) { |
|
334 | - list($uri, $result['query']) = explode('?', $uri, 2); |
|
335 | - } |
|
336 | - |
|
337 | - if ('///' === substr($uri, 0, 3)) { |
|
338 | - // The triple slash uris are a bit unusual, but we have special handling |
|
339 | - // for them. |
|
340 | - $result['path'] = substr($uri, 2); |
|
341 | - $result['host'] = ''; |
|
342 | - } elseif ('//' === substr($uri, 0, 2)) { |
|
343 | - // Uris that have an authority part. |
|
344 | - $regex = '%^ |
|
299 | + // Normally a URI must be ASCII, however. However, often it's not and |
|
300 | + // parse_url might corrupt these strings. |
|
301 | + // |
|
302 | + // For that reason we take any non-ascii characters from the uri and |
|
303 | + // uriencode them first. |
|
304 | + $uri = preg_replace_callback( |
|
305 | + '/[^[:ascii:]]/u', |
|
306 | + function ($matches) { |
|
307 | + return rawurlencode($matches[0]); |
|
308 | + }, |
|
309 | + $uri |
|
310 | + ); |
|
311 | + |
|
312 | + $result = [ |
|
313 | + 'scheme' => null, |
|
314 | + 'host' => null, |
|
315 | + 'port' => null, |
|
316 | + 'user' => null, |
|
317 | + 'path' => null, |
|
318 | + 'fragment' => null, |
|
319 | + 'query' => null, |
|
320 | + ]; |
|
321 | + |
|
322 | + if (preg_match('% ^([A-Za-z][A-Za-z0-9+-\.]+): %x', $uri, $matches)) { |
|
323 | + $result['scheme'] = $matches[1]; |
|
324 | + // Take what's left. |
|
325 | + $uri = substr($uri, strlen($result['scheme']) + 1); |
|
326 | + } |
|
327 | + |
|
328 | + // Taking off a fragment part |
|
329 | + if (false !== strpos($uri, '#')) { |
|
330 | + list($uri, $result['fragment']) = explode('#', $uri, 2); |
|
331 | + } |
|
332 | + // Taking off the query part |
|
333 | + if (false !== strpos($uri, '?')) { |
|
334 | + list($uri, $result['query']) = explode('?', $uri, 2); |
|
335 | + } |
|
336 | + |
|
337 | + if ('///' === substr($uri, 0, 3)) { |
|
338 | + // The triple slash uris are a bit unusual, but we have special handling |
|
339 | + // for them. |
|
340 | + $result['path'] = substr($uri, 2); |
|
341 | + $result['host'] = ''; |
|
342 | + } elseif ('//' === substr($uri, 0, 2)) { |
|
343 | + // Uris that have an authority part. |
|
344 | + $regex = '%^ |
|
345 | 345 | // |
346 | 346 | (?: (?<user> [^:@]+) (: (?<pass> [^@]+)) @)? |
347 | 347 | (?<host> ( [^:/]* | \[ [^\]]+ \] )) |
348 | 348 | (?: : (?<port> [0-9]+))? |
349 | 349 | (?<path> / .*)? |
350 | 350 | $%x'; |
351 | - if (!preg_match($regex, $uri, $matches)) { |
|
352 | - throw new InvalidUriException('Invalid, or could not parse URI'); |
|
353 | - } |
|
354 | - if ($matches['host']) { |
|
355 | - $result['host'] = $matches['host']; |
|
356 | - } |
|
357 | - if (isset($matches['port'])) { |
|
358 | - $result['port'] = (int) $matches['port']; |
|
359 | - } |
|
360 | - if (isset($matches['path'])) { |
|
361 | - $result['path'] = $matches['path']; |
|
362 | - } |
|
363 | - if ($matches['user']) { |
|
364 | - $result['user'] = $matches['user']; |
|
365 | - } |
|
366 | - if ($matches['pass']) { |
|
367 | - $result['pass'] = $matches['pass']; |
|
368 | - } |
|
369 | - } else { |
|
370 | - $result['path'] = $uri; |
|
371 | - } |
|
372 | - |
|
373 | - return $result; |
|
351 | + if (!preg_match($regex, $uri, $matches)) { |
|
352 | + throw new InvalidUriException('Invalid, or could not parse URI'); |
|
353 | + } |
|
354 | + if ($matches['host']) { |
|
355 | + $result['host'] = $matches['host']; |
|
356 | + } |
|
357 | + if (isset($matches['port'])) { |
|
358 | + $result['port'] = (int) $matches['port']; |
|
359 | + } |
|
360 | + if (isset($matches['path'])) { |
|
361 | + $result['path'] = $matches['path']; |
|
362 | + } |
|
363 | + if ($matches['user']) { |
|
364 | + $result['user'] = $matches['user']; |
|
365 | + } |
|
366 | + if ($matches['pass']) { |
|
367 | + $result['pass'] = $matches['pass']; |
|
368 | + } |
|
369 | + } else { |
|
370 | + $result['path'] = $uri; |
|
371 | + } |
|
372 | + |
|
373 | + return $result; |
|
374 | 374 | } |
@@ -16,35 +16,35 @@ |
||
16 | 16 | */ |
17 | 17 | class ClientHttpException extends \Exception implements HttpException |
18 | 18 | { |
19 | - /** |
|
20 | - * Response object. |
|
21 | - * |
|
22 | - * @var ResponseInterface |
|
23 | - */ |
|
24 | - protected $response; |
|
19 | + /** |
|
20 | + * Response object. |
|
21 | + * |
|
22 | + * @var ResponseInterface |
|
23 | + */ |
|
24 | + protected $response; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Constructor. |
|
28 | - */ |
|
29 | - public function __construct(ResponseInterface $response) |
|
30 | - { |
|
31 | - $this->response = $response; |
|
32 | - parent::__construct($response->getStatusText(), $response->getStatus()); |
|
33 | - } |
|
26 | + /** |
|
27 | + * Constructor. |
|
28 | + */ |
|
29 | + public function __construct(ResponseInterface $response) |
|
30 | + { |
|
31 | + $this->response = $response; |
|
32 | + parent::__construct($response->getStatusText(), $response->getStatus()); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * The http status code for the error. |
|
37 | - */ |
|
38 | - public function getHttpStatus(): int |
|
39 | - { |
|
40 | - return $this->response->getStatus(); |
|
41 | - } |
|
35 | + /** |
|
36 | + * The http status code for the error. |
|
37 | + */ |
|
38 | + public function getHttpStatus(): int |
|
39 | + { |
|
40 | + return $this->response->getStatus(); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Returns the full response object. |
|
45 | - */ |
|
46 | - public function getResponse(): ResponseInterface |
|
47 | - { |
|
48 | - return $this->response; |
|
49 | - } |
|
43 | + /** |
|
44 | + * Returns the full response object. |
|
45 | + */ |
|
46 | + public function getResponse(): ResponseInterface |
|
47 | + { |
|
48 | + return $this->response; |
|
49 | + } |
|
50 | 50 | } |
@@ -17,204 +17,204 @@ |
||
17 | 17 | */ |
18 | 18 | class AWS extends AbstractAuth |
19 | 19 | { |
20 | - /** |
|
21 | - * The signature supplied by the HTTP client. |
|
22 | - * |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - private $signature = null; |
|
26 | - |
|
27 | - /** |
|
28 | - * The accesskey supplied by the HTTP client. |
|
29 | - * |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - private $accessKey = null; |
|
33 | - |
|
34 | - /** |
|
35 | - * An error code, if any. |
|
36 | - * |
|
37 | - * This value will be filled with one of the ERR_* constants |
|
38 | - * |
|
39 | - * @var int |
|
40 | - */ |
|
41 | - public $errorCode = 0; |
|
42 | - |
|
43 | - const ERR_NOAWSHEADER = 1; |
|
44 | - const ERR_MD5CHECKSUMWRONG = 2; |
|
45 | - const ERR_INVALIDDATEFORMAT = 3; |
|
46 | - const ERR_REQUESTTIMESKEWED = 4; |
|
47 | - const ERR_INVALIDSIGNATURE = 5; |
|
48 | - |
|
49 | - /** |
|
50 | - * Gathers all information from the headers. |
|
51 | - * |
|
52 | - * This method needs to be called prior to anything else. |
|
53 | - */ |
|
54 | - public function init(): bool |
|
55 | - { |
|
56 | - $authHeader = $this->request->getHeader('Authorization'); |
|
57 | - |
|
58 | - if (null === $authHeader) { |
|
59 | - $this->errorCode = self::ERR_NOAWSHEADER; |
|
60 | - |
|
61 | - return false; |
|
62 | - } |
|
63 | - $authHeader = explode(' ', $authHeader); |
|
64 | - |
|
65 | - if ('AWS' !== $authHeader[0] || !isset($authHeader[1])) { |
|
66 | - $this->errorCode = self::ERR_NOAWSHEADER; |
|
67 | - |
|
68 | - return false; |
|
69 | - } |
|
70 | - |
|
71 | - list($this->accessKey, $this->signature) = explode(':', $authHeader[1]); |
|
72 | - |
|
73 | - return true; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Returns the username for the request. |
|
78 | - */ |
|
79 | - public function getAccessKey(): string |
|
80 | - { |
|
81 | - return $this->accessKey; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Validates the signature based on the secretKey. |
|
86 | - */ |
|
87 | - public function validate(string $secretKey): bool |
|
88 | - { |
|
89 | - $contentMD5 = $this->request->getHeader('Content-MD5'); |
|
90 | - |
|
91 | - if ($contentMD5) { |
|
92 | - // We need to validate the integrity of the request |
|
93 | - $body = $this->request->getBody(); |
|
94 | - $this->request->setBody($body); |
|
95 | - |
|
96 | - if ($contentMD5 !== base64_encode(md5((string) $body, true))) { |
|
97 | - // content-md5 header did not match md5 signature of body |
|
98 | - $this->errorCode = self::ERR_MD5CHECKSUMWRONG; |
|
99 | - |
|
100 | - return false; |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - if (!$requestDate = $this->request->getHeader('x-amz-date')) { |
|
105 | - $requestDate = $this->request->getHeader('Date'); |
|
106 | - } |
|
107 | - |
|
108 | - if (!$this->validateRFC2616Date((string) $requestDate)) { |
|
109 | - return false; |
|
110 | - } |
|
111 | - |
|
112 | - $amzHeaders = $this->getAmzHeaders(); |
|
113 | - |
|
114 | - $signature = base64_encode( |
|
115 | - $this->hmacsha1($secretKey, |
|
116 | - $this->request->getMethod()."\n". |
|
117 | - $contentMD5."\n". |
|
118 | - $this->request->getHeader('Content-type')."\n". |
|
119 | - $requestDate."\n". |
|
120 | - $amzHeaders. |
|
121 | - $this->request->getUrl() |
|
122 | - ) |
|
123 | - ); |
|
124 | - |
|
125 | - if ($this->signature !== $signature) { |
|
126 | - $this->errorCode = self::ERR_INVALIDSIGNATURE; |
|
127 | - |
|
128 | - return false; |
|
129 | - } |
|
130 | - |
|
131 | - return true; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Returns an HTTP 401 header, forcing login. |
|
136 | - * |
|
137 | - * This should be called when username and password are incorrect, or not supplied at all |
|
138 | - */ |
|
139 | - public function requireLogin() |
|
140 | - { |
|
141 | - $this->response->addHeader('WWW-Authenticate', 'AWS'); |
|
142 | - $this->response->setStatus(401); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Makes sure the supplied value is a valid RFC2616 date. |
|
147 | - * |
|
148 | - * If we would just use strtotime to get a valid timestamp, we have no way of checking if a |
|
149 | - * user just supplied the word 'now' for the date header. |
|
150 | - * |
|
151 | - * This function also makes sure the Date header is within 15 minutes of the operating |
|
152 | - * system date, to prevent replay attacks. |
|
153 | - */ |
|
154 | - protected function validateRFC2616Date(string $dateHeader): bool |
|
155 | - { |
|
156 | - $date = HTTP\parseDate($dateHeader); |
|
157 | - |
|
158 | - // Unknown format |
|
159 | - if (!$date) { |
|
160 | - $this->errorCode = self::ERR_INVALIDDATEFORMAT; |
|
161 | - |
|
162 | - return false; |
|
163 | - } |
|
164 | - |
|
165 | - $min = new \DateTime('-15 minutes'); |
|
166 | - $max = new \DateTime('+15 minutes'); |
|
167 | - |
|
168 | - // We allow 15 minutes around the current date/time |
|
169 | - if ($date > $max || $date < $min) { |
|
170 | - $this->errorCode = self::ERR_REQUESTTIMESKEWED; |
|
171 | - |
|
172 | - return false; |
|
173 | - } |
|
174 | - |
|
175 | - return true; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Returns a list of AMZ headers. |
|
180 | - */ |
|
181 | - protected function getAmzHeaders(): string |
|
182 | - { |
|
183 | - $amzHeaders = []; |
|
184 | - $headers = $this->request->getHeaders(); |
|
185 | - foreach ($headers as $headerName => $headerValue) { |
|
186 | - if (0 === strpos(strtolower($headerName), 'x-amz-')) { |
|
187 | - $amzHeaders[strtolower($headerName)] = str_replace(["\r\n"], [' '], $headerValue[0])."\n"; |
|
188 | - } |
|
189 | - } |
|
190 | - ksort($amzHeaders); |
|
191 | - |
|
192 | - $headerStr = ''; |
|
193 | - foreach ($amzHeaders as $h => $v) { |
|
194 | - $headerStr .= $h.':'.$v; |
|
195 | - } |
|
196 | - |
|
197 | - return $headerStr; |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Generates an HMAC-SHA1 signature. |
|
202 | - */ |
|
203 | - private function hmacsha1(string $key, string $message): string |
|
204 | - { |
|
205 | - if (function_exists('hash_hmac')) { |
|
206 | - return hash_hmac('sha1', $message, $key, true); |
|
207 | - } |
|
208 | - |
|
209 | - $blocksize = 64; |
|
210 | - if (strlen($key) > $blocksize) { |
|
211 | - $key = pack('H*', sha1($key)); |
|
212 | - } |
|
213 | - $key = str_pad($key, $blocksize, chr(0x00)); |
|
214 | - $ipad = str_repeat(chr(0x36), $blocksize); |
|
215 | - $opad = str_repeat(chr(0x5c), $blocksize); |
|
216 | - $hmac = pack('H*', sha1(($key ^ $opad).pack('H*', sha1(($key ^ $ipad).$message)))); |
|
217 | - |
|
218 | - return $hmac; |
|
219 | - } |
|
20 | + /** |
|
21 | + * The signature supplied by the HTTP client. |
|
22 | + * |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + private $signature = null; |
|
26 | + |
|
27 | + /** |
|
28 | + * The accesskey supplied by the HTTP client. |
|
29 | + * |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + private $accessKey = null; |
|
33 | + |
|
34 | + /** |
|
35 | + * An error code, if any. |
|
36 | + * |
|
37 | + * This value will be filled with one of the ERR_* constants |
|
38 | + * |
|
39 | + * @var int |
|
40 | + */ |
|
41 | + public $errorCode = 0; |
|
42 | + |
|
43 | + const ERR_NOAWSHEADER = 1; |
|
44 | + const ERR_MD5CHECKSUMWRONG = 2; |
|
45 | + const ERR_INVALIDDATEFORMAT = 3; |
|
46 | + const ERR_REQUESTTIMESKEWED = 4; |
|
47 | + const ERR_INVALIDSIGNATURE = 5; |
|
48 | + |
|
49 | + /** |
|
50 | + * Gathers all information from the headers. |
|
51 | + * |
|
52 | + * This method needs to be called prior to anything else. |
|
53 | + */ |
|
54 | + public function init(): bool |
|
55 | + { |
|
56 | + $authHeader = $this->request->getHeader('Authorization'); |
|
57 | + |
|
58 | + if (null === $authHeader) { |
|
59 | + $this->errorCode = self::ERR_NOAWSHEADER; |
|
60 | + |
|
61 | + return false; |
|
62 | + } |
|
63 | + $authHeader = explode(' ', $authHeader); |
|
64 | + |
|
65 | + if ('AWS' !== $authHeader[0] || !isset($authHeader[1])) { |
|
66 | + $this->errorCode = self::ERR_NOAWSHEADER; |
|
67 | + |
|
68 | + return false; |
|
69 | + } |
|
70 | + |
|
71 | + list($this->accessKey, $this->signature) = explode(':', $authHeader[1]); |
|
72 | + |
|
73 | + return true; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Returns the username for the request. |
|
78 | + */ |
|
79 | + public function getAccessKey(): string |
|
80 | + { |
|
81 | + return $this->accessKey; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Validates the signature based on the secretKey. |
|
86 | + */ |
|
87 | + public function validate(string $secretKey): bool |
|
88 | + { |
|
89 | + $contentMD5 = $this->request->getHeader('Content-MD5'); |
|
90 | + |
|
91 | + if ($contentMD5) { |
|
92 | + // We need to validate the integrity of the request |
|
93 | + $body = $this->request->getBody(); |
|
94 | + $this->request->setBody($body); |
|
95 | + |
|
96 | + if ($contentMD5 !== base64_encode(md5((string) $body, true))) { |
|
97 | + // content-md5 header did not match md5 signature of body |
|
98 | + $this->errorCode = self::ERR_MD5CHECKSUMWRONG; |
|
99 | + |
|
100 | + return false; |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + if (!$requestDate = $this->request->getHeader('x-amz-date')) { |
|
105 | + $requestDate = $this->request->getHeader('Date'); |
|
106 | + } |
|
107 | + |
|
108 | + if (!$this->validateRFC2616Date((string) $requestDate)) { |
|
109 | + return false; |
|
110 | + } |
|
111 | + |
|
112 | + $amzHeaders = $this->getAmzHeaders(); |
|
113 | + |
|
114 | + $signature = base64_encode( |
|
115 | + $this->hmacsha1($secretKey, |
|
116 | + $this->request->getMethod()."\n". |
|
117 | + $contentMD5."\n". |
|
118 | + $this->request->getHeader('Content-type')."\n". |
|
119 | + $requestDate."\n". |
|
120 | + $amzHeaders. |
|
121 | + $this->request->getUrl() |
|
122 | + ) |
|
123 | + ); |
|
124 | + |
|
125 | + if ($this->signature !== $signature) { |
|
126 | + $this->errorCode = self::ERR_INVALIDSIGNATURE; |
|
127 | + |
|
128 | + return false; |
|
129 | + } |
|
130 | + |
|
131 | + return true; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Returns an HTTP 401 header, forcing login. |
|
136 | + * |
|
137 | + * This should be called when username and password are incorrect, or not supplied at all |
|
138 | + */ |
|
139 | + public function requireLogin() |
|
140 | + { |
|
141 | + $this->response->addHeader('WWW-Authenticate', 'AWS'); |
|
142 | + $this->response->setStatus(401); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Makes sure the supplied value is a valid RFC2616 date. |
|
147 | + * |
|
148 | + * If we would just use strtotime to get a valid timestamp, we have no way of checking if a |
|
149 | + * user just supplied the word 'now' for the date header. |
|
150 | + * |
|
151 | + * This function also makes sure the Date header is within 15 minutes of the operating |
|
152 | + * system date, to prevent replay attacks. |
|
153 | + */ |
|
154 | + protected function validateRFC2616Date(string $dateHeader): bool |
|
155 | + { |
|
156 | + $date = HTTP\parseDate($dateHeader); |
|
157 | + |
|
158 | + // Unknown format |
|
159 | + if (!$date) { |
|
160 | + $this->errorCode = self::ERR_INVALIDDATEFORMAT; |
|
161 | + |
|
162 | + return false; |
|
163 | + } |
|
164 | + |
|
165 | + $min = new \DateTime('-15 minutes'); |
|
166 | + $max = new \DateTime('+15 minutes'); |
|
167 | + |
|
168 | + // We allow 15 minutes around the current date/time |
|
169 | + if ($date > $max || $date < $min) { |
|
170 | + $this->errorCode = self::ERR_REQUESTTIMESKEWED; |
|
171 | + |
|
172 | + return false; |
|
173 | + } |
|
174 | + |
|
175 | + return true; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Returns a list of AMZ headers. |
|
180 | + */ |
|
181 | + protected function getAmzHeaders(): string |
|
182 | + { |
|
183 | + $amzHeaders = []; |
|
184 | + $headers = $this->request->getHeaders(); |
|
185 | + foreach ($headers as $headerName => $headerValue) { |
|
186 | + if (0 === strpos(strtolower($headerName), 'x-amz-')) { |
|
187 | + $amzHeaders[strtolower($headerName)] = str_replace(["\r\n"], [' '], $headerValue[0])."\n"; |
|
188 | + } |
|
189 | + } |
|
190 | + ksort($amzHeaders); |
|
191 | + |
|
192 | + $headerStr = ''; |
|
193 | + foreach ($amzHeaders as $h => $v) { |
|
194 | + $headerStr .= $h.':'.$v; |
|
195 | + } |
|
196 | + |
|
197 | + return $headerStr; |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Generates an HMAC-SHA1 signature. |
|
202 | + */ |
|
203 | + private function hmacsha1(string $key, string $message): string |
|
204 | + { |
|
205 | + if (function_exists('hash_hmac')) { |
|
206 | + return hash_hmac('sha1', $message, $key, true); |
|
207 | + } |
|
208 | + |
|
209 | + $blocksize = 64; |
|
210 | + if (strlen($key) > $blocksize) { |
|
211 | + $key = pack('H*', sha1($key)); |
|
212 | + } |
|
213 | + $key = str_pad($key, $blocksize, chr(0x00)); |
|
214 | + $ipad = str_repeat(chr(0x36), $blocksize); |
|
215 | + $opad = str_repeat(chr(0x5c), $blocksize); |
|
216 | + $hmac = pack('H*', sha1(($key ^ $opad).pack('H*', sha1(($key ^ $ipad).$message)))); |
|
217 | + |
|
218 | + return $hmac; |
|
219 | + } |
|
220 | 220 | } |
@@ -19,13 +19,13 @@ |
||
19 | 19 | */ |
20 | 20 | interface HttpException |
21 | 21 | { |
22 | - /** |
|
23 | - * The http status code for the error. |
|
24 | - * |
|
25 | - * This may either be just the number, or a number and a human-readable |
|
26 | - * message, separated by a space. |
|
27 | - * |
|
28 | - * @return string|null |
|
29 | - */ |
|
30 | - public function getHttpStatus(); |
|
22 | + /** |
|
23 | + * The http status code for the error. |
|
24 | + * |
|
25 | + * This may either be just the number, or a number and a human-readable |
|
26 | + * message, separated by a space. |
|
27 | + * |
|
28 | + * @return string|null |
|
29 | + */ |
|
30 | + public function getHttpStatus(); |
|
31 | 31 | } |
@@ -386,7 +386,7 @@ discard block |
||
386 | 386 | */ |
387 | 387 | function encodePath(string $path): string |
388 | 388 | { |
389 | - return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\)\/:@])/', function ($match) { |
|
389 | + return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\)\/:@])/', function($match) { |
|
390 | 390 | return '%'.sprintf('%02x', ord($match[0])); |
391 | 391 | }, $path); |
392 | 392 | } |
@@ -398,7 +398,7 @@ discard block |
||
398 | 398 | */ |
399 | 399 | function encodePathSegment(string $pathSegment): string |
400 | 400 | { |
401 | - return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\):@])/', function ($match) { |
|
401 | + return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\):@])/', function($match) { |
|
402 | 402 | return '%'.sprintf('%02x', ord($match[0])); |
403 | 403 | }, $pathSegment); |
404 | 404 | } |
@@ -239,7 +239,7 @@ |
||
239 | 239 | (?: \s* ; (?: .*))? # Prefer parameters (ignored) |
240 | 240 | $ |
241 | 241 | /x |
242 | -REGEX; |
|
242 | +regex; |
|
243 | 243 | |
244 | 244 | $output = []; |
245 | 245 | foreach (getHeaderValues($input) as $value) { |
@@ -33,42 +33,42 @@ discard block |
||
33 | 33 | */ |
34 | 34 | function parseDate(string $dateString) |
35 | 35 | { |
36 | - // Only the format is checked, valid ranges are checked by strtotime below |
|
37 | - $month = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)'; |
|
38 | - $weekday = '(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)'; |
|
39 | - $wkday = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun)'; |
|
40 | - $time = '([0-1]\d|2[0-3])(\:[0-5]\d){2}'; |
|
41 | - $date3 = $month.' ([12]\d|3[01]| [1-9])'; |
|
42 | - $date2 = '(0[1-9]|[12]\d|3[01])\-'.$month.'\-\d{2}'; |
|
43 | - // 4-digit year cannot begin with 0 - unix timestamp begins in 1970 |
|
44 | - $date1 = '(0[1-9]|[12]\d|3[01]) '.$month.' [1-9]\d{3}'; |
|
45 | - |
|
46 | - // ANSI C's asctime() format |
|
47 | - // 4-digit year cannot begin with 0 - unix timestamp begins in 1970 |
|
48 | - $asctime_date = $wkday.' '.$date3.' '.$time.' [1-9]\d{3}'; |
|
49 | - // RFC 850, obsoleted by RFC 1036 |
|
50 | - $rfc850_date = $weekday.', '.$date2.' '.$time.' GMT'; |
|
51 | - // RFC 822, updated by RFC 1123 |
|
52 | - $rfc1123_date = $wkday.', '.$date1.' '.$time.' GMT'; |
|
53 | - // allowed date formats by RFC 2616 |
|
54 | - $HTTP_date = "($rfc1123_date|$rfc850_date|$asctime_date)"; |
|
55 | - |
|
56 | - // allow for space around the string and strip it |
|
57 | - $dateString = trim($dateString, ' '); |
|
58 | - if (!preg_match('/^'.$HTTP_date.'$/', $dateString)) { |
|
59 | - return false; |
|
60 | - } |
|
61 | - |
|
62 | - // append implicit GMT timezone to ANSI C time format |
|
63 | - if (false === strpos($dateString, ' GMT')) { |
|
64 | - $dateString .= ' GMT'; |
|
65 | - } |
|
66 | - |
|
67 | - try { |
|
68 | - return new DateTime($dateString, new \DateTimeZone('UTC')); |
|
69 | - } catch (\Exception $e) { |
|
70 | - return false; |
|
71 | - } |
|
36 | + // Only the format is checked, valid ranges are checked by strtotime below |
|
37 | + $month = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)'; |
|
38 | + $weekday = '(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)'; |
|
39 | + $wkday = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun)'; |
|
40 | + $time = '([0-1]\d|2[0-3])(\:[0-5]\d){2}'; |
|
41 | + $date3 = $month.' ([12]\d|3[01]| [1-9])'; |
|
42 | + $date2 = '(0[1-9]|[12]\d|3[01])\-'.$month.'\-\d{2}'; |
|
43 | + // 4-digit year cannot begin with 0 - unix timestamp begins in 1970 |
|
44 | + $date1 = '(0[1-9]|[12]\d|3[01]) '.$month.' [1-9]\d{3}'; |
|
45 | + |
|
46 | + // ANSI C's asctime() format |
|
47 | + // 4-digit year cannot begin with 0 - unix timestamp begins in 1970 |
|
48 | + $asctime_date = $wkday.' '.$date3.' '.$time.' [1-9]\d{3}'; |
|
49 | + // RFC 850, obsoleted by RFC 1036 |
|
50 | + $rfc850_date = $weekday.', '.$date2.' '.$time.' GMT'; |
|
51 | + // RFC 822, updated by RFC 1123 |
|
52 | + $rfc1123_date = $wkday.', '.$date1.' '.$time.' GMT'; |
|
53 | + // allowed date formats by RFC 2616 |
|
54 | + $HTTP_date = "($rfc1123_date|$rfc850_date|$asctime_date)"; |
|
55 | + |
|
56 | + // allow for space around the string and strip it |
|
57 | + $dateString = trim($dateString, ' '); |
|
58 | + if (!preg_match('/^'.$HTTP_date.'$/', $dateString)) { |
|
59 | + return false; |
|
60 | + } |
|
61 | + |
|
62 | + // append implicit GMT timezone to ANSI C time format |
|
63 | + if (false === strpos($dateString, ' GMT')) { |
|
64 | + $dateString .= ' GMT'; |
|
65 | + } |
|
66 | + |
|
67 | + try { |
|
68 | + return new DateTime($dateString, new \DateTimeZone('UTC')); |
|
69 | + } catch (\Exception $e) { |
|
70 | + return false; |
|
71 | + } |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -76,12 +76,12 @@ discard block |
||
76 | 76 | */ |
77 | 77 | function toDate(DateTime $dateTime): string |
78 | 78 | { |
79 | - // We need to clone it, as we don't want to affect the existing |
|
80 | - // DateTime. |
|
81 | - $dateTime = clone $dateTime; |
|
82 | - $dateTime->setTimezone(new \DateTimeZone('GMT')); |
|
79 | + // We need to clone it, as we don't want to affect the existing |
|
80 | + // DateTime. |
|
81 | + $dateTime = clone $dateTime; |
|
82 | + $dateTime->setTimezone(new \DateTimeZone('GMT')); |
|
83 | 83 | |
84 | - return $dateTime->format('D, d M Y H:i:s \G\M\T'); |
|
84 | + return $dateTime->format('D, d M Y H:i:s \G\M\T'); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
@@ -106,84 +106,84 @@ discard block |
||
106 | 106 | */ |
107 | 107 | function negotiateContentType($acceptHeaderValue, array $availableOptions) |
108 | 108 | { |
109 | - if (!$acceptHeaderValue) { |
|
110 | - // Grabbing the first in the list. |
|
111 | - return reset($availableOptions); |
|
112 | - } |
|
113 | - |
|
114 | - $proposals = array_map( |
|
115 | - 'Sabre\HTTP\parseMimeType', |
|
116 | - explode(',', $acceptHeaderValue) |
|
117 | - ); |
|
118 | - |
|
119 | - // Ensuring array keys are reset. |
|
120 | - $availableOptions = array_values($availableOptions); |
|
121 | - |
|
122 | - $options = array_map( |
|
123 | - 'Sabre\HTTP\parseMimeType', |
|
124 | - $availableOptions |
|
125 | - ); |
|
126 | - |
|
127 | - $lastQuality = 0; |
|
128 | - $lastSpecificity = 0; |
|
129 | - $lastOptionIndex = 0; |
|
130 | - $lastChoice = null; |
|
131 | - |
|
132 | - foreach ($proposals as $proposal) { |
|
133 | - // Ignoring broken values. |
|
134 | - if (null === $proposal) { |
|
135 | - continue; |
|
136 | - } |
|
137 | - |
|
138 | - // If the quality is lower we don't have to bother comparing. |
|
139 | - if ($proposal['quality'] < $lastQuality) { |
|
140 | - continue; |
|
141 | - } |
|
142 | - |
|
143 | - foreach ($options as $optionIndex => $option) { |
|
144 | - if ('*' !== $proposal['type'] && $proposal['type'] !== $option['type']) { |
|
145 | - // no match on type. |
|
146 | - continue; |
|
147 | - } |
|
148 | - if ('*' !== $proposal['subType'] && $proposal['subType'] !== $option['subType']) { |
|
149 | - // no match on subtype. |
|
150 | - continue; |
|
151 | - } |
|
152 | - |
|
153 | - // Any parameters appearing on the options must appear on |
|
154 | - // proposals. |
|
155 | - foreach ($option['parameters'] as $paramName => $paramValue) { |
|
156 | - if (!array_key_exists($paramName, $proposal['parameters'])) { |
|
157 | - continue 2; |
|
158 | - } |
|
159 | - if ($paramValue !== $proposal['parameters'][$paramName]) { |
|
160 | - continue 2; |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - // If we got here, we have a match on parameters, type and |
|
165 | - // subtype. We need to calculate a score for how specific the |
|
166 | - // match was. |
|
167 | - $specificity = |
|
168 | - ('*' !== $proposal['type'] ? 20 : 0) + |
|
169 | - ('*' !== $proposal['subType'] ? 10 : 0) + |
|
170 | - count($option['parameters']); |
|
171 | - |
|
172 | - // Does this entry win? |
|
173 | - if ( |
|
174 | - ($proposal['quality'] > $lastQuality) || |
|
175 | - ($proposal['quality'] === $lastQuality && $specificity > $lastSpecificity) || |
|
176 | - ($proposal['quality'] === $lastQuality && $specificity === $lastSpecificity && $optionIndex < $lastOptionIndex) |
|
177 | - ) { |
|
178 | - $lastQuality = $proposal['quality']; |
|
179 | - $lastSpecificity = $specificity; |
|
180 | - $lastOptionIndex = $optionIndex; |
|
181 | - $lastChoice = $availableOptions[$optionIndex]; |
|
182 | - } |
|
183 | - } |
|
184 | - } |
|
185 | - |
|
186 | - return $lastChoice; |
|
109 | + if (!$acceptHeaderValue) { |
|
110 | + // Grabbing the first in the list. |
|
111 | + return reset($availableOptions); |
|
112 | + } |
|
113 | + |
|
114 | + $proposals = array_map( |
|
115 | + 'Sabre\HTTP\parseMimeType', |
|
116 | + explode(',', $acceptHeaderValue) |
|
117 | + ); |
|
118 | + |
|
119 | + // Ensuring array keys are reset. |
|
120 | + $availableOptions = array_values($availableOptions); |
|
121 | + |
|
122 | + $options = array_map( |
|
123 | + 'Sabre\HTTP\parseMimeType', |
|
124 | + $availableOptions |
|
125 | + ); |
|
126 | + |
|
127 | + $lastQuality = 0; |
|
128 | + $lastSpecificity = 0; |
|
129 | + $lastOptionIndex = 0; |
|
130 | + $lastChoice = null; |
|
131 | + |
|
132 | + foreach ($proposals as $proposal) { |
|
133 | + // Ignoring broken values. |
|
134 | + if (null === $proposal) { |
|
135 | + continue; |
|
136 | + } |
|
137 | + |
|
138 | + // If the quality is lower we don't have to bother comparing. |
|
139 | + if ($proposal['quality'] < $lastQuality) { |
|
140 | + continue; |
|
141 | + } |
|
142 | + |
|
143 | + foreach ($options as $optionIndex => $option) { |
|
144 | + if ('*' !== $proposal['type'] && $proposal['type'] !== $option['type']) { |
|
145 | + // no match on type. |
|
146 | + continue; |
|
147 | + } |
|
148 | + if ('*' !== $proposal['subType'] && $proposal['subType'] !== $option['subType']) { |
|
149 | + // no match on subtype. |
|
150 | + continue; |
|
151 | + } |
|
152 | + |
|
153 | + // Any parameters appearing on the options must appear on |
|
154 | + // proposals. |
|
155 | + foreach ($option['parameters'] as $paramName => $paramValue) { |
|
156 | + if (!array_key_exists($paramName, $proposal['parameters'])) { |
|
157 | + continue 2; |
|
158 | + } |
|
159 | + if ($paramValue !== $proposal['parameters'][$paramName]) { |
|
160 | + continue 2; |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + // If we got here, we have a match on parameters, type and |
|
165 | + // subtype. We need to calculate a score for how specific the |
|
166 | + // match was. |
|
167 | + $specificity = |
|
168 | + ('*' !== $proposal['type'] ? 20 : 0) + |
|
169 | + ('*' !== $proposal['subType'] ? 10 : 0) + |
|
170 | + count($option['parameters']); |
|
171 | + |
|
172 | + // Does this entry win? |
|
173 | + if ( |
|
174 | + ($proposal['quality'] > $lastQuality) || |
|
175 | + ($proposal['quality'] === $lastQuality && $specificity > $lastSpecificity) || |
|
176 | + ($proposal['quality'] === $lastQuality && $specificity === $lastSpecificity && $optionIndex < $lastOptionIndex) |
|
177 | + ) { |
|
178 | + $lastQuality = $proposal['quality']; |
|
179 | + $lastSpecificity = $specificity; |
|
180 | + $lastOptionIndex = $optionIndex; |
|
181 | + $lastChoice = $availableOptions[$optionIndex]; |
|
182 | + } |
|
183 | + } |
|
184 | + } |
|
185 | + |
|
186 | + return $lastChoice; |
|
187 | 187 | } |
188 | 188 | |
189 | 189 | /** |
@@ -214,12 +214,12 @@ discard block |
||
214 | 214 | */ |
215 | 215 | function parsePrefer($input): array |
216 | 216 | { |
217 | - $token = '[!#$%&\'*+\-.^_`~A-Za-z0-9]+'; |
|
217 | + $token = '[!#$%&\'*+\-.^_`~A-Za-z0-9]+'; |
|
218 | 218 | |
219 | - // Work in progress |
|
220 | - $word = '(?: [a-zA-Z0-9]+ | "[a-zA-Z0-9]*" )'; |
|
219 | + // Work in progress |
|
220 | + $word = '(?: [a-zA-Z0-9]+ | "[a-zA-Z0-9]*" )'; |
|
221 | 221 | |
222 | - $regex = <<<REGEX |
|
222 | + $regex = <<<REGEX |
|
223 | 223 | / |
224 | 224 | ^ |
225 | 225 | (?<name> $token) # Prefer property name |
@@ -232,42 +232,42 @@ discard block |
||
232 | 232 | /x |
233 | 233 | REGEX; |
234 | 234 | |
235 | - $output = []; |
|
236 | - foreach (getHeaderValues($input) as $value) { |
|
237 | - if (!preg_match($regex, $value, $matches)) { |
|
238 | - // Ignore |
|
239 | - continue; |
|
240 | - } |
|
241 | - |
|
242 | - // Mapping old values to their new counterparts |
|
243 | - switch ($matches['name']) { |
|
244 | - case 'return-asynch': |
|
245 | - $output['respond-async'] = true; |
|
246 | - break; |
|
247 | - case 'return-representation': |
|
248 | - $output['return'] = 'representation'; |
|
249 | - break; |
|
250 | - case 'return-minimal': |
|
251 | - $output['return'] = 'minimal'; |
|
252 | - break; |
|
253 | - case 'strict': |
|
254 | - $output['handling'] = 'strict'; |
|
255 | - break; |
|
256 | - case 'lenient': |
|
257 | - $output['handling'] = 'lenient'; |
|
258 | - break; |
|
259 | - default: |
|
260 | - if (isset($matches['value'])) { |
|
261 | - $value = trim($matches['value'], '"'); |
|
262 | - } else { |
|
263 | - $value = true; |
|
264 | - } |
|
265 | - $output[strtolower($matches['name'])] = empty($value) ? true : $value; |
|
266 | - break; |
|
267 | - } |
|
268 | - } |
|
269 | - |
|
270 | - return $output; |
|
235 | + $output = []; |
|
236 | + foreach (getHeaderValues($input) as $value) { |
|
237 | + if (!preg_match($regex, $value, $matches)) { |
|
238 | + // Ignore |
|
239 | + continue; |
|
240 | + } |
|
241 | + |
|
242 | + // Mapping old values to their new counterparts |
|
243 | + switch ($matches['name']) { |
|
244 | + case 'return-asynch': |
|
245 | + $output['respond-async'] = true; |
|
246 | + break; |
|
247 | + case 'return-representation': |
|
248 | + $output['return'] = 'representation'; |
|
249 | + break; |
|
250 | + case 'return-minimal': |
|
251 | + $output['return'] = 'minimal'; |
|
252 | + break; |
|
253 | + case 'strict': |
|
254 | + $output['handling'] = 'strict'; |
|
255 | + break; |
|
256 | + case 'lenient': |
|
257 | + $output['handling'] = 'lenient'; |
|
258 | + break; |
|
259 | + default: |
|
260 | + if (isset($matches['value'])) { |
|
261 | + $value = trim($matches['value'], '"'); |
|
262 | + } else { |
|
263 | + $value = true; |
|
264 | + } |
|
265 | + $output[strtolower($matches['name'])] = empty($value) ? true : $value; |
|
266 | + break; |
|
267 | + } |
|
268 | + } |
|
269 | + |
|
270 | + return $output; |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | /** |
@@ -289,19 +289,19 @@ discard block |
||
289 | 289 | */ |
290 | 290 | function getHeaderValues($values, $values2 = null): array |
291 | 291 | { |
292 | - $values = (array) $values; |
|
293 | - if ($values2) { |
|
294 | - $values = array_merge($values, (array) $values2); |
|
295 | - } |
|
296 | - |
|
297 | - $result = []; |
|
298 | - foreach ($values as $l1) { |
|
299 | - foreach (explode(',', $l1) as $l2) { |
|
300 | - $result[] = trim($l2); |
|
301 | - } |
|
302 | - } |
|
303 | - |
|
304 | - return $result; |
|
292 | + $values = (array) $values; |
|
293 | + if ($values2) { |
|
294 | + $values = array_merge($values, (array) $values2); |
|
295 | + } |
|
296 | + |
|
297 | + $result = []; |
|
298 | + foreach ($values as $l1) { |
|
299 | + foreach (explode(',', $l1) as $l2) { |
|
300 | + $result[] = trim($l2); |
|
301 | + } |
|
302 | + } |
|
303 | + |
|
304 | + return $result; |
|
305 | 305 | } |
306 | 306 | |
307 | 307 | /** |
@@ -314,56 +314,56 @@ discard block |
||
314 | 314 | */ |
315 | 315 | function parseMimeType(string $str): array |
316 | 316 | { |
317 | - $parameters = []; |
|
318 | - // If no q= parameter appears, then quality = 1. |
|
319 | - $quality = 1; |
|
320 | - |
|
321 | - $parts = explode(';', $str); |
|
322 | - |
|
323 | - // The first part is the mime-type. |
|
324 | - $mimeType = trim(array_shift($parts)); |
|
325 | - |
|
326 | - if ('*' === $mimeType) { |
|
327 | - $mimeType = '*/*'; |
|
328 | - } |
|
329 | - |
|
330 | - $mimeType = explode('/', $mimeType); |
|
331 | - if (2 !== count($mimeType)) { |
|
332 | - // Illegal value |
|
333 | - var_dump($mimeType); |
|
334 | - exit(); |
|
335 | - // throw new InvalidArgumentException('Not a valid mime-type: '.$str); |
|
336 | - } |
|
337 | - list($type, $subType) = $mimeType; |
|
338 | - |
|
339 | - foreach ($parts as $part) { |
|
340 | - $part = trim($part); |
|
341 | - if (strpos($part, '=')) { |
|
342 | - list($partName, $partValue) = |
|
343 | - explode('=', $part, 2); |
|
344 | - } else { |
|
345 | - $partName = $part; |
|
346 | - $partValue = null; |
|
347 | - } |
|
348 | - |
|
349 | - // The quality parameter, if it appears, also marks the end of |
|
350 | - // the parameter list. Anything after the q= counts as an |
|
351 | - // 'accept extension' and could introduce new semantics in |
|
352 | - // content-negotiation. |
|
353 | - if ('q' !== $partName) { |
|
354 | - $parameters[$partName] = $part; |
|
355 | - } else { |
|
356 | - $quality = (float) $partValue; |
|
357 | - break; // Stop parsing parts |
|
358 | - } |
|
359 | - } |
|
360 | - |
|
361 | - return [ |
|
362 | - 'type' => $type, |
|
363 | - 'subType' => $subType, |
|
364 | - 'quality' => $quality, |
|
365 | - 'parameters' => $parameters, |
|
366 | - ]; |
|
317 | + $parameters = []; |
|
318 | + // If no q= parameter appears, then quality = 1. |
|
319 | + $quality = 1; |
|
320 | + |
|
321 | + $parts = explode(';', $str); |
|
322 | + |
|
323 | + // The first part is the mime-type. |
|
324 | + $mimeType = trim(array_shift($parts)); |
|
325 | + |
|
326 | + if ('*' === $mimeType) { |
|
327 | + $mimeType = '*/*'; |
|
328 | + } |
|
329 | + |
|
330 | + $mimeType = explode('/', $mimeType); |
|
331 | + if (2 !== count($mimeType)) { |
|
332 | + // Illegal value |
|
333 | + var_dump($mimeType); |
|
334 | + exit(); |
|
335 | + // throw new InvalidArgumentException('Not a valid mime-type: '.$str); |
|
336 | + } |
|
337 | + list($type, $subType) = $mimeType; |
|
338 | + |
|
339 | + foreach ($parts as $part) { |
|
340 | + $part = trim($part); |
|
341 | + if (strpos($part, '=')) { |
|
342 | + list($partName, $partValue) = |
|
343 | + explode('=', $part, 2); |
|
344 | + } else { |
|
345 | + $partName = $part; |
|
346 | + $partValue = null; |
|
347 | + } |
|
348 | + |
|
349 | + // The quality parameter, if it appears, also marks the end of |
|
350 | + // the parameter list. Anything after the q= counts as an |
|
351 | + // 'accept extension' and could introduce new semantics in |
|
352 | + // content-negotiation. |
|
353 | + if ('q' !== $partName) { |
|
354 | + $parameters[$partName] = $part; |
|
355 | + } else { |
|
356 | + $quality = (float) $partValue; |
|
357 | + break; // Stop parsing parts |
|
358 | + } |
|
359 | + } |
|
360 | + |
|
361 | + return [ |
|
362 | + 'type' => $type, |
|
363 | + 'subType' => $subType, |
|
364 | + 'quality' => $quality, |
|
365 | + 'parameters' => $parameters, |
|
366 | + ]; |
|
367 | 367 | } |
368 | 368 | |
369 | 369 | /** |
@@ -373,9 +373,9 @@ discard block |
||
373 | 373 | */ |
374 | 374 | function encodePath(string $path): string |
375 | 375 | { |
376 | - return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\)\/:@])/', function ($match) { |
|
377 | - return '%'.sprintf('%02x', ord($match[0])); |
|
378 | - }, $path); |
|
376 | + return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\)\/:@])/', function ($match) { |
|
377 | + return '%'.sprintf('%02x', ord($match[0])); |
|
378 | + }, $path); |
|
379 | 379 | } |
380 | 380 | |
381 | 381 | /** |
@@ -385,9 +385,9 @@ discard block |
||
385 | 385 | */ |
386 | 386 | function encodePathSegment(string $pathSegment): string |
387 | 387 | { |
388 | - return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\):@])/', function ($match) { |
|
389 | - return '%'.sprintf('%02x', ord($match[0])); |
|
390 | - }, $pathSegment); |
|
388 | + return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\):@])/', function ($match) { |
|
389 | + return '%'.sprintf('%02x', ord($match[0])); |
|
390 | + }, $pathSegment); |
|
391 | 391 | } |
392 | 392 | |
393 | 393 | /** |
@@ -395,7 +395,7 @@ discard block |
||
395 | 395 | */ |
396 | 396 | function decodePath(string $path): string |
397 | 397 | { |
398 | - return decodePathSegment($path); |
|
398 | + return decodePathSegment($path); |
|
399 | 399 | } |
400 | 400 | |
401 | 401 | /** |
@@ -403,11 +403,11 @@ discard block |
||
403 | 403 | */ |
404 | 404 | function decodePathSegment(string $path): string |
405 | 405 | { |
406 | - $path = rawurldecode($path); |
|
406 | + $path = rawurldecode($path); |
|
407 | 407 | |
408 | - if (!mb_check_encoding($path, 'UTF-8') && mb_check_encoding($path, 'ISO-8859-1')) { |
|
409 | - $path = mb_convert_encoding($path, 'UTF-8', 'ISO-8859-1'); |
|
410 | - } |
|
408 | + if (!mb_check_encoding($path, 'UTF-8') && mb_check_encoding($path, 'ISO-8859-1')) { |
|
409 | + $path = mb_convert_encoding($path, 'UTF-8', 'ISO-8859-1'); |
|
410 | + } |
|
411 | 411 | |
412 | - return $path; |
|
412 | + return $path; |
|
413 | 413 | } |
@@ -40,9 +40,9 @@ discard block |
||
40 | 40 | */ |
41 | 41 | function enum(Writer $writer, array $values) |
42 | 42 | { |
43 | - foreach ($values as $value) { |
|
44 | - $writer->writeElement($value); |
|
45 | - } |
|
43 | + foreach ($values as $value) { |
|
44 | + $writer->writeElement($value); |
|
45 | + } |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | /** |
@@ -58,17 +58,17 @@ discard block |
||
58 | 58 | */ |
59 | 59 | function valueObject(Writer $writer, $valueObject, string $namespace) |
60 | 60 | { |
61 | - foreach (get_object_vars($valueObject) as $key => $val) { |
|
62 | - if (is_array($val)) { |
|
63 | - // If $val is an array, it has a special meaning. We need to |
|
64 | - // generate one child element for each item in $val |
|
65 | - foreach ($val as $child) { |
|
66 | - $writer->writeElement('{'.$namespace.'}'.$key, $child); |
|
67 | - } |
|
68 | - } elseif (null !== $val) { |
|
69 | - $writer->writeElement('{'.$namespace.'}'.$key, $val); |
|
70 | - } |
|
71 | - } |
|
61 | + foreach (get_object_vars($valueObject) as $key => $val) { |
|
62 | + if (is_array($val)) { |
|
63 | + // If $val is an array, it has a special meaning. We need to |
|
64 | + // generate one child element for each item in $val |
|
65 | + foreach ($val as $child) { |
|
66 | + $writer->writeElement('{'.$namespace.'}'.$key, $child); |
|
67 | + } |
|
68 | + } elseif (null !== $val) { |
|
69 | + $writer->writeElement('{'.$namespace.'}'.$key, $val); |
|
70 | + } |
|
71 | + } |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -88,9 +88,9 @@ discard block |
||
88 | 88 | */ |
89 | 89 | function repeatingElements(Writer $writer, array $items, string $childElementName) |
90 | 90 | { |
91 | - foreach ($items as $item) { |
|
92 | - $writer->writeElement($childElementName, $item); |
|
93 | - } |
|
91 | + foreach ($items as $item) { |
|
92 | + $writer->writeElement($childElementName, $item); |
|
93 | + } |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
@@ -152,57 +152,57 @@ discard block |
||
152 | 152 | */ |
153 | 153 | function standardSerializer(Writer $writer, $value) |
154 | 154 | { |
155 | - if (is_scalar($value)) { |
|
156 | - // String, integer, float, boolean |
|
157 | - $writer->text((string) $value); |
|
158 | - } elseif ($value instanceof XmlSerializable) { |
|
159 | - // XmlSerializable classes or Element classes. |
|
160 | - $value->xmlSerialize($writer); |
|
161 | - } elseif (is_object($value) && isset($writer->classMap[get_class($value)])) { |
|
162 | - // It's an object which class appears in the classmap. |
|
163 | - $writer->classMap[get_class($value)]($writer, $value); |
|
164 | - } elseif (is_callable($value)) { |
|
165 | - // A callback |
|
166 | - $value($writer); |
|
167 | - } elseif (is_array($value) && array_key_exists('name', $value)) { |
|
168 | - // if the array had a 'name' element, we assume that this array |
|
169 | - // describes a 'name' and optionally 'attributes' and 'value'. |
|
155 | + if (is_scalar($value)) { |
|
156 | + // String, integer, float, boolean |
|
157 | + $writer->text((string) $value); |
|
158 | + } elseif ($value instanceof XmlSerializable) { |
|
159 | + // XmlSerializable classes or Element classes. |
|
160 | + $value->xmlSerialize($writer); |
|
161 | + } elseif (is_object($value) && isset($writer->classMap[get_class($value)])) { |
|
162 | + // It's an object which class appears in the classmap. |
|
163 | + $writer->classMap[get_class($value)]($writer, $value); |
|
164 | + } elseif (is_callable($value)) { |
|
165 | + // A callback |
|
166 | + $value($writer); |
|
167 | + } elseif (is_array($value) && array_key_exists('name', $value)) { |
|
168 | + // if the array had a 'name' element, we assume that this array |
|
169 | + // describes a 'name' and optionally 'attributes' and 'value'. |
|
170 | 170 | |
171 | - $name = $value['name']; |
|
172 | - $attributes = isset($value['attributes']) ? $value['attributes'] : []; |
|
173 | - $value = isset($value['value']) ? $value['value'] : null; |
|
171 | + $name = $value['name']; |
|
172 | + $attributes = isset($value['attributes']) ? $value['attributes'] : []; |
|
173 | + $value = isset($value['value']) ? $value['value'] : null; |
|
174 | 174 | |
175 | - $writer->startElement($name); |
|
176 | - $writer->writeAttributes($attributes); |
|
177 | - $writer->write($value); |
|
178 | - $writer->endElement(); |
|
179 | - } elseif (is_array($value)) { |
|
180 | - foreach ($value as $name => $item) { |
|
181 | - if (is_int($name)) { |
|
182 | - // This item has a numeric index. We just loop through the |
|
183 | - // array and throw it back in the writer. |
|
184 | - standardSerializer($writer, $item); |
|
185 | - } elseif (is_string($name) && is_array($item) && isset($item['attributes'])) { |
|
186 | - // The key is used for a name, but $item has 'attributes' and |
|
187 | - // possibly 'value' |
|
188 | - $writer->startElement($name); |
|
189 | - $writer->writeAttributes($item['attributes']); |
|
190 | - if (isset($item['value'])) { |
|
191 | - $writer->write($item['value']); |
|
192 | - } |
|
193 | - $writer->endElement(); |
|
194 | - } elseif (is_string($name)) { |
|
195 | - // This was a plain key-value array. |
|
196 | - $writer->startElement($name); |
|
197 | - $writer->write($item); |
|
198 | - $writer->endElement(); |
|
199 | - } else { |
|
200 | - throw new InvalidArgumentException('The writer does not know how to serialize arrays with keys of type: '.gettype($name)); |
|
201 | - } |
|
202 | - } |
|
203 | - } elseif (is_object($value)) { |
|
204 | - throw new InvalidArgumentException('The writer cannot serialize objects of class: '.get_class($value)); |
|
205 | - } elseif (!is_null($value)) { |
|
206 | - throw new InvalidArgumentException('The writer cannot serialize values of type: '.gettype($value)); |
|
207 | - } |
|
175 | + $writer->startElement($name); |
|
176 | + $writer->writeAttributes($attributes); |
|
177 | + $writer->write($value); |
|
178 | + $writer->endElement(); |
|
179 | + } elseif (is_array($value)) { |
|
180 | + foreach ($value as $name => $item) { |
|
181 | + if (is_int($name)) { |
|
182 | + // This item has a numeric index. We just loop through the |
|
183 | + // array and throw it back in the writer. |
|
184 | + standardSerializer($writer, $item); |
|
185 | + } elseif (is_string($name) && is_array($item) && isset($item['attributes'])) { |
|
186 | + // The key is used for a name, but $item has 'attributes' and |
|
187 | + // possibly 'value' |
|
188 | + $writer->startElement($name); |
|
189 | + $writer->writeAttributes($item['attributes']); |
|
190 | + if (isset($item['value'])) { |
|
191 | + $writer->write($item['value']); |
|
192 | + } |
|
193 | + $writer->endElement(); |
|
194 | + } elseif (is_string($name)) { |
|
195 | + // This was a plain key-value array. |
|
196 | + $writer->startElement($name); |
|
197 | + $writer->write($item); |
|
198 | + $writer->endElement(); |
|
199 | + } else { |
|
200 | + throw new InvalidArgumentException('The writer does not know how to serialize arrays with keys of type: '.gettype($name)); |
|
201 | + } |
|
202 | + } |
|
203 | + } elseif (is_object($value)) { |
|
204 | + throw new InvalidArgumentException('The writer cannot serialize objects of class: '.get_class($value)); |
|
205 | + } elseif (!is_null($value)) { |
|
206 | + throw new InvalidArgumentException('The writer cannot serialize values of type: '.gettype($value)); |
|
207 | + } |
|
208 | 208 | } |