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