<?php
/**
* 安裝模組
*/
class IndexAction extends Action{
//安裝介面
public function _initialize(){
//檢測 right enter
if($_SESSION['right_enter']!=1) {
echo '系統不支援session!';
exit();
}
}
//許可協議
public function index(){
//判斷install.lock是否存在,存在則跳轉到前台首頁
$file = './Public/Install/install.lock';
if (file_exists($file)){
$this->redirect('Home/Index/index');
}
if ($_POST['startinstall']){
if ($_POST['union']){
$_SESSION['union'] = 1;
}else {
$_SESSION['union'] = 0;
}
$this->redirect('Install/Index/check');
}else{
$this->display();
}
}
//檢測伺服器環境
public function check(){
//判斷install.lock是否存在,存在則跳轉到前台首頁
$file = './Public/Install/install.lock';
if (file_exists($file)){
$this->redirect('Home/Index/index');
}
$error=true;
//需要檢測目錄、檔案
$check_file=array(
'./Uploads',
'./Uploads/LocalItems',
'./Public',
'./Public/Install',
'./Public/statics',
'./App/Conf',
'./App/Conf/config.php',
'./App/Conf/home.php',
'./App/Conf/Home',
'./App/Conf/Home/config.php',
'./App/Runtime',
'./App/Runtime/DataBackup',
);
$error_msg=array();
foreach ($check_file as $file){
//檢測檔案是否存在
if (!file_exists($file)){
$error_msg[]=$file.' 不存在!';
$error=false;
continue;
}
if (is_dir($file)){
//檢測目錄是否可寫
$file_test=@fopen($file.'/test.txt','w');
if(!$file_test){
$error_msg[] = $file." 不可寫!";
$error = false;
}
@fclose($file_test);
@unlink($file.'/test.txt');
}else {
//檢測檔案是否可寫
if (!is_writeable($file)) {
$error_msg[] = $file." 不可寫!";
$error = false;
}
}
}
//檢測是否支援gd
if(!function_exists("gd_info")){
$error_msg[] = "系統不支援gd!";
$error = false;
}
//檢測是否支援curl
if(!function_exists("curl_getinfo")){
$error_msg[] = "系統不支援curl!";
$error = false;
}
if (!$error) {
$this->assign('error_msg', $error_msg);
$this->display();
} else {
$this->redirect('Install/Index/setconf');
}
}
//填寫配置資訊
public function setconf(){
//判斷install.lock是否存在,存在則跳轉到前台首頁
$file = './Public/Install/install.lock';
if (file_exists($file)){
$this->redirect('Home/Index/index');
}
//預設配置資訊
$this->assign('db_host', 'localhost');
$this->assign('db_port', '3306');
$this->assign('db_user', 'root');
$this->assign('db_pwd', '');
$this->assign('db_name', 'jdcms');
$this->assign('db_prefix', 'jd_');
$this->assign('user_name', '');
$this->assign('password', '');
$this->assign('repassword', '');
$this->assign('email', '');
if (isset($_POST['edit'])) {
foreach ($_POST as $key=>$val) {
$this->assign($key, $val);
}
extract($_POST);
$web_path=trim($web_path);
$db_host=trim($db_host);
$db_port=trim($db_port);
$db_user=trim($db_user);
$db_pwd=trim($db_pwd);
$db_name=trim($db_name);
$db_prefix=trim($db_prefix);
$user_name=trim($user_name);
$password=trim($password);
$repassword=trim($repassword);
$email=trim($email);
//檢測資訊是否填寫
if (!$db_host || !$db_port || !$db_user || !$db_name || !$db_prefix || !$user_name || !$password || !$repassword || !$email) {
$this->assign('error_msg','請完整填寫配置資訊!');
$this->display();
exit;
}
if($user_name =='admin'){
$this->assign('error_msg','管理員帳號不能設為admin');
$this->display();
exit;
}
if(strlen($password)<6 || strlen($password)>20){
$this->assign('error_msg','密碼應在6-20位之間');
$this->display();
exit;
}
//檢查兩次密碼是否一致
if ($password != $repassword) {
$this->assign('error_msg','兩次密碼不一致!');
$this->display();
exit;
}
//檢查email格式
if (!$this->is_email($email)) {
$this->assign('error_msg','不是有效郵箱!');
$this->display();
exit;
}
//檢測資料庫連接
$conn = @mysql_connect($db_host,$db_user,$db_pwd);
if (!$conn) {
$this->assign('error_msg','資料庫連接失敗,請檢查資料庫配置資訊!');
$this->display();
exit;
}
//檢測資料庫是否存在,不存在則建立資料庫
if (!@mysql_select_db($db_name)){
$sql = "CREATE DATABASE `".$db_name."` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
mysql_query($sql);
}
//檢測資料庫是否建立成功
if(!@mysql_select_db($db_name)){
$this->assign('error_msg','資料庫建立失敗!');
$this->display();
exit;
}
$login_key=str_rand();
//儲存配置到config.php
$config = array(
'web_path' => $web_path,
'login_key' => $login_key,
'db_host' => $db_host,
'db_port' => $db_port,
'db_user' => $db_user,
'db_pwd' => $db_pwd,
'db_name' => $db_name,
'db_prefix' => $db_prefix,
);
$this->updateconfig($config);
//匯入SQL指令碼
$sqls = $this->get_sql('./Public/sql/install.sql');
$conn = @mysql_connect($db_host,$db_user,$db_pwd);
mysql_select_db($db_name);
foreach ($sqls as $sql){
//替換表首碼
$sql = str_replace('jd_',$db_prefix,$sql);
$result = mysql_query($sql,$conn);
if (@mysql_affected_rows($conn)<0){
$this->assign('error_msg','建立表失敗');
$this->display();
exit;
}
}
$domain = $config[cms_domain] = $config[site_domain] = 'http://'.$_SERVER['HTTP_HOST'].$web_path;
$this->updateconfig($config);
//添加管理員帳號
$password=md5($password);
$add_time=time();
$sql = "INSERT INTO `".$db_prefix."admin` (`user_name`, `password`, `add_time`) VALUES " .
"('".$user_name."', '".$password."', '".$add_time."');";
$result = mysql_query($sql);
if (@mysql_affected_rows($conn)<0){
$this->assign('error_msg','添加管理員失敗!');
$this->display();
exit;
}else{
$url = C('official_website').'push/index';
$data = array (
"union" => $_SESSION['union'],
"user" => $user_name,
"site" => $domain,
"ver" => C('cms_versions'),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
fopen('./Public/Install/install.lock','w');
$this->redirect('Install/Index/finish');
}
}
$this->display();
}
//安裝完成
public function finish(){
$this->display();
}
//儲存配置資訊
private function updateconfig($config){
$config_old = require './App/Conf/config.php';
if(is_array($config)){
$config_new = array_merge($config_old,$config);
}
arr2file('./App/Conf/config.php',$config_new);
@unlink('./App/Runtime/~runtime.php');
}
//擷取SQL指令碼
private function get_sql($sql_file){
$contents = file_get_contents($sql_file);
$contents = str_replace("\r\n", "\n", $contents);
$contents = trim(str_replace("\r", "\n", $contents));
$return_items = $items = array();
$items = explode(";\n", $contents);
foreach ($items as $item) {
$return_item = '';
$item = trim($item);
$lines = explode("\n", $item);
foreach ($lines as $line) {
if (isset($line[1]) && $line[0] . $line[1] == '--') {
continue;
}
$return_item .= $line;
}
if ($return_item) {
$return_items[] = $return_item;
}
}
return $return_items;
}
//檢測郵箱格式
private function is_email($email){
$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,5}\$/i";
if (strpos($email, '@') !== false && strpos($email, '.') !== false) {
if (preg_match($chars, $email)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
}
本文出自 “航仔” 部落格,請務必保留此出處http://hangzai.blog.51cto.com/4763780/1298905