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