GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch dev (76c677)
by Liuta
01:52
created
restore/xcloner_restore.php 1 patch
Spacing   +194 added lines, -194 removed lines patch added patch discarded remove patch
@@ -1,30 +1,30 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-if(!defined('AUTH_KEY'))
3
+if (!defined('AUTH_KEY'))
4 4
 {
5 5
 	define('AUTH_KEY', '');
6 6
 }
7 7
 
8
-if(!defined("DS"))
8
+if (!defined("DS"))
9 9
 {
10 10
 	define("DS", DIRECTORY_SEPARATOR);
11 11
 }
12 12
 
13
-if(!defined('XCLONER_PLUGIN_ACCESS') || XCLONER_PLUGIN_ACCESS != 1)
13
+if (!defined('XCLONER_PLUGIN_ACCESS') || XCLONER_PLUGIN_ACCESS != 1)
14 14
 {	
15
-	if(!AUTH_KEY)
15
+	if (!AUTH_KEY)
16 16
 	{
17 17
 			Xcloner_Restore::send_response("404", "Could not run restore script, AUTH_KEY not set!");
18 18
 			exit;
19 19
 	}
20 20
 	
21
-	if(!isset($_REQUEST['hash']))
21
+	if (!isset($_REQUEST['hash']))
22 22
 	{
23 23
 			Xcloner_Restore::send_response("404", "Could not run restore script, sent HASH is empty!");
24 24
 			exit;
25 25
 	}
26 26
 	
27
-	if($_REQUEST['hash'] != AUTH_KEY)
27
+	if ($_REQUEST['hash'] != AUTH_KEY)
28 28
 	{
29 29
 			Xcloner_Restore::send_response("404", "Could not run restore script, AUTH_KEY doesn't match the sent HASH!");
30 30
 			exit;
@@ -32,28 +32,28 @@  discard block
 block discarded – undo
32 32
 }
33 33
 
34 34
 //check minimum PHP version
35
-if(version_compare(phpversion(), Xcloner_Restore::xcloner_minimum_version, '<'))
35
+if (version_compare(phpversion(), Xcloner_Restore::xcloner_minimum_version, '<'))
36 36
 {
37
-	Xcloner_Restore::send_response(500, sprintf(("XCloner requires minimum PHP version %s in order to run correctly. We have detected your version as %s"),Xcloner_Restore::xcloner_minimum_version, phpversion()) );
37
+	Xcloner_Restore::send_response(500, sprintf(("XCloner requires minimum PHP version %s in order to run correctly. We have detected your version as %s"), Xcloner_Restore::xcloner_minimum_version, phpversion()));
38 38
 	exit;
39 39
 
40 40
 }
41 41
 
42
-$file = dirname( __DIR__ )  . DS.'vendor'.DS.'autoload.php';
42
+$file = dirname(__DIR__).DS.'vendor'.DS.'autoload.php';
43 43
 
44
-if(file_exists($file))
44
+if (file_exists($file))
45 45
 {
46 46
 	
47 47
 	require_once($file);
48 48
 }
49
-elseif(file_exists("vendor.phar") and extension_loaded('phar'))
49
+elseif (file_exists("vendor.phar") and extension_loaded('phar'))
50 50
 {
51 51
 	require_once(__DIR__.DS."vendor.phar");
52
-}else{	
52
+} else {	
53 53
 	
54
-	$file = dirname( __FILE__ )  . DS.'vendor'.DS.'autoload.php';
54
+	$file = dirname(__FILE__).DS.'vendor'.DS.'autoload.php';
55 55
 	
56
-	if(!file_exists($file))
56
+	if (!file_exists($file))
57 57
 	{
58 58
 		Xcloner_Restore::send_response("404", "File $file does not exists, please extract the vendor.tgz archive on the server or enable PHP Phar module!");
59 59
 		exit;
@@ -78,16 +78,16 @@  discard block
 block discarded – undo
78 78
 
79 79
 //do not modify below
80 80
 $that = "";
81
-if(defined('XCLONER_PLUGIN_ACCESS') && XCLONER_PLUGIN_ACCESS)
81
+if (defined('XCLONER_PLUGIN_ACCESS') && XCLONER_PLUGIN_ACCESS)
82 82
 {
83 83
 	$that = $this;
84 84
 }
85 85
 $xcloner_restore = new Xcloner_Restore($that);
86 86
 
87
-try{
87
+try {
88 88
 	$return = $xcloner_restore->init();
89 89
 	$xcloner_restore->send_response(200, $return);
90
-}catch(Exception $e){
90
+}catch (Exception $e) {
91 91
 	$xcloner_restore->send_response(417, $e->getMessage());
92 92
 }
93 93
 
@@ -96,10 +96,10 @@  discard block
 block discarded – undo
96 96
 	
97 97
 	const 	xcloner_minimum_version = "5.4.0";
98 98
 	
99
-	private $backup_archive_extensions 		= array("zip", "tar", "tgz", "tar.gz", "gz", "csv");
100
-	private $process_files_limit 			= 150;
101
-	private $process_files_limit_list 		= 350;
102
-	private $process_mysql_records_limit 	= 250;
99
+	private $backup_archive_extensions = array("zip", "tar", "tgz", "tar.gz", "gz", "csv");
100
+	private $process_files_limit = 150;
101
+	private $process_files_limit_list = 350;
102
+	private $process_mysql_records_limit = 250;
103 103
 	private $adapter;
104 104
 	private $filesystem;
105 105
 	private $logger;
@@ -115,12 +115,12 @@  discard block
 block discarded – undo
115 115
 	{
116 116
 		register_shutdown_function(array($this, 'exception_handler'));
117 117
 
118
-		if(defined('XCLONER_PLUGIN_ACCESS') && XCLONER_PLUGIN_ACCESS)
118
+		if (defined('XCLONER_PLUGIN_ACCESS') && XCLONER_PLUGIN_ACCESS)
119 119
 		{
120 120
 			$dir = $parent_api->get_xcloner_container()->get_xcloner_settings()->get_xcloner_store_path();
121 121
 		}
122 122
 		
123
-		if(!isset($dir) || !$dir){
123
+		if (!isset($dir) || !$dir) {
124 124
 			$dir = dirname(__FILE__);
125 125
 		}
126 126
 		
@@ -128,7 +128,7 @@  discard block
 block discarded – undo
128 128
 		
129 129
 		$this->backup_storage_dir = $dir;
130 130
 		
131
-		$this->adapter = new Local($dir ,LOCK_EX, 'SKIP_LINKS');
131
+		$this->adapter = new Local($dir, LOCK_EX, 'SKIP_LINKS');
132 132
 		$this->filesystem = new Filesystem($this->adapter, new Config([
133 133
 				'disable_asserts' => true,
134 134
 			]));
@@ -137,15 +137,15 @@  discard block
 block discarded – undo
137 137
 		
138 138
 		$logger_path = $this->get_logger_filename();
139 139
 		
140
-		if(!is_writeable($logger_path) and !touch($logger_path))
140
+		if (!is_writeable($logger_path) and !touch($logger_path))
141 141
 		{
142 142
 			$logger_path = "php://stderr";
143 143
 		}
144 144
 		
145 145
 		$this->logger->pushHandler(new StreamHandler($logger_path, Logger::DEBUG));
146 146
 		
147
-		if(isset($_POST['API_ID'])){
148
-			$this->logger->info("Processing ajax request ID ".substr(filter_input(INPUT_POST, 'API_ID', FILTER_SANITIZE_STRING), 0 , 15));
147
+		if (isset($_POST['API_ID'])) {
148
+			$this->logger->info("Processing ajax request ID ".substr(filter_input(INPUT_POST, 'API_ID', FILTER_SANITIZE_STRING), 0, 15));
149 149
 		}
150 150
 
151 151
 	}
@@ -157,9 +157,9 @@  discard block
 block discarded – undo
157 157
 		
158 158
 		$error = error_get_last();
159 159
 		
160
-		if($error['type'] and $this->logger)
160
+		if ($error['type'] and $this->logger)
161 161
 		{
162
-			$this->logger->info($this->friendly_error_type ($error['type']).": ".var_export($error, true));
162
+			$this->logger->info($this->friendly_error_type($error['type']).": ".var_export($error, true));
163 163
 		}
164 164
 	
165 165
 	}
@@ -169,12 +169,12 @@  discard block
 block discarded – undo
169 169
      * @return mixed|string
170 170
      */
171 171
 	private function friendly_error_type($type) {
172
-	    static $levels=null;
173
-	    if ($levels===null) {
174
-	        $levels=[];
172
+	    static $levels = null;
173
+	    if ($levels === null) {
174
+	        $levels = [];
175 175
 	        foreach (get_defined_constants() as $key=>$value) {
176
-	            if (strpos($key,'E_')!==0) {continue;}
177
-					$levels[$value]= $key; //substr($key,2);
176
+	            if (strpos($key, 'E_') !== 0) {continue; }
177
+					$levels[$value] = $key; //substr($key,2);
178 178
 	        }
179 179
 	    }
180 180
 	    return (isset($levels[$type]) ? $levels[$type] : "Error #{$type}");
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
 	
183 183
 	public function get_logger_filename()
184 184
 	{
185
-		$filename = $this->backup_storage_dir .DS. "xcloner_restore.log";
185
+		$filename = $this->backup_storage_dir.DS."xcloner_restore.log";
186 186
 		
187 187
 		return $filename;
188 188
 	}
@@ -195,7 +195,7 @@  discard block
 block discarded – undo
195 195
      */
196 196
 	public function init()
197 197
 	{
198
-		if(isset($_POST['xcloner_action']) and $_POST['xcloner_action'])
198
+		if (isset($_POST['xcloner_action']) and $_POST['xcloner_action'])
199 199
 		{
200 200
 			$method = filter_input(INPUT_POST, 'xcloner_action', FILTER_SANITIZE_STRING);
201 201
 			
@@ -203,13 +203,13 @@  discard block
 block discarded – undo
203 203
 			
204 204
 			$method .= "_action";
205 205
 			
206
-			if(method_exists($this, $method))
206
+			if (method_exists($this, $method))
207 207
 			{
208 208
 				$this->logger->debug(sprintf('Starting action %s', $method));
209 209
 				return call_user_func(array($this, $method));
210 210
 				
211
-			}else{
212
-				throw new Exception($method ." does not exists");
211
+			} else {
212
+				throw new Exception($method." does not exists");
213 213
 				}
214 214
 		}
215 215
 		
@@ -224,43 +224,43 @@  discard block
 block discarded – undo
224 224
      */
225 225
 	public function write_file_action()
226 226
 	{
227
-		if(isset($_POST['file']))
227
+		if (isset($_POST['file']))
228 228
 		{
229 229
 			$target_file = filter_input(INPUT_POST, 'file', FILTER_SANITIZE_STRING);
230 230
 			
231
-			if(!$_POST['start'])
231
+			if (!$_POST['start'])
232 232
 				$fp = fopen($target_file, "wb+");
233 233
 			else
234 234
 				$fp = fopen($target_file, "ab+");	
235 235
 			
236
-			if(!$fp)
236
+			if (!$fp)
237 237
 				throw new Exception("Unable to open $target_file file for writing");
238 238
 			
239 239
 			fseek($fp, $_POST['start']);
240 240
 			
241
-			if(isset($_FILES['blob']))
241
+			if (isset($_FILES['blob']))
242 242
 			{
243 243
 				$this->logger->debug(sprintf('Writing %s bytes to file %s starting position %s using FILES blob', filesize($_FILES['blob']['tmp_name']), $target_file, $_POST['start']));
244 244
 				
245 245
 				$blob = file_get_contents($_FILES['blob']['tmp_name']);
246 246
 				
247
-				if(!$bytes_written = fwrite($fp, $blob))
247
+				if (!$bytes_written = fwrite($fp, $blob))
248 248
 					throw new Exception("Unable to write data to file $target_file");
249 249
 
250 250
 				try {
251 251
                     unlink($_FILES['blob']['tmp_name']);
252
-                }catch(Exception $e){
252
+                }catch (Exception $e) {
253 253
 
254 254
                 }
255 255
 
256
-			}elseif(isset($_POST['blob'])){
256
+			}elseif (isset($_POST['blob'])) {
257 257
 				$this->logger->debug(sprintf('Writing %s bytes to file %s starting position %s using POST blob', strlen($_POST['blob']), $target_file, $_POST['start']));
258 258
 				
259 259
 				$blob = $_POST['blob'];
260 260
 
261
-				if(!$bytes_written = fwrite($fp, $blob))
261
+				if (!$bytes_written = fwrite($fp, $blob))
262 262
 					throw new Exception("Unable to write data to file $target_file");
263
-			}else{
263
+			} else {
264 264
 				throw new Exception("Upload failed, did not receive any binary data");
265 265
 			}
266 266
 			
@@ -281,20 +281,20 @@  discard block
 block discarded – undo
281 281
      * @return mysqli
282 282
      * @throws Exception
283 283
      */
284
-	public function mysql_connect($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db )
284
+	public function mysql_connect($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db)
285 285
 	{
286 286
 		$this->logger->info(sprintf('Connecting to mysql database %s with %s@%s', $remote_mysql_db, $remote_mysql_user, $remote_mysql_host));
287 287
 
288 288
 		$mysqli = new mysqli($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db);
289 289
 
290 290
 		if ($mysqli->connect_error) {
291
-			throw new Exception('Connect Error (' . $mysqli->connect_errno . ') '
291
+			throw new Exception('Connect Error ('.$mysqli->connect_errno.') '
292 292
 				. $mysqli->connect_error);
293 293
 		}
294 294
 		
295 295
 		$mysqli->query("SET sql_mode='';");
296 296
 		$mysqli->query("SET foreign_key_checks = 0;");
297
-		if(isset($_REQUEST['charset_of_file']) and $_REQUEST['charset_of_file'])
297
+		if (isset($_REQUEST['charset_of_file']) and $_REQUEST['charset_of_file'])
298 298
 			$mysqli->query("SET NAMES ".$_REQUEST['charset_of_file']."");
299 299
 		else
300 300
 			$mysqli->query("SET NAMES utf8;");
@@ -309,15 +309,15 @@  discard block
 block discarded – undo
309 309
      */
310 310
 	public function restore_mysql_backup_action()
311 311
 	{
312
-		$mysqldump_file 	= filter_input(INPUT_POST, 'mysqldump_file', FILTER_SANITIZE_STRING);
313
-		$remote_path 		= filter_input(INPUT_POST, 'remote_path', FILTER_SANITIZE_STRING);
312
+		$mysqldump_file = filter_input(INPUT_POST, 'mysqldump_file', FILTER_SANITIZE_STRING);
313
+		$remote_path = filter_input(INPUT_POST, 'remote_path', FILTER_SANITIZE_STRING);
314 314
 		$remote_mysql_user 	= filter_input(INPUT_POST, 'remote_mysql_user', FILTER_SANITIZE_STRING);
315 315
 		$remote_mysql_pass 	= filter_input(INPUT_POST, 'remote_mysql_pass', FILTER_SANITIZE_STRING);
316
-		$remote_mysql_db 	= filter_input(INPUT_POST, 'remote_mysql_db', FILTER_SANITIZE_STRING);
316
+		$remote_mysql_db = filter_input(INPUT_POST, 'remote_mysql_db', FILTER_SANITIZE_STRING);
317 317
 		$remote_mysql_host 	= filter_input(INPUT_POST, 'remote_mysql_host', FILTER_SANITIZE_STRING);
318
-		$execute_query 		= trim(stripslashes($_POST['query']));
319
-		$error_line			= filter_input(INPUT_POST, 'error_line', FILTER_SANITIZE_NUMBER_INT);
320
-		$start			 	= filter_input(INPUT_POST, 'start', FILTER_SANITIZE_NUMBER_INT);
318
+		$execute_query = trim(stripslashes($_POST['query']));
319
+		$error_line = filter_input(INPUT_POST, 'error_line', FILTER_SANITIZE_NUMBER_INT);
320
+		$start = filter_input(INPUT_POST, 'start', FILTER_SANITIZE_NUMBER_INT);
321 321
 		
322 322
 		$wp_home_url 		= filter_input(INPUT_POST, 'wp_home_url', FILTER_SANITIZE_STRING);
323 323
 		$remote_restore_url = filter_input(INPUT_POST, 'remote_restore_url', FILTER_SANITIZE_STRING);
@@ -327,8 +327,8 @@  discard block
 block discarded – undo
327 327
 		
328 328
 		$mysql_backup_file = $remote_path.DS.$mysqldump_file;
329 329
 		
330
-		if(!file_exists($mysql_backup_file))
331
-			throw new Exception(sprintf("Mysql backup file %s does not exists",$mysql_backup_file));
330
+		if (!file_exists($mysql_backup_file))
331
+			throw new Exception(sprintf("Mysql backup file %s does not exists", $mysql_backup_file));
332 332
 		
333 333
 		
334 334
 		/*if(defined('XCLONER_PLUGIN_ACCESS') && XCLONER_PLUGIN_ACCESS)
@@ -342,7 +342,7 @@  discard block
 block discarded – undo
342 342
 		}*/
343 343
 		
344 344
 		{
345
-			$mysqli = $this->mysql_connect($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db );
345
+			$mysqli = $this->mysql_connect($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db);
346 346
 		}
347 347
 		
348 348
 		$line_count = 0;
@@ -352,7 +352,7 @@  discard block
 block discarded – undo
352 352
 		$return['backup_size']	= filesize($mysql_backup_file);
353 353
 		
354 354
 		$fp = fopen($mysql_backup_file, "r");
355
-		if($fp)
355
+		if ($fp)
356 356
 		{
357 357
 			$this->logger->info(sprintf("Opening mysql dump file %s at position %s.", $mysql_backup_file, $start));
358 358
 			fseek($fp, $start);
@@ -360,49 +360,49 @@  discard block
 block discarded – undo
360 360
 			// process the line read.
361 361
 									
362 362
 				//check if line is comment
363
-				if(substr($line, 0, 1) == "#")
363
+				if (substr($line, 0, 1) == "#")
364 364
 					continue;
365 365
 				
366 366
 				//check if line is empty	
367
-				if($line == "\n" or trim($line) == "")
367
+				if ($line == "\n" or trim($line) == "")
368 368
 					continue;
369 369
 					
370
-				if(substr($line, strlen($line)-2, strlen($line)) == ";\n")
370
+				if (substr($line, strlen($line) - 2, strlen($line)) == ";\n")
371 371
 					$query .= $line;
372
-				else{
372
+				else {
373 373
 					$query .= $line;
374 374
 					continue;
375 375
 				}
376 376
 				
377
-				if($execute_query)
377
+				if ($execute_query)
378 378
 				{
379
-					$query  = (($execute_query));
379
+					$query = (($execute_query));
380 380
 					$execute_query = "";
381 381
 				}	
382 382
 				
383 383
 				//Doing serialized url replace here
384 384
 				
385
-				if($wp_site_url and $wp_home_url and strlen($wp_home_url) < strlen($wp_site_url))
385
+				if ($wp_site_url and $wp_home_url and strlen($wp_home_url) < strlen($wp_site_url))
386 386
 				{
387
-					list($wp_home_url,$wp_site_url) 			= array($wp_site_url,$wp_home_url);
388
-					list($remote_restore_url,$restore_site_url) = array($restore_site_url,$remote_restore_url);
387
+					list($wp_home_url, $wp_site_url) = array($wp_site_url, $wp_home_url);
388
+					list($remote_restore_url, $restore_site_url) = array($restore_site_url, $remote_restore_url);
389 389
 					
390 390
 				}
391 391
 				
392
-				if($wp_home_url and $remote_restore_url and strpos($query, $wp_home_url) !== false)
392
+				if ($wp_home_url and $remote_restore_url and strpos($query, $wp_home_url) !== false)
393 393
 				{
394 394
 					$query = $this->url_replace($wp_home_url, $remote_restore_url, $query);
395 395
 				}
396 396
 				
397
-				if($wp_site_url and $restore_site_url and strpos($query, $wp_site_url) !== false)
397
+				if ($wp_site_url and $restore_site_url and strpos($query, $wp_site_url) !== false)
398 398
 				{
399 399
 					$query = $this->url_replace($wp_site_url, $restore_site_url, $query);
400 400
 				}
401 401
 				
402
-				if(!$mysqli->query($query) && !stristr($mysqli->error,"Duplicate entry"))
402
+				if (!$mysqli->query($query) && !stristr($mysqli->error, "Duplicate entry"))
403 403
 				{
404 404
 					//$return['error_line'] = $line_count;
405
-					$return['start'] = ftell($fp)-strlen($line);
405
+					$return['start'] = ftell($fp) - strlen($line);
406 406
 					$return['query_error'] = true;
407 407
 					$return['query'] = $query;
408 408
 					$return['message'] = sprintf("Mysql Error: %s\n", $mysqli->error);
@@ -423,12 +423,12 @@  discard block
 block discarded – undo
423 423
 		
424 424
 		$return['start'] = ftell($fp);
425 425
 		
426
-		$this->logger->info(sprintf("Executed %s queries of size %s bytes", $line_count, ($return['start']-$start)));
426
+		$this->logger->info(sprintf("Executed %s queries of size %s bytes", $line_count, ($return['start'] - $start)));
427 427
 		
428
-		if(!feof($fp))
428
+		if (!feof($fp))
429 429
 		{
430 430
 			$return['finished'] = 0;
431
-		}else{
431
+		} else {
432 432
 			$this->logger->info(sprintf("Mysql Import Done."));
433 433
 		}
434 434
 		
@@ -451,12 +451,12 @@  discard block
 block discarded – undo
451 451
 		$query = str_replace($search, $replace, $query);
452 452
 		$original_query = $query;
453 453
 		
454
-		if($this->has_serialized($query))
454
+		if ($this->has_serialized($query))
455 455
 		{
456 456
 			$this->logger->info(sprintf("Query contains serialized data, doing serialized size fix"), array("QUERY_REPLACE"));
457 457
 			$query = $this->do_serialized_fix($query);
458 458
 			
459
-			if(!$query)
459
+			if (!$query)
460 460
 			{
461 461
 				$this->logger->info(sprintf("Serialization probably failed here..."), array("QUERY_REPLACE"));
462 462
 				$query = $original_query;
@@ -477,37 +477,37 @@  discard block
 block discarded – undo
477 477
 		$backup_parts = array();
478 478
 		
479 479
 		$source_backup_file = filter_input(INPUT_POST, 'file', FILTER_SANITIZE_STRING);
480
-		$start 				= (int)filter_input(INPUT_POST, 'start', FILTER_SANITIZE_STRING);
481
-		$return['part'] 	= (int)filter_input(INPUT_POST, 'part', FILTER_SANITIZE_STRING);
480
+		$start = (int)filter_input(INPUT_POST, 'start', FILTER_SANITIZE_STRING);
481
+		$return['part'] = (int)filter_input(INPUT_POST, 'part', FILTER_SANITIZE_STRING);
482 482
 		
483 483
 		$backup_file = $source_backup_file;
484 484
 		
485
-		if($this->is_multipart($backup_file))
485
+		if ($this->is_multipart($backup_file))
486 486
 		{
487 487
 			$backup_parts = $this->get_multipart_files($backup_file);
488 488
 			$backup_file = $backup_parts[$return['part']];
489 489
 		}
490 490
 		
491
-		try{
491
+		try {
492 492
 			$tar = new Tar();
493 493
 			$tar->open($this->backup_storage_dir.DS.$backup_file, $start);
494 494
 		
495 495
 			$data = $tar->contents($this->process_files_limit_list);
496
-		}catch(Exception $e)
496
+		}catch (Exception $e)
497 497
 		{
498 498
 			$return['error'] = true;
499 499
 			$return['message'] = $e->getMessage();
500 500
 			$this->send_response(200, $return);
501 501
 		}
502 502
 		
503
-		$return['files'] 		= array();
504
-		$return['finished'] 	= 1;
505
-		$return['total_size'] 	= filesize($this->backup_storage_dir.DS.$backup_file);
503
+		$return['files'] = array();
504
+		$return['finished'] = 1;
505
+		$return['total_size'] = filesize($this->backup_storage_dir.DS.$backup_file);
506 506
 		$i = 0;
507 507
 		
508
-		if(isset($data['extracted_files']) and is_array($data['extracted_files']))
508
+		if (isset($data['extracted_files']) and is_array($data['extracted_files']))
509 509
 		{
510
-			foreach($data['extracted_files'] as $file)
510
+			foreach ($data['extracted_files'] as $file)
511 511
 			{
512 512
 				$return['files'][$i]['path'] = $file->getPath();
513 513
 				$return['files'][$i]['size'] = $file->getSize();
@@ -517,18 +517,18 @@  discard block
 block discarded – undo
517 517
 			}
518 518
 		}
519 519
 		
520
-		if(isset($data['start']))
520
+		if (isset($data['start']))
521 521
 		{
522 522
 			$return['start'] = $data['start'];
523 523
 			$return['finished'] = 0;	
524
-		}else{
525
-			if($this->is_multipart($source_backup_file))
524
+		} else {
525
+			if ($this->is_multipart($source_backup_file))
526 526
 			{
527 527
 				$return['start'] = 0;
528 528
 				
529 529
 				++$return['part'];
530 530
 			
531
-				if($return['part'] < sizeof($backup_parts))	
531
+				if ($return['part'] < sizeof($backup_parts))	
532 532
 					$return['finished'] = 0;
533 533
 				
534 534
 			}
@@ -551,28 +551,28 @@  discard block
 block discarded – undo
551 551
 		
552 552
 		$remote_mysql_user 	= filter_input(INPUT_POST, 'remote_mysql_user', FILTER_SANITIZE_STRING);
553 553
 		$remote_mysql_pass 	= filter_input(INPUT_POST, 'remote_mysql_pass', FILTER_SANITIZE_STRING);
554
-		$remote_mysql_db 	= filter_input(INPUT_POST, 'remote_mysql_db', FILTER_SANITIZE_STRING);
554
+		$remote_mysql_db = filter_input(INPUT_POST, 'remote_mysql_db', FILTER_SANITIZE_STRING);
555 555
 		$remote_mysql_host 	= filter_input(INPUT_POST, 'remote_mysql_host', FILTER_SANITIZE_STRING);
556 556
 		
557
-		$update_remote_site_url			 	= filter_input(INPUT_POST, 'update_remote_site_url', FILTER_SANITIZE_NUMBER_INT);
558
-		$delete_restore_script			 	= filter_input(INPUT_POST, 'delete_restore_script', FILTER_SANITIZE_NUMBER_INT);
559
-		$delete_backup_temporary_folder		= filter_input(INPUT_POST, 'delete_backup_temporary_folder', FILTER_SANITIZE_NUMBER_INT);
557
+		$update_remote_site_url = filter_input(INPUT_POST, 'update_remote_site_url', FILTER_SANITIZE_NUMBER_INT);
558
+		$delete_restore_script = filter_input(INPUT_POST, 'delete_restore_script', FILTER_SANITIZE_NUMBER_INT);
559
+		$delete_backup_temporary_folder = filter_input(INPUT_POST, 'delete_backup_temporary_folder', FILTER_SANITIZE_NUMBER_INT);
560 560
 				
561
-		if($update_remote_site_url)
561
+		if ($update_remote_site_url)
562 562
 		{
563
-			$mysqli = $this->mysql_connect($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db );
563
+			$mysqli = $this->mysql_connect($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db);
564 564
 			$this->update_wp_config($remote_path, $remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db);
565 565
 			$this->update_wp_url($remote_path, $remote_restore_url, $mysqli);
566 566
 		}
567 567
 		
568
-		if($delete_backup_temporary_folder)
568
+		if ($delete_backup_temporary_folder)
569 569
 		{
570 570
 			$this->delete_backup_temporary_folder($remote_path);
571 571
 		}
572 572
 		
573
-		if(!defined('XCLONER_PLUGIN_ACCESS') || XCLONER_PLUGIN_ACCESS != 1)
573
+		if (!defined('XCLONER_PLUGIN_ACCESS') || XCLONER_PLUGIN_ACCESS != 1)
574 574
 		{
575
-			if($delete_restore_script)
575
+			if ($delete_restore_script)
576 576
 			{
577 577
 				$this->delete_self();
578 578
 			}
@@ -590,7 +590,7 @@  discard block
 block discarded – undo
590 590
      */
591 591
 	private function delete_backup_temporary_folder($remote_path)
592 592
 	{
593
-		$this->target_adapter = new Local($remote_path ,LOCK_EX, 'SKIP_LINKS');
593
+		$this->target_adapter = new Local($remote_path, LOCK_EX, 'SKIP_LINKS');
594 594
 		$this->target_filesystem = new Filesystem($this->target_adapter, new Config([
595 595
 				'disable_asserts' => true,
596 596
 			]));
@@ -598,13 +598,13 @@  discard block
 block discarded – undo
598 598
 		$mysqldump_list = array();
599 599
 		$list = $this->target_filesystem->listContents();
600 600
 		
601
-		foreach($list as $file)
601
+		foreach ($list as $file)
602 602
 		{
603 603
 			$matches = array();
604 604
 			
605
-			if($file['type'] == "dir")
605
+			if ($file['type'] == "dir")
606 606
 			{
607
-				if(preg_match("/xcloner-(\w*)/", $file['basename'], $matches)){
607
+				if (preg_match("/xcloner-(\w*)/", $file['basename'], $matches)) {
608 608
 					$this->logger->info(sprintf('Deleting temporary folder %s', $file['path']));
609 609
 					$this->target_filesystem->deleteDir($file['path']);
610 610
 				}
@@ -622,29 +622,29 @@  discard block
 block discarded – undo
622 622
      */
623 623
 	private function delete_self()
624 624
 	{
625
-		if($this->filesystem->has("vendor.phar"))
625
+		if ($this->filesystem->has("vendor.phar"))
626 626
 		{
627 627
 			$this->logger->info(sprintf('Deleting vendor.phar'));
628 628
 			$this->filesystem->delete("vendor.phar");
629 629
 		}
630
-		if($this->filesystem->has("vendor"))
630
+		if ($this->filesystem->has("vendor"))
631 631
 		{
632 632
 			$this->logger->info(sprintf('Deleting vendor folder'));
633 633
 			$this->filesystem->deleteDir("vendor");
634 634
 		}
635
-		if($this->filesystem->has("xcloner_restore.php"))
635
+		if ($this->filesystem->has("xcloner_restore.php"))
636 636
 		{
637 637
 			$this->logger->info(sprintf('Deleting xcloner_restore.php'));
638 638
 			$this->filesystem->delete("xcloner_restore.php");
639 639
 		}
640 640
 		
641
-		if($this->filesystem->has("xcloner_restore.log"))
641
+		if ($this->filesystem->has("xcloner_restore.log"))
642 642
 		{
643 643
 			$this->logger->info(sprintf('Deleting xcloner_restore.log'));
644 644
 			$this->filesystem->delete("xcloner_restore.log");
645 645
 		}
646 646
 		
647
-		if($this->filesystem->has($this->get_logger_filename()))
647
+		if ($this->filesystem->has($this->get_logger_filename()))
648 648
 		{
649 649
 			$this->logger->info(sprintf('Deleting logger file %s', $this->get_logger_filename()));
650 650
 			$this->filesystem->delete($this->get_logger_filename());
@@ -666,11 +666,11 @@  discard block
 block discarded – undo
666 666
 		
667 667
 		$this->logger->info(sprintf('Updating site url to %s', $url));
668 668
 		
669
-		if(file_exists($wp_config))
669
+		if (file_exists($wp_config))
670 670
 		{
671 671
 			$config = file_get_contents($wp_config);
672 672
 			preg_match("/.*table_prefix.*=.*'(.*)'/i", $config, $matches);
673
-			if(isset($matches[1]))
673
+			if (isset($matches[1]))
674 674
 				$table_prefix = $matches[1];
675 675
 			else
676 676
 				throw new Exception("Could not load wordpress table prefix from wp-config.php file.");
@@ -678,10 +678,10 @@  discard block
 block discarded – undo
678 678
 		else
679 679
 			throw new Exception("Could not update the SITEURL and HOME, wp-config.php file not found");
680 680
 			
681
-		if(!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='home'"))
681
+		if (!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='home'"))
682 682
 			throw new Exception(sprintf("Could not update the HOME option, error: %s\n", $mysqli->error));
683 683
 		
684
-		if(!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='siteurl'"))
684
+		if (!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='siteurl'"))
685 685
 			throw new Exception(sprintf("Could not update the SITEURL option, error: %s\n", $mysqli->error));
686 686
 		
687 687
 		return true;
@@ -702,7 +702,7 @@  discard block
 block discarded – undo
702 702
 	{
703 703
 		$wp_config = $remote_path.DS."wp-config.php";
704 704
 		
705
-		if(!file_exists($wp_config))
705
+		if (!file_exists($wp_config))
706 706
 		{
707 707
 			throw new Exception("Could not find the wp-config.php in ".$remote_path);
708 708
 		}
@@ -720,7 +720,7 @@  discard block
 block discarded – undo
720 720
 		
721 721
 		$this->logger->info(sprintf('Updating wp-config.php file with the new mysql details'));
722 722
 		
723
-		if(!file_put_contents($wp_config, $content))
723
+		if (!file_put_contents($wp_config, $content))
724 724
 			throw new Exception("Could not write updated config data to ".$wp_config);
725 725
 		
726 726
 		chmod($wp_config, $file_perms);
@@ -740,7 +740,7 @@  discard block
 block discarded – undo
740 740
 	
741 741
 		$hash = $this->get_hash_from_backup($source_backup_file);	
742 742
 		
743
-		$this->target_adapter = new Local($remote_path ,LOCK_EX, 'SKIP_LINKS');
743
+		$this->target_adapter = new Local($remote_path, LOCK_EX, 'SKIP_LINKS');
744 744
 		$this->target_filesystem = new Filesystem($this->target_adapter, new Config([
745 745
 				'disable_asserts' => true,
746 746
 			]));
@@ -748,25 +748,25 @@  discard block
 block discarded – undo
748 748
 		$mysqldump_list = array();
749 749
 		$list = $this->target_filesystem->listContents();
750 750
 		
751
-		foreach($list as $file)
751
+		foreach ($list as $file)
752 752
 		{
753 753
 			$matches = array();
754 754
 			
755
-			if($file['type'] == "dir")
755
+			if ($file['type'] == "dir")
756 756
 			{
757
-				if(preg_match("/xcloner-(\w*)/", $file['basename'], $matches))
757
+				if (preg_match("/xcloner-(\w*)/", $file['basename'], $matches))
758 758
 				{
759 759
 					$files = $this->target_filesystem->listContents($file['basename']);
760
-					foreach($files as $file)
760
+					foreach ($files as $file)
761 761
 					{
762
-						if($file['extension'] == "sql")
762
+						if ($file['extension'] == "sql")
763 763
 						{
764 764
 							$this->logger->info(sprintf('Found %s mysql backup file', $file['path']));
765 765
 							$mysqldump_list[$file['path']]['path'] = $file['path'];
766 766
 							$mysqldump_list[$file['path']]['size'] = $file['size'];
767
-							$mysqldump_list[$file['path']]['timestamp'] = date("d M,Y H:i",$file['timestamp']);
767
+							$mysqldump_list[$file['path']]['timestamp'] = date("d M,Y H:i", $file['timestamp']);
768 768
 							
769
-							if($hash and $hash == $matches[1])
769
+							if ($hash and $hash == $matches[1])
770 770
 								$mysqldump_list[$file['path']]['selected'] = "selected";
771 771
 							else
772 772
 								$mysqldump_list[$file['path']]['selected'] = "";	
@@ -776,7 +776,7 @@  discard block
 block discarded – undo
776 776
 			}	
777 777
 		}
778 778
 		
779
-		$this->sort_by($mysqldump_list, 'timestamp','desc');
779
+		$this->sort_by($mysqldump_list, 'timestamp', 'desc');
780 780
 		$return['files'] = $mysqldump_list;
781 781
 		
782 782
 		$this->send_response(200, $return);
@@ -790,12 +790,12 @@  discard block
 block discarded – undo
790 790
      */
791 791
 	private function get_hash_from_backup($backup_file)
792 792
 	{
793
-		if(!$backup_file)
793
+		if (!$backup_file)
794 794
 			return false;
795 795
 			
796
-		$result = preg_match("/-(\w*)./", substr($backup_file, strlen($backup_file)-10, strlen($backup_file)), $matches)	;
796
+		$result = preg_match("/-(\w*)./", substr($backup_file, strlen($backup_file) - 10, strlen($backup_file)), $matches);
797 797
 		
798
-		if($result and isset($matches[1]))
798
+		if ($result and isset($matches[1]))
799 799
 			return ($matches[1]);
800 800
 		
801 801
 		return false;
@@ -814,18 +814,18 @@  discard block
 block discarded – undo
814 814
 		$backup_files = array();
815 815
 		$parents = array();
816 816
 		
817
-		foreach($list as $file_info)
817
+		foreach ($list as $file_info)
818 818
 		{
819 819
 			$data = array();
820 820
 			
821
-			if(isset($file_info['extension']) and $file_info['extension'] == "csv")
821
+			if (isset($file_info['extension']) and $file_info['extension'] == "csv")
822 822
 			{
823 823
 				$lines = explode(PHP_EOL, $this->filesystem->read($file_info['path']));
824
-				foreach($lines as $line)
825
-					if($line)
824
+				foreach ($lines as $line)
825
+					if ($line)
826 826
 					{
827 827
 						$data = str_getcsv($line);
828
-						if(is_array($data)){
828
+						if (is_array($data)) {
829 829
 							$parents[$data[0]] = $file_info['path'];
830 830
 							$file_info['childs'][] = $data;
831 831
 							$file_info['size'] += $data[2];
@@ -834,19 +834,19 @@  discard block
 block discarded – undo
834 834
 						
835 835
 			}
836 836
 			
837
-			if($file_info['type'] == 'file' and isset($file_info['extension']) and in_array($file_info['extension'], $this->backup_archive_extensions))
837
+			if ($file_info['type'] == 'file' and isset($file_info['extension']) and in_array($file_info['extension'], $this->backup_archive_extensions))
838 838
 				$backup_files[$file_info['path']] = $file_info;
839 839
 		}
840 840
 		
841 841
 		$new_list = array();
842 842
 		
843
-		foreach($backup_files as $key=>$file_info)
843
+		foreach ($backup_files as $key=>$file_info)
844 844
 		{
845
-			if(isset($parents[$file_info['path']]))
845
+			if (isset($parents[$file_info['path']]))
846 846
 				$backup_files[$key]['parent'] = $parents[$file_info['path']];
847
-			else{
847
+			else {
848 848
 				
849
-				if($local_backup_file and ($file_info['basename'] == $local_backup_file))
849
+				if ($local_backup_file and ($file_info['basename'] == $local_backup_file))
850 850
 					$file_info['selected'] = 'selected';
851 851
 				
852 852
 				$this->logger->info(sprintf('Found %s backup file', $file_info['path']));
@@ -856,7 +856,7 @@  discard block
 block discarded – undo
856 856
 				
857 857
 		}
858 858
 		
859
-		$this->sort_by($new_list, "timestamp","desc");
859
+		$this->sort_by($new_list, "timestamp", "desc");
860 860
 		
861 861
 		$return['files'] = $new_list;
862 862
 		
@@ -872,15 +872,15 @@  discard block
 block discarded – undo
872 872
      */
873 873
 	public function restore_backup_to_path_action()
874 874
 	{
875
-		$source_backup_file 	= filter_input(INPUT_POST, 'backup_file', FILTER_SANITIZE_STRING);
876
-		$remote_path 			= filter_input(INPUT_POST, 'remote_path', FILTER_SANITIZE_STRING);
875
+		$source_backup_file = filter_input(INPUT_POST, 'backup_file', FILTER_SANITIZE_STRING);
876
+		$remote_path = filter_input(INPUT_POST, 'remote_path', FILTER_SANITIZE_STRING);
877 877
 		$include_filter_files 	= filter_input(INPUT_POST, 'filter_files', FILTER_SANITIZE_STRING);
878 878
 		$exclude_filter_files 	= "";
879
-		$start 					= filter_input(INPUT_POST, 'start', FILTER_SANITIZE_NUMBER_INT);
880
-		$return['part'] 		= (int)filter_input(INPUT_POST, 'part', FILTER_SANITIZE_NUMBER_INT);
879
+		$start = filter_input(INPUT_POST, 'start', FILTER_SANITIZE_NUMBER_INT);
880
+		$return['part'] = (int)filter_input(INPUT_POST, 'part', FILTER_SANITIZE_NUMBER_INT);
881 881
 		$return['processed'] 	= (int)filter_input(INPUT_POST, 'processed', FILTER_SANITIZE_NUMBER_INT);
882 882
 				
883
-		$this->target_adapter = new Local($remote_path ,LOCK_EX, 'SKIP_LINKS');
883
+		$this->target_adapter = new Local($remote_path, LOCK_EX, 'SKIP_LINKS');
884 884
 		$this->target_filesystem = new Filesystem($this->target_adapter, new Config([
885 885
 				'disable_asserts' => true,
886 886
 			]));
@@ -892,9 +892,9 @@  discard block
 block discarded – undo
892 892
 		$return['total_size'] = $this->get_backup_size($backup_file);
893 893
 		
894 894
 		$backup_archive = new Tar();
895
-		if($this->is_multipart($backup_file))
895
+		if ($this->is_multipart($backup_file))
896 896
 		{
897
-			if(!$return['part'])
897
+			if (!$return['part'])
898 898
 				$return['processed'] += $this->filesystem->getSize($backup_file);
899 899
 				
900 900
 			$backup_parts = $this->get_multipart_files($backup_file);
@@ -902,41 +902,41 @@  discard block
 block discarded – undo
902 902
 		}	
903 903
 		
904 904
 		$this->logger->info(sprintf('Opening backup archive %s at position %s', $backup_file, $start));
905
-		$backup_archive->open($this->backup_storage_dir .DS. $backup_file, $start);
905
+		$backup_archive->open($this->backup_storage_dir.DS.$backup_file, $start);
906 906
 
907
-		$data = $backup_archive->extract($remote_path, '',$exclude_filter_files,$include_filter_files, $this->process_files_limit);
907
+		$data = $backup_archive->extract($remote_path, '', $exclude_filter_files, $include_filter_files, $this->process_files_limit);
908 908
 		
909
-		if(isset($data['extracted_files']))
909
+		if (isset($data['extracted_files']))
910 910
 		{
911
-			foreach($data['extracted_files'] as $spl_fileinfo)
911
+			foreach ($data['extracted_files'] as $spl_fileinfo)
912 912
 			{
913 913
 				$this->logger->info(sprintf('Extracted %s file', $spl_fileinfo->getPath()));
914 914
 				$return['extracted_files'][] = $spl_fileinfo->getPath()." (".$spl_fileinfo->getSize()." bytes)";
915 915
 			}
916 916
 		}
917 917
 		
918
-		if(isset($data['start']))
918
+		if (isset($data['start']))
919 919
 		//if(isset($data['start']) and $data['start'] <= $this->filesystem->getSize($backup_file))
920 920
 		{
921 921
 			$return['finished'] = 0;
922 922
 			$return['start'] = $data['start'];
923
-		}else{
923
+		} else {
924 924
 			
925 925
 			$return['processed'] += $start;
926 926
 			
927
-			if($this->is_multipart($source_backup_file))
927
+			if ($this->is_multipart($source_backup_file))
928 928
 			{
929 929
 				$return['start'] = 0;
930 930
 				
931 931
 				++$return['part'];
932 932
 			
933
-				if($return['part'] < sizeof($backup_parts))	
933
+				if ($return['part'] < sizeof($backup_parts))	
934 934
 					$return['finished'] = 0;
935 935
 				
936 936
 			}
937 937
 		}
938 938
 		
939
-		if($return['finished'])
939
+		if ($return['finished'])
940 940
 			$this->logger->info(sprintf('Done extracting %s', $source_backup_file));
941 941
 		
942 942
 		$return['backup_file'] = $backup_file;
@@ -953,24 +953,24 @@  discard block
 block discarded – undo
953 953
 		
954 954
 		$restore_script_url = filter_input(INPUT_POST, 'restore_script_url', FILTER_SANITIZE_STRING);
955 955
 		
956
-		$pathinfo = pathinfo( __FILE__);
956
+		$pathinfo = pathinfo(__FILE__);
957 957
 		
958 958
 		$suffix = "";
959 959
 		$return['remote_mysql_host'] 	= "localhost";
960 960
 		$return['remote_mysql_user'] 	= "";
961 961
 		$return['remote_mysql_pass'] 	= "";
962
-		$return['remote_mysql_db'] 		= "";
962
+		$return['remote_mysql_db'] = "";
963 963
 		
964
-		if(defined('XCLONER_PLUGIN_ACCESS') && XCLONER_PLUGIN_ACCESS)
964
+		if (defined('XCLONER_PLUGIN_ACCESS') && XCLONER_PLUGIN_ACCESS)
965 965
 		{
966 966
 			$return['dir'] = realpath(get_home_path().DS.$suffix);
967
-			$return['restore_script_url']  	= get_site_url();
967
+			$return['restore_script_url'] = get_site_url();
968 968
 			$return['remote_mysql_host'] 	= $wpdb->dbhost;
969 969
 			$return['remote_mysql_user'] 	= $wpdb->dbuser;
970 970
 			$return['remote_mysql_pass'] 	= $wpdb->dbpassword;
971
-			$return['remote_mysql_db'] 		= $wpdb->dbname;
971
+			$return['remote_mysql_db'] = $wpdb->dbname;
972 972
 		}
973
-		else{
973
+		else {
974 974
 			$return['dir'] = ($pathinfo['dirname']).DS.$suffix;
975 975
 			$return['restore_script_url'] = str_replace($pathinfo['basename'], "", $restore_script_url).$suffix;
976 976
 		}	
@@ -989,17 +989,17 @@  discard block
 block discarded – undo
989 989
 	{
990 990
 		//check if i can write
991 991
 		$tmp_file = md5(time());
992
-		if(!file_put_contents($tmp_file, "++"))
992
+		if (!file_put_contents($tmp_file, "++"))
993 993
 			throw new Exception("Could not write to new host");
994 994
 		
995
-		if(!unlink($tmp_file))
995
+		if (!unlink($tmp_file))
996 996
 			throw new Exception("Could not delete temporary file from new host");
997 997
 		
998 998
 		$max_upload      = $this->return_bytes((ini_get('upload_max_filesize')));
999 999
 		$max_post        = $this->return_bytes((ini_get('post_max_size')));
1000 1000
 
1001 1001
 		$return['max_upload_size'] = min($max_upload, $max_post); // bytes
1002
-		$return['status']		= true;
1002
+		$return['status'] = true;
1003 1003
 		
1004 1004
 		$this->logger->info(sprintf('Current filesystem max upload size is %s bytes', $return['max_upload_size']));
1005 1005
 		
@@ -1015,8 +1015,8 @@  discard block
 block discarded – undo
1015 1015
      */
1016 1016
     private function return_bytes($val) {
1017 1017
         $numeric_val = (int)trim($val);
1018
-        $last = strtolower($val[strlen($val)-1]);
1019
-        switch($last) {
1018
+        $last = strtolower($val[strlen($val) - 1]);
1019
+        switch ($last) {
1020 1020
             // The 'G' modifier is available since PHP 5.1.0
1021 1021
             case 'g':
1022 1022
                 $numeric_val *= 1024;
@@ -1037,7 +1037,7 @@  discard block
 block discarded – undo
1037 1037
      */
1038 1038
 	public function is_multipart($backup_name)
1039 1039
 	{
1040
-		if(stristr($backup_name, "-multipart"))
1040
+		if (stristr($backup_name, "-multipart"))
1041 1041
 			return true;
1042 1042
 		
1043 1043
 		return false;	
@@ -1053,10 +1053,10 @@  discard block
 block discarded – undo
1053 1053
 	public function get_backup_size($backup_name)
1054 1054
 	{
1055 1055
 		$backup_size = $this->filesystem->getSize($backup_name);
1056
-		if($this->is_multipart($backup_name))
1056
+		if ($this->is_multipart($backup_name))
1057 1057
 		{
1058 1058
 			$backup_parts = $this->get_multipart_files($backup_name);
1059
-			foreach($backup_parts as $part_file)
1059
+			foreach ($backup_parts as $part_file)
1060 1060
 				$backup_size += $this->filesystem->getSize($part_file);
1061 1061
 		}
1062 1062
 		
@@ -1073,12 +1073,12 @@  discard block
 block discarded – undo
1073 1073
 	{
1074 1074
 		$files = array();
1075 1075
 		
1076
-		if($this->is_multipart($backup_name))
1076
+		if ($this->is_multipart($backup_name))
1077 1077
 		{
1078 1078
 			$lines = explode(PHP_EOL, $this->filesystem->read($backup_name));
1079
-			foreach($lines as $line)
1079
+			foreach ($lines as $line)
1080 1080
 			{
1081
-				if($line)
1081
+				if ($line)
1082 1082
 				{
1083 1083
 					$data = str_getcsv($line);
1084 1084
 					$files[] = $data[0];
@@ -1097,7 +1097,7 @@  discard block
 block discarded – undo
1097 1097
      * @param string $direction
1098 1098
      * @return bool
1099 1099
      */
1100
-    private function sort_by( &$array, $field, $direction = 'asc')
1100
+    private function sort_by(&$array, $field, $direction = 'asc')
1101 1101
     {
1102 1102
         $direction = strtolower($direction);
1103 1103
 
@@ -1112,18 +1112,18 @@  discard block
 block discarded – undo
1112 1112
                     return 0;
1113 1113
                 }
1114 1114
 
1115
-                if($direction == 'desc') {
1116
-                    if($a > $b) {
1115
+                if ($direction == 'desc') {
1116
+                    if ($a > $b) {
1117 1117
                         return -1;
1118 1118
                     }
1119
-                    else{
1119
+                    else {
1120 1120
                         return 1;
1121 1121
                     }
1122
-                }else {
1123
-                    if($a < $b) {
1122
+                } else {
1123
+                    if ($a < $b) {
1124 1124
                         return -1;
1125 1125
                     }
1126
-                    else{
1126
+                    else {
1127 1127
                         return 1;
1128 1128
                     }
1129 1129
                 }
@@ -1149,11 +1149,11 @@  discard block
 block discarded – undo
1149 1149
 		$return['status'] = $status;
1150 1150
 		$return['statusText'] = $response;
1151 1151
 		
1152
-		if(isset($response['error']) && $response['error'])
1152
+		if (isset($response['error']) && $response['error'])
1153 1153
 		{
1154 1154
 			$return['statusText'] = $response['message'];
1155 1155
 			$return['error'] = true;
1156
-		}elseif($status != 200 and $status != 418)
1156
+		}elseif ($status != 200 and $status != 418)
1157 1157
 		{
1158 1158
 			$return['error'] = true;
1159 1159
 			$return['message'] = $response;
@@ -1172,12 +1172,12 @@  discard block
 block discarded – undo
1172 1172
 	 
1173 1173
 	function do_serialized_fix($query)
1174 1174
 	{
1175
-		$query = str_replace(array("\\n","\\r","\\'"), array("","","\""), ($query));
1175
+		$query = str_replace(array("\\n", "\\r", "\\'"), array("", "", "\""), ($query));
1176 1176
 		
1177
-		return preg_replace_callback('!s:(\d+):([\\\\]?"[\\\\]?"|[\\\\]?"((.*?)[^\\\\])[\\\\]?");!', function ($m) {
1177
+		return preg_replace_callback('!s:(\d+):([\\\\]?"[\\\\]?"|[\\\\]?"((.*?)[^\\\\])[\\\\]?");!', function($m) {
1178 1178
 				  $data = "";
1179 1179
 				  	
1180
-				  if(!isset($m[3]))
1180
+				  if (!isset($m[3]))
1181 1181
 					$m[3] = "";
1182 1182
 					
1183 1183
 					$data = 's:'.strlen(($m[3])).':\"'.($m[3]).'\";';
@@ -1204,8 +1204,8 @@  discard block
 block discarded – undo
1204 1204
      * @return mixed
1205 1205
      */
1206 1206
 	private function unescape_mysql($value) {
1207
-		return str_replace(array("\\\\", "\\0", "\\n", "\\r", "\Z",  "\'", '\"'),
1208
-						   array("\\",   "\0",  "\n",  "\r",  "\x1a", "'", '"'), 
1207
+		return str_replace(array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'),
1208
+						   array("\\", "\0", "\n", "\r", "\x1a", "'", '"'), 
1209 1209
 						   $value);
1210 1210
 	}
1211 1211
 
@@ -1217,14 +1217,14 @@  discard block
 block discarded – undo
1217 1217
      */
1218 1218
 	private function has_serialized($s)
1219 1219
 	{
1220
-		if(
1221
-		    stristr($s, '{' ) !== false &&
1222
-		    stristr($s, '}' ) !== false &&
1223
-		    stristr($s, ';' ) !== false &&
1224
-		    stristr($s, ':' ) !== false
1225
-		    ){
1220
+		if (
1221
+		    stristr($s, '{') !== false &&
1222
+		    stristr($s, '}') !== false &&
1223
+		    stristr($s, ';') !== false &&
1224
+		    stristr($s, ':') !== false
1225
+		    ) {
1226 1226
 		    return true;
1227
-		}else{
1227
+		} else {
1228 1228
 		    return false;
1229 1229
 		}
1230 1230
 
Please login to merge, or discard this patch.
includes/class-xcloner-encryption.php 1 patch
Spacing   +78 added lines, -78 removed lines patch added patch discarded remove patch
@@ -10,7 +10,7 @@  discard block
 block discarded – undo
10 10
 
11 11
 class Xcloner_Encryption
12 12
 {
13
-    const FILE_ENCRYPTION_BLOCKS = 1024*1024;
13
+    const FILE_ENCRYPTION_BLOCKS = 1024 * 1024;
14 14
     const FILE_ENCRYPTION_SUFFIX = ".encrypted";
15 15
     const FILE_DECRYPTION_SUFFIX = ".decrypted";
16 16
 
@@ -21,15 +21,15 @@  discard block
 block discarded – undo
21 21
     public function __construct(Xcloner $xcloner_container)
22 22
     {
23 23
         $this->xcloner_container = $xcloner_container;
24
-        if(method_exists($xcloner_container, 'get_xcloner_settings')) {
24
+        if (method_exists($xcloner_container, 'get_xcloner_settings')) {
25 25
             $this->xcloner_settings = $xcloner_container->get_xcloner_settings();
26
-        }else{
26
+        } else {
27 27
             $this->xcloner_settings = "";
28 28
         }
29 29
 
30
-        if(method_exists($xcloner_container, 'get_xcloner_logger')) {
30
+        if (method_exists($xcloner_container, 'get_xcloner_logger')) {
31 31
             $this->logger = $xcloner_container->get_xcloner_logger()->withName("xcloner_encryption");
32
-        }else{
32
+        } else {
33 33
             $this->logger = "";
34 34
         }
35 35
     }
@@ -41,7 +41,7 @@  discard block
 block discarded – undo
41 41
      */
42 42
     public function get_backup_encryption_key()
43 43
     {
44
-        if(is_object($this->xcloner_settings)) {
44
+        if (is_object($this->xcloner_settings)) {
45 45
             return $this->xcloner_settings->get_xcloner_encryption_key();
46 46
         }
47 47
 
@@ -56,10 +56,10 @@  discard block
 block discarded – undo
56 56
      * @return bool
57 57
      */
58 58
     public function is_encrypted_file($filename) {
59
-        $fp = fopen($this->get_xcloner_path() .$filename, 'r');
59
+        $fp = fopen($this->get_xcloner_path().$filename, 'r');
60 60
         $encryption_length = fread($fp, 16);
61 61
         fclose($fp);
62
-        if(is_numeric($encryption_length)) {
62
+        if (is_numeric($encryption_length)) {
63 63
             return true;
64 64
         }
65 65
 
@@ -73,9 +73,9 @@  discard block
 block discarded – undo
73 73
      * @param $filename
74 74
      * @return string
75 75
      */
76
-    public function get_encrypted_target_backup_file_name( $filename ) {
76
+    public function get_encrypted_target_backup_file_name($filename) {
77 77
 
78
-        return str_replace(self::FILE_DECRYPTION_SUFFIX, "", $filename) . self::FILE_ENCRYPTION_SUFFIX;
78
+        return str_replace(self::FILE_DECRYPTION_SUFFIX, "", $filename).self::FILE_ENCRYPTION_SUFFIX;
79 79
     }
80 80
 
81 81
     /**
@@ -84,9 +84,9 @@  discard block
 block discarded – undo
84 84
      * @param $filename
85 85
      * @return string
86 86
      */
87
-    public function get_decrypted_target_backup_file_name( $filename ) {
87
+    public function get_decrypted_target_backup_file_name($filename) {
88 88
 
89
-        return str_replace(self::FILE_ENCRYPTION_SUFFIX, "", $filename) . self::FILE_DECRYPTION_SUFFIX;
89
+        return str_replace(self::FILE_ENCRYPTION_SUFFIX, "", $filename).self::FILE_DECRYPTION_SUFFIX;
90 90
     }
91 91
 
92 92
     /**
@@ -100,44 +100,44 @@  discard block
 block discarded – undo
100 100
      * @param bool $verification   Weather we should we try to verify the decryption.
101 101
      * @return string|false  Returns the file name that has been created or FALSE if an error occured
102 102
     */
103
-    public function encrypt_file($source, $dest = "" , $key= "", $start = 0, $iv = 0, $verification = true, $recursive = false)
103
+    public function encrypt_file($source, $dest = "", $key = "", $start = 0, $iv = 0, $verification = true, $recursive = false)
104 104
     {
105
-        if(is_object($this->logger)) {
105
+        if (is_object($this->logger)) {
106 106
             $this->logger->info(sprintf('Encrypting file %s at position %d IV %s', $source, $start, base64_encode($iv)));
107 107
         }
108 108
 
109 109
         //$key = substr(sha1($key, true), 0, 16);
110
-        if(!$key){
110
+        if (!$key) {
111 111
             $key = self::get_backup_encryption_key();
112 112
         }
113
-        $key_digest = openssl_digest ($key, "md5", true);
113
+        $key_digest = openssl_digest($key, "md5", true);
114 114
 
115 115
         $keep_local = 1;
116
-        if(!$dest) {
116
+        if (!$dest) {
117 117
             $dest = $this->get_encrypted_target_backup_file_name($source);
118 118
             $keep_local = 0;
119 119
         }
120 120
 
121
-        if(!$iv || !$start) {
121
+        if (!$iv || !$start) {
122 122
             //$iv = openssl_random_pseudo_bytes(16);
123 123
             $iv = str_pad(self::FILE_ENCRYPTION_BLOCKS, 16, "0000000000000000", STR_PAD_LEFT);
124 124
         }
125 125
 
126
-        if( !$start ) {
127
-            $fpOut = fopen($this->get_xcloner_path() .$dest, 'w');
128
-        }else{
129
-            $fpOut = fopen($this->get_xcloner_path() .$dest, 'a');
126
+        if (!$start) {
127
+            $fpOut = fopen($this->get_xcloner_path().$dest, 'w');
128
+        } else {
129
+            $fpOut = fopen($this->get_xcloner_path().$dest, 'a');
130 130
         }
131 131
 
132
-        if ( $fpOut ) {
132
+        if ($fpOut) {
133 133
 
134 134
             // Put the initialization vector to the beginning of the file
135
-            if(!$start) {
135
+            if (!$start) {
136 136
                 fwrite($fpOut, $iv);
137 137
             }
138 138
 
139
-            if (file_exists($this->get_xcloner_path() .$source) &&
140
-                $fpIn = fopen($this->get_xcloner_path() .$source, 'rb')) {
139
+            if (file_exists($this->get_xcloner_path().$source) &&
140
+                $fpIn = fopen($this->get_xcloner_path().$source, 'rb')) {
141 141
 
142 142
                 fseek($fpIn, (int)$start);
143 143
 
@@ -154,17 +154,17 @@  discard block
 block discarded – undo
154 154
 
155 155
                     $start = ftell($fpIn);
156 156
 
157
-                    fclose($fpOut );
157
+                    fclose($fpOut);
158 158
 
159
-                    if(!feof($fpIn)) {
159
+                    if (!feof($fpIn)) {
160 160
                         fclose($fpIn);
161 161
                         //echo "\n NEW:".$key.md5($iv);
162 162
                         //self::encryptFile($source, $dest, $key, $start, $iv);
163
-                        if($recursive ){
163
+                        if ($recursive) {
164 164
                             $this->encrypt_file($source, $dest, $key, $start, ($iv), $verification, $recursive);
165
-                        }else {
165
+                        } else {
166 166
 
167
-                            if(($iv) != base64_decode(base64_encode($iv)))
167
+                            if (($iv) != base64_decode(base64_encode($iv)))
168 168
                             {
169 169
                                 throw new \Exception('Could not encode IV for transport');
170 170
                             }
@@ -180,26 +180,26 @@  discard block
 block discarded – undo
180 180
 
181 181
                 }
182 182
             } else {
183
-                if(is_object($this->logger)) {
183
+                if (is_object($this->logger)) {
184 184
                     $this->logger->error('Unable to read source file for encryption.');
185 185
                 }
186 186
                 throw new \Exception("Unable to read source file for encryption.");
187 187
             }
188 188
         } else {
189
-            if(is_object($this->logger)) {
189
+            if (is_object($this->logger)) {
190 190
                 $this->logger->error('Unable to write destination file for encryption.');
191 191
             }
192 192
             throw new \Exception("Unable to write destination file for encryption.");
193 193
         }
194 194
 
195
-        if($verification) {
195
+        if ($verification) {
196 196
             $this->verify_encrypted_file($dest);
197 197
         }
198 198
 
199 199
         //we replace the original backup with the encrypted one
200
-        if( !$keep_local && copy($this->get_xcloner_path() .$dest,
201
-             $this->get_xcloner_path() .$source) ) {
202
-            unlink($this->get_xcloner_path() .$dest);
200
+        if (!$keep_local && copy($this->get_xcloner_path().$dest,
201
+             $this->get_xcloner_path().$source)) {
202
+            unlink($this->get_xcloner_path().$dest);
203 203
         }
204 204
 
205 205
 
@@ -207,7 +207,7 @@  discard block
 block discarded – undo
207 207
     }
208 208
 
209 209
     public function verify_encrypted_file($file) {
210
-        if(is_object($this->logger)) {
210
+        if (is_object($this->logger)) {
211 211
             $this->logger->info(sprintf('Verifying encrypted file %s', $file));
212 212
         }
213 213
 
@@ -229,50 +229,50 @@  discard block
 block discarded – undo
229 229
      */
230 230
     public function decrypt_file($source, $dest = "", $key = "", $start = 0, $iv = 0, $recursive = false)
231 231
     {
232
-        if(is_object($this->logger)) {
232
+        if (is_object($this->logger)) {
233 233
             $this->logger->info(sprintf('Decrypting file %s at position %d with IV %s', $source, $start, base64_encode($iv)));
234 234
         }
235 235
 
236 236
         //$key = substr(sha1($key, true), 0, 16);
237
-        if(!$key){
237
+        if (!$key) {
238 238
             $key = self::get_backup_encryption_key();
239 239
         }
240 240
 
241
-        $key_digest = openssl_digest ($key, "md5", true);
241
+        $key_digest = openssl_digest($key, "md5", true);
242 242
 
243 243
         $keep_local = 1;
244
-        if(!$dest) {
244
+        if (!$dest) {
245 245
             $dest = $this->get_decrypted_target_backup_file_name($source);
246 246
             $keep_local = 0;
247 247
         }
248 248
 
249
-        if( !$start ) {
250
-            if($this->verification){
249
+        if (!$start) {
250
+            if ($this->verification) {
251 251
                 $fpOut = fopen("php://stdout", 'w');
252
-            }else {
253
-                $fpOut = fopen($this->get_xcloner_path() . $dest, 'w');
252
+            } else {
253
+                $fpOut = fopen($this->get_xcloner_path().$dest, 'w');
254 254
             }
255
-        }else{
256
-            if($this->verification){
255
+        } else {
256
+            if ($this->verification) {
257 257
                 $fpOut = fopen("php://stdout", 'a');
258
-            }else {
259
-                $fpOut = fopen($this->get_xcloner_path() . $dest, 'a');
258
+            } else {
259
+                $fpOut = fopen($this->get_xcloner_path().$dest, 'a');
260 260
             }
261 261
         }
262 262
 
263
-        if ( $fpOut ) {
264
-            if ( file_exists($this->get_xcloner_path() .$source) &&
265
-                $fpIn = fopen($this->get_xcloner_path() .$source, 'rb')) {
263
+        if ($fpOut) {
264
+            if (file_exists($this->get_xcloner_path().$source) &&
265
+                $fpIn = fopen($this->get_xcloner_path().$source, 'rb')) {
266 266
 
267 267
                 $encryption_length = (int)fread($fpIn, 16);
268
-                if(!$encryption_length) {
268
+                if (!$encryption_length) {
269 269
                     $encryption_length = self::FILE_ENCRYPTION_BLOCKS;
270 270
                 }
271 271
 
272 272
                 fseek($fpIn, (int)$start);
273 273
 
274 274
                 // Get the initialzation vector from the beginning of the file
275
-                if(!$iv) {
275
+                if (!$iv) {
276 276
                     $iv = fread($fpIn, 16);
277 277
                 }
278 278
 
@@ -282,9 +282,9 @@  discard block
 block discarded – undo
282 282
                     $ciphertext = fread($fpIn, 16 * ($encryption_length + 1));
283 283
                     $plaintext = openssl_decrypt($ciphertext, 'AES-128-CBC', $key_digest, OPENSSL_RAW_DATA, $iv);
284 284
 
285
-                    if(!$plaintext){
286
-                        unlink($this->get_xcloner_path() . $dest);
287
-                        if(is_object($this->logger)) {
285
+                    if (!$plaintext) {
286
+                        unlink($this->get_xcloner_path().$dest);
287
+                        if (is_object($this->logger)) {
288 288
                             $this->logger->error('Backup decryption failed, please check your provided Encryption Key.');
289 289
                         }
290 290
                         throw new \Exception("Backup decryption failed, please check your provided Encryption Key.");
@@ -293,22 +293,22 @@  discard block
 block discarded – undo
293 293
                     // Use the first 16 bytes of the ciphertext as the next initialization vector
294 294
                     $iv = substr($ciphertext, 0, 16);
295 295
 
296
-                    if( !$this->verification) {
296
+                    if (!$this->verification) {
297 297
                         fwrite($fpOut, $plaintext);
298 298
                     }
299 299
 
300 300
                     $start = ftell($fpIn);
301 301
 
302
-                    fclose($fpOut );
302
+                    fclose($fpOut);
303 303
 
304
-                    if(!feof($fpIn)) {
304
+                    if (!feof($fpIn)) {
305 305
                         fclose($fpIn);
306
-                        if($this->verification || $recursive) {
306
+                        if ($this->verification || $recursive) {
307 307
                             $ciphertext = "";
308 308
                             $plaintext = "";
309 309
                             $this->decrypt_file($source, $dest, $key, $start, $iv, $recursive);
310
-                        }else {
311
-                            if(($iv) != base64_decode(base64_encode($iv)))
310
+                        } else {
311
+                            if (($iv) != base64_decode(base64_encode($iv)))
312 312
                             {
313 313
                                 throw new \Exception('Could not encode IV for transport');
314 314
                             }
@@ -325,30 +325,30 @@  discard block
 block discarded – undo
325 325
 
326 326
                 }
327 327
             } else {
328
-                if(is_object($this->logger)) {
328
+                if (is_object($this->logger)) {
329 329
                     $this->logger->error('Unable to read source file for decryption');
330 330
                 }
331 331
                 throw new \Exception("Unable to read source file for decryption");
332 332
             }
333 333
         } else {
334
-            if(is_object($this->logger)) {
334
+            if (is_object($this->logger)) {
335 335
                 $this->logger->error('Unable to write destination file for decryption');
336 336
             }
337 337
             throw new \Exception("Unable to write destination file for decryption");
338 338
         }
339 339
 
340 340
         //we replace the original backup with the encrypted one
341
-        if( !$keep_local && !$this->verification && copy($this->get_xcloner_path()  .$dest,
342
-            $this->get_xcloner_path()  .$source) ) {
343
-            unlink($this->get_xcloner_path()  .$dest);
341
+        if (!$keep_local && !$this->verification && copy($this->get_xcloner_path().$dest,
342
+            $this->get_xcloner_path().$source)) {
343
+            unlink($this->get_xcloner_path().$dest);
344 344
         }
345 345
 
346 346
         return array("target_file" => $dest, "finished" => 1);
347 347
     }
348 348
 
349
-    public function get_xcloner_path(){
350
-        if(is_object($this->xcloner_settings)) {
351
-            return $this->xcloner_settings->get_xcloner_store_path() . DS;
349
+    public function get_xcloner_path() {
350
+        if (is_object($this->xcloner_settings)) {
351
+            return $this->xcloner_settings->get_xcloner_store_path().DS;
352 352
         }
353 353
 
354 354
         return null;
@@ -359,21 +359,21 @@  discard block
 block discarded – undo
359 359
 
360 360
 try {
361 361
 
362
-    if(isset($argv[1])) {
362
+    if (isset($argv[1])) {
363 363
 
364 364
         class Xcloner {
365 365
             public function __construct()
366 366
             {
367 367
             }
368 368
         }
369
-        $xcloner_encryption = new Xcloner_Encryption( new Xcloner() );
369
+        $xcloner_encryption = new Xcloner_Encryption(new Xcloner());
370 370
 
371 371
         if ($argv[1] == "-e") {
372
-            $xcloner_encryption->encrypt_file($argv[2], $argv[2] . ".enc", $argv[4], '', '', '', true);
372
+            $xcloner_encryption->encrypt_file($argv[2], $argv[2].".enc", $argv[4], '', '', '', true);
373 373
         } elseif ($argv[1] == "-d") {
374
-            $xcloner_encryption->decrypt_file($argv[2], $argv[2] . ".dec", $argv[4], '', '', true);
374
+            $xcloner_encryption->decrypt_file($argv[2], $argv[2].".dec", $argv[4], '', '', true);
375 375
         }
376 376
     }
377
-}catch(\Exception $e) {
378
-    echo "CAUGHT: " . $e->getMessage();
377
+}catch (\Exception $e) {
378
+    echo "CAUGHT: ".$e->getMessage();
379 379
 }
Please login to merge, or discard this patch.