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