syslog.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. /**
  21. * \file htdocs/admin/syslog.php
  22. * \ingroup syslog
  23. * \brief Setup page for logs module
  24. */
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  27. global $conf;
  28. if (!$user->admin) accessforbidden();
  29. $langs->load("admin");
  30. $langs->load("other");
  31. $error=0;
  32. $action = GETPOST('action','aZ09');
  33. $syslogModules = array();
  34. $activeModules = array();
  35. if (! empty($conf->global->SYSLOG_HANDLERS)) $activeModules = json_decode($conf->global->SYSLOG_HANDLERS);
  36. $dirsyslogs = array_merge(array('/core/modules/syslog/'), $conf->modules_parts['syslog']);
  37. foreach ($dirsyslogs as $reldir) {
  38. $dir = dol_buildpath($reldir, 0);
  39. $newdir = dol_osencode($dir);
  40. if (is_dir($newdir)) {
  41. $handle = opendir($newdir);
  42. if (is_resource($handle)) {
  43. $var = true;
  44. while (($file = readdir($handle)) !== false) {
  45. if (substr($file, 0, 11) == 'mod_syslog_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
  46. $file = substr($file, 0, dol_strlen($file) - 4);
  47. require_once $newdir . $file . '.php';
  48. $module = new $file;
  49. // Show modules according to features level
  50. if ($module->getVersion() == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  51. continue;
  52. }
  53. if ($module->getVersion() == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  54. continue;
  55. }
  56. $syslogModules[] = $file;
  57. }
  58. }
  59. closedir($handle);
  60. }
  61. }
  62. }
  63. /*
  64. * Actions
  65. */
  66. // Set modes
  67. if ($action == 'set')
  68. {
  69. $db->begin();
  70. $newActiveModules = array();
  71. $selectedModules = (isset($_POST['SYSLOG_HANDLERS']) ? $_POST['SYSLOG_HANDLERS'] : array());
  72. // Save options of handler
  73. foreach ($syslogModules as $syslogHandler)
  74. {
  75. if (in_array($syslogHandler, $syslogModules))
  76. {
  77. $module = new $syslogHandler;
  78. if (in_array($syslogHandler, $selectedModules)) $newActiveModules[] = $syslogHandler;
  79. foreach ($module->configure() as $option)
  80. {
  81. if (isset($_POST[$option['constant']]))
  82. {
  83. $_POST[$option['constant']] = trim($_POST[$option['constant']]);
  84. dolibarr_del_const($db, $option['constant'], -1);
  85. dolibarr_set_const($db, $option['constant'], $_POST[$option['constant']], 'chaine',0, '', 0);
  86. }
  87. }
  88. }
  89. }
  90. $activeModules = $newActiveModules;
  91. dolibarr_del_const($db, 'SYSLOG_HANDLERS', -1); // To be sure ther is not a setup into another entity
  92. dolibarr_set_const($db, 'SYSLOG_HANDLERS', json_encode($activeModules), 'chaine',0,'',0);
  93. // Check configuration
  94. foreach ($activeModules as $modulename) {
  95. /**
  96. * @var LogHandler
  97. */
  98. $module = new $modulename;
  99. $error = $module->checkConfiguration();
  100. }
  101. if (! $error)
  102. {
  103. $db->commit();
  104. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  105. }
  106. else
  107. {
  108. $db->rollback();
  109. setEventMessages($error, $errors, 'errors');
  110. }
  111. }
  112. // Set level
  113. if ($action == 'setlevel')
  114. {
  115. $level = GETPOST("level");
  116. $res = dolibarr_set_const($db,"SYSLOG_LEVEL",$level,'chaine',0,'',0);
  117. dol_syslog("admin/syslog: level ".$level);
  118. if (! $res > 0) $error++;
  119. if (! $error)
  120. {
  121. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  122. }
  123. else
  124. {
  125. setEventMessages($langs->trans("Error"), null, 'errors');
  126. }
  127. }
  128. /*
  129. * View
  130. */
  131. llxHeader();
  132. $form=new Form($db);
  133. $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
  134. print load_fiche_titre($langs->trans("SyslogSetup"),$linkback,'title_setup');
  135. print '<br>';
  136. $def = array();
  137. $syslogfacility=$defaultsyslogfacility=dolibarr_get_const($db,"SYSLOG_FACILITY",0);
  138. $syslogfile=$defaultsyslogfile=dolibarr_get_const($db,"SYSLOG_FILE",0);
  139. if (! $defaultsyslogfacility) $defaultsyslogfacility='LOG_USER';
  140. if (! $defaultsyslogfile) $defaultsyslogfile='dolibarr.log';
  141. if ($conf->global->MAIN_MODULE_MULTICOMPANY && $user->entity)
  142. {
  143. print '<div class="error">'.$langs->trans("ContactSuperAdminForChange").'</div>';
  144. $option = 'disabled';
  145. }
  146. //print "conf->global->MAIN_FEATURES_LEVEL = ".$conf->global->MAIN_FEATURES_LEVEL."<br><br>\n";
  147. // Output mode
  148. print load_fiche_titre($langs->trans("SyslogOutput"));
  149. // Mode
  150. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  151. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  152. print '<input type="hidden" name="action" value="set">';
  153. print '<table class="noborder" width="100%">';
  154. print '<tr class="liste_titre">';
  155. print '<td>'.$langs->trans("Type").'</td><td>'.$langs->trans("Value").'</td>';
  156. print '<td align="right" colspan="2"><input type="submit" class="button" '.$option.' value="'.$langs->trans("Modify").'"></td>';
  157. print "</tr>\n";
  158. foreach ($syslogModules as $moduleName)
  159. {
  160. $module = new $moduleName;
  161. $moduleactive=(int) $module->isActive();
  162. //print $moduleName." = ".$moduleactive." - ".$module->getName()." ".($moduleactive == -1)."<br>\n";
  163. 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)
  164. print '<tr class="oddeven">';
  165. print '<td width="140">';
  166. print '<input class="oddeven" type="checkbox" name="SYSLOG_HANDLERS[]" value="'.$moduleName.'" '.(in_array($moduleName, $activeModules) ? 'checked' : '').($moduleactive <= 0 ? 'disabled' : '').'> ';
  167. print $module->getName();
  168. print '</td>';
  169. print '<td class="nowrap">';
  170. $setuparray=$module->configure();
  171. if ($setuparray)
  172. {
  173. foreach ($setuparray as $option)
  174. {
  175. $tmpoption=$option['constant'];
  176. if (! empty($tmpoption))
  177. {
  178. if (isset($_POST[$tmpoption])) $value=$_POST[$tmpoption];
  179. else if (! empty($conf->global->$tmpoption)) $value = $conf->global->$tmpoption;
  180. }
  181. else $value = (isset($option['default']) ? $option['default'] : '');
  182. print $option['name'].': <input type="text" class="flat" name="'.$option['constant'].'" value="'.$value.'"'.(isset($option['attr']) ? ' '.$option['attr'] : '').'>';
  183. if (! empty($option['example'])) print '<br>'.$langs->trans("Example").': '.$option['example'];
  184. }
  185. }
  186. print '</td>';
  187. print '<td align="left">';
  188. if ($module->getInfo())
  189. {
  190. print $form->textwithpicto('', $module->getInfo(), 1, 'help');
  191. }
  192. if ($module->getWarning())
  193. {
  194. print $form->textwithpicto('', $module->getWarning(), 1, 'warning');
  195. }
  196. print '</td>';
  197. print "</tr>\n";
  198. }
  199. print "</table>\n";
  200. print "</form>\n";
  201. print '<br>'."\n\n";
  202. print load_fiche_titre($langs->trans("SyslogLevel"));
  203. // Level
  204. print '<form action="syslog.php" method="post">';
  205. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  206. print '<input type="hidden" name="action" value="setlevel">';
  207. print '<table class="noborder" width="100%">';
  208. print '<tr class="liste_titre">';
  209. print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
  210. print '<td align="right"><input type="submit" class="button" '.$option.' value="'.$langs->trans("Modify").'"></td>';
  211. print "</tr>\n";
  212. $var=true;
  213. print '<tr class="oddeven"><td width="140">'.$langs->trans("SyslogLevel").'</td>';
  214. print '<td colspan="2"><select class="flat" name="level" '.$option.'>';
  215. print '<option value="'.LOG_EMERG.'" '.($conf->global->SYSLOG_LEVEL==LOG_EMERG?'SELECTED':'').'>LOG_EMERG ('.LOG_EMERG.')</option>';
  216. print '<option value="'.LOG_ALERT.'" '.($conf->global->SYSLOG_LEVEL==LOG_ALERT?'SELECTED':'').'>LOG_ALERT ('.LOG_ALERT.')</option>';
  217. print '<option value="'.LOG_CRIT.'" '.($conf->global->SYSLOG_LEVEL==LOG_CRIT?'SELECTED':'').'>LOG_CRIT ('.LOG_CRIT.')</option>';
  218. print '<option value="'.LOG_ERR.'" '.($conf->global->SYSLOG_LEVEL==LOG_ERR?'SELECTED':'').'>LOG_ERR ('.LOG_ERR.')</option>';
  219. print '<option value="'.LOG_WARNING.'" '.($conf->global->SYSLOG_LEVEL==LOG_WARNING?'SELECTED':'').'>LOG_WARNING ('.LOG_WARNING.')</option>';
  220. print '<option value="'.LOG_NOTICE.'" '.($conf->global->SYSLOG_LEVEL==LOG_NOTICE?'SELECTED':'').'>LOG_NOTICE ('.LOG_NOTICE.')</option>';
  221. print '<option value="'.LOG_INFO.'" '.($conf->global->SYSLOG_LEVEL==LOG_INFO?'SELECTED':'').'>LOG_INFO ('.LOG_INFO.')</option>';
  222. print '<option value="'.LOG_DEBUG.'" '.($conf->global->SYSLOG_LEVEL>=LOG_DEBUG?'SELECTED':'').'>LOG_DEBUG ('.LOG_DEBUG.')</option>';
  223. print '</select>';
  224. print '</td></tr>';
  225. print '</table>';
  226. print "</form>\n";
  227. llxFooter();
  228. $db->close();