From df947ba9dbb14037d12cd3428d3d73af67b856b5 Mon Sep 17 00:00:00 2001
From: Hypolite Petovan <hypolite@mrpetovan.com>
Date: Thu, 25 Apr 2019 22:55:05 -0400
Subject: [PATCH] Move admin/logs to src/Module

---
 mod/admin.php                                 | 145 ------------------
 src/App/Router.php                            |   3 +
 src/Module/Admin/Logs/Settings.php            |  73 +++++++++
 src/Module/Admin/Logs/View.php                |  52 +++++++
 src/Module/BaseAdminModule.php                |   4 +
 .../admin/{logs.tpl => logs/settings.tpl}     |   2 +-
 .../admin/{viewlogs.tpl => logs/view.tpl}     |   2 +-
 7 files changed, 134 insertions(+), 147 deletions(-)
 create mode 100644 src/Module/Admin/Logs/Settings.php
 create mode 100644 src/Module/Admin/Logs/View.php
 rename view/templates/admin/{logs.tpl => logs/settings.tpl} (89%)
 rename view/templates/admin/{viewlogs.tpl => logs/view.tpl} (87%)

diff --git a/mod/admin.php b/mod/admin.php
index ad4dcf3988..71089cc39a 100644
--- a/mod/admin.php
+++ b/mod/admin.php
@@ -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
diff --git a/src/App/Router.php b/src/App/Router.php
index cd806e59e6..47f71c988e 100644
--- a/src/App/Router.php
+++ b/src/App/Router.php
@@ -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);
diff --git a/src/Module/Admin/Logs/Settings.php b/src/Module/Admin/Logs/Settings.php
new file mode 100644
index 0000000000..f6a760135d
--- /dev/null
+++ b/src/Module/Admin/Logs/Settings.php
@@ -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,
+		]);
+	}
+}
diff --git a/src/Module/Admin/Logs/View.php b/src/Module/Admin/Logs/View.php
new file mode 100644
index 0000000000..45b19bfacf
--- /dev/null
+++ b/src/Module/Admin/Logs/View.php
@@ -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')
+		]);
+	}
+}
diff --git a/src/Module/BaseAdminModule.php b/src/Module/BaseAdminModule.php
index 13d1b1f19b..dc94473812 100644
--- a/src/Module/BaseAdminModule.php
+++ b/src/Module/BaseAdminModule.php
@@ -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 = [];
diff --git a/view/templates/admin/logs.tpl b/view/templates/admin/logs/settings.tpl
similarity index 89%
rename from view/templates/admin/logs.tpl
rename to view/templates/admin/logs/settings.tpl
index a218a38f56..b584605ed9 100644
--- a/view/templates/admin/logs.tpl
+++ b/view/templates/admin/logs/settings.tpl
@@ -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}}
diff --git a/view/templates/admin/viewlogs.tpl b/view/templates/admin/logs/view.tpl
similarity index 87%
rename from view/templates/admin/viewlogs.tpl
rename to view/templates/admin/logs/view.tpl
index c80264c52a..9ac5acd9dd 100644
--- a/view/templates/admin/viewlogs.tpl
+++ b/view/templates/admin/logs/view.tpl
@@ -1,4 +1,4 @@
-<div id='adminpage'>
+<div id="adminpage">
 	<h1>{{$title}} - {{$page}}</h1>
 	
 	<h3>{{$logname}}</h3>