add update

This commit is contained in:
xiaomlove
2021-01-21 20:42:22 +08:00
parent d5c4166c91
commit 4c9a4fc463
3 changed files with 93 additions and 26 deletions

View File

@@ -10,6 +10,7 @@ if (!empty($_SERVER['HTTP_X_REQUEST_ID'])) {
define('REQUEST_ID', intval(NEXUS_START * 10000));
}
define('ROOT_PATH', $rootpath);
define('VERSION', '1.6.0');
define('IS_ANNOUNCE', (basename($_SERVER['SCRIPT_FILENAME']) == 'announce.php'));
require $rootpath . 'include/database/interface_db.php';

View File

@@ -1,7 +1,4 @@
<?php
if(!defined('IN_TRACKER'))
die('Hacking attempt!');
function get_global_sp_state()
{
global $Cache;
@@ -256,38 +253,49 @@ function get_setting($name = null)
return arr_get($settings, $name);
}
function env($key, $default = null)
function env($key = null, $default = null)
{
static $env;
if (is_null($env)) {
$envFile = ROOT_PATH . '.env';
if (!file_exists($envFile)) {
throw new \RuntimeException(".env file is not exists in the root path.");
}
$fp = fopen($envFile, 'r');
if ($fp === false) {
throw new \RuntimeException(".env file: $envFile is not readable.");
}
while ($line = trim(fgets($fp))) {
if (empty($line)) {
continue;
}
$pos = strpos($line, '=');
if ($pos <= 0) {
continue;
}
if (mb_substr($line, 0, 1, 'utf-8') == '#') {
continue;
}
$lineKey = normalize_env(mb_substr($line, 0, $pos, 'utf-8'));
$lineValue = normalize_env(mb_substr($line, $pos + 1, null, 'utf-8'));
$env[$lineKey] = $lineValue;
}
$env = readEnvFile($envFile);
}
if (is_null($key)) {
return $env;
}
return $env[$key] ?? $default;
}
function readEnvFile($envFile)
{
if (!file_exists($envFile)) {
throw new \RuntimeException("env file is not exists in the root path.");
}
$env = [];
$fp = fopen($envFile, 'r');
if ($fp === false) {
throw new \RuntimeException(".env file: $envFile is not readable.");
}
while (($line = fgets($fp)) !== false) {
$line = trim($line);
if (empty($line)) {
continue;
}
$pos = strpos($line, '=');
if ($pos <= 0) {
continue;
}
if (mb_substr($line, 0, 1, 'utf-8') == '#') {
continue;
}
$lineKey = normalize_env(mb_substr($line, 0, $pos, 'utf-8'));
$lineValue = normalize_env(mb_substr($line, $pos + 1, null, 'utf-8'));
$env[$lineKey] = $lineValue;
}
return $env;
}
function normalize_env($value)
{
$value = trim($value);

58
include/update.php Normal file
View File

@@ -0,0 +1,58 @@
<?php
/**
* update 1.5 to 1.6, run in console
* @author xiaomlove<1939737565@qq.com>
*/
function printLine($msg, $exit = false)
{
echo sprintf("[%s] %s%s", date('Y-m-d H:i:s'), $msg, PHP_EOL);
if ($exit) {
exit(0);
}
}
if (php_sapi_name() != 'cli') {
printLine("Please run in console!", true);
}
//environment check
printLine("Checking the environment...");
if (version_compare(PHP_VERSION, '7.2.0', '<')) {
printLine(sprintf('Your PHP version: %s not match, require >= 7.2.0', PHP_VERSION));
}
$requireExtensions = ['mysqli', 'mbstring', 'gd', 'json'];
foreach ($requireExtensions as $ext) {
if (extension_loaded($ext)) {
printLine("extension: $ext is loaded.");
} else {
printLine("Error: required extension: $ext is not loaded!", true);
}
}
if (!extension_loaded('redis')) {
printLine("warning: redis is not loaded. highly recommand install it, see: https://pecl.php.net/package/redis/");
}
$gdInfo = gd_info();
$gdShouldAvaliable = ['JPEG Support', 'PNG Support', 'GIF Read Support'];
foreach ($gdShouldAvaliable as $value) {
if (!isset($gdInfo[$value]) || !$gdInfo[$value]) {
printLine("Error: gd $value is not available", true);
}
}
//create .env file
$rootPath = dirname(__DIR__);
$envExample = "$rootPath/.env.example";
$env = "$rootPath/.env";
if (!file_exists("$rootPath.env")) {
printLine("Error: $envExample not exists.");
}
$copyResult = copy($envExample, $env);
if ($copyResult !== true) {
}