2019-05-02 01:33:33 +00:00
< ? php
2020-02-09 14:45:36 +00:00
/**
2021-03-29 06:40:20 +00:00
* @ copyright Copyright ( C ) 2010 - 2021 , the Friendica project
2020-02-09 14:45:36 +00:00
*
* @ license GNU AGPL version 3 or any later version
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*
*/
2019-05-02 01:33:33 +00:00
namespace Friendica\Module\Special ;
2021-10-29 23:21:07 +00:00
use Friendica\Core\Logger ;
2019-05-02 01:33:33 +00:00
use Friendica\Core\Renderer ;
use Friendica\Core\System ;
2020-01-18 21:07:07 +00:00
use Friendica\DI ;
2019-05-02 01:33:33 +00:00
/**
* This special module displays HTTPException when they are thrown in modules .
*
* @ package Friendica\Module\Special
*/
class HTTPException
{
/**
* Generates the necessary template variables from the caught HTTPException .
*
* Fills in the blanks if title or descriptions aren ' t provided by the exception .
*
* @ param \Friendica\Network\HTTPException $e
* @ return array [ '$title' => ... , '$description' => ... ]
*/
private static function getVars ( \Friendica\Network\HTTPException $e )
{
2021-10-29 23:21:07 +00:00
// Explanations are mostly taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
2020-12-31 02:15:01 +00:00
$vars = [
2021-10-31 04:54:24 +00:00
'$title' => $e -> getDescription () ? : 'Error ' . $e -> getCode (),
'$message' => $e -> getMessage () ? : $e -> getExplanation (),
2020-12-31 02:15:01 +00:00
'$back' => DI :: l10n () -> t ( 'Go back' ),
'$stack_trace' => DI :: l10n () -> t ( 'Stack trace:' ),
];
2020-01-19 14:40:56 +00:00
2021-11-04 20:29:59 +00:00
if ( DI :: app () -> isSiteAdmin ()) {
2020-12-31 02:15:01 +00:00
$vars [ '$thrown' ] = DI :: l10n () -> t ( 'Exception thrown in %s:%d' , $e -> getFile (), $e -> getLine ());
2020-01-19 14:40:56 +00:00
$vars [ '$trace' ] = $e -> getTraceAsString ();
}
return $vars ;
2019-05-02 01:33:33 +00:00
}
/**
* Displays a bare message page with no theming at all .
*
* @ param \Friendica\Network\HTTPException $e
2019-05-05 01:54:05 +00:00
* @ throws \Exception
2019-05-02 01:33:33 +00:00
*/
2021-11-14 22:13:47 +00:00
public function rawContent ( \Friendica\Network\HTTPException $e )
2019-05-02 01:33:33 +00:00
{
$content = '' ;
if ( $e -> getCode () >= 400 ) {
$tpl = Renderer :: getMarkupTemplate ( 'http_status.tpl' );
$content = Renderer :: replaceMacros ( $tpl , self :: getVars ( $e ));
}
2021-10-31 04:54:24 +00:00
System :: httpExit ( $e -> getCode (), $e -> getDescription (), $content );
2019-05-02 01:33:33 +00:00
}
/**
* Returns a content string that can be integrated in the current theme .
*
* @ param \Friendica\Network\HTTPException $e
* @ return string
2019-05-05 01:54:05 +00:00
* @ throws \Exception
2019-05-02 01:33:33 +00:00
*/
2021-11-14 22:13:47 +00:00
public function content ( \Friendica\Network\HTTPException $e ) : string
2019-05-02 01:33:33 +00:00
{
2021-10-31 04:54:24 +00:00
header ( $_SERVER [ " SERVER_PROTOCOL " ] . ' ' . $e -> getCode () . ' ' . $e -> getDescription ());
2019-05-02 01:33:33 +00:00
2021-10-29 23:21:07 +00:00
if ( $e -> getCode () >= 400 ) {
2021-10-31 04:54:24 +00:00
Logger :: debug ( 'Exit with error' , [ 'code' => $e -> getCode (), 'description' => $e -> getDescription (), 'query' => DI :: args () -> getQueryString (), 'callstack' => System :: callstack ( 20 ), 'method' => $_SERVER [ 'REQUEST_METHOD' ], 'agent' => $_SERVER [ 'HTTP_USER_AGENT' ] ? ? '' ]);
2021-10-29 23:21:07 +00:00
}
2019-05-02 01:33:33 +00:00
$tpl = Renderer :: getMarkupTemplate ( 'exception.tpl' );
return Renderer :: replaceMacros ( $tpl , self :: getVars ( $e ));
}
}