| Conditions | 64 |
| Paths | > 20000 |
| Total Lines | 274 |
| Code Lines | 154 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 187 | public function parser($urlRSS, $maxNb = 0, $cachedelay = 60, $cachedir = '') |
||
| 188 | { |
||
| 189 | global $conf; |
||
| 190 | |||
| 191 | include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; |
||
| 192 | |||
| 193 | $rss = ''; |
||
| 194 | $str = ''; // This will contain content of feed |
||
| 195 | |||
| 196 | // Check parameters |
||
| 197 | if (!dol_is_url($urlRSS)) |
||
| 198 | { |
||
| 199 | $this->error = "ErrorBadUrl"; |
||
| 200 | return -1; |
||
| 201 | } |
||
| 202 | |||
| 203 | $this->_urlRSS = $urlRSS; |
||
| 204 | $newpathofdestfile = $cachedir.'/'.dol_hash($this->_urlRSS, 3); // Force md5 hash (does not contains special chars) |
||
| 205 | $newmask = '0644'; |
||
| 206 | |||
| 207 | //dol_syslog("RssPArser::parser parse url=".$urlRSS." => cache file=".$newpathofdestfile); |
||
| 208 | $nowgmt = dol_now(); |
||
| 209 | |||
| 210 | // Search into cache |
||
| 211 | $foundintocache = 0; |
||
| 212 | if ($cachedelay > 0 && $cachedir) |
||
| 213 | { |
||
| 214 | $filedate = dol_filemtime($newpathofdestfile); |
||
| 215 | if ($filedate >= ($nowgmt - $cachedelay)) |
||
| 216 | { |
||
| 217 | //dol_syslog("RssParser::parser cache file ".$newpathofdestfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay.") so we use it."); |
||
| 218 | $foundintocache = 1; |
||
| 219 | |||
| 220 | $this->_lastfetchdate = $filedate; |
||
| 221 | } |
||
| 222 | else |
||
| 223 | { |
||
| 224 | 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."); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | // Load file into $str |
||
| 229 | if ($foundintocache) // Cache file found and is not too old |
||
| 230 | { |
||
| 231 | $str = file_get_contents($newpathofdestfile); |
||
| 232 | } |
||
| 233 | else |
||
| 234 | { |
||
| 235 | try { |
||
| 236 | ini_set("user_agent", "Dolibarr ERP-CRM RSS reader"); |
||
| 237 | ini_set("max_execution_time", $conf->global->MAIN_USE_RESPONSE_TIMEOUT); |
||
| 238 | ini_set("default_socket_timeout", $conf->global->MAIN_USE_RESPONSE_TIMEOUT); |
||
| 239 | |||
| 240 | $opts = array('http'=>array('method'=>"GET")); |
||
| 241 | if (!empty($conf->global->MAIN_USE_CONNECT_TIMEOUT)) $opts['http']['timeout'] = $conf->global->MAIN_USE_CONNECT_TIMEOUT; |
||
| 242 | if (!empty($conf->global->MAIN_PROXY_USE)) $opts['http']['proxy'] = 'tcp://'.$conf->global->MAIN_PROXY_HOST.':'.$conf->global->MAIN_PROXY_PORT; |
||
| 243 | //var_dump($opts);exit; |
||
| 244 | $context = stream_context_create($opts); |
||
| 245 | |||
| 246 | $str = file_get_contents($this->_urlRSS, false, $context); |
||
| 247 | } |
||
| 248 | catch (Exception $e) { |
||
| 249 | print 'Error retrieving URL '.$this->_urlRSS.' - '.$e->getMessage(); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | if ($str !== false) |
||
| 254 | { |
||
| 255 | // Convert $str into xml |
||
| 256 | if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) |
||
| 257 | { |
||
| 258 | //print 'xx'.LIBXML_NOCDATA; |
||
| 259 | libxml_use_internal_errors(false); |
||
| 260 | $rss = simplexml_load_string($str, "SimpleXMLElement", LIBXML_NOCDATA); |
||
| 261 | } |
||
| 262 | else |
||
| 263 | { |
||
| 264 | $xmlparser = xml_parser_create(''); |
||
| 265 | if (!is_resource($xmlparser)) { |
||
| 266 | $this->error = "ErrorFailedToCreateParser"; return -1; |
||
| 267 | } |
||
| 268 | |||
| 269 | xml_set_object($xmlparser, $this); |
||
| 270 | xml_set_element_handler($xmlparser, 'feed_start_element', 'feed_end_element'); |
||
| 271 | xml_set_character_data_handler($xmlparser, 'feed_cdata'); |
||
| 272 | $status = xml_parse($xmlparser, $str); |
||
| 273 | xml_parser_free($xmlparser); |
||
| 274 | $rss = $this; |
||
| 275 | //var_dump($rss->_format);exit; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | // If $rss loaded |
||
| 280 | if ($rss) |
||
| 281 | { |
||
| 282 | // Save file into cache |
||
| 283 | if (empty($foundintocache) && $cachedir) |
||
| 284 | { |
||
| 285 | dol_syslog(get_class($this)."::parser cache file ".$newpathofdestfile." is saved onto disk."); |
||
| 286 | if (!dol_is_dir($cachedir)) dol_mkdir($cachedir); |
||
| 287 | $fp = fopen($newpathofdestfile, 'w'); |
||
| 288 | if ($fp) |
||
| 289 | { |
||
| 290 | fwrite($fp, $str); |
||
| 291 | fclose($fp); |
||
| 292 | if (!empty($conf->global->MAIN_UMASK)) $newmask = $conf->global->MAIN_UMASK; |
||
| 293 | @chmod($newpathofdestfile, octdec($newmask)); |
||
| 294 | |||
| 295 | $this->_lastfetchdate = $nowgmt; |
||
| 296 | } |
||
| 297 | else |
||
| 298 | { |
||
| 299 | print 'Error, failed to open file '.$newpathofdestfile.' for write'; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | unset($str); // Free memory |
||
| 304 | |||
| 305 | if (empty($rss->_format)) // If format not detected automatically |
||
| 306 | { |
||
| 307 | $rss->_format = 'rss'; |
||
|
|
|||
| 308 | if (empty($rss->channel)) $rss->_format = 'atom'; |
||
| 309 | } |
||
| 310 | |||
| 311 | $items = array(); |
||
| 312 | |||
| 313 | // Save description entries |
||
| 314 | if ($rss->_format == 'rss') |
||
| 315 | { |
||
| 316 | //var_dump($rss); |
||
| 317 | if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) |
||
| 318 | { |
||
| 319 | if (!empty($rss->channel->language)) $this->_language = (string) $rss->channel->language; |
||
| 320 | if (!empty($rss->channel->generator)) $this->_generator = (string) $rss->channel->generator; |
||
| 321 | if (!empty($rss->channel->copyright)) $this->_copyright = (string) $rss->channel->copyright; |
||
| 322 | if (!empty($rss->channel->lastbuilddate)) $this->_lastbuilddate = (string) $rss->channel->lastbuilddate; |
||
| 323 | if (!empty($rss->channel->image->url[0])) $this->_imageurl = (string) $rss->channel->image->url[0]; |
||
| 324 | if (!empty($rss->channel->link)) $this->_link = (string) $rss->channel->link; |
||
| 325 | if (!empty($rss->channel->title)) $this->_title = (string) $rss->channel->title; |
||
| 326 | if (!empty($rss->channel->description)) $this->_description = (string) $rss->channel->description; |
||
| 327 | } |
||
| 328 | else |
||
| 329 | { |
||
| 330 | //var_dump($rss->channel); |
||
| 331 | if (!empty($rss->channel['language'])) $this->_language = (string) $rss->channel['language']; |
||
| 332 | if (!empty($rss->channel['generator'])) $this->_generator = (string) $rss->channel['generator']; |
||
| 333 | if (!empty($rss->channel['copyright'])) $this->_copyright = (string) $rss->channel['copyright']; |
||
| 334 | if (!empty($rss->channel['lastbuilddate'])) $this->_lastbuilddate = (string) $rss->channel['lastbuilddate']; |
||
| 335 | if (!empty($rss->image['url'])) $this->_imageurl = (string) $rss->image['url']; |
||
| 336 | if (!empty($rss->channel['link'])) $this->_link = (string) $rss->channel['link']; |
||
| 337 | if (!empty($rss->channel['title'])) $this->_title = (string) $rss->channel['title']; |
||
| 338 | if (!empty($rss->channel['description'])) $this->_description = (string) $rss->channel['description']; |
||
| 339 | } |
||
| 340 | |||
| 341 | if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) $items = $rss->channel->item; // With simplexml |
||
| 342 | else $items = $rss->items; // With xmlparse |
||
| 343 | //var_dump($items);exit; |
||
| 344 | } |
||
| 345 | elseif ($rss->_format == 'atom') |
||
| 346 | { |
||
| 347 | //var_dump($rss); |
||
| 348 | if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) |
||
| 349 | { |
||
| 350 | if (!empty($rss->generator)) $this->_generator = (string) $rss->generator; |
||
| 351 | if (!empty($rss->lastbuilddate)) $this->_lastbuilddate = (string) $rss->modified; |
||
| 352 | if (!empty($rss->link->href)) $this->_link = (string) $rss->link->href; |
||
| 353 | if (!empty($rss->title)) $this->_title = (string) $rss->title; |
||
| 354 | if (!empty($rss->description)) $this->_description = (string) $rss->description; |
||
| 355 | } |
||
| 356 | else |
||
| 357 | { |
||
| 358 | //if (!empty($rss->channel['rss_language'])) $this->_language = (string) $rss->channel['rss_language']; |
||
| 359 | if (!empty($rss->channel['generator'])) $this->_generator = (string) $rss->channel['generator']; |
||
| 360 | //if (!empty($rss->channel['rss_copyright'])) $this->_copyright = (string) $rss->channel['rss_copyright']; |
||
| 361 | if (!empty($rss->channel['modified'])) $this->_lastbuilddate = (string) $rss->channel['modified']; |
||
| 362 | //if (!empty($rss->image['rss_url'])) $this->_imageurl = (string) $rss->image['rss_url']; |
||
| 363 | if (!empty($rss->channel['link'])) $this->_link = (string) $rss->channel['link']; |
||
| 364 | if (!empty($rss->channel['title'])) $this->_title = (string) $rss->channel['title']; |
||
| 365 | //if (!empty($rss->channel['rss_description'])) $this->_description = (string) $rss->channel['rss_description']; |
||
| 366 | |||
| 367 | $this->_imageurl = $this->getAtomImageUrl($rss->channel); |
||
| 368 | } |
||
| 369 | if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) { |
||
| 370 | $tmprss = xml2php($rss); $items = $tmprss['entry']; |
||
| 371 | } // With simplexml |
||
| 372 | else $items = $rss->items; // With xmlparse |
||
| 373 | //var_dump($items);exit; |
||
| 374 | } |
||
| 375 | |||
| 376 | $i = 0; |
||
| 377 | |||
| 378 | // Loop on each record |
||
| 379 | if (is_array($items)) |
||
| 380 | { |
||
| 381 | foreach ($items as $item) |
||
| 382 | { |
||
| 383 | //var_dump($item);exit; |
||
| 384 | if ($rss->_format == 'rss') |
||
| 385 | { |
||
| 386 | if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) |
||
| 387 | { |
||
| 388 | $itemLink = (string) $item->link; |
||
| 389 | $itemTitle = (string) $item->title; |
||
| 390 | $itemDescription = (string) $item->description; |
||
| 391 | $itemPubDate = (string) $item->pubDate; |
||
| 392 | $itemId = ''; |
||
| 393 | $itemAuthor = ''; |
||
| 394 | } |
||
| 395 | else |
||
| 396 | { |
||
| 397 | $itemLink = (string) $item['link']; |
||
| 398 | $itemTitle = (string) $item['title']; |
||
| 399 | $itemDescription = (string) $item['description']; |
||
| 400 | $itemPubDate = (string) $item['pubdate']; |
||
| 401 | $itemId = (string) $item['guid']; |
||
| 402 | $itemAuthor = (string) $item['author']; |
||
| 403 | } |
||
| 404 | |||
| 405 | // Loop on each category |
||
| 406 | $itemCategory = array(); |
||
| 407 | if (is_array($item->category)) |
||
| 408 | { |
||
| 409 | foreach ($item->category as $cat) |
||
| 410 | { |
||
| 411 | $itemCategory[] = (string) $cat; |
||
| 412 | } |
||
| 413 | } |
||
| 414 | } |
||
| 415 | elseif ($rss->_format == 'atom') |
||
| 416 | { |
||
| 417 | if (!empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) |
||
| 418 | { |
||
| 419 | $itemLink = (isset($item['link']) ? (string) $item['link'] : ''); |
||
| 420 | $itemTitle = (string) $item['title']; |
||
| 421 | $itemDescription = $this->getAtomItemDescription($item); |
||
| 422 | $itemPubDate = (string) $item['created']; |
||
| 423 | $itemId = (string) $item['id']; |
||
| 424 | $itemAuthor = (string) ($item['author'] ? $item['author'] : $item['author_name']); |
||
| 425 | } |
||
| 426 | else |
||
| 427 | { |
||
| 428 | $itemLink = (isset($item['link']) ? (string) $item['link'] : ''); |
||
| 429 | $itemTitle = (string) $item['title']; |
||
| 430 | $itemDescription = $this->getAtomItemDescription($item); |
||
| 431 | $itemPubDate = (string) $item['created']; |
||
| 432 | $itemId = (string) $item['id']; |
||
| 433 | $itemAuthor = (string) ($item['author'] ? $item['author'] : $item['author_name']); |
||
| 434 | } |
||
| 435 | } |
||
| 436 | else print 'ErrorBadFeedFormat'; |
||
| 437 | |||
| 438 | // Add record to result array |
||
| 439 | $this->_rssarray[$i] = array( |
||
| 440 | 'link'=>$itemLink, |
||
| 441 | 'title'=>$itemTitle, |
||
| 442 | 'description'=>$itemDescription, |
||
| 443 | 'pubDate'=>$itemPubDate, |
||
| 444 | 'category'=>$itemCategory, |
||
| 445 | 'id'=>$itemId, |
||
| 446 | 'author'=>$itemAuthor); |
||
| 447 | //var_dump($this->_rssarray); |
||
| 448 | |||
| 449 | $i++; |
||
| 450 | |||
| 451 | if ($i > $maxNb) break; // We get all records we want |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | return 1; |
||
| 456 | } |
||
| 457 | else |
||
| 458 | { |
||
| 459 | $this->error = 'ErrorFailedToLoadRSSFile'; |
||
| 460 | return -1; |
||
| 461 | } |
||
| 871 |