PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Install/index.php

https://gitlab.com/selecterskyphp/gaylkj
PHP | 345 lines | 291 code | 48 blank | 6 comment | 37 complexity | e65ce0f5259c7062a0c4ea6fea65b0f6 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. if (file_exists('../install.lock')){
  3. echo '
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  7. </head>
  8. <body>
  9. 你已经安装过该系统如果想重新安装请先删除站点根目录下的 install.lock 文件然后再安装
  10. </body>
  11. </html>';
  12. exit;
  13. }
  14. @set_time_limit(1000);
  15. if(phpversion() <= '5.3.0') set_magic_quotes_runtime(0);
  16. if('5.2.0' > phpversion() ) exit('您的php版本过低,不能安装本软件,请升级到5.2.0或更高版本再安装,谢谢!');
  17. date_default_timezone_set('PRC');
  18. error_reporting(E_ALL & ~E_NOTICE);
  19. header('Content-Type: text/html; charset=UTF-8');
  20. define('SITEDIR', _dir_path(substr(dirname(__FILE__), 0, -8)));
  21. include_once (SITEDIR."/App/Common/common.php");
  22. $sqlFile = 'yourphp.sql';
  23. $configFile = 'config.php';
  24. if(!file_exists(SITEDIR.'Install/'.$sqlFile) || !file_exists(SITEDIR.'Install/'.$configFile)){
  25. echo '缺少必要的安装文件!';exit;
  26. }
  27. $steps= array(
  28. '1'=>'安装许可协议',
  29. '2'=>'运行环境检测',
  30. '3'=>'安装参数设置',
  31. '4'=>'安装详细过程',
  32. '5'=>'安装完成',
  33. );
  34. $step = isset($_GET['step'])? $_GET['step'] : 1;
  35. switch($step)
  36. {
  37. case '1':
  38. include_once ("./templates/s1.html");
  39. exit ();
  40. case '2':
  41. if(phpversion()<5){
  42. die('本系统需要PHP5+MYSQL >=4.1环境,当前PHP版本为:'.phpversion());
  43. }
  44. $phpv = @ phpversion();
  45. $os = PHP_OS;
  46. $os = php_uname();
  47. $tmp = function_exists('gd_info') ? gd_info() : array();
  48. $server = $_SERVER["SERVER_SOFTWARE"];
  49. $host = (empty ($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_HOST"] : $_SERVER["SERVER_ADDR"]);
  50. $name = $_SERVER["SERVER_NAME"];
  51. $max_execution_time = ini_get('max_execution_time');
  52. $allow_reference = (ini_get('allow_call_time_pass_reference') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
  53. $allow_url_fopen = (ini_get('allow_url_fopen') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
  54. $safe_mode = (ini_get('safe_mode') ? '<font color=red>[×]On</font>' : '<font color=green>[√]Off</font>');
  55. $err=0;
  56. if(empty($tmp['GD Version'])){
  57. $gd = '<font color=red>[×]Off</font>' ;
  58. $err++;
  59. }else{
  60. $gd = '<font color=green>[√]On</font> '.$tmp['GD Version'];
  61. }
  62. if(function_exists('mysql_connect')){
  63. $mysql = '<font color=green>[√]On</font>';
  64. }else{
  65. $mysql = '<font color=red>[×]Off</font>';
  66. $err++;
  67. }
  68. if(ini_get('file_uploads')){
  69. $uploadSize = '<font color=green>[√]On</font> 文件限制:'.ini_get('upload_max_filesize');
  70. }else{
  71. $uploadSize = '禁止上传';
  72. }
  73. if(function_exists('session_start')){
  74. $session = '<font color=green>[√]On</font>' ;
  75. }else{
  76. $session = '<font color=red>[×]Off</font>';
  77. $err++;
  78. }
  79. $folder = array ('/',
  80. 'Uploads',
  81. 'Public/Data',
  82. 'Cache/Html',
  83. 'Cache',
  84. 'Cache/Cache',
  85. 'Cache/Data',
  86. 'Cache/Temp',
  87. 'Cache/Logs');
  88. include_once ("./templates/s2.html");
  89. exit ();
  90. case '3':
  91. if($_GET['testdbpwd']){
  92. $dbHost = $_POST['dbHost'].':'.$_POST['dbPort'];
  93. $conn = @mysql_connect($dbHost, $_POST['dbUser'], $_POST['dbPwd']);
  94. if($conn){die("1"); }else{die("");}
  95. }
  96. $scriptName = !empty ($_SERVER["REQUEST_URI"]) ? $scriptName = $_SERVER["REQUEST_URI"] : $scriptName = $_SERVER["PHP_SELF"];
  97. $rootpath = @preg_replace("/\/(I|i)nstall\/index\.php(.*)$/", "", $scriptName);
  98. $domain = empty ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'] ;
  99. $domain = $domain.$rootpath;
  100. include_once ("./templates/s3.html");
  101. exit ();
  102. case '4':
  103. if(intval($_GET['install'])){
  104. $n = intval($_GET['n']);
  105. $arr=array();
  106. $dbHost = trim($_POST['dbHost']);
  107. $dbPort = trim($_POST['dbPort']);
  108. $dbName = trim($_POST['dbName']);
  109. $dbHost = empty($dbPort) || $dbPort == 3306 ? $dbHost : $dbHost.':'.$dbPort;
  110. $dbUser = trim($_POST['dbUser']);
  111. $dbPwd= trim($_POST['dbPwd']);
  112. $dbPrefix = empty($_POST['dbPrefix']) ? 'yourphp_' : trim($_POST['dbPrefix']);
  113. $username = trim($_POST['username']);
  114. $password = trim($_POST['password']);
  115. $site_name = addslashes(trim($_POST['site_name']));
  116. $site_url = trim($_POST['site_url']);
  117. $site_email = trim($_POST['site_email']);
  118. $seo_description = trim($_POST['seo_description']);
  119. $seo_keywords = trim($_POST['seo_keywords']);
  120. $conn = @ mysql_connect($dbHost, $dbUser, $dbPwd);
  121. if(!$conn){
  122. $arr['msg'] = "连接数据库失败!";
  123. echo json_encode($arr);exit;
  124. }
  125. mysql_query("SET NAMES 'utf8'");//,character_set_client=binary,sql_mode='';
  126. $version = mysql_get_server_info($conn);
  127. if($version < 4.1){
  128. $arr['msg'] = '数据库版本太低!';
  129. echo json_encode($arr);exit;
  130. }
  131. if(!mysql_select_db($dbName, $conn)){
  132. if(!mysql_query("CREATE DATABASE IF NOT EXISTS `".$dbName."`;", $conn)){
  133. $arr['msg'] = '数据库 '.$dbName.' 不存在,也没权限创建新的数据库!';
  134. echo json_encode($arr);exit;
  135. }else{
  136. $arr['n']=0;
  137. $arr['msg'] = "成功创建数据库:{$dbName}<br>";
  138. echo json_encode($arr);exit;
  139. }
  140. }
  141. //读取数据文件
  142. $sqldata = file_get_contents(SITEDIR.'Install/'.$sqlFile);
  143. $sqlFormat = sql_split($sqldata, $dbPrefix);
  144. /**
  145. 执行SQL语句
  146. */
  147. $counts =count($sqlFormat);
  148. if($n < $counts) {
  149. $sql = trim($sqlFormat[$n]);
  150. $n++;
  151. if (strstr($sql, 'CREATE TABLE')){
  152. preg_match('/CREATE TABLE `([^ ]*)`/', $sql, $matches);
  153. mysql_query("DROP TABLE IF EXISTS `$matches[1]");
  154. $ret = mysql_query($sql);
  155. if($ret){
  156. $message = '<font color="gree">成功创建数据表:'.$matches[1].' </font><br />';
  157. }else{
  158. $message = '<font color="red">创建数据表失败:'.$matches[1].' </font><br />';
  159. }
  160. $arr=array('n'=>$n,'msg'=>$message);
  161. echo json_encode($arr); exit;
  162. }
  163. }
  164. if($i==999999)exit;
  165. $sqldata = file_get_contents(SITEDIR.'Install/yourphp_data.sql');
  166. sql_execute($sqldata, $dbPrefix);
  167. $sqldata = file_get_contents(SITEDIR.'Install/yourphp_area.sql');
  168. sql_execute($sqldata, $dbPrefix);
  169. if( $_POST['lang']){
  170. $langsql = file_get_contents(SITEDIR.'Install/yourphp_lang.sql');
  171. sql_execute($langsql, $dbPrefix);
  172. }else{
  173. @unlink(SITEDIR.'index.php');
  174. @copy(SITEDIR.'Install/index_one.php',SITEDIR.'index.php');
  175. mysql_query("UPDATE `{$dbPrefix}menu` SET `status` ='0' WHERE model='Lang' ");
  176. }
  177. mysql_query("UPDATE `{$dbPrefix}config` SET `value` = '$site_name' WHERE varname='site_name' and lang=1");
  178. mysql_query("UPDATE `{$dbPrefix}config` SET `value` = '$site_url' WHERE varname='site_url' ");
  179. mysql_query("UPDATE `{$dbPrefix}config` SET `value` = '$site_email' WHERE varname='site_email'");
  180. mysql_query("UPDATE `{$dbPrefix}config` SET `value` = '$seo_description' WHERE varname='seo_description' and lang=1");
  181. mysql_query("UPDATE `{$dbPrefix}config` SET `value` = '$seo_keywords' WHERE varname='seo_keywords' and lang=1");
  182. //读取配置文件,并替换真实配置数据
  183. $strConfig = file_get_contents(SITEDIR.'Install/'.$configFile);
  184. $strConfig = str_replace('#DB_HOST#', $dbHost, $strConfig);
  185. $strConfig = str_replace('#DB_NAME#', $dbName, $strConfig);
  186. $strConfig = str_replace('#DB_USER#', $dbUser, $strConfig);
  187. $strConfig = str_replace('#DB_PWD#', $dbPwd, $strConfig);
  188. $strConfig = str_replace('#DB_PORT#', $dbPort, $strConfig);
  189. $strConfig = str_replace('#DB_PREFIX#', $dbPrefix, $strConfig);
  190. @file_put_contents(SITEDIR.'/'.$configFile, $strConfig);
  191. $code=md5(time());
  192. $query = "UPDATE `{$dbPrefix}config` SET value='$code' WHERE varname='ADMIN_ACCESS'";
  193. mysql_query($query);
  194. //插入管理员
  195. $time=time();
  196. $ip = get_client_ip();
  197. $password = hash ( sha1, $password.$code );
  198. $query = "INSERT INTO `{$dbPrefix}user` (`groupid`, `username`, `password`, `realname`, `email`, `createtime`, `updatetime`, `reg_ip`, `status`) VALUES( 1, '$username', '$password', '$username', '$site_email', '$time', '$time', '$ip', '1')";
  199. mysql_query($query);
  200. $message = '成功添加管理员<br />成功写入配置文件<br>安装完成.';
  201. $arr=array('n'=>999999,'msg'=>$message);
  202. echo json_encode($arr);exit;
  203. }
  204. include_once ("./templates/s4.html");
  205. exit();
  206. case '5':
  207. dir_delete(SITEDIR.'/Cache');
  208. $scriptName = !empty ($_SERVER["REQUEST_URI"]) ? $scriptName = $_SERVER["REQUEST_URI"] : $scriptName = $_SERVER["PHP_SELF"];
  209. $rootpath = @preg_replace("/\/(I|i)nstall\/index\.php(.*)/", "", $scriptName);
  210. $domain = empty ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'] ;
  211. $domain = $domain.$rootpath;
  212. include_once ("./templates/s5.html");
  213. @touch('../install.lock');
  214. exit ();
  215. }
  216. function testwrite( $d )
  217. {
  218. $tfile = "_test.txt";
  219. $fp = @fopen( $d."/".$tfile, "w" );
  220. if ( !$fp )
  221. {
  222. return false;
  223. }
  224. fclose( $fp );
  225. $rs = @unlink( $d."/".$tfile );
  226. if ( $rs )
  227. {
  228. return true;
  229. }
  230. return false;
  231. }
  232. function sql_execute($sql,$tablepre) {
  233. $sqls = sql_split($sql,$tablepre);
  234. if(is_array($sqls))
  235. {
  236. foreach($sqls as $sql)
  237. {
  238. if(trim($sql) != '')
  239. {
  240. mysql_query($sql);
  241. }
  242. }
  243. }
  244. else
  245. {
  246. mysql_query($sqls);
  247. }
  248. return true;
  249. }
  250. function sql_split($sql,$tablepre) {
  251. if($tablepre != "yourphp_") $sql = str_replace("yourphp_", $tablepre, $sql);
  252. $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8",$sql);
  253. if($r_tablepre != $s_tablepre) $sql = str_replace($s_tablepre, $r_tablepre, $sql);
  254. $sql = str_replace("\r", "\n", $sql);
  255. $ret = array();
  256. $num = 0;
  257. $queriesarray = explode(";\n", trim($sql));
  258. unset($sql);
  259. foreach($queriesarray as $query)
  260. {
  261. $ret[$num] = '';
  262. $queries = explode("\n", trim($query));
  263. $queries = array_filter($queries);
  264. foreach($queries as $query)
  265. {
  266. $str1 = substr($query, 0, 1);
  267. if($str1 != '#' && $str1 != '-') $ret[$num] .= $query;
  268. }
  269. $num++;
  270. }
  271. return $ret;
  272. }
  273. function _dir_path($path) {
  274. $path = str_replace('\\', '/', $path);
  275. if(substr($path, -1) != '/') $path = $path.'/';
  276. return $path;
  277. }
  278. // 获取客户端IP地址
  279. function get_client_ip() {
  280. static $ip = NULL;
  281. if ($ip !== NULL) return $ip;
  282. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  283. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  284. $pos = array_search('unknown',$arr);
  285. if(false !== $pos) unset($arr[$pos]);
  286. $ip = trim($arr[0]);
  287. }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  288. $ip = $_SERVER['HTTP_CLIENT_IP'];
  289. }elseif (isset($_SERVER['REMOTE_ADDR'])) {
  290. $ip = $_SERVER['REMOTE_ADDR'];
  291. }
  292. // IP地址合法验证
  293. $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
  294. return $ip;
  295. }
  296. ?>