dolibarr  9.0.0
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 <http://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 
54  while (($file = readdir($handle)) !== false) {
55  if (substr($file, 0, 11) == 'mod_syslog_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
56  $file = substr($file, 0, dol_strlen($file) - 4);
57 
58  require_once $newdir . $file . '.php';
59 
60  $module = new $file;
61 
62  // Show modules according to features level
63  if ($module->getVersion() == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
64  continue;
65  }
66  if ($module->getVersion() == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
67  continue;
68  }
69 
70  $syslogModules[] = $file;
71  }
72  }
73  closedir($handle);
74  }
75  }
76 }
77 
78 
79 /*
80  * Actions
81  */
82 
83 // Set modes
84 if ($action == 'set')
85 {
86  $db->begin();
87 
88  $newActiveModules = array();
89  $selectedModules = (isset($_POST['SYSLOG_HANDLERS']) ? $_POST['SYSLOG_HANDLERS'] : array());
90 
91  // Save options of handler
92  foreach ($syslogModules as $syslogHandler)
93  {
94  if (in_array($syslogHandler, $syslogModules))
95  {
96  $module = new $syslogHandler;
97 
98  if (in_array($syslogHandler, $selectedModules)) $newActiveModules[] = $syslogHandler;
99  foreach ($module->configure() as $option)
100  {
101  if (isset($_POST[$option['constant']]))
102  {
103  $_POST[$option['constant']] = trim($_POST[$option['constant']]);
104  dolibarr_del_const($db, $option['constant'], -1);
105  dolibarr_set_const($db, $option['constant'], $_POST[$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  {
128  $db->commit();
129  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
130  }
131  else
132  {
133  $db->rollback();
134  setEventMessages($error, $errors, 'errors');
135  }
136 }
137 
138 // Set level
139 if ($action == 'setlevel')
140 {
141  $level = GETPOST("level");
142  $res = dolibarr_set_const($db,"SYSLOG_LEVEL",$level,'chaine',0,'',0);
143  dol_syslog("admin/syslog: level ".$level);
144 
145  if (! $res > 0) $error++;
146 
147  if (! $error)
148  {
149  $file_saves = GETPOST("file_saves");
150  $res = dolibarr_set_const($db,"SYSLOG_FILE_SAVES",$file_saves,'chaine',0,'',0);
151  dol_syslog("admin/syslog: file saves ".$file_saves);
152 
153  if (! $res > 0) $error++;
154  }
155 
156  if (! $error)
157  {
158  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
159  }
160  else
161  {
162  setEventMessages($langs->trans("Error"), null, 'errors');
163  }
164 }
165 
166 /*
167  * View
168  */
169 
170 llxHeader();
171 
172 $form=new Form($db);
173 
174 $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
175 print load_fiche_titre($langs->trans("SyslogSetup"),$linkback,'title_setup');
176 print '<br>';
177 
178 $def = array();
179 
180 $syslogfacility=$defaultsyslogfacility=dolibarr_get_const($db,"SYSLOG_FACILITY",0);
181 $syslogfile=$defaultsyslogfile=dolibarr_get_const($db,"SYSLOG_FILE",0);
182 
183 if (! $defaultsyslogfacility) $defaultsyslogfacility='LOG_USER';
184 if (! $defaultsyslogfile) $defaultsyslogfile='dolibarr.log';
185 
186 if ($conf->global->MAIN_MODULE_MULTICOMPANY && $user->entity)
187 {
188  print '<div class="error">'.$langs->trans("ContactSuperAdminForChange").'</div>';
189  $option = 'disabled';
190 }
191 
192 
193 //print "conf->global->MAIN_FEATURES_LEVEL = ".$conf->global->MAIN_FEATURES_LEVEL."<br><br>\n";
194 
195 // Output mode
196 print load_fiche_titre($langs->trans("SyslogOutput"));
197 
198 // Mode
199 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
200 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
201 print '<input type="hidden" name="action" value="set">';
202 print '<table class="noborder" width="100%">';
203 print '<tr class="liste_titre">';
204 print '<td>'.$langs->trans("Type").'</td><td>'.$langs->trans("Value").'</td>';
205 print '<td align="right" colspan="2"><input type="submit" class="button" '.$option.' value="'.$langs->trans("Modify").'"></td>';
206 print "</tr>\n";
207 
208 foreach ($syslogModules as $moduleName)
209 {
210  $module = new $moduleName;
211 
212  $moduleactive=(int) $module->isActive();
213  //print $moduleName." = ".$moduleactive." - ".$module->getName()." ".($moduleactive == -1)."<br>\n";
214  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)
215 
216 
217  print '<tr class="oddeven">';
218  print '<td width="140">';
219  print '<input class="oddeven" type="checkbox" name="SYSLOG_HANDLERS[]" value="'.$moduleName.'" '.(in_array($moduleName, $activeModules) ? 'checked' : '').($moduleactive <= 0 ? 'disabled' : '').'> ';
220  print $module->getName();
221  print '</td>';
222 
223  print '<td class="nowrap">';
224  $setuparray=$module->configure();
225  if ($setuparray)
226  {
227  foreach ($setuparray as $option)
228  {
229  $tmpoption=$option['constant'];
230  if (! empty($tmpoption))
231  {
232  if (isset($_POST[$tmpoption])) $value=$_POST[$tmpoption];
233  else if (! empty($conf->global->$tmpoption)) $value = $conf->global->$tmpoption;
234  }
235  else $value = (isset($option['default']) ? $option['default'] : '');
236 
237  print $option['name'].': <input type="text" class="flat" name="'.$option['constant'].'" value="'.$value.'"'.(isset($option['attr']) ? ' '.$option['attr'] : '').'>';
238  if (! empty($option['example'])) print '<br>'.$langs->trans("Example").': '.$option['example'];
239 
240  if ($option['constant'] == 'SYSLOG_FILE' && preg_match('/^DOL_DATA_ROOT\/[^\/]*$/',$value))
241  {
242  $filelogparam =' (<a href="'.DOL_URL_ROOT.'/document.php?modulepart=logs&file='.basename($value).'">';
243  $filelogparam.=$langs->trans('Download');
244  $filelogparam.=$filelog.'</a>)';
245  print $filelogparam;
246  }
247  }
248  }
249  print '</td>';
250 
251  print '<td align="left">';
252  if ($module->getInfo())
253  {
254  print $form->textwithpicto('', $module->getInfo(), 1, 'help');
255  }
256  if ($module->getWarning())
257  {
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="syslog.php" method="post">';
273 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
274 print '<input type="hidden" name="action" value="setlevel">';
275 print '<table class="noborder" width="100%">';
276 print '<tr class="liste_titre">';
277 print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
278 print '<td align="right"><input type="submit" class="button" '.$option.' 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" '.$option.'>';
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']) && ! empty($conf->cron->enabled)) {
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="'.$conf->global->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();
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dolibarr_get_const($db, $name, $entity=1)
Recupere une constante depuis la base de donnees.
Definition: admin.lib.php:475
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:510
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
dolibarr_del_const($db, $name, $entity=1)
Effacement d&#39;une constante dans la base de donnees.
Definition: admin.lib.php:435
llxHeader()
Empty header.
Definition: wrapper.php:44
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.