spiderling-php /
crawler
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SP\Crawler; |
||
| 4 | |||
| 5 | use SP\Spiderling\CrawlerInterface; |
||
| 6 | use SP\Crawler\Element\ClickRequestInterface; |
||
| 7 | use SP\Crawler\Element\ClickableInterface; |
||
| 8 | use SP\Crawler\Element\SelectableInterface; |
||
| 9 | use SP\Crawler\Element\File; |
||
| 10 | use Psr\Http\Message\ServerRequestInterface; |
||
| 11 | use SP\Spiderling\Query\AbstractQuery; |
||
| 12 | use GuzzleHttp\Psr7\Uri; |
||
| 13 | use Psr\Http\Message\UriInterface; |
||
| 14 | use DOMDocument; |
||
| 15 | use DOMElement; |
||
| 16 | use InvalidArgumentException; |
||
| 17 | use BadMethodCallException; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @author Ivan Kerin <[email protected]> |
||
| 21 | * @copyright 2015, Clippings Ltd. |
||
| 22 | * @license http://spdx.org/licenses/BSD-3-Clause |
||
| 23 | */ |
||
| 24 | class Reader implements CrawlerInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var DOMDocument |
||
| 28 | */ |
||
| 29 | private $document; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var SafeXPath |
||
| 33 | */ |
||
| 34 | private $xpath; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var ElementMap |
||
| 38 | */ |
||
| 39 | private $inputMap; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param DOMDocument $document |
||
| 43 | */ |
||
| 44 | 1 | public function __construct(DOMDocument $document) |
|
| 45 | { |
||
| 46 | 1 | $this->document = $document; |
|
| 47 | |||
| 48 | 1 | $this->xpath = new SafeXPath($document); |
|
| 49 | |||
| 50 | 1 | $this->inputMap = new InputMap($this); |
|
|
0 ignored issues
–
show
|
|||
| 51 | 1 | } |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @param string $content |
||
| 55 | * @return self |
||
| 56 | */ |
||
| 57 | 1 | public function setDocumentContent($content) |
|
| 58 | { |
||
| 59 | 1 | $this->document->loadHtml((string) $content); |
|
| 60 | 1 | $this->xpath = new SafeXPath($this->document); |
|
| 61 | |||
| 62 | 1 | return $this; |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return DOMDocument |
||
| 67 | */ |
||
| 68 | 1 | public function getDocument() |
|
| 69 | { |
||
| 70 | 1 | return $this->document; |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return SafeXPath |
||
| 75 | */ |
||
| 76 | 1 | public function getXPath() |
|
| 77 | { |
||
| 78 | 1 | return $this->xpath; |
|
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return ElementMap |
||
| 83 | */ |
||
| 84 | 1 | public function getInputMap() |
|
| 85 | { |
||
| 86 | 1 | return $this->inputMap; |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $id |
||
| 91 | * @throws BadMethodCallException |
||
| 92 | */ |
||
| 93 | 3 | public function click($id) |
|
| 94 | { |
||
| 95 | 3 | $input = $this->getInput($this->getElement($id)); |
|
| 96 | |||
| 97 | 3 | if ($input instanceof ClickableInterface) { |
|
| 98 | 1 | $input->click(); |
|
| 99 | 3 | } elseif ($input instanceof ClickRequestInterface) { |
|
| 100 | 1 | $request = $input->clickRequest(); |
|
| 101 | 1 | $this->sendRequest($request); |
|
| 102 | 1 | } else { |
|
| 103 | 1 | throw new BadMethodCallException( |
|
| 104 | 1 | sprintf('Cannot click on %s, %s', get_class($input), $id) |
|
| 105 | 1 | ); |
|
| 106 | } |
||
| 107 | 2 | } |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $id |
||
| 111 | * @throws BadMethodCallException |
||
| 112 | */ |
||
| 113 | 2 | public function select($id) |
|
| 114 | { |
||
| 115 | 2 | $input = $this->getInput($this->getElement($id)); |
|
| 116 | |||
| 117 | 2 | if ($input instanceof SelectableInterface) { |
|
| 118 | 1 | $input->select(); |
|
| 119 | 1 | } else { |
|
| 120 | 1 | throw new BadMethodCallException( |
|
| 121 | 1 | sprintf('Cannot select on %s, %s', get_class($input), $id) |
|
| 122 | 1 | ); |
|
| 123 | } |
||
| 124 | 1 | } |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @param ServerRequestInterface $input |
||
| 128 | * @throws BadMethodCallException |
||
| 129 | */ |
||
| 130 | 1 | public function sendRequest(ServerRequestInterface $request) |
|
| 131 | { |
||
| 132 | 1 | throw new BadMethodCallException( |
|
| 133 | 1 | sprintf('Cannot send request to %s', $request->getUri()) |
|
| 134 | 1 | ); |
|
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $url |
||
| 139 | * @throws BadMethodCallException |
||
| 140 | */ |
||
| 141 | 1 | public function open(UriInterface $url) |
|
| 142 | { |
||
| 143 | 1 | throw new BadMethodCallException( |
|
| 144 | 1 | sprintf('Method %s not supported by %s', __METHOD__, __CLASS__) |
|
| 145 | 1 | ); |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return Psr\Http\Message\UriInterface |
||
| 150 | */ |
||
| 151 | 1 | public function getUri() |
|
| 152 | { |
||
| 153 | 1 | return new Uri(''); |
|
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $xpath |
||
| 158 | * @param DOMElement|null $scope |
||
| 159 | * @throws InvalidArgumentException If xpath is not valid |
||
| 160 | * @return DOMNodeList |
||
| 161 | */ |
||
| 162 | 1 | public function query($xpath, DOMElement $scope = null) |
|
| 163 | { |
||
| 164 | 1 | return $this->getXpath()->query($xpath, $scope); |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $xpath |
||
| 169 | * @throws InvalidArgumentException when id not found |
||
| 170 | * @return DOMElement |
||
| 171 | */ |
||
| 172 | 1 | public function getElement($xpath) |
|
| 173 | { |
||
| 174 | 1 | $items = $this->query($xpath); |
|
| 175 | |||
| 176 | 1 | if (0 === $items->length) { |
|
| 177 | 1 | throw new InvalidArgumentException( |
|
| 178 | 1 | sprintf('Node with id %s does not exist', $xpath) |
|
| 179 | 1 | ); |
|
| 180 | } |
||
| 181 | |||
| 182 | 1 | return $items->item(0); |
|
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $id |
||
| 187 | * @throws InvalidArgumentException when id not found |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | 1 | public function getText($id) |
|
| 191 | { |
||
| 192 | 1 | $element = $this->getElement($id); |
|
| 193 | |||
| 194 | 1 | return trim(preg_replace('/[ \s\f\n\r\t\v ]+/u', ' ', $element->textContent)); |
|
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $id |
||
| 199 | * @throws InvalidArgumentException when id not found |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | 1 | public function getTagName($id) |
|
| 203 | { |
||
| 204 | 1 | return $this->getElement($id)->tagName; |
|
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $id |
||
| 209 | * @param string $name |
||
| 210 | * @throws InvalidArgumentException when id not found |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 1 | public function getAttribute($id, $name) |
|
| 214 | { |
||
| 215 | 1 | return $this->getElement($id)->getAttribute($name); |
|
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $id |
||
| 220 | * @throws InvalidArgumentException when id not found |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | 1 | public function getHtml($id) |
|
| 224 | { |
||
| 225 | 1 | return $this->document->saveXml($this->getElement($id)); |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | 1 | public function getFullHtml() |
|
| 232 | { |
||
| 233 | 1 | return $this->document->saveHtml(); |
|
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $id |
||
| 238 | * @throws InvalidArgumentException when id not found |
||
| 239 | * @return boolean |
||
| 240 | */ |
||
| 241 | 3 | public function isVisible($id) |
|
| 242 | { |
||
| 243 | 3 | $element = $this->getElement($id); |
|
| 244 | |||
| 245 | $conditions = [ |
||
| 246 | 3 | "contains(@style, 'display:none')", |
|
| 247 | 3 | "contains(@style, 'display: none')", |
|
| 248 | 3 | "self::script", |
|
| 249 | 3 | "self::head", |
|
| 250 | 3 | ]; |
|
| 251 | |||
| 252 | 3 | $hidden = $this->xpath->query( |
|
| 253 | 3 | './ancestor-or-self::*['.join(' or ', $conditions).']', |
|
| 254 | $element |
||
| 255 | 3 | ); |
|
| 256 | |||
| 257 | 3 | return $hidden->length == 0; |
|
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $id |
||
| 262 | * @throws InvalidArgumentException when id not found |
||
| 263 | * @return boolean |
||
| 264 | */ |
||
| 265 | 4 | public function isSelected($id) |
|
| 266 | { |
||
| 267 | 4 | return $this->getElement($id)->hasAttribute('selected'); |
|
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $id |
||
| 272 | * @throws InvalidArgumentException when id not found |
||
| 273 | * @return boolean |
||
| 274 | */ |
||
| 275 | 6 | public function isChecked($id) |
|
| 276 | { |
||
| 277 | 6 | return $this->getElement($id)->hasAttribute('checked'); |
|
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param DOMElement $element |
||
| 282 | * @throws InvalidArgumentException when id not found |
||
| 283 | * @return Element\AbstractElement |
||
| 284 | */ |
||
| 285 | 9 | public function getInput(DOMElement $element) |
|
| 286 | { |
||
| 287 | 9 | return $this->inputMap->get($element); |
|
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $id |
||
| 292 | * @throws InvalidArgumentException when id not found |
||
| 293 | * @return mixed |
||
| 294 | */ |
||
| 295 | 1 | public function getValue($id) |
|
| 296 | { |
||
| 297 | 1 | return $this->getInput($this->getElement($id))->getValue(); |
|
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $id |
||
| 302 | * @param string $value |
||
| 303 | * @throws InvalidArgumentException when id not found |
||
| 304 | */ |
||
| 305 | 1 | public function setValue($id, $value) |
|
| 306 | { |
||
| 307 | 1 | $input = $this->getInput($this->getElement($id)); |
|
| 308 | |||
| 309 | 1 | if (false === $input->isDisabled()) { |
|
| 310 | 1 | $input->setValue($value); |
|
| 311 | 1 | } |
|
| 312 | 1 | } |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $id |
||
| 316 | * @param string $file |
||
| 317 | * @throws InvalidArgumentException when id not found or not a file |
||
| 318 | */ |
||
| 319 | 2 | public function setFile($id, $file) |
|
| 320 | { |
||
| 321 | 2 | $input = $this->getInput($this->getElement($id)); |
|
| 322 | |||
| 323 | 2 | if (false === ($input instanceof File)) { |
|
| 324 | 1 | throw new InvalidArgumentException( |
|
| 325 | 1 | sprintf('Node with id %s is not a file', $id) |
|
| 326 | 1 | ); |
|
| 327 | } |
||
| 328 | |||
| 329 | 1 | if (false === $input->isDisabled()) { |
|
| 330 | 1 | $input->setValue($file); |
|
| 331 | 1 | } |
|
| 332 | 1 | } |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @param AbstractQuery $query |
||
| 336 | * @param string $parent |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | 1 | public function queryIds(AbstractQuery $query, $parent = null) |
|
| 340 | { |
||
| 341 | 1 | $xpath = $parent.$query->getXPath(); |
|
| 342 | |||
| 343 | 1 | $ids = []; |
|
| 344 | |||
| 345 | 1 | foreach ($this->query($xpath) as $index => $element) { |
|
| 346 | 1 | $ids []= "($xpath)[".($index+1)."]"; |
|
| 347 | 1 | } |
|
| 348 | |||
| 349 | 1 | return $query->getFilters()->matchAll($this, $ids); |
|
| 350 | } |
||
| 351 | } |
||
| 352 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..