syslog.php 10 KB

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