| Conditions | 66 |
| Paths | > 20000 |
| Total Lines | 321 |
| Code Lines | 198 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 210 | public function parser($urlRSS, $maxNb = 0, $cachedelay = 60, $cachedir = '') |
||
| 211 | { |
||
| 212 | include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; |
||
| 213 | include_once DOL_DOCUMENT_ROOT . '/core/lib/geturl.lib.php'; |
||
| 214 | |||
| 215 | $rss = ''; |
||
| 216 | $str = ''; // This will contain content of feed |
||
| 217 | |||
| 218 | // Check parameters |
||
| 219 | if (!dol_is_url($urlRSS)) { |
||
| 220 | $this->error = "ErrorBadUrl"; |
||
| 221 | return -1; |
||
| 222 | } |
||
| 223 | |||
| 224 | $this->_urlRSS = $urlRSS; |
||
| 225 | $newpathofdestfile = $cachedir . '/' . dol_hash($this->_urlRSS, 3); // Force md5 hash (does not contain special chars) |
||
| 226 | $newmask = '0644'; |
||
| 227 | |||
| 228 | //dol_syslog("RssParser::parser parse url=".$urlRSS." => cache file=".$newpathofdestfile); |
||
| 229 | $nowgmt = dol_now(); |
||
| 230 | |||
| 231 | // Search into cache |
||
| 232 | $foundintocache = 0; |
||
| 233 | if ($cachedelay > 0 && $cachedir) { |
||
| 234 | $filedate = dol_filemtime($newpathofdestfile); |
||
| 235 | if ($filedate >= ($nowgmt - $cachedelay)) { |
||
| 236 | //dol_syslog("RssParser::parser cache file ".$newpathofdestfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay.") so we use it."); |
||
| 237 | $foundintocache = 1; |
||
| 238 | |||
| 239 | $this->_lastfetchdate = $filedate; |
||
| 240 | } else { |
||
| 241 | dol_syslog(get_class($this) . "::parser cache file " . $newpathofdestfile . " is not found or older than now - cachedelay (" . $nowgmt . " - " . $cachedelay . ") so we can't use it."); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | // Load file into $str |
||
| 246 | if ($foundintocache) { // Cache file found and is not too old |
||
| 247 | $str = file_get_contents($newpathofdestfile); |
||
| 248 | } else { |
||
| 249 | try { |
||
| 250 | $result = getURLContent($this->_urlRSS, 'GET', '', 1, array(), array('http', 'https'), 0); |
||
| 251 | |||
| 252 | if (!empty($result['content'])) { |
||
| 253 | $str = $result['content']; |
||
| 254 | } elseif (!empty($result['curl_error_msg'])) { |
||
| 255 | $this->error = 'Error retrieving URL ' . $this->_urlRSS . ' - ' . $result['curl_error_msg']; |
||
| 256 | return -1; |
||
| 257 | } |
||
| 258 | } catch (Exception $e) { |
||
|
|
|||
| 259 | $this->error = 'Error retrieving URL ' . $this->_urlRSS . ' - ' . $e->getMessage(); |
||
| 260 | return -2; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | if ($str !== false) { |
||
| 265 | // Convert $str into xml |
||
| 266 | if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) { |
||
| 267 | //print 'xx'.LIBXML_NOCDATA; |
||
| 268 | libxml_use_internal_errors(false); |
||
| 269 | if (LIBXML_VERSION < 20900) { |
||
| 270 | // Avoid load of external entities (security problem). |
||
| 271 | // Required only if LIBXML_VERSION < 20900 |
||
| 272 | // @phan-suppress-next-line PhanDeprecatedFunctionInternal |
||
| 273 | libxml_disable_entity_loader(true); |
||
| 274 | } |
||
| 275 | |||
| 276 | $rss = simplexml_load_string($str, "SimpleXMLElement", LIBXML_NOCDATA); |
||
| 277 | } else { |
||
| 278 | if (!function_exists('xml_parser_create')) { |
||
| 279 | $this->error = 'Function xml_parser_create are not supported by your PHP'; |
||
| 280 | return -1; |
||
| 281 | } |
||
| 282 | |||
| 283 | try { |
||
| 284 | // @phan-suppress-next-line PhanTypeMismatchArgumentInternalProbablyReal |
||
| 285 | $xmlparser = xml_parser_create(null); |
||
| 286 | |||
| 287 | xml_parser_set_option($xmlparser, XML_OPTION_CASE_FOLDING, 0); |
||
| 288 | xml_parser_set_option($xmlparser, XML_OPTION_SKIP_WHITE, 1); |
||
| 289 | xml_parser_set_option($xmlparser, XML_OPTION_TARGET_ENCODING, "UTF-8"); |
||
| 290 | //xml_set_external_entity_ref_handler($xmlparser, 'extEntHandler'); // Seems to have no effect even when function extEntHandler exists. |
||
| 291 | |||
| 292 | if (!is_resource($xmlparser) && !is_object($xmlparser)) { |
||
| 293 | $this->error = "ErrorFailedToCreateParser"; |
||
| 294 | return -1; |
||
| 295 | } |
||
| 296 | |||
| 297 | xml_set_object($xmlparser, $this); |
||
| 298 | // @phan-suppress-next-line PhanUndeclaredFunctionInCallable |
||
| 299 | xml_set_element_handler($xmlparser, 'feed_start_element', 'feed_end_element'); |
||
| 300 | // @phan-suppress-next-line PhanUndeclaredFunctionInCallable |
||
| 301 | xml_set_character_data_handler($xmlparser, 'feed_cdata'); |
||
| 302 | |||
| 303 | $status = xml_parse($xmlparser, $str, false); |
||
| 304 | |||
| 305 | xml_parser_free($xmlparser); |
||
| 306 | |||
| 307 | $rss = $this; |
||
| 308 | //var_dump($status.' '.$rss->_format);exit; |
||
| 309 | } catch (Exception $e) { |
||
| 310 | $rss = null; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | // If $rss loaded |
||
| 316 | if ($rss) { |
||
| 317 | // Save file into cache |
||
| 318 | if (empty($foundintocache) && $cachedir) { |
||
| 319 | dol_syslog(get_class($this) . "::parser cache file " . $newpathofdestfile . " is saved onto disk."); |
||
| 320 | if (!dol_is_dir($cachedir)) { |
||
| 321 | dol_mkdir($cachedir); |
||
| 322 | } |
||
| 323 | $fp = fopen($newpathofdestfile, 'w'); |
||
| 324 | if ($fp) { |
||
| 325 | fwrite($fp, $str); |
||
| 326 | fclose($fp); |
||
| 327 | dolChmod($newpathofdestfile); |
||
| 328 | |||
| 329 | $this->_lastfetchdate = $nowgmt; |
||
| 330 | } else { |
||
| 331 | print 'Error, failed to open file ' . $newpathofdestfile . ' for write'; |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | unset($str); // Free memory |
||
| 336 | |||
| 337 | if (empty($rss->_format)) { // If format not detected automatically |
||
| 338 | $rss->_format = 'rss'; |
||
| 339 | if (empty($rss->channel)) { |
||
| 340 | $rss->_format = 'atom'; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | $items = array(); |
||
| 345 | |||
| 346 | // Save description entries |
||
| 347 | if ($rss->_format == 'rss') { |
||
| 348 | //var_dump($rss); |
||
| 349 | if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) { |
||
| 350 | if (!empty($rss->channel->language)) { |
||
| 351 | $this->_language = sanitizeVal((string) $rss->channel->language); |
||
| 352 | } |
||
| 353 | if (!empty($rss->channel->generator)) { |
||
| 354 | $this->_generator = sanitizeVal((string) $rss->channel->generator); |
||
| 355 | } |
||
| 356 | if (!empty($rss->channel->copyright)) { |
||
| 357 | $this->_copyright = sanitizeVal((string) $rss->channel->copyright); |
||
| 358 | } |
||
| 359 | if (!empty($rss->channel->lastbuilddate)) { |
||
| 360 | $this->_lastbuilddate = sanitizeVal((string) $rss->channel->lastbuilddate); |
||
| 361 | } |
||
| 362 | if (!empty($rss->channel->image->url[0])) { |
||
| 363 | $this->_imageurl = sanitizeVal((string) $rss->channel->image->url[0]); |
||
| 364 | } |
||
| 365 | if (!empty($rss->channel->link)) { |
||
| 366 | $this->_link = sanitizeVal((string) $rss->channel->link); |
||
| 367 | } |
||
| 368 | if (!empty($rss->channel->title)) { |
||
| 369 | $this->_title = sanitizeVal((string) $rss->channel->title); |
||
| 370 | } |
||
| 371 | if (!empty($rss->channel->description)) { |
||
| 372 | $this->_description = sanitizeVal((string) $rss->channel->description); |
||
| 373 | } |
||
| 374 | } else { |
||
| 375 | //var_dump($rss->channel); |
||
| 376 | if (!empty($rss->channel['language'])) { |
||
| 377 | $this->_language = sanitizeVal((string) $rss->channel['language']); |
||
| 378 | } |
||
| 379 | if (!empty($rss->channel['generator'])) { |
||
| 380 | $this->_generator = sanitizeVal((string) $rss->channel['generator']); |
||
| 381 | } |
||
| 382 | if (!empty($rss->channel['copyright'])) { |
||
| 383 | $this->_copyright = sanitizeVal((string) $rss->channel['copyright']); |
||
| 384 | } |
||
| 385 | if (!empty($rss->channel['lastbuilddate'])) { |
||
| 386 | $this->_lastbuilddate = sanitizeVal((string) $rss->channel['lastbuilddate']); |
||
| 387 | } |
||
| 388 | if (!empty($rss->image['url'])) { |
||
| 389 | $this->_imageurl = sanitizeVal((string) $rss->image['url']); |
||
| 390 | } |
||
| 391 | if (!empty($rss->channel['link'])) { |
||
| 392 | $this->_link = sanitizeVal((string) $rss->channel['link']); |
||
| 393 | } |
||
| 394 | if (!empty($rss->channel['title'])) { |
||
| 395 | $this->_title = sanitizeVal((string) $rss->channel['title']); |
||
| 396 | } |
||
| 397 | if (!empty($rss->channel['description'])) { |
||
| 398 | $this->_description = sanitizeVal((string) $rss->channel['description']); |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) { |
||
| 403 | $items = $rss->channel->item; // With simplexml |
||
| 404 | } else { |
||
| 405 | $items = $rss->items; // With xmlparse |
||
| 406 | } |
||
| 407 | //var_dump($items);exit; |
||
| 408 | } elseif ($rss->_format == 'atom') { |
||
| 409 | //var_dump($rss); |
||
| 410 | if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) { |
||
| 411 | if (!empty($rss->generator)) { |
||
| 412 | $this->_generator = sanitizeVal((string) $rss->generator); |
||
| 413 | } |
||
| 414 | if (!empty($rss->lastbuilddate)) { |
||
| 415 | $this->_lastbuilddate = sanitizeVal((string) $rss->modified); |
||
| 416 | } |
||
| 417 | if (!empty($rss->link->href)) { |
||
| 418 | $this->_link = sanitizeVal((string) $rss->link->href); |
||
| 419 | } |
||
| 420 | if (!empty($rss->title)) { |
||
| 421 | $this->_title = sanitizeVal((string) $rss->title); |
||
| 422 | } |
||
| 423 | if (!empty($rss->description)) { |
||
| 424 | $this->_description = sanitizeVal((string) $rss->description); |
||
| 425 | } |
||
| 426 | } else { |
||
| 427 | //if (!empty($rss->channel['rss_language'])) $this->_language = (string) $rss->channel['rss_language']; |
||
| 428 | if (!empty($rss->channel['generator'])) { |
||
| 429 | $this->_generator = sanitizeVal((string) $rss->channel['generator']); |
||
| 430 | } |
||
| 431 | //if (!empty($rss->channel['rss_copyright'])) $this->_copyright = (string) $rss->channel['rss_copyright']; |
||
| 432 | if (!empty($rss->channel['modified'])) { |
||
| 433 | $this->_lastbuilddate = sanitizeVal((string) $rss->channel['modified']); |
||
| 434 | } |
||
| 435 | //if (!empty($rss->image['rss_url'])) $this->_imageurl = (string) $rss->image['rss_url']; |
||
| 436 | if (!empty($rss->channel['link'])) { |
||
| 437 | $this->_link = sanitizeVal((string) $rss->channel['link']); |
||
| 438 | } |
||
| 439 | if (!empty($rss->channel['title'])) { |
||
| 440 | $this->_title = sanitizeVal((string) $rss->channel['title']); |
||
| 441 | } |
||
| 442 | //if (!empty($rss->channel['rss_description'])) $this->_description = (string) $rss->channel['rss_description']; |
||
| 443 | |||
| 444 | if (!empty($rss->channel)) { |
||
| 445 | $this->_imageurl = sanitizeVal($this->getAtomImageUrl($rss->channel)); |
||
| 446 | } |
||
| 447 | } |
||
| 448 | if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) { |
||
| 449 | $tmprss = xml2php($rss); |
||
| 450 | $items = $tmprss['entry']; |
||
| 451 | } else { |
||
| 452 | // With simplexml |
||
| 453 | $items = $rss->items; // With xmlparse |
||
| 454 | } |
||
| 455 | //var_dump($items);exit; |
||
| 456 | } |
||
| 457 | |||
| 458 | $i = 0; |
||
| 459 | |||
| 460 | // Loop on each record |
||
| 461 | if (is_array($items)) { |
||
| 462 | foreach ($items as $item) { |
||
| 463 | //var_dump($item);exit; |
||
| 464 | if ($rss->_format == 'rss') { |
||
| 465 | if (getDolGlobalString('EXTERNALRSS_USE_SIMPLEXML')) { |
||
| 466 | $itemLink = sanitizeVal((string) $item->link); |
||
| 467 | $itemTitle = sanitizeVal((string) $item->title); |
||
| 468 | $itemDescription = sanitizeVal((string) $item->description); |
||
| 469 | $itemPubDate = sanitizeVal((string) $item->pubDate); |
||
| 470 | $itemId = ''; |
||
| 471 | $itemAuthor = ''; |
||
| 472 | } else { |
||
| 473 | $itemLink = sanitizeVal((string) $item['link']); |
||
| 474 | $itemTitle = sanitizeVal((string) $item['title']); |
||
| 475 | $itemDescription = sanitizeVal((string) $item['description']); |
||
| 476 | $itemPubDate = sanitizeVal((string) $item['pubdate']); |
||
| 477 | $itemId = sanitizeVal((string) $item['guid']); |
||
| 478 | $itemAuthor = sanitizeVal((string) ($item['author'] ?? '')); |
||
| 479 | } |
||
| 480 | |||
| 481 | // Loop on each category |
||
| 482 | $itemCategory = array(); |
||
| 483 | if (!empty($item->category) && is_array($item->category)) { |
||
| 484 | foreach ($item->category as $cat) { |
||
| 485 | $itemCategory[] = (string) $cat; |
||
| 486 | } |
||
| 487 | } |
||
| 488 | } elseif ($rss->_format == 'atom') { |
||
| 489 | $itemLink = (isset($item['link']) ? sanitizeVal((string) $item['link']) : ''); |
||
| 490 | $itemTitle = sanitizeVal((string) $item['title']); |
||
| 491 | $itemDescription = sanitizeVal($this->getAtomItemDescription($item)); |
||
| 492 | $itemPubDate = sanitizeVal((string) $item['created']); |
||
| 493 | $itemId = sanitizeVal((string) $item['id']); |
||
| 494 | $itemAuthor = sanitizeVal((string) ($item['author'] ? $item['author'] : $item['author_name'])); |
||
| 495 | $itemCategory = array(); |
||
| 496 | } else { |
||
| 497 | $itemLink = ''; |
||
| 498 | $itemTitle = ''; |
||
| 499 | $itemDescription = ''; |
||
| 500 | $itemPubDate = ''; |
||
| 501 | $itemId = ''; |
||
| 502 | $itemAuthor = ''; |
||
| 503 | $itemCategory = array(); |
||
| 504 | print 'ErrorBadFeedFormat'; |
||
| 505 | } |
||
| 506 | |||
| 507 | // Add record to result array |
||
| 508 | $this->_rssarray[$i] = array( |
||
| 509 | 'link' => $itemLink, |
||
| 510 | 'title' => $itemTitle, |
||
| 511 | 'description' => $itemDescription, |
||
| 512 | 'pubDate' => $itemPubDate, |
||
| 513 | 'category' => $itemCategory, |
||
| 514 | 'id' => $itemId, |
||
| 515 | 'author' => $itemAuthor |
||
| 516 | ); |
||
| 517 | //var_dump($this->_rssarray); |
||
| 518 | |||
| 519 | $i++; |
||
| 520 | |||
| 521 | if ($i > $maxNb) { |
||
| 522 | break; // We get all records we want |
||
| 523 | } |
||
| 524 | } |
||
| 525 | } |
||
| 526 | |||
| 527 | return 1; |
||
| 528 | } else { |
||
| 529 | $this->error = 'ErrorFailedToLoadRSSFile'; |
||
| 530 | return -1; |
||
| 531 | } |
||
| 890 |