Explorar el Código

Optimize installation script / 优化安装脚本

star7th hace 4 años
padre
commit
989a3e5923

+ 37 - 56
install/ajax.php

@@ -6,6 +6,8 @@ ini_set("display_errors", "Off");
 error_reporting(E_ALL | E_STRICT);
 header("Content-type: text/html; charset=utf-8"); 
 include("common.php");
+$lang = $_REQUEST['lang'] ? $_REQUEST['lang'] :"zh";
+
 if(file_exists('./install.lock') && $f = file_get_contents("./install.lock")){
 	ajax_out(L("lock"),10099);
 }
@@ -24,76 +26,55 @@ if(!new_is_writeable("../server/Application/Runtime")){
 }
 
 
-if(!new_is_writeable("../server/Application/Home/Conf/config.php")){
-	ajax_out(L("not_writable_home_config"),10098);
+if(!new_is_writeable("../Sqlite")){
+    ajax_out(L("not_writable_sqlite"),10097);
 }
 
+if(!new_is_writeable("../Sqlite/showdoc.db.php")){
+    ajax_out(L("not_writable_sqlite_db"),10096);
+}
 
-$db_type = $_POST["db_type"] ?  $_POST["db_type"] :"sqlite";
-if ($db_type == "sqlite") {
-	if(!new_is_writeable("../Sqlite")){
-		ajax_out(L("not_writable_sqlite"),10097);
-	}
 
-	if(!new_is_writeable("../Sqlite/showdoc.db.php")){
-		ajax_out(L("not_writable_sqlite_db"),10096);
-	}
-    user_sqlite();
-}
-elseif ($db_type == "mysql") {
-    //showdoc不再支持mysql http://www.showdoc.cc/help?page_id=31990
-}
-function user_sqlite(){
-        clear_runtime();//清除缓存
-        write_js_lang();
-        
-        $ret = write_home_config();
-        if ($ret) {
-        	file_put_contents("./install.lock","https://www.showdoc.cc/");
-            ajax_out(L("install_success"));
-        }else{
-            ajax_out(L("not_writable_home_config"),10001);
-        }
-}
+if($lang == 'en'){
+
+    if(!new_is_writeable("../server/Application/Home/Conf/config.php")){
+        ajax_out(L("not_writable_home_config"),10096);
+    }
+    if(!new_is_writeable("../web/index.html")){
+        ajax_out(L("not_writable_web_docconfig"),10096);
+    }
+    if(!new_is_writeable("../web_src/index.html")){
+        ajax_out(L("not_writable_web_src_docconfig"),10096);
+    }    
 
+    replace_file_content("../web/index.html","zh-cn","en") ;
+    replace_file_content("../web_src/index.html","zh-cn","en") ;
 
-function write_home_config(){
-	$lang = $_REQUEST['lang'] ? $_REQUEST['lang'] :"zh";
-	if ($lang == 'en') {
-		$DEFAULT_LANG = 'en-us';
-	}else{
-		$DEFAULT_LANG = 'zh-cn';
-	}
-        $config = "<?php ";
-        $config .= "
-return array(
-	//'配置项'=>'配置值'
+    clear_runtime();//清除缓存
+    
+    $config = "<?php ";
+    $config .= "
+    return array(
+    //'配置项'=>'配置值'
     'DB_TYPE'   => 'Sqlite', 
     'DB_NAME'   => './Sqlite/showdoc.db.php', 
     'LANG_SWITCH_ON' => true,   // 开启语言包功能
     'LANG_AUTO_DETECT' => false, // 自动侦测语言 开启多语言功能后有效
-    'DEFAULT_LANG' => '{$DEFAULT_LANG}', // 默认语言
+    'DEFAULT_LANG' => 'en-us', // 默认语言
     'LANG_LIST'        => 'zh-cn,en-us', // 允许切换的语言列表 用逗号分隔
     'VAR_LANGUAGE'     => 'l', // 默认语言切换变量
-);";
+    );";
 
-	$ret = file_put_contents("../server/Application/Home/Conf/config.php", $config);
-    return $ret ;
-}
+    $ret = file_put_contents("../server/Application/Home/Conf/config.php", $config);
 
-function write_js_lang(){
-    $lang = $_REQUEST['lang'] ? $_REQUEST['lang'] :"zh";
-    if ($lang == 'en') {
-       replace_file_content("../web/index.html","zh-cn","en") ;
-       replace_file_content("../web_src/index.html","zh-cn","en") ;
+    if (!$ret) {
+        ajax_out(L("not_writable_home_config"),10001);
     }
-    
 }
 
-function replace_file_content($file , $from ,$to ){
-    $content = file_get_contents($file);
-    $content2 = str_replace($from,$to,$content);
-    if ($content2) {
-        file_put_contents($file,$content2);
-    }
-}
+clear_runtime();//清除缓存
+
+file_put_contents("./install.lock","https://www.showdoc.com.cn/");
+ajax_out(L("install_success"));
+
+

+ 13 - 4
install/common.php

@@ -14,10 +14,10 @@ function lang(){
 
 
 function L($field){
-  if (!isset($GLOBALS['lang'])) {
-      $GLOBALS['lang'] = lang();
+  if (!isset($GLOBALS['lang_array'])) {
+      $GLOBALS['lang_array'] = lang();
   }
-  return $GLOBALS['lang'][$field] ;
+  return $GLOBALS['lang_array'][$field] ;
 }
 
 /**
@@ -73,6 +73,15 @@ function clear_runtime($path = "../server/Application/Runtime"){
 } 
 
 function ajax_out($message,$error_code = 0){
-        echo json_encode(array("error_code"=>$error_code,"message"=>$message));
+        echo json_encode(array("error_code"=>$error_code,"error_message"=>$message));
         exit();
+}
+
+
+function replace_file_content($file , $from ,$to ){
+  $content = file_get_contents($file);
+  $content2 = str_replace($from,$to,$content);
+  if ($content2) {
+      file_put_contents($file,$content2);
+  }
 }

+ 0 - 212
install/database.php

@@ -1,212 +0,0 @@
-<?php
-// ShowDoc安装脚本
-// install Showdoc
-// 
-// --------
-// 	如果你能在浏览器中看到本句话,则证明你没有安装好PHP运行环境。请先安装好PHP运行环境
-// --------
-include("common.php");
-$cur_lang = $_REQUEST['lang'] ? $_REQUEST['lang'] :"zh";
-header("Content-type: text/html; charset=utf-8"); 
-
-// 检测PHP环境
-if(version_compare(PHP_VERSION,'5.3.0','<'))  die(L('require_php_version'));
-
-if(file_exists('./install.lock') && $f = file_get_contents("./install.lock")){
-  echo L("lock").'<br>';
-  exit();
-}
-
-$go = 1 ;
-
-//检测文件权限
-if(!new_is_writeable("./")){
-  echo L("not_writable_install").'<br>';
-  $go = 0;
-}
-if(!new_is_writeable("../Public/Uploads")){
-  echo L("not_writable_upload").'<br>';
-  $go = 0;
-}
-
-if(!new_is_writeable("../server/Application/Runtime")){
-  echo L("not_writable_server_runtime").'<br>';
-  $go = 0;
-}
-
-if(!new_is_writeable("../server/Application/Common/Conf/config.php")){
-  echo L("not_writable_config").'<br>';
-  $go = 0;
-}
-if(!new_is_writeable("../server/Application/Home/Conf/config.php")){
-  echo L("not_writable_home_config").'<br>';
-  $go = 0;
-}
-
-if ($cur_lang == 'en') {
-  if(!new_is_writeable("../web/index.html")){
-    echo L("not_writable_web_docconfig").'<br>';
-    $go = 0;
-  }
-
-  if(!new_is_writeable("../web_src/index.html")){
-    echo L("not_writable_web_src_docconfig").'<br>';
-    $go = 0;
-  }
-}
-
-
-//检查扩展
-if(!extension_loaded("gd")){
-  echo '请安装php-gd<br>';
-  $go = 0;
-}
-/*
-if(!extension_loaded("mcrypt")){
-  echo '请安装php-mcrypt<br>';
-  $go = 0;
-}
-*/
-if(!extension_loaded("mbstring")){
-  echo '请安装php-mbstring<br>';
-  $go = 0;
-}
-
-if(!extension_loaded("zlib")){
-  echo '请安装php-zlib<br>';
-  $go = 0;
-}
-
-if(!extension_loaded("PDO") && !extension_loaded("pdo") ){
-  echo '请安装php-pdo<br>';
-  $go = 0;
-}
-
-/*if(extension_loaded("sqlite") || extension_loaded("sqlite3")){
-  echo '请安装php-sqlite<br>';
-  $go = 0;
-}
-*/
-
-if (!$go) {
-  exit();
-}
-
-
-
-?>
-
-<!DOCTYPE html>
-<html lang="en">
-  <head>
-    <meta charset="utf-8">
-    <title> ShowDoc</title>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0">
-    <meta name="description" content="">
-    <meta name="author" content="">
-    <link href="../Public/bootstrap/css/bootstrap.min.css" rel="stylesheet">
-    <style type="text/css">
-    @charset "utf-8";
-	body {
-		font:14px/1.5 "Microsoft Yahei","微软雅黑",Tahoma,Arial,Helvetica,STHeiti;
-	}
-  .form-signin{
-    display: none;
-  }
-    </style>
-
-  </head>
-  <body>
-<link rel="stylesheet" href="../Public/css/login.css" />
-
-    <div class="container">
-
-      <form class="form-signin" method="post">
-        <h3 class="form-signin-heading"><?php echo L("install_title");?></h3>
-        <br>
-        <div>
-	        <select id="db_type">
-	        	<option value="sqlite"><?php echo L("use_sqlite");?></option>
-	        	<option value="mysql"><?php echo L("use_mysql");?></option>
-	        </select>
-        </div>
-        <br>
-        <div class="mysql-info" style="display:none">
-	        <input type="text" class="input-block-level"  name="db_host" id = "db_host" placeholder="<?php echo L("server_address");?>">
-	        <input type="text" class="input-block-level"  name="db_port" id = "db_port"  placeholder="<?php echo L("server_port");?>">
-	        <input type="text" class="input-block-level"  name="db_name" id = "db_name"  placeholder="<?php echo L("db_name");?>">
-	        <input type="text" class="input-block-level"  name="db_user" id = "db_user"  placeholder="<?php echo L("db_user");?>">
-	        <input type="text" class="input-block-level"  name="db_password" id = "db_password"  placeholder="<?php echo L("db_password");?>">
-        </div>
-        <div class="sqlite_tips" ><?php echo L("sqlite_tips");?></div>
-        <input type="hidden" value="<?php echo $cur_lang;?>" id="lang">
-        <br>
-        <div>
-        	 <button class="btn btn-large btn-primary " id="start" type="submit"><?php echo L("go");?>&nbsp;&nbsp;<i class="icon-circle-arrow-right"></i></button>
-          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="https://www.showdoc.cc/help?page_id=16118" target="_blank"><?php echo L("FAQ");?></a>
-        </div>
-      </form>
-
-    </div> <!-- /container -->
-
-    
-	<script src="../Public/js/common/jquery.min.js"></script>
-    <script src="../Public/bootstrap/js/bootstrap.min.js"></script>
-  </body>
-</html> 
-
- <script type="text/javascript">
- $(function(){
- 	$("#db_type").change(function(){
- 		if ($("#db_type").val() == 'mysql') {
-      $(".mysql-info").show();
- 			$(".sqlite_tips").hide();
- 		};
- 		if ($("#db_type").val() == 'sqlite') {
-      $(".mysql-info").hide();
-      $(".sqlite_tips").show();
- 		};
- 	});
-
-
-
-  function install(){
-    var db_type = $("#db_type").val();
-    var db_host = $("#db_host").val();
-    var db_port = $("#db_port").val();
-    var db_name = $("#db_name").val();
-    var db_user = $("#db_user").val();
-    var lang = $("#lang").val();
-    var db_password = $("#db_password").val();
-    $.post(
-      'ajax.php',
-      {"lang":lang,"db_type":db_type,"db_host":db_host,"db_port":db_port,"db_name":db_name,"db_user":db_user,"db_password":db_password},
-      function(data){
-        if (data.error_code === 0) {
-          //安装成功
-          //alert(data.message);
-          var text = '<div><?php echo L("install_success_help");?></div><br>';
-           text += '<div><a href="../" ><?php echo L("home");?></a></div>';
-           $(".form-signin").html(text);
-             $(".form-signin").show();
-
-        }else{
-          alert(data.message);
-        }
-      },
-      "json"
-
-      ); 
-  }
-
-  $("#start").click(function(){
-
-    install();
-    return false;
-  });
-  //showdoc不再支持mysql,所以不再让用户选择数据库,而是直接跳过去.
-  install();
-
- });
-
-</script>

+ 134 - 24
install/index.php

@@ -6,6 +6,69 @@
 // 	如果你能在浏览器中看到本句话,则证明你没有安装好PHP运行环境。请先安装好PHP运行环境
 // --------
 include("common.php");
+// 检测PHP环境
+if(version_compare(PHP_VERSION,'5.3.0','<'))  die(L('require_php_version'));
+
+if(file_exists('./install.lock') && $f = file_get_contents("./install.lock")){
+  echo L("lock").'<br>';
+  exit();
+}
+
+$go = 1 ;
+
+//检测文件权限
+if(!new_is_writeable("./")){
+  //本安装目录需要写入lock文件
+  echo L("not_writable_install").'<br>';
+  $go = 0;
+}
+if(!new_is_writeable("../Public/Uploads")){
+  echo L("not_writable_upload").'<br>';
+  $go = 0;
+}
+
+if(!new_is_writeable("../server/Application/Runtime")){
+  echo L("not_writable_server_runtime").'<br>';
+  $go = 0;
+}
+
+
+//检查扩展
+if(!extension_loaded("gd")){
+  echo '请安装php-gd<br>';
+  $go = 0;
+}
+/*
+if(!extension_loaded("mcrypt")){
+  echo '请安装php-mcrypt<br>';
+  $go = 0;
+}
+*/
+if(!extension_loaded("mbstring")){
+  echo '请安装php-mbstring<br>';
+  $go = 0;
+}
+
+if(!extension_loaded("zlib")){
+  echo '请安装php-zlib<br>';
+  $go = 0;
+}
+
+if(!extension_loaded("PDO") && !extension_loaded("pdo") ){
+  echo '请安装php-pdo<br>';
+  $go = 0;
+}
+
+/*if(extension_loaded("sqlite") || extension_loaded("sqlite3")){
+  echo '请安装php-sqlite<br>';
+  $go = 0;
+}
+*/
+
+if (!$go) {
+  exit();
+}
+
 
 ?>
 
@@ -17,41 +80,88 @@ include("common.php");
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <meta name="description" content="">
     <meta name="author" content="">
-    <link href="../Public/bootstrap/css/bootstrap.min.css" rel="stylesheet">
     <style type="text/css">
-    @charset "utf-8";
-	body {
-		font:14px/1.5 "Microsoft Yahei","微软雅黑",Tahoma,Arial,Helvetica,STHeiti;
-	}
+        html,body{
+          margin:0 0 ;
+          padding:0 0 ;
+        }
+        .container{
+            display: -webkit-flex; /* Safari */
+            display: flex;
+            height:100%;
+            width:100%;
+            position: absolute;
+          }
+
+        .flex-item{
+          display: -webkit-inline-flex;
+          display: inline-flex;
+          justify-content:center; /*水平居中*/
+          align-items: center; /*垂直居中*/
+        }
+          
+        .left{
+          width: 50%;
+          background-color:#F59064;
+          /* background-image: linear-gradient(to right, red, white); */
+          height:100%;
+          
+        }
+        .right{
+          width: 50%;
+          background-color:#87CEEB;
+          /* background-image: linear-gradient(to  left,  black , white); */
+          height:100% ;
+        }
+        .text{
+          font-size:30px;
+          cursor:pointer ;
+        }
     </style>
 
   </head>
   <body>
-<link rel="stylesheet" href="../Public/css/login.css" />
 
     <div class="container">
 
-      <form class="form-signin" method="get" action="database.php">
-        <h3 class="form-signin-heading">选择语言<br>(Choose language)</h3>
-        <br>
-        <div>
-	        <select id="db_type" name="lang">
-	        	<option value="zh">中文</option>
-	        	<option value="en">English</option>
-	        </select>
+      <div class="flex-item left">
+        <div class="text" id="en">
+          Language : English &nbsp;   →
         </div>
-        <br>
-
-        <br>
-        <div>
-        	 <button class="btn btn-large btn-primary " id="start" type="submit">ok&nbsp;&nbsp;<i class="icon-circle-arrow-right"></i></button>
+      </div>
+      <div class="flex-item right">
+      <div class="text" id="zh">
+          语言 :中文  &nbsp;  →
         </div>
-      </form>
-
+      </div>
     </div> <!-- /container -->
 
-    
-	<script src="../Public/js/common/jquery.min.js"></script>
-    <script src="../Public/bootstrap/js/bootstrap.min.js"></script>
   </body>
 </html> 
+<script>
+  document.getElementById("en").onclick = function(){ 
+      toInstall("en"); 
+   }
+   document.getElementById("zh").onclick = function(){ 
+      toInstall("zh"); 
+   }
+
+   function toInstall (lang){
+      //创建异步对象  
+      var xhr = new XMLHttpRequest();
+      xhr.open("GET","ajax.php?lang="+lang,true);
+      xhr.send();
+      xhr.onreadystatechange = function () {
+        // 这步为判断服务器是否正确响应
+        if (xhr.readyState == 4 && xhr.status == 200) {
+          var json = JSON.parse(xhr.responseText) ;
+          // console.log(json);
+          if(json.error_code === 0){
+            window.location.href = "../";
+          }else{
+            alert(json.error_message)
+          }
+        }
+      };
+   }
+</script>

+ 10 - 4
server/Application/Home/Conf/config.php

@@ -1,5 +1,11 @@
 <?php 
-return array(
-	//'配置项'=>'配置值'
-    'showdoc' => 'showdoc not install'
-);
+    return array(
+    //'配置项'=>'配置值'
+    'DB_TYPE'   => 'Sqlite', 
+    'DB_NAME'   => './Sqlite/showdoc.db.php', 
+    'LANG_SWITCH_ON' => true,   // 开启语言包功能
+    'LANG_AUTO_DETECT' => false, // 自动侦测语言 开启多语言功能后有效
+    'DEFAULT_LANG' => 'zh-cn', // 默认语言
+    'LANG_LIST'        => 'zh-cn,en-us', // 允许切换的语言列表 用逗号分隔
+    'VAR_LANGUAGE'     => 'l', // 默认语言切换变量
+    );

+ 42 - 26
server/Application/Home/Controller/IndexController.class.php

@@ -3,33 +3,49 @@ namespace Home\Controller;
 use Think\Controller;
 class IndexController extends BaseController {
     public function index(){
-        $tmp = @file_get_contents('./server/Application/Home/Conf/config.php');
-        if (strstr($tmp, "showdoc not install")) {
-            header("location:./install/index.php");
-            exit();
-        }
 
-        //跳转到web目录
-        header("location:./web/#/");
-        exit();
+				//不存在安装文件夹的,表示已经安装过
+				if(!file_exists("./install")){
+					//跳转到web目录
+					header("location:./web/#/");
+					exit();
+				}
 
-    	$this->checkLogin(false);
-    	$login_user = session("login_user");
-    	$this->assign("login_user" ,$login_user);
-    	if (LANG_SET == 'en-us') {
-    		$demo_url = "https://www.showdoc.cc/demo-en";
-    		$help_url = "https://www.showdoc.cc/help-en";
-    		$creator_url = "https://github.com/star7th";
-    	}
-    	else{
-    		$demo_url = "https://www.showdoc.cc/demo";
-    		$help_url = "https://www.showdoc.cc/help";
-    		$creator_url = "https://blog.star7th.com/";
-    	}
-    	$this->assign("demo_url" ,$demo_url);
-    	$this->assign("help_url" ,$help_url);
-    	$this->assign("creator_url" ,$creator_url);
-
-        $this->display();
+				if(file_exists("./install") && file_exists("./install/install.lock") && $this->new_is_writeable("./install") && $this->new_is_writeable("./install/install.lock") ){
+					//跳转到web目录
+					header("location:./web/#/");
+					exit();
+				}
+				//其他情况都跳转到安装页面
+				header("location:./install/index.php");
     }
+
+		/**
+		 * 判断 文件/目录 是否可写(取代系统自带的 is_writeable 函数)
+		 *
+		 * @param string $file 文件/目录
+		 * @return boolean
+		 */
+		public function new_is_writeable($file) {
+			if (is_dir($file)){
+				$dir = $file;
+				if ($fp = @fopen("$dir/test.txt", 'w')) {
+					@fclose($fp);
+					@unlink("$dir/test.txt");
+					$writeable = 1;
+				} else {
+					$writeable = 0;
+				}
+			} else {
+				if ($fp = @fopen($file, 'a+')) {
+					@fclose($fp);
+					$writeable = 1;
+				} else {
+					$writeable = 0;
+				}
+			}
+
+			return $writeable;
+		}
+
 }