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