box_external_rss.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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@inodbox.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 <https://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. public $boxcode = "lastrssinfos";
  34. public $boximg = "object_rss";
  35. public $boxlabel = "BoxLastRssInfos";
  36. public $depends = array("externalrss");
  37. /**
  38. * @var DoliDB Database handler.
  39. */
  40. public $db;
  41. public $paramdef; // Params of box definition (not user params)
  42. public $info_box_head = array();
  43. public $info_box_contents = array();
  44. /**
  45. * Constructor
  46. *
  47. * @param DoliDB $db Database handler
  48. * @param string $param More parameters
  49. */
  50. public 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. public 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. $reg = array();
  69. preg_match('/^([0-9]+) /', $this->paramdef, $reg);
  70. $site = $reg[1];
  71. // Create dir nor required
  72. // documents/externalrss is created by module activation
  73. // documents/externalrss/tmp is created by rssparser
  74. $keyforparamurl = "EXTERNAL_RSS_URLRSS_".$site;
  75. $keyforparamtitle = "EXTERNAL_RSS_TITLE_".$site;
  76. // Get RSS feed
  77. $url = $conf->global->$keyforparamurl;
  78. $rssparser = new RssParser($this->db);
  79. $result = $rssparser->parser($url, $this->max, $cachedelay, $conf->externalrss->dir_temp);
  80. // INFO on channel
  81. $description = $rssparser->getDescription();
  82. $link = $rssparser->getLink();
  83. $title = $langs->trans("BoxTitleLastRssInfos", $max, $conf->global->$keyforparamtitle);
  84. if ($result < 0 || !empty($rssparser->error)) {
  85. // Show warning
  86. $errormessage = $langs->trans("FailedToRefreshDataInfoNotUpToDate", ($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")));
  87. if ($rssparser->error) {
  88. $errormessage .= " - ".$rssparser->error;
  89. }
  90. $title .= " ".img_error($errormessage);
  91. $this->info_box_head = array('text' => $title, 'limit' => 0);
  92. } else {
  93. $this->info_box_head = array(
  94. 'text' => $title,
  95. 'sublink' => $link,
  96. 'subtext'=>$langs->trans("LastRefreshDate").': '.($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")),
  97. 'subpicto'=>'globe',
  98. 'target'=>'_blank',
  99. );
  100. }
  101. // INFO on items
  102. $items = $rssparser->getItems();
  103. //print '<pre>'.print_r($items,true).'</pre>';
  104. $nbitems = count($items);
  105. for ($line = 0; $line < $max && $line < $nbitems; $line++) {
  106. $item = $items[$line];
  107. // Feed common fields
  108. $href = $item['link'];
  109. $title = urldecode($item['title']);
  110. $date = $item['date_timestamp']; // date will be empty if conversion into timestamp failed
  111. if ($rssparser->getFormat() == 'rss') { // If RSS
  112. if (!$date && isset($item['pubdate'])) {
  113. $date = $item['pubdate'];
  114. }
  115. if (!$date && isset($item['dc']['date'])) {
  116. $date = $item['dc']['date'];
  117. }
  118. //$item['dc']['language']
  119. //$item['dc']['publisher']
  120. }
  121. if ($rssparser->getFormat() == 'atom') { // If Atom
  122. if (!$date && isset($item['issued'])) {
  123. $date = $item['issued'];
  124. }
  125. if (!$date && isset($item['modified'])) {
  126. $date = $item['modified'];
  127. }
  128. //$item['issued']
  129. //$item['modified']
  130. //$item['atom_content']
  131. }
  132. if (is_numeric($date)) {
  133. $date = dol_print_date($date, "dayhour");
  134. }
  135. $isutf8 = utf8_check($title);
  136. if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
  137. $title = utf8_encode($title);
  138. } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
  139. $title = utf8_decode($title);
  140. }
  141. $title = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $title); // Gere probleme des apostrophes mal codee/decodee par utf8
  142. $title = preg_replace("/^\s+/", "", $title); // Supprime espaces de debut
  143. $this->info_box_contents["$href"] = "$title";
  144. $tooltip = $title;
  145. $description = !empty($item['description']) ? $item['description'] : '';
  146. $isutf8 = utf8_check($description);
  147. if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
  148. $description = utf8_encode($description);
  149. } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
  150. $description = utf8_decode($description);
  151. }
  152. $description = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $description);
  153. $description = preg_replace("/^\s+/", "", $description);
  154. $description = str_replace("\r\n", "", $description);
  155. $tooltip .= '<br>'.$description;
  156. $this->info_box_contents[$line][0] = array(
  157. 'td' => 'class="left" width="16"',
  158. 'logo' => $this->boximg,
  159. 'url' => $href,
  160. 'tooltip' => $tooltip,
  161. 'target' => 'newrss',
  162. );
  163. $this->info_box_contents[$line][1] = array(
  164. 'td' => '',
  165. 'text' => $title,
  166. 'url' => $href,
  167. 'tooltip' => $tooltip,
  168. 'maxlength' => 64,
  169. 'target' => 'newrss',
  170. );
  171. $this->info_box_contents[$line][2] = array(
  172. 'td' => 'class="right nowrap"',
  173. 'text' => $date,
  174. );
  175. }
  176. }
  177. /**
  178. * Method to show box
  179. *
  180. * @param array $head Array with properties of box title
  181. * @param array $contents Array with properties of box lines
  182. * @param int $nooutput No print, only return string
  183. * @return string
  184. */
  185. public function showBox($head = null, $contents = null, $nooutput = 0)
  186. {
  187. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  188. }
  189. }