Added Http class implementation (#632)

* Added Http class implementation

* Applied requested changes
This commit is contained in:
Roman Kelesidis 2023-03-20 14:58:14 +07:00 committed by GitHub
commit b0a0ec1dd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 2 deletions

View file

@ -457,7 +457,7 @@ if (
* Exit if board is disabled via trigger * Exit if board is disabled via trigger
*/ */
if (($bb_cfg['board_disable'] || file_exists(BB_DISABLED)) && !defined('IN_ADMIN') && !defined('IN_AJAX') && !defined('IN_LOGIN')) { if (($bb_cfg['board_disable'] || file_exists(BB_DISABLED)) && !defined('IN_ADMIN') && !defined('IN_AJAX') && !defined('IN_LOGIN')) {
header('HTTP/1.0 503 Service Unavailable'); \TorrentPier\Common\Http::statusCode(503);
if ($bb_cfg['board_disable']) { if ($bb_cfg['board_disable']) {
// admin lock // admin lock
send_no_cache_headers(); send_no_cache_headers();

54
src/Common/Http.php Normal file
View file

@ -0,0 +1,54 @@
<?php
/**
* TorrentPier Bull-powered BitTorrent tracker engine
*
* @copyright Copyright (c) 2005-2023 TorrentPier (https://torrentpier.com)
* @link https://github.com/torrentpier/torrentpier for the canonical source repository
* @license https://github.com/torrentpier/torrentpier/blob/master/LICENSE MIT License
*/
namespace TorrentPier\Common;
/**
* Class Http
* @package TorrentPier\Common
*/
class Http
{
/**
* Set a HTTP response code
*
* @param int $statusCode
*/
public static function statusCode(int $statusCode): void
{
static $status_codes = null;
if ($status_codes === null) {
$status_codes = [
100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing',
200 => 'OK', 201 => 'Created', 202 => 'Accepted',
203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content',
206 => 'Partial Content', 207 => 'Multi-Status', 300 => 'Multiple Choices',
301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other',
304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect',
400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required',
403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed',
406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout',
409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required',
412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed',
422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency',
426 => 'Upgrade Required', 500 => 'Internal Server Error', 501 => 'Not Implemented',
502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported', 506 => 'Variant Also Negotiates', 507 => 'Insufficient Storage',
509 => 'Bandwidth Limit Exceeded', 510 => 'Not Extended'
];
}
if ($status_codes[$statusCode] !== null) {
$status_string = $statusCode . ' ' . $status_codes[$statusCode];
header($_SERVER['SERVER_PROTOCOL'] . ' ' . $status_string, true, $statusCode);
}
}
}

View file

@ -10,6 +10,7 @@
namespace TorrentPier\Legacy; namespace TorrentPier\Legacy;
use mysqli_result; use mysqli_result;
use TorrentPier\Common\Http;
/** /**
* Class SqlDb * Class SqlDb
@ -104,7 +105,7 @@ class SqlDb
if (mysqli_connect_error()) { if (mysqli_connect_error()) {
$server = DBG_USER ? $this->cfg['dbhost'] : ''; $server = DBG_USER ? $this->cfg['dbhost'] : '';
header('HTTP/1.0 503 Service Unavailable'); Http::statusCode(503);
bb_log(' ', "db_err/connect_failed_{$this->cfg['dbhost']}"); bb_log(' ', "db_err/connect_failed_{$this->cfg['dbhost']}");
die("Could not connect to mysql server $server"); die("Could not connect to mysql server $server");
} }