dolibarr 25.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-2025 Frédéric 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
46 public $paramdef; // Params of box definition (not user params)
47
54 public function __construct($db, $param)
55 {
56 $this->db = $db;
57 $this->paramdef = $param;
58 $this->urltoaddentry = DOL_URL_ROOT.'/admin/external_rss.php';
59 }
60
68 public function loadBox($max = 5, $cachedelay = 3600)
69 {
70 global $user, $langs, $conf;
71 $langs->load("boxes");
72
73 $this->max = $max;
74
75 // Retrieve box param number
76 $reg = array();
77 preg_match('/^([0-9]+) /', $this->paramdef, $reg);
78 $site = $reg[1];
79
80 // Create dir nor required
81 // documents/externalrss is created by module activation
82 // documents/externalrss/tmp is created by rssparser
83
84 $keyforparamurl = "EXTERNAL_RSS_URLRSS_".$site;
85 $keyforparamtitle = "EXTERNAL_RSS_TITLE_".$site;
86
87 // Get RSS feed
88 $url = getDolGlobalString($keyforparamurl);
89
90 $rssparser = new RssParser($this->db);
91 $result = $rssparser->parser($url, $this->max, $cachedelay, $conf->externalrss->dir_temp);
92
93 // INFO on channel
94 $description = $rssparser->getDescription();
95 $link = $rssparser->getLink();
96
97 $title = $langs->trans("BoxTitleLastRssInfos", $max, getDolGlobalString($keyforparamtitle));
98 if ($result < 0 || !empty($rssparser->error)) {
99 // Show warning
100 $errormessage = $langs->trans("FailedToRefreshDataInfoNotUpToDate", ($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")));
101 if ($rssparser->error) {
102 $errormessage .= " - ".$rssparser->error;
103 }
104 $title .= " ".img_error($errormessage);
105 $this->info_box_head = array('text' => $title, 'limit' => 0);
106 } else {
107 $this->info_box_head = array(
108 'text' => $title,
109 'sublink' => $link,
110 'subtext' => $langs->trans("LastRefreshDate").': '.($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")),
111 'subpicto' => 'globe',
112 'target' => '_blank',
113 );
114 }
115
116 // INFO on items
117 $items = $rssparser->getItems();
118 //print '<pre>'.print_r($items,true).'</pre>';
119
120 // Loop on last items
121 $nbitems = count($items);
122 for ($line = 0; $line < $max && $line < $nbitems; $line++) {
123 $item = $items[$line];
124
125 // Feed common fields
126 $href = $item['link'];
127 // urldecode() can give back HTML tags (%3C...%3E) that were removed by the parser, and the text of the box is
128 // shown with no escaping when it starts with <img, <div or <span
129 $title = dol_string_nohtmltag(urldecode($item['title']), 0);
130 $date = empty($item['date_timestamp']) ? null : $item['date_timestamp']; // date will be empty if conversion into timestamp failed
131 if ($rssparser->getFormat() == 'rss') { // If RSS
132 if (!$date && isset($item['pubdate'])) {
133 $date = $item['pubdate'];
134 }
135 if (!$date && isset($item['pubDate'])) {
136 $date = $item['pubDate'];
137 }
138 if (!$date && isset($item['dc']['date'])) {
139 $date = $item['dc']['date'];
140 }
141 //$item['dc']['language']
142 //$item['dc']['publisher']
143 }
144 if ($rssparser->getFormat() == 'atom') { // If Atom
145 if (!$date && !empty($item['pubDate'])) {
146 $date = $item['pubDate'];
147 }
148 if (!$date && isset($item['issued'])) {
149 $date = $item['issued'];
150 }
151 if (!$date && isset($item['modified'])) {
152 $date = $item['modified'];
153 }
154 //$item['issued']
155 //$item['modified']
156 //$item['atom_content']
157 }
158 if (!is_numeric($date)) {
159 $timestamp = strtotime($date);
160 if ($timestamp > 0) {
161 $date = $timestamp;
162 }
163 }
164 if (is_numeric($date)) {
165 $date = dol_print_date($date, "dayhour", 'tzuserrel');
166 }
167
168 $isutf8 = utf8_check($title);
169 if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
170 $title = mb_convert_encoding($title, 'UTF-8', 'ISO-8859-1');
171 } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
172 $title = mb_convert_encoding($title, 'ISO-8859-1');
173 }
174
175 $title = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $title); // Manage issue of quotes improperly (de)coded in utf-8
176 $title = preg_replace("/^\s+/", "", $title); // Remove leading whitespace
177
178 $tooltip = $title;
179 $description = !empty($item['description']) ? $item['description'] : '';
180 $isutf8 = utf8_check($description);
181 if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
182 $description = mb_convert_encoding($description, 'UTF-8', 'ISO-8859-1');
183 } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
184 $description = mb_convert_encoding($description, 'ISO-8859-1');
185 }
186 $description = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $description);
187 $description = preg_replace("/^\s+/", "", $description);
188 $description = str_replace("\r\n", "", $description);
189 $tooltip .= '<br>'.$description;
190
191 // Note: Escaping of value will be done by the showBox rendering method.
192 $this->info_box_contents[$line][0] = array(
193 'td' => 'class="left" width="16"',
194 'text' => img_picto('', 'rss'),
195 'url' => $href,
196 'tooltip' => $tooltip,
197 'target' => 'newrss',
198 );
199
200 $this->info_box_contents[$line][1] = array(
201 'td' => 'class="tdoverflowmax300"',
202 'text' => $title,
203 'url' => $href,
204 'tooltip' => $tooltip,
205 'maxlength' => 0,
206 'target' => 'newrss',
207 );
208
209 $this->info_box_contents[$line][2] = array(
210 'td' => 'class="right nowraponall"',
211 'text' => $date,
212 );
213 }
214 }
215
216
217
218
227 public function showBox($head = null, $contents = null, $nooutput = 0)
228 {
229 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
230 }
231}
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
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.