diff --git a/.env.example b/.env.example index bdbdebb7..438b2c46 100644 --- a/.env.example +++ b/.env.example @@ -6,4 +6,11 @@ MYSQL_DATABASE=nexusphp REDIS_HOST=127.0.0.1 REDIS_PORT=6379 -REDIS_DATABASE=0 \ No newline at end of file +REDIS_DATABASE=0 + +USE_CRON_TRIGGER_CLEANUP=false + +LOG_FILE=/tmp/nexus.log +LOG_SPLIT=daily + +TIMEZONE=PRC \ No newline at end of file diff --git a/config/nexus.php b/config/nexus.php new file mode 100644 index 00000000..00088fb0 --- /dev/null +++ b/config/nexus.php @@ -0,0 +1,13 @@ + env('TIMEZONE', 'PRC'), + + 'log_file' => env('LOG_FILE', '/tmp/nexus.log'), + + 'log_split' => env('LOG_SPLIT', 'daily'), + + 'use_cron_trigger_cleanup' => env('USE_CRON_TRIGGER_CLEANUP', false), + +]; \ No newline at end of file diff --git a/include/cleanup.php b/include/cleanup.php index 9bdf1180..200fe8a3 100644 --- a/include/cleanup.php +++ b/include/cleanup.php @@ -4,6 +4,7 @@ if(!defined('IN_TRACKER')) die('Hacking attempt!'); function printProgress($msg) { + do_log($msg); echo $msg.'...done
'; ob_flush(); flush(); diff --git a/include/config.php b/include/config.php index 955d75c7..a59e53b9 100644 --- a/include/config.php +++ b/include/config.php @@ -401,7 +401,7 @@ $SUBSPATH = "subs"; //Make sure you have wget installed on your OS //replace "http://www.nexusphp.com/" with your own site address -$useCronTriggerCleanUp = config('use_cron_trigger_clean_up', false); +$useCronTriggerCleanUp = config('nexus.use_cron_trigger_cleanup', false); //some promotion rules //$promotionrules_torrent = array(0 => array("mediumid" => array(1), "promotion" => 5), 1 => array("mediumid" => array(3), "promotion" => 5), 2 => array("catid" => array(402), "standardid" => array(3), "promotion" => 4), 3 => array("catid" => array(403), "standardid" => array(3), "promotion" => 4)); $promotionrules_torrent = array(); diff --git a/include/core.php b/include/core.php index e66edd18..bcbe8233 100644 --- a/include/core.php +++ b/include/core.php @@ -2,6 +2,18 @@ if(!defined('IN_TRACKER')) { die('Hacking attempt!'); } +define('ROOT_PATH', $rootpath); +define('VERSION_NUMBER', '1.6.0'); +define('IS_ANNOUNCE', (basename($_SERVER['SCRIPT_FILENAME']) == 'announce.php')); +if (!empty($_SERVER['HTTP_X_REQUEST_ID'])) { + define('REQUEST_ID', $_SERVER['HTTP_X_REQUEST_ID']); +} else { + define('REQUEST_ID', intval(NEXUS_START * 10000)); +} +ini_set('date.timezone', config('nexus.timezone')); +ini_set('error_reporting', E_ALL); +ini_set('display_errors', 0); + if (!file_exists($rootpath . '.env')) { $installScriptRelativePath = 'install/install.php'; $installScriptFile = $rootpath . "public/$installScriptRelativePath"; @@ -10,16 +22,8 @@ if (!file_exists($rootpath . '.env')) { exit(0); } } -error_reporting(E_ALL); -ini_set('display_errors', 0); -if (!empty($_SERVER['HTTP_X_REQUEST_ID'])) { - define('REQUEST_ID', $_SERVER['HTTP_X_REQUEST_ID']); -} else { - define('REQUEST_ID', intval(NEXUS_START * 10000)); -} -define('ROOT_PATH', $rootpath); -define('VERSION_NUMBER', '1.6.0'); -define('IS_ANNOUNCE', (basename($_SERVER['SCRIPT_FILENAME']) == 'announce.php')); + + require $rootpath . 'vendor/autoload.php'; require $rootpath . 'nexus/Database/helpers.php'; require $rootpath . 'classes/class_advertisement.php'; @@ -31,7 +35,6 @@ if (!IS_ANNOUNCE) { $Cache = new class_cache_redis(); //Load the caching class $Cache->setLanguageFolderArray(get_langfolder_list()); define('TIMENOW', time()); -define('TIMENOW_STRING', date('Y-m-d H:i:s')); $USERUPDATESET = array(); $query_name=array(); diff --git a/include/functions.php b/include/functions.php index 579f9d1e..4dc75011 100644 --- a/include/functions.php +++ b/include/functions.php @@ -1870,19 +1870,21 @@ function userlogin() { function autoclean() { global $autoclean_interval_one, $rootpath; $now = TIMENOW; - $res = sql_query("SELECT value_u FROM avps WHERE arg = 'lastcleantime'"); $row = mysql_fetch_array($res); if (!$row) { + do_log("SELECT value_u FROM avps WHERE arg = 'lastcleantime', empty"); sql_query("INSERT INTO avps (arg, value_u) VALUES ('lastcleantime',$now)") or sqlerr(__FILE__, __LINE__); return false; } $ts = $row[0]; if ($ts + $autoclean_interval_one > $now) { + do_log("ts: {$ts} + autoclean_interval_one: $autoclean_interval_one > now: $now"); return false; } sql_query("UPDATE avps SET value_u=$now WHERE arg='lastcleantime' AND value_u = $ts") or sqlerr(__FILE__, __LINE__); if (!mysql_affected_rows()) { + do_log("UPDATE avps SET value_u=$now WHERE arg='lastcleantime' AND value_u = $ts, affectedRows = 0"); return false; } require_once($rootpath . 'include/cleanup.php'); diff --git a/include/functions_announce.php b/include/functions_announce.php index 4ddea032..d3be7990 100644 --- a/include/functions_announce.php +++ b/include/functions_announce.php @@ -5,15 +5,7 @@ if(!defined('IN_TRACKER')) function dbconn_announce() { - $config = config('database.mysql'); - - if (!@mysql_connect($config['host'], $config['username'], $config['password'], $config['database'], $config['port'])) - { - die('dbconn: mysql_connect: ' . mysql_error()); - } - mysql_query("SET NAMES UTF8"); - mysql_query("SET collation_connection = 'utf8_general_ci'"); - mysql_query("SET sql_mode=''"); + \Nexus\Database\DB::getInstance()->autoConnect(); } function hash_where_arr($name, $hash_arr) { diff --git a/include/globalfunctions.php b/include/globalfunctions.php index e8c25a45..824e2e8e 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -147,18 +147,14 @@ function dd($vars) function do_log($log, $level = 'info') { - global $TWEAK; - $logging = sys_get_temp_dir() . '/nexus_' . date('Y-m-d') . '.log'; - if (!empty($TWEAK['logging'])) { - $logging = $TWEAK['logging']; - } - if (($fd = fopen($logging, 'a')) !== false) { + $logFile = getLogFile(); + if (($fd = fopen($logFile, 'a')) !== false) { $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); $content = sprintf( "[%s] [%s] [%s] %s:%s %s%s%s %s%s", date('Y-m-d H:i:s'), $level, - REQUEST_ID, + defined('REQUEST_ID') ? REQUEST_ID : '', $backtrace[0]['file'] ?? '', $backtrace[0]['line'] ?? '', $backtrace[1]['class'] ?? '', @@ -172,6 +168,43 @@ function do_log($log, $level = 'info') } } +function getLogFile() +{ + static $logFile; + if (!is_null($logFile)) { + return $logFile; + } + $config = config('nexus'); + $logFile = sys_get_temp_dir() . '/nexus_' . date('Y-m-d') . '.log'; + if (!empty($config['log_file'])) { + $logFile = $config['log_file']; + } + $validSplit = ['daily', 'monthly']; + if (empty($config['log_split']) || !in_array($config['log_split'], $validSplit)) { + return $logFile; + } + $lastDotPos = strrpos($logFile, '.'); + if ($lastDotPos !== false) { + $prefix = substr($logFile, 0, $lastDotPos); + $suffix = substr($logFile, $lastDotPos); + } else { + $prefix = $logFile; + $suffix = ''; + } + switch ($config['log_split']) { + case 'daily': + $logFile = sprintf('%s-%s%s', $prefix, date('Y-m-d'), $suffix); + break; + case 'monthly': + $logFile = sprintf('%s-%s%s', $prefix, date('Ym'), $suffix); + break; + default: + break; + } + return $logFile; + +} + function config($key, $default = null) { static $configs; diff --git a/lang/chs/lang_settings.php b/lang/chs/lang_settings.php index 18d56376..d9532b29 100644 --- a/lang/chs/lang_settings.php +++ b/lang/chs/lang_settings.php @@ -680,8 +680,6 @@ $lang_settings = array 'text_email_notification_note' => "是否允许用户在收取新短讯、评论等时候收到邮件提示。", 'text_users_get' => "首次升级至此等级的用户将获得", 'text_invitations_default' => "个邀请名额。默认", - 'row_logging' => '运行日志路径', - 'text_logging_note' => '请填写你的运行时日志路径', ); ?> diff --git a/lang/cht/lang_settings.php b/lang/cht/lang_settings.php index 8dd1745b..76ddcc3e 100644 --- a/lang/cht/lang_settings.php +++ b/lang/cht/lang_settings.php @@ -680,8 +680,6 @@ $lang_settings = array 'text_email_notification_note' => "是否允許用戶在收取新短訊、評論等時候收到郵件輔助說明。", 'text_users_get' => "首次升級至此等級的用戶將獲得", 'text_invitations_default' => "個邀請名額。預設", - 'row_logging' => '運行日誌路徑', - 'text_logging_note' => '請填寫妳的運行時日誌路徑', ); ?> diff --git a/lang/en/lang_settings.php b/lang/en/lang_settings.php index a03fddb8..87e9c819 100644 --- a/lang/en/lang_settings.php +++ b/lang/en/lang_settings.php @@ -680,8 +680,6 @@ $lang_settings = array 'text_email_notification_note' => "Allow users to receive email notification when he gets new pm, comment, etc.", 'text_users_get' => "Users would get ", 'text_invitations_default' => " invitations the first time he is promoted to this class. Default ", - 'row_logging' => 'Runtime log path', - 'text_logging_note' => 'Please enter your runtime log path', ); ?> diff --git a/nexus/Database/DB.php b/nexus/Database/DB.php index 2ccbd5f6..3d59e62a 100644 --- a/nexus/Database/DB.php +++ b/nexus/Database/DB.php @@ -8,8 +8,6 @@ class DB private static $instance; - private static $queries = []; - private $isConnected = false; private function __construct() @@ -55,8 +53,6 @@ class DB $this->driver->query("SET collation_connection = 'utf8_general_ci'"); $this->driver->query("SET sql_mode=''"); $this->isConnected = true; - $log = json_encode(func_get_args()); - do_log($log); return true; } @@ -66,7 +62,6 @@ class DB return null; } $config = config('database.mysql'); - do_log(json_encode($config)); return $this->connect($config['host'], $config['username'], $config['password'], $config['database'], $config['port']); } diff --git a/nexus/Install/Install.php b/nexus/Install/Install.php index af234eb3..fc080b59 100644 --- a/nexus/Install/Install.php +++ b/nexus/Install/Install.php @@ -10,6 +10,8 @@ class Install private $minimumPhpVersion = '7.2.0'; + private $progressKeyPrefix = '__step'; + protected $steps = ['环境检测', '添加 .env 文件', '创建数据表', '导入数据', '创建管理员账号']; protected $initializeTables = [ @@ -18,9 +20,15 @@ class Install 'searchbox', 'secondicons', 'sources', 'standards', 'stylesheets', 'sysoppanel', 'teams', 'torrents_state', 'uploadspeed', 'agent_allowed_family', ]; + protected $envNames = ['MYSQL_HOST', 'MYSQL_PORT', 'MYSQL_USERNAME', 'MYSQL_PASSWORD', 'MYSQL_DATABASE', 'REDIS_HOST', 'REDIS_PORT', 'REDIS_DATABASE']; + + public function __construct() { + if (!session_id()) { + session_start(); + } $this->currentStep = min(intval($_REQUEST['step'] ?? 1) ?: 1, count($this->steps) + 1); } @@ -29,6 +37,31 @@ class Install return $this->currentStep; } + public function canAccessStep($step) + { + for ($i = 1; $i < $step; $i++) { + $progressKey = $this->getProgressKey($i); + if (!isset($_SESSION[$progressKey])) { + do_log("check step: $i, session doesn't have, session: " . json_encode($_SESSION)); + return false; + } + } + do_log("check step: $step, can access, session: " . json_encode($_SESSION)); + return true; + } + + public function doneStep($step) + { + $progressKey = $this->getProgressKey($step); + do_log("doneStep: $step, $progressKey = 1"); + $_SESSION[$progressKey] = 1; + } + + private function getProgressKey($step) + { + return $this->progressKeyPrefix . $step; + } + public function getLogFile() { return sprintf('%s/nexus_install_%s.log', sys_get_temp_dir(), date('Ymd')); @@ -221,6 +254,7 @@ class Install public function nextStep() { + $this->doneStep($this->currentStep); $this->gotoStep($this->currentStep + 1); } @@ -308,13 +342,14 @@ class Install } $mergeData = array_merge($envExampleData, $envData); $formControls = []; - foreach ($mergeData as $key => $value) { - if (isset($_POST[$key])) { - $value = $_POST[$key]; + foreach ($this->envNames as $name) { + $value = $mergeData[$name]; + if (isset($_POST[$name])) { + $value = $_POST[$name]; } $formControls[] = [ - 'label' => $key, - 'name' => $key, + 'label' => $name, + 'name' => $name, 'value' => $value, ]; } @@ -324,6 +359,10 @@ class Install public function createAdministrator($username, $email, $password, $confirmPassword) { + $count = get_row_count('users', 'where class = 16'); + if ($count > 0) { + throw new \InvalidArgumentException("Administrator already exists"); + } if (!validusername($username)) { throw new \InvalidArgumentException("Innvalid username: $username"); } @@ -398,7 +437,7 @@ class Install } fwrite($fp, $content); fclose($fp); - $this->doLog("[CREATE ENV] $envFile with content: \n $content"); + $this->doLog("[CREATE ENV] $envFile with content: $content"); return true; } @@ -464,6 +503,12 @@ class Install if (!in_array($table, $this->initializeTables)) { continue; } + //if table not empty, skip + $count = get_row_count($table); + if ($count > 0) { + $this->doLog("[IMPORT DATA] $table, not empty, skip"); + continue; + } $this->doLog("[IMPORT DATA] $table, $sql"); sql_query("truncate table $table"); sql_query($sql); diff --git a/nexus/Install/install/install.php b/nexus/Install/install/install.php index 62b7d004..60869a12 100644 --- a/nexus/Install/install/install.php +++ b/nexus/Install/install/install.php @@ -1,9 +1,6 @@ currentStep(); $maxStep = $install->maxStep(); +if (!$install->canAccessStep($currentStep)) { + $install->gotoStep(1); +} +$error = $copy = ''; //step 1 if ($currentStep == 1) { @@ -35,7 +36,7 @@ if ($currentStep == 2) { $install->createEnvFile($_POST); $install->nextStep(); } catch (\Exception $exception) { - $_SESSION['error'] = $exception->getMessage(); + $error = $exception->getMessage(); break; } break; @@ -60,7 +61,7 @@ if ($currentStep == 3) { $install->createTable($shouldCreateTable); $install->nextStep(); } catch (\Exception $exception) { - $_SESSION['error'] = $exception->getMessage(); + $error = $exception->getMessage(); break; } break; @@ -90,7 +91,7 @@ if ($currentStep == 3) { // sql_query($sqlAlter); // } // } catch (\Exception $e) { -// $_SESSION['error'] = $e->getMessage(); +// $error = $e->getMessage(); // break; // } // } @@ -113,7 +114,7 @@ if ($currentStep == 4) { $install->importInitialData(); $install->nextStep(); } catch (\Exception $e) { - $_SESSION['error'] = $e->getMessage(); + $error = $e->getMessage(); break; } break; @@ -126,7 +127,7 @@ if ($currentStep == 5) { $install->createAdministrator($_POST['username'], $_POST['email'], $_POST['password'], $_POST['confirm_password']); $install->nextStep(); } catch (\Exception $exception) { - $_SESSION['error'] = $exception->getMessage(); + $error = $exception->getMessage(); } } $pass = true; @@ -134,7 +135,7 @@ if ($currentStep == 5) { ['label' => '用户名', 'name' => 'username', 'value' => $_POST['username'] ?? ''], ['label' => '邮箱', 'name' => 'email', 'value' => $_POST['email'] ?? ''], ['label' => '密码', 'name' => 'password', 'value' => $_POST['password'] ?? ''], - ['label' => '重复密码', 'name' => 'confirm_password', 'value' => $_POST['confirm_password'] ?? ''], + ['label' => '确认密码', 'name' => 'confirm_password', 'value' => $_POST['confirm_password'] ?? ''], ]; } ?> @@ -186,13 +187,13 @@ if ($currentStep == 5) { } echo''; - if (!empty($_SESSION['error'])) { - echo sprintf('
Error: %s
', nl2br($_SESSION['error'])); - unset($_SESSION['error']); + if (!empty($error)) { + echo sprintf('
Error: %s
', nl2br($error)); + unset($error); } - if (!empty($_SESSION['copy'])) { - echo sprintf('
', $_SESSION['copy']); - unset($_SESSION['copy']); + if (!empty($copy)) { + echo sprintf('
', $copy); + unset($copy); } ?>
@@ -207,7 +208,7 @@ if ($currentStep == 5) {
- 欢迎使用 NexusPHP 安装程序,如有疑问,点击这里获取参考。 + 欢迎使用 NexusPHP 安装程序,如有疑问,点击这里获取帮助。