dolibarr 21.0.0-beta
syslog.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
5 * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29// Load Dolibarr environment
30require '../main.inc.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
32
41if (!$user->admin) {
43}
44
45// Load translation files required by the page
46$langs->loadLangs(array("admin", "other"));
47
48$error = 0;
49$action = GETPOST('action', 'aZ09');
50
51$syslogModules = array();
52$activeModules = array();
53
54if (getDolGlobalString('SYSLOG_HANDLERS')) {
55 $activeModules = json_decode($conf->global->SYSLOG_HANDLERS);
56 if (!is_array($activeModules)) {
57 $activeModules = array();
58 }
59}
60
61$dirsyslogs = array_merge(array('/core/modules/syslog/'), $conf->modules_parts['syslog']);
62foreach ($dirsyslogs as $reldir) {
63 $dir = dol_buildpath($reldir, 0);
64 $newdir = dol_osencode($dir);
65 if (is_dir($newdir)) {
66 $handle = opendir($newdir);
67
68 if (is_resource($handle)) {
69 while (($file = readdir($handle)) !== false) {
70 if (substr($file, 0, 11) == 'mod_syslog_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
71 $file = substr($file, 0, dol_strlen($file) - 4);
72
73 require_once $newdir.$file.'.php';
74
75 $module = new $file();
76 '@phan-var-force LogHandler $module';
77
78 // Show modules according to features level
79 if ($module->getVersion() == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
80 continue;
81 }
82 if ($module->getVersion() == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
83 continue;
84 }
85
86 $syslogModules[] = $file;
87 }
88 }
89 closedir($handle);
90 }
91 }
92}
93
94
95/*
96 * Actions
97 */
98
99// Set modes
100if ($action == 'set') {
101 $db->begin();
102
103 $newActiveModules = array();
104 $selectedModules = (GETPOSTISSET('SYSLOG_HANDLERS') ? GETPOST('SYSLOG_HANDLERS') : array());
105
106 // Save options of handler
107 foreach ($syslogModules as $syslogHandler) {
108 if (in_array($syslogHandler, $syslogModules)) {
109 $module = new $syslogHandler();
110 '@phan-var-force LogHandler $module';
111
112 if (in_array($syslogHandler, $selectedModules)) {
113 $newActiveModules[] = $syslogHandler;
114 }
115 foreach ($module->configure() as $option) {
116 if (GETPOSTISSET($option['constant'])) {
117 dolibarr_del_const($db, $option['constant'], -1);
118 dolibarr_set_const($db, $option['constant'], trim(GETPOST($option['constant'])), 'chaine', 0, '', 0);
119 }
120 }
121 }
122 }
123
124 $activeModules = $newActiveModules;
125
126 dolibarr_del_const($db, 'SYSLOG_HANDLERS', -1); // To be sure there is not a setup into another entity
127 dolibarr_set_const($db, 'SYSLOG_HANDLERS', json_encode($activeModules), 'chaine', 0, '', 0);
128 $error = 0;
129 $errors = [];
130 // Check configuration
131 foreach ($activeModules as $modulename) {
132 $module = new $modulename();
133 '@phan-var-force LogHandler $module';
134 $res = $module->checkConfiguration();
135 if (!$res) {
136 $error++;
137 $errors = array_merge($errors, $module->errors);
138 }
139 }
140
141
142 if (!$error) {
143 $db->commit();
144 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
145 } else {
146 $db->rollback();
147 setEventMessages('', $errors, 'errors');
148 }
149}
150
151// Set level
152if ($action == 'setlevel') {
153 $level = GETPOST("level");
154 $res = dolibarr_set_const($db, "SYSLOG_LEVEL", $level, 'chaine', 0, '', 0);
155 dol_syslog("admin/syslog: level ".$level);
156
157 if (!($res > 0)) {
158 $error++;
159 }
160
161 if (!$error) {
162 $file_saves = GETPOST("file_saves");
163 $res = dolibarr_set_const($db, "SYSLOG_FILE_SAVES", $file_saves, 'chaine', 0, '', 0);
164 dol_syslog("admin/syslog: file saves ".$file_saves);
165
166 if (!($res > 0)) {
167 $error++;
168 }
169 }
170
171 if (!$error) {
172 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
173 } else {
174 setEventMessages($langs->trans("Error"), null, 'errors');
175 }
176}
177
178
179/*
180 * View
181 */
182
183llxHeader('', $langs->trans("SyslogSetup"), '', '', 0, 0, '', '', '', 'mod-admin page-syslog');
184
185$form = new Form($db);
186
187$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
188print load_fiche_titre($langs->trans("SyslogSetup"), $linkback, 'title_setup');
189print '<br>';
190
191$syslogfacility = $defaultsyslogfacility = dolibarr_get_const($db, "SYSLOG_FACILITY", 0);
192$syslogfile = $defaultsyslogfile = dolibarr_get_const($db, "SYSLOG_FILE", 0);
193
194if (!$defaultsyslogfacility) {
195 $defaultsyslogfacility = 'LOG_USER';
196}
197if (!$defaultsyslogfile) {
198 $defaultsyslogfile = 'dolibarr.log';
199}
200$optionmc = '';
201if (isModEnabled('multicompany') && $user->entity) {
202 print '<div class="error">'.$langs->trans("ContactSuperAdminForChange").'</div>';
203 $optionmc = 'disabled';
204}
205
206
207// Output mode
208
209print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
210
211print load_fiche_titre($langs->trans("SyslogOutput"), '', '');
212
213print '<input type="hidden" name="token" value="'.newToken().'">';
214print '<input type="hidden" name="action" value="set">';
215
216print '<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
217print '<table class="noborder centpercent">';
218print '<tr class="liste_titre">';
219print '<td>'.$langs->trans("Type").'</td>';
220print '<td>'.$langs->trans("Value").'</td>';
221print '<td class="center width150"><input type="submit" class="button small" '.$optionmc.' value="'.$langs->trans("Modify").'"></td>';
222print "</tr>\n";
223
224foreach ($syslogModules as $moduleName) {
225 $module = new $moduleName();
226 '@phan-var-force LogHandler $module';
227
228 $moduleactive = (int) $module->isActive();
229 //print $moduleName." = ".$moduleactive." - ".$module->getName()." ".($moduleactive == -1)."<br>\n";
230 if (($moduleactive == -1) && getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0) {
231 continue; // Some modules are hidden if not activable and not into debug mode (end user must not see them)
232 }
233
234
235 print '<tr class="oddeven">';
236 print '<td class="nowraponall" width="140">';
237 print '<input class="oddeven" type="checkbox" id="syslog_handler_'.$moduleName.'" name="SYSLOG_HANDLERS[]" value="'.$moduleName.'" '.(in_array($moduleName, $activeModules) ? 'checked' : '').($moduleactive <= 0 ? 'disabled' : '').'> ';
238 print '<label for="syslog_handler_'.$moduleName.'">'.$module->getName().'</label>';
239 if ($moduleName == 'mod_syslog_syslog') {
240 if (!$module->isActive()) {
241 $langs->load("errors");
242 print $form->textwithpicto('', $langs->trans("ErrorPHPNeedModule", 'SysLog'));
243 }
244 }
245 print '</td>';
246
247 print '<td class="nowrap">';
248 $setuparray = $module->configure();
249
250 if ($setuparray) {
251 foreach ($setuparray as $option) {
252 $tmpoption = $option['constant'];
253 $value = '';
254 if (!empty($tmpoption)) {
255 if (GETPOSTISSET($tmpoption)) {
256 $value = GETPOST($tmpoption);
257 } else {
258 $value = getDolGlobalString($tmpoption);
259 }
260 } else {
261 $value = (isset($option['default']) ? $option['default'] : '');
262 }
263
264 print '<span class="hideonsmartphone opacitymedium">'.$option['name'].': </span><input type="text" class="flat'.(empty($option['css']) ? '' : ' '.$option['css']).'" name="'.dol_escape_htmltag($option['constant']).'" value="'.$value.'"'.(isset($option['attr']) ? ' '.$option['attr'] : '').'>';
265 if (!empty($option['example'])) {
266 print '<br>'.$langs->trans("Example").': '.dol_escape_htmltag($option['example']);
267 }
268
269 if ($option['constant'] == 'SYSLOG_FILE' && preg_match('/^DOL_DATA_ROOT\/[^\/]*$/', $value)) {
270 $filelogparam = ' &nbsp; &nbsp; <a href="'.DOL_URL_ROOT.'/document.php?modulepart=logs&file='.basename($value).'">';
271 $filelogparam .= $langs->trans('Download');
272 $filelogparam .= img_picto($langs->trans('Download').' '.basename($value), 'download', 'class="paddingleft"');
273 $filelogparam .= '</a>';
274 print $filelogparam;
275 }
276 }
277 }
278 print '</td>';
279
280 print '<td class="center">';
281 if ($module->getInfo()) {
282 print $form->textwithpicto('', $module->getInfo(), 1, 'help');
283 }
284 if ($module->getWarning()) {
285 print $form->textwithpicto('', $module->getWarning(), 1, 'warning');
286 }
287 print '</td>';
288 print "</tr>\n";
289}
290
291print "</table>\n";
292print "</div>\n";
293
294print "</form>\n";
295
296
297print '<br>'."\n\n";
298
299
300// Level
301
302print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
303
304print load_fiche_titre($langs->trans("SyslogLevel"), '', '');
305
306print '<input type="hidden" name="token" value="'.newToken().'">';
307print '<input type="hidden" name="action" value="setlevel">';
308
309print '<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
310print '<table class="noborder centpercent">';
311print '<tr class="liste_titre">';
312print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
313print '<td class="center width150"><input type="submit" class="button small" '.$optionmc.' value="'.$langs->trans("Modify").'"></td>';
314print "</tr>\n";
315
316print '<tr class="oddeven"><td>'.$langs->trans("SyslogLevel").'</td>';
317print '<td colspan="2"><select class="flat minwidth400" id="level" name="level" '.$optionmc.'>';
318print '<option value="'.LOG_EMERG.'" '.($conf->global->SYSLOG_LEVEL == LOG_EMERG ? 'SELECTED' : '').'>LOG_EMERG ('.LOG_EMERG.')</option>';
319print '<option value="'.LOG_ALERT.'" '.($conf->global->SYSLOG_LEVEL == LOG_ALERT ? 'SELECTED' : '').'>LOG_ALERT ('.LOG_ALERT.')</option>';
320print '<option value="'.LOG_CRIT.'" '.($conf->global->SYSLOG_LEVEL == LOG_CRIT ? 'SELECTED' : '').'>LOG_CRIT ('.LOG_CRIT.')</option>';
321print '<option value="'.LOG_ERR.'" '.($conf->global->SYSLOG_LEVEL == LOG_ERR ? 'SELECTED' : '').'>LOG_ERR ('.LOG_ERR.')</option>';
322print '<option value="'.LOG_WARNING.'" '.($conf->global->SYSLOG_LEVEL == LOG_WARNING ? 'SELECTED' : '').'">LOG_WARNING ('.LOG_WARNING.')</option>';
323print '<option value="'.LOG_NOTICE.'" '.($conf->global->SYSLOG_LEVEL == LOG_NOTICE ? 'SELECTED' : '').' data-html="'.dol_escape_htmltag('LOG_NOTICE ('.LOG_NOTICE.') - <span class="opacitymedium">'.$langs->trans("RecommendedForProduction").'</span>').'">LOG_NOTICE ('.LOG_NOTICE.')</option>';
324print '<option value="'.LOG_INFO.'" '.($conf->global->SYSLOG_LEVEL == LOG_INFO ? 'SELECTED' : '').'>LOG_INFO ('.LOG_INFO.')</option>';
325print '<option value="'.LOG_DEBUG.'" '.($conf->global->SYSLOG_LEVEL >= LOG_DEBUG ? 'SELECTED' : '').' data-html="'.dol_escape_htmltag('LOG_DEBUG ('.LOG_DEBUG.') - <span class="opacitymedium">'.$langs->trans("RecommendedForDebug").'</span>').'">LOG_DEBUG ('.LOG_DEBUG.')</option>';
326print '</select>';
327
328print ajax_combobox("level");
329print '</td></tr>';
330
331if (!empty($conf->loghandlers['mod_syslog_file']) && isModEnabled('cron')) {
332 print '<tr class="oddeven"><td>'.$langs->trans("SyslogFileNumberOfSaves").'</td>';
333 print '<td colspan="2"><input class="width50" type="number" name="file_saves" placeholder="14" min="0" step="1" value="'.getDolGlobalString('SYSLOG_FILE_SAVES').'" />';
334 print ' &nbsp; (<a href="'.dol_buildpath('/cron/list.php', 1).'?search_label=CompressSyslogs&status=-1">'.$langs->trans('ConfigureCleaningCronjobToSetFrequencyOfSaves').'</a>)</td></tr>';
335}
336
337print '</table>';
338print "</div>\n";
339
340print "</form>\n";
341
342// End of page
343llxFooter();
344$db->close();
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.
dolibarr_get_const($db, $name, $entity=1)
Get the value of a setup constant from database.
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:459
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:71
Class to manage generation of HTML components Only common components must be here.
llxFooter()
Footer empty.
Definition document.php:107
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.