rssparser.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. <?php
  2. /* Copyright (C) 2011-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/class/rssparser.class.php
  19. * \ingroup core
  20. * \brief File of class to parse RSS feeds
  21. */
  22. /**
  23. * Class to parse RSS files
  24. */
  25. class RssParser
  26. {
  27. /**
  28. * @var DoliDB Database handler.
  29. */
  30. public $db;
  31. /**
  32. * @var string Error code (or message)
  33. */
  34. public $error='';
  35. private $_format='';
  36. private $_urlRSS;
  37. private $_language;
  38. private $_generator;
  39. private $_copyright;
  40. private $_lastbuilddate;
  41. private $_imageurl;
  42. private $_link;
  43. private $_title;
  44. private $_description;
  45. private $_lastfetchdate; // Last successful fetch
  46. private $_rssarray=array();
  47. // For parsing with xmlparser
  48. var $stack = array(); // parser stack
  49. var $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright');
  50. /**
  51. * Constructor
  52. *
  53. * @param DoliDB $db Database handler
  54. */
  55. public function __construct($db)
  56. {
  57. $this->db = $db;
  58. }
  59. /**
  60. * getFormat
  61. *
  62. * @return string
  63. */
  64. public function getFormat()
  65. {
  66. return $this->_format;
  67. }
  68. /**
  69. * getUrlRss
  70. *
  71. * @return string
  72. */
  73. public function getUrlRss()
  74. {
  75. return $this->_urlRSS;
  76. }
  77. /**
  78. * getLanguage
  79. *
  80. * @return string
  81. */
  82. public function getLanguage()
  83. {
  84. return $this->_language;
  85. }
  86. /**
  87. * getGenerator
  88. *
  89. * @return string
  90. */
  91. public function getGenerator()
  92. {
  93. return $this->_generator;
  94. }
  95. /**
  96. * getCopyright
  97. *
  98. * @return string
  99. */
  100. public function getCopyright()
  101. {
  102. return $this->_copyright;
  103. }
  104. /**
  105. * getLastBuildDate
  106. *
  107. * @return string
  108. */
  109. public function getLastBuildDate()
  110. {
  111. return $this->_lastbuilddate;
  112. }
  113. /**
  114. * getImageUrl
  115. *
  116. * @return string
  117. */
  118. public function getImageUrl()
  119. {
  120. return $this->_imageurl;
  121. }
  122. /**
  123. * getLink
  124. *
  125. * @return string
  126. */
  127. public function getLink()
  128. {
  129. return $this->_link;
  130. }
  131. /**
  132. * getTitle
  133. *
  134. * @return string
  135. */
  136. public function getTitle()
  137. {
  138. return $this->_title;
  139. }
  140. /**
  141. * getDescription
  142. *
  143. * @return string
  144. */
  145. public function getDescription()
  146. {
  147. return $this->_description;
  148. }
  149. /**
  150. * getLastFetchDate
  151. *
  152. * @return string
  153. */
  154. public function getLastFetchDate()
  155. {
  156. return $this->_lastfetchdate;
  157. }
  158. /**
  159. * getItems
  160. *
  161. * @return string
  162. */
  163. public function getItems()
  164. {
  165. return $this->_rssarray;
  166. }
  167. /**
  168. * Parse rss URL
  169. *
  170. * @param string $urlRSS Url to parse
  171. * @param int $maxNb Max nb of records to get (0 for no limit)
  172. * @param int $cachedelay 0=No cache, nb of seconds we accept cache files (cachedir must also be defined)
  173. * @param string $cachedir Directory where to save cache file
  174. * @return int <0 if KO, >0 if OK
  175. */
  176. public function parser($urlRSS, $maxNb=0, $cachedelay=60, $cachedir='')
  177. {
  178. global $conf;
  179. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  180. $rss='';
  181. $str=''; // This will contain content of feed
  182. // Check parameters
  183. if (! dol_is_url($urlRSS))
  184. {
  185. $this->error="ErrorBadUrl";
  186. return -1;
  187. }
  188. $this->_urlRSS = $urlRSS;
  189. $newpathofdestfile=$cachedir.'/'.dol_hash($this->_urlRSS,3); // Force md5 hash (does not contains special chars)
  190. $newmask='0644';
  191. //dol_syslog("RssPArser::parser parse url=".$urlRSS." => cache file=".$newpathofdestfile);
  192. $nowgmt = dol_now();
  193. // Search into cache
  194. $foundintocache=0;
  195. if ($cachedelay > 0 && $cachedir)
  196. {
  197. $filedate=dol_filemtime($newpathofdestfile);
  198. if ($filedate >= ($nowgmt - $cachedelay))
  199. {
  200. //dol_syslog("RssParser::parser cache file ".$newpathofdestfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay.") so we use it.");
  201. $foundintocache=1;
  202. $this->_lastfetchdate=$filedate;
  203. }
  204. else
  205. {
  206. 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.");
  207. }
  208. }
  209. // Load file into $str
  210. if ($foundintocache) // Cache file found and is not too old
  211. {
  212. $str = file_get_contents($newpathofdestfile);
  213. }
  214. else
  215. {
  216. try {
  217. ini_set("user_agent","Dolibarr ERP-CRM RSS reader");
  218. ini_set("max_execution_time", $conf->global->MAIN_USE_RESPONSE_TIMEOUT);
  219. ini_set("default_socket_timeout", $conf->global->MAIN_USE_RESPONSE_TIMEOUT);
  220. $opts = array('http'=>array('method'=>"GET"));
  221. if (! empty($conf->global->MAIN_USE_CONNECT_TIMEOUT)) $opts['http']['timeout']=$conf->global->MAIN_USE_CONNECT_TIMEOUT;
  222. if (! empty($conf->global->MAIN_PROXY_USE)) $opts['http']['proxy']='tcp://'.$conf->global->MAIN_PROXY_HOST.':'.$conf->global->MAIN_PROXY_PORT;
  223. //var_dump($opts);exit;
  224. $context = stream_context_create($opts);
  225. $str = file_get_contents($this->_urlRSS, false, $context);
  226. }
  227. catch (Exception $e) {
  228. print 'Error retrieving URL '.$this->urlRSS.' - '.$e->getMessage();
  229. }
  230. }
  231. if ($str !== false)
  232. {
  233. // Convert $str into xml
  234. if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML))
  235. {
  236. //print 'xx'.LIBXML_NOCDATA;
  237. libxml_use_internal_errors(false);
  238. $rss = simplexml_load_string($str, "SimpleXMLElement", LIBXML_NOCDATA);
  239. }
  240. else
  241. {
  242. $xmlparser=xml_parser_create('');
  243. if (!is_resource($xmlparser)) {
  244. $this->error="ErrorFailedToCreateParser"; return -1;
  245. }
  246. xml_set_object($xmlparser, $this);
  247. xml_set_element_handler($xmlparser, 'feed_start_element', 'feed_end_element');
  248. xml_set_character_data_handler($xmlparser, 'feed_cdata');
  249. $status = xml_parse($xmlparser, $str);
  250. xml_parser_free($xmlparser);
  251. $rss=$this;
  252. //var_dump($rss->_format);exit;
  253. }
  254. }
  255. // If $rss loaded
  256. if ($rss)
  257. {
  258. // Save file into cache
  259. if (empty($foundintocache) && $cachedir)
  260. {
  261. dol_syslog(get_class($this)."::parser cache file ".$newpathofdestfile." is saved onto disk.");
  262. if (! dol_is_dir($cachedir)) dol_mkdir($cachedir);
  263. $fp = fopen($newpathofdestfile, 'w');
  264. if ($fp)
  265. {
  266. fwrite($fp, $str);
  267. fclose($fp);
  268. if (! empty($conf->global->MAIN_UMASK)) $newmask=$conf->global->MAIN_UMASK;
  269. @chmod($newpathofdestfile, octdec($newmask));
  270. $this->_lastfetchdate=$nowgmt;
  271. }
  272. else
  273. {
  274. print 'Error, failed to open file '.$newpathofdestfile.' for write';
  275. }
  276. }
  277. unset($str); // Free memory
  278. if (empty($rss->_format)) // If format not detected automatically
  279. {
  280. $rss->_format='rss';
  281. if (empty($rss->channel)) $rss->_format='atom';
  282. }
  283. $items=array();
  284. // Save description entries
  285. if ($rss->_format == 'rss')
  286. {
  287. //var_dump($rss);
  288. if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML))
  289. {
  290. if (!empty($rss->channel->language)) $this->_language = (string) $rss->channel->language;
  291. if (!empty($rss->channel->generator)) $this->_generator = (string) $rss->channel->generator;
  292. if (!empty($rss->channel->copyright)) $this->_copyright = (string) $rss->channel->copyright;
  293. if (!empty($rss->channel->lastbuilddate)) $this->_lastbuilddate = (string) $rss->channel->lastbuilddate;
  294. if (!empty($rss->channel->image->url[0])) $this->_imageurl = (string) $rss->channel->image->url[0];
  295. if (!empty($rss->channel->link)) $this->_link = (string) $rss->channel->link;
  296. if (!empty($rss->channel->title)) $this->_title = (string) $rss->channel->title;
  297. if (!empty($rss->channel->description)) $this->_description = (string) $rss->channel->description;
  298. }
  299. else
  300. {
  301. //var_dump($rss->channel);
  302. if (!empty($rss->channel['language'])) $this->_language = (string) $rss->channel['language'];
  303. if (!empty($rss->channel['generator'])) $this->_generator = (string) $rss->channel['generator'];
  304. if (!empty($rss->channel['copyright'])) $this->_copyright = (string) $rss->channel['copyright'];
  305. if (!empty($rss->channel['lastbuilddate'])) $this->_lastbuilddate = (string) $rss->channel['lastbuilddate'];
  306. if (!empty($rss->image['url'])) $this->_imageurl = (string) $rss->image['url'];
  307. if (!empty($rss->channel['link'])) $this->_link = (string) $rss->channel['link'];
  308. if (!empty($rss->channel['title'])) $this->_title = (string) $rss->channel['title'];
  309. if (!empty($rss->channel['description'])) $this->_description = (string) $rss->channel['description'];
  310. }
  311. if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) $items=$rss->channel->item; // With simplexml
  312. else $items=$rss->items; // With xmlparse
  313. //var_dump($items);exit;
  314. }
  315. else if ($rss->_format == 'atom')
  316. {
  317. //var_dump($rss);
  318. if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML))
  319. {
  320. if (!empty($rss->generator)) $this->_generator = (string) $rss->generator;
  321. if (!empty($rss->lastbuilddate)) $this->_lastbuilddate = (string) $rss->modified;
  322. if (!empty($rss->link->href)) $this->_link = (string) $rss->link->href;
  323. if (!empty($rss->title)) $this->_title = (string) $rss->title;
  324. if (!empty($rss->description)) $this->_description = (string) $rss->description;
  325. }
  326. else
  327. {
  328. //if (!empty($rss->channel['rss_language'])) $this->_language = (string) $rss->channel['rss_language'];
  329. if (!empty($rss->channel['generator'])) $this->_generator = (string) $rss->channel['generator'];
  330. //if (!empty($rss->channel['rss_copyright'])) $this->_copyright = (string) $rss->channel['rss_copyright'];
  331. if (!empty($rss->channel['modified'])) $this->_lastbuilddate = (string) $rss->channel['modified'];
  332. //if (!empty($rss->image['rss_url'])) $this->_imageurl = (string) $rss->image['rss_url'];
  333. if (!empty($rss->channel['link'])) $this->_link = (string) $rss->channel['link'];
  334. if (!empty($rss->channel['title'])) $this->_title = (string) $rss->channel['title'];
  335. //if (!empty($rss->channel['rss_description'])) $this->_description = (string) $rss->channel['rss_description'];
  336. }
  337. if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML)) {
  338. $tmprss=xml2php($rss); $items=$tmprss['entry'];
  339. } // With simplexml
  340. else $items=$rss->items; // With xmlparse
  341. //var_dump($items);exit;
  342. }
  343. $i = 0;
  344. // Loop on each record
  345. if (is_array($items))
  346. {
  347. foreach($items as $item)
  348. {
  349. //var_dump($item);exit;
  350. if ($rss->_format == 'rss')
  351. {
  352. if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML))
  353. {
  354. $itemLink = (string) $item->link;
  355. $itemTitle = (string) $item->title;
  356. $itemDescription = (string) $item->description;
  357. $itemPubDate = (string) $item->pubDate;
  358. $itemId = '';
  359. $itemAuthor = '';
  360. }
  361. else
  362. {
  363. $itemLink = (string) $item['link'];
  364. $itemTitle = (string) $item['title'];
  365. $itemDescription = (string) $item['description'];
  366. $itemPubDate = (string) $item['pubdate'];
  367. $itemId = (string) $item['guid'];
  368. $itemAuthor = (string) $item['author'];
  369. }
  370. // Loop on each category
  371. $itemCategory=array();
  372. if (is_array($item->category))
  373. {
  374. foreach ($item->category as $cat)
  375. {
  376. $itemCategory[] = (string) $cat;
  377. }
  378. }
  379. }
  380. else if ($rss->_format == 'atom')
  381. {
  382. if (! empty($conf->global->EXTERNALRSS_USE_SIMPLEXML))
  383. {
  384. $itemLink = (isset($item['link']['href']) ? (string) $item['link']['href'] : '');
  385. $itemTitle = (string) $item['title'];
  386. $itemDescription = (string) $item['summary'];
  387. $itemPubDate = (string) $item['created'];
  388. $itemId = (string) $item['id'];
  389. $itemAuthor = (string) ($item['author']?$item['author']:$item['author_name']);
  390. }
  391. else
  392. {
  393. $itemLink = (isset($item['link']['href']) ? (string) $item['link']['href'] : '');
  394. $itemTitle = (string) $item['title'];
  395. $itemDescription = (string) $item['summary'];
  396. $itemPubDate = (string) $item['created'];
  397. $itemId = (string) $item['id'];
  398. $itemAuthor = (string) ($item['author']?$item['author']:$item['author_name']);
  399. }
  400. }
  401. else print 'ErrorBadFeedFormat';
  402. // Add record to result array
  403. $this->_rssarray[$i] = array(
  404. 'link'=>$itemLink,
  405. 'title'=>$itemTitle,
  406. 'description'=>$itemDescription,
  407. 'pubDate'=>$itemPubDate,
  408. 'category'=>$itemCategory,
  409. 'id'=>$itemId,
  410. 'author'=>$itemAuthor);
  411. //var_dump($this->_rssarray);
  412. $i++;
  413. if ($i > $maxNb) break; // We get all records we want
  414. }
  415. }
  416. return 1;
  417. }
  418. else
  419. {
  420. $this->error='ErrorFailedToLoadRSSFile';
  421. return -1;
  422. }
  423. }
  424. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  425. /**
  426. * Triggered when opened tag is found
  427. *
  428. * @param string $p Start
  429. * @param string $element Tag
  430. * @param array $attrs Attributes of tags
  431. * @return void
  432. */
  433. function feed_start_element($p, $element, &$attrs)
  434. {
  435. // phpcs:enable
  436. $el = $element = strtolower($element);
  437. $attrs = array_change_key_case($attrs, CASE_LOWER);
  438. // check for a namespace, and split if found
  439. $ns = false;
  440. if (strpos($element, ':'))
  441. {
  442. list($ns, $el) = explode(':', $element, 2);
  443. }
  444. if ( $ns and $ns != 'rdf' )
  445. {
  446. $this->current_namespace = $ns;
  447. }
  448. // if feed type isn't set, then this is first element of feed identify feed from root element
  449. if (empty($this->_format))
  450. {
  451. if ( $el == 'rdf' ) {
  452. $this->_format = 'rss';
  453. $this->feed_version = '1.0';
  454. }
  455. elseif ( $el == 'rss' ) {
  456. $this->_format = 'rss';
  457. $this->feed_version = $attrs['version'];
  458. }
  459. elseif ( $el == 'feed' ) {
  460. $this->_format = 'atom';
  461. $this->feed_version = $attrs['version'];
  462. $this->inchannel = true;
  463. }
  464. return;
  465. }
  466. if ( $el == 'channel' )
  467. {
  468. $this->inchannel = true;
  469. }
  470. elseif ($el == 'item' or $el == 'entry' )
  471. {
  472. $this->initem = true;
  473. if ( isset($attrs['rdf:about']) ) {
  474. $this->current_item['about'] = $attrs['rdf:about'];
  475. }
  476. }
  477. // if we're in the default namespace of an RSS feed,
  478. // record textinput or image fields
  479. elseif (
  480. $this->_format == 'rss' and
  481. $this->current_namespace == '' and
  482. $el == 'textinput' )
  483. {
  484. $this->intextinput = true;
  485. }
  486. elseif (
  487. $this->_format == 'rss' and
  488. $this->current_namespace == '' and
  489. $el == 'image' )
  490. {
  491. $this->inimage = true;
  492. }
  493. // handle atom content constructs
  494. elseif ( $this->_format == 'atom' and in_array($el, $this->_CONTENT_CONSTRUCTS) )
  495. {
  496. // avoid clashing w/ RSS mod_content
  497. if ($el == 'content' ) {
  498. $el = 'atom_content';
  499. }
  500. $this->incontent = $el;
  501. }
  502. // if inside an Atom content construct (e.g. content or summary) field treat tags as text
  503. elseif ($this->_format == 'atom' and $this->incontent )
  504. {
  505. // if tags are inlined, then flatten
  506. $attrs_str = join(' ', array_map('map_attrs', array_keys($attrs), array_values($attrs)));
  507. $this->append_content("<$element $attrs_str>");
  508. array_unshift($this->stack, $el);
  509. }
  510. // Atom support many links per containging element.
  511. // Magpie treats link elements of type rel='alternate'
  512. // as being equivalent to RSS's simple link element.
  513. //
  514. elseif ($this->_format == 'atom' and $el == 'link' )
  515. {
  516. if ( isset($attrs['rel']) && $attrs['rel'] == 'alternate' )
  517. {
  518. $link_el = 'link';
  519. }
  520. else {
  521. $link_el = 'link_' . $attrs['rel'];
  522. }
  523. $this->append($link_el, $attrs['href']);
  524. }
  525. // set stack[0] to current element
  526. else {
  527. array_unshift($this->stack, $el);
  528. }
  529. }
  530. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  531. /**
  532. * Triggered when CDATA is found
  533. *
  534. * @param string $p P
  535. * @param string $text Tag
  536. * @return void
  537. */
  538. function feed_cdata($p, $text)
  539. {
  540. // phpcs:enable
  541. if ($this->_format == 'atom' and $this->incontent)
  542. {
  543. $this->append_content($text);
  544. }
  545. else
  546. {
  547. $current_el = join('_', array_reverse($this->stack));
  548. $this->append($current_el, $text);
  549. }
  550. }
  551. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  552. /**
  553. * Triggered when closed tag is found
  554. *
  555. * @param string $p P
  556. * @param string $el Tag
  557. * @return void
  558. */
  559. function feed_end_element($p, $el)
  560. {
  561. // phpcs:enable
  562. $el = strtolower($el);
  563. if ($el == 'item' or $el == 'entry')
  564. {
  565. $this->items[] = $this->current_item;
  566. $this->current_item = array();
  567. $this->initem = false;
  568. }
  569. elseif ($this->_format == 'rss' and $this->current_namespace == '' and $el == 'textinput' )
  570. {
  571. $this->intextinput = false;
  572. }
  573. elseif ($this->_format == 'rss' and $this->current_namespace == '' and $el == 'image' )
  574. {
  575. $this->inimage = false;
  576. }
  577. elseif ($this->_format == 'atom' and in_array($el, $this->_CONTENT_CONSTRUCTS) )
  578. {
  579. $this->incontent = false;
  580. }
  581. elseif ($el == 'channel' or $el == 'feed' )
  582. {
  583. $this->inchannel = false;
  584. }
  585. elseif ($this->_format == 'atom' and $this->incontent ) {
  586. // balance tags properly
  587. // note: i don't think this is actually neccessary
  588. if ( $this->stack[0] == $el )
  589. {
  590. $this->append_content("</$el>");
  591. }
  592. else {
  593. $this->append_content("<$el />");
  594. }
  595. array_shift($this->stack);
  596. }
  597. else {
  598. array_shift($this->stack);
  599. }
  600. $this->current_namespace = false;
  601. }
  602. /**
  603. * To concat 2 string with no warning if an operand is not defined
  604. *
  605. * @param string $str1 Str1
  606. * @param string $str2 Str2
  607. * @return string String cancatenated
  608. */
  609. function concat(&$str1, $str2="")
  610. {
  611. if (!isset($str1) ) {
  612. $str1="";
  613. }
  614. $str1 .= $str2;
  615. }
  616. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  617. /**
  618. * Enter description here ...
  619. *
  620. * @param string $text Text
  621. * @return void
  622. */
  623. function append_content($text)
  624. {
  625. // phpcs:enable
  626. if ( $this->initem ) {
  627. $this->concat($this->current_item[ $this->incontent ], $text);
  628. }
  629. elseif ( $this->inchannel ) {
  630. $this->concat($this->channel[ $this->incontent ], $text);
  631. }
  632. }
  633. /**
  634. * smart append - field and namespace aware
  635. *
  636. * @param string $el El
  637. * @param string $text Text
  638. * @return void
  639. */
  640. function append($el, $text)
  641. {
  642. if (!$el) {
  643. return;
  644. }
  645. if ( $this->current_namespace )
  646. {
  647. if ( $this->initem ) {
  648. $this->concat($this->current_item[ $this->current_namespace ][ $el ], $text);
  649. }
  650. elseif ($this->inchannel) {
  651. $this->concat($this->channel[ $this->current_namespace][ $el ], $text);
  652. }
  653. elseif ($this->intextinput) {
  654. $this->concat($this->textinput[ $this->current_namespace][ $el ], $text);
  655. }
  656. elseif ($this->inimage) {
  657. $this->concat($this->image[ $this->current_namespace ][ $el ], $text);
  658. }
  659. }
  660. else {
  661. if ( $this->initem ) {
  662. $this->concat($this->current_item[ $el ], $text);
  663. }
  664. elseif ($this->intextinput) {
  665. $this->concat($this->textinput[ $el ], $text);
  666. }
  667. elseif ($this->inimage) {
  668. $this->concat($this->image[ $el ], $text);
  669. }
  670. elseif ($this->inchannel) {
  671. $this->concat($this->channel[ $el ], $text);
  672. }
  673. }
  674. }
  675. }
  676. /**
  677. * Function to convert an XML object into an array
  678. *
  679. * @param SimpleXMLElement $xml Xml
  680. * @return void
  681. */
  682. function xml2php($xml)
  683. {
  684. $fils = 0;
  685. $tab = false;
  686. $array = array();
  687. foreach($xml->children() as $key => $value)
  688. {
  689. $child = xml2php($value);
  690. //To deal with the attributes
  691. foreach($value->attributes() as $ak=>$av)
  692. {
  693. $child[$ak] = (string) $av;
  694. }
  695. //Let see if the new child is not in the array
  696. if ($tab==false && in_array($key,array_keys($array)))
  697. {
  698. //If this element is already in the array we will create an indexed array
  699. $tmp = $array[$key];
  700. $array[$key] = null;
  701. $array[$key][] = $tmp;
  702. $array[$key][] = $child;
  703. $tab = true;
  704. }
  705. elseif($tab == true)
  706. {
  707. //Add an element in an existing array
  708. $array[$key][] = $child;
  709. }
  710. else
  711. {
  712. //Add a simple element
  713. $array[$key] = $child;
  714. }
  715. $fils++;
  716. }
  717. if ($fils==0)
  718. {
  719. return (string) $xml;
  720. }
  721. return $array;
  722. }