index.php:
if (!file_exists(CONFIG_PATH . 'install.lock')) {
include VIEW_PATH . 'install.php';
exit;
}
视图install.php:
<div class="box-body">
<div class="form-group">
<input type="text" placeholder="数据库用户名" id="dbuname" class="form-control">
<input type="text" placeholder="数据库密码" id="dbpwd" class="form-control">
<input type="text" placeholder="数据库名" id="dbname" class="form-control">
<input type="text" placeholder="数据库HOST" id="host" class="form-control">
<input type="text" placeholder="数据库端口" id="port" value="3306" class="form-control">
</div>
</div>
<div class="box-footer">
<button class="btn btn-primary" type="button" οnclick="install(this);"><?php echo t('一键安装'); ?></button>
</div>
js:
function install(obj) {
var name = $('#name').val();
var pwd = $('#pwd').val();
var file = $('#file').val();
var dbuname = $('#dbuname').val();
var dbpwd = $('#dbpwd').val();
var dbname = $('#dbname').val();
var host = $('#host').val();
if (!name) {
error($('#name'));
return;
}
if (!name) {
error($('#name'));
return;
}
if (!pwd) {
error($('#pwd'));
return;
}
if (!file) {
error($('#file'));
return;
}
if (!dbname) {
error($('#dbname'));
return;
}
if (!host) {
error($('#host'));
return;
}
$(obj).text('安装中...');
$.ajax({
url: 'install.php',
type: 'POST',
data:{ name : name, pwd : pwd, file : file, dbuname : dbuname, dbpwd : dbpwd, dbname : dbname, host : host },
dataType: 'json',
timeout: 8000,
success: function(data){
if (data.code == 1) {
window.location.href = 'index.php';
} else {
alert(data.data);
$(obj).text('一键安装');
}
}
});
}
install.php后台处理:
public function check() {
if (phpversion() < '5.0.0') {
return Response::json(FAIL, array(t('您的php版本过低,不能安装本软件,请升级到5.0.0或更高版本再安装,谢谢!')));
}
if (!extension_loaded('PDO')) {
return Response::json(FAIL, array(t('请加载PHP的PDO模块,谢谢!')));
}
if (!function_exists('session_start')) {
return Response::json(FAIL, array(t('请开启session,谢谢!')));
}
if (!is_writable(ROOT_PATH)) {
return Response::json(FAIL, array(t('请保证代码目录有写权限,谢谢!')));
}
$config = require CONFIG_PATH . 'mysql.php';
try {
$mysql = new PDO('mysql:host=' . $config['master']['host'] . ';port=' . $config['master']['port'], $config['master']['user'], $config['master']['pwd']);
} catch (Exception $e) {
return Response::json(FAIL, array(t('请正确输入信息连接mysql;开启php的PDO扩展,mysql扩展;保证启动mysql,谢谢!')));
}
$mysql->exec('CREATE DATABASE ' . $config['master']['dbname']);
$mysql = null;
unset($config);
return Response::json(SUCC, array(t('检测通过')));
}
public function install() {
//ini_set('display_errors', 'On');
$name = self::trimSpace($_POST['name']);
$pwd = self::trimSpace($_POST['pwd']);
$file = self::trimSpace($_POST['file']);
$dbuname = self::trimSpace($_POST['dbuname']);
$dbpwd = self::trimSpace($_POST['dbpwd']);
$dbname = self::trimSpace($_POST['dbname']);
$host = self::trimSpace($_POST['host']);
$port = $_POST['port'] ? (int)$_POST['port'] : 3306;
if (!$name || !$pwd || !$file || !$dbuname || !$dbname || !$host) {
return Response::json(FAIL, array('数据不能为空'));
}
$res = mkdir($file, 0777, true);
if (!$res && !file_exists($file)) {
return Response::json(FAIL, array(t('文件存储目录创建失败,请检查对应目录是否有写权限后重试')));
}
$mysqlConf = array();
$mysqlConf['slave'][0]['user'] = $mysqlConf['master']['user'] = $dbuname;
$mysqlConf['slave'][0]['pwd'] = $mysqlConf['master']['pwd'] = $dbpwd;
$mysqlConf['slave'][0]['host'] = $mysqlConf['master']['host'] = $host;
$mysqlConf['slave'][0]['port'] = $mysqlConf['master']['port'] = $port;
$mysqlConf['slave'][0]['dbname'] = $mysqlConf['master']['dbname'] = $dbname;
if (!file_exists(CONFIG_PATH . 'mysql.php')) {
$int = file_put_contents(CONFIG_PATH . 'mysql.php', '<?php return ' . var_export($mysqlConf, true) . '; ?>');
if (!$int) {
return Response::json(FAIL, array(t('Conf目录文件写入失败,请检查是否有写权限')));
}
}
$cres = $this->check();
$check = json_decode($cres, true);
if ($check['code'] != 1) {
return $cres;
}
$this->executeSql(SQL_PATH . 'yun.sql');
$ures = Factory::getInstance('user')->setadmin($name,$pwd);
if (!$ures) {
return Response::json(FAIL, array(管理员账号创建失败,请重新安装.')));
}else{
$handle = fopen(CONFIG_PATH . 'install.lock', 'w');
fclose($handle);
$_SESSION['CLOUD_UID'] = $ures;
setcookie('CLOUD_UID', $ures, time() + 3600 * 24);
return Response::json(SUCC, array(t('安装成功')));
}
}
public function executeSql($file) {
$fileInfo = file_get_contents($file);
$sql = $this->parseSql($fileInfo);
unset($fileInfo);
$mysql = Mysql::getInstance();
foreach ($sql as $s) {
$mysql->execute($s);
}
}
public function parseSql($sql = '') {
$sql = str_replace("\r", "\n", $sql);
$ret = array();
$aSql = explode(";\n", trim($sql));
unset($sql);
$num = 0;
foreach ($aSql as $sql) {
$ret[$num] = '';
$queries = explode("\n", $sql);
$queries = array_filter($queries);
foreach ($queries as $query) {
$str1 = substr($query, 0, 1);
if ($str1 !== '#' && $str1 !== '-') {
$ret[$num] .= $query;
}
}
$num++;
}
return $ret;
}
判断install.lock文件是否存在,如果不存在进行安装。