diff --git a/nexus/Install/Install.php b/nexus/Install/Install.php new file mode 100644 index 00000000..9453a251 --- /dev/null +++ b/nexus/Install/Install.php @@ -0,0 +1,209 @@ +currentStep = min($_REQUEST['step'] ?? 1, count($this->steps)); + $this->logFile = $this->getLogFile(); + } + + protected function getLogFile() + { + return sprintf('%s/nexus_install_%s.log', sys_get_temp_dir(), date('Ymd')); + } + + public function doLog($log) + { + $log = sprintf('[%s] [%s] %s%s', date('Y-m-d H:i:s'), $this->currentStep, $log, PHP_EOL); + file_put_contents($this->logFile, $log, FILE_APPEND); + } + + public function listAllTableCreate($sqlFile = '') + { + if (empty($sqlFile)) { + $sqlFile = ROOT_PATH . '_db/dbstructure_v1.6.sql'; + } + $pattern = '/CREATE TABLE `(.*)`.*;/isU'; + $string = file_get_contents($sqlFile); + if ($string === false) { + throw new \RuntimeException("sql file: $sqlFile can not read, make sure it exits and can be read."); + } + $count = preg_match_all($pattern, $string, $matches, PREG_SET_ORDER); + if ($count == 0) { + return []; + } + return array_column($matches, 0, 1); + } + + public function listExistsTable() + { + dbconn(false, false); + $sql = 'show tables'; + $res = sql_query($sql); + $data = []; + while ($row = mysql_fetch_row($res)) { + $data[] = $row[0]; + } + return $data; + } + + public function listShouldAlterTable() + { + $tables = $this->listExistsTable(); + $data = []; + foreach ($tables as $table) { + $sql = "desc $table"; + $res = sql_query($sql); + while ($row = mysql_fetch_assoc($res)) { + if ($row['Type'] == 'datetime' && $row['Default'] == '0000-00-00 00:00:00') { + $data[$table][] = $row['Field']; + } + } + } + return $data; + } + + public function listRequirementTableRows() + { + $gdInfo = gd_info(); + $tableRows = [ + [ + 'label' => 'PHP version', + 'required' => '>= ' . $this->minimumPhpVersion, + 'current' => PHP_VERSION, + 'result' => $this->yesOrNo(version_compare(PHP_VERSION, $this->minimumPhpVersion, '>=')), + ], + [ + 'label' => 'PHP extension redis', + 'required' => 'optional', + 'current' => extension_loaded('redis'), + 'result' => $this->yesOrNo(extension_loaded('redis')), + ], + [ + 'label' => 'PHP extension mysqli', + 'required' => 'enabled', + 'current' => extension_loaded('mysqli'), + 'result' => $this->yesOrNo(extension_loaded('mysqli')), + ], + [ + 'label' => 'PHP extension mbstring', + 'required' => 'enabled', + 'current' => extension_loaded('mbstring'), + 'result' => $this->yesOrNo(extension_loaded('mbstring')), + ], + [ + 'label' => 'PHP extension gd', + 'required' => 'enabled', + 'current' => extension_loaded('gd'), + 'result' => $this->yesOrNo(extension_loaded('gd')), + ], + [ + 'label' => 'PHP extension gd JPEG Support', + 'required' => 'true', + 'current' => $gdInfo['JPEG Support'], + 'result' => $this->yesOrNo($gdInfo['JPEG Support']), + ], + [ + 'label' => 'PHP extension gd PNG Support', + 'required' => 'true', + 'current' => $gdInfo['PNG Support'], + 'result' => $this->yesOrNo($gdInfo['PNG Support']), + ], + [ + 'label' => 'PHP extension gd GIF Read Support', + 'required' => 'true', + 'current' => $gdInfo['GIF Read Support'], + 'result' => $this->yesOrNo($gdInfo['GIF Read Support']), + ], + ]; + $fails = array_filter($tableRows, function ($value) {return $value['required'] == 'true' && $value['result'] == 'NO';}); + $pass = empty($fails); + + return [ + 'table_rows' => $tableRows, + 'pass' => $pass, + ]; + } + + public function nextStep() + { + return $this->gotoStep($this->currentStep + 1); + } + + public function gotoStep($step) + { + header('Location: ' . getBaseUrl() . "?step=$step"); + exit(0); + } + + private function yesOrNo($condition) { + if ($condition) { + return 'YES'; + } + return 'NO'; + } + + public function getEnvExampleData($envFile = '') + { + if (empty($envFile)) { + $envFile = ROOT_PATH . '.env.example'; + } + return readEnvFile($envFile); + + } + + public function renderTable($header, $data) + { + $table = '
'; + $table .= '
'; + $table .= '
'; + foreach ($header as $value) { + $table .= '
' . $value . '
'; + } + $table .= '
'; + foreach ($data as $value) { + $table .= '
'; + $table .= '
' . $value['label'] . '
'; + $table .= '
' . $value['required'] . '
'; + $table .= '
' . $value['current'] . '
'; + $table .= '
' . $value['result'] . '
'; + $table .= '
'; + } + $table .= '
'; + $table .= '
'; + + return $table; + + } + + public function renderForm($formControls, $formWidth = '1/2', $labelWidth = '1/3', $valueWidth = '2/3') + { + $form = '
'; + foreach ($formControls as $value) { + $form .= '
'; + $form .= sprintf('
%s
', $labelWidth, $value['label']); + $form .= sprintf( + '
', + $valueWidth, $value['name'], $value['value'] ?? '' + ); + $form .= '
'; + } + $form .= '
'; + return $form; + } + + + +} \ No newline at end of file