From 09b3fa314122aa94a59cae632fa0ee22d6a9405f Mon Sep 17 00:00:00 2001 From: djmaze Date: Wed, 14 Jul 2021 11:26:48 +0200 Subject: [PATCH] Cleanup MailSo\Base\Http --- .../0.0.0/app/libraries/MailSo/Base/Http.php | 106 ++---------------- .../0.0.0/app/libraries/RainLoop/Actions.php | 6 +- .../app/libraries/RainLoop/Actions/Raw.php | 2 +- .../0.0.0/app/libraries/RainLoop/Service.php | 2 +- .../app/libraries/RainLoop/ServiceActions.php | 22 ++-- 5 files changed, 28 insertions(+), 110 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Base/Http.php b/snappymail/v/0.0.0/app/libraries/MailSo/Base/Http.php index e54af30fd..280563d2a 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Base/Http.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Base/Http.php @@ -31,66 +31,6 @@ class Http return $oInstance; } - public function HasQuery(string $sKey) : bool - { - return isset($_GET[$sKey]); - } - - /** - * @param mixed $mDefault = null - * - * @return mixed - */ - public function GetQuery(string $sKey, $mDefault = null) - { - return isset($_GET[$sKey]) ? $_GET[$sKey] : $mDefault; - } - - public function GetQueryAsArray() : ?array - { - return isset($_GET) && \is_array($_GET) ? $_GET : null; - } - - public function HasPost(string $sKey) : bool - { - return isset($_POST[$sKey]); - } - - /** - * @param mixed $mDefault = null - * - * @return mixed - */ - public function GetPost(string $sKey, $mDefault = null) - { - return isset($_POST[$sKey]) ? $_POST[$sKey] : $mDefault; - } - - public function GetPostAsArray() : ?array - { - return isset($_POST) && \is_array($_POST) ? $_POST : null; - } - - public function HasRequest(string $sKey) : bool - { - return isset($_REQUEST[$sKey]); - } - - /** - * @param mixed $mDefault = null - * - * @return mixed - */ - public function GetRequest(string $sKey, $mDefault = null) - { - return isset($_REQUEST[$sKey]) ? $_REQUEST[$sKey] : $mDefault; - } - - public function HasServer(string $sKey) : bool - { - return isset($_SERVER[$sKey]); - } - /** * @param mixed $mDefault = null * @@ -101,26 +41,6 @@ class Http return isset($_SERVER[$sKey]) ? $_SERVER[$sKey] : $mDefault; } - public function HasEnv(string $sKey) : bool - { - return isset($_ENV[$sKey]); - } - - /** - * @param mixed $mDefault = null - * - * @return mixed - */ - public function GetEnv(string $sKey, $mDefault = null) - { - return isset($_ENV[$sKey]) ? $_ENV[$sKey] : $mDefault; - } - - public function ServerProtocol() : string - { - return $this->GetServer('SERVER_PROTOCOL', 'HTTP/1.0'); - } - public function GetMethod() : string { return $this->GetServer('REQUEST_METHOD', ''); @@ -136,11 +56,6 @@ class Http return ('GET' === $this->GetMethod()); } - public function GetQueryString() : string - { - return $this->GetServer('QUERY_STRING', ''); - } - public function CheckLocalhost(string $sServer) : bool { return \in_array(\strtolower(\trim($sServer)), array( @@ -229,7 +144,7 @@ class Http if ($bWithRemoteUserData) { - $sUser = \trim($this->HasServer('REMOTE_USER') ? $this->GetServer('REMOTE_USER', '') : ''); + $sUser = \trim($this->GetServer('REMOTE_USER', '')); $sHost = (0 < \strlen($sUser) ? $sUser.'@' : '').$sHost; } @@ -529,7 +444,7 @@ class Http } else { - $this->StatusHeader(304); + static::StatusHeader(304); $bResult = true; } } @@ -570,9 +485,8 @@ class Http } } - public function StatusHeader(int $iStatus, string $sCustomStatusText = '') : void + public static function StatusHeader(int $iStatus, string $sCustomStatusText = '') : void { - $iStatus = (int) $iStatus; if (99 < $iStatus) { $aStatus = array( @@ -588,11 +502,15 @@ class Http 416 => 'Requested range not satisfiable' ); - $sCustomStatusText = \trim($sCustomStatusText); - $sHeaderHead = \ini_get('cgi.rfc2616_headers') && false !== \strpos(\strtolower(\php_sapi_name()), 'cgi') ? 'Status:' : $this->ServerProtocol(); $sHeaderText = (0 === \strlen($sCustomStatusText) && isset($aStatus[$iStatus]) ? $aStatus[$iStatus] : $sCustomStatusText); - \header(\trim($sHeaderHead.' '.$iStatus.' '.$sHeaderText), true, $iStatus); + \http_response_code($iStatus); + if (isset($_SERVER['SERVER_PROTOCOL'])) { + \header("{$_SERVER['SERVER_PROTOCOL']} {$iStatus} {$sHeaderText}", true, $iStatus); + } + if (\ini_get('cgi.rfc2616_headers') && false !== \strpos(\strtolower(\php_sapi_name()), 'cgi')) { + \header("Status: {$iStatus} {$sHeaderText}"); + } } } @@ -602,9 +520,9 @@ class Http return '' === $sUrl ? '/' : '/'.$sUrl.'/'; } - public function GetUrl() + public function GetUrl() : string { - return $this->GetServer('REQUEST_URI', ''); + return $_SERVER['REQUEST_URI'] ?? ''; } public function GetFullUrl() : string diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php index 6935da9b4..6a1c4b024 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php @@ -298,7 +298,7 @@ class Actions public function ParseQueryString(): string { - $sQuery = \trim($this->Http()->GetQueryString()); + $sQuery = \trim($_SERVER['QUERY_STRING'] ?? ''); $iPos = \strpos($sQuery, '&'); if (0 < $iPos) { @@ -307,7 +307,7 @@ class Actions $sQuery = \trim(\trim($sQuery), ' /'); - $aSubQuery = $this->Http()->GetQuery('q'); + $aSubQuery = $_GET['q'] ?? null; if (\is_array($aSubQuery)) { $aSubQuery = \array_map(function ($sS) { return \trim(\trim($sS), ' /'); @@ -2021,7 +2021,7 @@ class Actions if (!empty($sKey) && ($bForce || $this->Config()->Get('cache', 'enable', true) && $this->Config()->Get('cache', 'http', true))) { $sIfNoneMatch = $this->Http()->GetHeader('If-None-Match', ''); if ($this->etag($sKey) === $sIfNoneMatch) { - $this->Http()->StatusHeader(304); + \MailSo\Base\Http::StatusHeader(304); $this->cacheByKey($sKey); exit(0); } diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Raw.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Raw.php index 08e7e3830..1bd539b97 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Raw.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Raw.php @@ -303,7 +303,7 @@ trait Raw { $iFullContentLength = \strlen($sLoadedData); - $self->Http()->StatusHeader(206); + \MailSo\Base\Http::StatusHeader(206); $iRangeStart = (int) $sRangeStart; $iRangeEnd = (int) $sRangeEnd; diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Service.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Service.php index 2fbd9fac9..e051540fa 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Service.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Service.php @@ -110,7 +110,7 @@ class Service if ($bAdmin && !$this->oActions->Config()->Get('security', 'allow_admin_panel', true)) { - $this->oHttp->StatusHeader(403); + \MailSo\Base\Http::StatusHeader(403); echo $this->oServiceActions->ErrorTemplates('Access Denied.', 'Access to the SnappyMail Admin Panel is not allowed!', true); diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php b/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php index 5e0b7fe8f..78d522d08 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php @@ -81,7 +81,7 @@ class ServiceActions $aResponseItem = null; $oException = null; - $sAction = $this->oHttp->GetPost('Action'); + $sAction = $_POST['Action'] ?? null; if (empty($sAction) && $this->oHttp->IsGet() && !empty($this->aPaths[2])) { $sAction = $this->aPaths[2]; @@ -93,7 +93,7 @@ class ServiceActions { if ($this->oHttp->IsPost() && $this->Config()->Get('security', 'csrf_protection', false) && - $this->oHttp->GetPost('XToken', '') !== Utils::GetCsrfToken()) + ($_POST['XToken'] ?? '') !== Utils::GetCsrfToken()) { throw new Exceptions\ClientException(Notifications::InvalidToken); } @@ -107,7 +107,7 @@ class ServiceActions $this->Logger()->Write('Action: '.$sMethodName, \MailSo\Log\Enumerations\Type::NOTE, 'JSON'); - $aPost = $this->oHttp->GetPostAsArray(); + $aPost = $_POST ?? null; if ($aPost) { $this->oActions->SetActionParams($aPost, $sMethodName); @@ -222,7 +222,7 @@ class ServiceActions if (\method_exists($this->oActions, 'Append') && \is_callable(array($this->oActions, 'Append'))) { - $this->oActions->SetActionParams($this->oHttp->GetPostAsArray(), 'Append'); + isset($_POST) && $this->oActions->SetActionParams($_POST, 'Append'); $bResponse = \call_user_func(array($this->oActions, 'Append')); } } @@ -291,7 +291,7 @@ class ServiceActions if (\method_exists($this->oActions, $sAction) && \is_callable(array($this->oActions, $sAction))) { - $aActionParams = $this->oHttp->GetQueryAsArray(); + $aActionParams = isset($_GET) && \is_array($_GET) ? $_GET : null; $aActionParams['File'] = $aFile; $aActionParams['Error'] = $iError; @@ -372,7 +372,7 @@ class ServiceActions if (!$bResult) { - $this->oHttp->StatusHeader(404); + \MailSo\Base\Http::StatusHeader(404); } return ''; @@ -629,7 +629,7 @@ class ServiceActions { $this->oHttp->ServerNoCache(); - $sTo = \trim($this->oHttp->GetQuery('to', '')); + $sTo = \trim($_GET['to'] ?? ''); if (!empty($sTo) && \preg_match('/^mailto:/i', $sTo)) { $oAccount = $this->oActions->GetAccountFromSignMeToken(); @@ -660,7 +660,7 @@ class ServiceActions $oAccount = null; $bLogout = true; - $sSsoHash = $this->oHttp->GetRequest('hash', ''); + $sSsoHash = $_REQUEST['hash'] ?? ''; if (!empty($sSsoHash)) { $mData = null; @@ -738,8 +738,8 @@ class ServiceActions $oAccount = null; $bLogout = true; - $sEmail = $this->oHttp->GetEnv('REMOTE_USER', ''); - $sPassword = $this->oHttp->GetEnv('REMOTE_PASSWORD', ''); + $sEmail = $_ENV['REMOTE_USER'] ?? ''; + $sPassword = $_ENV['REMOTE_PASSWORD'] ?? ''; if (0 < \strlen($sEmail) && 0 < \strlen(\trim($sPassword))) { @@ -772,7 +772,7 @@ class ServiceActions $oAccount = null; $bLogout = true; - switch (\strtolower($this->oHttp->GetRequest('Output', 'Redirect'))) + switch (\strtolower($_REQUEST['Output'] ?? 'Redirect')) { case 'json':