1
0
Fork 0

Move admin/logs to src/Module

This commit is contained in:
Hypolite Petovan 2019-04-25 22:55:05 -04:00
parent 740723534f
commit df947ba9db
7 changed files with 134 additions and 147 deletions

View file

@ -67,9 +67,6 @@ function admin_post(App $a)
$return_path = 'admin';
if ($a->argc > 1) {
switch ($a->argv[1]) {
case 'logs':
admin_page_logs_post($a);
break;
case 'deleteitem':
admin_page_deleteitem_post($a);
break;
@ -169,12 +166,6 @@ function admin_content(App $a)
// urls
if ($a->argc > 1) {
switch ($a->argv[1]) {
case 'logs':
$o = admin_page_logs($a);
break;
case 'viewlogs':
$o = admin_page_viewlogs($a);
break;
case 'dbsync':
$o = admin_page_dbsync($a);
break;
@ -353,142 +344,6 @@ function admin_page_dbsync(App $a)
return $o;
}
/**
* @brief Prosesses data send by Logs admin page
*
* @param App $a
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
function admin_page_logs_post(App $a)
{
if (!empty($_POST['page_logs'])) {
BaseModule::checkFormSecurityTokenRedirectOnError('/admin/logs', 'admin_logs');
$logfile = (!empty($_POST['logfile']) ? Strings::escapeTags(trim($_POST['logfile'])) : '');
$debugging = !empty($_POST['debugging']);
$loglevel = defaults($_POST, 'loglevel', LogLevel::ERROR);
Config::set('system', 'logfile', $logfile);
Config::set('system', 'debugging', $debugging);
Config::set('system', 'loglevel', $loglevel);
}
info(L10n::t("Log settings updated."));
$a->internalRedirect('admin/logs');
return; // NOTREACHED
}
/**
* @brief Generates admin panel subpage for configuration of the logs
*
* This function take the view/templates/admin_logs.tpl file and generates a
* page where admin can configure the logging of friendica.
*
* Displaying the log is separated from the log config as the logfile can get
* big depending on the settings and changing settings regarding the logs can
* thus waste bandwidth.
*
* The string returned contains the content of the template file with replaced
* macros.
*
* @param App $a
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
function admin_page_logs(App $a)
{
$log_choices = [
LogLevel::ERROR => 'Error',
LogLevel::WARNING => 'Warning',
LogLevel::NOTICE => 'Notice',
LogLevel::INFO => 'Info',
LogLevel::DEBUG => 'Debug',
];
if (ini_get('log_errors')) {
$phplogenabled = L10n::t('PHP log currently enabled.');
} else {
$phplogenabled = L10n::t('PHP log currently disabled.');
}
$t = Renderer::getMarkupTemplate('admin/logs.tpl');
return Renderer::replaceMacros($t, [
'$title' => L10n::t('Administration'),
'$page' => L10n::t('Logs'),
'$submit' => L10n::t('Save Settings'),
'$clear' => L10n::t('Clear'),
'$baseurl' => System::baseUrl(true),
'$logname' => Config::get('system', 'logfile'),
// name, label, value, help string, extra data...
'$debugging' => ['debugging', L10n::t("Enable Debugging"), Config::get('system', 'debugging'), ""],
'$logfile' => ['logfile', L10n::t("Log file"), Config::get('system', 'logfile'), L10n::t("Must be writable by web server. Relative to your Friendica top-level directory.")],
'$loglevel' => ['loglevel', L10n::t("Log level"), Config::get('system', 'loglevel'), "", $log_choices],
'$form_security_token' => BaseModule::getFormSecurityToken("admin_logs"),
'$phpheader' => L10n::t("PHP logging"),
'$phphint' => 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."),
'$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');",
'$phplogenabled' => $phplogenabled,
]);
}
/**
* @brief Generates admin panel subpage to view the Friendica log
*
* This function loads the template view/templates/admin_viewlogs.tpl to
* display the systemlog content. The filename for the systemlog of friendica
* is relative to the base directory and taken from the config entry 'logfile'
* in the 'system' category.
*
* Displaying the log is separated from the log config as the logfile can get
* big depending on the settings and changing settings regarding the logs can
* thus waste bandwidth.
*
* The string returned contains the content of the template file with replaced
* macros.
*
* @param App $a
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
function admin_page_viewlogs(App $a)
{
$t = Renderer::getMarkupTemplate('admin/viewlogs.tpl');
$f = Config::get('system', 'logfile');
$data = '';
if (!file_exists($f)) {
$data = L10n::t('Error trying to open <strong>%1$s</strong> log file.\r\n<br/>Check to see if file %1$s exist and is readable.', $f);
} else {
$fp = fopen($f, 'r');
if (!$fp) {
$data = L10n::t('Couldn\'t open <strong>%1$s</strong> log file.\r\n<br/>Check to see if file %1$s is readable.', $f);
} else {
$fstat = fstat($fp);
$size = $fstat['size'];
if ($size != 0) {
if ($size > 5000000 || $size < 0) {
$size = 5000000;
}
$seek = fseek($fp, 0 - $size, SEEK_END);
if ($seek === 0) {
$data = Strings::escapeHtml(fread($fp, $size));
while (!feof($fp)) {
$data .= Strings::escapeHtml(fread($fp, 4096));
}
}
}
fclose($fp);
}
}
return Renderer::replaceMacros($t, [
'$title' => L10n::t('Administration'),
'$page' => L10n::t('View Logs'),
'$data' => $data,
'$logname' => Config::get('system', 'logfile')
]);
}
function admin_page_server_vital()
{
// Fetch the host-meta to check if this really is a vital server

View file

@ -130,6 +130,9 @@ class Router
$collector->addRoute(['GET', 'POST'], '/features' , Module\Admin\Features::class);
$collector->addRoute(['GET'] , '/federation' , Module\Admin\Federation::class);
$collector->addRoute(['GET'] , '/logs/view' , Module\Admin\Logs\View::class);
$collector->addRoute(['GET', 'POST'], '/logs' , Module\Admin\Logs\Settings::class);
$collector->addRoute(['GET'] , '/queue[/deferred]' , Module\Admin\Queue::class);
$collector->addRoute(['GET', 'POST'], '/site' , Module\Admin\Site::class);

View file

@ -0,0 +1,73 @@
<?php
namespace Friendica\Module\Admin\Logs;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\System;
use Friendica\Module\BaseAdminModule;
use Friendica\Util\Strings;
use Psr\Log\LogLevel;
class Settings extends BaseAdminModule
{
public static function post()
{
parent::post();
if (!empty($_POST['page_logs'])) {
parent::checkFormSecurityTokenRedirectOnError('/admin/logs', 'admin_logs');
$logfile = (!empty($_POST['logfile']) ? Strings::escapeTags(trim($_POST['logfile'])) : '');
$debugging = !empty($_POST['debugging']);
$loglevel = defaults($_POST, 'loglevel', LogLevel::ERROR);
Config::set('system', 'logfile', $logfile);
Config::set('system', 'debugging', $debugging);
Config::set('system', 'loglevel', $loglevel);
}
info(L10n::t("Log settings updated."));
self::getApp()->internalRedirect('admin/logs');
}
public static function content()
{
parent::content();
$log_choices = [
LogLevel::ERROR => 'Error',
LogLevel::WARNING => 'Warning',
LogLevel::NOTICE => 'Notice',
LogLevel::INFO => 'Info',
LogLevel::DEBUG => 'Debug',
];
if (ini_get('log_errors')) {
$phplogenabled = L10n::t('PHP log currently enabled.');
} else {
$phplogenabled = L10n::t('PHP log currently disabled.');
}
$t = Renderer::getMarkupTemplate('admin/logs/settings.tpl');
return Renderer::replaceMacros($t, [
'$title' => L10n::t('Administration'),
'$page' => L10n::t('Logs'),
'$submit' => L10n::t('Save Settings'),
'$clear' => L10n::t('Clear'),
'$baseurl' => System::baseUrl(true),
'$logname' => Config::get('system', 'logfile'),
// name, label, value, help string, extra data...
'$debugging' => ['debugging', L10n::t("Enable Debugging"), Config::get('system', 'debugging'), ""],
'$logfile' => ['logfile', L10n::t("Log file"), Config::get('system', 'logfile'), L10n::t("Must be writable by web server. Relative to your Friendica top-level directory.")],
'$loglevel' => ['loglevel', L10n::t("Log level"), Config::get('system', 'loglevel'), "", $log_choices],
'$form_security_token' => parent::getFormSecurityToken("admin_logs"),
'$phpheader' => L10n::t("PHP logging"),
'$phphint' => 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."),
'$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');",
'$phplogenabled' => $phplogenabled,
]);
}
}

View file

@ -0,0 +1,52 @@
<?php
namespace Friendica\Module\Admin\Logs;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Module\BaseAdminModule;
use Friendica\Util\Strings;
class View extends BaseAdminModule
{
public static function content()
{
parent::content();
$t = Renderer::getMarkupTemplate('admin/logs/view.tpl');
$f = Config::get('system', 'logfile');
$data = '';
if (!file_exists($f)) {
$data = L10n::t('Error trying to open <strong>%1$s</strong> log file.\r\n<br/>Check to see if file %1$s exist and is readable.', $f);
} else {
$fp = fopen($f, 'r');
if (!$fp) {
$data = L10n::t('Couldn\'t open <strong>%1$s</strong> log file.\r\n<br/>Check to see if file %1$s is readable.', $f);
} else {
$fstat = fstat($fp);
$size = $fstat['size'];
if ($size != 0) {
if ($size > 5000000 || $size < 0) {
$size = 5000000;
}
$seek = fseek($fp, 0 - $size, SEEK_END);
if ($seek === 0) {
$data = Strings::escapeHtml(fread($fp, $size));
while (!feof($fp)) {
$data .= Strings::escapeHtml(fread($fp, 4096));
}
}
}
fclose($fp);
}
}
return Renderer::replaceMacros($t, [
'$title' => L10n::t('Administration'),
'$page' => L10n::t('View Logs'),
'$data' => $data,
'$logname' => Config::get('system', 'logfile')
]);
}
}

View file

@ -68,6 +68,10 @@ abstract class BaseAdminModule extends BaseModule
'contactblock' => ['admin/blocklist/contact', L10n::t('Contact Blocklist') , 'contactblock'],
'blocklist' => ['admin/blocklist/server' , L10n::t('Server Blocklist') , 'blocklist'],
]],
'logs' => [L10n::t('Logs'), [
'logsconfig' => ['admin/logs/', L10n::t('Logs') , 'logs'],
'logsview' => ['admin/logs/view' , L10n::t('View Logs') , 'viewlogs'],
]],
];
$addons_admin = [];

View file

@ -1,7 +1,7 @@
<div id='adminpage'>
<h1>{{$title}} - {{$page}}</h1>
<form action="{{$baseurl}}/admin/logs" method="post">
<form action="{{$baseurl}}/admin/logs/settings" method="post">
<input type='hidden' name='form_security_token' value="{{$form_security_token}}">
{{include file="field_checkbox.tpl" field=$debugging}}

View file

@ -1,4 +1,4 @@
<div id='adminpage'>
<div id="adminpage">
<h1>{{$title}} - {{$page}}</h1>
<h3>{{$logname}}</h3>