Update.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // 注意:修改此文件后,需同步更新 application/extra/version.php 中的 update_hash 值
  3. // update_hash = md5_file('application/admin/controller/Update.php')
  4. namespace app\admin\controller;
  5. use think\Db;
  6. use app\common\util\PclZip;
  7. class Update extends Base
  8. {
  9. var $_url;
  10. var $_save_path;
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. //header('X-Accel-Buffering: no');
  15. $this->_url = base64_decode("aHR0cHM6Ly91cGRhdGUubWFjY21zLmxhLw==")."v10/";
  16. $this->_save_path = './application/data/update/';
  17. }
  18. public function index()
  19. {
  20. return $this->fetch('admin@test/index');
  21. }
  22. public function step1($file='')
  23. {
  24. if(empty($file)){
  25. return $this->error(lang('param_err'));
  26. }
  27. $version = config('version.code');
  28. $url = $this->_url .$file . '.zip?t='.time();
  29. echo $this->fetch('admin@public/head');
  30. echo "<div class='update'><h1>".lang('admin/update/step1_a')."</h1><textarea rows=\"25\" class='layui-textarea' readonly>".lang('admin/update/step1_b')."\n";
  31. ob_flush();flush();
  32. sleep(1);
  33. $save_file = $version.'.zip';
  34. $html = mac_curl_get($url);
  35. @fwrite(@fopen($this->_save_path.$save_file,'wb'),$html);
  36. if(!is_file($this->_save_path.$save_file)){
  37. echo lang('admin/update/download_err')."\n";
  38. exit;
  39. }
  40. if(filesize($this->_save_path.$save_file) <1){
  41. @unlink($this->_save_path.$save_file);
  42. echo lang('admin/update/download_err')."\n";
  43. exit;
  44. }
  45. // SHA1校验:.sha1文件进行比对防篡改
  46. $sha1_url = $this->_url . $file . '.zip.sha1?t=' . time();
  47. $remote_sha1 = trim(mac_curl_get($sha1_url));
  48. $local_sha1 = sha1_file($this->_save_path . $save_file);
  49. if (empty($remote_sha1) || strpos($remote_sha1, $local_sha1) !== 0) {
  50. @unlink($this->_save_path . $save_file);
  51. echo lang('admin/update/sha1_err') . "\n";
  52. exit;
  53. }
  54. echo lang('admin/update/sha1_ok') . "\n";
  55. echo lang('admin/update/download_ok')."\n";
  56. echo lang('admin/update/upgrade_package_processed')."\n";
  57. ob_flush();flush();
  58. sleep(1);
  59. $archive = new PclZip();
  60. $archive->PclZip($this->_save_path.$save_file);
  61. if(!$archive->extract(PCLZIP_OPT_PATH, '', PCLZIP_OPT_REPLACE_NEWER)) {
  62. echo $archive->error_string."\n";
  63. echo lang('admin/update/upgrade_err').'' ."\n";;
  64. exit;
  65. }
  66. else{
  67. }
  68. @unlink($this->_save_path.$save_file);
  69. echo '</textarea></div>';
  70. mac_jump( url('update/step2',['jump'=>1]) ,3);
  71. }
  72. public function step2()
  73. {
  74. $version = config('version.code');
  75. $save_file = 'database.php';
  76. echo $this->fetch('admin@public/head');
  77. echo "<div class='update'><h1>".lang('admin/update/step2_a')."</h1><textarea rows=\"25\" class='layui-textarea' readonly>\n";
  78. ob_flush();flush();
  79. sleep(1);
  80. $res=true;
  81. // 导入SQL
  82. $sql_file = $this->_save_path .$save_file;
  83. if (is_file($sql_file)) {
  84. echo lang('admin/update/upgrade_sql')."\n";
  85. ob_flush();flush();
  86. $pre = config('database.prefix');
  87. $schema = Db::query('select * from information_schema.columns where table_schema = ?',[ config('database.database') ]);
  88. $col_list = [];
  89. $sql='';
  90. foreach($schema as $k=>$v){
  91. $col_list[$v['TABLE_NAME']][$v['COLUMN_NAME']] = $v;
  92. }
  93. @include $sql_file;
  94. //dump($sql);die;
  95. /*
  96. //$html = @file_get_contents($sql_file);
  97. //$sql = mac_get_body($html,'--'.$version.'-start--','--'.$version.'-end--');
  98. $sql = @file_get_contents($sql_file);
  99. */
  100. if(!empty($sql)) {
  101. $sql_list = mac_parse_sql($sql, 0, ['mac_' => $pre]);
  102. if ($sql_list) {
  103. $sql_list = array_filter($sql_list);
  104. foreach ($sql_list as $v) {
  105. echo $v;
  106. try {
  107. Db::execute($v);
  108. echo " ---".lang('success')."\n\n";
  109. } catch (\Exception $e) {
  110. echo " ---".lang('fail')."\n\n";
  111. }
  112. ob_flush();flush();
  113. }
  114. }
  115. }
  116. else{
  117. }
  118. @unlink($sql_file);
  119. }
  120. else{
  121. echo lang('admin/update/no_sql')."\n";
  122. }
  123. echo '</textarea></div>';
  124. mac_jump(url('update/step3', ['jump' => 1]), 3);
  125. }
  126. public function step3()
  127. {
  128. echo $this->fetch('admin@public/head');
  129. echo "<div class='update'><h1>".lang('admin/update/step3_a')."</h1><div rows=\"25\" class='layui-textarea' readonly>\n";
  130. ob_flush();flush();
  131. sleep(1);
  132. $this->_cache_clear();
  133. echo lang('admin/update/update_cache')."<br>";
  134. echo lang('admin/update/upgrade_complete')."<br>";
  135. if(is_file($this->_save_path . 'database.php')){
  136. echo "<strong style='color: red;'>" . lang('admin/update/not_delete') . ":application/data/update/database.php</strong>";
  137. }
  138. ob_flush();flush();
  139. echo '</div></div>';
  140. }
  141. }