change file require order to fix get config from database

This commit is contained in:
xiaomlove
2021-01-12 01:54:46 +08:00
parent 7373ec16e8
commit f763eeac98
7 changed files with 160 additions and 52 deletions
+1 -1
View File
@@ -6,4 +6,4 @@ npm-debug.log
.env .env
/.settings /.settings
.buildpath .buildpath
.project .project
+19
View File
@@ -0,0 +1,19 @@
<?php
return [
'mysql' => [
'host' => env('MYSQL_HOST', '127.0.0.1'),
'port' => env('MYSQL_PORT', 3306),
'username' => env('MYSQL_USERNAME', 'root'),
'password' => env('MYSQL_PASSWORD', ''),
'database' => env('MYSQL_DATABASE', 'nexusphp'),
],
'redis' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
],
];
-13
View File
@@ -10,18 +10,5 @@ $showversion = " - Powered by ".PROJECTNAME;
$rootpath=realpath(dirname(__FILE__) . '/..'); $rootpath=realpath(dirname(__FILE__) . '/..');
set_include_path(get_include_path() . PATH_SEPARATOR . $rootpath); set_include_path(get_include_path() . PATH_SEPARATOR . $rootpath);
$rootpath .= "/"; $rootpath .= "/";
require $rootpath . 'include/config.php';
require $rootpath . 'include/functions.php';
require $rootpath . 'include/globalfunctions.php';
require $rootpath . get_langfile_path("functions.php");
require $rootpath . 'include/database/interface_db.php';
require $rootpath . 'include/database/class_db_mysqli.php';
require $rootpath . 'include/database/class_db.php';
require $rootpath . 'include/database/helpers.php';
require $rootpath . 'include/database/class_exception.php';
require $rootpath . 'classes/class_advertisement.php';
require $rootpath . 'classes/class_cache_redis.php';
require $rootpath . 'include/core.php'; require $rootpath . 'include/core.php';
+27 -23
View File
@@ -5,27 +5,30 @@ die('Hacking attempt!');
$CONFIGURATIONS = array('ACCOUNT', 'ADVERTISEMENT', 'ATTACHMENT', 'AUTHORITY', 'BASIC', 'BONUS', 'CODE', 'MAIN', 'SECURITY', 'SMTP', 'TORRENT', 'TWEAK'); $CONFIGURATIONS = array('ACCOUNT', 'ADVERTISEMENT', 'ATTACHMENT', 'AUTHORITY', 'BASIC', 'BONUS', 'CODE', 'MAIN', 'SECURITY', 'SMTP', 'TORRENT', 'TWEAK');
function ReadConfig ($configname = NULL) { function ReadConfig ($configname = NULL) {
// static $allConfig; static $allConfig;
// if (is_null($allConfig)) {
// $configs = config();
// foreach ($configs as $name => $value) {
// $prefix = strtoupper(strstr($name, '.', true));
// $pureName = substr($name, strpos($name, '.') + 1);
// $GLOBALS[$prefix][$pureName] = $value;
// }
// }
global $CONFIGURATIONS; global $CONFIGURATIONS;
if ($configname) {
$configname = basename($configname); if (is_null($allConfig)) {
$tmp = oldReadConfig($configname); $configs = config();
WriteConfig($configname, $tmp); foreach ($configs as $name => $value) {
@unlink('./config/'.$configname); $prefix = strtoupper(strstr($name, '.', true));
return $tmp; $pureName = substr($name, strpos($name, '.') + 1);
} else { $GLOBALS[$prefix][$pureName] = $value;
foreach ($CONFIGURATIONS as $CONFIGURATION) {
$GLOBALS[$CONFIGURATION] = ReadConfig($CONFIGURATION);
} }
} }
dd($GLOBALS);
// if ($configname) {
// $configname = basename($configname);
// $tmp = oldReadConfig($configname);
// WriteConfig($configname, $tmp);
// @unlink('./config/'.$configname);
// return $tmp;
// } else {
// foreach ($CONFIGURATIONS as $CONFIGURATION) {
// $GLOBALS[$CONFIGURATION] = ReadConfig($CONFIGURATION);
// }
// }
} }
function oldReadConfig ($configname) { function oldReadConfig ($configname) {
@@ -62,12 +65,13 @@ function oldReadConfig ($configname) {
} }
} }
ReadConfig();
if (file_exists('config/allconfig.php')) { //if (file_exists('config/allconfig.php')) {
require('config/allconfig.php'); // require('config/allconfig.php');
} else { //} else {
ReadConfig(); // ReadConfig();
} //}
$SITENAME = $BASIC['SITENAME']; $SITENAME = $BASIC['SITENAME'];
$BASEURL = $BASIC['BASEURL']; $BASEURL = $BASIC['BASEURL'];
$announce_urls = array(); $announce_urls = array();
+18 -1
View File
@@ -1,7 +1,24 @@
<?php <?php
if(!defined('IN_TRACKER')) if(!defined('IN_TRACKER'))
die('Hacking attempt!'); die('Hacking attempt!');
error_reporting(E_ERROR | E_PARSE); error_reporting(E_ALL);
require $rootpath . 'include/functions.php';
require $rootpath . 'include/globalfunctions.php';
require $rootpath . 'include/database/interface_db.php';
require $rootpath . 'include/database/class_db_mysqli.php';
require $rootpath . 'include/database/class_db.php';
require $rootpath . 'include/database/helpers.php';
require $rootpath . 'include/database/class_exception.php';
require $rootpath . 'classes/class_advertisement.php';
require $rootpath . 'classes/class_cache_redis.php';
require $rootpath . 'include/config.php';
require $rootpath . get_langfile_path("functions.php");
ini_set('display_errors', $TWEAK['display_errors']); ini_set('display_errors', $TWEAK['display_errors']);
$Cache = new RedisCache(); //Load the caching class $Cache = new RedisCache(); //Load the caching class
$Cache->setLanguageFolderArray(get_langfolder_list()); $Cache->setLanguageFolderArray(get_langfolder_list());
+8 -1
View File
@@ -39,7 +39,9 @@ class DB
public function connect($host, $username, $password, $database, $port) public function connect($host, $username, $password, $database, $port)
{ {
return $this->driver->connect($host, $username, $password, $database, $port); if (is_null($this->driver)) {
$this->driver->connect($host, $username, $password, $database, $port);
}
} }
public function query(string $sql) public function query(string $sql)
@@ -108,6 +110,11 @@ class DB
return $this->driver->freeResult($result); return $this->driver->freeResult($result);
} }
public function isConnected()
{
return !is_null($this->driver);
}
+87 -13
View File
@@ -1717,6 +1717,9 @@ function getExportedValue($input,$t = null) {
function dbconn($autoclean = false) function dbconn($autoclean = false)
{ {
if (DB::getInstance()->isConnected()) {
return;
}
global $lang_functions; global $lang_functions;
global $mysql_host, $mysql_user, $mysql_pass, $mysql_db; global $mysql_host, $mysql_user, $mysql_pass, $mysql_db;
global $useCronTriggerCleanUp; global $useCronTriggerCleanUp;
@@ -1770,6 +1773,9 @@ function get_user_row($id)
} }
function userlogin() { function userlogin() {
if (isset($GLOBALS['CURUSER'])) {
return;
}
global $lang_functions; global $lang_functions;
global $Cache; global $Cache;
global $SITE_ONLINE, $oldip; global $SITE_ONLINE, $oldip;
@@ -4427,7 +4433,6 @@ function do_log($log)
* *
* @author xiaomlove * @author xiaomlove
* @date 2021/1/11 * @date 2021/1/11
* @time 10:42
* @param null $name * @param null $name
* @param null $prefix * @param null $prefix
* @return mixed|string * @return mixed|string
@@ -4444,8 +4449,8 @@ function __($name = null, $prefix = null)
$prefix = strstr($prefix, '.php', true); $prefix = strstr($prefix, '.php', true);
} }
if (is_null($i18n)) { if (is_null($i18n)) {
//get all in18 may be used, incldue user locale and default locale, and name = _target(because it is common) or prefixed with given prefix //get all in18 may be used, incldue user locale and default locale, and name in('_target', 'functions') (because they are common) or prefixed with given prefix
$sql = "select locale, name, translation from i18n where locale in (" . sqlesc($userLocale) . ", " . sqlesc($defaultLocale) . ") and (name = '_target' or name like '{$prefix}%')"; $sql = "select locale, name, translation from i18n where locale in (" . sqlesc($userLocale) . ", " . sqlesc($defaultLocale) . ") and (name in ('_target', 'functions') or name like '{$prefix}%')";
$result = sql_query($sql); $result = sql_query($sql);
while ($row = mysql_fetch_assoc($result)) { while ($row = mysql_fetch_assoc($result)) {
$i18n[$row['locale']][$row['name']] = $row['translation']; $i18n[$row['locale']][$row['name']] = $row['translation'];
@@ -4460,24 +4465,44 @@ function __($name = null, $prefix = null)
} }
function config($key, $default = null)
{
static $configs;
if (is_null($configs)) {
//get all configuration from config file
$files = glob($rootpath . 'config/*.php');
foreach ($files as $file) {
$basename = basename($file);
if ($basename != 'allconfig.php') {
continue;
}
$values = require $file;
$configPrefix = strstr($basename, '.php', true);
foreach ($values as $key => $value) {
$configs["$configPrefix.$key"] = $value;
}
}
}
return $configs[$key] ?? $default;
}
/** /**
* get configuation for given name and prefix * get setting for given name and prefix
* *
* $name == null and $prefix == null, return all * $name == null and $prefix == null, return all
* $name == null and $prefix != null, return with specified prefix, but the result's prefix will be stripped * $name == null and $prefix != null, return with specified prefix, but the result's prefix will be stripped
* *
* @author xiaomlove * @author xiaomlove
* @date 2021/1/11 * @date 2021/1/11
* @time 16:37
* @param null $name * @param null $name
* @param null $prefix * @param null $prefix
* @return array|mixed|string * @return array|mixed|string
*/ */
function config($name = null, $prefix = null) function get_setting($name = null, $prefix = null)
{ {
static $config; static $settings;
if (is_null($config)) { if (is_null($settings)) {
//get all configuations //get all settings from database
$sql = "select config_name, config_value from configs"; $sql = "select config_name, config_value from configs";
$result = sql_query($sql); $result = sql_query($sql);
while ($row = mysql_fetch_assoc($result)) { while ($row = mysql_fetch_assoc($result)) {
@@ -4486,20 +4511,21 @@ function config($name = null, $prefix = null)
if (is_array($arr)) { if (is_array($arr)) {
$value = $arr; $value = $arr;
} }
$config[$row['config_name']] = $value; $settings[$row['config_name']] = $value;
} }
} }
if (!is_null($name)) { if (!is_null($name)) {
if (!is_null($prefix)) { if (!is_null($prefix)) {
$name = "$prefix.$name"; $name = "$prefix.$name";
} }
return $config[$name] ?? ''; return $settings[$name] ?? null;
} }
if (is_null($prefix)) { if (is_null($prefix)) {
return $config; return $settings;
} }
$filtered = []; $filtered = [];
foreach ($config as $name => $value) { foreach ($settings as $name => $value) {
if (preg_match("/^$prefix/", $name)) { if (preg_match("/^$prefix/", $name)) {
$nameWithoutPrefix = substr($name, strpos($name, '.') + 1); $nameWithoutPrefix = substr($name, strpos($name, '.') + 1);
$filtered[$nameWithoutPrefix] = $value; $filtered[$nameWithoutPrefix] = $value;
@@ -4509,4 +4535,52 @@ function config($name = null, $prefix = null)
} }
function env($key, $default = null)
{
static $env;
if (is_null($env)) {
$envFile = $rootpath . '.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, '=');
$key = normalize_env(mb_substr($line, 0, $pos, 'utf-8'));
$value = normalize_env(mb_substr($line, $pos + 1, null, 'utf-8'));
$env[$key] = $value;
}
}
return $env[$key] ?? $default;
}
function normalize_env($value)
{
$value = trim($value);
$toStrip = ['\'', '"'];
if (in_array(mb_substr($value, 0, 1, 'utf-8'), $toStrip)) {
$value = mb_substr($value, 1, null, 'utf-8');
}
if (in_array(mb_substr($value, -1, 'utf-8'), $toStrip)) {
$value = mb_substr($value, 0, -1, 'utf-8');
}
switch (strtolower($value)) {
case 'true':
return true;
case 'false':
return false;
case 'null':
return null;
default:
return $value;
}
}
?> ?>