dolibarr  7.0.0-beta
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@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 
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 
42  var $db;
43  var $paramdef; // Params of box definition (not user params)
44 
45  var $info_box_head = array();
46  var $info_box_contents = array();
47 
48 
55  function __construct($db,$param)
56  {
57  $this->db=$db;
58  $this->paramdef=$param;
59  }
60 
68  function loadBox($max=5, $cachedelay=3600)
69  {
70  global $user, $langs, $conf;
71  $langs->load("boxes");
72 
73  $this->max=$max;
74 
75  // On recupere numero de param de la boite
76  preg_match('/^([0-9]+) /',$this->paramdef,$reg);
77  $site=$reg[1];
78 
79  // Create dir nor required
80  // documents/externalrss is created by module activation
81  // documents/externalrss/tmp is created by rssparser
82 
83  $keyforparamurl="EXTERNAL_RSS_URLRSS_".$site;
84  $keyforparamtitle="EXTERNAL_RSS_TITLE_".$site;
85 
86  // Get RSS feed
87  $url=$conf->global->$keyforparamurl;
88 
89  $rssparser=new RssParser($this->db);
90  $result = $rssparser->parser($url, $this->max, $cachedelay, $conf->externalrss->dir_temp);
91 
92  // INFO on channel
93  $description=$rssparser->getDescription();
94  $link=$rssparser->getLink();
95 
96  $title=$langs->trans("BoxTitleLastRssInfos", $max, $conf->global->$keyforparamtitle);
97  if ($result < 0 || ! empty($rssparser->error))
98  {
99  // Show warning
100  $title.=" ".img_error($langs->trans("FailedToRefreshDataInfoNotUpToDate",($rssparser->getLastFetchDate()?dol_print_date($rssparser->getLastFetchDate(),"dayhourtext"):$langs->trans("Unknown"))));
101  $this->info_box_head = array('text' => $title,'limit' => 0);
102  }
103  else
104  {
105  $this->info_box_head = array(
106  'text' => $title,
107  'sublink' => $link,
108  'subtext'=>$langs->trans("LastRefreshDate").': '.($rssparser->getLastFetchDate()?dol_print_date($rssparser->getLastFetchDate(),"dayhourtext"):$langs->trans("Unknown")),
109  'subpicto'=>'help',
110  );
111  }
112 
113  // INFO on items
114  $items=$rssparser->getItems();
115  //print '<pre>'.print_r($items,true).'</pre>';
116  $nbitems=count($items);
117  for($line = 0; $line < $max && $line < $nbitems; $line++)
118  {
119  $item = $items[$line];
120 
121  // Feed common fields
122  $href = $item['link'];
123  $title = urldecode($item['title']);
124  $date = $item['date_timestamp']; // date will be empty if conversion into timestamp failed
125  if ($rssparser->getFormat() == 'rss') // If RSS
126  {
127  if (! $date && isset($item['pubdate'])) $date=$item['pubdate'];
128  if (! $date && isset($item['dc']['date'])) $date=$item['dc']['date'];
129  //$item['dc']['language']
130  //$item['dc']['publisher']
131  }
132  if ($rssparser->getFormat() == 'atom') // If Atom
133  {
134  if (! $date && isset($item['issued'])) $date=$item['issued'];
135  if (! $date && isset($item['modified'])) $date=$item['modified'];
136  //$item['issued']
137  //$item['modified']
138  //$item['atom_content']
139  }
140  if (is_numeric($date)) $date=dol_print_date($date,"dayhour");
141 
142  $isutf8 = utf8_check($title);
143  if (! $isutf8 && $conf->file->character_set_client == 'UTF-8') $title=utf8_encode($title);
144  elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') $title=utf8_decode($title);
145 
146  $title=preg_replace("/([[:alnum:]])\?([[:alnum:]])/","\\1'\\2",$title); // Gere probleme des apostrophes mal codee/decodee par utf8
147  $title=preg_replace("/^\s+/","",$title); // Supprime espaces de debut
148  $this->info_box_contents["$href"]="$title";
149 
150  $tooltip = $title;
151  $description = ! empty($item['description'])?$item['description']:'';
152  $isutf8 = utf8_check($description);
153  if (! $isutf8 && $conf->file->character_set_client == 'UTF-8') $description=utf8_encode($description);
154  elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') $description=utf8_decode($description);
155  $description=preg_replace("/([[:alnum:]])\?([[:alnum:]])/","\\1'\\2",$description);
156  $description=preg_replace("/^\s+/","",$description);
157  $description=str_replace("\r\n","",$description);
158  $tooltip.= '<br>'.$description;
159 
160  $this->info_box_contents[$line][0] = array(
161  'td' => 'align="left" width="16"',
162  'logo' => $this->boximg,
163  'url' => $href,
164  'tooltip' => $tooltip,
165  'target' => 'newrss',
166  );
167 
168  $this->info_box_contents[$line][1] = array(
169  'td' => '',
170  'text' => $title,
171  'url' => $href,
172  'tooltip' => $tooltip,
173  'maxlength' => 64,
174  'target' => 'newrss',
175  );
176 
177  $this->info_box_contents[$line][2] = array(
178  'td' => 'align="right" nowrap="1"',
179  'text' => $date,
180  );
181  }
182  }
183 
184 
193  function showBox($head = null, $contents = null, $nooutput=0)
194  {
195  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
196  }
197 
198 }
199 
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
Class to manage the box to show RSS feeds.
Class ModeleBoxes.
if($_POST["cancel"]==$langs->trans("Cancel")&&!$id) if($action== 'setdatev'&&$user->rights->tax->charges->creer) if($action== 'add'&&$_POST["cancel"]<> $langs->trans("Cancel")) if($action== 'delete') $title
Actions.
Definition: card.php:183
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.