dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29include_once DOL_DOCUMENT_ROOT.'/core/class/rssparser.class.php';
30include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
31
32
37{
38 public $boxcode = "lastrssinfos";
39 public $boximg = "object_rss";
40 public $boxlabel = "BoxLastRssInfos";
41 public $depends = array("externalrss");
42
43 public $paramdef; // Params of box definition (not user params)
44
51 public function __construct($db, $param)
52 {
53 $this->db = $db;
54 $this->paramdef = $param;
55 }
56
64 public function loadBox($max = 5, $cachedelay = 3600)
65 {
66 global $user, $langs, $conf;
67 $langs->load("boxes");
68
69 $this->max = $max;
70
71 // On recupere numero de param de la boite
72 $reg = array();
73 preg_match('/^([0-9]+) /', $this->paramdef, $reg);
74 $site = $reg[1];
75
76 // Create dir nor required
77 // documents/externalrss is created by module activation
78 // documents/externalrss/tmp is created by rssparser
79
80 $keyforparamurl = "EXTERNAL_RSS_URLRSS_".$site;
81 $keyforparamtitle = "EXTERNAL_RSS_TITLE_".$site;
82
83 // Get RSS feed
84 $url = getDolGlobalString($keyforparamurl);
85
86 $rssparser = new RssParser($this->db);
87 $result = $rssparser->parser($url, $this->max, $cachedelay, $conf->externalrss->dir_temp);
88
89 // INFO on channel
90 $description = $rssparser->getDescription();
91 $link = $rssparser->getLink();
92
93 $title = $langs->trans("BoxTitleLastRssInfos", $max, getDolGlobalString($keyforparamtitle));
94 if ($result < 0 || !empty($rssparser->error)) {
95 // Show warning
96 $errormessage = $langs->trans("FailedToRefreshDataInfoNotUpToDate", ($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")));
97 if ($rssparser->error) {
98 $errormessage .= " - ".$rssparser->error;
99 }
100 $title .= " ".img_error($errormessage);
101 $this->info_box_head = array('text' => $title, 'limit' => 0);
102 } else {
103 $this->info_box_head = array(
104 'text' => $title,
105 'sublink' => $link,
106 'subtext' => $langs->trans("LastRefreshDate").': '.($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")),
107 'subpicto' => 'globe',
108 'target' => '_blank',
109 );
110 }
111
112 // INFO on items
113 $items = $rssparser->getItems();
114 //print '<pre>'.print_r($items,true).'</pre>';
115
116 // Loop on last items
117 $nbitems = count($items);
118 for ($line = 0; $line < $max && $line < $nbitems; $line++) {
119 $item = $items[$line];
120
121 // Feed common fields
122 $href = $item['link'];
123 $title = urldecode($item['title']);
124 $date = empty($item['date_timestamp']) ? null : $item['date_timestamp']; // date will be empty if conversion into timestamp failed
125 if ($rssparser->getFormat() == 'rss') { // If RSS
126 if (!$date && isset($item['pubdate'])) {
127 $date = $item['pubdate'];
128 }
129 if (!$date && isset($item['pubDate'])) {
130 $date = $item['pubDate'];
131 }
132 if (!$date && isset($item['dc']['date'])) {
133 $date = $item['dc']['date'];
134 }
135 //$item['dc']['language']
136 //$item['dc']['publisher']
137 }
138 if ($rssparser->getFormat() == 'atom') { // If Atom
139 if (!$date && isset($item['issued'])) {
140 $date = $item['issued'];
141 }
142 if (!$date && isset($item['modified'])) {
143 $date = $item['modified'];
144 }
145 //$item['issued']
146 //$item['modified']
147 //$item['atom_content']
148 }
149 if (!is_numeric($date)) {
150 $timestamp = strtotime($date);
151 if ($timestamp > 0) {
152 $date = $timestamp;
153 }
154 }
155 if (is_numeric($date)) {
156 $date = dol_print_date($date, "dayhour", 'tzuserrel');
157 }
158
159 $isutf8 = utf8_check($title);
160 if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
161 $title = mb_convert_encoding($title, 'UTF-8', 'ISO-8859-1');
162 } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
163 $title = mb_convert_encoding($title, 'ISO-8859-1');
164 }
165
166 $title = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $title); // Manage issue of quotes improperly (de)coded in utf-8
167 $title = preg_replace("/^\s+/", "", $title); // Remove leading whitespace
168
169 $tooltip = $title;
170 $description = !empty($item['description']) ? $item['description'] : '';
171 $isutf8 = utf8_check($description);
172 if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
173 $description = mb_convert_encoding($description, 'UTF-8', 'ISO-8859-1');
174 } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
175 $description = mb_convert_encoding($description, 'ISO-8859-1');
176 }
177 $description = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $description);
178 $description = preg_replace("/^\s+/", "", $description);
179 $description = str_replace("\r\n", "", $description);
180 $tooltip .= '<br>'.$description;
181
182 $this->info_box_contents[$line][0] = array(
183 'td' => 'class="left" width="16"',
184 'text' => img_picto('', 'rss'),
185 'url' => $href,
186 'tooltip' => dol_escape_htmltag($tooltip),
187 'target' => 'newrss',
188 );
189
190 $this->info_box_contents[$line][1] = array(
191 'td' => 'class="tdoverflowmax300"',
192 'text' => dol_escape_htmltag($title),
193 'url' => $href,
194 'tooltip' => dol_escape_htmltag($tooltip),
195 'maxlength' => 0,
196 'target' => 'newrss',
197 );
198
199 $this->info_box_contents[$line][2] = array(
200 'td' => 'class="right nowraponall"',
201 'text' => dol_escape_htmltag($date),
202 );
203 }
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.
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 a 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...