PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Admin/Lib/Action/BakAction.class.php

http://nblog-thinkphp.googlecode.com/
PHP | 409 lines | 309 code | 42 blank | 58 comment | 72 complexity | e355fb321637ecee2bc5e1d338fd8cd7 MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ?????
  4. // +----------------------------------------------------------------------
  5. // | @link ( http://www.yurnero.net )
  6. // +----------------------------------------------------------------------
  7. // | @copyright
  8. // +----------------------------------------------------------------------
  9. // | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // | @author Haijun Wu <nicholasinlove@126.com>
  12. // +----------------------------------------------------------------------
  13. // | $Id: BakAction.class.php 109 2011-05-06 08:21:00Z nicholasinlove1986@gmail.com $
  14. // +----------------------------------------------------------------------
  15. class BakAction extends BaseAction {
  16. public function index() {
  17. redirect(__APP__);
  18. }
  19. //????
  20. public function backup() {
  21. //$this->checkLimits();
  22. /* ?????? */
  23. $path = ROOT_PATH . DATA_DIR . '/Sqldata';
  24. $mask = file_mode_info($path);
  25. if ($mask === false) {
  26. $warning = sprintf(L('dir_not_exist'), $path);
  27. $this->assign('warning', $warning);
  28. } elseif ($mask != 15) {
  29. $warning = sprintf(L('dir_priv'), $path) . '<br/>';
  30. if (($mask&1) < 1) {
  31. $warning .= L('cannot_read') . '&nbsp;&nbsp;';
  32. }
  33. if (($mask&2) < 1) {
  34. $warning .= L('cannot_write') . '&nbsp;&nbsp;';
  35. }
  36. if (($mask&4) < 1) {
  37. $warning .= L('cannot_add') . '&nbsp;&nbsp;';
  38. }
  39. if (($mask&8) < 1) {
  40. $warning .= L('cannot_modify');
  41. }
  42. $this->assign('warning', $warning);
  43. }
  44. //????????
  45. $allow_max_size = return_bytes(@ini_get('upload_max_filesize')); // ?????
  46. $allow_max_size = $allow_max_size / 1024; // ????? KB
  47. $table = $this->getTables();
  48. $this->assign('action_link', array('href'=>__URL__.'/restore/', 'text' => L('restore')));
  49. $this->assign('ur_here',L('backup'));
  50. $this->assign('file_name', get_random_name().'.sql');
  51. $this->assign('vol_size', $allow_max_size);
  52. $this->assign('table', $table);
  53. $this->display();
  54. }
  55. //????
  56. public function dumpSql() {
  57. $this->checkLimits();
  58. if ($_REQUEST['dumpsql']) {
  59. /* ?????? */
  60. $path = ROOT_PATH . DATA_DIR . '/Sqldata';
  61. $mask = file_mode_info($path);
  62. if ($mask === false) {
  63. $warning = sprintf(L('dir_not_exist'), $path);
  64. $this->error ($warning);
  65. } elseif ($mask != 15) {
  66. $warning = sprintf(L('dir_priv'), $path);
  67. if (($mask&1) < 1) {
  68. $warning .= L('cannot_read') . '&nbsp;&nbsp;';
  69. }
  70. if (($mask&2) < 1) {
  71. $warning .= L('cannot_write') . '&nbsp;&nbsp;';
  72. }
  73. if (($mask&4) < 1) {
  74. $warning .= L('cannot_add') . '&nbsp;&nbsp;';
  75. }
  76. if (($mask&8) < 1) {
  77. $warning .= L('cannot_modify');
  78. }
  79. $this->error ($warning);
  80. }
  81. import("@.ORG.Sql");
  82. $dump = new Sql ();
  83. $max_size = empty($_REQUEST['vol_size']) ? 0 : intval($_REQUEST['vol_size']);
  84. /* ???? */
  85. $allow_max_size = intval(@ini_get('upload_max_filesize')); //??M
  86. if ($allow_max_size > 0 && $max_size > ($allow_max_size * 1024)) {
  87. $max_size = $allow_max_size * 1024; //??K
  88. }
  89. if ($max_size > 0) {
  90. $max_size = $max_size * 1024;
  91. }
  92. /* ????????*/
  93. if (empty($_REQUEST['customtables'])) {
  94. $tables = $this->getTables();
  95. } else {
  96. $tables = $_REQUEST['customtables'];
  97. }
  98. /* ??????? */
  99. if (empty($_REQUEST['file_name'])) {
  100. $file_name = $dump->get_random_name();
  101. } else {
  102. $file_name = str_replace("0xa", '', trim($_REQUEST['file_name'])); // ?? 0xa ????
  103. $pos = strpos($file_name, '.sql');
  104. if ($pos !== false) {
  105. $file_name = substr($file_name, 0, $pos);
  106. }
  107. }
  108. if ($dump->dumpTable($path,$file_name,$tables,$max_size)) {
  109. set_log('backup(Sql)');
  110. $this->assign('jumpUrl',__URL__.'/restore/');
  111. $this->success (L('success'));
  112. } else {
  113. $this->error (L('error'));
  114. }
  115. } else {
  116. $this->error (L('illegal'));
  117. }
  118. }
  119. //??????
  120. public function restore() {
  121. //$this->checkLimits();
  122. /* ?????? */
  123. $path = ROOT_PATH . DATA_DIR . '/Sqldata';
  124. $mask = file_mode_info($path);
  125. if ($mask === false) {
  126. $warning = sprintf(L('dir_not_exist'), $path);
  127. $this->assign('warning', $warning);
  128. } elseif ($mask != 15) {
  129. $warning = sprintf(L('dir_priv'), $path);
  130. if (($mask&1) < 1) {
  131. $warning .= L('cannot_read') . '&nbsp;&nbsp;';
  132. }
  133. if (($mask&2) < 1) {
  134. $warning .= L('cannot_write') . '&nbsp;&nbsp;';
  135. }
  136. if (($mask&4) < 1) {
  137. $warning .= L('cannot_add') . '&nbsp;&nbsp;';
  138. }
  139. if (($mask&8) < 1) {
  140. $warning .= L('cannot_modify');
  141. }
  142. $this->assign('warning', $warning);
  143. } else {
  144. import("@.ORG.Sql");
  145. /* ?????? */
  146. $real_list = array();
  147. $folder = opendir($path);
  148. while ($file = readdir($folder)) {
  149. if (strpos($file,'.sql') !== false) {
  150. $real_list[] = $file;
  151. }
  152. }
  153. natsort($real_list);
  154. $match = array();
  155. foreach ($real_list as $file) {
  156. if (preg_match('/_([0-9])+\.sql$/', $file, $match)) {
  157. if ($match[1] == 1) {
  158. $mark = 1;
  159. }else {
  160. $mark = 2;
  161. }
  162. }else {
  163. $mark = 0;
  164. }
  165. $file_size = filesize($path ."/". $file);
  166. $info = Sql::getHead($path ."/". $file);
  167. $list[] = array('name' => $file, 'ver' => $info['nblog_ver'], 'add_time' => $info['date'], 'vol' => $info['vol'], 'file_size' => num_bitunit($file_size), 'mark' => $mark);
  168. }
  169. }
  170. //print_r($list);
  171. $this->assign('action_link', array('href'=>__URL__.'/backup/', 'text' => L('backup')));
  172. $this->assign('ur_here',L('restore'));
  173. $this->assign('list',$list);
  174. $this->display();
  175. }
  176. //???????
  177. public function remove() {
  178. $this->checkLimits();
  179. if ($_REQUEST['delSql']) {
  180. //print_r($_POST['chkvalue']);
  181. if (isset($_POST['chkvalue'])) {
  182. $m_file = array(); //????
  183. $s_file = array(); //????
  184. $path = ROOT_PATH . DATA_DIR . '/Sqldata/';
  185. foreach ($_POST['chkvalue'] as $file) {
  186. if (preg_match('/_[0-9]+\.sql$/', $file)) {
  187. $m_file[] = substr($file, 0, strrpos($file, '_'));
  188. } else {
  189. $s_file[] = $file;
  190. }
  191. }
  192. if ($m_file) {
  193. $m_file = array_unique ($m_file);
  194. // ??????
  195. $real_file = array();
  196. $folder = opendir($path);
  197. while ($file = readdir($folder)) {
  198. if ( preg_match('/_[0-9]+\.sql$/', $file) && is_file($path . $file)) {
  199. $real_file[] = $file;
  200. }
  201. }
  202. foreach ($real_file as $file) {
  203. $short_file = substr($file, 0, strrpos($file, '_'));
  204. if (in_array($short_file, $m_file)) {
  205. @unlink($path . $file);
  206. }
  207. }
  208. }
  209. if ($s_file) {
  210. foreach ($s_file as $file) {
  211. @unlink($path . $file);
  212. }
  213. }
  214. set_log('drop(Sql)');
  215. $this->assign('jumpUrl',__URL__.'/restore/');
  216. $this->success (L('success'));
  217. }
  218. } else {
  219. $this->error (L('illegal'));
  220. }
  221. }
  222. //????
  223. public function import() {
  224. $this->checkLimits();
  225. if ($_REQUEST['act']) {
  226. $file_name = empty($_GET['f']) ? '': trim($_GET['f']);
  227. $path = ROOT_PATH . DATA_DIR . '/Sqldata/';
  228. import("@.ORG.Sql");
  229. $dump = new Sql ();
  230. if (preg_match('/_[0-9]+\.sql$/', $file_name)) {
  231. //??
  232. $short_name = substr($file_name, 0, strrpos($file_name, '_'));
  233. // ??????
  234. $real_file = array();
  235. $folder = opendir($path);
  236. while ($file = readdir($folder)) {
  237. if (is_file($path . $file) && preg_match('/_[0-9]+\.sql$/', $file)) {
  238. $real_file[] = $file;
  239. }
  240. }
  241. // ??????????
  242. $post_list = array();
  243. foreach ($real_file as $file) {
  244. $tmp_name = substr($file, 0, strrpos($file, '_'));
  245. if ($tmp_name == $short_name) {
  246. $post_list[] = $file;
  247. }
  248. }
  249. natsort($post_list);
  250. // ??????
  251. foreach ($post_list as $file) {
  252. $info = $dump->getHead($path . $file_name);
  253. if ($info['nblog_ver'] != VERSION ) {
  254. $this->error(sprintf(L('version_error'), VERSION, $info['nblog_ver']));
  255. }
  256. if (!$dump->sqlImport($path . $file)) {
  257. $this->error(L('sqlfile_error'));
  258. }
  259. }
  260. clear_cache_files(); //????
  261. set_log('restore(Sql, Date: '.$info['date'].')');
  262. $this->assign('jumpUrl',__URL__.'/restore/');
  263. $this->success (L('success'));
  264. } else {
  265. // ??
  266. $info = $dump->getHead($path . $file_name);
  267. if ($info['nblog_ver'] != VERSION ) {
  268. $this->error(sprintf(L('version_error'), VERSION, $info['nblog_ver']));
  269. }
  270. if ($dump->sqlImport($path . $file_name)) {
  271. clear_cache_files(); //????
  272. set_log('restore(Sql, Date: '.$info['date'].')');
  273. $this->assign('jumpUrl',__URL__.'/restore/');
  274. $this->success (L('success'));
  275. } else {
  276. $this->error(L('sqlfile_error'));
  277. }
  278. }
  279. } else {
  280. $this->error (L('illegal'));
  281. }
  282. }
  283. //?????
  284. public function optimize() {
  285. $db = M();
  286. $db_ver = $db->query('select version();');
  287. $db_ver = $db_ver[0]['version()'];
  288. $table = $this->getTables();
  289. for ($i=0; $i<count($table); $i++) {
  290. $res[] = $db->query("SHOW TABLE STATUS LIKE '" .$table[$i] . "%'");
  291. $status[] = $db->query('CHECK TABLE '.$table[$i]);
  292. }
  293. //echo "<pre>";
  294. //print_r($res);
  295. //echo "</pre>";
  296. for ($i=0; $i<count($res); $i++) {
  297. $type = $db_ver >= '4.1' ? $res[$i][0]['Engine'] : 'Type';
  298. $charset = $db_ver >= '4.1' ? $res[$i][0]['Collation'] : 'N/A';
  299. $num += $res[$i][0]['Data_free'];
  300. $res_list[$i]['Name'] = $res[$i][0]['Name'];
  301. $res_list[$i]['Type'] = $type;
  302. $res_list[$i]['Rows'] = $res[$i][0]['Rows'];
  303. $res_list[$i]['Data_length'] = sprintf(" %.2f KB", $res[$i][0]['Data_length'] / 1024);
  304. $res_list[$i]['Data_free'] = $res[$i][0]['Data_free'];
  305. $res_list[$i]['Charset'] = $charset;
  306. $res_list[$i]['Create_time'] = $res[$i][0]['Create_time'];
  307. $res_list[$i]['Update_time'] = $res[$i][0]['Update_time'];
  308. $res_list[$i]['Msg_text'] = $status[$i][0]['Msg_text'];
  309. }
  310. //echo "<pre>";
  311. //print_r($res_list);
  312. //echo "</pre>";
  313. $this->assign('ur_here',L('optimize'));
  314. $this->assign('num',$num);
  315. $this->assign('res_list',$res_list);
  316. $this->display();
  317. }
  318. //?????
  319. public function runOptimize() {
  320. $this->checkLimits();
  321. if ($_REQUEST['run']) {
  322. $db = M();
  323. $table = $this->getTables();
  324. foreach ($table as $name) {
  325. if (isset($name)) {
  326. //$msg .='?????: '.$name;
  327. $res = $db->query('optimize TABLE '.$name);
  328. /* ????????? */
  329. if ($res[0]['Msg_type'] =='error' && strpos($res[0]['Msg_text'], 'repair') !== false) {
  330. $db->query('REPAIR TABLE ' . $name);
  331. }
  332. /*if ($res) {
  333. $msg .= ' .........................................??<br>';
  334. } else {
  335. $msg .= ' <font color=\"red\"><b>??</b></font>';
  336. }*/
  337. }
  338. }
  339. $msgg .= L('optimize_ok').$_POST['num'];
  340. set_log('optimize(Sql, Overhead: '.$_POST["num"].')');
  341. $this->assign('jumpUrl',__URL__.'/optimize/');
  342. $this->success ($msgg);
  343. } else {
  344. $this->error (L('illegal'));
  345. }
  346. }
  347. /**
  348. +----------------------------------------------------------
  349. * ?????????
  350. +----------------------------------------------------------
  351. * @access public
  352. +----------------------------------------------------------
  353. */
  354. public function getTables() {
  355. $sql = 'SHOW TABLES FROM '.C('DB_NAME');
  356. $db = M();
  357. $result = $db->query($sql);
  358. $info = array();
  359. foreach ($result as $val) {
  360. if(substr(current($val),0,strlen(C('DB_PREFIX'))) == C('DB_PREFIX')) {
  361. $info[] = current($val);
  362. }
  363. }
  364. return $info;
  365. }
  366. }
  367. ?>