web/wp-content/plugins/duplicator/inc/actions.php
changeset 195 c7c0fbc09788
child 204 09a1c134465b
equal deleted inserted replaced
194:32102edaa81b 195:c7c0fbc09788
       
     1 <?php
       
     2 /**
       
     3  *  DUPLICATOR_CREATE
       
     4  *  Creates the zip file, database entry, and installer file for the
       
     5  *  new culmination of a 'Package Set'
       
     6  *
       
     7  *  @return string   A message about the action
       
     8  *		- log:act__create=>done
       
     9  */
       
    10 function duplicator_create() {
       
    11 
       
    12 	global $wp_version;
       
    13 	global $wpdb;
       
    14 	global $current_user;
       
    15 	
       
    16 	$packname = isset($_POST['package_name']) ? trim($_POST['package_name']) : null;
       
    17 	
       
    18 	$secure_token  = uniqid() . mt_rand(1000, 9999);
       
    19 	$uniquename    = "{$secure_token}_{$packname}";
       
    20 	foreach(glob(DUPLICATOR_SSDIR_PATH . '/*.log') as $log_file){
       
    21 		@unlink($log_file);
       
    22 	}
       
    23 	
       
    24 	$logfilename = "{$uniquename}.log";
       
    25 	$GLOBALS['duplicator_package_log_handle'] = @fopen(DUPLICATOR_SSDIR_PATH . "/{$logfilename}", "c+");
       
    26 	
       
    27 	duplicator_log("*********************************************************");
       
    28 	duplicator_log("PACKAGE-LOG: ");
       
    29 	duplicator_log("*********************************************************");
       
    30 	duplicator_log("duplicator: " . DUPLICATOR_VERSION);
       
    31 	duplicator_log("wordpress: {$wp_version}");
       
    32 	duplicator_log("php: " .  phpversion());
       
    33 	duplicator_log("php sapi: " .  php_sapi_name());
       
    34 	duplicator_log("server: {$_SERVER['SERVER_SOFTWARE']}");
       
    35 	duplicator_log("browser: {$_SERVER['HTTP_USER_AGENT']}");
       
    36 	duplicator_log("package name: {$packname}");
       
    37 
       
    38 	if($packname) {
       
    39 		
       
    40 		$max_time   = @ini_set("max_execution_time", $GLOBALS['duplicator_opts']['max_time']); 
       
    41 		$max_memory = @ini_set('memory_limit', $GLOBALS['duplicator_opts']['max_memory']);
       
    42 		$max_time   = ($max_time === false)   ? "Unabled to set max_execution_time"  : "from={$max_time} to={$GLOBALS['duplicator_opts']['max_time']}";
       
    43 		$max_memory = ($max_memory === false) ? "Unabled to set memory_limit"		 : "from={$max_memory} to={$GLOBALS['duplicator_opts']['max_memory']}";
       
    44 		
       
    45 		@set_time_limit(0);
       
    46 		duplicator_log("max_time: {$max_time}");
       
    47 		duplicator_log("max_memory: {$max_memory}");
       
    48 
       
    49 		$zipfilename  = "{$uniquename}_package.zip";
       
    50 		$sqlfilename  = "{$uniquename}_database.sql";
       
    51 		$exefilename  = "{$uniquename}_installer.php";
       
    52 		
       
    53 		$zipfilepath  = DUPLICATOR_SSDIR_PATH . "/{$zipfilename}";
       
    54 		$sqlfilepath  = DUPLICATOR_SSDIR_PATH . "/{$sqlfilename}";
       
    55 		$exefilepath  = DUPLICATOR_SSDIR_PATH . "/{$exefilename}";
       
    56 		$zipsize 	  = 0;
       
    57 		
       
    58 		duplicator_log("mysql wait_timeout: {$GLOBALS['duplicator_opts']['max_time']}");
       
    59 		$wpdb->query("SET session wait_timeout = {$GLOBALS['duplicator_opts']['max_time']}");
       
    60 		
       
    61 
       
    62 		duplicator_log("*********************************************************");
       
    63 		duplicator_log("SQL SCRIPT");
       
    64 		duplicator_log("*********************************************************");
       
    65 		duplicator_create_dbscript($sqlfilepath);		
       
    66 		
       
    67 		
       
    68 		//CREATE ZIP ARCHIVE
       
    69 		duplicator_log("*********************************************************");
       
    70 		duplicator_log("ZIP ARCHIVE");
       
    71 		duplicator_log("*********************************************************");
       
    72 		
       
    73 		$zip = new Duplicator_Zip($zipfilepath, rtrim(DUPLICATOR_WPROOTPATH, '/'), $sqlfilepath);
       
    74 		$zipsize = filesize($zipfilepath);
       
    75 			
       
    76 		($zipsize == false) 
       
    77 			? duplicator_log("log:act__create=>warning: zipsize is unknown.")
       
    78 			: duplicator_log("log:act__create=>zip file size is: " . duplicator_bytesize($zipsize));
       
    79 	
       
    80 		duplicator_log("log:act__create=>zip archive complete.", 2);
       
    81 		
       
    82 		//Serlized settings
       
    83 		$settings = array('plugin_version' => DUPLICATOR_VERSION,
       
    84 						  'type' 		   => 'Manual');
       
    85 		$serialized_settings = serialize($settings);
       
    86 		
       
    87 		//Record archive info to database
       
    88 		 $results = $wpdb->insert($wpdb->prefix . "duplicator", 
       
    89 						array(
       
    90 						      'token'    => $secure_token, 
       
    91 							  'packname' => $packname, 
       
    92 							  'zipname'  => $zipfilename, 
       
    93 							  'zipsize'  => $zipsize, 
       
    94 							  'created'  => current_time('mysql', get_option('gmt_offset')),
       
    95 							  'owner'    => $current_user->user_login,
       
    96 							  'settings' => "{$serialized_settings}") 
       
    97 						);
       
    98 		if ($wpdb->insert_id) {
       
    99 			duplicator_log("log:act__create=>recorded archieve id: " . $wpdb->insert_id);
       
   100 		} else {
       
   101 			duplicator_log("log:act__create=>unable to record to database.");
       
   102 		}
       
   103 		$wpdb->flush();
       
   104 			
       
   105 	
       
   106 		//UPDATE INSTALL FILE
       
   107 		duplicator_log("*********************************************************");
       
   108 		duplicator_log("UPDATE INSTALLER FILE");
       
   109 		duplicator_log("*********************************************************");
       
   110 		duplicator_create_installerFile($uniquename);
       
   111 
       
   112 		//SEND EMAIL
       
   113 		//TODO: Send only SQL File via mail.  Zip files can get too large
       
   114 		if( $GLOBALS['duplicator_opts']['email-me'] == "1" ) {
       
   115 			duplicator_log("log:act__create=>email started");
       
   116 			$status      = ($zipsize) ? 'Success' : 'Failure';
       
   117 			$attachments = ""; //array(DUPLICATOR_SSDIR_PATH . '/' . $packname .'.zip');
       
   118 			$headers = 'From: Duplicator Plugin <no-reply@lifeinthegrid.com>' ."\r\n";
       
   119 			$subject = "Package '{$packname}' completed";
       
   120 			$message = "Run Status: {$status}\n\rSite Name: " . get_bloginfo('name') . "\n\rPackage Name: {$packname} \n\rCompleted at: " .current_time('mysql',get_option('gmt_offset'))  ;
       
   121 			wp_mail($current_user->user_email, $subject, $message, $headers, $attachments);
       
   122 			duplicator_log("log:act__create=>sent email to: {$current_user->user_email}");
       
   123 			$other_emails = @preg_split("/[,;]/",  $GLOBALS['duplicator_opts']['email_others']);
       
   124 			if (count($other_emails)) {
       
   125 				wp_mail($other_emails, $subject, $message, $headers, $attachments);
       
   126 				duplicator_log("log:act__create=>other emails sent: {$GLOBALS['duplicator_opts']['email_others']}");
       
   127 			}
       
   128 			duplicator_log("log:act__create=>email finished");
       
   129 		}
       
   130 
       
   131 	} 
       
   132 	
       
   133 	duplicator_log("*********************************************************");
       
   134 	duplicator_log("DONE PROCESSING => {$packname}");
       
   135 	duplicator_log("*********************************************************");
       
   136 	@fclose($GLOBALS['duplicator_package_log_handle']);
       
   137 	die();
       
   138 }
       
   139 
       
   140 
       
   141 /**
       
   142  *  DUPLICATOR_DELETE
       
   143  *  Deletes the zip file and database record entries for the
       
   144  *  selected ids.  Supports 1 to many deletes
       
   145  *
       
   146  *  @return string   A message about the action.  
       
   147  *		- see: duplicator_unlink
       
   148  */
       
   149 function duplicator_delete() {
       
   150 	try
       
   151 	{
       
   152 		$uniqueid = isset($_POST['duplicator_delid']) ? trim($_POST['duplicator_delid']) : null;
       
   153 		if ($uniqueid != null) {
       
   154 			$unique_list = explode(",", $uniqueid);
       
   155 			foreach ($unique_list as $id) {
       
   156 				$msg  = duplicator_unlink($id);
       
   157 			}
       
   158 		}
       
   159 		die($msg);
       
   160 	}  
       
   161 	catch(Exception $e) 
       
   162 	{
       
   163 		die("log:fun__delete=>runtime error: " . $e);
       
   164 	}
       
   165 }
       
   166 
       
   167 
       
   168 /**
       
   169  *  DUPLICATOR_SYSTEM_CHECK
       
   170  *  Check to see if the package already exsits or required files
       
   171  *  are installed.  Also check for package size
       
   172  *  
       
   173  *  @return string   A message about the action
       
   174  *		- log:act__system_check=>create new package
       
   175  *		- log:act__system_check=>overwrite
       
   176  */
       
   177 function duplicator_system_check() {
       
   178 	global $wpdb;
       
   179 	
       
   180 	@set_time_limit($GLOBALS['duplicator_opts']['max_time']);
       
   181 	duplicator_init_snapshotpath();
       
   182 	
       
   183 	$json = array();
       
   184 		
       
   185 	//SYS-100: FILE PERMS
       
   186 	$test = is_readable(DUPLICATOR_WPROOTPATH)
       
   187 			&& is_writeable(DUPLICATOR_SSDIR_PATH)
       
   188 			&& is_writeable(DUPLICATOR_PLUGIN_PATH . 'files/')
       
   189 			&& is_writeable(DUPLICATOR_PLUGIN_PATH . 'files/installer.rescue.php');
       
   190 	$json['SYS-100'] = ($test) ? 'Pass' : 'Fail';
       
   191 
       
   192 	//SYS-101 RESERVED FILE
       
   193 	$phpFile = file_exists(DUPLICATOR_WPROOTPATH . DUPLICATOR_INSTALL_PHP) ? DUPLICATOR_INSTALL_PHP : "";
       
   194 	$sqlFile = file_exists(DUPLICATOR_WPROOTPATH . DUPLICATOR_INSTALL_SQL) ? DUPLICATOR_INSTALL_SQL : "";
       
   195 	$logFile = file_exists(DUPLICATOR_WPROOTPATH . DUPLICATOR_INSTALL_LOG) ? DUPLICATOR_INSTALL_LOG : "";
       
   196 	$test = ! (strlen($phpFile) || strlen($sqlFile ) || strlen($logFile));
       
   197 	$json['SYS-101'] = ($test) ? 'Pass' : 'Fail';
       
   198 
       
   199 	//SYS-102 ZIP-ARCHIVE
       
   200 	$test = class_exists('ZipArchive');
       
   201 	$json['SYS-102'] = ($test) ? 'Pass' : 'Fail';
       
   202 	
       
   203 	//SYS-103 SAFE MODE
       
   204 	$test = ini_get('safe_mode');;
       
   205 	$json['SYS-103'] = ! ($test) ? 'Pass' : 'Fail';
       
   206 	
       
   207 	//SYS-104 MYSQLI SUPPORT
       
   208 	$test = function_exists('mysqli_connect');
       
   209 	$json['SYS-104'] = ($test) ? 'Pass' : 'Fail';
       
   210 
       
   211 	$result = in_array('Fail', $json);
       
   212 	$json['Success'] = ! $result;
       
   213 	
       
   214 	die(json_encode($json));
       
   215 }
       
   216 
       
   217 
       
   218 /**
       
   219  *  DUPLICATOR_SYSTEM_DIRECTORY
       
   220  *  Returns the directory size and file count for the root directory minus
       
   221  *  any of the filters
       
   222  *  
       
   223  *  @return json   size and file count of directory
       
   224  */
       
   225 function duplicator_system_directory() {
       
   226 
       
   227 	$json = array();
       
   228 	$dirInfo = duplicator_dirInfo(rtrim(duplicator_safe_path(DUPLICATOR_WPROOTPATH), '/'));
       
   229 	$dirSizeFormat   = duplicator_bytesize($dirInfo['size']) or "0";
       
   230 	$dirCountFormat  = number_format($dirInfo['count']) or "unknown";
       
   231 	$dirFolderFormat = number_format($dirInfo['folders']) or "unknown";
       
   232 
       
   233 	$json['size']    = $dirSizeFormat;
       
   234 	$json['count']   = $dirCountFormat;
       
   235 	$json['folders'] = $dirFolderFormat;
       
   236 	$json['flag'] = $dirInfo['flag'];
       
   237 
       
   238 	die(json_encode($json));
       
   239 }
       
   240 
       
   241 /**
       
   242  *  DUPLICATOR_UNLINK
       
   243  *  Removes the package
       
   244  *  
       
   245  *  @param string $file	 	The file name of the file to delete.
       
   246  *  @param string $path		The full path and file name of the file to delete
       
   247  *
       
   248  *  @return string   A message about the action
       
   249  *		- log:act__unlink=>removed
       
   250  *		- log:act__unlink=>file not found
       
   251  */
       
   252 function duplicator_unlink($uniqueid) {
       
   253 	try
       
   254 	{
       
   255 		$msg = "log:act__unlink=>file not found";
       
   256 		if($uniqueid) {
       
   257 			global $wpdb;
       
   258 			$table_name = $wpdb->prefix . 'duplicator';
       
   259 			if($wpdb->query("DELETE FROM {$table_name} WHERE zipname= '{$uniqueid}_package.zip'" ) != 0) {
       
   260 				$msg = "log:act__unlink=>removed";
       
   261 				try {
       
   262 					@chmod(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}_package.zip"), 0644);
       
   263 				    @chmod(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}_database.sql"), 0644);
       
   264 					@unlink(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}_package.zip"));
       
   265 					@unlink(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}_database.sql"));
       
   266 					@unlink(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}_installer.php"));
       
   267 				}
       
   268 				catch(Exception $e) {
       
   269 					error_log(var_dump($e->getMessage()));
       
   270 				}
       
   271 			//Check for Legacy pre 0.3.1
       
   272 			} else if($wpdb->query("DELETE FROM {$table_name} WHERE zipname= '{$uniqueid}'" ) != 0) {
       
   273 				try {
       
   274 					@chmod(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}"), 0644);
       
   275 					@unlink(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}"));
       
   276 				} catch(Exception $e) {
       
   277 					error_log(var_dump($e->getMessage()));
       
   278 				}
       
   279 			}
       
   280 		}
       
   281 		return $msg;
       
   282 	}  
       
   283 	catch(Exception $e) 
       
   284 	{
       
   285 		die("log:fun__unlink=>runtime error: " . $e);
       
   286 	}	
       
   287 }
       
   288 
       
   289 
       
   290 /**
       
   291  *  DUPLICATOR_SETTINGS
       
   292  *  Saves plugin settings
       
   293  *  
       
   294  *  @return string   A message about the action
       
   295  *		- log:act__settings=>saved
       
   296  */
       
   297 function duplicator_settings(){
       
   298 
       
   299 	//defaults
       
   300 	add_option('duplicator_options', '');
       
   301 	
       
   302 	$skip_ext = str_replace(array(' ', '.'), "", $_POST['skip_ext']);
       
   303 	$by_pass_array = explode(";", $_POST['dir_bypass']);
       
   304 	$by_pass_clean = "";
       
   305 	
       
   306 	foreach ($by_pass_array as $val) {
       
   307 		if (strlen($val) >= 2) {
       
   308 			$by_pass_clean .= duplicator_safe_path(trim(rtrim($val, "/\\"))) . ";";
       
   309 		}
       
   310 	}
       
   311 
       
   312 	if (is_numeric($_POST['max_memory'])) {
       
   313 		$maxmem = $_POST['max_memory'] < 256 ? 256 : $_POST['max_memory'];
       
   314 	} else {
       
   315 		$maxmem = 256;
       
   316 	}
       
   317 	
       
   318 	$duplicator_opts = array(
       
   319 		'dbhost'		=>$_POST['dbhost'],
       
   320 		'dbname'		=>$_POST['dbname'],
       
   321 		'dbuser'		=>$_POST['dbuser'],
       
   322 		'dbiconv'		=>$_POST['dbiconv'],
       
   323 		'nurl'			=>rtrim($_POST['nurl'], '/'),
       
   324 		'email-me'		=>$_POST['email-me'],
       
   325 		'email_others'	=>$_POST['email_others'],
       
   326 		'max_time'		=>$_POST['max_time'],
       
   327 		'max_memory'	=>preg_replace('/\D/', '', $maxmem) . 'M',
       
   328 		'skip_ext'		=>str_replace(",", ";", $skip_ext),
       
   329 		'dir_bypass'	=>$by_pass_clean,
       
   330 		'log_level'		=>$_POST['log_level'],
       
   331 		'rm_snapshot'	=>$_POST['rm_snapshot']);
       
   332 		
       
   333 
       
   334 	update_option('duplicator_options', serialize($duplicator_opts));
       
   335 	
       
   336 	die("log:act__settings=>saved");
       
   337 }
       
   338 //DO NOT ADD A CARRIAGE RETURN BEYOND THIS POINT (headers issue)!!
       
   339 ?>