dolibarr 21.0.0-alpha
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
39// Load translation files required by the page
40$langs->load("admin");
41
42$lastexternalrss = 0;
43$action = GETPOST('action', 'aZ09');
44
45// Security check
46if (!$user->admin) {
48}
49
50
51/*
52 * Actions
53 */
54
55// positionne la variable pour le nombre de rss externes
56$sql = "SELECT ".$db->decrypt('name')." as name FROM ".MAIN_DB_PREFIX."const";
57$sql .= " WHERE ".$db->decrypt('name')." LIKE 'EXTERNAL_RSS_URLRSS_%'";
58//print $sql;
59$result = $db->query($sql); // We can't use SELECT MAX() because EXTERNAL_RSS_URLRSS_10 is lower than EXTERNAL_RSS_URLRSS_9
60if ($result) {
61 $reg = array();
62 while ($obj = $db->fetch_object($result)) {
63 preg_match('/([0-9]+)$/i', $obj->name, $reg);
64 if ($reg[1] && $reg[1] > $lastexternalrss) {
65 $lastexternalrss = $reg[1];
66 }
67 }
68} else {
69 dol_print_error($db);
70}
71
72if ($action == 'add' || GETPOST("modify")) {
73 $external_rss_title = "external_rss_title_".GETPOSTINT("norss");
74 $external_rss_urlrss = "external_rss_urlrss_".GETPOSTINT("norss");
75
76 if (GETPOST($external_rss_urlrss, 'alpha')) {
77 $boxlabel = '(ExternalRSSInformations)';
78 //$external_rss_url = "external_rss_url_" . GETPOST("norss");
79
80 $db->begin();
81
82 if (GETPOST("modify")) {
83 // Supprime boite box_external_rss de definition des boites
84 /* $sql = "UPDATE ".MAIN_DB_PREFIX."boxes_def";
85 $sql.= " SET name = '".$db->escape($boxlabel)."'";
86 $sql.= " WHERE file ='box_external_rss.php' AND note like '".$db->escape(GETPOST("norss"))." %'";
87
88 $resql=$db->query($sql);
89 if (! $resql)
90 {
91 dol_print_error($db,"sql=$sql");
92 exit;
93 }
94 */
95 } else {
96 // Ajoute boite box_external_rss dans definition des boites
97 $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes_def (file, note)";
98 $sql .= " VALUES ('box_external_rss.php', '".$db->escape(GETPOSTINT("norss")." (".GETPOST($external_rss_title)).")')";
99 if (!$db->query($sql)) {
100 dol_print_error($db);
101 $error++;
102 }
103 //print $sql;exit;
104 }
105
106 $result1 = dolibarr_set_const($db, "EXTERNAL_RSS_TITLE_".GETPOSTINT("norss"), GETPOST($external_rss_title), 'chaine', 0, '', $conf->entity);
107 $result2 = 0;
108 if ($result1) {
109 $consttosave = "EXTERNAL_RSS_URLRSS_".GETPOSTINT("norss");
110 $urltosave = GETPOST($external_rss_urlrss, 'alpha');
111 $result2 = dolibarr_set_const($db, $consttosave, $urltosave, 'chaine', 0, '', $conf->entity);
112 //var_dump($result2);exit;
113 }
114
115 if ($result1 && $result2) {
116 $db->commit();
117 header("Location: ".$_SERVER["PHP_SELF"]);
118 exit;
119 } else {
120 $db->rollback();
121 dol_print_error($db);
122 }
123 }
124}
125
126if (GETPOST("delete")) {
127 if (GETPOSTINT("norss")) {
128 $db->begin();
129
130 // Supprime boite box_external_rss de definition des boites
131 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."boxes_def";
132 $sql .= " WHERE file = 'box_external_rss.php' AND note LIKE '".$db->escape(GETPOSTINT("norss"))." %'";
133
134 $resql = $db->query($sql);
135 if ($resql) {
136 $num = $db->num_rows($resql);
137 $i = 0;
138 while ($i < $num) {
139 $obj = $db->fetch_object($resql);
140
141 $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
142 $sql .= " WHERE entity = ".$conf->entity;
143 $sql .= " AND box_id = ".((int) $obj->rowid);
144 $resql = $db->query($sql);
145
146 $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes_def";
147 $sql .= " WHERE rowid = ".((int) $obj->rowid);
148 $resql = $db->query($sql);
149
150 if (!$resql) {
151 $db->rollback();
152 dol_print_error($db, "sql=".$sql);
153 exit;
154 }
155
156 $i++;
157 }
158
159 $db->commit();
160 } else {
161 $db->rollback();
162 dol_print_error($db, "sql=".$sql);
163 exit;
164 }
165
166
167 $result1 = dolibarr_del_const($db, "EXTERNAL_RSS_TITLE_".GETPOSTINT("norss"), $conf->entity);
168 $result2 = 0;
169 if ($result1) {
170 $result2 = dolibarr_del_const($db, "EXTERNAL_RSS_URLRSS_".GETPOSTINT("norss"), $conf->entity);
171 }
172
173 if ($result1 && $result2) {
174 $db->commit();
175 header("Location: external_rss.php");
176 exit;
177 } else {
178 $db->rollback();
179 dol_print_error($db);
180 }
181 }
182}
183
184
185/*
186 * View
187 */
188$form = new Form($db);
189
190llxHeader('', $langs->trans("ExternalRSSSetup"), '', '', 0, 0, '', '', '', 'mod-admin page-external_rss');
191
192$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
193print load_fiche_titre($langs->trans("ExternalRSSSetup"), $linkback, 'title_setup');
194print '<br>';
195
196// Formulaire ajout
197print '<form name="externalrssconfig" action="'.$_SERVER["PHP_SELF"].'" method="post">';
198print '<input type="hidden" name="token" value="'.newToken().'">';
199
200print '<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
201print '<table class="noborder centpercent">';
202
203print '<tr class="liste_titre">';
204print '<td colspan="2">'.$langs->trans("NewRSS").'</td>';
205print '<td>'.$langs->trans("Example").'</td>';
206print '</tr>';
207
208print '<tr class="oddeven">';
209print '<td width="100">'.$langs->trans("Title").'</td>';
210print '<td><input type="text" class="flat minwidth300" name="external_rss_title_'.($lastexternalrss + 1).'" value=""></td>';
211print '<td>'.$langs->trans('RSSUrlExample').'</td>';
212print '</tr>';
213
214print '<tr class="oddeven">';
215print '<td>'.$langs->trans('RSSUrl').'</td>';
216print '<td><input type="text" class="flat minwidth300" name="external_rss_urlrss_'.($lastexternalrss + 1).'" value=""></td>';
217print '<td>https://news.google.com/news?ned=us&topic=h&output=rss<br>https://www.dolibarr.org/rss</td>';
218print '</tr>';
219print '</table>';
220
221print '</div>';
222
223print $form->buttonsSaveCancel("Add", '');
224print '<input type="hidden" name="action" value="add">';
225print '<input type="hidden" name="norss" value="'.($lastexternalrss + 1).'">';
226
227print '</form>';
228
229print '<br><br>';
230print '<span class="opacitymedium">'.$langs->trans('RssNote').'</span>';
231print ' - ';
232print '<a href="'.DOL_URL_ROOT.'/admin/boxes.php?backtopage='.urlencode($_SERVER["PHP_SELF"]).'">'.$langs->trans('JumpToBoxes').'</a>';
233print '<br><br>';
234
235$sql = "SELECT rowid, file, note FROM ".MAIN_DB_PREFIX."boxes_def";
236$sql .= " WHERE file = 'box_external_rss.php'";
237$sql .= " ORDER BY note";
238
239dol_syslog("select rss boxes", LOG_DEBUG);
240$resql = $db->query($sql);
241if ($resql) {
242 $boxlist = InfoBox::listBoxes($db, 'activated', -1, null);
243 $num = $db->num_rows($resql);
244 $i = 0;
245
246 while ($i < $num) {
247 $obj = $db->fetch_object($resql);
248
249 preg_match('/^([0-9]+)/i', $obj->note, $reg);
250 $idrss = $reg[1];
251 $keyrsstitle = "EXTERNAL_RSS_TITLE_".$idrss;
252 $keyrssurl = "EXTERNAL_RSS_URLRSS_".$idrss;
253 //print "x".$idrss;
254
255 $rssparser = new RssParser($db);
256 $result = $rssparser->parser(getDolGlobalString($keyrssurl), 5, 300, $conf->externalrss->dir_temp);
257
258 print "<br>";
259 print '<form name="externalrssconfig" action="'.$_SERVER["PHP_SELF"].'" method="post">'."\n";
260
261 print '<table class="noborder centpercent">'."\n";
262 print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
263
264 print '<tr class="liste_titre">';
265 print "<td>".$langs->trans("RSS")." ".($i + 1)."</td>";
266 print '<td class="right">';
267 print '<input type="submit" class="button buttongen button-save" name="modify" value="'.$langs->trans("Modify").'">';
268 print '<input type="submit" class="button buttongen button-cancel" name="delete" value="'.$langs->trans("Delete").'">';
269 print '<input type="hidden" name="norss" value="'.$idrss.'">';
270 print '</td>';
271 print '</tr>'."\n";
272
273
274 print '<tr class="oddeven">';
275 print '<td class="titlefield">'.$langs->trans("Title")."</td>";
276 print '<td><input type="text" class="flat minwidth300" name="external_rss_title_'.$idrss.'" value="'.dol_escape_htmltag(getDolGlobalString($keyrsstitle)).'"></td>';
277 print '</tr>'."\n";
278
279
280 print '<tr class="oddeven">';
281 print "<td>".$langs->trans("URL")."</td>";
282 print '<td><input type="text" class="flat minwidth300" name="external_rss_urlrss_'.$idrss.'" value="'.dol_escape_htmltag(getDolGlobalString($keyrssurl)).'"></td>';
283 print '</tr>'."\n";
284
285
286 print '<tr class="oddeven">';
287 print "<td>".$langs->trans("Status")."</td>";
288 print "<td>";
289 if ($result > 0 && empty($rssparser->error)) {
290 print '<span class="ok">'.img_picto($langs->trans("Online"), 'tick', 'class="pictofixedwidth"').$langs->trans("Online").'</div>';
291 } else {
292 print '<span class="error">'.$langs->trans("Offline");
293 $langs->load("errors");
294 if ($rssparser->error) {
295 print ' - '.$langs->trans($rssparser->error);
296 }
297 print '</div>';
298 }
299 print "</td>";
300 print '</tr>'."\n";
301
302 // Logo
303 if ($result > 0 && empty($rssparser->error)) {
304 print '<tr class="oddeven">';
305 print "<td>".$langs->trans("Logo")."</td>";
306 print '<td>';
307 $imageurl = $rssparser->getImageUrl();
308 $linkrss = $rssparser->getLink();
309 if (!preg_match('/^http/', $imageurl)) {
310 include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
311 if (image_format_supported($imageurl) >= 0) {
312 // 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.
313 $imageurl = $linkrss.$imageurl;
314 }
315 }
316 if ($imageurl && preg_match('/^http/', $imageurl)) {
317 print '<img height="32" src="'.$imageurl.'">';
318 } else {
319 print $langs->trans("None");
320 }
321 print '</td>';
322 print '</tr>'."\n";
323 }
324
325 // Active
326 $active = _isInBoxList((int) $idrss, $boxlist) ? 'yes' : 'no';
327 print '<tr class="oddeven">';
328 print '<td>'.$langs->trans('WidgetAvailable').'</td>';
329 print '<td>'.yn($active);
330 print ' &nbsp; - &nbsp; <a href="'.DOL_URL_ROOT.'/admin/boxes.php?backtopage='.urlencode($_SERVER["PHP_SELF"]).'">';
331 print $langs->trans("JumpToBoxes");
332 print '</a>';
333 print '</td>';
334 print '</tr>'."\n";
335
336 print '</table>'."\n";
337
338 print "</form>\n";
339
340 $i++;
341 }
342} else {
343 dol_print_error($db);
344}
345
346// End of page
347llxFooter();
348$db->close();
349
357function _isInBoxList($idrss, array $boxlist)
358{
359 foreach ($boxlist as $box) {
360 if ($box->boxcode === "lastrssinfos" && strpos($box->note, (string) $idrss) !== false) {
361 return true;
362 }
363 }
364
365 return false;
366}
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:70
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.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.