PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Upload/admin/modules/tools/backupdb.php

https://gitlab.com/Conors99/ppm-1.8
PHP | 484 lines | 391 code | 71 blank | 22 comment | 85 complexity | 976cb44d3b9f338e72623c1fa2949293 MD5 | raw file
  1. <?php
  2. /**
  3. * MyBB 1.8
  4. * Copyright 2014 MyBB Group, All Rights Reserved
  5. *
  6. * Website: http://www.mybb.com
  7. * License: http://www.mybb.com/about/license
  8. *
  9. */
  10. // Disallow direct access to this file for security reasons
  11. if(!defined("IN_MYBB"))
  12. {
  13. die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
  14. }
  15. /**
  16. * Allows us to refresh cache to prevent over flowing
  17. *
  18. * @param resource $fp
  19. * @param string $contents
  20. */
  21. function clear_overflow($fp, &$contents)
  22. {
  23. global $mybb;
  24. if($mybb->input['method'] == 'disk')
  25. {
  26. if($mybb->input['filetype'] == 'gzip')
  27. {
  28. gzwrite($fp, $contents);
  29. }
  30. else
  31. {
  32. fwrite($fp, $contents);
  33. }
  34. }
  35. else
  36. {
  37. if($mybb->input['filetype'] == "gzip")
  38. {
  39. echo gzencode($contents);
  40. }
  41. else
  42. {
  43. echo $contents;
  44. }
  45. }
  46. $contents = '';
  47. }
  48. $page->add_breadcrumb_item($lang->database_backups, "index.php?module=tools-backupdb");
  49. $plugins->run_hooks("admin_tools_backupdb_begin");
  50. if($mybb->input['action'] == "dlbackup")
  51. {
  52. if(empty($mybb->input['file']))
  53. {
  54. flash_message($lang->error_file_not_specified, 'error');
  55. admin_redirect("index.php?module=tools-backupdb");
  56. }
  57. $plugins->run_hooks("admin_tools_backupdb_dlbackup");
  58. $file = basename($mybb->input['file']);
  59. $ext = get_extension($file);
  60. if(file_exists(MYBB_ADMIN_DIR.'backups/'.$file) && filetype(MYBB_ADMIN_DIR.'backups/'.$file) == 'file' && ($ext == 'gz' || $ext == 'sql'))
  61. {
  62. $plugins->run_hooks("admin_tools_backupdb_dlbackup_commit");
  63. // Log admin action
  64. log_admin_action($file);
  65. header('Content-disposition: attachment; filename='.$file);
  66. header("Content-type: ".$ext);
  67. header("Content-length: ".filesize(MYBB_ADMIN_DIR.'backups/'.$file));
  68. $handle = fopen(MYBB_ADMIN_DIR.'backups/'.$file, 'rb');
  69. while(!feof($handle))
  70. {
  71. echo fread($handle, 8192);
  72. }
  73. fclose($handle);
  74. }
  75. else
  76. {
  77. flash_message($lang->error_invalid_backup, 'error');
  78. admin_redirect("index.php?module=tools-backupdb");
  79. }
  80. }
  81. if($mybb->input['action'] == "delete")
  82. {
  83. if($mybb->input['no'])
  84. {
  85. admin_redirect("index.php?module=tools-backupdb");
  86. }
  87. $file = basename($mybb->input['file']);
  88. if(!trim($mybb->input['file']) || !file_exists(MYBB_ADMIN_DIR.'backups/'.$file))
  89. {
  90. flash_message($lang->error_backup_doesnt_exist, 'error');
  91. admin_redirect("index.php?module=tools-backupdb");
  92. }
  93. $plugins->run_hooks("admin_tools_backupdb_delete");
  94. if($mybb->request_method == "post")
  95. {
  96. $delete = @unlink(MYBB_ADMIN_DIR.'backups/'.$file);
  97. if($delete)
  98. {
  99. $plugins->run_hooks("admin_tools_backupdb_delete_commit");
  100. // Log admin action
  101. log_admin_action($file);
  102. flash_message($lang->success_backup_deleted, 'success');
  103. admin_redirect("index.php?module=tools-backupdb");
  104. }
  105. else
  106. {
  107. flash_message($lang->error_backup_not_deleted, 'error');
  108. admin_redirect("index.php?module=tools-backupdb");
  109. }
  110. }
  111. else
  112. {
  113. $page->output_confirm_action("index.php?module=tools-backupdb&amp;action=delete&amp;file={$mybb->input['file']}", $lang->confirm_backup_deletion);
  114. }
  115. }
  116. if($mybb->input['action'] == "backup")
  117. {
  118. $plugins->run_hooks("admin_tools_backupdb_backup");
  119. if($mybb->request_method == "post")
  120. {
  121. if(!is_array($mybb->input['tables']))
  122. {
  123. flash_message($lang->error_tables_not_selected, 'error');
  124. admin_redirect("index.php?module=tools-backupdb&action=backup");
  125. }
  126. @set_time_limit(0);
  127. if($mybb->input['method'] == 'disk')
  128. {
  129. $file = MYBB_ADMIN_DIR.'backups/backup_'.date("_Ymd_His_").random_str(16);
  130. if($mybb->input['filetype'] == 'gzip')
  131. {
  132. if(!function_exists('gzopen')) // check zlib-ness
  133. {
  134. flash_message($lang->error_no_zlib, 'error');
  135. admin_redirect("index.php?module=tools-backupdb&action=backup");
  136. }
  137. $fp = gzopen($file.'.incomplete.sql.gz', 'w9');
  138. }
  139. else
  140. {
  141. $fp = fopen($file.'.incomplete.sql', 'w');
  142. }
  143. }
  144. else
  145. {
  146. $file = 'backup_'.substr(md5($mybb->user['uid'].TIME_NOW), 0, 10).random_str(54);
  147. if($mybb->input['filetype'] == 'gzip')
  148. {
  149. if(!function_exists('gzopen')) // check zlib-ness
  150. {
  151. flash_message($lang->error_no_zlib, 'error');
  152. admin_redirect("index.php?module=tools-backupdb&action=backup");
  153. }
  154. // Send headers for gzip file
  155. header('Content-Encoding: gzip');
  156. header('Content-Type: application/x-gzip');
  157. header('Content-Disposition: attachment; filename="'.$file.'.sql.gz"');
  158. }
  159. else
  160. {
  161. // Send standard headers for .sql
  162. header('Content-Type: text/x-sql');
  163. header('Content-Disposition: attachment; filename="'.$file.'.sql"');
  164. }
  165. }
  166. $db->set_table_prefix('');
  167. $time = date('dS F Y \a\t H:i', TIME_NOW);
  168. $header = "-- MyBB Database Backup\n-- Generated: {$time}\n-- -------------------------------------\n\n";
  169. $contents = $header;
  170. foreach($mybb->input['tables'] as $table)
  171. {
  172. if(!$db->table_exists($db->escape_string($table)))
  173. {
  174. continue;
  175. }
  176. if($mybb->input['analyzeoptimize'] == 1)
  177. {
  178. $db->optimize_table($table);
  179. $db->analyze_table($table);
  180. }
  181. $field_list = array();
  182. $fields_array = $db->show_fields_from($table);
  183. foreach($fields_array as $field)
  184. {
  185. $field_list[] = $field['Field'];
  186. }
  187. $fields = "`".implode("`,`", $field_list)."`";
  188. if($mybb->input['contents'] != 'data')
  189. {
  190. $structure = $db->show_create_table($table).";\n";
  191. $contents .= $structure;
  192. clear_overflow($fp, $contents);
  193. }
  194. if($mybb->input['contents'] != 'structure')
  195. {
  196. if($db->engine == 'mysqli')
  197. {
  198. $query = mysqli_query($db->read_link, "SELECT * FROM {$db->table_prefix}{$table}", MYSQLI_USE_RESULT);
  199. }
  200. else
  201. {
  202. $query = $db->simple_select($table);
  203. }
  204. while($row = $db->fetch_array($query))
  205. {
  206. $insert = "INSERT INTO {$table} ($fields) VALUES (";
  207. $comma = '';
  208. foreach($field_list as $field)
  209. {
  210. if(!isset($row[$field]) || is_null($row[$field]))
  211. {
  212. $insert .= $comma."NULL";
  213. }
  214. else if($db->engine == 'mysqli')
  215. {
  216. $insert .= $comma."'".mysqli_real_escape_string($db->read_link, $row[$field])."'";
  217. }
  218. else
  219. {
  220. $insert .= $comma."'".$db->escape_string($row[$field])."'";
  221. }
  222. $comma = ',';
  223. }
  224. $insert .= ");\n";
  225. $contents .= $insert;
  226. clear_overflow($fp, $contents);
  227. }
  228. $db->free_result($query);
  229. }
  230. }
  231. $db->set_table_prefix(TABLE_PREFIX);
  232. if($mybb->input['method'] == 'disk')
  233. {
  234. if($mybb->input['filetype'] == 'gzip')
  235. {
  236. gzwrite($fp, $contents);
  237. gzclose($fp);
  238. rename($file.'.incomplete.sql.gz', $file.'.sql.gz');
  239. }
  240. else
  241. {
  242. fwrite($fp, $contents);
  243. fclose($fp);
  244. rename($file.'.incomplete.sql', $file.'.sql');
  245. }
  246. if($mybb->input['filetype'] == 'gzip')
  247. {
  248. $ext = '.sql.gz';
  249. }
  250. else
  251. {
  252. $ext = '.sql';
  253. }
  254. $plugins->run_hooks("admin_tools_backupdb_backup_disk_commit");
  255. // Log admin action
  256. log_admin_action("disk", $file.$ext);
  257. $file_from_admindir = 'index.php?module=tools-backupdb&amp;action=dlbackup&amp;file='.basename($file).$ext;
  258. flash_message("<span><em>{$lang->success_backup_created}</em></span><p>{$lang->backup_saved_to}<br />{$file}{$ext} (<a href=\"{$file_from_admindir}\">{$lang->download}</a>)</p>", 'success');
  259. admin_redirect("index.php?module=tools-backupdb");
  260. }
  261. else
  262. {
  263. $plugins->run_hooks("admin_tools_backupdb_backup_download_commit");
  264. // Log admin action
  265. log_admin_action("download");
  266. if($mybb->input['filetype'] == 'gzip')
  267. {
  268. echo gzencode($contents);
  269. }
  270. else
  271. {
  272. echo $contents;
  273. }
  274. }
  275. exit;
  276. }
  277. $page->extra_header = " <script type=\"text/javascript\">
  278. function changeSelection(action, prefix)
  279. {
  280. var select_box = document.getElementById('table_select');
  281. for(var i = 0; i < select_box.length; i++)
  282. {
  283. if(action == 'select')
  284. {
  285. select_box[i].selected = true;
  286. }
  287. else if(action == 'deselect')
  288. {
  289. select_box[i].selected = false;
  290. }
  291. else if(action == 'forum' && prefix != 0)
  292. {
  293. select_box[i].selected = false;
  294. var row = select_box[i].value;
  295. var subString = row.substring(prefix.length, 0);
  296. if(subString == prefix)
  297. {
  298. select_box[i].selected = true;
  299. }
  300. }
  301. }
  302. }
  303. </script>\n";
  304. $page->add_breadcrumb_item($lang->new_database_backup);
  305. $page->output_header($lang->new_database_backup);
  306. $sub_tabs['database_backup'] = array(
  307. 'title' => $lang->database_backups,
  308. 'link' => "index.php?module=tools-backupdb"
  309. );
  310. $sub_tabs['new_backup'] = array(
  311. 'title' => $lang->new_backup,
  312. 'link' => "index.php?module=tools-backupdb&amp;action=backup",
  313. 'description' => $lang->new_backup_desc
  314. );
  315. $page->output_nav_tabs($sub_tabs, 'new_backup');
  316. // Check if file is writable, before allowing submission
  317. if(!is_writable(MYBB_ADMIN_DIR."/backups"))
  318. {
  319. $lang->update_button = '';
  320. $page->output_alert($lang->alert_not_writable);
  321. $cannot_write = true;
  322. }
  323. $table = new Table;
  324. $table->construct_header($lang->table_selection);
  325. $table->construct_header($lang->backup_options);
  326. $table_selects = array();
  327. $table_list = $db->list_tables($config['database']['database']);
  328. foreach($table_list as $id => $table_name)
  329. {
  330. $table_selects[$table_name] = $table_name;
  331. }
  332. $form = new Form("index.php?module=tools-backupdb&amp;action=backup", "post", "table_selection", 0, "table_selection");
  333. $table->construct_cell("{$lang->table_select_desc}\n<br /><br />\n<a href=\"javascript:changeSelection('select', 0);\">{$lang->select_all}</a><br />\n<a href=\"javascript:changeSelection('deselect', 0);\">{$lang->deselect_all}</a><br />\n<a href=\"javascript:changeSelection('forum', '".TABLE_PREFIX."');\">{$lang->select_forum_tables}</a>\n<br /><br />\n<div class=\"form_row\">".$form->generate_select_box("tables[]", $table_selects, false, array('multiple' => true, 'id' => 'table_select', 'size' => 20))."</div>", array('rowspan' => 5, 'width' => '50%', 'style' => 'border-bottom: 0px'));
  334. $table->construct_row();
  335. $table->construct_cell("<strong>{$lang->file_type}</strong><br />\n{$lang->file_type_desc}<br />\n<div class=\"form_row\">".$form->generate_radio_button("filetype", "gzip", $lang->gzip_compressed, array('checked' => 1))."<br />\n".$form->generate_radio_button("filetype", "plain", $lang->plain_text)."</div>", array('width' => '50%'));
  336. $table->construct_row();
  337. $table->construct_cell("<strong>{$lang->save_method}</strong><br />\n{$lang->save_method_desc}<br /><div class=\"form_row\">".$form->generate_radio_button("method", "disk", $lang->backup_directory)."<br />\n".$form->generate_radio_button("method", "download", $lang->download, array('checked' => 1))."</div>", array('width' => '50%'));
  338. $table->construct_row();
  339. $table->construct_cell("<strong>{$lang->backup_contents}</strong><br />\n{$lang->backup_contents_desc}<br /><div class=\"form_row\">".$form->generate_radio_button("contents", "both", $lang->structure_and_data, array('checked' => 1))."<br />\n".$form->generate_radio_button("contents", "structure", $lang->structure_only)."<br />\n".$form->generate_radio_button("contents", "data", $lang->data_only)."</div>", array('width' => '50%'));
  340. $table->construct_row();
  341. $table->construct_cell("<strong>{$lang->analyze_and_optimize}</strong><br />\n{$lang->analyze_and_optimize_desc}<br /><div class=\"form_row\">".$form->generate_yes_no_radio("analyzeoptimize")."</div>", array('width' => '50%'));
  342. $table->construct_row();
  343. $table->output($lang->new_database_backup);
  344. $buttons[] = $form->generate_submit_button($lang->perform_backup);
  345. $form->output_submit_wrapper($buttons);
  346. $form->end();
  347. $page->output_footer();
  348. }
  349. if(!$mybb->input['action'])
  350. {
  351. $page->add_breadcrumb_item($lang->backups);
  352. $page->output_header($lang->database_backups);
  353. $sub_tabs['database_backup'] = array(
  354. 'title' => $lang->database_backups,
  355. 'link' => "index.php?module=tools-backupdb",
  356. 'description' => $lang->database_backups_desc
  357. );
  358. $sub_tabs['new_backup'] = array(
  359. 'title' => $lang->new_backup,
  360. 'link' => "index.php?module=tools-backupdb&amp;action=backup",
  361. );
  362. $plugins->run_hooks("admin_tools_backupdb_start");
  363. $page->output_nav_tabs($sub_tabs, 'database_backup');
  364. $backups = array();
  365. $dir = MYBB_ADMIN_DIR.'backups/';
  366. $handle = opendir($dir);
  367. if($handle !== false)
  368. {
  369. while(($file = readdir($handle)) !== false)
  370. {
  371. if(filetype(MYBB_ADMIN_DIR.'backups/'.$file) == 'file')
  372. {
  373. $ext = get_extension($file);
  374. if($ext == 'gz' || $ext == 'sql')
  375. {
  376. $backups[@filemtime(MYBB_ADMIN_DIR.'backups/'.$file)] = array(
  377. "file" => $file,
  378. "time" => @filemtime(MYBB_ADMIN_DIR.'backups/'.$file),
  379. "type" => $ext
  380. );
  381. }
  382. }
  383. }
  384. closedir($handle);
  385. }
  386. $count = count($backups);
  387. krsort($backups);
  388. $table = new Table;
  389. $table->construct_header($lang->backup_filename);
  390. $table->construct_header($lang->file_size, array("class" => "align_center"));
  391. $table->construct_header($lang->creation_date);
  392. $table->construct_header($lang->controls, array("class" => "align_center"));
  393. foreach($backups as $backup)
  394. {
  395. $time = "-";
  396. if($backup['time'])
  397. {
  398. $time = my_date('relative', $backup['time']);
  399. }
  400. $table->construct_cell("<a href=\"index.php?module=tools-backupdb&amp;action=dlbackup&amp;file={$backup['file']}\">{$backup['file']}</a>");
  401. $table->construct_cell(get_friendly_size(filesize(MYBB_ADMIN_DIR.'backups/'.$backup['file'])), array("class" => "align_center"));
  402. $table->construct_cell($time);
  403. $table->construct_cell("<a href=\"index.php?module=tools-backupdb&amp;action=backup&amp;action=delete&amp;file={$backup['file']}&amp;my_post_key={$mybb->post_code}\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->confirm_backup_deletion}')\">{$lang->delete}</a>", array("class" => "align_center"));
  404. $table->construct_row();
  405. }
  406. if($count == 0)
  407. {
  408. $table->construct_cell($lang->no_backups, array('colspan' => 4));
  409. $table->construct_row();
  410. }
  411. $table->output($lang->existing_database_backups);
  412. $page->output_footer();
  413. }