dolibarr 21.0.0-beta
external_rss.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Eric Seigne <erics@rycks.com>
3 * Copyright (C) 2003,2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
4 * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
6 * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
7 * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
8 * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
9 * Copyright (C) 2020 Tobias Sekan <tobias.sekan@startmail.com>
10 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
11 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26
33// Load Dolibarr environment
34require '../main.inc.php';
35require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
36require_once DOL_DOCUMENT_ROOT.'/core/class/rssparser.class.php';
37require_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
38
47// Load translation files required by the page
48$langs->load("admin");
49
50$lastexternalrss = 0;
51$action = GETPOST('action', 'aZ09');
52
53// Security check
54if (!$user->admin) {
56}
57
58
59/*
60 * Actions
61 */
62$error = 0;
63
64// positionne la variable pour le nombre de rss externes
65$sql = "SELECT ".$db->decrypt('name')." as name FROM ".MAIN_DB_PREFIX."const";
66$sql .= " WHERE ".$db->decrypt('name')." LIKE 'EXTERNAL_RSS_URLRSS_%'";
67//print $sql;
68$result = $db->query($sql); // We can't use SELECT MAX() because EXTERNAL_RSS_URLRSS_10 is lower than EXTERNAL_RSS_URLRSS_9
69if ($result) {
70 $reg = array();
71 while ($obj = $db->fetch_object($result)) {
72 preg_match('/([0-9]+)$/i', $obj->name, $reg);
73 if ($reg[1] && $reg[1] > $lastexternalrss) {
74 $lastexternalrss = $reg[1];
75 }
76 }
77} else {
78 dol_print_error($db);
79}
80
81if ($action == 'add' || GETPOST("modify")) {
82 $external_rss_title = "external_rss_title_".GETPOSTINT("norss");
83 $external_rss_urlrss = "external_rss_urlrss_".GETPOSTINT("norss");
84
85 if (GETPOST($external_rss_urlrss, 'alpha')) {
86 $boxlabel = '(ExternalRSSInformations)';
87 //$external_rss_url = "external_rss_url_" . GETPOST("norss");
88
89 $db->begin();
90
91 if (GETPOST("modify")) {
92 // Supprime boite box_external_rss de definition des boites
93 /* $sql = "UPDATE ".MAIN_DB_PREFIX."boxes_def";
94 $sql.= " SET name = '".$db->escape($boxlabel)."'";
95 $sql.= " WHERE file ='box_external_rss.php' AND note like '".$db->escape(GETPOST("norss"))." %'";
96
97 $resql=$db->query($sql);
98 if (! $resql)
99 {
100 dol_print_error($db,"sql=$sql");
101 exit;
102 }
103 */
104 } else {
105 // Ajoute boite box_external_rss dans definition des boites
106 $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes_def (file, note)";
107 $sql .= " VALUES ('box_external_rss.php', '".$db->escape(GETPOSTINT("norss")." (".GETPOST($external_rss_title)).")')";
108 if (!$db->query($sql)) {
109 dol_print_error($db);
110 $error++;
111 }
112 //print $sql;exit;
113 }
114
115 $result1 = dolibarr_set_const($db, "EXTERNAL_RSS_TITLE_".GETPOSTINT("norss"), GETPOST($external_rss_title), 'chaine', 0, '', $conf->entity);
116 $result2 = 0;
117 if ($result1) {
118 $consttosave = "EXTERNAL_RSS_URLRSS_".GETPOSTINT("norss");
119 $urltosave = GETPOST($external_rss_urlrss, 'alpha');
120 $result2 = dolibarr_set_const($db, $consttosave, $urltosave, 'chaine', 0, '', $conf->entity);
121 //var_dump($result2);exit;
122 }
123
124 if ($result1 && $result2) {
125 $db->commit();
126 header("Location: ".$_SERVER["PHP_SELF"]);
127 exit;
128 } else {
129 $db->rollback();
130 dol_print_error($db);
131 }
132 }
133}
134
135if (GETPOST("delete")) {
136 if (GETPOSTINT("norss")) {
137 $db->begin();
138
139 // Supprime boite box_external_rss de definition des boites
140 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."boxes_def";
141 $sql .= " WHERE file = 'box_external_rss.php' AND note LIKE '".$db->escape(GETPOSTINT("norss"))." %'";
142
143 $resql = $db->query($sql);
144 if ($resql) {
145 $num = $db->num_rows($resql);
146 $i = 0;
147 while ($i < $num) {
148 $obj = $db->fetch_object($resql);
149
150 $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
151 $sql .= " WHERE entity = ".$conf->entity;
152 $sql .= " AND box_id = ".((int) $obj->rowid);
153 $resql = $db->query($sql);
154
155 $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes_def";
156 $sql .= " WHERE rowid = ".((int) $obj->rowid);
157 $resql = $db->query($sql);
158
159 if (!$resql) {
160 $db->rollback();
161 dol_print_error($db, "sql=".$sql);
162 exit;
163 }
164
165 $i++;
166 }
167
168 $db->commit();
169 } else {
170 $db->rollback();
171 dol_print_error($db, "sql=".$sql);
172 exit;
173 }
174
175
176 $result1 = dolibarr_del_const($db, "EXTERNAL_RSS_TITLE_".GETPOSTINT("norss"), $conf->entity);
177 $result2 = 0;
178 if ($result1) {
179 $result2 = dolibarr_del_const($db, "EXTERNAL_RSS_URLRSS_".GETPOSTINT("norss"), $conf->entity);
180 }
181
182 if ($result1 && $result2) {
183 $db->commit();
184 header("Location: external_rss.php");
185 exit;
186 } else {
187 $db->rollback();
188 dol_print_error($db);
189 }
190 }
191}
192
193
194/*
195 * View
196 */
197$form = new Form($db);
198
199llxHeader('', $langs->trans("ExternalRSSSetup"), '', '', 0, 0, '', '', '', 'mod-admin page-external_rss');
200
201$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
202print load_fiche_titre($langs->trans("ExternalRSSSetup"), $linkback, 'title_setup');
203print '<br>';
204
205// Formulaire ajout
206print '<form name="externalrssconfig" action="'.$_SERVER["PHP_SELF"].'" method="post">';
207print '<input type="hidden" name="token" value="'.newToken().'">';
208
209print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
210print '<table class="noborder centpercent">';
211
212print '<tr class="liste_titre">';
213print '<td colspan="2">'.$langs->trans("NewRSS").'</td>';
214print '<td>'.$langs->trans("Example").'</td>';
215print '</tr>';
216
217print '<tr class="oddeven">';
218print '<td width="100">'.$langs->trans("Title").'</td>';
219print '<td><input type="text" class="flat minwidth300" name="external_rss_title_'.($lastexternalrss + 1).'" value=""></td>';
220print '<td>'.$langs->trans('RSSUrlExample').'</td>';
221print '</tr>';
222
223print '<tr class="oddeven">';
224print '<td>'.$langs->trans('RSSUrl').'</td>';
225print '<td><input type="text" class="flat minwidth300" name="external_rss_urlrss_'.($lastexternalrss + 1).'" value=""></td>';
226print '<td>https://news.google.com/news?ned=us&topic=h&output=rss<br>https://www.dolibarr.org/rss</td>';
227print '</tr>';
228print '</table>';
229
230print '</div>';
231
232print $form->buttonsSaveCancel("Add", '');
233print '<input type="hidden" name="action" value="add">';
234print '<input type="hidden" name="norss" value="'.($lastexternalrss + 1).'">';
235
236print '</form>';
237
238print '<br><br>';
239print '<span class="opacitymedium">'.$langs->trans('RssNote').'</span>';
240print ' - ';
241print '<a href="'.DOL_URL_ROOT.'/admin/boxes.php?backtopage='.urlencode($_SERVER["PHP_SELF"]).'">'.$langs->trans('JumpToBoxes').'</a>';
242print '<br><br>';
243
244$sql = "SELECT rowid, file, note FROM ".MAIN_DB_PREFIX."boxes_def";
245$sql .= " WHERE file = 'box_external_rss.php'";
246$sql .= " ORDER BY note";
247
248dol_syslog("select rss boxes", LOG_DEBUG);
249$resql = $db->query($sql);
250if ($resql) {
251 $boxlist = InfoBox::listBoxes($db, 'activated', -1, null);
252 $num = $db->num_rows($resql);
253 $i = 0;
254
255 while ($i < $num) {
256 $obj = $db->fetch_object($resql);
257
258 preg_match('/^([0-9]+)/i', $obj->note, $reg);
259 $idrss = $reg[1];
260 $keyrsstitle = "EXTERNAL_RSS_TITLE_".$idrss;
261 $keyrssurl = "EXTERNAL_RSS_URLRSS_".$idrss;
262 //print "x".$idrss;
263
264 $rssparser = new RssParser($db);
265 $result = $rssparser->parser(getDolGlobalString($keyrssurl), 5, 300, $conf->externalrss->dir_temp);
266
267 print "<br>";
268 print '<form name="externalrssconfig" action="'.$_SERVER["PHP_SELF"].'" method="post">'."\n";
269
270 print '<table class="noborder centpercent">'."\n";
271 print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
272
273 print '<tr class="liste_titre">';
274 print "<td>".$langs->trans("RSS")." ".($i + 1)."</td>";
275 print '<td class="right">';
276 print '<input type="submit" class="button buttongen button-save" name="modify" value="'.$langs->trans("Modify").'">';
277 print '<input type="submit" class="button buttongen button-cancel" name="delete" value="'.$langs->trans("Delete").'">';
278 print '<input type="hidden" name="norss" value="'.$idrss.'">';
279 print '</td>';
280 print '</tr>'."\n";
281
282
283 print '<tr class="oddeven">';
284 print '<td class="titlefield">'.$langs->trans("Title")."</td>";
285 print '<td><input type="text" class="flat minwidth300" name="external_rss_title_'.$idrss.'" value="'.dol_escape_htmltag(getDolGlobalString($keyrsstitle)).'"></td>';
286 print '</tr>'."\n";
287
288
289 print '<tr class="oddeven">';
290 print "<td>".$langs->trans("URL")."</td>";
291 print '<td><input type="text" class="flat minwidth300" name="external_rss_urlrss_'.$idrss.'" value="'.dol_escape_htmltag(getDolGlobalString($keyrssurl)).'"></td>';
292 print '</tr>'."\n";
293
294
295 print '<tr class="oddeven">';
296 print "<td>".$langs->trans("Status")."</td>";
297 print "<td>";
298 if ($result > 0 && empty($rssparser->error)) {
299 print '<span class="ok">'.img_picto($langs->trans("Online"), 'tick', 'class="pictofixedwidth"').$langs->trans("Online").'</div>';
300 } else {
301 print '<span class="error">'.$langs->trans("Offline");
302 $langs->load("errors");
303 if ($rssparser->error) {
304 print ' - '.$langs->trans($rssparser->error);
305 }
306 print '</div>';
307 }
308 print "</td>";
309 print '</tr>'."\n";
310
311 // Logo
312 if ($result > 0 && empty($rssparser->error)) {
313 print '<tr class="oddeven">';
314 print "<td>".$langs->trans("Logo")."</td>";
315 print '<td>';
316 $imageurl = $rssparser->getImageUrl();
317 $linkrss = $rssparser->getLink();
318 if (!preg_match('/^http/', $imageurl)) {
319 include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
320 if (image_format_supported($imageurl) >= 0) {
321 // If we are sure imageurl is a path to an image file, and if it does not start with http, we append root url to it.
322 $imageurl = $linkrss.$imageurl;
323 }
324 }
325 if ($imageurl && preg_match('/^http/', $imageurl)) {
326 print '<img height="32" src="'.$imageurl.'">';
327 } else {
328 print $langs->trans("None");
329 }
330 print '</td>';
331 print '</tr>'."\n";
332 }
333
334 // Active
335 $active = _isInBoxList((int) $idrss, $boxlist) ? 'yes' : 'no';
336 print '<tr class="oddeven">';
337 print '<td>'.$langs->trans('WidgetAvailable').'</td>';
338 print '<td>'.yn($active);
339 print ' &nbsp; - &nbsp; <a href="'.DOL_URL_ROOT.'/admin/boxes.php?backtopage='.urlencode($_SERVER["PHP_SELF"]).'">';
340 print $langs->trans("JumpToBoxes");
341 print '</a>';
342 print '</td>';
343 print '</tr>'."\n";
344
345 print '</table>'."\n";
346
347 print "</form>\n";
348
349 $i++;
350 }
351} else {
352 dol_print_error($db);
353}
354
355// End of page
356llxFooter();
357$db->close();
358
366function _isInBoxList($idrss, array $boxlist)
367{
368 foreach ($boxlist as $box) {
369 if ($box->boxcode === "lastrssinfos" && strpos($box->note, (string) $idrss) !== false) {
370 return true;
371 }
372 }
373
374 return false;
375}
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
Class to manage generation of HTML components Only common components must be here.
static listBoxes($dbs, $mode, $zone, $user=null, $excludelist=array(), $includehidden=1)
Return array of boxes qualified for area and user.
Class to parse RSS files.
llxFooter()
Footer empty.
Definition document.php:107
_isInBoxList($idrss, array $boxlist)
Check if the given RSS feed if inside the list of boxes/widgets.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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...
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.