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