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 $this->urltoaddentry = DOL_URL_ROOT.'/admin/external_rss.php';
56 }
57
65 public function loadBox($max = 5, $cachedelay = 3600)
66 {
67 global $user, $langs, $conf;
68 $langs->load("boxes");
69
70 $this->max = $max;
71
72 // On recupere numero de param de la boite
73 $reg = array();
74 preg_match('/^([0-9]+) /', $this->paramdef, $reg);
75 $site = $reg[1];
76
77 // Create dir nor required
78 // documents/externalrss is created by module activation
79 // documents/externalrss/tmp is created by rssparser
80
81 $keyforparamurl = "EXTERNAL_RSS_URLRSS_".$site;
82 $keyforparamtitle = "EXTERNAL_RSS_TITLE_".$site;
83
84 // Get RSS feed
85 $url = getDolGlobalString($keyforparamurl);
86
87 $rssparser = new RssParser($this->db);
88 $result = $rssparser->parser($url, $this->max, $cachedelay, $conf->externalrss->dir_temp);
89
90 // INFO on channel
91 $description = $rssparser->getDescription();
92 $link = $rssparser->getLink();
93
94 $title = $langs->trans("BoxTitleLastRssInfos", $max, getDolGlobalString($keyforparamtitle));
95 if ($result < 0 || !empty($rssparser->error)) {
96 // Show warning
97 $errormessage = $langs->trans("FailedToRefreshDataInfoNotUpToDate", ($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")));
98 if ($rssparser->error) {
99 $errormessage .= " - ".$rssparser->error;
100 }
101 $title .= " ".img_error($errormessage);
102 $this->info_box_head = array('text' => $title, 'limit' => 0);
103 } else {
104 $this->info_box_head = array(
105 'text' => $title,
106 'sublink' => $link,
107 'subtext' => $langs->trans("LastRefreshDate").': '.($rssparser->getLastFetchDate() ? dol_print_date($rssparser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")),
108 'subpicto' => 'globe',
109 'target' => '_blank',
110 );
111 }
112
113 // INFO on items
114 $items = $rssparser->getItems();
115 //print '<pre>'.print_r($items,true).'</pre>';
116
117 // Loop on last items
118 $nbitems = count($items);
119 for ($line = 0; $line < $max && $line < $nbitems; $line++) {
120 $item = $items[$line];
121
122 // Feed common fields
123 $href = $item['link'];
124 $title = urldecode($item['title']);
125 $date = empty($item['date_timestamp']) ? null : $item['date_timestamp']; // date will be empty if conversion into timestamp failed
126 if ($rssparser->getFormat() == 'rss') { // If RSS
127 if (!$date && isset($item['pubdate'])) {
128 $date = $item['pubdate'];
129 }
130 if (!$date && isset($item['pubDate'])) {
131 $date = $item['pubDate'];
132 }
133 if (!$date && isset($item['dc']['date'])) {
134 $date = $item['dc']['date'];
135 }
136 //$item['dc']['language']
137 //$item['dc']['publisher']
138 }
139 if ($rssparser->getFormat() == 'atom') { // If Atom
140 if (!$date && isset($item['issued'])) {
141 $date = $item['issued'];
142 }
143 if (!$date && isset($item['modified'])) {
144 $date = $item['modified'];
145 }
146 //$item['issued']
147 //$item['modified']
148 //$item['atom_content']
149 }
150 if (!is_numeric($date)) {
151 $timestamp = strtotime($date);
152 if ($timestamp > 0) {
153 $date = $timestamp;
154 }
155 }
156 if (is_numeric($date)) {
157 $date = dol_print_date($date, "dayhour", 'tzuserrel');
158 }
159
160 $isutf8 = utf8_check($title);
161 if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
162 $title = mb_convert_encoding($title, 'UTF-8', 'ISO-8859-1');
163 } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
164 $title = mb_convert_encoding($title, 'ISO-8859-1');
165 }
166
167 $title = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $title); // Manage issue of quotes improperly (de)coded in utf-8
168 $title = preg_replace("/^\s+/", "", $title); // Remove leading whitespace
169
170 $tooltip = $title;
171 $description = !empty($item['description']) ? $item['description'] : '';
172 $isutf8 = utf8_check($description);
173 if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
174 $description = mb_convert_encoding($description, 'UTF-8', 'ISO-8859-1');
175 } elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
176 $description = mb_convert_encoding($description, 'ISO-8859-1');
177 }
178 $description = preg_replace("/([[:alnum:]])\?([[:alnum:]])/", "\\1'\\2", $description);
179 $description = preg_replace("/^\s+/", "", $description);
180 $description = str_replace("\r\n", "", $description);
181 $tooltip .= '<br>'.$description;
182
183 $this->info_box_contents[$line][0] = array(
184 'td' => 'class="left" width="16"',
185 'text' => img_picto('', 'rss'),
186 'url' => $href,
187 'tooltip' => dol_escape_htmltag($tooltip),
188 'target' => 'newrss',
189 );
190
191 $this->info_box_contents[$line][1] = array(
192 'td' => 'class="tdoverflowmax300"',
193 'text' => dol_escape_htmltag($title),
194 'url' => $href,
195 'tooltip' => dol_escape_htmltag($tooltip),
196 'maxlength' => 0,
197 'target' => 'newrss',
198 );
199
200 $this->info_box_contents[$line][2] = array(
201 'td' => 'class="right nowraponall"',
202 'text' => dol_escape_htmltag($date),
203 );
204 }
205 }
206
207
208
209
218 public function showBox($head = null, $contents = null, $nooutput = 0)
219 {
220 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
221 }
222}
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...