IndexAction.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. //系统安装
  3. class IndexAction extends Action{
  4. private function getRealPath()
  5. {
  6. return APP_ROOT_PATH;
  7. }
  8. private $install_lock;
  9. public function __construct()
  10. {
  11. import("ORG.Io.Dir");
  12. parent::__construct();
  13. $this->rebuile();
  14. $this->install_lock = $this->getRealPath()."public/install.lock";
  15. }
  16. public function rebuile()
  17. {
  18. $this->clear_dir_file($this->getRealPath()."public/runtime/admin/Cache/");
  19. $this->clear_dir_file($this->getRealPath()."public/runtime/admin/Data/_fields/");
  20. $this->clear_dir_file($this->getRealPath()."public/runtime/admin/Temp/");
  21. $this->clear_dir_file($this->getRealPath()."public/runtime/admin/Logs/");
  22. @unlink($this->getRealPath()."public/runtime/admin/~app.php");
  23. @unlink($this->getRealPath()."public/runtime/admin/~runtime.php");
  24. @unlink($this->getRealPath()."public/runtime/admin/lang.js");
  25. @unlink($this->getRealPath()."public/runtime/app/config_cache.php");
  26. $this->clear_dir_file($this->getRealPath()."public/runtime/statics/");
  27. $this->clear_dir_file($this->getRealPath()."public/runtime/app/tpl_caches/");
  28. $this->clear_dir_file($this->getRealPath()."public/runtime/app/tpl_compiled/");
  29. $this->clear_dir_file($this->getRealPath()."public/runtime/data/");
  30. $this->clear_dir_file($this->getRealPath()."public/runtime/app/data_caches/");
  31. $this->clear_dir_file($this->getRealPath()."public/runtime/app/db_caches/");
  32. @unlink($this->getRealPath()."public/runtime/app/lang.js");
  33. }
  34. public function clear_dir_file($path)
  35. {
  36. if ( $dir = opendir( $path ) )
  37. {
  38. while ( $file = readdir( $dir ) )
  39. {
  40. $check = is_dir( $file );
  41. if ( !$check )
  42. unlink( $path . $file );
  43. }
  44. closedir( $dir );
  45. return true;
  46. }
  47. }
  48. /**
  49. * 文件或目录权限检查函数
  50. *
  51. * @access private
  52. * @param string $file_path 文件路径
  53. * @param bool $rename_prv 是否在检查修改权限时检查执行rename()函数的权限
  54. *
  55. * @return int 返回值的取值范围为{0 <= x <= 15},每个值表示的含义可由四位二进制数组合推出。
  56. * 返回值在二进制计数法中,四位由高到低分别代表
  57. * 可执行rename()函数权限、可对文件追加内容权限、可写入文件权限、可读取文件权限。
  58. */
  59. private function file_mode_info($file_path)
  60. {
  61. /* 如果不存在,则不可读、不可写、不可改 */
  62. if (!file_exists($file_path))
  63. {
  64. return false;
  65. }
  66. $mark = 0;
  67. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
  68. {
  69. /* 测试文件 */
  70. $test_file = $file_path . '/cf_test.txt';
  71. /* 如果是目录 */
  72. if (is_dir($file_path))
  73. {
  74. /* 检查目录是否可读 */
  75. $dir = @opendir($file_path);
  76. if ($dir === false)
  77. {
  78. return $mark; //如果目录打开失败,直接返回目录不可修改、不可写、不可读
  79. }
  80. if (@readdir($dir) !== false)
  81. {
  82. $mark ^= 1; //目录可读 001,目录不可读 000
  83. }
  84. @closedir($dir);
  85. /* 检查目录是否可写 */
  86. $fp = @fopen($test_file, 'wb');
  87. if ($fp === false)
  88. {
  89. return $mark; //如果目录中的文件创建失败,返回不可写。
  90. }
  91. if (@fwrite($fp, 'directory access testing.') !== false)
  92. {
  93. $mark ^= 2; //目录可写可读011,目录可写不可读 010
  94. }
  95. @fclose($fp);
  96. @unlink($test_file);
  97. /* 检查目录是否可修改 */
  98. $fp = @fopen($test_file, 'ab+');
  99. if ($fp === false)
  100. {
  101. return $mark;
  102. }
  103. if (@fwrite($fp, "modify test.\r\n") !== false)
  104. {
  105. $mark ^= 4;
  106. }
  107. @fclose($fp);
  108. /* 检查目录下是否有执行rename()函数的权限 */
  109. if (@rename($test_file, $test_file) !== false)
  110. {
  111. $mark ^= 8;
  112. }
  113. @unlink($test_file);
  114. }
  115. /* 如果是文件 */
  116. elseif (is_file($file_path))
  117. {
  118. /* 以读方式打开 */
  119. $fp = @fopen($file_path, 'rb');
  120. if ($fp)
  121. {
  122. $mark ^= 1; //可读 001
  123. }
  124. @fclose($fp);
  125. /* 试着修改文件 */
  126. $fp = @fopen($file_path, 'ab+');
  127. if ($fp && @fwrite($fp, '') !== false)
  128. {
  129. $mark ^= 6; //可修改可写可读 111,不可修改可写可读011...
  130. }
  131. @fclose($fp);
  132. /* 检查目录下是否有执行rename()函数的权限 */
  133. if (@rename($test_file, $test_file) !== false)
  134. {
  135. $mark ^= 8;
  136. }
  137. }
  138. }
  139. else
  140. {
  141. if (@is_readable($file_path))
  142. {
  143. $mark ^= 1;
  144. }
  145. if (@is_writable($file_path))
  146. {
  147. $mark ^= 14;
  148. }
  149. }
  150. return $mark;
  151. }
  152. /**
  153. * 执行SQL脚本文件
  154. *
  155. * @param array $filelist
  156. * @return string
  157. */
  158. private function restore($file,$db_config)
  159. {
  160. set_time_limit(0);
  161. $db = Db::getInstance(array('dbms'=>'mysql','hostname'=>$db_config['DB_HOST'],'username'=>$db_config['DB_USER'],'password'=>$db_config['DB_PWD'],'hostport'=>$db_config['DB_PORT'],'database'=>$db_config['DB_NAME']));
  162. $sql = file_get_contents($file);
  163. $sql = $this->remove_comment($sql);
  164. $sql = trim($sql);
  165. $sql = str_replace("\r", '', $sql);
  166. $segmentSql = explode(";\n", $sql);
  167. foreach($segmentSql as $k=>$itemSql)
  168. {
  169. $itemSql = str_replace("%DB_PREFIX%",$db_config['DB_PREFIX'],$itemSql);
  170. $db->query($itemSql);
  171. }
  172. return "";
  173. }
  174. /**
  175. * 过滤SQL查询串中的注释。该方法只过滤SQL文件中独占一行或一块的那些注释。
  176. *
  177. * @access public
  178. * @param string $sql SQL查询串
  179. * @return string 返回已过滤掉注释的SQL查询串。
  180. */
  181. private function remove_comment($sql)
  182. {
  183. /* 删除SQL行注释,行注释不匹配换行符 */
  184. $sql = preg_replace('/^\s*(?:--|#).*/m', '', $sql);
  185. /* 删除SQL块注释,匹配换行符,且为非贪婪匹配 */
  186. //$sql = preg_replace('/^\s*\/\*(?:.|\n)*\*\//m', '', $sql);
  187. $sql = preg_replace('/^\s*\/\*.*?\*\//ms', '', $sql);
  188. return $sql;
  189. }
  190. public function index(){
  191. $this->display();
  192. }
  193. public function do_update()
  194. {
  195. header("Content-type: text/html; charset=utf-8");
  196. echo "<script>function jump(){ parent.location.href = ".__ROOT__."/"."; }</script>";
  197. echo "<style type='text/css'> body{ font-size:12px; line-height:18px; font-family:'arial'; } div{ margin:5px 0px;} .error{ border:#f30 solid 1px; color:#f30;}</style>";
  198. @set_time_limit(0);
  199. $return_rs = array(
  200. 'msg'=>'更新成功',
  201. 'status'=>true,
  202. ); //用于返回的数据
  203. $db_config = require_once $this->getRealPath()."public/db_config.php";
  204. $connect = @mysql_connect($db_config['DB_HOST'].":".$db_config['DB_PORT'],$db_config['DB_USER'],$db_config['DB_PWD']);
  205. if(mysql_error()=="")
  206. {
  207. $rs = mysql_select_db($db_config['DB_NAME'],$connect);
  208. if($rs)
  209. {
  210. $return_rs['status'] = true;
  211. }
  212. else
  213. {
  214. $return_rs['msg'] = "不存在的数据库";
  215. $return_rs['status'] = false;
  216. }
  217. }
  218. else
  219. {
  220. $return_rs['msg'] = "连接数据库失败";
  221. $return_rs['status'] = false;
  222. }
  223. if($return_rs['status'])
  224. {
  225. set_time_limit(0);
  226. $db = Db::getInstance(array('dbms'=>'mysql','hostname'=>$db_config['DB_HOST'],'username'=>$db_config['DB_USER'],'password'=>$db_config['DB_PWD'],'hostport'=>$db_config['DB_PORT'],'database'=>$db_config['DB_NAME']));
  227. $sql = file_get_contents($this->getRealPath()."update_live_pay/update.sql");
  228. $sql = $this->remove_comment($sql);
  229. $sql = trim($sql);
  230. $sql = str_replace("\r", '', $sql);
  231. $segmentSql = explode(";\n", $sql);
  232. if(!is_numeric($segmentSql[0])&&$segmentSql[0]!='license')
  233. {
  234. $this->assign("waitSecond",'-1');
  235. die("脚本没有版本号,无法更新");
  236. }
  237. else
  238. {
  239. $version = $segmentSql[0];
  240. $db_version = $db->query("select value from ".$db_config['DB_PREFIX']."conf where name='DB_VERSION'");
  241. $db_version = $db_version[0]['value'];
  242. if($db_version==$version)
  243. {
  244. die("数据库已经是最新版本");
  245. }
  246. if(floatval($db_version)>floatval($version)&&$segmentSql[0]!='license')
  247. {
  248. die("不能更新旧版本的数据脚本");
  249. }
  250. }
  251. $errmsg = '';
  252. $output_msg = '';
  253. foreach($segmentSql as $k=>$itemSql)
  254. {
  255. $itemSql = str_replace("%DB_PREFIX%",$db_config['DB_PREFIX'],$itemSql);
  256. if($itemSql!=''&&!is_numeric($itemSql)&&$itemSql!='license')
  257. {
  258. $db->query($itemSql);
  259. $current_err = $db->getError();
  260. if($current_err!=$errmsg)
  261. {
  262. $errmsg = $current_err;
  263. echo "<div class='error'>".$itemSql."错误信息:".$current_err."</div>";
  264. }
  265. else
  266. {
  267. echo "<div>".$itemSql."</div>";
  268. }
  269. }
  270. }
  271. //开始写入配置文件
  272. $sys_configs = $db->query("select * from ".$db_config['DB_PREFIX']."conf");
  273. $config_str = "<?php\n";
  274. $config_str .= "return array(\n";
  275. foreach($sys_configs as $k=>$v)
  276. {
  277. $config_str.="'".$v['name']."'=>'".addslashes($v['value'])."',\n";
  278. }
  279. $config_str.=");\n ?>";
  280. file_put_contents($this->getRealPath()."public/sys_config.php",$config_str);
  281. $this->rebuile();
  282. //更新成功后执行
  283. if($output_msg!='')
  284. {
  285. import("ORG.Io.Dir");
  286. $this->rebuile();
  287. echo "<br />".$output_msg." <a href='javascript:jump();'>返回首页</a>";
  288. }
  289. else
  290. {
  291. import("ORG.Io.Dir");
  292. $this->rebuile();
  293. echo "<br />".$return_rs['msg']." <a href='javascript:jump();'>返回首页</a>";
  294. }
  295. }
  296. else
  297. {
  298. echo "<br />".$return_rs['msg']." <a href='javascript:jump();'>返回首页</a>";
  299. }
  300. }
  301. }
  302. ?>