dolibarr  16.0.5
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 require '../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29 
30 global $conf;
31 
32 if (!$user->admin) {
34 }
35 
36 // Load translation files required by the page
37 $langs->loadLangs(array("admin", "other"));
38 
39 $error = 0;
40 $action = GETPOST('action', 'aZ09');
41 
42 $syslogModules = array();
43 $activeModules = array();
44 
45 if (!empty($conf->global->SYSLOG_HANDLERS)) {
46  $activeModules = json_decode($conf->global->SYSLOG_HANDLERS);
47 }
48 
49 $dirsyslogs = array_merge(array('/core/modules/syslog/'), $conf->modules_parts['syslog']);
50 foreach ($dirsyslogs as $reldir) {
51  $dir = dol_buildpath($reldir, 0);
52  $newdir = dol_osencode($dir);
53  if (is_dir($newdir)) {
54  $handle = opendir($newdir);
55 
56  if (is_resource($handle)) {
57  while (($file = readdir($handle)) !== false) {
58  if (substr($file, 0, 11) == 'mod_syslog_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
59  $file = substr($file, 0, dol_strlen($file) - 4);
60 
61  require_once $newdir.$file.'.php';
62 
63  $module = new $file;
64 
65  // Show modules according to features level
66  if ($module->getVersion() == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
67  continue;
68  }
69  if ($module->getVersion() == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
70  continue;
71  }
72 
73  $syslogModules[] = $file;
74  }
75  }
76  closedir($handle);
77  }
78  }
79 }
80 
81 
82 /*
83  * Actions
84  */
85 
86 // Set modes
87 if ($action == 'set') {
88  $db->begin();
89 
90  $newActiveModules = array();
91  $selectedModules = (GETPOSTISSET('SYSLOG_HANDLERS') ? GETPOST('SYSLOG_HANDLERS') : array());
92 
93  // Save options of handler
94  foreach ($syslogModules as $syslogHandler) {
95  if (in_array($syslogHandler, $syslogModules)) {
96  $module = new $syslogHandler;
97 
98  if (in_array($syslogHandler, $selectedModules)) {
99  $newActiveModules[] = $syslogHandler;
100  }
101  foreach ($module->configure() as $option) {
102  if (GETPOSTISSET($option['constant'])) {
103  dolibarr_del_const($db, $option['constant'], -1);
104  dolibarr_set_const($db, $option['constant'], trim(GETPOST($option['constant'])), 'chaine', 0, '', 0);
105  }
106  }
107  }
108  }
109 
110  $activeModules = $newActiveModules;
111 
112  dolibarr_del_const($db, 'SYSLOG_HANDLERS', -1); // To be sure ther is not a setup into another entity
113  dolibarr_set_const($db, 'SYSLOG_HANDLERS', json_encode($activeModules), 'chaine', 0, '', 0);
114 
115  // Check configuration
116  foreach ($activeModules as $modulename) {
120  $module = new $modulename;
121  $error = $module->checkConfiguration();
122  }
123 
124 
125  if (!$error) {
126  $db->commit();
127  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
128  } else {
129  $db->rollback();
130  setEventMessages($error, $errors, 'errors');
131  }
132 }
133 
134 // Set level
135 if ($action == 'setlevel') {
136  $level = GETPOST("level");
137  $res = dolibarr_set_const($db, "SYSLOG_LEVEL", $level, 'chaine', 0, '', 0);
138  dol_syslog("admin/syslog: level ".$level);
139 
140  if (!($res > 0)) {
141  $error++;
142  }
143 
144  if (!$error) {
145  $file_saves = GETPOST("file_saves");
146  $res = dolibarr_set_const($db, "SYSLOG_FILE_SAVES", $file_saves, 'chaine', 0, '', 0);
147  dol_syslog("admin/syslog: file saves ".$file_saves);
148 
149  if (!($res > 0)) {
150  $error++;
151  }
152  }
153 
154  if (!$error) {
155  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
156  } else {
157  setEventMessages($langs->trans("Error"), null, 'errors');
158  }
159 }
160 
161 
162 /*
163  * View
164  */
165 
166 llxHeader('', $langs->trans("SyslogSetup"));
167 
168 $form = new Form($db);
169 
170 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
171 print load_fiche_titre($langs->trans("SyslogSetup"), $linkback, 'title_setup');
172 print '<br>';
173 
174 $syslogfacility = $defaultsyslogfacility = dolibarr_get_const($db, "SYSLOG_FACILITY", 0);
175 $syslogfile = $defaultsyslogfile = dolibarr_get_const($db, "SYSLOG_FILE", 0);
176 
177 if (!$defaultsyslogfacility) {
178  $defaultsyslogfacility = 'LOG_USER';
179 }
180 if (!$defaultsyslogfile) {
181  $defaultsyslogfile = 'dolibarr.log';
182 }
183 $optionmc = '';
184 if (!empty($conf->global->MAIN_MODULE_MULTICOMPANY) && $user->entity) {
185  print '<div class="error">'.$langs->trans("ContactSuperAdminForChange").'</div>';
186  $optionmc = 'disabled';
187 }
188 
189 
190 // Output mode
191 print load_fiche_titre($langs->trans("SyslogOutput"), '', '');
192 
193 // Mode
194 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
195 print '<input type="hidden" name="token" value="'.newToken().'">';
196 print '<input type="hidden" name="action" value="set">';
197 print '<table class="noborder centpercent">';
198 print '<tr class="liste_titre">';
199 print '<td>'.$langs->trans("Type").'</td><td>'.$langs->trans("Value").'</td>';
200 print '<td class="right" colspan="2"><input type="submit" class="button" '.$optionmc.' value="'.$langs->trans("Modify").'"></td>';
201 print "</tr>\n";
202 
203 foreach ($syslogModules as $moduleName) {
204  $module = new $moduleName;
205 
206  $moduleactive = (int) $module->isActive();
207  //print $moduleName." = ".$moduleactive." - ".$module->getName()." ".($moduleactive == -1)."<br>\n";
208  if (($moduleactive == -1) && getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0) {
209  continue; // Some modules are hidden if not activable and not into debug mode (end user must not see them)
210  }
211 
212 
213  print '<tr class="oddeven">';
214  print '<td width="140">';
215  print '<input class="oddeven" type="checkbox" name="SYSLOG_HANDLERS[]" value="'.$moduleName.'" '.(in_array($moduleName, $activeModules) ? 'checked' : '').($moduleactive <= 0 ? 'disabled' : '').'> ';
216  print $module->getName();
217  if ($moduleName == 'mod_syslog_syslog') {
218  if (!$module->isActive()) {
219  $langs->load("errors");
220  print $form->textwithpicto('', $langs->trans("ErrorPHPNeedModule", 'SysLog'));
221  }
222  }
223  print '</td>';
224 
225  print '<td class="nowrap">';
226  $setuparray = $module->configure();
227  if ($setuparray) {
228  foreach ($setuparray as $option) {
229  $tmpoption = $option['constant'];
230  if (!empty($tmpoption)) {
231  if (GETPOSTISSET($tmpoption)) {
232  $value = GETPOST($tmpoption);
233  } elseif (!empty($conf->global->$tmpoption)) {
234  $value = $conf->global->$tmpoption;
235  }
236  } else {
237  $value = (isset($option['default']) ? $option['default'] : '');
238  }
239 
240  print $option['name'].': <input type="text" class="flat" name="'.$option['constant'].'" value="'.$value.'"'.(isset($option['attr']) ? ' '.$option['attr'] : '').'>';
241  if (!empty($option['example'])) {
242  print '<br>'.$langs->trans("Example").': '.$option['example'];
243  }
244 
245  if ($option['constant'] == 'SYSLOG_FILE' && preg_match('/^DOL_DATA_ROOT\/[^\/]*$/', $value)) {
246  $filelogparam = ' (<a href="'.DOL_URL_ROOT.'/document.php?modulepart=logs&file='.basename($value).'">';
247  $filelogparam .= $langs->trans('Download');
248  $filelogparam .= ' '.basename($value).'</a>)';
249  print $filelogparam;
250  }
251  }
252  }
253  print '</td>';
254 
255  print '<td class="left">';
256  if ($module->getInfo()) {
257  print $form->textwithpicto('', $module->getInfo(), 1, 'help');
258  }
259  if ($module->getWarning()) {
260  print $form->textwithpicto('', $module->getWarning(), 1, 'warning');
261  }
262  print '</td>';
263  print "</tr>\n";
264 }
265 
266 print "</table>\n";
267 print "</form>\n";
268 
269 print '<br>'."\n\n";
270 
271 print load_fiche_titre($langs->trans("SyslogLevel"), '', '');
272 
273 // Level
274 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
275 print '<input type="hidden" name="token" value="'.newToken().'">';
276 print '<input type="hidden" name="action" value="setlevel">';
277 print '<table class="noborder centpercent">';
278 print '<tr class="liste_titre">';
279 print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
280 print '<td class="right"><input type="submit" class="button" '.$optionmc.' value="'.$langs->trans("Modify").'"></td>';
281 print "</tr>\n";
282 
283 print '<tr class="oddeven"><td width="140">'.$langs->trans("SyslogLevel").'</td>';
284 print '<td colspan="2"><select class="flat" name="level" '.$optionmc.'>';
285 print '<option value="'.LOG_EMERG.'" '.($conf->global->SYSLOG_LEVEL == LOG_EMERG ? 'SELECTED' : '').'>LOG_EMERG ('.LOG_EMERG.')</option>';
286 print '<option value="'.LOG_ALERT.'" '.($conf->global->SYSLOG_LEVEL == LOG_ALERT ? 'SELECTED' : '').'>LOG_ALERT ('.LOG_ALERT.')</option>';
287 print '<option value="'.LOG_CRIT.'" '.($conf->global->SYSLOG_LEVEL == LOG_CRIT ? 'SELECTED' : '').'>LOG_CRIT ('.LOG_CRIT.')</option>';
288 print '<option value="'.LOG_ERR.'" '.($conf->global->SYSLOG_LEVEL == LOG_ERR ? 'SELECTED' : '').'>LOG_ERR ('.LOG_ERR.')</option>';
289 print '<option value="'.LOG_WARNING.'" '.($conf->global->SYSLOG_LEVEL == LOG_WARNING ? 'SELECTED' : '').'>LOG_WARNING ('.LOG_WARNING.')</option>';
290 print '<option value="'.LOG_NOTICE.'" '.($conf->global->SYSLOG_LEVEL == LOG_NOTICE ? 'SELECTED' : '').'>LOG_NOTICE ('.LOG_NOTICE.')</option>';
291 print '<option value="'.LOG_INFO.'" '.($conf->global->SYSLOG_LEVEL == LOG_INFO ? 'SELECTED' : '').'>LOG_INFO ('.LOG_INFO.')</option>';
292 print '<option value="'.LOG_DEBUG.'" '.($conf->global->SYSLOG_LEVEL >= LOG_DEBUG ? 'SELECTED' : '').'>LOG_DEBUG ('.LOG_DEBUG.')</option>';
293 print '</select>';
294 print '</td></tr>';
295 
296 if (!empty($conf->loghandlers['mod_syslog_file']) && !empty($conf->cron->enabled)) {
297  print '<tr class="oddeven"><td width="140">'.$langs->trans("SyslogFileNumberOfSaves").'</td>';
298  print '<td colspan="2"><input type="number" name="file_saves" placeholder="14" min="0" step="1" value="'.getDolGlobalString('SYSLOG_FILE_SAVES').'" />';
299  print ' (<a href="'.dol_buildpath('/cron/list.php', 1).'?search_label=CompressSyslogs&status=-1">'.$langs->trans('ConfigureCleaningCronjobToSetFrequencyOfSaves').'</a>)</td></tr>';
300 }
301 
302 print '</table>';
303 print "</form>\n";
304 
305 // End of page
306 llxFooter();
307 $db->close();
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
dolibarr_del_const
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
Definition: admin.lib.php:552
dol_osencode
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Definition: functions.lib.php:8499
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
dolibarr_get_const
dolibarr_get_const($db, $name, $entity=1)
Get the value of a setup constant from database.
Definition: admin.lib.php:592
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
dolibarr_set_const
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).
Definition: admin.lib.php:627
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:93
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59