syslog.php 10 KB

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