/system/manager/class/web/database.php

https://github.com/baijiacms/baijiacmsV3 · PHP · 251 lines · 223 code · 28 blank · 0 comment · 60 complexity · cdb4ed09f11caa7c4c2a99d1cf3c4298 MD5 · raw file

  1. <?php
  2. defined('SYSTEM_IN') or exit('Access Denied');
  3. $operation = !empty($_GP['op']) ? $_GP['op'] : 'display';
  4. if($operation=='display')
  5. {
  6. function dump_escape_mimic($inp) {
  7. return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $inp);
  8. }
  9. function dump_export($continue = array()) {
  10. global $_GP;
  11. $sql = "SHOW TABLE STATUS LIKE 'baijiacms_%'";
  12. $tables = mysqld_selectall($sql);
  13. if(empty($tables)) {
  14. return false;
  15. }
  16. if(empty($continue)) {
  17. do {
  18. $bakdir = WEB_ROOT . '/config/data_backup/' . time() . '_' . random(8);
  19. } while(is_dir($bakdir));
  20. mkdirs($bakdir);
  21. } else {
  22. $bakdir = $continue['bakdir'];
  23. }
  24. $size = 300;
  25. $volumn = 1024 * 1024 * 2;
  26. $series = 1;
  27. if(!empty($continue)) {
  28. $series = $continue['series'];
  29. }
  30. $dump = '';
  31. $catch = false;
  32. if(empty($continue)) {
  33. $catch = true;
  34. }
  35. foreach($tables as $t) {
  36. $t = array_shift($t);
  37. if(!empty($continue) && $t == $continue['table']) {
  38. $catch = true;
  39. }
  40. if(!$catch ) {
  41. continue;
  42. }
  43. if(!empty($dump)) {
  44. $dump .= "\n\n";
  45. }
  46. if($t != $continue['table']) {
  47. $dump .= "DROP TABLE IF EXISTS {$t};\n";
  48. $sql = "SHOW CREATE TABLE {$t}";
  49. $row = mysqld_select($sql);
  50. $dump .= $row['Create Table'];
  51. $dump .= ";\n\n";
  52. }
  53. $fields = mysqld_selectall("SHOW FULL COLUMNS FROM {$t}", array(), 'Field');
  54. if(empty($fields)) {
  55. continue;
  56. }
  57. $index = 0;
  58. if(!empty($continue)) {
  59. $index = $continue['index'];
  60. $continue = array();
  61. }
  62. while(true) {
  63. $start = $index * $size;
  64. $sql = "SELECT * FROM {$t} LIMIT {$start}, {$size}";
  65. $rs = mysqld_selectall($sql);
  66. if(!empty($rs)) {
  67. $tmp = '';
  68. foreach($rs as $row) {
  69. $tmp .= '(';
  70. foreach($row as $k => $v) {
  71. $tmp .= "'" . dump_escape_mimic($v) . "',";
  72. }
  73. $tmp = rtrim($tmp, ',');
  74. $tmp .= "),\n";
  75. }
  76. $tmp = rtrim($tmp, ",\n");
  77. $dump .= "INSERT INTO {$t} VALUES \n{$tmp};\n";
  78. if(strlen($dump) > $volumn) {
  79. $bakfile = $bakdir . "/baijiacms-{$series}.sql";
  80. $dump .= "\n\n";
  81. file_put_contents($bakfile, $dump);
  82. $series++;
  83. $ctu = array();
  84. $ctu['table'] = $t;
  85. $ctu['index'] = $index + 1;
  86. $ctu['series'] = $series;
  87. $ctu['bakdir'] = $bakdir;
  88. return $ctu;
  89. }
  90. }
  91. if(empty($rs) || count($rs) < $size) {
  92. break;
  93. }
  94. $index++;
  95. }
  96. }
  97. $bakfile = $bakdir . "/baijiacms-{$series}.sql";
  98. $dump .= "\n\n----Baijiacms MySQL Dump End";
  99. file_put_contents($bakfile, $dump);
  100. return false;
  101. }
  102. if (checksubmit("submit")) {
  103. $continue = dump_export();
  104. if(!empty($continue)) {
  105. $postctu=base64_encode(json_encode($continue));
  106. message('正在导出数据, 请不要关闭浏览器, 当前第 1 卷.', create_url('site', array('name' => 'manager','do' => 'database','op'=>'display','ctu'=>$postctu)),'success');
  107. } else {
  108. message('数据已经备份完成', create_url('site', array('name' => 'manager','do' => 'database','op'=>'display')),'success');
  109. }
  110. }
  111. if($_GP['ctu']) {
  112. $ctu = json_decode(base64_decode($_GP['ctu']), true);
  113. $continue = dump_export($ctu);
  114. if(!empty($continue)) {
  115. $postctu=base64_encode(json_encode($continue));
  116. message('正在导出数据, 请不要关闭浏览器, 当前第 ' . $ctu['series'] . ' 卷.', create_url('site', array('name' => 'manager','do' => 'database','op'=>'display','ctu'=>$postctu)),'success');
  117. } else {
  118. message('数据已经备份完成', create_url('site', array('name' => 'manager','do' => 'database','op'=>'display')),'success');
  119. }
  120. }
  121. include page('database');
  122. }
  123. if($operation=='restore')
  124. {
  125. $ds = array();
  126. $path = WEB_ROOT . '/config/data_backup/';
  127. if (is_dir($path)) {
  128. if ($handle = opendir($path)) {
  129. while (false !== ($bakdir = readdir($handle))) {
  130. if($bakdir == '.' || $bakdir == '..') {
  131. continue;
  132. }
  133. if(preg_match('/^(?P<time>\d{10})_[a-z\d]{8}$/i', $bakdir, $match)) {
  134. $time = $match['time'];
  135. for($i = 1;;) {
  136. $last = $path . $bakdir . "/baijiacms-{$i}.sql";
  137. $i++;
  138. $next = $path . $bakdir . "/baijiacms-{$i}.sql";
  139. if(!is_file($next)) {
  140. break;
  141. }
  142. }
  143. if(is_file($last)) {
  144. $fp = fopen($last, 'r');
  145. fseek($fp, -27, SEEK_END);
  146. $end = fgets($fp);
  147. fclose($fp);
  148. if($end == '----Baijiacms MySQL Dump End'||$end == '---Baijiacms MySQL Dump End') {
  149. $row = array();
  150. $row['bakdir'] = $bakdir;
  151. $row['time'] = $time;
  152. $row['volume'] = $i - 1;
  153. $ds[$bakdir] = $row;
  154. continue;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
  161. include page('database');
  162. }
  163. if($operation=='torestore')
  164. {
  165. $ds = array();
  166. $path = WEB_ROOT . '/config/data_backup/';
  167. if (is_dir($path)) {
  168. if ($handle = opendir($path)) {
  169. while (false !== ($bakdir = readdir($handle))) {
  170. if($bakdir == '.' || $bakdir == '..') {
  171. continue;
  172. }
  173. if(preg_match('/^(?P<time>\d{10})_[a-z\d]{8}$/i', $bakdir, $match)) {
  174. $time = $match['time'];
  175. for($i = 1;;) {
  176. $last = $path . $bakdir . "/baijiacms-{$i}.sql";
  177. $i++;
  178. $next = $path . $bakdir . "/baijiacms-{$i}.sql";
  179. if(!is_file($next)) {
  180. break;
  181. }
  182. }
  183. if(is_file($last)) {
  184. $fp = fopen($last, 'r');
  185. fseek($fp, -27, SEEK_END);
  186. $end = fgets($fp);
  187. fclose($fp);
  188. if($end == '----Baijiacms MySQL Dump End'||$end == '---Baijiacms MySQL Dump End') {
  189. $row = array();
  190. $row['bakdir'] = $bakdir;
  191. $row['time'] = $time;
  192. $row['volume'] = $i - 1;
  193. $ds[$bakdir] = $row;
  194. continue;
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. $r = base64_decode($_GP['id']);
  202. $path = WEB_ROOT . '/config/data_backup/';
  203. if(is_dir($path . $r)) {
  204. $row = $ds[$r];
  205. for($i = 1; $i <= $row['volume']; $i++) {
  206. $sql = file_get_contents($path . $row['bakdir'] . "/baijiacms-{$i}.sql");
  207. if(!empty($sql))
  208. {
  209. mysqld_batch($sql);
  210. }
  211. }
  212. message('还原成功!', create_url('site', array('name' => 'manager','do' => 'database','op'=>'restore')),'success');
  213. }
  214. }
  215. if($operation=='delete')
  216. {
  217. $d = base64_decode($_GP['id']);
  218. $path = WEB_ROOT . '/config/data_backup/';
  219. if(is_dir($path . $d)) {
  220. rmdirs($path . $d);
  221. message('备份删除成功!', create_url('site', array('name' => 'manager','do' => 'database','op'=>'restore')),'success');
  222. }
  223. }