dolibarr  13.0.2
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  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  $errormessage = $langs->trans("FailedToRefreshDataInfoNotUpToDate", ($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")));
105  if ($rssparser->error) $errormessage .= " - ".$rssparser->error;
106  $title .= " ".img_error($errormessage);
107  $this->info_box_head = array('text' => $title, 'limit' => 0);
108  } else {
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'=>'globe',
114  'target'=>'_blank',
115  );
116  }
117 
118  // INFO on items
119  $items = $rssparser->getItems();
120  //print '<pre>'.print_r($items,true).'</pre>';
121  $nbitems = count($items);
122  for ($line = 0; $line < $max && $line < $nbitems; $line++)
123  {
124  $item = $items[$line];
125 
126  // Feed common fields
127  $href = $item['link'];
128  $title = urldecode($item['title']);
129  $date = $item['date_timestamp']; // date will be empty if conversion into timestamp failed
130  if ($rssparser->getFormat() == 'rss') // If RSS
131  {
132  if (!$date && isset($item['pubdate'])) $date = $item['pubdate'];
133  if (!$date && isset($item['dc']['date'])) $date = $item['dc']['date'];
134  //$item['dc']['language']
135  //$item['dc']['publisher']
136  }
137  if ($rssparser->getFormat() == 'atom') // If Atom
138  {
139  if (!$date && isset($item['issued'])) $date = $item['issued'];
140  if (!$date && isset($item['modified'])) $date = $item['modified'];
141  //$item['issued']
142  //$item['modified']
143  //$item['atom_content']
144  }
145  if (is_numeric($date)) $date = dol_print_date($date, "dayhour");
146 
147  $isutf8 = utf8_check($title);
148  if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') $title = utf8_encode($title);
149  elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') $title = utf8_decode($title);
150 
151  $title = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $title); // Gere probleme des apostrophes mal codee/decodee par utf8
152  $title = preg_replace("/^\s+/", "", $title); // Supprime espaces de debut
153  $this->info_box_contents["$href"] = "$title";
154 
155  $tooltip = $title;
156  $description = !empty($item['description']) ? $item['description'] : '';
157  $isutf8 = utf8_check($description);
158  if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') $description = utf8_encode($description);
159  elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') $description = utf8_decode($description);
160  $description = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $description);
161  $description = preg_replace("/^\s+/", "", $description);
162  $description = str_replace("\r\n", "", $description);
163  $tooltip .= '<br>'.$description;
164 
165  $this->info_box_contents[$line][0] = array(
166  'td' => 'class="left" width="16"',
167  'logo' => $this->boximg,
168  'url' => $href,
169  'tooltip' => $tooltip,
170  'target' => 'newrss',
171  );
172 
173  $this->info_box_contents[$line][1] = array(
174  'td' => '',
175  'text' => $title,
176  'url' => $href,
177  'tooltip' => $tooltip,
178  'maxlength' => 64,
179  'target' => 'newrss',
180  );
181 
182  $this->info_box_contents[$line][2] = array(
183  'td' => 'class="right nowrap"',
184  'text' => $date,
185  );
186  }
187  }
188 
189 
198  public function showBox($head = null, $contents = null, $nooutput = 0)
199  {
200  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
201  }
202 }
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
Class to manage the box to show RSS feeds.
Class ModeleBoxes.
$conf db
API class for accounts.
Definition: inc.php:54
utf8_check($str)
Check if a string is in UTF8.
dol_print_date($time, $format= '', $tzoutput= 'auto', $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.