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 if ($result1) {
108 $consttosave = "EXTERNAL_RSS_URLRSS_".GETPOSTINT("norss");
109 $urltosave = GETPOST($external_rss_urlrss, 'alpha');
110 $result2 = dolibarr_set_const($db, $consttosave, $urltosave, 'chaine', 0, '', $conf->entity);
111 //var_dump($result2);exit;
112 }
113
114 if ($result1 && $result2) {
115 $db->commit();
116 header("Location: ".$_SERVER["PHP_SELF"]);
117 exit;
118 } else {
119 $db->rollback();
120 dol_print_error($db);
121 }
122 }
123}
124
125if (GETPOST("delete")) {
126 if (GETPOSTINT("norss")) {
127 $db->begin();
128
129 // Supprime boite box_external_rss de definition des boites
130 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."boxes_def";
131 $sql .= " WHERE file = 'box_external_rss.php' AND note LIKE '".$db->escape(GETPOSTINT("norss"))." %'";
132
133 $resql = $db->query($sql);
134 if ($resql) {
135 $num = $db->num_rows($resql);
136 $i = 0;
137 while ($i < $num) {
138 $obj = $db->fetch_object($resql);
139
140 $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
141 $sql .= " WHERE entity = ".$conf->entity;
142 $sql .= " AND box_id = ".((int) $obj->rowid);
143 $resql = $db->query($sql);
144
145 $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes_def";
146 $sql .= " WHERE rowid = ".((int) $obj->rowid);
147 $resql = $db->query($sql);
148
149 if (!$resql) {
150 $db->rollback();
151 dol_print_error($db, "sql=".$sql);
152 exit;
153 }
154
155 $i++;
156 }
157
158 $db->commit();
159 } else {
160 $db->rollback();
161 dol_print_error($db, "sql=".$sql);
162 exit;
163 }
164
165
166 $result1 = dolibarr_del_const($db, "EXTERNAL_RSS_TITLE_".GETPOSTINT("norss"), $conf->entity);
167 if ($result1) {
168 $result2 = dolibarr_del_const($db, "EXTERNAL_RSS_URLRSS_".GETPOSTINT("norss"), $conf->entity);
169 }
170
171 if ($result1 && $result2) {
172 $db->commit();
173 header("Location: external_rss.php");
174 exit;
175 } else {
176 $db->rollback();
177 dol_print_error($db);
178 }
179 }
180}
181
182
183/*
184 * View
185 */
186$form = new Form($db);
187
188llxHeader('', $langs->trans("ExternalRSSSetup"), '', '', 0, 0, '', '', '', 'mod-admin page-external_rss');
189
190$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
191print load_fiche_titre($langs->trans("ExternalRSSSetup"), $linkback, 'title_setup');
192print '<br>';
193
194// Formulaire ajout
195print '<form name="externalrssconfig" action="'.$_SERVER["PHP_SELF"].'" method="post">';
196print '<input type="hidden" name="token" value="'.newToken().'">';
197
198print '<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
199print '<table class="noborder centpercent">';
200
201print '<tr class="liste_titre">';
202print '<td colspan="2">'.$langs->trans("NewRSS").'</td>';
203print '<td>'.$langs->trans("Example").'</td>';
204print '</tr>';
205
206print '<tr class="oddeven">';
207print '<td width="100">'.$langs->trans("Title").'</td>';
208print '<td><input type="text" class="flat minwidth300" name="external_rss_title_'.($lastexternalrss + 1).'" value=""></td>';
209print '<td>'.$langs->trans('RSSUrlExample').'</td>';
210print '</tr>';
211
212print '<tr class="oddeven">';
213print '<td>'.$langs->trans('RSSUrl').'</td>';
214print '<td><input type="text" class="flat minwidth300" name="external_rss_urlrss_'.($lastexternalrss + 1).'" value=""></td>';
215print '<td>http://news.google.com/news?ned=us&topic=h&output=rss<br>http://www.dolibarr.org/rss</td>';
216print '</tr>';
217print '</table>';
218
219print '</div>';
220
221print $form->buttonsSaveCancel("Add", '');
222print '<input type="hidden" name="action" value="add">';
223print '<input type="hidden" name="norss" value="'.($lastexternalrss + 1).'">';
224
225print '</form>';
226
227print '<br><br>';
228print '<span class="opacitymedium">'.$langs->trans('RssNote').'</span>';
229print ' - ';
230print '<a href="'.DOL_URL_ROOT.'/admin/boxes.php?backtopage='.urlencode($_SERVER["PHP_SELF"]).'">'.$langs->trans('JumpToBoxes').'</a>';
231print '<br><br>';
232
233$sql = "SELECT rowid, file, note FROM ".MAIN_DB_PREFIX."boxes_def";
234$sql .= " WHERE file = 'box_external_rss.php'";
235$sql .= " ORDER BY note";
236
237dol_syslog("select rss boxes", LOG_DEBUG);
238$resql = $db->query($sql);
239if ($resql) {
240 $boxlist = InfoBox::listBoxes($db, 'activated', -1, null);
241 $num = $db->num_rows($resql);
242 $i = 0;
243
244 while ($i < $num) {
245 $obj = $db->fetch_object($resql);
246
247 preg_match('/^([0-9]+)/i', $obj->note, $reg);
248 $idrss = $reg[1];
249 $keyrsstitle = "EXTERNAL_RSS_TITLE_".$idrss;
250 $keyrssurl = "EXTERNAL_RSS_URLRSS_".$idrss;
251 //print "x".$idrss;
252
253 $rssparser = new RssParser($db);
254 $result = $rssparser->parser(getDolGlobalString($keyrssurl), 5, 300, $conf->externalrss->dir_temp);
255
256 print "<br>";
257 print '<form name="externalrssconfig" action="'.$_SERVER["PHP_SELF"].'" method="post">'."\n";
258
259 print '<table class="noborder centpercent">'."\n";
260 print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
261
262 print '<tr class="liste_titre">';
263 print "<td>".$langs->trans("RSS")." ".($i + 1)."</td>";
264 print '<td class="right">';
265 print '<input type="submit" class="button buttongen button-save" name="modify" value="'.$langs->trans("Modify").'">';
266 print '<input type="submit" class="button buttongen button-cancel" name="delete" value="'.$langs->trans("Delete").'">';
267 print '<input type="hidden" name="norss" value="'.$idrss.'">';
268 print '</td>';
269 print '</tr>'."\n";
270
271
272 print '<tr class="oddeven">';
273 print '<td class="titlefield">'.$langs->trans("Title")."</td>";
274 print '<td><input type="text" class="flat minwidth300" name="external_rss_title_'.$idrss.'" value="'.dol_escape_htmltag(getDolGlobalString($keyrsstitle)).'"></td>';
275 print '</tr>'."\n";
276
277
278 print '<tr class="oddeven">';
279 print "<td>".$langs->trans("URL")."</td>";
280 print '<td><input type="text" class="flat minwidth300" name="external_rss_urlrss_'.$idrss.'" value="'.dol_escape_htmltag(getDolGlobalString($keyrssurl)).'"></td>';
281 print '</tr>'."\n";
282
283
284 print '<tr class="oddeven">';
285 print "<td>".$langs->trans("Status")."</td>";
286 print "<td>";
287 if ($result > 0 && empty($rssparser->error)) {
288 print '<span class="ok">'.img_picto($langs->trans("Online"), 'tick', 'class="pictofixedwidth"').$langs->trans("Online").'</div>';
289 } else {
290 print '<span class="error">'.$langs->trans("Offline");
291 $langs->load("errors");
292 if ($rssparser->error) {
293 print ' - '.$langs->trans($rssparser->error);
294 }
295 print '</div>';
296 }
297 print "</td>";
298 print '</tr>'."\n";
299
300 // Logo
301 if ($result > 0 && empty($rss->error)) {
302 print '<tr class="oddeven">';
303 print "<td>".$langs->trans("Logo")."</td>";
304 print '<td>';
305 $imageurl = $rssparser->getImageUrl();
306 $linkrss = $rssparser->getLink();
307 if (!preg_match('/^http/', $imageurl)) {
308 include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
309 if (image_format_supported($imageurl) >= 0) {
310 // 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.
311 $imageurl = $linkrss.$imageurl;
312 }
313 }
314 if ($imageurl && preg_match('/^http/', $imageurl)) {
315 print '<img height="32" src="'.$imageurl.'">';
316 } else {
317 print $langs->trans("None");
318 }
319 print '</td>';
320 print '</tr>'."\n";
321 }
322
323 // Active
324 $active = _isInBoxList((int) $idrss, $boxlist) ? 'yes' : 'no';
325 print '<tr class="oddeven">';
326 print '<td>'.$langs->trans('WidgetAvailable').'</td>';
327 print '<td>'.yn($active);
328 print ' &nbsp; - &nbsp; <a href="'.DOL_URL_ROOT.'/admin/boxes.php?backtopage='.urlencode($_SERVER["PHP_SELF"]).'">';
329 print $langs->trans("JumpToBoxes");
330 print '</a>';
331 print '</td>';
332 print '</tr>'."\n";
333
334 print '</table>'."\n";
335
336 print "</form>\n";
337
338 $i++;
339 }
340} else {
341 dol_print_error($db);
342}
343
344// End of page
345llxFooter();
346$db->close();
347
355function _isInBoxList($idrss, array $boxlist)
356{
357 foreach ($boxlist as $box) {
358 if ($box->boxcode === "lastrssinfos" && strpos($box->note, (string) $idrss) !== false) {
359 return true;
360 }
361 }
362
363 return false;
364}
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 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.