dolibarr  19.0.0-dev
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) {
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
133 if ($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 
164 llxHeader('', $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>';
169 print load_fiche_titre($langs->trans("SyslogSetup"), $linkback, 'title_setup');
170 print '<br>';
171 
172 $syslogfacility = $defaultsyslogfacility = dolibarr_get_const($db, "SYSLOG_FACILITY", 0);
173 $syslogfile = $defaultsyslogfile = dolibarr_get_const($db, "SYSLOG_FILE", 0);
174 
175 if (!$defaultsyslogfacility) {
176  $defaultsyslogfacility = 'LOG_USER';
177 }
178 if (!$defaultsyslogfile) {
179  $defaultsyslogfile = 'dolibarr.log';
180 }
181 $optionmc = '';
182 if (!empty($conf->global->MAIN_MODULE_MULTICOMPANY) && $user->entity) {
183  print '<div class="error">'.$langs->trans("ContactSuperAdminForChange").'</div>';
184  $optionmc = 'disabled';
185 }
186 
187 
188 // Output mode
189 print load_fiche_titre($langs->trans("SyslogOutput"), '', '');
190 
191 // Mode
192 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
193 print '<input type="hidden" name="token" value="'.newToken().'">';
194 print '<input type="hidden" name="action" value="set">';
195 print '<table class="noborder centpercent">';
196 print '<tr class="liste_titre">';
197 print '<td>'.$langs->trans("Type").'</td><td>'.$langs->trans("Value").'</td>';
198 print '<td class="right" colspan="2"><input type="submit" class="button" '.$optionmc.' value="'.$langs->trans("Modify").'"></td>';
199 print "</tr>\n";
200 
201 foreach ($syslogModules as $moduleName) {
202  $module = new $moduleName;
203 
204  $moduleactive = (int) $module->isActive();
205  //print $moduleName." = ".$moduleactive." - ".$module->getName()." ".($moduleactive == -1)."<br>\n";
206  if (($moduleactive == -1) && getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0) {
207  continue; // Some modules are hidden if not activable and not into debug mode (end user must not see them)
208  }
209 
210 
211  print '<tr class="oddeven">';
212  print '<td width="140">';
213  print '<input class="oddeven" type="checkbox" name="SYSLOG_HANDLERS[]" value="'.$moduleName.'" '.(in_array($moduleName, $activeModules) ? 'checked' : '').($moduleactive <= 0 ? 'disabled' : '').'> ';
214  print $module->getName();
215  if ($moduleName == 'mod_syslog_syslog') {
216  if (!$module->isActive()) {
217  $langs->load("errors");
218  print $form->textwithpicto('', $langs->trans("ErrorPHPNeedModule", 'SysLog'));
219  }
220  }
221  print '</td>';
222 
223  print '<td class="nowrap">';
224  $setuparray = $module->configure();
225  if ($setuparray) {
226  foreach ($setuparray as $option) {
227  $tmpoption = $option['constant'];
228  if (!empty($tmpoption)) {
229  if (GETPOSTISSET($tmpoption)) {
230  $value = GETPOST($tmpoption);
231  } elseif (!empty($conf->global->$tmpoption)) {
232  $value = $conf->global->$tmpoption;
233  }
234  } else {
235  $value = (isset($option['default']) ? $option['default'] : '');
236  }
237 
238  print $option['name'].': <input type="text" class="flat" name="'.$option['constant'].'" value="'.$value.'"'.(isset($option['attr']) ? ' '.$option['attr'] : '').'>';
239  if (!empty($option['example'])) {
240  print '<br>'.$langs->trans("Example").': '.$option['example'];
241  }
242 
243  if ($option['constant'] == 'SYSLOG_FILE' && preg_match('/^DOL_DATA_ROOT\/[^\/]*$/', $value)) {
244  $filelogparam = ' (<a href="'.DOL_URL_ROOT.'/document.php?modulepart=logs&file='.basename($value).'">';
245  $filelogparam .= $langs->trans('Download');
246  $filelogparam .= ' '.basename($value).'</a>)';
247  print $filelogparam;
248  }
249  }
250  }
251  print '</td>';
252 
253  print '<td class="left">';
254  if ($module->getInfo()) {
255  print $form->textwithpicto('', $module->getInfo(), 1, 'help');
256  }
257  if ($module->getWarning()) {
258  print $form->textwithpicto('', $module->getWarning(), 1, 'warning');
259  }
260  print '</td>';
261  print "</tr>\n";
262 }
263 
264 print "</table>\n";
265 print "</form>\n";
266 
267 print '<br>'."\n\n";
268 
269 print load_fiche_titre($langs->trans("SyslogLevel"), '', '');
270 
271 // Level
272 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
273 print '<input type="hidden" name="token" value="'.newToken().'">';
274 print '<input type="hidden" name="action" value="setlevel">';
275 print '<table class="noborder centpercent">';
276 print '<tr class="liste_titre">';
277 print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
278 print '<td class="right"><input type="submit" class="button" '.$optionmc.' value="'.$langs->trans("Modify").'"></td>';
279 print "</tr>\n";
280 
281 print '<tr class="oddeven"><td width="140">'.$langs->trans("SyslogLevel").'</td>';
282 print '<td colspan="2"><select class="flat" name="level" '.$optionmc.'>';
283 print '<option value="'.LOG_EMERG.'" '.($conf->global->SYSLOG_LEVEL == LOG_EMERG ? 'SELECTED' : '').'>LOG_EMERG ('.LOG_EMERG.')</option>';
284 print '<option value="'.LOG_ALERT.'" '.($conf->global->SYSLOG_LEVEL == LOG_ALERT ? 'SELECTED' : '').'>LOG_ALERT ('.LOG_ALERT.')</option>';
285 print '<option value="'.LOG_CRIT.'" '.($conf->global->SYSLOG_LEVEL == LOG_CRIT ? 'SELECTED' : '').'>LOG_CRIT ('.LOG_CRIT.')</option>';
286 print '<option value="'.LOG_ERR.'" '.($conf->global->SYSLOG_LEVEL == LOG_ERR ? 'SELECTED' : '').'>LOG_ERR ('.LOG_ERR.')</option>';
287 print '<option value="'.LOG_WARNING.'" '.($conf->global->SYSLOG_LEVEL == LOG_WARNING ? 'SELECTED' : '').'>LOG_WARNING ('.LOG_WARNING.')</option>';
288 print '<option value="'.LOG_NOTICE.'" '.($conf->global->SYSLOG_LEVEL == LOG_NOTICE ? 'SELECTED' : '').'>LOG_NOTICE ('.LOG_NOTICE.')</option>';
289 print '<option value="'.LOG_INFO.'" '.($conf->global->SYSLOG_LEVEL == LOG_INFO ? 'SELECTED' : '').'>LOG_INFO ('.LOG_INFO.')</option>';
290 print '<option value="'.LOG_DEBUG.'" '.($conf->global->SYSLOG_LEVEL >= LOG_DEBUG ? 'SELECTED' : '').'>LOG_DEBUG ('.LOG_DEBUG.')</option>';
291 print '</select>';
292 print '</td></tr>';
293 
294 if (!empty($conf->loghandlers['mod_syslog_file']) && isModEnabled('cron')) {
295  print '<tr class="oddeven"><td width="140">'.$langs->trans("SyslogFileNumberOfSaves").'</td>';
296  print '<td colspan="2"><input type="number" name="file_saves" placeholder="14" min="0" step="1" value="'.getDolGlobalString('SYSLOG_FILE_SAVES').'" />';
297  print ' (<a href="'.dol_buildpath('/cron/list.php', 1).'?search_label=CompressSyslogs&status=-1">'.$langs->trans('ConfigureCleaningCronjobToSetFrequencyOfSaves').'</a>)</td></tr>';
298 }
299 
300 print '</table>';
301 print "</form>\n";
302 
303 // End of page
304 llxFooter();
305 $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:638
dolibarr_del_const($db, $name, $entity=1)
Delete a constant.
Definition: admin.lib.php:562
dolibarr_get_const($db, $name, $entity=1)
Get the value of a setup constant from database.
Definition: admin.lib.php:602
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.
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.
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.
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.