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