dolibarr 18.0.6
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 '<table class="noborder centpercent">';
197print '<tr class="liste_titre">';
198print '<td colspan="2">'.$langs->trans("NewRSS").'</td>';
199print '<td>'.$langs->trans("Example").'</td>';
200print '</tr>';
201print '<tr class="impair">';
202print '<td width="100">'.$langs->trans("Title").'</td>';
203print '<td><input type="text" class="flat minwidth300" name="external_rss_title_'.($lastexternalrss + 1).'" value=""></td>';
204print '<td>'.$langs->trans('RSSUrlExample').'</td>';
205print '</tr>';
206
207print '<tr class="pair">';
208print '<td>'.$langs->trans('RSSUrl').'</td>';
209print '<td><input type="text" class="flat minwidth300" name="external_rss_urlrss_'.($lastexternalrss + 1).'" value=""></td>';
210print '<td>http://news.google.com/news?ned=us&topic=h&output=rss<br>http://www.dolibarr.org/rss</td>';
211print '</tr>';
212print '</table>';
213
214print $form->buttonsSaveCancel("Add", '');
215print '<input type="hidden" name="action" value="add">';
216print '<input type="hidden" name="norss" value="'.($lastexternalrss + 1).'">';
217
218print '</form>';
219
220print '<br><br>';
221print '<span class="opacitymedium">'.$langs->trans('RssNote').'</span> - <a href="'.DOL_MAIN_URL_ROOT.'/admin/boxes.php">'.$langs->trans('JumpToBoxes').'</a>';
222print '<br><br>';
223
224$sql = "SELECT rowid, file, note FROM ".MAIN_DB_PREFIX."boxes_def";
225$sql .= " WHERE file = 'box_external_rss.php'";
226$sql .= " ORDER BY note";
227
228dol_syslog("select rss boxes", LOG_DEBUG);
229$resql = $db->query($sql);
230if ($resql) {
231 $boxlist = InfoBox::listBoxes($db, 'activated', -1, null);
232 $num = $db->num_rows($resql);
233 $i = 0;
234
235 while ($i < $num) {
236 $obj = $db->fetch_object($resql);
237
238 preg_match('/^([0-9]+)/i', $obj->note, $reg);
239 $idrss = $reg[1];
240 $keyrsstitle = "EXTERNAL_RSS_TITLE_".$idrss;
241 $keyrssurl = "EXTERNAL_RSS_URLRSS_".$idrss;
242 //print "x".$idrss;
243
244 $rssparser = new RssParser($db);
245 $result = $rssparser->parser($conf->global->$keyrssurl, 5, 300, $conf->externalrss->dir_temp);
246
247 print "<br>";
248 print '<form name="externalrssconfig" action="'.$_SERVER["PHP_SELF"].'" method="post">'."\n";
249
250 print '<table class="noborder centpercent">'."\n";
251 print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
252
253 print '<tr class="liste_titre">';
254 print "<td>".$langs->trans("RSS")." ".($i + 1)."</td>";
255 print '<td class="right">';
256 print '<input type="submit" class="button buttongen button-save" name="modify" value="'.$langs->trans("Modify").'">';
257 print '<input type="submit" class="button buttongen button-cancel" name="delete" value="'.$langs->trans("Delete").'">';
258 print '<input type="hidden" name="norss" value="'.$idrss.'">';
259 print '</td>';
260 print '</tr>'."\n";
261
262
263 print '<tr class="oddeven">';
264 print '<td class="titlefield">'.$langs->trans("Title")."</td>";
265 print '<td><input type="text" class="flat minwidth300" name="external_rss_title_'.$idrss.'" value="'.dol_escape_htmltag($conf->global->$keyrsstitle).'"></td>';
266 print '</tr>'."\n";
267
268
269 print '<tr class="oddeven">';
270 print "<td>".$langs->trans("URL")."</td>";
271 print '<td><input type="text" class="flat minwidth300" name="external_rss_urlrss_'.$idrss.'" value="'.dol_escape_htmltag($conf->global->$keyrssurl).'"></td>';
272 print '</tr>'."\n";
273
274
275 print '<tr class="oddeven">';
276 print "<td>".$langs->trans("Status")."</td>";
277 print "<td>";
278 if ($result > 0 && empty($rss->error)) {
279 print '<span class="ok">'.$langs->trans("Online").'</div>';
280 } else {
281 print '<span class="error">'.$langs->trans("Offline");
282 $langs->load("errors");
283 if ($rssparser->error) {
284 print ' - '.$langs->trans($rssparser->error);
285 }
286 print '</div>';
287 }
288 print "</td>";
289 print '</tr>'."\n";
290
291 // Logo
292 if ($result > 0 && empty($rss->error)) {
293 print '<tr class="oddeven">';
294 print "<td>".$langs->trans("Logo")."</td>";
295 print '<td>';
296 $imageurl = $rssparser->getImageUrl();
297 $linkrss = $rssparser->getLink();
298 if (!preg_match('/^http/', $imageurl)) {
299 include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
300 if (image_format_supported($imageurl) >= 0) {
301 // 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.
302 $imageurl = $linkrss.$imageurl;
303 }
304 }
305 if ($imageurl && preg_match('/^http/', $imageurl)) {
306 print '<img height="32" src="'.$imageurl.'">';
307 } else {
308 print $langs->trans("None");
309 }
310 print '</td>';
311 print '</tr>'."\n";
312 }
313
314 // Active
315 $active = _isInBoxList($idrss, $boxlist) ? 'yes' : 'no';
316 print '<tr class="oddeven">';
317 print '<td>'.$langs->trans('WidgetAvailable').'</td>';
318 print '<td>'.yn($active).'</td>';
319 print '</tr>'."\n";
320
321 print '</table>'."\n";
322
323 print "</form>\n";
324
325 $i++;
326 }
327} else {
328 dol_print_error($db);
329}
330
331// End of page
332llxFooter();
333$db->close();
334
342function _isInBoxList($idrss, array $boxlist)
343{
344 foreach ($boxlist as $box) {
345 if ($box->boxcode === "lastrssinfos" && strpos($box->note, $idrss) !== false) {
346 return true;
347 }
348 }
349
350 return false;
351}
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:56
llxFooter()
Empty footer.
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.
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.
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.