dolibarr  7.0.0-beta
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@capnetworks.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 $langs->load("admin");
35 $langs->load("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  $var = true;
54 
55  while (($file = readdir($handle)) !== false) {
56  if (substr($file, 0, 11) == 'mod_syslog_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
57  $file = substr($file, 0, dol_strlen($file) - 4);
58 
59  require_once $newdir . $file . '.php';
60 
61  $module = new $file;
62 
63  // Show modules according to features level
64  if ($module->getVersion() == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
65  continue;
66  }
67  if ($module->getVersion() == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
68  continue;
69  }
70 
71  $syslogModules[] = $file;
72  }
73  }
74  closedir($handle);
75  }
76  }
77 }
78 
79 
80 /*
81  * Actions
82  */
83 
84 // Set modes
85 if ($action == 'set')
86 {
87  $db->begin();
88 
89  $newActiveModules = array();
90  $selectedModules = (isset($_POST['SYSLOG_HANDLERS']) ? $_POST['SYSLOG_HANDLERS'] : array());
91 
92  // Save options of handler
93  foreach ($syslogModules as $syslogHandler)
94  {
95  if (in_array($syslogHandler, $syslogModules))
96  {
97  $module = new $syslogHandler;
98 
99  if (in_array($syslogHandler, $selectedModules)) $newActiveModules[] = $syslogHandler;
100  foreach ($module->configure() as $option)
101  {
102  if (isset($_POST[$option['constant']]))
103  {
104  $_POST[$option['constant']] = trim($_POST[$option['constant']]);
105  dolibarr_del_const($db, $option['constant'], -1);
106  dolibarr_set_const($db, $option['constant'], $_POST[$option['constant']], 'chaine',0, '', 0);
107  }
108  }
109  }
110  }
111 
112  $activeModules = $newActiveModules;
113 
114  dolibarr_del_const($db, 'SYSLOG_HANDLERS', -1); // To be sure ther is not a setup into another entity
115  dolibarr_set_const($db, 'SYSLOG_HANDLERS', json_encode($activeModules), 'chaine',0,'',0);
116 
117  // Check configuration
118  foreach ($activeModules as $modulename) {
122  $module = new $modulename;
123  $error = $module->checkConfiguration();
124  }
125 
126 
127  if (! $error)
128  {
129  $db->commit();
130  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
131  }
132  else
133  {
134  $db->rollback();
135  setEventMessages($error, $errors, 'errors');
136 
137  }
138 
139 }
140 
141 // Set level
142 if ($action == 'setlevel')
143 {
144  $level = GETPOST("level");
145  $res = dolibarr_set_const($db,"SYSLOG_LEVEL",$level,'chaine',0,'',0);
146  dol_syslog("admin/syslog: level ".$level);
147 
148  if (! $res > 0) $error++;
149  if (! $error)
150  {
151  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
152  }
153  else
154  {
155  setEventMessages($langs->trans("Error"), null, 'errors');
156  }
157 }
158 
159 /*
160  * View
161  */
162 
163 llxHeader();
164 
165 $form=new Form($db);
166 
167 $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
168 print load_fiche_titre($langs->trans("SyslogSetup"),$linkback,'title_setup');
169 print '<br>';
170 
171 $def = array();
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="'.$_SESSION['newtoken'].'">';
194 print '<input type="hidden" name="action" value="set">';
195 print '<table class="noborder" width="100%">';
196 print '<tr class="liste_titre">';
197 print '<td>'.$langs->trans("Type").'</td><td>'.$langs->trans("Value").'</td>';
198 print '<td align="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  print '</td>';
215 
216  print '<td class="nowrap">';
217  $setuparray=$module->configure();
218  if ($setuparray)
219  {
220  foreach ($setuparray as $option)
221  {
222  $tmpoption=$option['constant'];
223  if (! empty($tmpoption))
224  {
225  if (isset($_POST[$tmpoption])) $value=$_POST[$tmpoption];
226  else if (! empty($conf->global->$tmpoption)) $value = $conf->global->$tmpoption;
227  }
228  else $value = (isset($option['default']) ? $option['default'] : '');
229 
230  print $option['name'].': <input type="text" class="flat" name="'.$option['constant'].'" value="'.$value.'"'.(isset($option['attr']) ? ' '.$option['attr'] : '').'>';
231  if (! empty($option['example'])) print '<br>'.$langs->trans("Example").': '.$option['example'];
232 
233  if ($option['constant'] == 'SYSLOG_FILE' && preg_match('/^DOL_DATA_ROOT\/[^\/]*$/',$value))
234  {
235  $filelogparam =' (<a href="'.DOL_URL_ROOT.'/document.php?modulepart=logs&file='.basename($value).'">';
236  $filelogparam.=$langs->trans('Download');
237  $filelogparam.=$filelog.'</a>)';
238  print $filelogparam;
239  }
240  }
241  }
242  print '</td>';
243 
244  print '<td align="left">';
245  if ($module->getInfo())
246  {
247  print $form->textwithpicto('', $module->getInfo(), 1, 'help');
248  }
249  if ($module->getWarning())
250  {
251  print $form->textwithpicto('', $module->getWarning(), 1, 'warning');
252  }
253  print '</td>';
254  print "</tr>\n";
255 }
256 
257 print "</table>\n";
258 print "</form>\n";
259 
260 print '<br>'."\n\n";
261 
262 print load_fiche_titre($langs->trans("SyslogLevel"));
263 
264 // Level
265 print '<form action="syslog.php" method="post">';
266 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
267 print '<input type="hidden" name="action" value="setlevel">';
268 print '<table class="noborder" width="100%">';
269 print '<tr class="liste_titre">';
270 print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
271 print '<td align="right"><input type="submit" class="button" '.$option.' value="'.$langs->trans("Modify").'"></td>';
272 print "</tr>\n";
273 $var=true;
274 
275 print '<tr class="oddeven"><td width="140">'.$langs->trans("SyslogLevel").'</td>';
276 print '<td colspan="2"><select class="flat" name="level" '.$option.'>';
277 print '<option value="'.LOG_EMERG.'" '.($conf->global->SYSLOG_LEVEL==LOG_EMERG?'SELECTED':'').'>LOG_EMERG ('.LOG_EMERG.')</option>';
278 print '<option value="'.LOG_ALERT.'" '.($conf->global->SYSLOG_LEVEL==LOG_ALERT?'SELECTED':'').'>LOG_ALERT ('.LOG_ALERT.')</option>';
279 print '<option value="'.LOG_CRIT.'" '.($conf->global->SYSLOG_LEVEL==LOG_CRIT?'SELECTED':'').'>LOG_CRIT ('.LOG_CRIT.')</option>';
280 print '<option value="'.LOG_ERR.'" '.($conf->global->SYSLOG_LEVEL==LOG_ERR?'SELECTED':'').'>LOG_ERR ('.LOG_ERR.')</option>';
281 print '<option value="'.LOG_WARNING.'" '.($conf->global->SYSLOG_LEVEL==LOG_WARNING?'SELECTED':'').'>LOG_WARNING ('.LOG_WARNING.')</option>';
282 print '<option value="'.LOG_NOTICE.'" '.($conf->global->SYSLOG_LEVEL==LOG_NOTICE?'SELECTED':'').'>LOG_NOTICE ('.LOG_NOTICE.')</option>';
283 print '<option value="'.LOG_INFO.'" '.($conf->global->SYSLOG_LEVEL==LOG_INFO?'SELECTED':'').'>LOG_INFO ('.LOG_INFO.')</option>';
284 print '<option value="'.LOG_DEBUG.'" '.($conf->global->SYSLOG_LEVEL>=LOG_DEBUG?'SELECTED':'').'>LOG_DEBUG ('.LOG_DEBUG.')</option>';
285 print '</select>';
286 print '</td></tr>';
287 print '</table>';
288 print "</form>\n";
289 
290 llxFooter();
291 
292 $db->close();
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
llxFooter()
Empty footer.
Definition: wrapper.php:58
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:450
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:485
if(empty($reshook)) $form
View.
Definition: perms.php:103
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
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'une constante dans la base de donnees.
Definition: admin.lib.php:410
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
llxHeader()
Empty header.
Definition: wrapper.php:46
print
Draft customers invoices.
Definition: index.php:91
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.