dolibarr  17.0.4
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
28 require '../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
30 
31 global $conf;
32 
33 if (!$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 
46 if (!empty($conf->global->SYSLOG_HANDLERS)) {
47  $activeModules = json_decode($conf->global->SYSLOG_HANDLERS);
48 }
49 
50 $dirsyslogs = array_merge(array('/core/modules/syslog/'), $conf->modules_parts['syslog']);
51 foreach ($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' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
68  continue;
69  }
70  if ($module->getVersion() == 'experimental' && $conf->global->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
88 if ($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) {
121  $module = new $modulename;
122  $error = $module->checkConfiguration();
123  }
124 
125 
126  if (!$error) {
127  $db->commit();
128  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
129  } else {
130  $db->rollback();
131  setEventMessages($error, $errors, 'errors');
132  }
133 }
134 
135 // Set level
136 if ($action == 'setlevel') {
137  $level = GETPOST("level");
138  $res = dolibarr_set_const($db, "SYSLOG_LEVEL", $level, 'chaine', 0, '', 0);
139  dol_syslog("admin/syslog: level ".$level);
140 
141  if (!($res > 0)) {
142  $error++;
143  }
144 
145  if (!$error) {
146  $file_saves = GETPOST("file_saves");
147  $res = dolibarr_set_const($db, "SYSLOG_FILE_SAVES", $file_saves, 'chaine', 0, '', 0);
148  dol_syslog("admin/syslog: file saves ".$file_saves);
149 
150  if (!($res > 0)) {
151  $error++;
152  }
153  }
154 
155  if (!$error) {
156  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
157  } else {
158  setEventMessages($langs->trans("Error"), null, 'errors');
159  }
160 }
161 
162 
163 /*
164  * View
165  */
166 
167 llxHeader('', $langs->trans("SyslogSetup"));
168 
169 $form = new Form($db);
170 
171 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
172 print load_fiche_titre($langs->trans("SyslogSetup"), $linkback, 'title_setup');
173 print '<br>';
174 
175 $syslogfacility = $defaultsyslogfacility = dolibarr_get_const($db, "SYSLOG_FACILITY", 0);
176 $syslogfile = $defaultsyslogfile = dolibarr_get_const($db, "SYSLOG_FILE", 0);
177 
178 if (!$defaultsyslogfacility) {
179  $defaultsyslogfacility = 'LOG_USER';
180 }
181 if (!$defaultsyslogfile) {
182  $defaultsyslogfile = 'dolibarr.log';
183 }
184 $optionmc = '';
185 if (!empty($conf->global->MAIN_MODULE_MULTICOMPANY) && $user->entity) {
186  print '<div class="error">'.$langs->trans("ContactSuperAdminForChange").'</div>';
187  $optionmc = 'disabled';
188 }
189 
190 
191 // Output mode
192 print load_fiche_titre($langs->trans("SyslogOutput"), '', '');
193 
194 // Mode
195 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
196 print '<input type="hidden" name="token" value="'.newToken().'">';
197 print '<input type="hidden" name="action" value="set">';
198 print '<table class="noborder centpercent">';
199 print '<tr class="liste_titre">';
200 print '<td>'.$langs->trans("Type").'</td><td>'.$langs->trans("Value").'</td>';
201 print '<td class="right" colspan="2"><input type="submit" class="button" '.$optionmc.' value="'.$langs->trans("Modify").'"></td>';
202 print "</tr>\n";
203 
204 foreach ($syslogModules as $moduleName) {
205  $module = new $moduleName;
206 
207  $moduleactive = (int) $module->isActive();
208  //print $moduleName." = ".$moduleactive." - ".$module->getName()." ".($moduleactive == -1)."<br>\n";
209  if (($moduleactive == -1) && getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0) {
210  continue; // Some modules are hidden if not activable and not into debug mode (end user must not see them)
211  }
212 
213 
214  print '<tr class="oddeven">';
215  print '<td width="140">';
216  print '<input class="oddeven" type="checkbox" name="SYSLOG_HANDLERS[]" value="'.$moduleName.'" '.(in_array($moduleName, $activeModules) ? 'checked' : '').($moduleactive <= 0 ? 'disabled' : '').'> ';
217  print $module->getName();
218  if ($moduleName == 'mod_syslog_syslog') {
219  if (!$module->isActive()) {
220  $langs->load("errors");
221  print $form->textwithpicto('', $langs->trans("ErrorPHPNeedModule", 'SysLog'));
222  }
223  }
224  print '</td>';
225 
226  print '<td class="nowrap">';
227  $setuparray = $module->configure();
228  if ($setuparray) {
229  foreach ($setuparray as $option) {
230  $tmpoption = $option['constant'];
231  if (!empty($tmpoption)) {
232  if (GETPOSTISSET($tmpoption)) {
233  $value = GETPOST($tmpoption);
234  } elseif (!empty($conf->global->$tmpoption)) {
235  $value = $conf->global->$tmpoption;
236  }
237  } else {
238  $value = (isset($option['default']) ? $option['default'] : '');
239  }
240 
241  print $option['name'].': <input type="text" class="flat" name="'.$option['constant'].'" value="'.$value.'"'.(isset($option['attr']) ? ' '.$option['attr'] : '').'>';
242  if (!empty($option['example'])) {
243  print '<br>'.$langs->trans("Example").': '.$option['example'];
244  }
245 
246  if ($option['constant'] == 'SYSLOG_FILE' && preg_match('/^DOL_DATA_ROOT\/[^\/]*$/', $value)) {
247  $filelogparam = ' (<a href="'.DOL_URL_ROOT.'/document.php?modulepart=logs&file='.basename($value).'">';
248  $filelogparam .= $langs->trans('Download');
249  $filelogparam .= ' '.basename($value).'</a>)';
250  print $filelogparam;
251  }
252  }
253  }
254  print '</td>';
255 
256  print '<td class="left">';
257  if ($module->getInfo()) {
258  print $form->textwithpicto('', $module->getInfo(), 1, 'help');
259  }
260  if ($module->getWarning()) {
261  print $form->textwithpicto('', $module->getWarning(), 1, 'warning');
262  }
263  print '</td>';
264  print "</tr>\n";
265 }
266 
267 print "</table>\n";
268 print "</form>\n";
269 
270 print '<br>'."\n\n";
271 
272 print load_fiche_titre($langs->trans("SyslogLevel"), '', '');
273 
274 // Level
275 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
276 print '<input type="hidden" name="token" value="'.newToken().'">';
277 print '<input type="hidden" name="action" value="setlevel">';
278 print '<table class="noborder centpercent">';
279 print '<tr class="liste_titre">';
280 print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
281 print '<td class="right"><input type="submit" class="button" '.$optionmc.' value="'.$langs->trans("Modify").'"></td>';
282 print "</tr>\n";
283 
284 print '<tr class="oddeven"><td width="140">'.$langs->trans("SyslogLevel").'</td>';
285 print '<td colspan="2"><select class="flat" name="level" '.$optionmc.'>';
286 print '<option value="'.LOG_EMERG.'" '.($conf->global->SYSLOG_LEVEL == LOG_EMERG ? 'SELECTED' : '').'>LOG_EMERG ('.LOG_EMERG.')</option>';
287 print '<option value="'.LOG_ALERT.'" '.($conf->global->SYSLOG_LEVEL == LOG_ALERT ? 'SELECTED' : '').'>LOG_ALERT ('.LOG_ALERT.')</option>';
288 print '<option value="'.LOG_CRIT.'" '.($conf->global->SYSLOG_LEVEL == LOG_CRIT ? 'SELECTED' : '').'>LOG_CRIT ('.LOG_CRIT.')</option>';
289 print '<option value="'.LOG_ERR.'" '.($conf->global->SYSLOG_LEVEL == LOG_ERR ? 'SELECTED' : '').'>LOG_ERR ('.LOG_ERR.')</option>';
290 print '<option value="'.LOG_WARNING.'" '.($conf->global->SYSLOG_LEVEL == LOG_WARNING ? 'SELECTED' : '').'>LOG_WARNING ('.LOG_WARNING.')</option>';
291 print '<option value="'.LOG_NOTICE.'" '.($conf->global->SYSLOG_LEVEL == LOG_NOTICE ? 'SELECTED' : '').'>LOG_NOTICE ('.LOG_NOTICE.')</option>';
292 print '<option value="'.LOG_INFO.'" '.($conf->global->SYSLOG_LEVEL == LOG_INFO ? 'SELECTED' : '').'>LOG_INFO ('.LOG_INFO.')</option>';
293 print '<option value="'.LOG_DEBUG.'" '.($conf->global->SYSLOG_LEVEL >= LOG_DEBUG ? 'SELECTED' : '').'>LOG_DEBUG ('.LOG_DEBUG.')</option>';
294 print '</select>';
295 print '</td></tr>';
296 
297 if (!empty($conf->loghandlers['mod_syslog_file']) && isModEnabled('cron')) {
298  print '<tr class="oddeven"><td width="140">'.$langs->trans("SyslogFileNumberOfSaves").'</td>';
299  print '<td colspan="2"><input type="number" name="file_saves" placeholder="14" min="0" step="1" value="'.getDolGlobalString('SYSLOG_FILE_SAVES').'" />';
300  print ' (<a href="'.dol_buildpath('/cron/list.php', 1).'?search_label=CompressSyslogs&status=-1">'.$langs->trans('ConfigureCleaningCronjobToSetFrequencyOfSaves').'</a>)</td></tr>';
301 }
302 
303 print '</table>';
304 print "</form>\n";
305 
306 // End of page
307 llxFooter();
308 $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).
Definition: admin.lib.php:632
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
Definition: admin.lib.php:556
dolibarr_get_const($db, $name, $entity=1)
Get the value of a setup constant from database.
Definition: admin.lib.php:596
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
Class to manage generation of HTML components Only common components must be here.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return 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.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.