dolibarr  17.0.4
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['dc']['date'])) {
138  $date = $item['dc']['date'];
139  }
140  //$item['dc']['language']
141  //$item['dc']['publisher']
142  }
143  if ($rssparser->getFormat() == 'atom') { // If Atom
144  if (!$date && isset($item['issued'])) {
145  $date = $item['issued'];
146  }
147  if (!$date && isset($item['modified'])) {
148  $date = $item['modified'];
149  }
150  //$item['issued']
151  //$item['modified']
152  //$item['atom_content']
153  }
154  if (is_numeric($date)) {
155  $date = dol_print_date($date, "dayhour", 'tzuserrel');
156  }
157 
158  $isutf8 = utf8_check($title);
159  if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
160  $title = utf8_encode($title);
161  } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
162  $title = utf8_decode($title);
163  }
164 
165  $title = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $title); // Gere probleme des apostrophes mal codee/decodee par utf8
166  $title = preg_replace("/^\s+/", "", $title); // Supprime espaces de debut
167  $this->info_box_contents["$href"] = "$title";
168 
169  $tooltip = $title;
170  $description = !empty($item['description']) ? $item['description'] : '';
171  $isutf8 = utf8_check($description);
172  if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
173  $description = utf8_encode($description);
174  } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
175  $description = utf8_decode($description);
176  }
177  $description = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $description);
178  $description = preg_replace("/^\s+/", "", $description);
179  $description = str_replace("\r\n", "", $description);
180  $tooltip .= '<br>'.$description;
181 
182  $this->info_box_contents[$line][0] = array(
183  'td' => 'class="left" width="16"',
184  'text' => img_picto('', 'rss'),
185  'url' => $href,
186  'tooltip' => $tooltip,
187  'target' => 'newrss',
188  );
189 
190  $this->info_box_contents[$line][1] = array(
191  'td' => 'class="tdoverflowmax300"',
192  'text' => $title,
193  'url' => $href,
194  'tooltip' => $tooltip,
195  'maxlength' => 0,
196  'target' => 'newrss',
197  );
198 
199  $this->info_box_contents[$line][2] = array(
200  'td' => 'class="right nowrap"',
201  'text' => $date,
202  );
203  }
204  }
205 
206 
215  public function showBox($head = null, $contents = null, $nooutput = 0)
216  {
217  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
218  }
219 }
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.
$conf db
API class for accounts.
Definition: inc.php:41