Move admin/logs to src/Module
authorHypolite Petovan <hypolite@mrpetovan.com>
Fri, 26 Apr 2019 02:55:05 +0000 (22:55 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Thu, 2 May 2019 13:52:51 +0000 (09:52 -0400)
mod/admin.php
src/App/Router.php
src/Module/Admin/Logs/Settings.php [new file with mode: 0644]
src/Module/Admin/Logs/View.php [new file with mode: 0644]
src/Module/BaseAdminModule.php
view/templates/admin/logs.tpl [deleted file]
view/templates/admin/logs/settings.tpl [new file with mode: 0644]
view/templates/admin/logs/view.tpl [new file with mode: 0644]
view/templates/admin/viewlogs.tpl [deleted file]

index ad4dcf3..71089cc 100644 (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
index cd806e5..47f71c9 100644 (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);
diff --git a/src/Module/Admin/Logs/Settings.php b/src/Module/Admin/Logs/Settings.php
new file mode 100644 (file)
index 0000000..f6a7601
--- /dev/null
@@ -0,0 +1,73 @@
+<?php\r
+\r
+namespace Friendica\Module\Admin\Logs;\r
+\r
+use Friendica\Core\Config;\r
+use Friendica\Core\L10n;\r
+use Friendica\Core\Renderer;\r
+use Friendica\Core\System;\r
+use Friendica\Module\BaseAdminModule;\r
+use Friendica\Util\Strings;\r
+use Psr\Log\LogLevel;\r
+\r
+class Settings extends BaseAdminModule\r
+{\r
+       public static function post()\r
+       {\r
+               parent::post();\r
+\r
+               if (!empty($_POST['page_logs'])) {\r
+                       parent::checkFormSecurityTokenRedirectOnError('/admin/logs', 'admin_logs');\r
+\r
+                       $logfile   = (!empty($_POST['logfile']) ? Strings::escapeTags(trim($_POST['logfile'])) : '');\r
+                       $debugging = !empty($_POST['debugging']);\r
+                       $loglevel  = defaults($_POST, 'loglevel', LogLevel::ERROR);\r
+\r
+                       Config::set('system', 'logfile', $logfile);\r
+                       Config::set('system', 'debugging', $debugging);\r
+                       Config::set('system', 'loglevel', $loglevel);\r
+               }\r
+\r
+               info(L10n::t("Log settings updated."));\r
+               self::getApp()->internalRedirect('admin/logs');\r
+       }\r
+\r
+       public static function content()\r
+       {\r
+               parent::content();\r
+\r
+               $log_choices = [\r
+                       LogLevel::ERROR   => 'Error',\r
+                       LogLevel::WARNING => 'Warning',\r
+                       LogLevel::NOTICE  => 'Notice',\r
+                       LogLevel::INFO    => 'Info',\r
+                       LogLevel::DEBUG   => 'Debug',\r
+               ];\r
+\r
+               if (ini_get('log_errors')) {\r
+                       $phplogenabled = L10n::t('PHP log currently enabled.');\r
+               } else {\r
+                       $phplogenabled = L10n::t('PHP log currently disabled.');\r
+               }\r
+\r
+               $t = Renderer::getMarkupTemplate('admin/logs/settings.tpl');\r
+\r
+               return Renderer::replaceMacros($t, [\r
+                       '$title' => L10n::t('Administration'),\r
+                       '$page' => L10n::t('Logs'),\r
+                       '$submit' => L10n::t('Save Settings'),\r
+                       '$clear' => L10n::t('Clear'),\r
+                       '$baseurl' => System::baseUrl(true),\r
+                       '$logname' => Config::get('system', 'logfile'),\r
+                       // name, label, value, help string, extra data...\r
+                       '$debugging' => ['debugging', L10n::t("Enable Debugging"), Config::get('system', 'debugging'), ""],\r
+                       '$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.")],\r
+                       '$loglevel' => ['loglevel', L10n::t("Log level"), Config::get('system', 'loglevel'), "", $log_choices],\r
+                       '$form_security_token' => parent::getFormSecurityToken("admin_logs"),\r
+                       '$phpheader' => L10n::t("PHP logging"),\r
+                       '$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."),\r
+                       '$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');",\r
+                       '$phplogenabled' => $phplogenabled,\r
+               ]);\r
+       }\r
+}\r
diff --git a/src/Module/Admin/Logs/View.php b/src/Module/Admin/Logs/View.php
new file mode 100644 (file)
index 0000000..45b19bf
--- /dev/null
@@ -0,0 +1,52 @@
+<?php\r
+\r
+namespace Friendica\Module\Admin\Logs;\r
+\r
+use Friendica\Core\Config;\r
+use Friendica\Core\L10n;\r
+use Friendica\Core\Renderer;\r
+use Friendica\Module\BaseAdminModule;\r
+use Friendica\Util\Strings;\r
+\r
+class View extends BaseAdminModule\r
+{\r
+       public static function content()\r
+       {\r
+               parent::content();\r
+\r
+               $t = Renderer::getMarkupTemplate('admin/logs/view.tpl');\r
+               $f = Config::get('system', 'logfile');\r
+               $data = '';\r
+\r
+               if (!file_exists($f)) {\r
+                       $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);\r
+               } else {\r
+                       $fp = fopen($f, 'r');\r
+                       if (!$fp) {\r
+                               $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);\r
+                       } else {\r
+                               $fstat = fstat($fp);\r
+                               $size = $fstat['size'];\r
+                               if ($size != 0) {\r
+                                       if ($size > 5000000 || $size < 0) {\r
+                                               $size = 5000000;\r
+                                       }\r
+                                       $seek = fseek($fp, 0 - $size, SEEK_END);\r
+                                       if ($seek === 0) {\r
+                                               $data = Strings::escapeHtml(fread($fp, $size));\r
+                                               while (!feof($fp)) {\r
+                                                       $data .= Strings::escapeHtml(fread($fp, 4096));\r
+                                               }\r
+                                       }\r
+                               }\r
+                               fclose($fp);\r
+                       }\r
+               }\r
+               return Renderer::replaceMacros($t, [\r
+                       '$title' => L10n::t('Administration'),\r
+                       '$page' => L10n::t('View Logs'),\r
+                       '$data' => $data,\r
+                       '$logname' => Config::get('system', 'logfile')\r
+               ]);\r
+       }\r
+}\r
index 13d1b1f..dc94473 100644 (file)
@@ -68,6 +68,10 @@ abstract class BaseAdminModule extends BaseModule
                                'contactblock' => ['admin/blocklist/contact', L10n::t('Contact Blocklist')  , 'contactblock'],\r
                                'blocklist'    => ['admin/blocklist/server' , L10n::t('Server Blocklist')   , 'blocklist'],\r
                        ]],\r
