syslog.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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' && getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2) {
  55. continue;
  56. }
  57. if ($module->getVersion() == 'experimental' && getDolGlobalInt('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 '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  151. print load_fiche_titre($langs->trans("SyslogOutput"), '', '');
  152. print '<input type="hidden" name="token" value="'.newToken().'">';
  153. print '<input type="hidden" name="action" value="set">';
  154. print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  155. print '<table class="noborder centpercent">';
  156. print '<tr class="liste_titre">';
  157. print '<td>'.$langs->trans("Type").'</td>';
  158. print '<td>'.$langs->trans("Value").'</td>';
  159. print '<td class="center width150"><input type="submit" class="button small" '.$optionmc.' value="'.$langs->trans("Modify").'"></td>';
  160. print "</tr>\n";
  161. foreach ($syslogModules as $moduleName) {
  162. $module = new $moduleName();
  163. $moduleactive = (int) $module->isActive();
  164. //print $moduleName." = ".$moduleactive." - ".$module->getName()." ".($moduleactive == -1)."<br>\n";
  165. if (($moduleactive == -1) && getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0) {
  166. continue; // Some modules are hidden if not activable and not into debug mode (end user must not see them)
  167. }
  168. print '<tr class="oddeven">';
  169. print '<td class="nowraponall" width="140">';
  170. print '<input class="oddeven" type="checkbox" id="syslog_handler_'.$moduleName.'" name="SYSLOG_HANDLERS[]" value="'.$moduleName.'" '.(in_array($moduleName, $activeModules) ? 'checked' : '').($moduleactive <= 0 ? 'disabled' : '').'> ';
  171. print '<label for="syslog_handler_'.$moduleName.'">'.$module->getName().'</label>';
  172. if ($moduleName == 'mod_syslog_syslog') {
  173. if (!$module->isActive()) {
  174. $langs->load("errors");
  175. print $form->textwithpicto('', $langs->trans("ErrorPHPNeedModule", 'SysLog'));
  176. }
  177. }
  178. print '</td>';
  179. print '<td class="nowrap">';
  180. $setuparray = $module->configure();
  181. if ($setuparray) {
  182. foreach ($setuparray as $option) {
  183. $tmpoption = $option['constant'];
  184. $value = '';
  185. if (!empty($tmpoption)) {
  186. if (GETPOSTISSET($tmpoption)) {
  187. $value = GETPOST($tmpoption);
  188. } else {
  189. $value = getDolGlobalString($tmpoption);
  190. }
  191. } else {
  192. $value = (isset($option['default']) ? $option['default'] : '');
  193. }
  194. print '<span class="hideonsmartphone opacitymedium">'.$option['name'].': </span><input type="text" class="flat'.(empty($option['css']) ? '' : ' '.$option['css']).'" name="'.dol_escape_htmltag($option['constant']).'" value="'.$value.'"'.(isset($option['attr']) ? ' '.$option['attr'] : '').'>';
  195. if (!empty($option['example'])) {
  196. print '<br>'.$langs->trans("Example").': '.dol_escape_htmltag($option['example']);
  197. }
  198. if ($option['constant'] == 'SYSLOG_FILE' && preg_match('/^DOL_DATA_ROOT\/[^\/]*$/', $value)) {
  199. $filelogparam = ' &nbsp; &nbsp; <a href="'.DOL_URL_ROOT.'/document.php?modulepart=logs&file='.basename($value).'">';
  200. $filelogparam .= $langs->trans('Download');
  201. $filelogparam .= img_picto($langs->trans('Download').' '.basename($value), 'download', 'class="paddingleft"');
  202. $filelogparam .= '</a>';
  203. print $filelogparam;
  204. }
  205. }
  206. }
  207. print '</td>';
  208. print '<td class="center">';
  209. if ($module->getInfo()) {
  210. print $form->textwithpicto('', $module->getInfo(), 1, 'help');
  211. }
  212. if ($module->getWarning()) {
  213. print $form->textwithpicto('', $module->getWarning(), 1, 'warning');
  214. }
  215. print '</td>';
  216. print "</tr>\n";
  217. }
  218. print "</table>\n";
  219. print "</div>\n";
  220. print "</form>\n";
  221. print '<br>'."\n\n";
  222. // Level
  223. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  224. print load_fiche_titre($langs->trans("SyslogLevel"), '', '');
  225. print '<input type="hidden" name="token" value="'.newToken().'">';
  226. print '<input type="hidden" name="action" value="setlevel">';
  227. print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  228. print '<table class="noborder centpercent">';
  229. print '<tr class="liste_titre">';
  230. print '<td>'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td>';
  231. print '<td class="center width150"><input type="submit" class="button small" '.$optionmc.' value="'.$langs->trans("Modify").'"></td>';
  232. print "</tr>\n";
  233. print '<tr class="oddeven"><td>'.$langs->trans("SyslogLevel").'</td>';
  234. print '<td colspan="2"><select class="flat minwidth400" id="level" name="level" '.$optionmc.'>';
  235. print '<option value="'.LOG_EMERG.'" '.($conf->global->SYSLOG_LEVEL == LOG_EMERG ? 'SELECTED' : '').'>LOG_EMERG ('.LOG_EMERG.')</option>';
  236. print '<option value="'.LOG_ALERT.'" '.($conf->global->SYSLOG_LEVEL == LOG_ALERT ? 'SELECTED' : '').'>LOG_ALERT ('.LOG_ALERT.')</option>';
  237. print '<option value="'.LOG_CRIT.'" '.($conf->global->SYSLOG_LEVEL == LOG_CRIT ? 'SELECTED' : '').'>LOG_CRIT ('.LOG_CRIT.')</option>';
  238. print '<option value="'.LOG_ERR.'" '.($conf->global->SYSLOG_LEVEL == LOG_ERR ? 'SELECTED' : '').'>LOG_ERR ('.LOG_ERR.')</option>';
  239. print '<option value="'.LOG_WARNING.'" '.($conf->global->SYSLOG_LEVEL == LOG_WARNING ? 'SELECTED' : '').'">LOG_WARNING ('.LOG_WARNING.')</option>';
  240. print '<option value="'.LOG_NOTICE.'" '.($conf->global->SYSLOG_LEVEL == LOG_NOTICE ? 'SELECTED' : '').' data-html="'.dol_escape_htmltag('LOG_NOTICE ('.LOG_NOTICE.') - <span class="opacitymedium">'.$langs->trans("RecommendedForProduction").'</span>').'">LOG_NOTICE ('.LOG_NOTICE.')</option>';
  241. print '<option value="'.LOG_INFO.'" '.($conf->global->SYSLOG_LEVEL == LOG_INFO ? 'SELECTED' : '').'>LOG_INFO ('.LOG_INFO.')</option>';
  242. print '<option value="'.LOG_DEBUG.'" '.($conf->global->SYSLOG_LEVEL >= LOG_DEBUG ? 'SELECTED' : '').' data-html="'.dol_escape_htmltag('LOG_DEBUG ('.LOG_DEBUG.') - <span class="opacitymedium">'.$langs->trans("RecommendedForDebug").'</span>').'">LOG_DEBUG ('.LOG_DEBUG.')</option>';
  243. print '</select>';
  244. print ajax_combobox("level");
  245. print '</td></tr>';
  246. if (!empty($conf->loghandlers['mod_syslog_file']) && isModEnabled('cron')) {
  247. print '<tr class="oddeven"><td>'.$langs->trans("SyslogFileNumberOfSaves").'</td>';
  248. print '<td colspan="2"><input class="width50" type="number" name="file_saves" placeholder="14" min="0" step="1" value="'.getDolGlobalString('SYSLOG_FILE_SAVES').'" />';
  249. print ' &nbsp; (<a href="'.dol_buildpath('/cron/list.php', 1).'?search_label=CompressSyslogs&status=-1">'.$langs->trans('ConfigureCleaningCronjobToSetFrequencyOfSaves').'</a>)</td></tr>';
  250. }
  251. print '</table>';
  252. print "</div>\n";
  253. print "</form>\n";
  254. // End of page
  255. llxFooter();
  256. $db->close();