ajax.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. // --------
  3. // 如果你能在浏览器中看到本句话,则证明你没有安装好PHP运行环境。请先安装好PHP运行环境
  4. // --------
  5. ini_set("display_errors", "Off");
  6. error_reporting(E_ALL | E_STRICT);
  7. header("Content-type: text/html; charset=utf-8");
  8. if($f = file_get_contents("./install.lock")){
  9. ajax_out("本程序已经安装过!如果要解除安装锁定,则可删除/install目录下的install.lock文件后再重新访问本页面",10099);
  10. }
  11. if(!new_is_writeable("./")){
  12. ajax_out("请赋予 /install 目录以可写权限!",10098);
  13. }
  14. if(!new_is_writeable("../Application/Runtime")){
  15. ajax_out("请赋予 /Application/Runtime 目录以可写权限!",10095);
  16. }
  17. if(!new_is_writeable("../Application/Common/Conf/config.php")){
  18. ajax_out("请赋予 /Application/Common/Conf/config.php 文件以可写权限!",10094);
  19. }
  20. $db_type = $_POST["db_type"] ? $_POST["db_type"] :"sqlite";
  21. if ($db_type == "sqlite") {
  22. if(!new_is_writeable("../Sqlite")){
  23. ajax_out("请赋予 /Sqlite 目录以可写权限!",10097);
  24. }
  25. if(!new_is_writeable("../Sqlite/showdoc.db.php")){
  26. ajax_out("请赋予 /Sqlite/showdoc.db.php 以可写权限!",10096);
  27. }
  28. user_sqlite();
  29. }
  30. elseif ($db_type == "mysql") {
  31. user_mysql();
  32. }
  33. function user_sqlite(){
  34. clear_runtime();//清除缓存
  35. $config =
  36. <<<EOD
  37. <?php
  38. return array(
  39. //'配置项'=>'配置值'
  40. //使用sqlite数据库(ShowDoc默认)
  41. 'DB_TYPE' => 'Sqlite',
  42. 'DB_NAME' => 'Sqlite/showdoc.db.php',
  43. //使用mysql数据库
  44. //'DB_TYPE' => 'mysql',
  45. //'DB_NAME' => 'showdoc',
  46. 'DB_HOST' => 'localhost',
  47. 'DB_USER' => 'showdoc',
  48. 'DB_PWD' => 'showdoc123456',
  49. 'DB_PORT' => 3306, // 端口
  50. 'DB_PREFIX' => '', // 数据库表前缀
  51. 'DB_CHARSET'=> 'utf8', // 字符集
  52. 'DB_DEBUG' => TRUE, // 数据库调试模式 开启后可以记录SQL日志
  53. 'URL_HTML_SUFFIX' => '',//url伪静态后缀
  54. 'URL_MODEL' => 3 ,//URL兼容模式
  55. 'URL_ROUTER_ON' => true,
  56. 'URL_ROUTE_RULES'=>array(
  57. ':id\d' => 'Home/Item/Show?item_id=:1',
  58. 'uid/:id\d' => 'Home/Item/showByUid?uid=:1',
  59. ),
  60. 'URL_CASE_INSENSITIVE'=>true,
  61. 'SHOW_ERROR_MSG' => true, // 显示错误信息,这样在部署模式下也能显示错误
  62. );
  63. EOD;
  64. $ret = file_put_contents("../Application/Common/Conf/config.php", $config);
  65. if ($ret) {
  66. file_put_contents("./install.lock","http://doc.star7th.com/");
  67. ajax_out("安装成功!建议删除/install目录,以免安装脚本被再次执行。");
  68. }else{
  69. ajax_out("安装失败,配置文件写入错误!",10001);
  70. }
  71. }
  72. function user_mysql(){
  73. $db_host = $_POST["db_host"] ;
  74. $db_user = $_POST["db_user"] ;
  75. $db_port = $_POST["db_port"] ;
  76. $db_name = $_POST["db_name"] ;
  77. $db_password = $_POST["db_password"] ;
  78. clear_runtime();//清除缓存
  79. //检测数据库配置是否能链接得上
  80. $con = mysql_connect($db_host.":".$db_port,$db_user,$db_password);
  81. $r = mysql_select_db($db_name);
  82. if (!$con || !$r) {
  83. ajax_out("数据库链接错误,请检查配置信息是否填写正确",10002);
  84. exit();
  85. }
  86. mysql_query("SET NAMES UTF8");
  87. $row = mysql_fetch_array(mysql_query(" SELECT COUNT(*) FROM user "));
  88. if ($row) {
  89. ajax_out("检测到该数据库已经存在数据。请清理后再重试",10003);
  90. exit();
  91. }
  92. //开始导入mysql数据库
  93. $ret = import_mysql();
  94. if (!$ret) {
  95. ajax_out("创建数据库表失败!",10004);
  96. exit();
  97. }
  98. $config = "<?php ";
  99. $config .= "
  100. return array(
  101. //'配置项'=>'配置值'
  102. //使用sqlite数据库(ShowDoc默认)
  103. //'DB_TYPE' => 'Sqlite',
  104. //'DB_NAME' => 'Sqlite/showdoc.db.php',
  105. //使用mysql数据库
  106. 'DB_TYPE' => 'mysql',
  107. 'DB_NAME' => '{$db_name}',
  108. 'DB_HOST' => '{$db_host}',
  109. 'DB_USER' => '{$db_user}',
  110. 'DB_PWD' => '{$db_password}',
  111. 'DB_PORT' => {$db_port}, // 端口
  112. 'DB_PREFIX' => '', // 数据库表前缀
  113. 'DB_CHARSET'=> 'utf8', // 字符集
  114. 'DB_DEBUG' => TRUE, // 数据库调试模式 开启后可以记录SQL日志
  115. 'URL_HTML_SUFFIX' => '',//url伪静态后缀
  116. 'URL_MODEL' => 3 ,//URL兼容模式
  117. 'URL_ROUTER_ON' => true,
  118. 'URL_ROUTE_RULES'=>array(
  119. ':id\d' => 'Home/Item/Show?item_id=:1',
  120. 'uid/:id\d' => 'Home/Item/showByUid?uid=:1',
  121. ),
  122. 'URL_CASE_INSENSITIVE'=>true,
  123. 'SHOW_ERROR_MSG' => true, // 显示错误信息,这样在部署模式下也能显示错误
  124. );";
  125. $ret = file_put_contents("../Application/Common/Conf/config.php", $config);
  126. if ($ret) {
  127. file_put_contents("./install.lock","http://doc.star7th.com/");
  128. ajax_out("安装成功!建议删除/install目录,以免安装脚本被再次执行。");
  129. }else{
  130. ajax_out("安装失败,配置文件写入错误!",10001);
  131. }
  132. }
  133. function ajax_out($message,$error_code = 0){
  134. echo json_encode(array("error_code"=>$error_code,"message"=>$message));
  135. exit();
  136. }
  137. function clear_runtime($path = "../Application/Runtime"){
  138. //给定的目录不是一个文件夹
  139. if(!is_dir($path)){
  140. return null;
  141. }
  142. $fh = opendir($path);
  143. while(($row = readdir($fh)) !== false){
  144. //过滤掉虚拟目录
  145. if($row == '.' || $row == '..'|| $row == 'index.html'){
  146. continue;
  147. }
  148. if(!is_dir($path.'/'.$row)){
  149. unlink($path.'/'.$row);
  150. }
  151. clear_runtime($path.'/'.$row);
  152. }
  153. //关闭目录句柄,否则出Permission denied
  154. closedir($fh);
  155. return true;
  156. }
  157. function import_mysql($con){
  158. //创建目录表
  159. $sql = "CREATE TABLE IF NOT EXISTS `catalog` (
  160. `cat_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '目录id',
  161. `cat_name` varchar(20) NOT NULL DEFAULT '' COMMENT '目录名',
  162. `item_id` int(10) NOT NULL DEFAULT '0' COMMENT '所在的项目id',
  163. `s_number` int(10) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序',
  164. `addtime` int(11) NOT NULL DEFAULT '0',
  165. `parent_cat_id` int(10) NOT NULL DEFAULT '0' COMMENT '上一级目录的id',
  166. `level` int(10) NOT NULL DEFAULT '2' COMMENT '2为二级目录,3为三级目录',
  167. PRIMARY KEY (`cat_id`),
  168. KEY `addtime` (`addtime`),
  169. KEY `s_number` (`s_number`)
  170. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='目录表' AUTO_INCREMENT=1 ";
  171. mysql_query($sql);
  172. //创建item表
  173. $sql = "CREATE TABLE IF NOT EXISTS `item` (
  174. `item_id` int(10) NOT NULL AUTO_INCREMENT,
  175. `item_name` varchar(50) NOT NULL DEFAULT '',
  176. `item_description` varchar(225) NOT NULL DEFAULT '' COMMENT '项目描述',
  177. `uid` int(10) NOT NULL DEFAULT '0',
  178. `username` varchar(50) NOT NULL DEFAULT '',
  179. `password` varchar(50) NOT NULL DEFAULT '',
  180. `addtime` int(11) NOT NULL DEFAULT '0',
  181. `last_update_time` int(11) NOT NULL DEFAULT '0' COMMENT '最后更新时间',
  182. PRIMARY KEY (`item_id`),
  183. KEY `addtime` (`addtime`)
  184. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='项目表' AUTO_INCREMENT=1 ";
  185. mysql_query($sql);
  186. //创建项目成员表
  187. $sql = "CREATE TABLE IF NOT EXISTS `item_member` (
  188. `item_member_id` int(10) NOT NULL AUTO_INCREMENT,
  189. `item_id` int(10) NOT NULL DEFAULT '0',
  190. `uid` int(10) NOT NULL DEFAULT '0',
  191. `username` varchar(50) NOT NULL DEFAULT '',
  192. `addtime` int(11) NOT NULL DEFAULT '0',
  193. PRIMARY KEY (`item_member_id`)
  194. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='项目成员表' AUTO_INCREMENT=1 ";
  195. mysql_query($sql);
  196. //创建项目page表
  197. $sql = "CREATE TABLE IF NOT EXISTS `page` (
  198. `page_id` int(10) NOT NULL AUTO_INCREMENT,
  199. `author_uid` int(10) NOT NULL DEFAULT '0' COMMENT '页面作者uid',
  200. `author_username` varchar(50) NOT NULL DEFAULT '' COMMENT '页面作者名字',
  201. `item_id` int(10) NOT NULL DEFAULT '0',
  202. `cat_id` int(10) NOT NULL DEFAULT '0',
  203. `page_title` varchar(50) NOT NULL DEFAULT '',
  204. `page_content` text NOT NULL,
  205. `s_number` int(10) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序',
  206. `addtime` int(11) NOT NULL DEFAULT '0',
  207. PRIMARY KEY (`page_id`),
  208. KEY `addtime` (`addtime`),
  209. KEY `s_number` (`s_number`)
  210. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='文章页面表' AUTO_INCREMENT=1 ";
  211. mysql_query($sql);
  212. //创建项目page_history表
  213. $sql = "CREATE TABLE IF NOT EXISTS `page_history` (
  214. `page_history_id` int(10) NOT NULL AUTO_INCREMENT,
  215. `page_id` int(10) NOT NULL DEFAULT '0',
  216. `author_uid` int(10) NOT NULL DEFAULT '0' COMMENT '页面作者uid',
  217. `author_username` varchar(50) NOT NULL DEFAULT '' COMMENT '页面作者名字',
  218. `item_id` int(10) NOT NULL DEFAULT '0',
  219. `cat_id` int(10) NOT NULL DEFAULT '0',
  220. `page_title` varchar(50) NOT NULL DEFAULT '',
  221. `page_content` text NOT NULL,
  222. `s_number` int(10) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序',
  223. `addtime` int(11) NOT NULL DEFAULT '0',
  224. PRIMARY KEY (`page_history_id`),
  225. KEY `addtime` (`addtime`),
  226. KEY `page_id` (`page_id`)
  227. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='页面历史表' AUTO_INCREMENT=1 ";
  228. mysql_query($sql);
  229. //创建项目user表
  230. $sql = "CREATE TABLE IF NOT EXISTS `user` (
  231. `uid` int(10) NOT NULL AUTO_INCREMENT,
  232. `username` varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '',
  233. `groupid` tinyint(2) NOT NULL DEFAULT '2' COMMENT '1为超级管理员,2为普通用户',
  234. `name` varchar(15) CHARACTER SET utf8 DEFAULT '',
  235. `avatar` varchar(200) CHARACTER SET utf8 DEFAULT '' COMMENT '头像',
  236. `avatar_small` varchar(200) DEFAULT '',
  237. `email` varchar(50) CHARACTER SET utf8 DEFAULT '',
  238. `password` varchar(50) CHARACTER SET utf8 NOT NULL,
  239. `cookie_token` varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '' COMMENT '实现cookie自动登录的token凭证',
  240. `cookie_token_expire` int(11) NOT NULL DEFAULT '0',
  241. `reg_time` int(11) NOT NULL DEFAULT '0',
  242. `last_login_time` int(11) NOT NULL DEFAULT '0',
  243. PRIMARY KEY (`uid`),
  244. UNIQUE KEY `username` (`username`) USING BTREE
  245. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='用户表' AUTO_INCREMENT=1 ";
  246. $ret = mysql_query($sql);
  247. if ($ret) {
  248. return true;
  249. }else{
  250. return false;
  251. }
  252. }
  253. /**
  254. * 判断 文件/目录 是否可写(取代系统自带的 is_writeable 函数)
  255. *
  256. * @param string $file 文件/目录
  257. * @return boolean
  258. */
  259. function new_is_writeable($file) {
  260. if (is_dir($file)){
  261. $dir = $file;
  262. if ($fp = @fopen("$dir/test.txt", 'w')) {
  263. @fclose($fp);
  264. @unlink("$dir/test.txt");
  265. $writeable = 1;
  266. } else {
  267. $writeable = 0;
  268. }
  269. } else {
  270. if ($fp = @fopen($file, 'a+')) {
  271. @fclose($fp);
  272. $writeable = 1;
  273. } else {
  274. $writeable = 0;
  275. }
  276. }
  277. return $writeable;
  278. }