PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/wp-db-backup/wp-db-backup.php

https://bitbucket.org/metrobee/tr.deconord.eu
PHP | 1463 lines | 1235 code | 110 blank | 118 comment | 185 complexity | 085a2a2e99a2451a5c1f5c7c11a1839d MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. Plugin Name: WordPress Database Backup
  4. Plugin URI: http://austinmatzko.com/wordpress-plugins/wp-db-backup/
  5. Description: On-demand backup of your WordPress database. Navigate to <a href="edit.php?page=wp-db-backup">Tools &rarr; Backup</a> to get started.
  6. Author: Austin Matzko
  7. Author URI: http://austinmatzko.com/
  8. Version: 2.2.3
  9. Copyright 2010 Austin Matzko (email : austin at pressedcode.com)
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
  21. */
  22. /**
  23. * Change WP_BACKUP_DIR if you want to
  24. * use a different backup location
  25. */
  26. if ( ! defined('ABSPATH') ) {
  27. die('Please do not load this file directly.');
  28. }
  29. $rand = substr( md5( md5( DB_PASSWORD ) ), -5 );
  30. global $wpdbb_content_dir, $wpdbb_content_url, $wpdbb_plugin_dir;
  31. $wpdbb_content_dir = ( defined('WP_CONTENT_DIR') ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content';
  32. $wpdbb_content_url = ( defined('WP_CONTENT_URL') ) ? WP_CONTENT_URL : get_option('siteurl') . '/wp-content';
  33. $wpdbb_plugin_dir = ( defined('WP_PLUGIN_DIR') ) ? WP_PLUGIN_DIR : $wpdbb_content_dir . '/plugins';
  34. if ( ! defined('WP_BACKUP_DIR') ) {
  35. define('WP_BACKUP_DIR', $wpdbb_content_dir . '/backup-' . $rand . '/');
  36. }
  37. if ( ! defined('WP_BACKUP_URL') ) {
  38. define('WP_BACKUP_URL', $wpdbb_content_url . '/backup-' . $rand . '/');
  39. }
  40. if ( ! defined('ROWS_PER_SEGMENT') ) {
  41. define('ROWS_PER_SEGMENT', 100);
  42. }
  43. /**
  44. * Set MOD_EVASIVE_OVERRIDE to true
  45. * and increase MOD_EVASIVE_DELAY
  46. * if the backup stops prematurely.
  47. */
  48. // define('MOD_EVASIVE_OVERRIDE', false);
  49. if ( ! defined('MOD_EVASIVE_DELAY') ) {
  50. define('MOD_EVASIVE_DELAY', '500');
  51. }
  52. class wpdbBackup {
  53. var $backup_complete = false;
  54. var $backup_file = '';
  55. var $backup_filename;
  56. var $core_table_names = array();
  57. var $errors = array();
  58. var $basename;
  59. var $page_url;
  60. var $referer_check_key;
  61. var $version = '2.1.5-alpha';
  62. function module_check() {
  63. $mod_evasive = false;
  64. if ( defined( 'MOD_EVASIVE_OVERRIDE' ) && true === MOD_EVASIVE_OVERRIDE ) return true;
  65. if ( ! defined( 'MOD_EVASIVE_OVERRIDE' ) || false === MOD_EVASIVE_OVERRIDE ) return false;
  66. if ( function_exists('apache_get_modules') )
  67. foreach( (array) apache_get_modules() as $mod )
  68. if ( false !== strpos($mod,'mod_evasive') || false !== strpos($mod,'mod_dosevasive') )
  69. return true;
  70. return false;
  71. }
  72. function wpdbBackup() {
  73. global $table_prefix, $wpdb;
  74. add_action('wp_ajax_save_backup_time', array(&$this, 'save_backup_time'));
  75. add_action('init', array(&$this, 'init_textdomain'));
  76. add_action('init', array(&$this, 'set_page_url'));
  77. add_action('load-update-core.php', array(&$this, 'update_notice_action'));
  78. add_action('wp_db_backup_cron', array(&$this, 'cron_backup'));
  79. add_action('wp_cron_daily', array(&$this, 'wp_cron_daily'));
  80. add_filter('cron_schedules', array(&$this, 'add_sched_options'));
  81. add_filter('wp_db_b_schedule_choices', array(&$this, 'schedule_choices'));
  82. $table_prefix = ( isset( $table_prefix ) ) ? $table_prefix : $wpdb->prefix;
  83. $datum = date("Ymd_B");
  84. $this->backup_filename = DB_NAME . "_$table_prefix$datum.sql";
  85. $possible_names = array(
  86. 'categories',
  87. 'commentmeta',
  88. 'comments',
  89. 'link2cat',
  90. 'linkcategories',
  91. 'links',
  92. 'options',
  93. 'post2cat',
  94. 'postmeta',
  95. 'posts',
  96. 'terms',
  97. 'term_taxonomy',
  98. 'term_relationships',
  99. 'users',
  100. 'usermeta',
  101. );
  102. foreach( $possible_names as $name ) {
  103. if ( isset( $wpdb->{$name} ) ) {
  104. $this->core_table_names[] = $wpdb->{$name};
  105. }
  106. }
  107. $this->backup_dir = trailingslashit(apply_filters('wp_db_b_backup_dir', WP_BACKUP_DIR));
  108. $this->basename = 'wp-db-backup';
  109. $this->referer_check_key = $this->basename . '-download_' . DB_NAME;
  110. if (isset($_POST['do_backup'])) {
  111. $this->wp_secure('fatal');
  112. check_admin_referer($this->referer_check_key);
  113. $this->can_user_backup('main');
  114. // save exclude prefs
  115. $exc_revisions = isset( $_POST['exclude-revisions'] ) ? (array) $_POST['exclude-revisions'] : array();
  116. $exc_spam = isset( $_POST['exclude-spam'] ) ? (array) $_POST['exclude-spam'] : array();
  117. update_option('wp_db_backup_excs', array('revisions' => $exc_revisions, 'spam' => $exc_spam));
  118. switch($_POST['do_backup']) {
  119. case 'backup':
  120. add_action('init', array(&$this, 'perform_backup'));
  121. break;
  122. case 'fragments':
  123. add_action('admin_menu', array(&$this, 'fragment_menu'));
  124. break;
  125. }
  126. } elseif (isset($_GET['fragment'] )) {
  127. $this->can_user_backup('frame');
  128. add_action('init', array(&$this, 'init'));
  129. } elseif (isset($_GET['backup'] )) {
  130. $this->can_user_backup();
  131. add_action('init', array(&$this, 'init'));
  132. } else {
  133. add_action('admin_menu', array(&$this, 'admin_menu'));
  134. }
  135. }
  136. function init() {
  137. $this->can_user_backup();
  138. if (isset($_GET['backup'])) {
  139. $via = isset($_GET['via']) ? $_GET['via'] : 'http';
  140. $this->backup_file = $_GET['backup'];
  141. $this->validate_file($this->backup_file);
  142. switch($via) {
  143. case 'smtp':
  144. case 'email':
  145. $success = $this->deliver_backup($this->backup_file, 'smtp', $_GET['recipient'], 'frame');
  146. $this->error_display( 'frame' );
  147. if ( $success ) {
  148. echo '
  149. <!-- ' . $via . ' -->
  150. <script type="text/javascript"><!--\\
  151. ';
  152. echo '
  153. alert("' . __('Backup Complete!','wp-db-backup') . '");
  154. window.onbeforeunload = null;
  155. </script>
  156. ';
  157. }
  158. break;
  159. default:
  160. $this->deliver_backup($this->backup_file, $via);
  161. $this->error_display( 'frame' );
  162. }
  163. die();
  164. }
  165. if (isset($_GET['fragment'] )) {
  166. list($table, $segment, $filename) = explode(':', $_GET['fragment']);
  167. $this->validate_file($filename);
  168. $this->backup_fragment($table, $segment, $filename);
  169. }
  170. die();
  171. }
  172. function init_textdomain() {
  173. load_plugin_textdomain('wp-db-backup', str_replace(ABSPATH, '', dirname(__FILE__)), dirname(plugin_basename(__FILE__)));
  174. }
  175. function set_page_url() {
  176. $query_args = array( 'page' => $this->basename );
  177. if ( function_exists('wp_create_nonce') )
  178. $query_args = array_merge( $query_args, array('_wpnonce' => wp_create_nonce($this->referer_check_key)) );
  179. $base = ( function_exists('site_url') ) ? site_url('', 'admin') : get_option('siteurl');
  180. $this->page_url = add_query_arg( $query_args, $base . '/wp-admin/edit.php');
  181. }
  182. /*
  183. * Add a link to back up your database when doing a core upgrade
  184. */
  185. function update_notice_action() {
  186. if ( 'upgrade-core' == $_REQUEST['action'] ) :
  187. ob_start(array(&$this, 'update_notice'));
  188. add_action('admin_footer', create_function('', 'ob_end_flush();'));
  189. endif;
  190. }
  191. function update_notice($text = '') {
  192. $pattern = '#(<a href\="' . __('http://codex.wordpress.org/WordPress_Backups') . '">.*?</p>)#';
  193. $replace = '$1' . "\n<p>" . sprintf(__('Click <a href="%s" target="_blank">here</a> to back up your database using the WordPress Database Backup plugin. <strong>Note:</strong> WordPress Database Backup does <em>not</em> back up your files, just your database.', 'wp-db-backup'), 'tools.php?page=wp-db-backup') . "</p>\n";
  194. $text = preg_replace($pattern, $replace, $text);
  195. return $text;
  196. }
  197. function build_backup_script() {
  198. global $table_prefix, $wpdb;
  199. echo "<div class='wrap'>";
  200. echo '<fieldset class="options"><legend>' . __('Progress','wp-db-backup') . '</legend>
  201. <p><strong>' .
  202. __('DO NOT DO THE FOLLOWING AS IT WILL CAUSE YOUR BACKUP TO FAIL:','wp-db-backup').
  203. '</strong></p>
  204. <ol>
  205. <li>'.__('Close this browser','wp-db-backup').'</li>
  206. <li>'.__('Reload this page','wp-db-backup').'</li>
  207. <li>'.__('Click the Stop or Back buttons in your browser','wp-db-backup').'</li>
  208. </ol>
  209. <p><strong>' . __('Progress:','wp-db-backup') . '</strong></p>
  210. <div id="meterbox" style="height:11px;width:80%;padding:3px;border:1px solid #659fff;"><div id="meter" style="color:#fff;height:11px;line-height:11px;background-color:#659fff;width:0%;text-align:center;font-size:6pt;">&nbsp;</div></div>
  211. <div id="progress_message"></div>
  212. <div id="errors"></div>
  213. </fieldset>
  214. <iframe id="backuploader" src="about:blank" style="visibility:hidden;border:none;height:1em;width:1px;"></iframe>
  215. <script type="text/javascript">
  216. //<![CDATA[
  217. window.onbeforeunload = function() {
  218. return "' . __('Navigating away from this page will cause your backup to fail.', 'wp-db-backup') . '";
  219. }
  220. function setMeter(pct) {
  221. var meter = document.getElementById("meter");
  222. meter.style.width = pct + "%";
  223. meter.innerHTML = Math.floor(pct) + "%";
  224. }
  225. function setProgress(str) {
  226. var progress = document.getElementById("progress_message");
  227. progress.innerHTML = str;
  228. }
  229. function addError(str) {
  230. var errors = document.getElementById("errors");
  231. errors.innerHTML = errors.innerHTML + str + "<br />";
  232. }
  233. function backup(table, segment) {
  234. var fram = document.getElementById("backuploader");
  235. fram.src = "' . $this->page_url . '&fragment=" + table + ":" + segment + ":' . $this->backup_filename . ':";
  236. }
  237. var curStep = 0;
  238. function nextStep() {
  239. backupStep(curStep);
  240. curStep++;
  241. }
  242. function finishBackup() {
  243. var fram = document.getElementById("backuploader");
  244. setMeter(100);
  245. ';
  246. $download_uri = add_query_arg('backup', $this->backup_filename, $this->page_url);
  247. switch($_POST['deliver']) {
  248. case 'http':
  249. echo '
  250. setProgress("' . __('Backup Complete!','wp-db-backup') . '");
  251. window.onbeforeunload = null;
  252. fram.src = "' . $download_uri . '";
  253. ';
  254. break;
  255. case 'smtp':
  256. if ( get_option('wpdb_backup_recip') != $_POST['backup_recipient'] ) {
  257. update_option('wpdb_backup_recip', $_POST['backup_recipient'] );
  258. }
  259. echo '
  260. setProgress("' . sprintf(__('Your backup has been emailed to %s','wp-db-backup'), $_POST['backup_recipient']) . '");
  261. window.onbeforeunload = null;
  262. fram.src = "' . $download_uri . '&via=email&recipient=' . $_POST['backup_recipient'] . '";
  263. ';
  264. break;
  265. default:
  266. echo '
  267. setProgress("' . __('Backup Complete!','wp-db-backup') . '");
  268. window.onbeforeunload = null;
  269. ';
  270. }
  271. echo '
  272. }
  273. function backupStep(step) {
  274. switch(step) {
  275. case 0: backup("", 0); break;
  276. ';
  277. $also_backup = array();
  278. if (isset($_POST['other_tables'])) {
  279. $also_backup = $_POST['other_tables'];
  280. } else {
  281. $also_backup = array();
  282. }
  283. $core_tables = $_POST['core_tables'];
  284. $tables = array_merge($core_tables, $also_backup);
  285. $step_count = 1;
  286. foreach ($tables as $table) {
  287. $rec_count = $wpdb->get_var("SELECT count(*) FROM {$table}");
  288. $rec_segments = ceil($rec_count / ROWS_PER_SEGMENT);
  289. $table_count = 0;
  290. if ( $this->module_check() ) {
  291. $delay = "setTimeout('";
  292. $delay_time = "', " . (int) MOD_EVASIVE_DELAY . ")";
  293. }
  294. else { $delay = $delay_time = ''; }
  295. do {
  296. echo "case {$step_count}: {$delay}backup(\"{$table}\", {$table_count}){$delay_time}; break;\n";
  297. $step_count++;
  298. $table_count++;
  299. } while($table_count < $rec_segments);
  300. echo "case {$step_count}: {$delay}backup(\"{$table}\", -1){$delay_time}; break;\n";
  301. $step_count++;
  302. }
  303. echo "case {$step_count}: finishBackup(); break;";
  304. echo '
  305. }
  306. if(step != 0) setMeter(100 * step / ' . $step_count . ');
  307. }
  308. nextStep();
  309. // ]]>
  310. </script>
  311. </div>
  312. ';
  313. $this->backup_menu();
  314. }
  315. function backup_fragment($table, $segment, $filename) {
  316. global $table_prefix, $wpdb;
  317. echo "$table:$segment:$filename";
  318. if($table == '') {
  319. $msg = __('Creating backup file...','wp-db-backup');
  320. } else {
  321. if($segment == -1) {
  322. $msg = sprintf(__('Finished backing up table \\"%s\\".','wp-db-backup'), $table);
  323. } else {
  324. $msg = sprintf(__('Backing up table \\"%s\\"...','wp-db-backup'), $table);
  325. }
  326. }
  327. if (is_writable($this->backup_dir)) {
  328. $this->fp = $this->open($this->backup_dir . $filename, 'a');
  329. if(!$this->fp) {
  330. $this->error(__('Could not open the backup file for writing!','wp-db-backup'));
  331. $this->error(array('loc' => 'frame', 'kind' => 'fatal', 'msg' => __('The backup file could not be saved. Please check the permissions for writing to your backup directory and try again.','wp-db-backup')));
  332. }
  333. else {
  334. if($table == '') {
  335. //Begin new backup of MySql
  336. $this->stow("# " . __('WordPress MySQL database backup','wp-db-backup') . "\n");
  337. $this->stow("#\n");
  338. $this->stow("# " . sprintf(__('Generated: %s','wp-db-backup'),date("l j. F Y H:i T")) . "\n");
  339. $this->stow("# " . sprintf(__('Hostname: %s','wp-db-backup'),DB_HOST) . "\n");
  340. $this->stow("# " . sprintf(__('Database: %s','wp-db-backup'),$this->backquote(DB_NAME)) . "\n");
  341. $this->stow("# --------------------------------------------------------\n");
  342. } else {
  343. if($segment == 0) {
  344. // Increase script execution time-limit to 15 min for every table.
  345. if ( !ini_get('safe_mode')) @set_time_limit(15*60);
  346. // Create the SQL statements
  347. $this->stow("# --------------------------------------------------------\n");
  348. $this->stow("# " . sprintf(__('Table: %s','wp-db-backup'),$this->backquote($table)) . "\n");
  349. $this->stow("# --------------------------------------------------------\n");
  350. }
  351. $this->backup_table($table, $segment);
  352. }
  353. }
  354. } else {
  355. $this->error(array('kind' => 'fatal', 'loc' => 'frame', 'msg' => __('The backup directory is not writeable! Please check the permissions for writing to your backup directory and try again.','wp-db-backup')));
  356. }
  357. if($this->fp) $this->close($this->fp);
  358. $this->error_display('frame');
  359. echo '<script type="text/javascript"><!--//
  360. var msg = "' . $msg . '";
  361. window.parent.setProgress(msg);
  362. window.parent.nextStep();
  363. //--></script>
  364. ';
  365. die();
  366. }
  367. function perform_backup() {
  368. // are we backing up any other tables?
  369. $also_backup = array();
  370. if (isset($_POST['other_tables']))
  371. $also_backup = $_POST['other_tables'];
  372. $core_tables = $_POST['core_tables'];
  373. $this->backup_file = $this->db_backup($core_tables, $also_backup);
  374. if (false !== $this->backup_file) {
  375. if ('smtp' == $_POST['deliver']) {
  376. $this->deliver_backup($this->backup_file, $_POST['deliver'], $_POST['backup_recipient'], 'main');
  377. if ( get_option('wpdb_backup_recip') != $_POST['backup_recipient'] ) {
  378. update_option('wpdb_backup_recip', $_POST['backup_recipient'] );
  379. }
  380. wp_redirect($this->page_url);
  381. } elseif ('http' == $_POST['deliver']) {
  382. $download_uri = add_query_arg('backup',$this->backup_file,$this->page_url);
  383. wp_redirect($download_uri);
  384. exit;
  385. }
  386. // we do this to say we're done.
  387. $this->backup_complete = true;
  388. }
  389. }
  390. function admin_header() {
  391. ?>
  392. <script type="text/javascript">
  393. //<![CDATA[
  394. if ( 'undefined' != typeof addLoadEvent ) {
  395. addLoadEvent(function() {
  396. var t = {'extra-tables-list':{name: 'other_tables[]'}, 'include-tables-list':{name: 'wp_cron_backup_tables[]'}};
  397. for ( var k in t ) {
  398. t[k].s = null;
  399. var d = document.getElementById(k);
  400. if ( ! d )
  401. continue;
  402. var ul = d.getElementsByTagName('ul').item(0);
  403. if ( ul ) {
  404. var lis = ul.getElementsByTagName('li');
  405. if ( 2 < lis.length ) {
  406. var text = document.createElement('p');
  407. text.className = 'instructions';
  408. text.innerHTML = '<?php _e('Click and hold down <code>[SHIFT]</code> to toggle multiple checkboxes', 'wp-db-backup'); ?>';
  409. ul.parentNode.insertBefore(text, ul);
  410. }
  411. }
  412. t[k].p = d.getElementsByTagName("input");
  413. for(var i=0; i < t[k].p.length; i++) {
  414. if(t[k].name == t[k].p[i].getAttribute('name')) {
  415. t[k].p[i].id = k + '-table-' + i;
  416. t[k].p[i].onkeyup = t[k].p[i].onclick = function(e) {
  417. e = e ? e : event;
  418. if ( 16 == e.keyCode )
  419. return;
  420. var match = /([\w-]*)-table-(\d*)/.exec(this.id);
  421. var listname = match[1];
  422. var that = match[2];
  423. if ( null === t[listname].s )
  424. t[listname].s = that;
  425. else if ( e.shiftKey ) {
  426. var start = Math.min(that, t[listname].s) + 1;
  427. var end = Math.max(that, t[listname].s);
  428. for( var j=start; j < end; j++)
  429. t[listname].p[j].checked = t[listname].p[j].checked ? false : true;
  430. t[listname].s = null;
  431. }
  432. }
  433. }
  434. }
  435. }
  436. <?php if ( function_exists('wp_schedule_event') ) : // needs to be at least WP 2.1 for ajax ?>
  437. if ( 'undefined' == typeof XMLHttpRequest )
  438. var xml = new ActiveXObject( navigator.userAgent.indexOf('MSIE 5') >= 0 ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP' );
  439. else
  440. var xml = new XMLHttpRequest();
  441. var initTimeChange = function() {
  442. var timeWrap = document.getElementById('backup-time-wrap');
  443. var backupTime = document.getElementById('next-backup-time');
  444. if ( !! timeWrap && !! backupTime && ( 1 == <?php
  445. echo (int) ( 'en' == strtolower( substr( get_locale(), 0, 2 ) ) );
  446. ?> ) ) {
  447. var span = document.createElement('span');
  448. span.className = 'submit';
  449. span.id = 'change-wrap';
  450. span.innerHTML = '<input type="submit" id="change-backup-time" name="change-backup-time" value="<?php _e('Change','wp-db-backup'); ?>" />';
  451. timeWrap.appendChild(span);
  452. backupTime.ondblclick = function(e) { span.parentNode.removeChild(span); clickTime(e, backupTime); };
  453. span.onclick = function(e) { span.parentNode.removeChild(span); clickTime(e, backupTime); };
  454. }
  455. }
  456. var clickTime = function(e, backupTime) {
  457. var tText = backupTime.innerHTML;
  458. backupTime.innerHTML = '<input type="text" value="' + tText + '" name="backup-time-text" id="backup-time-text" /> <span class="submit"><input type="submit" name="save-backup-time" id="save-backup-time" value="<?php _e('Save', 'wp-db-backup'); ?>" /></span>';
  459. backupTime.ondblclick = null;
  460. var mainText = document.getElementById('backup-time-text');
  461. mainText.focus();
  462. var saveTButton = document.getElementById('save-backup-time');
  463. if ( !! saveTButton )
  464. saveTButton.onclick = function(e) { saveTime(backupTime, mainText); return false; };
  465. if ( !! mainText )
  466. mainText.onkeydown = function(e) {
  467. e = e || window.event;
  468. if ( 13 == e.keyCode ) {
  469. saveTime(backupTime, mainText);
  470. return false;
  471. }
  472. }
  473. }
  474. var saveTime = function(backupTime, mainText) {
  475. var tVal = mainText.value;
  476. xml.open('POST', 'admin-ajax.php', true);
  477. xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  478. if ( xml.overrideMimeType )
  479. xml.setRequestHeader('Connection', 'close');
  480. xml.send('action=save_backup_time&_wpnonce=<?php echo wp_create_nonce($this->referer_check_key); ?>&backup-time='+tVal);
  481. xml.onreadystatechange = function() {
  482. if ( 4 == xml.readyState && '0' != xml.responseText ) {
  483. backupTime.innerHTML = xml.responseText;
  484. initTimeChange();
  485. }
  486. }
  487. }
  488. initTimeChange();
  489. <?php endif; // wp_schedule_event exists ?>
  490. });
  491. }
  492. //]]>
  493. </script>
  494. <style type="text/css">
  495. .wp-db-backup-updated {
  496. margin-top: 1em;
  497. }
  498. fieldset.options {
  499. border: 1px solid;
  500. margin-top: 1em;
  501. padding: 1em;
  502. -moz-border-radius: 8px;
  503. -khtml-border-radius: 8px;
  504. -webkit-border-top-left-radius: 8px;
  505. -webkit-border-top-right-radius: 8px;
  506. -webkit-border-bottom-left-radius: 8px;
  507. -webkit-border-bottom-right-radius: 8px;
  508. border-radius: 8px;
  509. }
  510. fieldset.options div.tables-list {
  511. float: left;
  512. padding: 1em;
  513. }
  514. fieldset.options input {
  515. }
  516. fieldset.options legend {
  517. font-size: larger;
  518. font-weight: bold;
  519. margin-bottom: .5em;
  520. padding: 1em;
  521. }
  522. fieldset.options .instructions {
  523. font-size: smaller;
  524. }
  525. fieldset.options ul {
  526. list-style-type: none;
  527. }
  528. fieldset.options li {
  529. text-align: left;
  530. }
  531. fieldset.options .submit {
  532. border-top: none;
  533. }
  534. </style>
  535. <?php
  536. }
  537. function admin_load() {
  538. add_action('admin_head', array(&$this, 'admin_header'));
  539. }
  540. function admin_menu() {
  541. $_page_hook = add_management_page(__('Backup','wp-db-backup'), __('Backup','wp-db-backup'), 'import', $this->basename, array(&$this, 'backup_menu'));
  542. add_action('load-' . $_page_hook, array(&$this, 'admin_load'));
  543. if ( function_exists('add_contextual_help') ) {
  544. $text = $this->help_menu();
  545. add_contextual_help($_page_hook, $text);
  546. }
  547. }
  548. function fragment_menu() {
  549. $page_hook = add_management_page(__('Backup','wp-db-backup'), __('Backup','wp-db-backup'), 'import', $this->basename, array(&$this, 'build_backup_script'));
  550. add_action('load-' . $page_hook, array(&$this, 'admin_load'));
  551. }
  552. /**
  553. * Add WP-DB-Backup-specific help options to the 2.7 =< WP contextual help menu
  554. * return string The text of the help menu.
  555. */
  556. function help_menu() {
  557. $text = "\n<a href=\"http://wordpress.org/extend/plugins/wp-db-backup/faq/\" target=\"_blank\">" . __('FAQ', 'wp-db-backup') . '</a>';
  558. $text .= "\n<br />\n<a href=\"http://www.ilfilosofo.com/forum/forum/2\" target=\"_blank\">" . __('WP-DB-Backup Support Forum', 'wp-db-backup') . '</a>';
  559. return $text;
  560. }
  561. function save_backup_time() {
  562. if ( $this->can_user_backup() ) {
  563. // try to get a time from the input string
  564. $time = strtotime(strval($_POST['backup-time']));
  565. if ( ! empty( $time ) && time() < $time ) {
  566. wp_clear_scheduled_hook( 'wp_db_backup_cron' ); // unschedule previous
  567. $scheds = (array) wp_get_schedules();
  568. $name = get_option('wp_cron_backup_schedule');
  569. if ( 0 != $time ) {
  570. wp_schedule_event($time, $name, 'wp_db_backup_cron');
  571. echo gmdate(get_option('date_format') . ' ' . get_option('time_format'), $time + (get_option('gmt_offset') * 3600));
  572. exit;
  573. }
  574. }
  575. } else {
  576. die(0);
  577. }
  578. }
  579. /**
  580. * Better addslashes for SQL queries.
  581. * Taken from phpMyAdmin.
  582. */
  583. function sql_addslashes($a_string = '', $is_like = false) {
  584. if ($is_like) $a_string = str_replace('\\', '\\\\\\\\', $a_string);
  585. else $a_string = str_replace('\\', '\\\\', $a_string);
  586. return str_replace('\'', '\\\'', $a_string);
  587. }
  588. /**
  589. * Add backquotes to tables and db-names in
  590. * SQL queries. Taken from phpMyAdmin.
  591. */
  592. function backquote($a_name) {
  593. if (!empty($a_name) && $a_name != '*') {
  594. if (is_array($a_name)) {
  595. $result = array();
  596. reset($a_name);
  597. while(list($key, $val) = each($a_name))
  598. $result[$key] = '`' . $val . '`';
  599. return $result;
  600. } else {
  601. return '`' . $a_name . '`';
  602. }
  603. } else {
  604. return $a_name;
  605. }
  606. }
  607. function open($filename = '', $mode = 'w') {
  608. if ('' == $filename) return false;
  609. $fp = @fopen($filename, $mode);
  610. return $fp;
  611. }
  612. function close($fp) {
  613. fclose($fp);
  614. }
  615. /**
  616. * Write to the backup file
  617. * @param string $query_line the line to write
  618. * @return null
  619. */
  620. function stow($query_line) {
  621. if(false === @fwrite($this->fp, $query_line))
  622. $this->error(__('There was an error writing a line to the backup script:','wp-db-backup') . ' ' . $query_line . ' ' . $php_errormsg);
  623. }
  624. /**
  625. * Logs any error messages
  626. * @param array $args
  627. * @return bool
  628. */
  629. function error($args = array()) {
  630. if ( is_string( $args ) )
  631. $args = array('msg' => $args);
  632. $args = array_merge( array('loc' => 'main', 'kind' => 'warn', 'msg' => ''), $args);
  633. $this->errors[$args['kind']][] = $args['msg'];
  634. if ( 'fatal' == $args['kind'] || 'frame' == $args['loc'])
  635. $this->error_display($args['loc']);
  636. return true;
  637. }
  638. /**
  639. * Displays error messages
  640. * @param array $errs
  641. * @param string $loc
  642. * @return string
  643. */
  644. function error_display($loc = 'main', $echo = true) {
  645. $errs = $this->errors;
  646. unset( $this->errors );
  647. if ( ! count($errs) ) return;
  648. $msg = '';
  649. $errs['fatal'] = isset( $errs['fatal'] ) ? (array) $errs['fatal'] : array();
  650. $errs['warn'] = isset( $errs['warn'] ) ? (array) $errs['warn'] : array();
  651. $err_list = array_slice( array_merge( $errs['fatal'], $errs['warn'] ), 0, 10);
  652. if ( 10 == count( $err_list ) )
  653. $err_list[9] = __('Subsequent errors have been omitted from this log.','wp-db-backup');
  654. $wrap = ( 'frame' == $loc ) ? "<script type=\"text/javascript\">\n var msgList = ''; \n %1\$s \n if ( msgList ) alert(msgList); \n </script>" : '%1$s';
  655. $line = ( 'frame' == $loc ) ?
  656. "try{ window.parent.addError('%1\$s'); } catch(e) { msgList += ' %1\$s';}\n" :
  657. "%1\$s<br />\n";
  658. foreach( (array) $err_list as $err )
  659. $msg .= sprintf($line,str_replace(array("\n","\r"), '', addslashes($err)));
  660. $msg = sprintf($wrap,$msg);
  661. if ( count($errs['fatal'] ) ) {
  662. if ( function_exists('wp_die') && 'frame' != $loc ) wp_die(stripslashes($msg));
  663. else die($msg);
  664. }
  665. else {
  666. if ( $echo ) echo $msg;
  667. else return $msg;
  668. }
  669. }
  670. /**
  671. * Taken partially from phpMyAdmin and partially from
  672. * Alain Wolf, Zurich - Switzerland
  673. * Website: http://restkultur.ch/personal/wolf/scripts/db_backup/
  674. * Modified by Scott Merrill (http://www.skippy.net/)
  675. * to use the WordPress $wpdb object
  676. * @param string $table
  677. * @param string $segment
  678. * @return void
  679. */
  680. function backup_table($table, $segment = 'none') {
  681. global $wpdb;
  682. $table_structure = $wpdb->get_results("DESCRIBE $table");
  683. if (! $table_structure) {
  684. $this->error(__('Error getting table details','wp-db-backup') . ": $table");
  685. return false;
  686. }
  687. if(($segment == 'none') || ($segment == 0)) {
  688. // Add SQL statement to drop existing table
  689. $this->stow("\n\n");
  690. $this->stow("#\n");
  691. $this->stow("# " . sprintf(__('Delete any existing table %s','wp-db-backup'),$this->backquote($table)) . "\n");
  692. $this->stow("#\n");
  693. $this->stow("\n");
  694. $this->stow("DROP TABLE IF EXISTS " . $this->backquote($table) . ";\n");
  695. // Table structure
  696. // Comment in SQL-file
  697. $this->stow("\n\n");
  698. $this->stow("#\n");
  699. $this->stow("# " . sprintf(__('Table structure of table %s','wp-db-backup'),$this->backquote($table)) . "\n");
  700. $this->stow("#\n");
  701. $this->stow("\n");
  702. $create_table = $wpdb->get_results("SHOW CREATE TABLE $table", ARRAY_N);
  703. if (false === $create_table) {
  704. $err_msg = sprintf(__('Error with SHOW CREATE TABLE for %s.','wp-db-backup'), $table);
  705. $this->error($err_msg);
  706. $this->stow("#\n# $err_msg\n#\n");
  707. }
  708. $this->stow($create_table[0][1] . ' ;');
  709. if (false === $table_structure) {
  710. $err_msg = sprintf(__('Error getting table structure of %s','wp-db-backup'), $table);
  711. $this->error($err_msg);
  712. $this->stow("#\n# $err_msg\n#\n");
  713. }
  714. // Comment in SQL-file
  715. $this->stow("\n\n");
  716. $this->stow("#\n");
  717. $this->stow('# ' . sprintf(__('Data contents of table %s','wp-db-backup'),$this->backquote($table)) . "\n");
  718. $this->stow("#\n");
  719. }
  720. if(($segment == 'none') || ($segment >= 0)) {
  721. $defs = array();
  722. $ints = array();
  723. foreach ($table_structure as $struct) {
  724. if ( (0 === strpos($struct->Type, 'tinyint')) ||
  725. (0 === strpos(strtolower($struct->Type), 'smallint')) ||
  726. (0 === strpos(strtolower($struct->Type), 'mediumint')) ||
  727. (0 === strpos(strtolower($struct->Type), 'int')) ||
  728. (0 === strpos(strtolower($struct->Type), 'bigint')) ) {
  729. $defs[strtolower($struct->Field)] = ( null === $struct->Default ) ? 'NULL' : $struct->Default;
  730. $ints[strtolower($struct->Field)] = "1";
  731. }
  732. }
  733. // Batch by $row_inc
  734. if($segment == 'none') {
  735. $row_start = 0;
  736. $row_inc = ROWS_PER_SEGMENT;
  737. } else {
  738. $row_start = $segment * ROWS_PER_SEGMENT;
  739. $row_inc = ROWS_PER_SEGMENT;
  740. }
  741. do {
  742. // don't include extra stuff, if so requested
  743. $excs = (array) get_option('wp_db_backup_excs');
  744. $where = '';
  745. if ( is_array($excs['spam'] ) && in_array($table, $excs['spam']) ) {
  746. $where = ' WHERE comment_approved != "spam"';
  747. } elseif ( is_array($excs['revisions'] ) && in_array($table, $excs['revisions']) ) {
  748. $where = ' WHERE post_type != "revision"';
  749. }
  750. if ( !ini_get('safe_mode')) @set_time_limit(15*60);
  751. $table_data = $wpdb->get_results("SELECT * FROM $table $where LIMIT {$row_start}, {$row_inc}", ARRAY_A);
  752. $entries = 'INSERT INTO ' . $this->backquote($table) . ' VALUES (';
  753. // \x08\\x09, not required
  754. $search = array("\x00", "\x0a", "\x0d", "\x1a");
  755. $replace = array('\0', '\n', '\r', '\Z');
  756. if($table_data) {
  757. foreach ($table_data as $row) {
  758. $values = array();
  759. foreach ($row as $key => $value) {
  760. if ($ints[strtolower($key)]) {
  761. // make sure there are no blank spots in the insert syntax,
  762. // yet try to avoid quotation marks around integers
  763. $value = ( null === $value || '' === $value) ? $defs[strtolower($key)] : $value;
  764. $values[] = ( '' === $value ) ? "''" : $value;
  765. } else {
  766. $values[] = "'" . str_replace($search, $replace, $this->sql_addslashes($value)) . "'";
  767. }
  768. }
  769. $this->stow(" \n" . $entries . implode(', ', $values) . ');');
  770. }
  771. $row_start += $row_inc;
  772. }
  773. } while((count($table_data) > 0) and ($segment=='none'));
  774. }
  775. if(($segment == 'none') || ($segment < 0)) {
  776. // Create footer/closing comment in SQL-file
  777. $this->stow("\n");
  778. $this->stow("#\n");
  779. $this->stow("# " . sprintf(__('End of data contents of table %s','wp-db-backup'),$this->backquote($table)) . "\n");
  780. $this->stow("# --------------------------------------------------------\n");
  781. $this->stow("\n");
  782. }
  783. } // end backup_table()
  784. function db_backup($core_tables, $other_tables) {
  785. global $table_prefix, $wpdb;
  786. if (is_writable($this->backup_dir)) {
  787. $this->fp = $this->open($this->backup_dir . $this->backup_filename);
  788. if(!$this->fp) {
  789. $this->error(__('Could not open the backup file for writing!','wp-db-backup'));
  790. return false;
  791. }
  792. } else {
  793. $this->error(__('The backup directory is not writeable!','wp-db-backup'));
  794. return false;
  795. }
  796. //Begin new backup of MySql
  797. $this->stow("# " . __('WordPress MySQL database backup','wp-db-backup') . "\n");
  798. $this->stow("#\n");
  799. $this->stow("# " . sprintf(__('Generated: %s','wp-db-backup'),date("l j. F Y H:i T")) . "\n");
  800. $this->stow("# " . sprintf(__('Hostname: %s','wp-db-backup'),DB_HOST) . "\n");
  801. $this->stow("# " . sprintf(__('Database: %s','wp-db-backup'),$this->backquote(DB_NAME)) . "\n");
  802. $this->stow("# --------------------------------------------------------\n");
  803. if ( (is_array($other_tables)) && (count($other_tables) > 0) )
  804. $tables = array_merge($core_tables, $other_tables);
  805. else
  806. $tables = $core_tables;
  807. foreach ($tables as $table) {
  808. // Increase script execution time-limit to 15 min for every table.
  809. if ( !ini_get('safe_mode')) @set_time_limit(15*60);
  810. // Create the SQL statements
  811. $this->stow("# --------------------------------------------------------\n");
  812. $this->stow("# " . sprintf(__('Table: %s','wp-db-backup'),$this->backquote($table)) . "\n");
  813. $this->stow("# --------------------------------------------------------\n");
  814. $this->backup_table($table);
  815. }
  816. $this->close($this->fp);
  817. if (count($this->errors)) {
  818. return false;
  819. } else {
  820. return $this->backup_filename;
  821. }
  822. } //wp_db_backup
  823. /**
  824. * Sends the backed-up file via email
  825. * @param string $to
  826. * @param string $subject
  827. * @param string $message
  828. * @return bool
  829. */
  830. function send_mail( $to, $subject, $message, $diskfile) {
  831. global $phpmailer;
  832. $filename = basename($diskfile);
  833. extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message' ) ) );
  834. if ( !is_object( $phpmailer ) || ( strtolower(get_class( $phpmailer )) != 'phpmailer' ) ) {
  835. if ( file_exists( ABSPATH . WPINC . '/class-phpmailer.php' ) )
  836. require_once ABSPATH . WPINC . '/class-phpmailer.php';
  837. if ( file_exists( ABSPATH . WPINC . '/class-smtp.php' ) )
  838. require_once ABSPATH . WPINC . '/class-smtp.php';
  839. if ( class_exists( 'PHPMailer') )
  840. $phpmailer = new PHPMailer();
  841. }
  842. // try to use phpmailer directly (WP 2.2+)
  843. if ( is_object( $phpmailer ) && ( strtolower(get_class( $phpmailer )) == 'phpmailer' ) ) {
  844. // Get the site domain and get rid of www.
  845. $sitename = strtolower( $_SERVER['SERVER_NAME'] );
  846. if ( substr( $sitename, 0, 4 ) == 'www.' ) {
  847. $sitename = substr( $sitename, 4 );
  848. }
  849. $from_email = 'wordpress@' . $sitename;
  850. $from_name = 'WordPress';
  851. // Empty out the values that may be set
  852. $phpmailer->ClearAddresses();
  853. $phpmailer->ClearAllRecipients();
  854. $phpmailer->ClearAttachments();
  855. $phpmailer->ClearBCCs();
  856. $phpmailer->ClearCCs();
  857. $phpmailer->ClearCustomHeaders();
  858. $phpmailer->ClearReplyTos();
  859. $phpmailer->AddAddress( $to );
  860. $phpmailer->AddAttachment($diskfile, $filename);
  861. $phpmailer->Body = $message;
  862. $phpmailer->CharSet = apply_filters( 'wp_mail_charset', get_bloginfo('charset') );
  863. $phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
  864. $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
  865. $phpmailer->IsMail();
  866. $phpmailer->Subject = $subject;
  867. do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
  868. $result = @$phpmailer->Send();
  869. // old-style: build the headers directly
  870. } else {
  871. $randomish = md5(time());
  872. $boundary = "==WPBACKUP-$randomish";
  873. $fp = fopen($diskfile,"rb");
  874. $file = fread($fp,filesize($diskfile));
  875. $this->close($fp);
  876. $data = chunk_split(base64_encode($file));
  877. $headers .= "MIME-Version: 1.0\n";
  878. $headers = 'From: wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])) . "\n";
  879. $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
  880. // Add a multipart boundary above the plain message
  881. $message = "This is a multi-part message in MIME format.\n\n" .
  882. "--{$boundary}\n" .
  883. "Content-Type: text/plain; charset=\"" . get_bloginfo('charset') . "\"\n" .
  884. "Content-Transfer-Encoding: 7bit\n\n" .
  885. $message . "\n\n";
  886. // Add file attachment to the message
  887. $message .= "--{$boundary}\n" .
  888. "Content-Type: application/octet-stream;\n" .
  889. " name=\"{$filename}\"\n" .
  890. "Content-Disposition: attachment;\n" .
  891. " filename=\"{$filename}\"\n" .
  892. "Content-Transfer-Encoding: base64\n\n" .
  893. $data . "\n\n" .
  894. "--{$boundary}--\n";
  895. $result = @wp_mail($to, $subject, $message, $headers);
  896. }
  897. return $result;
  898. }
  899. function deliver_backup($filename = '', $delivery = 'http', $recipient = '', $location = 'main') {
  900. if ('' == $filename) { return false; }
  901. $diskfile = $this->backup_dir . $filename;
  902. /**
  903. * Try to compress to gzip, if available
  904. */
  905. if ( function_exists('gzencode') ) {
  906. $gz_diskfile = "{$diskfile}.gz";
  907. if ( function_exists('file_get_contents') ) {
  908. $text = file_get_contents($diskfile);
  909. } else {
  910. $text = implode("", file($diskfile));
  911. }
  912. $gz_text = gzencode($text, 9);
  913. $fp = fopen($gz_diskfile, "w");
  914. fwrite($fp, $gz_text);
  915. if ( fclose($fp) ) {
  916. unlink($diskfile);
  917. $diskfile = $gz_diskfile;
  918. $filename = "{$filename}.gz";
  919. }
  920. }
  921. /*
  922. *
  923. */
  924. if ('http' == $delivery) {
  925. if (! file_exists($diskfile))
  926. $this->error(array('kind' => 'fatal', 'msg' => sprintf(__('File not found:%s','wp-db-backup'), "&nbsp;<strong>$filename</strong><br />") . '<br /><a href="' . $this->page_url . '">' . __('Return to Backup','wp-db-backup') . '</a>'));
  927. header('Content-Description: File Transfer');
  928. header('Content-Type: application/octet-stream');
  929. header('Content-Length: ' . filesize($diskfile));
  930. header("Content-Disposition: attachment; filename=$filename");
  931. $success = readfile($diskfile);
  932. unlink($diskfile);
  933. } elseif ('smtp' == $delivery) {
  934. if (! file_exists($diskfile)) {
  935. $msg = sprintf(__('File %s does not exist!','wp-db-backup'), $diskfile);
  936. $this->error($msg);
  937. return false;
  938. }
  939. if (! is_email($recipient)) {
  940. $recipient = get_option('admin_email');
  941. }
  942. $message = sprintf(__("Attached to this email is\n %1s\n Size:%2s kilobytes\n",'wp-db-backup'), $filename, round(filesize($diskfile)/1024));
  943. $success = $this->send_mail($recipient, get_bloginfo('name') . ' ' . __('Database Backup','wp-db-backup'), $message, $diskfile);
  944. if ( false === $success ) {
  945. $msg = __('The following errors were reported:','wp-db-backup') . "\n ";
  946. if ( function_exists('error_get_last') ) {
  947. $err = error_get_last();
  948. $msg .= $err['message'];
  949. } else {
  950. $msg .= __('ERROR: The mail application has failed to deliver the backup.','wp-db-backup');
  951. }
  952. $this->error(array('kind' => 'fatal', 'loc' => $location, 'msg' => $msg));
  953. } else {
  954. unlink($diskfile);
  955. }
  956. }
  957. return $success;
  958. }
  959. function backup_menu() {
  960. global $table_prefix, $wpdb;
  961. $feedback = '';
  962. $whoops = false;
  963. // did we just do a backup? If so, let's report the status
  964. if ( $this->backup_complete ) {
  965. $feedback = '<div class="updated wp-db-backup-updated"><p>' . __('Backup Successful','wp-db-backup') . '!';
  966. $file = $this->backup_file;
  967. switch($_POST['deliver']) {
  968. case 'http':
  969. $feedback .= '<br />' . sprintf(__('Your backup file: <a href="%1s">%2s</a> should begin downloading shortly.','wp-db-backup'), WP_BACKUP_URL . "{$this->backup_file}", $this->backup_file);
  970. break;
  971. case 'smtp':
  972. if (! is_email($_POST['backup_recipient'])) {
  973. $feedback .= get_option('admin_email');
  974. } else {
  975. $feedback .= $_POST['backup_recipient'];
  976. }
  977. $feedback = '<br />' . sprintf(__('Your backup has been emailed to %s','wp-db-backup'), $feedback);
  978. break;
  979. case 'none':
  980. $feedback .= '<br />' . __('Your backup file has been saved on the server. If you would like to download it now, right click and select "Save As"','wp-db-backup');
  981. $feedback .= ':<br /> <a href="' . WP_BACKUP_URL . "$file\">$file</a> : " . sprintf(__('%s bytes','wp-db-backup'), filesize($this->backup_dir . $file));
  982. }
  983. $feedback .= '</p></div>';
  984. }
  985. // security check
  986. $this->wp_secure();
  987. if (count($this->errors)) {
  988. $feedback .= '<div class="updated wp-db-backup-updated error"><p><strong>' . __('The following errors were reported:','wp-db-backup') . '</strong></p>';
  989. $feedback .= '<p>' . $this->error_display( 'main', false ) . '</p>';
  990. $feedback .= "</p></div>";
  991. }
  992. // did we just save options for wp-cron?
  993. if ( (function_exists('wp_schedule_event') || function_exists('wp_cron_init'))
  994. && isset($_POST['wp_cron_backup_options']) ) :
  995. do_action('wp_db_b_update_cron_options');
  996. if ( function_exists('wp_schedule_event') ) {
  997. wp_clear_scheduled_hook( 'wp_db_backup_cron' ); // unschedule previous
  998. $scheds = (array) wp_get_schedules();
  999. $name = strval($_POST['wp_cron_schedule']);
  1000. $interval = ( isset($scheds[$name]['interval']) ) ?
  1001. (int) $scheds[$name]['interval'] : 0;
  1002. update_option('wp_cron_backup_schedule', $name, false);
  1003. if ( 0 !== $interval ) {
  1004. wp_schedule_event(time() + $interval, $name, 'wp_db_backup_cron');
  1005. }
  1006. }
  1007. else {
  1008. update_option('wp_cron_backup_schedule', intval($_POST['cron_schedule']), false);
  1009. }
  1010. update_option('wp_cron_backup_tables', isset( $_POST['wp_cron_backup_tables'] ) ? $_POST['wp_cron_backup_tables'] : array() );
  1011. if (is_email($_POST['cron_backup_recipient'])) {
  1012. update_option('wp_cron_backup_recipient', $_POST['cron_backup_recipient'], false);
  1013. }
  1014. $feedback .= '<div class="updated wp-db-backup-updated"><p>' . __('Scheduled Backup Options Saved!','wp-db-backup') . '</p></div>';
  1015. endif;
  1016. $other_tables = array();
  1017. $also_backup = array();
  1018. // Get complete db table list
  1019. $all_tables = $wpdb->get_results("SHOW TABLES", ARRAY_N);
  1020. $all_tables = array_map(create_function('$a', 'return $a[0];'), $all_tables);
  1021. // Get list of WP tables that actually exist in this DB (for 1.6 compat!)
  1022. $wp_backup_default_tables = array_intersect($all_tables, $this->core_table_names);
  1023. // Get list of non-WP tables
  1024. $other_tables = array_diff($all_tables, $wp_backup_default_tables);
  1025. if ('' != $feedback)
  1026. echo $feedback;
  1027. if ( ! $this->wp_secure() )
  1028. return;
  1029. // Give the new dirs the same perms as wp-content.
  1030. // $stat = stat( ABSPATH . 'wp-content' );
  1031. // $dir_perms = $stat['mode'] & 0000777; // Get the permission bits.
  1032. $dir_perms = '0777';
  1033. // the file doesn't exist and can't create it
  1034. if ( ! file_exists($this->backup_dir) && ! @mkdir($this->backup_dir) ) {
  1035. ?><div class="updated wp-db-backup-updated error"><p><?php _e('WARNING: Your backup directory does <strong>NOT</strong> exist, and we cannot create it.','wp-db-backup'); ?></p>
  1036. <p><?php printf(__('Using your FTP client, try to create the backup directory yourself: %s', 'wp-db-backup'), '<code>' . $this->backup_dir . '</code>'); ?></p></div><?php
  1037. $whoops = true;
  1038. // not writable due to write permissions
  1039. } elseif ( !is_writable($this->backup_dir) && ! @chmod($this->backup_dir, $dir_perms) ) {
  1040. ?><div class="updated wp-db-backup-updated error"><p><?php _e('WARNING: Your backup directory is <strong>NOT</strong> writable! We cannot create the backup files.','wp-db-backup'); ?></p>
  1041. <p><?php printf(__('Using your FTP client, try to set the backup directory&rsquo;s write permission to %1$s or %2$s: %3$s', 'wp-db-backup'), '<code>777</code>', '<code>a+w</code>', '<code>' . $this->backup_dir . '</code>'); ?>
  1042. </p></div><?php
  1043. $whoops = true;
  1044. } else {
  1045. $this->fp = $this->open($this->backup_dir . 'test' );
  1046. if( $this->fp ) {
  1047. $this->close($this->fp);
  1048. @unlink($this->backup_dir . 'test' );
  1049. // the directory is not writable probably due to safe mode
  1050. } else {
  1051. ?><div class="updated wp-db-backup-updated error"><p><?php _e('WARNING: Your backup directory is <strong>NOT</strong> writable! We cannot create the backup files.','wp-db-backup'); ?></p><?php
  1052. if( ini_get('safe_mode') ){
  1053. ?><p><?php _e('This problem seems to be caused by your server&rsquo;s <code>safe_mode</code> file ownership restrictions, which limit what files web applications like WordPress can create.', 'wp-db-backup'); ?></p><?php
  1054. }
  1055. ?><?php printf(__('You can try to correct this problem by using your FTP client to delete and then re-create the backup directory: %s', 'wp-db-backup'), '<code>' . $this->backup_dir . '</code>');
  1056. ?></div><?php
  1057. $whoops = true;
  1058. }
  1059. }
  1060. if ( !file_exists($this->backup_dir . 'index.php') )
  1061. @ touch($this->backup_dir . 'index.php');
  1062. ?><div class='wrap'>
  1063. <h2><?php _e('Backup','wp-db-backup') ?></h2>
  1064. <form method="post" action="">
  1065. <?php if ( function_exists('wp_nonce_field') ) wp_nonce_field($this->referer_check_key); ?>
  1066. <fieldset class="options"><legend><?php _e('Tables','wp-db-backup') ?></legend>
  1067. <div class="tables-list core-tables alternate">
  1068. <h4><?php _e('These core WordPress tables will always be backed up:','wp-db-backup') ?></h4><ul><?php
  1069. $excs = (array) get_option('wp_db_backup_excs');
  1070. foreach ($wp_backup_default_tables as $table) {
  1071. if ( $table == $wpdb->comments ) {
  1072. $checked = ( isset($excs['spam']) && is_array($excs['spam'] ) && in_array($table, $excs['spam']) ) ? ' checked=\'checked\'' : '';
  1073. echo "<li><input type='hidden' name='core_tables[]' value='$table' /><code>$table</code> <span class='instructions'> <input type='checkbox' name='exclude-spam[]' value='$table' $checked /> " . __('Exclude spam comments', 'wp-db-backup') . '</span></li>';
  1074. } elseif ( function_exists('wp_get_post_revisions') && $table == $wpdb->posts ) {
  1075. $checked = ( isset($excs['revisions']) && is_array($excs['revisions'] ) && in_array($table, $excs['revisions']) ) ? ' checked=\'checked\'' : '';
  1076. echo "<li><input type='hidden' name='core_tables[]' value='$table' /><code>$table</code> <span class='instructions'> <input type='checkbox' name='exclude-revisions[]' value='$table' $checked /> " . __('Exclude post revisions', 'wp-db-backup') . '</span></li>';
  1077. } else {
  1078. echo "<li><input type='hidden' name='core_tables[]' value='$table' /><code>$table</code></li>";
  1079. }
  1080. }
  1081. ?></ul>
  1082. </div>
  1083. <div class="tables-list extra-tables" id="extra-tables-list">
  1084. <?php
  1085. if (count($other_tables) > 0) {
  1086. ?>
  1087. <h4><?php _e('You may choose to include any of the following tables:','wp-db-backup'); ?></h4>
  1088. <ul>
  1089. <?php
  1090. foreach ($other_tables as $table) {
  1091. ?>
  1092. <li><label><input type="checkbox" name="other_tables[]" value="<?php echo $table; ?>" /> <code><?php echo $table; ?></code></label>
  1093. <?php
  1094. }
  1095. ?></ul><?php
  1096. }
  1097. ?></div>
  1098. </fieldset>
  1099. <fieldset class="options">
  1100. <legend><?php _e('Backup Options','wp-db-backup'); ?></legend>
  1101. <p><?php _e('What to do with the backup file:','wp-db-backup'); ?></p>
  1102. <ul>
  1103. <li><label for="do_save">
  1104. <input type="radio" id="do_save" name="deliver" value="none" style="border:none;" />
  1105. <?php _e('Save to server','wp-db-backup');
  1106. echo " (<code>" . $this->backup_dir . "</code>)"; ?>
  1107. </label></li>
  1108. <li><label for="do_download">
  1109. <input type="radio" checked="checked" id="do_download" name="deliver" value="http" style="border:none;" />
  1110. <?php _e('Download to your computer','wp-db-backup'); ?>
  1111. </label></li>
  1112. <li><label for="do_email">
  1113. <input type="radio" name="deliver" id="do_email" value="smtp" style="border:none;" />
  1114. <?php _e('Email backup to:','wp-db-backup'); ?>
  1115. <input type="text" name="backup_recipient" size="20" value="<?php
  1116. $backup_recip = get_option('wpdb_backup_recip');
  1117. if ( empty( $backup_recip ) ) {
  1118. $backup_recip = get_option('admin_email');
  1119. }
  1120. echo $backup_recip; ?>" />
  1121. </label></li>
  1122. </ul>
  1123. <?php if ( ! $whoops ) : ?>
  1124. <input type="hidden" name="do_backup" id="do_backup" value="backup" />
  1125. <p class="submit">
  1126. <input type="submit" name="submit" onclick="document.getElementById('do_backup').value='fragments';" value="<?php _e('Backup now!','wp-db-backup'); ?>" />
  1127. </p>
  1128. <?php else : ?>
  1129. <div class="updated wp-db-backup-updated error"><p><?php _e('WARNING: Your backup directory is <strong>NOT</strong> writable!','wp-db-backup'); ?></p></div>
  1130. <?php endif; // ! whoops ?>
  1131. </fieldset>
  1132. <?php do_action('wp_db_b_backup_opts'); ?>
  1133. </form>
  1134. <?php
  1135. // this stuff only displays if some sort of wp-cron is available
  1136. $cron = ( function_exists('wp_schedule_event') ) ? true : false; // wp-cron in WP 2.1+
  1137. $cron_old = ( function_exists('wp_cron_init') && ! $cron ) ? true : false; // wp-cron plugin by Skippy
  1138. if ( $cron_old || $cron ) :
  1139. echo '<fieldset class="options"><legend>' . __('Scheduled Backup','wp-db-backup') . '</legend>';
  1140. $datetime = get_option('date_format') . ' ' . get_option('time_format');
  1141. if ( $cron ) :
  1142. $next_cron = wp_next_scheduled('wp_db_backup_cron');
  1143. if ( ! empty( $next_cron ) ) :
  1144. ?>
  1145. <p id="backup-time-wrap">
  1146. <?php printf(__('Next Backup: %s','wp-db-backup'), '<span id="next-backup-time">' . gmdate($datetime, $next_cron + (get_option('gmt_offset') * 3600)) . '</span>'); ?>
  1147. </p>
  1148. <?php
  1149. endif;
  1150. elseif ( $cron_old ) :
  1151. ?><p><?php printf(__('Last WP-Cron Daily Execution: %s','wp-db-backup'), gmdate($datetime, get_option('wp_cron_daily_lastrun') + (get_option('gmt_offset') * 3600))); ?><br /><?php
  1152. printf(__('Next WP-Cron Daily Execution: %s','wp-db-backup'), gmdate($datetime, (get_option('wp_cron_daily_lastrun') + (get_option('gmt_offset') * 3600) + 86400))); ?></p><?php
  1153. endif;
  1154. ?><form method="post" action="">
  1155. <?php if ( function_exists('wp_nonce_field') ) wp_nonce_field($this->referer_check_key); ?>
  1156. <div class="tables-list">
  1157. <h4><?php _e('Schedule: ','wp-db-backup'); ?></h4>
  1158. <?php
  1159. if ( $cron_old ) :
  1160. $wp_cron_backup_schedule = get_option('wp_cron_backup_schedule');
  1161. $schedule = array(0 => __('None','wp-db-backup'), 1 => __('Daily','wp-db-backup'));
  1162. foreach ($schedule as $value => $name) {
  1163. echo ' <input type="radio" style="border:none;" name="cron_schedule"';
  1164. if ($wp_cron_backup_schedule == $value) {
  1165. echo ' checked="checked" ';
  1166. }
  1167. echo 'value="' . $value . '" /> ' . $name;
  1168. }
  1169. elseif ( $cron ) :
  1170. echo apply_filters('wp_db_b_schedule_choices', wp_get_schedules() );
  1171. endif;
  1172. $cron_recipient = get_option('wp_cron_backup_recipient');
  1173. if (! is_email($cron_recipient)) {
  1174. $cron_recipient = get_option('admin_email');
  1175. }
  1176. $cron_recipient_input = '<p><label for="cron_backup_recipient">' . __('Email backup to:','wp-db-backup') . ' <input type="text" name="cron_backup_recipient" id="cron_backup_recipient" size="20" value="' . $cron_recipient . '" /></label></p>';
  1177. echo apply_filters('wp_db_b_cron_recipient_input', $cron_recipient_input);
  1178. echo '<p class="submit"><input type="submit" name="submit" value="' . __('Schedule backup','wp-db-backup') . '" /></p>';
  1179. echo '</div>';
  1180. $cron_tables = get_option('wp_cron_backup_tables');
  1181. if (! is_array($cron_tables)) {
  1182. $cron_tables = array();
  1183. }
  1184. if (count($other_tables) > 0) {
  1185. echo '<div class="tables-list alternate" id="include-tables-list">';
  1186. echo '<h4>' . __('Tables to include in the scheduled backup:','wp-db-backup') . '</h4><ul>';
  1187. foreach ($other_tables as $table) {
  1188. echo '<li><input type="checkbox" ';
  1189. if (in_array($table, $cron_tables)) {
  1190. echo 'checked="checked" ';
  1191. }
  1192. echo "name='wp_cron_backup_tables[]' value='{$table}' /> <code>{$table}</code></li>";
  1193. }
  1194. echo '</ul></div>';
  1195. }
  1196. echo '<input type="hidden" name=…

Large files files are truncated, but you can click here to view the full file