dolibarr 20.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 <https://www.gnu.org/licenses/>.
20 */
21
28include_once DOL_DOCUMENT_ROOT.'/core/class/rssparser.class.php';
29include_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
42 public $paramdef; // Params of box definition (not user params)
43
50 public function __construct($db, $param)
51 {
52 $this->db = $db;
53 $this->paramdef = $param;
54 }
55
63 public function loadBox($max = 5, $cachedelay = 3600)
64 {
65 global $user, $langs, $conf;
66 $langs->load("boxes");
67
68 $this->max = $max;
69
70 // On recupere numero de param de la boite
71 $reg = array();
72 preg_match('/^([0-9]+) /', $this->paramdef, $reg);
73 $site = $reg[1];
74
75 // Create dir nor required
76 // documents/externalrss is created by module activation
77 // documents/externalrss/tmp is created by rssparser
78
79 $keyforparamurl = "EXTERNAL_RSS_URLRSS_".$site;
80 $keyforparamtitle = "EXTERNAL_RSS_TITLE_".$site;
81
82 // Get RSS feed
83 $url = getDolGlobalString($keyforparamurl);
84
85 $rssparser = new RssParser($this->db);
86 $result = $rssparser->parser($url, $this->max, $cachedelay, $conf->externalrss->dir_temp);
87
88 // INFO on channel
89 $description = $rssparser->getDescription();
90 $link = $rssparser->getLink();
91
92 $title = $langs->trans("BoxTitleLastRssInfos", $max, getDolGlobalString($keyforparamtitle));
93 if ($result < 0 || !empty($rssparser->error)) {
94 // Show warning
95 $errormessage = $langs->trans("FailedToRefreshDataInfoNotUpToDate", ($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")));
96 if ($rssparser->error) {
97 $errormessage .= " - ".$rssparser->error;
98 }
99 $title .= " ".img_error($errormessage);
100 $this->info_box_head = array('text' => $title, 'limit' => 0);
101 } else {
102 $this->info_box_head = array(
103 'text' => $title,
104 'sublink' => $link,
105 'subtext'=>$langs->trans("LastRefreshDate").': '.($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")),
106 'subpicto'=>'globe',
107 'target'=>'_blank',
108 );
109 }
110
111 // INFO on items
112 $items = $rssparser->getItems();
113 //print '<pre>'.print_r($items,true).'</pre>';
114
115 // Loop on last items
116 $nbitems = count($items);
117 for ($line = 0; $line < $max && $line < $nbitems; $line++) {
118 $item = $items[$line];
119
120 // Feed common fields
121 $href = $item['link'];
122 $title = urldecode($item['title']);
123 $date = empty($item['date_timestamp']) ? null : $item['date_timestamp']; // date will be empty if conversion into timestamp failed
124 if ($rssparser->getFormat() == 'rss') { // If RSS
125 if (!$date && isset($item['pubdate'])) {
126 $date = $item['pubdate'];
127 }
128 if (!$date && isset($item['pubDate'])) {
129 $date = $item['pubDate'];
130 }
131 if (!$date && isset($item['dc']['date'])) {
132 $date = $item['dc']['date'];
133 }
134 //$item['dc']['language']
135 //$item['dc']['publisher']
136 }
137 if ($rssparser->getFormat() == 'atom') { // If Atom
138 if (!$date && isset($item['issued'])) {
139 $date = $item['issued'];
140 }
141 if (!$date && isset($item['modified'])) {
142 $date = $item['modified'];
143 }
144 //$item['issued']
145 //$item['modified']
146 //$item['atom_content']
147 }
148 if (!is_numeric($date)) {
149 $timestamp = strtotime($date);
150 if ($timestamp > 0) {
151 $date = $timestamp;
152 }
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 = mb_convert_encoding($title, 'UTF-8', 'ISO-8859-1');
161 } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
162 $title = mb_convert_encoding($title, 'ISO-8859-1');
163 }
164
165 $title = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $title); // Manage issue of quotes improperly (de)coded in utf-8
166 $title = preg_replace("/^\s+/", "", $title); // Remove leading whitespace
167
168 $tooltip = $title;
169 $description = !empty($item['description']) ? $item['description'] : '';
170 $isutf8 = utf8_check($description);
171 if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
172 $description = mb_convert_encoding($description, 'UTF-8', 'ISO-8859-1');
173 } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
174 $description = mb_convert_encoding($description, 'ISO-8859-1');
175 }
176 $description = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $description);
177 $description = preg_replace("/^\s+/", "", $description);
178 $description = str_replace("\r\n", "", $description);
179 $tooltip .= '<br>'.$description;
180
181 $this->info_box_contents[$line][0] = array(
182 'td' => 'class="left" width="16"',
183 'text' => img_picto('', 'rss'),
184 'url' => $href,
185 'tooltip' => dol_escape_htmltag($tooltip),
186 'target' => 'newrss',
187 );
188
189 $this->info_box_contents[$line][1] = array(
190 'td' => 'class="tdoverflowmax300"',
191 'text' => dol_escape_htmltag($title),
192 'url' => $href,
193 'tooltip' => dol_escape_htmltag($tooltip),
194 'maxlength' => 0,
195 'target' => 'newrss',
196 );
197
198 $this->info_box_contents[$line][2] = array(
199 'td' => 'class="right nowraponall"',
200 'text' => dol_escape_htmltag($date),
201 );
202 }
203 }
204
205
214 public function showBox($head = null, $contents = null, $nooutput = 0)
215 {
216 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
217 }
218}
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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
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...