/src/Module/Admin/Logs/Settings.php

https://github.com/friendica/friendica · PHP · 96 lines · 62 code · 14 blank · 20 comment · 5 complexity · 29a0c709a2d7955a50aab50ac8ba10ec MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2010-2022, the Friendica project
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (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 Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Friendica\Module\Admin\Logs;
  22. use Friendica\Core\Renderer;
  23. use Friendica\DI;
  24. use Friendica\Module\BaseAdmin;
  25. use Psr\Log\LogLevel;
  26. class Settings extends BaseAdmin
  27. {
  28. protected function post(array $request = [])
  29. {
  30. self::checkAdminAccess();
  31. if (empty($_POST['page_logs'])) {
  32. return;
  33. }
  34. self::checkFormSecurityTokenRedirectOnError('/admin/logs', 'admin_logs');
  35. $logfile = (!empty($_POST['logfile']) ? trim($_POST['logfile']) : '');
  36. $debugging = !empty($_POST['debugging']);
  37. $loglevel = ($_POST['loglevel'] ?? '') ?: LogLevel::ERROR;
  38. if (is_file($logfile) &&
  39. !is_writeable($logfile)) {
  40. notice(DI::l10n()->t('The logfile \'%s\' is not writable. No logging possible', $logfile));
  41. return;
  42. }
  43. DI::config()->set('system', 'logfile', $logfile);
  44. DI::config()->set('system', 'debugging', $debugging);
  45. DI::config()->set('system', 'loglevel', $loglevel);
  46. DI::baseUrl()->redirect('admin/logs');
  47. }
  48. protected function content(array $request = []): string
  49. {
  50. parent::content();
  51. $log_choices = [
  52. LogLevel::ERROR => 'Error',
  53. LogLevel::WARNING => 'Warning',
  54. LogLevel::NOTICE => 'Notice',
  55. LogLevel::INFO => 'Info',
  56. LogLevel::DEBUG => 'Debug',
  57. ];
  58. if (ini_get('log_errors')) {
  59. $phplogenabled = DI::l10n()->t('PHP log currently enabled.');
  60. } else {
  61. $phplogenabled = DI::l10n()->t('PHP log currently disabled.');
  62. }
  63. $t = Renderer::getMarkupTemplate('admin/logs/settings.tpl');
  64. return Renderer::replaceMacros($t, [
  65. '$title' => DI::l10n()->t('Administration'),
  66. '$page' => DI::l10n()->t('Logs'),
  67. '$submit' => DI::l10n()->t('Save Settings'),
  68. '$clear' => DI::l10n()->t('Clear'),
  69. '$baseurl' => DI::baseUrl()->get(true),
  70. '$logname' => DI::config()->get('system', 'logfile'),
  71. // see /help/smarty3-templates#1_1 on any Friendica node
  72. '$debugging' => ['debugging', DI::l10n()->t("Enable Debugging"), DI::config()->get('system', 'debugging'), ""],
  73. '$logfile' => ['logfile', DI::l10n()->t("Log file"), DI::config()->get('system', 'logfile'), DI::l10n()->t("Must be writable by web server. Relative to your Friendica top-level directory.")],
  74. '$loglevel' => ['loglevel', DI::l10n()->t("Log level"), DI::config()->get('system', 'loglevel'), "", $log_choices],
  75. '$form_security_token' => self::getFormSecurityToken("admin_logs"),
  76. '$phpheader' => DI::l10n()->t("PHP logging"),
  77. '$phphint' => DI::l10n()->t("To temporarily enable logging of PHP errors and warnings you can prepend the following to the index.php file of your installation. The filename set in the 'error_log' line is relative to the friendica top-level directory and must be writeable by the web server. The option '1' for 'log_errors' and 'display_errors' is to enable these options, set to '0' to disable them."),
  78. '$phplogcode' => "error_reporting(E_ERROR | E_WARNING | E_PARSE);\nini_set('error_log','php.out');\nini_set('log_errors','1');\nini_set('display_errors', '1');",
  79. '$phplogenabled' => $phplogenabled,
  80. ]);
  81. }
  82. }