1 | <?php namespace CalDAVClient\Facade\Responses; |
||
2 | /** |
||
3 | * Copyright 2017 OpenStack Foundation |
||
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
5 | * you may not use this file except in compliance with the License. |
||
6 | * You may obtain a copy of the License at |
||
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
8 | * Unless required by applicable law or agreed to in writing, software |
||
9 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
11 | * See the License for the specific language governing permissions and |
||
12 | * limitations under the License. |
||
13 | **/ |
||
14 | use CalDAVClient\Facade\Exceptions\XMLResponseParseException; |
||
15 | /** |
||
16 | * Class AbstractCalDAVResponse |
||
17 | * @package CalDAVClient\Facade\Responses |
||
18 | */ |
||
19 | abstract class AbstractCalDAVResponse extends HttpResponse |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $server_url; |
||
26 | |||
27 | /** |
||
28 | * @var \SimpleXMLElement |
||
29 | */ |
||
30 | protected $xml; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $content; |
||
36 | |||
37 | private $stripped; |
||
38 | |||
39 | /** |
||
40 | * AbstractCalDAVResponse constructor. |
||
41 | * @param string|null $server_url |
||
42 | * @param string|null $body |
||
43 | * @param int $code |
||
44 | */ |
||
45 | public function __construct($server_url = null, $body = null, $code = HttpResponse::HttpCodeOk ) |
||
46 | { |
||
47 | parent::__construct($body, $code); |
||
48 | $this->server_url = $server_url; |
||
49 | if(!empty($this->body)) { |
||
50 | $this->stripped = $this->stripNamespacesFromTags($this->body); |
||
51 | // Merge CDATA as text nodes |
||
52 | $this->xml = simplexml_load_string($this->stripped, null, LIBXML_NOCDATA); |
||
0 ignored issues
–
show
|
|||
53 | if($this->xml === FALSE) |
||
54 | throw new XMLResponseParseException(); |
||
55 | $this->content = $this->toAssocArray($this->xml); |
||
56 | |||
57 | $this->parse(); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | public function __destruct() |
||
62 | { |
||
63 | } |
||
64 | |||
65 | protected function setContent($content){ |
||
66 | $this->content = $content; |
||
67 | } |
||
68 | |||
69 | abstract protected function parse(); |
||
70 | |||
71 | /** |
||
72 | * Strip namespaces from the XML, because otherwise we can't always properly convert |
||
73 | * the XML to an associative JSON array, and some CalDAV servers (such as SabreDAV) |
||
74 | * return only namespaced XML. |
||
75 | * |
||
76 | * @param $xml |
||
77 | * @return string |
||
78 | */ |
||
79 | private function stripNamespacesFromTags($xml) { |
||
80 | // `simplexml_load_string` treats namespaced XML differently than non-namespaced XML, and |
||
81 | // calling `json_encode` on the results of a parsed namespaced XML string will only |
||
82 | // include the non-namespaced tags. Therefore, we remove the namespaces. |
||
83 | // |
||
84 | // Almost literally taken from |
||
85 | // https://laracasts.com/discuss/channels/general-discussion/converting-xml-to-jsonarray/replies/112561 |
||
86 | |||
87 | |||
88 | // We retrieve the namespaces from the XML code so we can check for |
||
89 | // them to remove them |
||
90 | $obj = simplexml_load_string($xml); |
||
91 | $namespaces = $obj->getNamespaces(true); |
||
92 | $toRemove = array_keys($namespaces); |
||
93 | |||
94 | // This is part of a regex I will use to remove the namespace declaration from string |
||
95 | $nameSpaceDefRegEx = '(\S+)=["\']?((?:.(?!["\']?\s+(?:\S+)=|[>"\']))+.)["\']?'; |
||
96 | |||
97 | // Cycle through each namespace and remove it from the XML string |
||
98 | foreach( $toRemove as $remove ) { |
||
99 | // First remove the namespace from the opening of the tag |
||
100 | $xml = str_replace('<' . $remove . ':', '<', $xml); |
||
101 | // Now remove the namespace from the closing of the tag |
||
102 | $xml = str_replace('</' . $remove . ':', '</', $xml); |
||
103 | // This XML uses the name space with CommentText, so remove that too |
||
104 | $xml = str_replace($remove . ':commentText', 'commentText', $xml); |
||
105 | // Complete the pattern for RegEx to remove this namespace declaration |
||
106 | $pattern = "/xmlns:{$remove}{$nameSpaceDefRegEx}/"; |
||
107 | // Remove the actual namespace declaration using the Pattern |
||
108 | $xml = preg_replace($pattern, '', $xml, 1); |
||
109 | } |
||
110 | |||
111 | // Return sanitized and cleaned up XML with no namespaces |
||
112 | return $xml; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param $xml |
||
117 | * @return array |
||
118 | */ |
||
119 | protected function toAssocArray($xml) { |
||
120 | $string = json_encode($xml); |
||
121 | $array = json_decode($string, true); |
||
122 | return $array; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @return bool |
||
127 | */ |
||
128 | protected function isValid(){ |
||
129 | return isset($this->content['response']); |
||
130 | } |
||
131 | } |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.