box_external_rss.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2003 Eric Seigne <erics@rycks.com>
  4. * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@capnetworks.com>
  6. * Copyright (C) 2015 Frederic France <frederic.france@free.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/core/boxes/box_external_rss.php
  23. * \ingroup external_rss
  24. * \brief Fichier de gestion d'une box pour le module external_rss
  25. */
  26. include_once DOL_DOCUMENT_ROOT.'/core/class/rssparser.class.php';
  27. include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
  28. /**
  29. * Class to manage the box to show RSS feeds
  30. */
  31. class box_external_rss extends ModeleBoxes
  32. {
  33. var $boxcode="lastrssinfos";
  34. var $boximg="object_rss";
  35. var $boxlabel="BoxLastRssInfos";
  36. var $depends = array("externalrss");
  37. /**
  38. * @var DoliDB Database handler.
  39. */
  40. public $db;
  41. var $paramdef; // Params of box definition (not user params)
  42. var $info_box_head = array();
  43. var $info_box_contents = array();
  44. /**
  45. * Constructor
  46. *
  47. * @param DoliDB $db Database handler
  48. * @param string $param More parameters
  49. */
  50. function __construct($db,$param)
  51. {
  52. $this->db=$db;
  53. $this->paramdef=$param;
  54. }
  55. /**
  56. * Load data into info_box_contents array to show array later.
  57. *
  58. * @param int $max Maximum number of records to load
  59. * @param int $cachedelay Delay we accept for cache file
  60. * @return void
  61. */
  62. function loadBox($max=5, $cachedelay=3600)
  63. {
  64. global $user, $langs, $conf;
  65. $langs->load("boxes");
  66. $this->max=$max;
  67. // On recupere numero de param de la boite
  68. preg_match('/^([0-9]+) /',$this->paramdef,$reg);
  69. $site=$reg[1];
  70. // Create dir nor required
  71. // documents/externalrss is created by module activation
  72. // documents/externalrss/tmp is created by rssparser
  73. $keyforparamurl="EXTERNAL_RSS_URLRSS_".$site;
  74. $keyforparamtitle="EXTERNAL_RSS_TITLE_".$site;
  75. // Get RSS feed
  76. $url=$conf->global->$keyforparamurl;
  77. $rssparser=new RssParser($this->db);
  78. $result = $rssparser->parser($url, $this->max, $cachedelay, $conf->externalrss->dir_temp);
  79. // INFO on channel
  80. $description=$rssparser->getDescription();
  81. $link=$rssparser->getLink();
  82. $title=$langs->trans("BoxTitleLastRssInfos", $max, $conf->global->$keyforparamtitle);
  83. if ($result < 0 || ! empty($rssparser->error))
  84. {
  85. // Show warning
  86. $title.=" ".img_error($langs->trans("FailedToRefreshDataInfoNotUpToDate",($rssparser->getLastFetchDate()?dol_print_date($rssparser->getLastFetchDate(),"dayhourtext"):$langs->trans("Unknown"))));
  87. $this->info_box_head = array('text' => $title,'limit' => 0);
  88. }
  89. else
  90. {
  91. $this->info_box_head = array(
  92. 'text' => $title,
  93. 'sublink' => $link,
  94. 'subtext'=>$langs->trans("LastRefreshDate").': '.($rssparser->getLastFetchDate()?dol_print_date($rssparser->getLastFetchDate(),"dayhourtext"):$langs->trans("Unknown")),
  95. 'subpicto'=>'help',
  96. );
  97. }
  98. // INFO on items
  99. $items=$rssparser->getItems();
  100. //print '<pre>'.print_r($items,true).'</pre>';
  101. $nbitems=count($items);
  102. for($line = 0; $line < $max && $line < $nbitems; $line++)
  103. {
  104. $item = $items[$line];
  105. // Feed common fields
  106. $href = $item['link'];
  107. $title = urldecode($item['title']);
  108. $date = $item['date_timestamp']; // date will be empty if conversion into timestamp failed
  109. if ($rssparser->getFormat() == 'rss') // If RSS
  110. {
  111. if (! $date && isset($item['pubdate'])) $date=$item['pubdate'];
  112. if (! $date && isset($item['dc']['date'])) $date=$item['dc']['date'];
  113. //$item['dc']['language']
  114. //$item['dc']['publisher']
  115. }
  116. if ($rssparser->getFormat() == 'atom') // If Atom
  117. {
  118. if (! $date && isset($item['issued'])) $date=$item['issued'];
  119. if (! $date && isset($item['modified'])) $date=$item['modified'];
  120. //$item['issued']
  121. //$item['modified']
  122. //$item['atom_content']
  123. }
  124. if (is_numeric($date)) $date=dol_print_date($date,"dayhour");
  125. $isutf8 = utf8_check($title);
  126. if (! $isutf8 && $conf->file->character_set_client == 'UTF-8') $title=utf8_encode($title);
  127. elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') $title=utf8_decode($title);
  128. $title=preg_replace("/([[:alnum:]])\?([[:alnum:]])/","\\1'\\2",$title); // Gere probleme des apostrophes mal codee/decodee par utf8
  129. $title=preg_replace("/^\s+/","",$title); // Supprime espaces de debut
  130. $this->info_box_contents["$href"]="$title";
  131. $tooltip = $title;
  132. $description = ! empty($item['description'])?$item['description']:'';
  133. $isutf8 = utf8_check($description);
  134. if (! $isutf8 && $conf->file->character_set_client == 'UTF-8') $description=utf8_encode($description);
  135. elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') $description=utf8_decode($description);
  136. $description=preg_replace("/([[:alnum:]])\?([[:alnum:]])/","\\1'\\2",$description);
  137. $description=preg_replace("/^\s+/","",$description);
  138. $description=str_replace("\r\n","",$description);
  139. $tooltip.= '<br>'.$description;
  140. $this->info_box_contents[$line][0] = array(
  141. 'td' => 'align="left" width="16"',
  142. 'logo' => $this->boximg,
  143. 'url' => $href,
  144. 'tooltip' => $tooltip,
  145. 'target' => 'newrss',
  146. );
  147. $this->info_box_contents[$line][1] = array(
  148. 'td' => '',
  149. 'text' => $title,
  150. 'url' => $href,
  151. 'tooltip' => $tooltip,
  152. 'maxlength' => 64,
  153. 'target' => 'newrss',
  154. );
  155. $this->info_box_contents[$line][2] = array(
  156. 'td' => 'align="right" nowrap="1"',
  157. 'text' => $date,
  158. );
  159. }
  160. }
  161. /**
  162. * Method to show box
  163. *
  164. * @param array $head Array with properties of box title
  165. * @param array $contents Array with properties of box lines
  166. * @param int $nooutput No print, only return string
  167. * @return string
  168. */
  169. function showBox($head = null, $contents = null, $nooutput=0)
  170. {
  171. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  172. }
  173. }