Added system check requirements and more (#645)

* Added system check requirements and more

* Update IsHelper.php
This commit is contained in:
Roman Kelesidis 2023-03-20 17:09:51 +07:00 committed by GitHub
commit 4d812bb1fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 130 additions and 11 deletions

View file

@ -197,9 +197,24 @@ switch ($bb_cfg['datastore_type']) {
$datastore = new TorrentPier\Legacy\Datastore\File($bb_cfg['cache']['db_dir'] . 'datastore/', $bb_cfg['cache']['prefix']);
}
function is_ajax(): bool
{
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if (CHECK_REQIREMENTS['status'] && !CACHE('bb_cache')->get('system_req')) {
// [1] Check PHP Version
if (!\TorrentPier\Helpers\IsHelper::isPHP(CHECK_REQIREMENTS['php_min_version'])) {
die("TorrentPier requires PHP version " . CHECK_REQIREMENTS['php_min_version'] . "+ Your PHP version " . PHP_VERSION);
}
// [2] Check installed PHP Extensions on server
$data = [];
foreach (CHECK_REQIREMENTS['ext_list'] as $ext) {
if (!extension_loaded($ext)) {
$data[] = $ext;
}
}
if (!empty($data)) {
die(sprintf("TorrentPier requires %s extension(s) installed on server", implode(', ', $data)));
}
CACHE('bb_cache')->set('system_req', true);
}
function sql_dbg_enabled()

View file

@ -11,6 +11,24 @@ if (!defined('BB_ROOT')) {
die(basename(__FILE__));
}
// System
define('CHECK_REQIREMENTS', [
'status' => true,
'php_min_version' => '7.4.0',
'ext_list' => [
'json',
'gd',
'zlib',
'curl',
'tidy',
'mysqli',
'bcmath',
'intl',
'xml',
'xmlwriter',
],
]);
// Root path
$rootPath = __DIR__;
if (DIRECTORY_SEPARATOR != '/') $rootPath = str_replace(DIRECTORY_SEPARATOR, '/', $rootPath);

View file

@ -11,13 +11,6 @@ if (!defined('BB_ROOT')) {
die(basename(__FILE__));
}
/**
* Check PHP version
*/
if (PHP_VERSION_ID < 70400) {
die('TorrentPier requires PHP version 7.4+. Your PHP version ' . PHP_VERSION);
}
/**
* Define some basic configuration arrays
*/

93
src/Helpers/IsHelper.php Normal file
View file

@ -0,0 +1,93 @@
<?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\Helpers;
/**
* Class IsHelper
* @package TorrentPier\Helpers
*/
class IsHelper
{
/**
* Determines if the current version of PHP is equal to or greater than the supplied value
*
* @param string $version
* @return bool TRUE if the current version is $version or higher
*/
public static function isPHP(string $version): bool
{
static $_is_php;
$version = (string)$version;
if (!isset($_is_php[$version])) {
$_is_php[$version] = version_compare(PHP_VERSION, $version, '>=');
}
return $_is_php[$version];
}
/**
* Return true if ajax request
*
* @return bool
*/
public static function isAJAX(): bool
{
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
}
/**
* Return true if server have SSL
*
* @return bool
*/
public static function isHTTPS(): bool
{
$is_secure = false;
if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
$is_secure = true;
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
$is_secure = true;
}
return $is_secure;
}
/**
* Return true if $value contains numbers
*
* @param string $value
* @return bool
*/
public static function isContainsNums(string $value): bool
{
return preg_match('@[[:digit:]]@', $value);
}
/**
* Return true if $value contains letters (Uppercase included)
*
* @param string $value
* @param bool $uppercase
* @return bool
*/
public static function isContainsLetters(string $value, bool $uppercase = false): bool
{
return $uppercase ? preg_match('@[A-Z]@', $value) : preg_match('@[a-z]@', $value);
}
/**
* Return true if $value contains special symbols
*
* @param string $value
* @return bool
*/
public static function isContainsSpecSymbols(string $value): bool
{
return preg_match('@[[:punct:]]@', $value);
}
}

View file

@ -74,7 +74,7 @@ class Ajax
// Action params
$action_params = null;
if (!is_ajax()) {
if (!\TorrentPier\Helpers\IsHelper::isAJAX()) {
$this->ajax_die('Not AJAX request', E_AJAX_NOT_REQUEST);
}