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