dolibarr  9.0.0
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 <http://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  var $boxcode="lastrssinfos";
38  var $boximg="object_rss";
39  var $boxlabel="BoxLastRssInfos";
40  var $depends = array("externalrss");
41 
45  public $db;
46 
47  var $paramdef; // Params of box definition (not user params)
48 
49  var $info_box_head = array();
50  var $info_box_contents = array();
51 
52 
59  function __construct($db,$param)
60  {
61  $this->db=$db;
62  $this->paramdef=$param;
63  }
64 
72  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  preg_match('/^([0-9]+) /',$this->paramdef,$reg);
81  $site=$reg[1];
82 
83  // Create dir nor required
84  // documents/externalrss is created by module activation
85  // documents/externalrss/tmp is created by rssparser
86 
87  $keyforparamurl="EXTERNAL_RSS_URLRSS_".$site;
88  $keyforparamtitle="EXTERNAL_RSS_TITLE_".$site;
89 
90  // Get RSS feed
91  $url=$conf->global->$keyforparamurl;
92 
93  $rssparser=new RssParser($this->db);
94  $result = $rssparser->parser($url, $this->max, $cachedelay, $conf->externalrss->dir_temp);
95 
96  // INFO on channel
97  $description=$rssparser->getDescription();
98  $link=$rssparser->getLink();
99 
100  $title=$langs->trans("BoxTitleLastRssInfos", $max, $conf->global->$keyforparamtitle);
101  if ($result < 0 || ! empty($rssparser->error))
102  {
103  // Show warning
104  $title.=" ".img_error($langs->trans("FailedToRefreshDataInfoNotUpToDate",($rssparser->getLastFetchDate()?dol_print_date($rssparser->getLastFetchDate(),"dayhourtext"):$langs->trans("Unknown"))));
105  $this->info_box_head = array('text' => $title,'limit' => 0);
106  }
107  else
108  {
109  $this->info_box_head = array(
110  'text' => $title,
111  'sublink' => $link,
112  'subtext'=>$langs->trans("LastRefreshDate").': '.($rssparser->getLastFetchDate()?dol_print_date($rssparser->getLastFetchDate(),"dayhourtext"):$langs->trans("Unknown")),
113  'subpicto'=>'help',
114  );
115  }
116 
117  // INFO on items
118  $items=$rssparser->getItems();
119  //print '<pre>'.print_r($items,true).'</pre>';
120  $nbitems=count($items);
121  for($line = 0; $line < $max && $line < $nbitems; $line++)
122  {
123  $item = $items[$line];
124 
125  // Feed common fields
126  $href = $item['link'];
127  $title = urldecode($item['title']);
128  $date = $item['date_timestamp']; // date will be empty if conversion into timestamp failed
129  if ($rssparser->getFormat() == 'rss') // If RSS
130  {
131  if (! $date && isset($item['pubdate'])) $date=$item['pubdate'];
132  if (! $date && isset($item['dc']['date'])) $date=$item['dc']['date'];
133  //$item['dc']['language']
134  //$item['dc']['publisher']
135  }
136  if ($rssparser->getFormat() == 'atom') // If Atom
137  {
138  if (! $date && isset($item['issued'])) $date=$item['issued'];
139  if (! $date && isset($item['modified'])) $date=$item['modified'];
140  //$item['issued']
141  //$item['modified']
142  //$item['atom_content']
143  }
144  if (is_numeric($date)) $date=dol_print_date($date,"dayhour");
145 
146  $isutf8 = utf8_check($title);
147  if (! $isutf8 && $conf->file->character_set_client == 'UTF-8') $title=utf8_encode($title);
148  elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') $title=utf8_decode($title);
149 
150  $title=preg_replace("/([[:alnum:]])\?([[:alnum:]])/","\\1'\\2",$title); // Gere probleme des apostrophes mal codee/decodee par utf8
151  $title=preg_replace("/^\s+/","",$title); // Supprime espaces de debut
152  $this->info_box_contents["$href"]="$title";
153 
154  $tooltip = $title;
155  $description = ! empty($item['description'])?$item['description']:'';
156  $isutf8 = utf8_check($description);
157  if (! $isutf8 && $conf->file->character_set_client == 'UTF-8') $description=utf8_encode($description);
158  elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') $description=utf8_decode($description);
159  $description=preg_replace("/([[:alnum:]])\?([[:alnum:]])/","\\1'\\2",$description);
160  $description=preg_replace("/^\s+/","",$description);
161  $description=str_replace("\r\n","",$description);
162  $tooltip.= '<br>'.$description;
163 
164  $this->info_box_contents[$line][0] = array(
165  'td' => 'align="left" width="16"',
166  'logo' => $this->boximg,
167  'url' => $href,
168  'tooltip' => $tooltip,
169  'target' => 'newrss',
170  );
171 
172  $this->info_box_contents[$line][1] = array(
173  'td' => '',
174  'text' => $title,
175  'url' => $href,
176  'tooltip' => $tooltip,
177  'maxlength' => 64,
178  'target' => 'newrss',
179  );
180 
181  $this->info_box_contents[$line][2] = array(
182  'td' => 'align="right" nowrap="1"',
183  'text' => $date,
184  );
185  }
186  }
187 
188 
197  function showBox($head = null, $contents = null, $nooutput=0)
198  {
199  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
200  }
201 }
202 
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
Class to manage the box to show RSS feeds.
Class ModeleBoxes.
utf8_check($str)
Check if a string is in UTF8.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
__construct($db, $param)
Constructor.
loadBox($max=5, $cachedelay=3600)
Load data into info_box_contents array to show array later.
Class to parse RSS files.