<?php
// $Id: install.php 98 2011-06-11 12:07:19Z yd2004 $
error_reporting(E_ALL ^ E_NOTICE);
define('DIDA_ROOT', getcwd());
if (is_file('sites/config.php')) {
require_once 'sites/config.php';
if ($config && $config[$_SERVER['HTTP_HOST']]) {
$conf_dir = $config[$_SERVER['HTTP_HOST']];
} else if (!empty($config['default'])) {
$conf_dir = $config['default'];
}
}
if (empty($conf_dir)) {
if (!is_dir('sites/' . $_SERVER['HTTP_HOST'])) {
$conf_dir = 'sites';
} else {
$conf_dir = 'sites/' . $_SERVER['HTTP_HOST'];
}
}
$conf_file = $conf_dir .'/cache/conf.php';
$setting_file = $conf_dir .'/setting.php';
if (is_file($conf_file)) {
require_once $conf_file;
}
if (is_file($setting_file)) {
require_once $setting_file;
if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {
$base_path = "/$dir";
$base_path .= '/';
} else {
$base_path = '/';
}
if (!empty($installed)) {
if (!empty($conf['status'])) {
header('Location: ' . $base_path, true, 301);
} else {
header('Content-Type: text/html; charset=utf-8');
echo $setting_file . ' 文件显示系统已经安装,或许这是一个旧的配置文件,请确认并清除该文件内容';
}
exit;
}
}
require_once './includes/bootstrap.inc';
require_once './includes/database.inc';
require_once './includes/form.inc';
require_once './includes/cache.inc';
conf_init();
/**
* 自定义安装
*/
$custom_install = DIDA_ROOT . '/install/install.php';
if (is_file($custom_install)) {
require_once $custom_install;
if (function_exists('install')) {
call_user_func('install');
exit;
}
}
$title = '检查安装环境';
if (!$error = dida_is_setup()) { // 检查安装环境
if (empty($_GET['setup'])) {
dd_goto(f('install.php?setup=1'));
} else {
switch ($_GET['setup']) {
case 1:
$title = '选择数据库';
$database_type = dida_setup_data_select(); // 选择数据库
break;
case 2:
$title = '填写数据库信息';
$database_form = dida_setup_data_form(); // 填写数据库信息
break;
case 3:
$title = '测试数据库权限';
$body = dida_setup_data_test(); // 测试数据库
break;
case 4:
$title = '填写管理员信息';
$body = dida_setup(); // 安装程序
break;
default:
dd_goto(f('install.php?setup=1'));
}
}
}
function dida_is_setup() {
$error = NULL;
if (version_compare(PHP_VERSION, '5.2.0', '<')) {
$error[] = 'PHP版本至少为 5.2';
}
if (!function_exists('json_decode')) {
$error[] = '请开启 json 扩展';
}
if (!function_exists('mb_strlen')) {
$error[] = '请开启 mb 扩展';
}
if (!class_exists('PDO')) {
$error[] = '请开启 PDO 扩展';
}
if (!function_exists('gd_info')) {
$error[] = '请开启 GD 扩展';
}
if (!is_writable('./sites/logs')) {
$error[] = ' 日志目录(sites/logs)必须有读取权限 ';
}
if (!empty($error)) return $error;
global $conf, $conf_dir, $conf_file, $setting_file;
if (is_file($conf_file)) {
if (!is_writable($conf_file)) {
$error[] = $conf_file . ' 必须有读写权限';
}
} else {
if (!is_dir($conf_dir.'/cache') && !mkdir($conf_dir.'/cache', 0750)) {
$error[] = '无法创建目录 '.$conf_dir.'/cache,请手动创建,并将权限设置为可读写。';
}
if (is_file('sites/cache/default.conf.php')) {
file_put_contents($conf_file, file_get_contents('sites/cache/default.conf.php'));
chmod($conf_file, 0777);
} else {
$error[] = '请不要删除 sites/cache/default.conf.php';
}
}
if (is_file($setting_file)) {
if (!is_writable($setting_file)) {
$error[] = $setting_file . ' 必须有读写权限';
}
} else if (!$handle = fopen($setting_file, "wb")) {
$error[] = '配置文件('.$setting_file.')不存在且无法自动创建,请复制 sites/default.setting.php 并重命名为 setting.php。';
} else {
chmod($setting_file, 0777);
fclose($handle);
}
return $error;
}
function dida_setup_data_select() {
global $database, $setting_file;
$type = db_info();
if (!empty($_GET['clear'])) {
if (file_put_contents($setting_file, '')) {
$database = NULL;
}
}
if (!empty($_POST['driver']) && !empty($type[$_POST['driver']])) {
$setting_string = '<?php';
$setting_string .= "\n";
$setting_string .= '$database[\'default\'] = array(';
$setting_string .= "\n 'driver' => '$_POST[driver]',\n);";
if (file_put_contents($setting_file, $setting_string)) {
dd_goto(f('install.php?setup=2'));
}
}
if (!empty($database) && !empty($database['default'])
&& !empty($database['default']['driver']) && !empty($type[$database['default']['driver']])) {
dd_goto(f('install.php?setup=2'));
}
return $type;
}
function dida_setup_data_form() {
global $database, $error, $conf_dir, $setting_file;
$type = db_info();
if (empty($database) || empty($database['default'])
|| empty($database['default']['driver']) || empty($type[$database['default']['driver']])) {
dd_goto(f('install.php?setup=1'));
}
require_once './includes/database/install.'.$database['default']['driver'].'.inc';
$form = '<input type="hidden" name="driver" value="'.$database['default']['driver'].'" />';
if ($_POST) {
if (!$error = db_install_test($_POST)) {
require_once './includes/cache.inc';
if (cache_system_set_file('setting.php', 'database[\'default\']', $_POST, $conf_dir)) {
dd_goto(f('install.php?setup=3'));
} else {
$error[] = '保存失败,请删除 ' . $setting_file;
}
} else {
$database['default'] = $_POST;
}
} else {
$database['default'] = NULL;
}
if ($fields = db_install_form($database['default'])) {
foreach ($fields as $field) {
$form .= '<div class="form_item">'.$field.'</div>';
}
return $form;
}
}
function dida_setup_data_test() {
global $error, $database;
if (db_connect('default')) {
$table = array(
'description' => '测试',
'fields' => array(
'id' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'tid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => false,
'default' => 0,
'description' => '测试'
),
'name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => '测试',
),
),
'indexes' => array(
'tid' => array('tid')
),
'primary key' => array('id'),
);
if (db_is_table('dida_test')) {
if (!db_drop_table('dida_test')) {
$error[] = '没有数据库表删除权限';
}
}
// 测试表
if (db_create_table('dida_test', $table)) {
if (db_exec('INSERT INTO {dida_test} (tid, name) VALUES (:tid, :name)', array(':tid' => 10, ':name' => 'test'))) {
if (db_query('SELECT * FROM {dida_test}')) {
if (db_exec('UPDATE {dida_test} SET name = :name WHERE tid = :tid', array(':tid' => 10, ':name' => 'newtest'))) {
if (db_exec('DELETE FROM {dida_test} WHERE tid = 10')) {
if (!db_drop_table('dida_test')) {
$error[] = '没有数据库表删除权限';
} else if (db_is_table('system')) {
$error[] = '系统配置表已经存在,该数据库中是否已经存在一个程序,建议使用全新数据库,或设置表前缀';
}
} else {
$error[] = '没有数据删除权限';
}
} else {