Cleanup MailSo\Base\Http

This commit is contained in:
djmaze 2021-07-14 11:26:48 +02:00
parent 51d9c3adbb
commit 09b3fa3141
5 changed files with 28 additions and 110 deletions

View file

@ -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

View file

@ -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);
}

View file

@ -303,7 +303,7 @@ trait Raw
{
$iFullContentLength = \strlen($sLoadedData);
$self->Http()->StatusHeader(206);
\MailSo\Base\Http::StatusHeader(206);
$iRangeStart = (int) $sRangeStart;
$iRangeEnd = (int) $sRangeEnd;

View file

@ -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);

View file

@ -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':