dolibarr 21.0.0-beta
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
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 // On recupere numero de param de la boite
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 $title = urldecode($item['title']);
128 $date = empty($item['date_timestamp']) ? null : $item['date_timestamp']; // date will be empty if conversion into timestamp failed
129 if ($rssparser->getFormat() == 'rss') { // If RSS
130 if (!$date && isset($item['pubdate'])) {
131 $date = $item['pubdate'];
132 }
133 if (!$date && isset($item['pubDate'])) {
134 $date = $item['pubDate'];
135 }
136 if (!$date && isset($item['dc']['date'])) {
137 $date = $item['dc']['date'];
138 }
139 //$item['dc']['language']
140 //$item['dc']['publisher']
141 }
142 if ($rssparser->getFormat() == 'atom') { // If Atom
143 if (!$date && isset($item['issued'])) {
144 $date = $item['issued'];
145 }
146 if (!$date && isset($item['modified'])) {
147 $date = $item['modified'];
148 }
149 //$item['issued']
150 //$item['modified']
151 //$item['atom_content']
152 }
153 if (!is_numeric($date)) {
154 $timestamp = strtotime($date);
155 if ($timestamp > 0) {
156 $date = $timestamp;
157 }
158 }
159 if (is_numeric($date)) {
160 $date = dol_print_date($date, "dayhour", 'tzuserrel');
161 }
162
163 $isutf8 = utf8_check($title);
164 if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
165 $title = mb_convert_encoding($title, 'UTF-8', 'ISO-8859-1');
166 } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
167 $title = mb_convert_encoding($title, 'ISO-8859-1');
168 }
169
170 $title = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $title); // Manage issue of quotes improperly (de)coded in utf-8
171 $title = preg_replace("/^\s+/", "", $title); // Remove leading whitespace
172
173 $tooltip = $title;
174 $description = !empty($item['description']) ? $item['description'] : '';
175 $isutf8 = utf8_check($description);
176 if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
177 $description = mb_convert_encoding($description, 'UTF-8', 'ISO-8859-1');
178 } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
179 $description = mb_convert_encoding($description, 'ISO-8859-1');
180 }
181 $description = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $description);
182 $description = preg_replace("/^\s+/", "", $description);
183 $description = str_replace("\r\n", "", $description);
184 $tooltip .= '<br>'.$description;
185
186 $this->info_box_contents[$line][0] = array(
187 'td' => 'class="left" width="16"',
188 'text' => img_picto('', 'rss'),
189 'url' => $href,
190 'tooltip' => dol_escape_htmltag($tooltip),
191 'target' => 'newrss',
192 );
193
194 $this->info_box_contents[$line][1] = array(
195 'td' => 'class="tdoverflowmax300"',
196 'text' => dol_escape_htmltag($title),
197 'url' => $href,
198 'tooltip' => dol_escape_htmltag($tooltip),
199 'maxlength' => 0,
200 'target' => 'newrss',
201 );
202
203 $this->info_box_contents[$line][2] = array(
204 'td' => 'class="right nowraponall"',
205 'text' => dol_escape_htmltag($date),
206 );
207 }
208 }
209
210
211
212
221 public function showBox($head = null, $contents = null, $nooutput = 0)
222 {
223 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
224 }
225}
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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79