+                       'logs' => [L10n::t('Logs'), [\r
+                               'logsconfig'   => ['admin/logs/', L10n::t('Logs')                   , 'logs'],\r
+                               'logsview'     => ['admin/logs/view'    , L10n::t('View Logs')              , 'viewlogs'],\r
+                       ]],\r
                ];\r
 \r
                $addons_admin = [];\r
diff --git a/view/templates/admin/logs.tpl b/view/templates/admin/logs.tpl
deleted file mode 100644 (file)
index a218a38..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-<div id='adminpage'>
-    <h1>{{$title}} - {{$page}}</h1>
-       
-       <form action="{{$baseurl}}/admin/logs" method="post">
-           <input type='hidden' name='form_security_token' value="{{$form_security_token}}">
-
-           {{include file="field_checkbox.tpl" field=$debugging}}
-           {{include file="field_input.tpl" field=$logfile}}
-           {{include file="field_select.tpl" field=$loglevel}}
-       
-           <div class="submit"><input type="submit" name="page_logs" value="{{$submit}}" /></div>
-       
-       </form>
-
-       <h2>{{$phpheader}}</h2>
-       <div>
-               <p>{{$phplogenabled}}<p>
-               <p>{{$phphint}}</p>
-               <pre>{{$phplogcode}}</pre>
-       </div>
-       
-</div>
diff --git a/view/templates/admin/logs/settings.tpl b/view/templates/admin/logs/settings.tpl
new file mode 100644 (file)
index 0000000..b584605
--- /dev/null
@@ -0,0 +1,22 @@
+<div id='adminpage'>
+    <h1>{{$title}} - {{$page}}</h1>
+       
+       <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}}
+           {{include file="field_input.tpl" field=$logfile}}
+           {{include file="field_select.tpl" field=$loglevel}}
+       
+           <div class="submit"><input type="submit" name="page_logs" value="{{$submit}}" /></div>
+       
+       </form>
+
+       <h2>{{$phpheader}}</h2>
+       <div>
+               <p>{{$phplogenabled}}<p>
+               <p>{{$phphint}}</p>
+               <pre>{{$phplogcode}}</pre>
+       </div>
+       
+</div>
diff --git a/view/templates/admin/logs/view.tpl b/view/templates/admin/logs/view.tpl
new file mode 100644 (file)
index 0000000..9ac5acd
--- /dev/null
@@ -0,0 +1,6 @@
+<div id="adminpage">
+       <h1>{{$title}} - {{$page}}</h1>
+       
+       <h3>{{$logname}}</h3>
+       <div style="width:100%; height:400px; overflow: auto; "><pre>{{$data}}</pre></div>
+</div>
diff --git a/view/templates/admin/viewlogs.tpl b/view/templates/admin/viewlogs.tpl
deleted file mode 100644 (file)
index c80264c..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-<div id='adminpage'>
-       <h1>{{$title}} - {{$page}}</h1>
-       
-       <h3>{{$logname}}</h3>
-       <div style="width:100%; height:400px; overflow: auto; "><pre>{{$data}}</pre></div>
-</div>