3 Star 12 Fork 2

火云判官 / EasySwoolAdmin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
install.php 16.46 KB
一键复制 编辑 原始数据 按行查看 历史
火云判官 提交于 2020-11-23 21:09 . 1
<?php
// 定义目录分隔符
define('DS', DIRECTORY_SEPARATOR);
// 定义根目录
define('ROOT_PATH', __DIR__ . DS );
// 定义应用目录
define('APP_PATH', ROOT_PATH . 'App' . DS);
// 安装包目录
define('INSTALL_PATH', APP_PATH . 'Http' . DS . 'Install' . DS);
// 判断文件或目录是否有写的权限
function is_really_writable($file)
{
if (DIRECTORY_SEPARATOR == '/' AND @ ini_get("safe_mode") == FALSE) {
return is_writable($file);
}
if (!is_file($file) OR ($fp = @fopen($file, "r+")) === FALSE) {
return FALSE;
}
fclose($fp);
return TRUE;
}
$sitename = "EasyAdmin";
// 检测目录是否存在
$checkDirs = [
'vendor',
];
//错误信息
$errInfo = '';
$dbPrefix = '`tp_';
//数据库配置文件
$dbConfigFile = './dev.php';
// 锁定的文件
$lockFile = INSTALL_PATH . 'install.lock';
if (is_file($lockFile)) {
$errInfo = "当前已经安装{$sitename},如果需要重新安装,请手动移除App/Http/Install/install.lock文件";
} else if (version_compare(PHP_VERSION, '7.2.0', '<')) {
$errInfo = "当前版本(" . PHP_VERSION . ")过低,请使用PHP7.2以上版本";
} else if (!extension_loaded("PDO")) {
$errInfo = "当前未开启PDO,无法进行安装";
} else if (!extension_loaded("swoole")) {
$errInfo = "当前未开启swoole,无法进行安装";
}else if (!is_really_writable($dbConfigFile)) {
$open_basedir = ini_get('open_basedir');
if ($open_basedir) {
$dirArr = explode(PATH_SEPARATOR, $open_basedir);
if ($dirArr && in_array(__DIR__, $dirArr)) {
$errInfo = '当前服务器因配置了open_basedir,导致无法读取父目录';
}
}
if (!$errInfo) {
$errInfo = '当前权限不足,无法写入配置文件.dev';
}
} else {
$dirArr = [];
foreach ($checkDirs as $k => $v) {
if (!is_dir(ROOT_PATH . $v)) {
$errInfo = '当前代码仅包含核心代码';
break;
}
}
}
// 当前是POST请求
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
if ($errInfo) {
echo $errInfo;
exit;
}
$err = '';
$mysqlHostname = isset($_POST['mysqlHost']) ? $_POST['mysqlHost'] : '127.0.0.1';
$mysqlHostport = isset($_POST['mysqlPort']) ? $_POST['mysqlPort'] : 3306;
$hostArr = explode(':', $mysqlHostname);
if (count($hostArr) > 1) {
$mysqlHostname = $hostArr[0];
$mysqlHostport = $hostArr[1];
}
$mysqlUsername = isset($_POST['mysqlUsername']) ? $_POST['mysqlUsername'] : 'root';
$mysqlPassword = isset($_POST['mysqlPassword']) ? $_POST['mysqlPassword'] : '';
$mysqlDatabase = isset($_POST['mysqlDatabase']) ? $_POST['mysqlDatabase'] : 'fastadmin';
$mysqlPrefix = isset($_POST['mysqlPrefix']) ? $_POST['mysqlPrefix'] : 'tp_';
$adminUsername = isset($_POST['adminUsername']) ? $_POST['adminUsername'] : 'admin';
$adminPassword = isset($_POST['adminPassword']) ? $_POST['adminPassword'] : '123456';
$adminPasswordConfirmation = isset($_POST['adminPasswordConfirmation']) ? $_POST['adminPasswordConfirmation'] : '123456';
$adminEmail = isset($_POST['adminEmail']) ? $_POST['adminEmail'] : 'admin@admin.com';
if ($adminPassword !== $adminPasswordConfirmation) {
echo "两次输入的密码不一致";
exit;
} else if (!preg_match("/^\w+$/", $adminUsername)) {
echo "用户名只能输入字母、数字、下划线";
exit;
} else if (!preg_match("/^[\S]+$/", $adminPassword)) {
echo "密码不能包含空格";
exit;
} else if (strlen($adminUsername) < 3 || strlen($adminUsername) > 12) {
echo "用户名请输入3~12位字符";
exit;
} else if (strlen($adminPassword) < 6 || strlen($adminPassword) > 16) {
echo "密码请输入6~16位字符";
exit;
}
try {
//检测能否读取安装文件
$sql = @file_get_contents(INSTALL_PATH . $sitename .'.sql');
if (!$sql) {
throw new Exception("无法读取App/Http/Install/{$sitename}.sql文件,请检查是否有读权限");
}
$sql = str_replace($dbPrefix, "`{$mysqlPrefix}", $sql);
$pdo = new PDO("mysql:host={$mysqlHostname};port={$mysqlHostport}", $mysqlUsername, $mysqlPassword, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
//检测是否支持innodb存储引擎
$pdoStatement = $pdo->query("SHOW VARIABLES LIKE 'innodb_version'");
$result = $pdoStatement->fetch();
if (!$result) {
throw new Exception("当前数据库不支持innodb存储引擎,请开启后再重新尝试安装");
}
$pdo->query("CREATE DATABASE IF NOT EXISTS `{$mysqlDatabase}` CHARACTER SET utf8 COLLATE utf8_general_ci;");
$pdo->query("USE `{$mysqlDatabase}`");
$pdo->exec($sql);
$config = @file_get_contents($dbConfigFile);
$callback = function ($matches) use ($mysqlHostname, $mysqlHostport, $mysqlUsername, $mysqlPassword, $mysqlDatabase, $mysqlPrefix) {
switch($matches[1]) {
case 'host':
$field = 'hostname';
break;
case 'port':
$field = 'hostport';
break;
case 'user':
$field = 'username';
break;
default:
$field = $matches[1];
break;
}
$field = ucfirst($field);
$replace = ${"mysql{$field}"};
return "'{$matches[1]}'{$matches[2]}=>{$matches[3]}'{$replace}',";
};
$config = preg_replace_callback("/'(host|database|user|password|port|prefix)'(\s+)=>(\s+)(.*)\,/", $callback, $config);
//检测能否成功写入数据库配置
$result = @file_put_contents($dbConfigFile, $config);
if (!$result) {
throw new Exception("无法写入数据库信息到/.dev.php文件,请检查是否有写权限");
}
//检测能否成功写入lock文件
$result = @file_put_contents($lockFile, 1);
if (!$result) {
throw new Exception("无法写入安装锁定到App/Http/Install/install.lock文件,请检查是否有写权限");
}
$newSalt = substr(md5(uniqid(true)), 0, 6);
$newPassword = md5(md5($adminPassword . 'tp_auth_user') . $newSalt);
$pdo->query("UPDATE {$mysqlPrefix}auth_user SET username = '{$adminUsername}', password = '{$newPassword}', salt = '{$newSalt}' WHERE username = 'admin'");
echo "success";
} catch (PDOException $e) {
$err = $e->getMessage();
} catch (Exception $e) {
$err = $e->getMessage();
}
echo $err;
exit;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>安装<?php echo $sitename; ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1">
<meta name="renderer" content="webkit">
<style>
body {
background: #fff;
margin: 0;
padding: 0;
line-height: 1.5;
}
body, input, button {
font-family: 'Open Sans', sans-serif;
font-size: 16px;
color: #7E96B3;
}
.container {
max-width: 515px;
margin: 0 auto;
padding: 20px;
text-align: center;
}
a {
color: #18bc9c;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
h1 {
margin-top: 0;
margin-bottom: 10px;
}
h2 {
font-size: 28px;
font-weight: normal;
color: #3C5675;
margin-bottom: 0;
}
form {
margin-top: 40px;
}
.form-group {
margin-bottom: 20px;
}
.form-group .form-field:first-child input {
border-top-left-radius: 4px;
border-top-right-radius: 4px;
}
.form-group .form-field:last-child input {
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
}
.form-field input {
background: #EDF2F7;
margin: 0 0 1px;
border: 2px solid transparent;
transition: background 0.2s, border-color 0.2s, color 0.2s;
width: 100%;
padding: 15px 15px 15px 180px;
box-sizing: border-box;
}
.form-field input:focus {
border-color: #18bc9c;
background: #fff;
color: #444;
outline: none;
}
.form-field label {
float: left;
width: 160px;
text-align: right;
margin-right: -160px;
position: relative;
margin-top: 18px;
font-size: 14px;
pointer-events: none;
opacity: 0.7;
}
button, .btn {
background: #3C5675;
color: #fff;
border: 0;
font-weight: bold;
border-radius: 4px;
cursor: pointer;
padding: 15px 30px;
-webkit-appearance: none;
}
button[disabled] {
opacity: 0.5;
}
#error, .error, #success, .success {
background: #D83E3E;
color: #fff;
padding: 15px 20px;
border-radius: 4px;
margin-bottom: 20px;
}
#success {
background: #3C5675;
}
#error a, .error a {
color: white;
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>
<svg width="100px" height="120px" viewBox="0 0 768 830" version="1.1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="logo" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<path d="M64.433651,605.899968 C20.067302,536.265612 0,469.698785 0,389.731348 C0,174.488668 171.922656,0 384,0 C596.077344,0 768,174.488668 768,389.731348 C768,469.698785 747.932698,536.265612 703.566349,605.899968 C614.4,753.480595 441.6,870.4 384,870.4 C326.4,870.4 153.6,753.480595 64.433651,605.899968 L64.433651,605.899968 Z"
id="body" fill="#18BC9C"></path>
<path d="M429.648991,190.816 L430.160991,190.816 L429.648991,190.816 L429.648991,190.816 Z M429.648991,156 L427.088991,156 C419.408991,157.024 411.728991,160.608 404.560991,168.8 L403.024991,170.848 L206.928991,429.92 C198.736991,441.184 197.712991,453.984 204.368991,466.784 C210.512991,478.048 222.288991,485.728 235.600991,485.728 L336.464991,486.24 L304.208991,673.632 C301.648991,689.504 310.352991,705.376 325.200991,712.032 C329.808991,714.08 334.416991,714.592 339.536991,714.592 C349.776991,714.592 358.992991,709.472 366.160991,700.256 L561.744991,419.168 C569.936991,407.904 570.960991,395.104 564.304991,382.304 C557.648991,369.504 547.408991,363.36 533.072991,363.36 L432.208991,363.36 L463.952991,199.008 C464.464991,196.448 464.976991,193.376 464.976991,190.816 C464.976991,171.872 449.104991,156 431.184991,156 L429.648991,156 L429.648991,156 Z"
id="flash" fill="#FFFFFF"></path>
</g>
</svg>
</h1>
<h2>安装 <?php echo $sitename; ?></h2>
<div>
<form method="post">
<?php if ($errInfo): ?>
<div class="error">
<?php echo $errInfo; ?>
</div>
<?php endif; ?>
<div id="error" style="display:none"></div>
<div id="success" style="display:none"></div>
<div class="form-group">
<div class="form-field">
<label>MySQL 数据库地址</label>
<input type="text" name="mysqlHost" value="127.0.0.1" required="">
</div>
<div class="form-field">
<label>MySQL 数据库端口</label>
<input type="text" name="mysqlPort" value="8889" required="">
</div>
<div class="form-field">
<label>MySQL 数据库名</label>
<input type="text" name="mysqlDatabase" value="easyadmin" required="">
</div>
<div class="form-field">
<label>MySQL 用户名</label>
<input type="text" name="mysqlUsername" value="root" required="">
</div>
<div class="form-field">
<label>MySQL 密码</label>
<input type="password" name="mysqlPassword" value="">
</div>
<div class="form-field">
<label>MySQL 数据表前缀</label>
<input type="text" name="mysqlPrefix" value="tp_">
</div>
</div>
<div class="form-group">
<div class="form-field">
<label>管理者用户名</label>
<input name="adminUsername" value="admin" required=""/>
</div>
<div class="form-field">
<label>管理者Email</label>
<input name="adminEmail" value="admin@admin.com" required="">
</div>
<div class="form-field">
<label>管理者密码</label>
<input type="password" name="adminPassword" value="" required="">
</div>
<div class="form-field">
<label>重复密码</label>
<input type="password" name="adminPasswordConfirmation" value="" required="">
</div>
</div>
<div class="form-buttons">
<button type="submit" <?php echo $errInfo ? 'disabled' : '' ?>>点击安装</button>
</div>
</form>
<script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
<script>
$(function () {
$('form :input:first').select();
$('form').on('submit', function (e) {
e.preventDefault();
var $button = $(this).find('button')
.text('安装中...')
.prop('disabled', true);
$.post('', $(this).serialize())
.done(function (ret) {
if (ret === 'success') {
$('#error').hide();
$("#success").text("安装成功!开始你的<?php echo $sitename; ?>之旅吧!").show();
start();
$button.remove();
localStorage.setItem("fastep", "installed");
} else {
$('#error').show().text(ret);
$button.prop('disabled', false).text('点击安装');
$("html,body").animate({
scrollTop: 0
}, 500);
}
})
.fail(function (data) {
$('#error').show().text('发生错误:\n\n' + data.responseText);
$button.prop('disabled', false).text('点击安装');
$("html,body").animate({
scrollTop: 0
}, 500);
});
return false;
});
function start() {
$.post('/index.php', {f:'restartShell'})
.done(function (ret) {
if(ret === 'true') {
location.href = location.origin + '/admin'
}else{
alert('服务启动失败!');
}
})
.fail(function (data) {
$('#error').show().text('发生错误:\n\n' + data.responseText);
});
}
});
</script>
</div>
</div>
</body>
</html>
PHP
1
https://gitee.com/tv1898/easyadmin.git
git@gitee.com:tv1898/easyadmin.git
tv1898
easyadmin
EasySwoolAdmin
master

搜索帮助