diff --git a/common.php b/common.php index 4ad1a9dd6..f65d6fb75 100644 --- a/common.php +++ b/common.php @@ -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() diff --git a/library/defines.php b/library/defines.php index ae12adccc..405900375 100644 --- a/library/defines.php +++ b/library/defines.php @@ -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); diff --git a/library/includes/init_bb.php b/library/includes/init_bb.php index da914c743..f387c6792 100644 --- a/library/includes/init_bb.php +++ b/library/includes/init_bb.php @@ -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 */ diff --git a/src/Helpers/IsHelper.php b/src/Helpers/IsHelper.php new file mode 100644 index 000000000..fa81566bc --- /dev/null +++ b/src/Helpers/IsHelper.php @@ -0,0 +1,93 @@ +='); + } + 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); + } +} diff --git a/src/Legacy/Ajax.php b/src/Legacy/Ajax.php index 5d612d077..dcec7d568 100644 --- a/src/Legacy/Ajax.php +++ b/src/Legacy/Ajax.php @@ -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); }