dolibarr  19.0.0-dev
box_external_rss.php
Go to the documentation of this file.
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 
28 include_once DOL_DOCUMENT_ROOT.'/core/class/rssparser.class.php';
29 include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
30 
31 
36 {
37  public $boxcode = "lastrssinfos";
38  public $boximg = "object_rss";
39  public $boxlabel = "BoxLastRssInfos";
40  public $depends = array("externalrss");
41 
45  public $db;
46 
47  public $paramdef; // Params of box definition (not user params)
48 
49  public $info_box_head = array();
50  public $info_box_contents = array();
51 
52 
59  public function __construct($db, $param)
60  {
61  $this->db = $db;
62  $this->paramdef = $param;
63  }
64 
72  public function loadBox($max = 5, $cachedelay = 3600)
73  {
74  global $user, $langs, $conf;
75  $langs->load("boxes");
76 
77  $this->max = $max;
78 
79  // On recupere numero de param de la boite
80  $reg = array();
81  preg_match('/^([0-9]+) /', $this->paramdef, $reg);
82  $site = $reg[1];
83 
84  // Create dir nor required
85  // documents/externalrss is created by module activation
86  // documents/externalrss/tmp is created by rssparser
87 
88  $keyforparamurl = "EXTERNAL_RSS_URLRSS_".$site;
89  $keyforparamtitle = "EXTERNAL_RSS_TITLE_".$site;
90 
91  // Get RSS feed
92  $url = $conf->global->$keyforparamurl;
93 
94  $rssparser = new RssParser($this->db);
95  $result = $rssparser->parser($url, $this->max, $cachedelay, $conf->externalrss->dir_temp);
96 
97  // INFO on channel
98  $description = $rssparser->getDescription();
99  $link = $rssparser->getLink();
100 
101  $title = $langs->trans("BoxTitleLastRssInfos", $max, $conf->global->$keyforparamtitle);
102  if ($result < 0 || !empty($rssparser->error)) {
103  // Show warning
104  $errormessage = $langs->trans("FailedToRefreshDataInfoNotUpToDate", ($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")));
105  if ($rssparser->error) {
106  $errormessage .= " - ".$rssparser->error;
107  }
108  $title .= " ".img_error($errormessage);
109  $this->info_box_head = array('text' => $title, 'limit' => 0);
110  } else {
111  $this->info_box_head = array(
112  'text' => $title,
113  'sublink' => $link,
114  'subtext'=>$langs->trans("LastRefreshDate").': '.($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")),
115  'subpicto'=>'globe',
116  'target'=>'_blank',
117  );
118  }
119 
120  // INFO on items
121  $items = $rssparser->getItems();
122  //print '<pre>'.print_r($items,true).'</pre>';
123 
124  // Loop on last items
125  $nbitems = count($items);
126  for ($line = 0; $line < $max && $line < $nbitems; $line++) {
127  $item = $items[$line];
128 
129  // Feed common fields
130  $href = $item['link'];
131  $title = urldecode($item['title']);
132  $date = empty($item['date_timestamp']) ? null : $item['date_timestamp']; // date will be empty if conversion into timestamp failed
133  if ($rssparser->getFormat() == 'rss') { // If RSS
134  if (!$date && isset($item['pubdate'])) {
135  $date = $item['pubdate'];
136  }
137  if (!$date && isset($item['pubDate'])) {
138  $date = $item['pubDate'];
139  }
140  if (!$date && isset($item['dc']['date'])) {
141  $date = $item['dc']['date'];
142  }
143  //$item['dc']['language']
144  //$item['dc']['publisher']
145  }
146  if ($rssparser->getFormat() == 'atom') { // If Atom
147  if (!$date && isset($item['issued'])) {
148  $date = $item['issued'];
149  }
150  if (!$date && isset($item['modified'])) {
151  $date = $item['modified'];
152  }
153  //$item['issued']
154  //$item['modified']
155  //$item['atom_content']
156  }
157  if (is_numeric($date)) {
158  $date = dol_print_date($date, "dayhour", 'tzuserrel');
159  }
160 
161  $isutf8 = utf8_check($title);
162  if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
163  $title = utf8_encode($title);
164  } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
165  $title = utf8_decode($title);
166  }
167 
168  $title = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $title); // Gere probleme des apostrophes mal codee/decodee par utf8
169  $title = preg_replace("/^\s+/", "", $title); // Supprime espaces de debut
170  $this->info_box_contents["$href"] = "$title";
171 
172  $tooltip = $title;
173  $description = !empty($item['description']) ? $item['description'] : '';
174  $isutf8 = utf8_check($description);
175  if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
176  $description = utf8_encode($description);
177  } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
178  $description = utf8_decode($description);
179  }
180  $description = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $description);
181  $description = preg_replace("/^\s+/", "", $description);
182  $description = str_replace("\r\n", "", $description);
183  $tooltip .= '<br>'.$description;
184 
185  $this->info_box_contents[$line][0] = array(
186  'td' => 'class="left" width="16"',
187  'text' => img_picto('', 'rss'),
188  'url' => $href,
189  'tooltip' => dol_escape_htmltag($tooltip),
190  'target' => 'newrss',
191  );
192 
193  $this->info_box_contents[$line][1] = array(
194  'td' => 'class="tdoverflowmax300"',
195  'text' => dol_escape_htmltag($title),
196  'url' => $href,
197  'tooltip' => dol_escape_htmltag($tooltip),
198  'maxlength' => 0,
199  'target' => 'newrss',
200  );
201 
202  $this->info_box_contents[$line][2] = array(
203  'td' => 'class="right nowrap"',
204  'text' => dol_escape_htmltag($date),
205  );
206  }
207  }
208 
209 
218  public function showBox($head = null, $contents = null, $nooutput = 0)
219  {
220  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
221  }
222 }
Class ModeleBoxes.
Class to parse RSS files.
Class to manage the box to show RSS feeds.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
__construct($db, $param)
Constructor.
loadBox($max=5, $cachedelay=3600)
Load data into info_box_contents array to show array later.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
utf8_check($str)
Check if a string is in UTF8.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...