web/wp-content/plugins/duplicator/duplicator.php
changeset 195 c7c0fbc09788
child 204 09a1c134465b
equal deleted inserted replaced
194:32102edaa81b 195:c7c0fbc09788
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: Duplicator
       
     4 Plugin URI: http://www.lifeinthegrid.com/duplicator/
       
     5 Description: Create a full WordPress backup of your files and database with one click. Duplicate and move an entire site from one location to another in 3 easy steps. Create full snapshot of your site at any point in time.
       
     6 Version: 0.3.2
       
     7 Author: LifeInTheGrid
       
     8 Author URI: http://www.lifeinthegrid.com
       
     9 License: GPLv2 or later
       
    10 */
       
    11 
       
    12 /* ================================================================================ 
       
    13 Copyright 2011-2012  Cory Lamle 
       
    14 Copyright 2011  Gaurav Aggarwal  
       
    15 This program is free software; you can redistribute it and/or modify
       
    16 it under the terms of the GNU General Public License, version 2, as 
       
    17 published by the Free Software Foundation.
       
    18 
       
    19 This program is distributed in the hope that it will be useful,
       
    20 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    22 GNU General Public License for more details.
       
    23 
       
    24 You should have received a copy of the GNU General Public License
       
    25 along with this program; if not, write to the Free Software
       
    26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    27 	
       
    28 Contributors:
       
    29  Many thanks go out to Gaurav Aggarwal for starting the Backup and Move Plugin.
       
    30  This project is a fork of that project see backup-and-move for more details.		
       
    31 
       
    32 ================================================================================ */
       
    33 
       
    34 require_once("define.php");
       
    35 
       
    36 if (is_admin() == true) {
       
    37 	
       
    38 	$_tmpDuplicatorOptions = get_option('duplicator_options', false);
       
    39 	$GLOBALS['duplicator_opts'] = ($_tmpDuplicatorOptions == false) ? array() : @unserialize($_tmpDuplicatorOptions);
       
    40 	
       
    41 	//OPTIONS
       
    42 	$GLOBALS['duplicator_opts']['dbhost']		= isset($GLOBALS['duplicator_opts']['dbhost'])			? $GLOBALS['duplicator_opts']['dbhost']			: '';
       
    43 	$GLOBALS['duplicator_opts']['dbname']		= isset($GLOBALS['duplicator_opts']['dbname'])			? $GLOBALS['duplicator_opts']['dbname']			: '';
       
    44 	$GLOBALS['duplicator_opts']['dbuser']		= isset($GLOBALS['duplicator_opts']['dbuser'])			? $GLOBALS['duplicator_opts']['dbuser'] 		: '';
       
    45 	$GLOBALS['duplicator_opts']['dbiconv']		= isset($GLOBALS['duplicator_opts']['dbiconv'])			? $GLOBALS['duplicator_opts']['dbiconv']		: '1';
       
    46 	$GLOBALS['duplicator_opts']['dbadd_drop']	= isset($GLOBALS['duplicator_opts']['dbadd_drop'])		? $GLOBALS['duplicator_opts']['dbadd_drop']     : '0';
       
    47 	$GLOBALS['duplicator_opts']['nurl']  		= isset($GLOBALS['duplicator_opts']['nurl'] ) 			? $GLOBALS['duplicator_opts']['nurl']  			: '';
       
    48 	$GLOBALS['duplicator_opts']['email-me']		= isset($GLOBALS['duplicator_opts']['email-me'])		? $GLOBALS['duplicator_opts']['email-me']		: '0';
       
    49 	$GLOBALS['duplicator_opts']['log_level'] 	= isset($GLOBALS['duplicator_opts']['log_level'])		? $GLOBALS['duplicator_opts']['log_level']  	: '0';
       
    50 	$GLOBALS['duplicator_opts']['max_time']		= is_numeric($GLOBALS['duplicator_opts']['max_time'])	? $GLOBALS['duplicator_opts']['max_time']   	: 1000;
       
    51 	$GLOBALS['duplicator_opts']['max_memory']	= isset($GLOBALS['duplicator_opts']['max_memory'])  	? $GLOBALS['duplicator_opts']['max_memory'] 	: '1000M';
       
    52 	$GLOBALS['duplicator_opts']['email_others']	= isset($GLOBALS['duplicator_opts']['email_others']) 	? $GLOBALS['duplicator_opts']['email_others']	: '';
       
    53 	$GLOBALS['duplicator_opts']['skip_ext']		= isset($GLOBALS['duplicator_opts']['skip_ext'])  		? $GLOBALS['duplicator_opts']['skip_ext'] 		: '';
       
    54 	$GLOBALS['duplicator_opts']['dir_bypass']	= isset($GLOBALS['duplicator_opts']['dir_bypass'])		? $GLOBALS['duplicator_opts']['dir_bypass']		: '';
       
    55 	$GLOBALS['duplicator_opts']['rm_snapshot']  = isset($GLOBALS['duplicator_opts']['rm_snapshot']) 	? $GLOBALS['duplicator_opts']['rm_snapshot'] 	: '1';
       
    56 	
       
    57 	//Default Arrays
       
    58 	$GLOBALS['duplicator_bypass-array']	  = explode(";", $GLOBALS['duplicator_opts']['dir_bypass'], -1);
       
    59 	$GLOBALS['duplicator_bypass-array']   = count($GLOBALS['duplicator_bypass-array']) 			  ? $GLOBALS['duplicator_bypass-array'] 			      : null;
       
    60 	$GLOBALS['duplicator_skip_ext-array'] = explode(";", $GLOBALS['duplicator_opts']['skip_ext']) ? explode(";", $GLOBALS['duplicator_opts']['skip_ext']) : array();
       
    61 
       
    62 	require_once 'inc/functions.php';
       
    63 	require_once 'inc/class.zip.php';
       
    64 	require_once 'inc/actions.php';
       
    65 	
       
    66 	/* ACTIVATION 
       
    67 	Only called when plugin is activated */
       
    68 	function duplicator_activate() {
       
    69 
       
    70 		global $wpdb;
       
    71 		$table_name = $wpdb->prefix . "duplicator";
       
    72 		
       
    73 		//PRIMARY KEY must have 2 spaces before for dbDelta
       
    74 		$sql = "CREATE TABLE `{$table_name}` (
       
    75 		 id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT  PRIMARY KEY,
       
    76 		 token 	 	 VARCHAR(25) NOT NULL, 
       
    77 		 packname 	 VARCHAR(250) NOT NULL, 
       
    78 		 zipname 	 VARCHAR(250) NOT NULL, 
       
    79 		 zipsize 	 INT (11),
       
    80 		 created 	 DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
       
    81 		 owner 		 VARCHAR(60) NOT NULL,
       
    82 		 settings 	 LONGTEXT NOT NULL)" ;
       
    83 
       
    84 		require_once(DUPLICATOR_WPROOTPATH . 'wp-admin/includes/upgrade.php');
       
    85 		@dbDelta($sql);
       
    86 		
       
    87 		$duplicator_opts = array(
       
    88 			'dbhost'		=>'localhost',
       
    89 			'dbname'		=>'',
       
    90 			'dbuser'		=>'',
       
    91 			'nurl'			=>'',
       
    92 			'email-me'		=>"{$GLOBALS['duplicator_opts']['email-me']}",
       
    93 			'email_others'	=>"{$GLOBALS['duplicator_opts']['email_others']}",
       
    94 			'max_time'		=>$GLOBALS['duplicator_opts']['max_time'],
       
    95 			'max_memory'	=>$GLOBALS['duplicator_opts']['max_memory'],
       
    96 			'dir_bypass'	=>"{$GLOBALS['duplicator_opts']['dir_bypass']}",
       
    97 			'log_level'		=>'0',
       
    98 			'dbiconv'		=>"{$GLOBALS['duplicator_opts']['dbiconv']}",
       
    99 			'skip_ext'		=>"{$GLOBALS['duplicator_opts']['skip_ext']}",
       
   100 			'rm_snapshot'	=>"{$GLOBALS['duplicator_opts']['rm_snapshot']}");
       
   101 				
       
   102 		update_option('duplicator_version_plugin', 	DUPLICATOR_VERSION);
       
   103 		update_option('duplicator_options', serialize($duplicator_opts));
       
   104 
       
   105 		//CLEANUP LEGACY
       
   106 		//PRE 0.2.9
       
   107 		delete_option('duplicator_version_database');
       
   108 		$wpdb->query("ALTER TABLE `{$table_name}` CHANGE bid id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT");
       
   109 		$wpdb->query("ALTER TABLE `{$table_name}` DROP COLUMN ver_db");
       
   110 		$wpdb->query("ALTER TABLE `{$table_name}` DROP COLUMN ver_plug");
       
   111 		$wpdb->query("ALTER TABLE `{$table_name}` DROP COLUMN status");
       
   112 		$wpdb->query("ALTER TABLE `{$table_name}` DROP COLUMN type");	
       
   113 		
       
   114 		//Setup All Directories
       
   115 		duplicator_init_snapshotpath();
       
   116 	}
       
   117 	
       
   118 	/* UPDATE 
       
   119 	register_activation_hook is not called when a plugin is updated
       
   120 	so we need to use the following function */
       
   121 	function duplicator_update() {
       
   122 		if (DUPLICATOR_VERSION != get_option("duplicator_version_plugin")) {
       
   123 			duplicator_activate();
       
   124 		}
       
   125 	}
       
   126 
       
   127 
       
   128 	/* DEACTIVATION 
       
   129 	Only called when plugin is deactivated */
       
   130 	function duplicator_deactivate() {
       
   131 		//No actions needed yet
       
   132 	}
       
   133 	
       
   134 	
       
   135 	/* UNINSTALL 
       
   136 	Uninstall all duplicator logic */
       
   137 	function duplicator_uninstall() {
       
   138 		global $wpdb;
       
   139 		$table_name = $wpdb->prefix . "duplicator";
       
   140 		$wpdb->query("DROP TABLE `{$table_name}`");
       
   141 	
       
   142 		delete_option('duplicator_version_plugin');
       
   143 		delete_option('duplicator_options');
       
   144 		
       
   145 		if ($GLOBALS['duplicator_opts']['rm_snapshot']) {
       
   146 		
       
   147 			$ssdir = duplicator_safe_path(DUPLICATOR_SSDIR_PATH);
       
   148 		
       
   149 			//Sanity check for strange setup
       
   150 			$check = glob("{$ssdir}/wp-config.php");
       
   151 			if (count($check) == 0) {
       
   152 
       
   153 				//PHP is sometimes flaky so lets do a sanity check
       
   154 				foreach (glob("{$ssdir}/*_database.sql")  as $file) { if (strstr($file, '_database.sql'))  {@unlink("{$file}");} }
       
   155 				foreach (glob("{$ssdir}/*_installer.php") as $file) { if (strstr($file, '_installer.php')) {@unlink("{$file}");} }
       
   156 				foreach (glob("{$ssdir}/*_package.zip")   as $file) { if (strstr($file, '_package.zip'))   {@unlink("{$file}");} }
       
   157 				foreach (glob("{$ssdir}/*.log") as $file) 			{ if (strstr($file, '.log'))   		   {@unlink("{$file}");} }
       
   158 				
       
   159 				//Check for core files and only continue removing data if the snapshots directory
       
   160 				//has not been edited by 3rd party sources, this helps to keep the system stable
       
   161 				$files = glob("{$ssdir}/*");
       
   162 				if(is_array($files) && count($files) == 3)
       
   163 				{
       
   164 					$defaults = array("{$ssdir}/index.php", "{$ssdir}/robots.txt", "{$ssdir}/dtoken.php");
       
   165 					$compare = array_diff($defaults, $files);
       
   166 					
       
   167 					if (count($compare) == 0) {
       
   168 						foreach($defaults as $file) {@unlink("{$file}");}
       
   169 						@unlink("{$ssdir}/.htaccess");
       
   170 						@rmdir($ssdir);
       
   171 					}
       
   172 				//No packages have ever been created
       
   173 				} else if(is_array($files) && count($files) == 1) {
       
   174 					$defaults = array("{$ssdir}/index.php");
       
   175 					$compare  = array_diff($defaults, $files);
       
   176 					if (count($compare) == 0) {
       
   177 						@unlink("{$ssdir}/index.php");
       
   178 						@rmdir($ssdir);
       
   179 					}
       
   180 				} 
       
   181 			
       
   182 			}
       
   183 		}
       
   184 	}
       
   185 	
       
   186 	
       
   187 	/* META LINK ADDONS
       
   188 	Adds links to the plugins manager page */
       
   189 	function duplicator_meta_links( $links, $file ) {
       
   190 		$plugin = plugin_basename(__FILE__);
       
   191 		// create link
       
   192 		if ( $file == $plugin ) {
       
   193 			$links[] = '<a href="' . DUPLICATOR_HELPLINK . '" title="' . __( 'FAQ', 'wpduplicator' ) . '" target="_blank">' . __( 'FAQ', 'wpduplicator' ) . '</a>';
       
   194 			$links[] = '<a href="' . DUPLICATOR_GIVELINK . '" title="' . __( 'Partner', 'wpduplicator' )  . '" target="_blank">' . __( 'Partner', 'wpduplicator' )  . '</a>';
       
   195 			$links[] = '<a href="' . DUPLICATOR_CERTIFIED .'" title="' . __( 'Approved Hosts', 'wpduplicator'  ) . '"  target="_blank">' . __( 'Approved Hosts', 'wpduplicator'  ) . '</a>';
       
   196 			return $links;
       
   197 		}
       
   198 		return $links;
       
   199 	}
       
   200 	
       
   201 	//HOOKS & ACTIONS
       
   202 	load_plugin_textdomain('wpduplicator' , FALSE, basename( dirname( __FILE__ ) ) . '/lang/' );
       
   203 	register_activation_hook(__FILE__ ,	    'duplicator_activate');
       
   204 	register_deactivation_hook(__FILE__ ,	'duplicator_deactivate');
       
   205 	register_uninstall_hook(__FILE__ , 		'duplicator_uninstall');
       
   206 	
       
   207 	add_action('plugins_loaded', 						'duplicator_update');
       
   208 	add_action('admin_init', 							'duplicator_init' );
       
   209 	add_action('admin_menu', 							'duplicator_menu');
       
   210 	add_action('wp_ajax_duplicator_system_check',		'duplicator_system_check');
       
   211 	add_action('wp_ajax_duplicator_system_directory',	'duplicator_system_directory');
       
   212 	add_action('wp_ajax_duplicator_delete',				'duplicator_delete');
       
   213 	add_action('wp_ajax_duplicator_create',				'duplicator_create');
       
   214 	add_action('wp_ajax_duplicator_settings',			'duplicator_settings');
       
   215 	add_filter('plugin_action_links', 					'duplicator_manage_link', 10, 2 );
       
   216 	add_filter('plugin_row_meta', 						'duplicator_meta_links', 10, 2 );
       
   217 
       
   218 
       
   219 	/**
       
   220 	 *  DUPLICATOR_INIT
       
   221 	 *  Init routines  */
       
   222 	function duplicator_init() {
       
   223 	   /* Register our stylesheet. */
       
   224 	   wp_register_style('jquery-ui', 		  DUPLICATOR_PLUGIN_URL . 'css/jquery-ui.css', null , "1.8.21" );
       
   225 	   wp_register_style('duplicator_style',  DUPLICATOR_PLUGIN_URL . 'css/style.css' );
       
   226 	}
       
   227 
       
   228 	/**
       
   229 	 *  DUPLICATOR_VIEWS
       
   230 	 *  Inlcude all visual elements  */
       
   231 	function duplicator_main_page() {include 'inc/page.main.php';}
       
   232 	//Diagnostics Page
       
   233 	function duplicator_diag_page() {include 'inc/page.diag.php';}
       
   234 	//Support Page
       
   235 	function duplicator_support_page() {include 'inc/page.support.php';}
       
   236 	//All About Page
       
   237 	function duplicator_about_page() {include 'inc/page.about.php';}
       
   238 	
       
   239 	/**
       
   240 	 *  DUPLICATOR_MENU
       
   241 	 *  Loads the menu item into the WP tools section and queues the actions for only this plugin */
       
   242 	function duplicator_menu() {	
       
   243 		//Main Menu
       
   244 		$page_main = add_menu_page('Duplicator', 'Duplicator', "import", basename(__FILE__), 'duplicator_main_page', plugins_url('duplicator/img/create.png'));
       
   245 		add_submenu_page(basename(__FILE__), __('Dashboard', 'wpduplicator'),  __('Dashboard', 'wpduplicator'), "import" , basename(__FILE__), 'duplicator_main_page');
       
   246 		//Sub Menus
       
   247 		$page_diag  	= add_submenu_page(basename(__FILE__), __('Diagnostics', 'wpduplicator'), __('Diagnostics', 'wpduplicator'), 'import', 'duplicator_diag_page', 'duplicator_diag_page');
       
   248 		$page_support 	= add_submenu_page(basename(__FILE__), __('Support', 'wpduplicator'), __('Support', 'wpduplicator'), 'import', 'duplicator_support_page', 'duplicator_support_page');
       
   249 		$page_about 	= add_submenu_page(basename(__FILE__), __('All About', 'wpduplicator'), __('All About', 'wpduplicator'), 'import', 'duplicator_about_page', 'duplicator_about_page');
       
   250 		
       
   251 
       
   252 		//Apply scripts and styles
       
   253 		add_action('admin_print_scripts-' . $page_main, 'duplicator_scripts');
       
   254 		add_action('admin_print_styles-'  . $page_main, 'duplicator_styles' );
       
   255 		add_action('admin_print_styles-'  . $page_diag, 'duplicator_styles' );
       
   256 		add_action('admin_print_styles-'  . $page_about, 'duplicator_styles' );
       
   257 		add_action('admin_print_styles-'  . $page_support, 'duplicator_styles' );
       
   258 	}
       
   259 
       
   260 	/**
       
   261 	 *  DUPLICATOR_SCRIPTS
       
   262 	 *  Loads the required javascript libs only for this plugin  */
       
   263 	function duplicator_scripts() {
       
   264 		wp_enqueue_script("jquery-ui", DUPLICATOR_PLUGIN_URL . "js/jquery-ui.min.js", array( 'jquery' ), "1.8.21");
       
   265 	}
       
   266 
       
   267 	/**
       
   268 	 *  DUPLICATOR_STYLES
       
   269 	 *  Loads the required css links only for this plugin  */
       
   270 	function duplicator_styles() {
       
   271 	   wp_enqueue_style('jquery-ui');
       
   272 	   wp_enqueue_style('duplicator_style');
       
   273 	}
       
   274 	
       
   275 	/**
       
   276 	 *  DUPLICATOR_MANAGE_LINK
       
   277 	 *  Adds the manage link in the plugins list */
       
   278 	function duplicator_manage_link($links, $file) {
       
   279 		static $this_plugin;
       
   280 		if (!$this_plugin) $this_plugin = plugin_basename(__FILE__);
       
   281 		 
       
   282 		if ($file == $this_plugin){
       
   283 			$settings_link = '<a href="admin.php?page=duplicator.php">'. __("Manage", 'wpduplicator') .'</a>';
       
   284 		    array_unshift($links, $settings_link);
       
   285 		}
       
   286 		return $links;
       
   287 	}
       
   288 	
       
   289 	//Use WordPress Debugging log file. file is written to wp-content/debug.log
       
   290 	//trace with tail command to see real-time issues.
       
   291 	if(!function_exists('duplicator_debug')){
       
   292 		function duplicator_debug( $message ) {
       
   293 			if( WP_DEBUG === true ){
       
   294 			  if( is_array( $message ) || is_object( $message ) ){
       
   295 				error_log( print_r( $message, true ) );
       
   296 			  } else {
       
   297 				error_log( $message );
       
   298 			  }
       
   299 			}
       
   300 		}
       
   301 	}
       
   302 
       
   303 }
       
   304 ?>