dolibarr 23.0.3
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';
38require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
39
40if (!$user->admin) {
42}
43
44// Load translation files required by the page
45$langs->loadLangs(array("admin", "other"));
46
47$error = 0;
48$action = GETPOST('action', 'aZ09');
49
50$syslogModules = array();
51$activeModules = array();
52
53if (getDolGlobalString('SYSLOG_HANDLERS')) {
54 $activeModules = json_decode($conf->global->SYSLOG_HANDLERS);
55 if (!is_array($activeModules)) {
56 $activeModules = array();
57 }
58}
59
60$dirsyslogs = array_merge(array('/core/modules/syslog/'), $conf->modules_parts['syslog']);
61foreach ($dirsyslogs as $reldir) {
62 $dir = dol_buildpath($reldir, 0);
63 $newdir = dol_osencode($dir);
64 if (is_dir($newdir)) {
65 $handle = opendir($newdir);
66
67 if (is_resource($handle)) {
68 while (($file = readdir($handle)) !== false) {
69 if (substr($file, 0, 11) == 'mod_syslog_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
70 $file = substr($file, 0, dol_strlen($file) - 4);
71
72 try {
73 require_once $newdir.$file.'.php';
74
75 if (!class_exists($file)) {
76 dol_syslog('admin/syslog.php skipping stale handler '.$file.' (class not declared)', LOG_WARNING);
77 continue;
78 }
79 $module = new $file();
80 '@phan-var-force LogHandler $module';
81 } catch (Throwable $e) {
82 dol_syslog('admin/syslog.php skipping stale handler '.$file.': '.$e->getMessage(), LOG_WARNING);
83 continue;
84 }
85
86 // Show modules according to features level
87 if ($module->getVersion() == 'development' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
88 continue;
89 }
90 if ($module->getVersion() == 'experimental' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1) {
91 continue;
92 }
93
94 $syslogModules[] = $file;
95 }
96 }
97 closedir($handle);
98 }
99 }
100}
101
102
103/*
104 * Actions
105 */
106
107// Set modes
108if ($action == 'set') {
109 $db->begin();
110
111 $newActiveModules = array();
112 $selectedModules = (GETPOSTISSET('SYSLOG_HANDLERS') ? GETPOST('SYSLOG_HANDLERS') : array());
113
114 // Save options of handler
115 foreach ($syslogModules as $syslogHandler) {
116 if (in_array($syslogHandler, $syslogModules)) {
117 $module = new $syslogHandler();
118 '@phan-var-force LogHandler $module';
119
120 if (in_array($syslogHandler, $selectedModules)) {
121 $newActiveModules[] = $syslogHandler;
122 }
123 foreach ($module->configure() as $option) {
124 if (GETPOSTISSET($option['constant'])) {
125 dolibarr_del_const($db, $option['constant'], -1);
126 dolibarr_set_const($db, $option['constant'], trim(GETPOST($option['constant'])), 'chaine', 0, '', 0);
127 }
128 }
129 }
130 }
131
132 $activeModules = $newActiveModules;
133
134 dolibarr_del_const($db, 'SYSLOG_HANDLERS', -1); // To be sure there is not a setup into another entity
135 dolibarr_set_const($db, 'SYSLOG_HANDLERS', json_encode($activeModules), 'chaine', 0, '', 0);
136 $error = 0;
137 $errors = [];
138 // Check configuration
139 foreach ($activeModules as $modulename) {
140 $module = new $modulename();
141 '@phan-var-force LogHandler $module';
142 $res = $module->checkConfiguration();
143 if (!$res) {
144 $error++;
145 $errors = array_merge($errors, $module->errors);
146 }
147 }
148
149
150 if (!$error) {
151 $db->commit();
152 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
153 } else {
154 $db->rollback();
155 setEventMessages('', $errors, 'errors');
156 }
157}
158
159// Set level
160if ($action == 'setlevel') {
161 $level = GETPOST("level");
162 $res = dolibarr_set_const($db, "SYSLOG_LEVEL", $level, 'chaine', 0, '', 0);
163 dol_syslog("admin/syslog: level ".$level);
164
165 if (!($res > 0)) {
166 $error++;
167 }
168
169 if (!$error) {
170 $file_saves = GETPOST("file_saves");
171 $res = dolibarr_set_const($db, "SYSLOG_FILE_SAVES", $file_saves, 'chaine', 0, '', 0);
172 dol_syslog("admin/syslog: file saves ".$file_saves);
173
174 if (!($res > 0)) {
175 $error++;
176 }
177 }
178
179 if (!$error) {
180 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
181 } else {
182 setEventMessages($langs->trans("Error"), null, 'errors');
183 }
184}
185
186
187/*
188 * View
189 */
190
191llxHeader('', $langs->trans("SyslogSetup"), '', '', 0, 0, '', '', '', 'mod-admin page-syslog');
192
193$form = new Form($db);
194
195$linkback = '<a href="'.dolBuildUrl(DOL_URL_ROOT.'/admin/modules.php', ['restore_lastsearch_values' => 1]).'">'.img_picto($langs->trans("BackToModuleList"), 'back', 'class="pictofixedwidth"').'<span class="hideonsmartphone">'.$langs->trans("BackToModuleList").'</span></a>';
196
197print load_fiche_titre($langs->trans("SyslogSetup"), $linkback, 'title_setup');
198print '<br>';
199
200$syslogfacility = $defaultsyslogfacility = dolibarr_get_const($db, "SYSLOG_FACILITY", 0);
201$syslogfile = $defaultsyslogfile = dolibarr_get_const($db, "SYSLOG_FILE", 0);
202
203if (!$defaultsyslogfacility) {
204 $defaultsyslogfacility = 'LOG_USER';
205}
206if (!$defaultsyslogfile) {
207 $defaultsyslogfile = 'dolibarr.log';
208}
209$optionmc = '';
210if (isModEnabled('multicompany') && $user->entity) {
211 print '<div class="error">'.$langs->trans("ContactSuperAdminForChange").'</div>';
212 $optionmc = 'disabled';
213}
214
215
216// Output mode
217
218print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
219
220print load_fiche_titre($langs->trans("SyslogOutput"), '', '');
221
222print '<input type="hidden" name="token" value="'.newToken().'">';
223print '<input type="hidden" name="action" value="set">';
224
225print '<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
226print '<table class="noborder centpercent">';
227print '<tr class="liste_titre">';
228print '<td>'.$langs->trans("Type").'</td>';
229print '<td></td>';
230print '<td class="center width150"><input type="submit" class="button small" '.$optionmc.' value="'.$langs->trans("Modify").'"></td>';
231print "</tr>\n";
232
233foreach ($syslogModules as $moduleName) {
234 $module = new $moduleName();
235 '@phan-var-force LogHandler $module';
236
237 $moduleactive = (int) $module->isActive();
238 //print $moduleName." = ".$moduleactive." - ".$module->getName()." ".($moduleactive == -1)."<br>\n";
239 if (($moduleactive == -1) && getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0) {
240 continue; // Some modules are hidden if not activable and not into debug mode (end user must not see them)
241 }
242
243
244 print '<tr class="oddeven">';
245 print '<td class="nowraponall" width="140">';
246 print '<input class="oddeven" type="checkbox" id="syslog_handler_'.$moduleName.'" name="SYSLOG_HANDLERS[]" value="'.$moduleName.'" '.(in_array($moduleName, $activeModules) ? 'checked' : '').($moduleactive <= 0 ? 'disabled' : '').'> ';
247 print '<label for="syslog_handler_'.$moduleName.'">'.$module->getName().'</label>';
248 if ($moduleName == 'mod_syslog_syslog') {
249 if (!$module->isActive()) {
250 $langs->load("errors");
251 print $form->textwithpicto('', $langs->trans("ErrorPHPNeedModule", 'SysLog'));
252 }
253 }
254 print '</td>';
255
256 print '<td class="nowrap">';
257 $setuparray = $module->configure();
258
259 if ($setuparray) {
260 foreach ($setuparray as $option) {
261 $tmpoption = $option['constant'];
262 $value = '';
263 if (!empty($tmpoption)) {
264 if (GETPOSTISSET($tmpoption)) {
265 $value = GETPOST($tmpoption);
266 } else {
267 $value = getDolGlobalString($tmpoption);
268 }
269 } else {
270 $value = (isset($option['default']) ? $option['default'] : '');
271 }
272
273 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'] : '').'>';
274 if (!empty($option['example'])) {
275 print '<br>'.$langs->trans("Example").': '.dol_escape_htmltag($option['example']);
276 }
277
278 if ($option['constant'] == 'SYSLOG_FILE' && preg_match('/^DOL_DATA_ROOT\/[^\/]*$/', $value)) {
279 $filelogparam = ' &nbsp; &nbsp; <a href="'.DOL_URL_ROOT.'/document.php?modulepart=logs&file='.basename($value).'">';
280 $filelogparam .= $langs->trans('Download');
281 $filelogparam .= img_picto($langs->trans('Download').' '.basename($value), 'download', 'class="paddingleft"');
282 $filelogparam .= '</a>';
283 print $filelogparam;
284 }
285 }
286 }
287 print '</td>';
288
289 print '<td class="center">';
290 if ($module->getInfo()) {
291 print $form->textwithpicto('', $module->getInfo(), 1, 'help');
292 }
293 if ($module->getWarning()) {
294 print $form->textwithpicto('', $module->getWarning(), 1, 'warning');
295 }
296 print '</td>';
297 print "</tr>\n";
298}
299
300print "</table>\n";
301print "</div>\n";
302
303print "</form>\n";
304
305
306print '<br>'."\n\n";
307
308
309// Level
310
311print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
312
313print load_fiche_titre($langs->trans("SyslogLevel"), '', '');
314
315print '<input type="hidden" name="token" value="'.newToken().'">';
316print '<input type="hidden" name="action" value="setlevel">';
317
318print '<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
319print '<table class="noborder centpercent">';
320print '<tr class="liste_titre">';
321print '<td>'.$langs->trans("Parameter").'</td><td></td>';
322print '<td class="center width150"><input type="submit" class="button small" '.$optionmc.' value="'.$langs->trans("Modify").'"></td>';
323print "</tr>\n";
324
325print '<tr class="oddeven"><td>'.$langs->trans("SyslogLevel").'</td>';
326print '<td colspan="2"><select class="flat minwidth400" id="level" name="level" '.$optionmc.'>';
327print '<option value="'.LOG_EMERG.'" '.(getDolGlobalString('SYSLOG_LEVEL') == LOG_EMERG ? 'selected' : '').'>LOG_EMERG ('.LOG_EMERG.')</option>';
328print '<option value="'.LOG_ALERT.'" '.(getDolGlobalString('SYSLOG_LEVEL') == LOG_ALERT ? 'selected' : '').'>LOG_ALERT ('.LOG_ALERT.')</option>';
329print '<option value="'.LOG_CRIT.'" '.(getDolGlobalString('SYSLOG_LEVEL') == LOG_CRIT ? 'selected' : '').'>LOG_CRIT ('.LOG_CRIT.')</option>';
330print '<option value="'.LOG_ERR.'" '.(getDolGlobalString('SYSLOG_LEVEL') == LOG_ERR ? 'selected' : '').'>LOG_ERR ('.LOG_ERR.')</option>';
331print '<option value="'.LOG_WARNING.'" '.(getDolGlobalString('SYSLOG_LEVEL') == LOG_WARNING ? 'selected' : '').'>LOG_WARNING ('.LOG_WARNING.')</option>';
332print '<option value="'.LOG_NOTICE.'" '.(getDolGlobalString('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>';
333print '<option value="'.LOG_INFO.'" '.(getDolGlobalString('SYSLOG_LEVEL') == LOG_INFO ? 'selected' : '').'>LOG_INFO ('.LOG_INFO.')</option>';
334print '<option value="'.LOG_DEBUG.'" '.(getDolGlobalString('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>';
335print '</select>';
336
337print ajax_combobox("level");
338print '</td></tr>';
339
340if (!empty($conf->loghandlers['mod_syslog_file']) && isModEnabled('cron')) {
341 print '<tr class="oddeven"><td>'.$langs->trans("SyslogFileNumberOfSaves").'</td>';
342 print '<td colspan="2"><input class="width50" type="number" name="file_saves" placeholder="14" min="0" step="1" value="'.getDolGlobalString('SYSLOG_FILE_SAVES').'" />';
343 print ' &nbsp; <a href="'.dol_buildpath('/cron/list.php', 1).'?search_label=CompressSyslogs&status=-1">'.$langs->trans('ConfigureCleaningCronjobToSetFrequencyOfSaves').'</a></td></tr>';
344}
345
346print '</table>';
347print "</div>\n";
348
349print "</form>\n";
350
351// End of page
352llxFooter();
353$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:475
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
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:73
Class to manage generation of HTML components Only common components must be here.
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, $allowothertags=array())
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.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.