Completed
Push — 14.2 ( f19788...0130a1 )
by Ralf
28:55
created
phpgwapi/inc/class.db_backup.inc.php 2 patches
Spacing   +157 added lines, -157 removed lines patch added patch discarded remove patch
@@ -46,9 +46,9 @@  discard block
 block discarded – undo
46 46
 	 * @var array
47 47
 	 */
48 48
 	var $exclude_tables = array(
49
-		'egw_sessions','egw_app_sessions','phpgw_sessions','phpgw_app_sessions',	// eGW's session-tables
50
-		'phpgw_anglemail',	// email's cache
51
-		'egw_felamimail_cache','egw_felamimail_folderstatus','phpgw_felamimail_cache','phpgw_felamimail_folderstatus',	// felamimail's cache
49
+		'egw_sessions', 'egw_app_sessions', 'phpgw_sessions', 'phpgw_app_sessions', // eGW's session-tables
50
+		'phpgw_anglemail', // email's cache
51
+		'egw_felamimail_cache', 'egw_felamimail_folderstatus', 'phpgw_felamimail_cache', 'phpgw_felamimail_folderstatus', // felamimail's cache
52 52
 		'egw_phpfreechat', // as of the fieldnames of the table a restore would fail within egroupware, and chatcontent is of no particular intrest
53 53
 	);
54 54
 	/**
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
 	 *
81 81
 	 * @var boolean
82 82
 	 */
83
-	var $backup_files = false ;
83
+	var $backup_files = false;
84 84
 	/**
85 85
 	 * Reference to schema_proc's egw_db object
86 86
 	 *
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 	{
96 96
 		if (isset($GLOBALS['egw_setup']) && is_object($GLOBALS['egw_setup']) && !isset($GLOBALS['egw_setup']->db))
97 97
 		{
98
-			$GLOBALS['egw_setup']->loaddb();	// we run inside setup, but db object is not loaded
98
+			$GLOBALS['egw_setup']->loaddb(); // we run inside setup, but db object is not loaded
99 99
 		}
100 100
 		if (isset($GLOBALS['egw_setup']->oProc) && is_object($GLOBALS['egw_setup']->oProc))	// schema_proc already instanciated, use it
101 101
 		{
@@ -113,37 +113,37 @@  discard block
 block discarded – undo
113 113
 		{
114 114
 			if ($GLOBALS['egw_setup']->config_table && $GLOBALS['egw_setup']->table_exist(array($GLOBALS['egw_setup']->config_table)))
115 115
 			{
116
-				$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='backup_dir'",__LINE__,__FILE__);
116
+				$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='backup_dir'", __LINE__, __FILE__);
117 117
 				$this->db->next_record();
118 118
 				if (!($this->backup_dir = $this->db->f(0)))
119 119
 				{
120
-					$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='files_dir'",__LINE__,__FILE__);
120
+					$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='files_dir'", __LINE__, __FILE__);
121 121
 					$this->db->next_record();
122 122
 					$this->backup_dir = $this->db->f(0).'/db_backup';
123 123
 				}
124
-				$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='files_dir'",__LINE__,__FILE__);
124
+				$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='files_dir'", __LINE__, __FILE__);
125 125
 				$this->db->next_record();
126 126
 				if (!($this->files_dir = $this->db->f(0)))
127 127
 				{
128 128
 					error_log(__METHOD__."->"."No files Directory set/found");
129 129
 				}
130
-				$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='system_charset'",__LINE__,__FILE__);
130
+				$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='system_charset'", __LINE__, __FILE__);
131 131
 				$this->db->next_record();
132 132
 				$this->charset = $this->db->f(0);
133 133
 				if (!$this->charset)
134 134
 				{
135
-					$this->db->query("SELECT content FROM {$GLOBALS['egw_setup']->lang_table} WHERE message_id='charset' AND app_name='common' AND lang!='en'",__LINE__,__FILE__);
135
+					$this->db->query("SELECT content FROM {$GLOBALS['egw_setup']->lang_table} WHERE message_id='charset' AND app_name='common' AND lang!='en'", __LINE__, __FILE__);
136 136
 					$this->db->next_record();
137 137
 					$this->charset = $this->db->f(0);
138 138
 				}
139
-				$this->db->select($GLOBALS['egw_setup']->applications_table,'app_version',array('app_name'=>'phpgwapi'),__LINE__,__FILE__);
139
+				$this->db->select($GLOBALS['egw_setup']->applications_table, 'app_version', array('app_name'=>'phpgwapi'), __LINE__, __FILE__);
140 140
 				$this->api_version = $this->db->next_record() ? $this->db->f(0) : false;
141 141
 				/* Backup settings */
142
-				$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='backup_mincount'",__LINE__,__FILE__);
142
+				$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='backup_mincount'", __LINE__, __FILE__);
143 143
 				$this->db->next_record();
144 144
 				$this->backup_mincount = $this->db->f(0);
145 145
 				// backup files too
146
-				$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='backup_files'",__LINE__,__FILE__);
146
+				$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='backup_files'", __LINE__, __FILE__);
147 147
 				$this->db->next_record();
148 148
 				$this->backup_files = (bool)$this->db->f(0);
149 149
 			}
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
 		{
178 178
 			mkdir($this->backup_dir);
179 179
 		}
180
-		switch($this->db->Type)
180
+		switch ($this->db->Type)
181 181
 		{
182 182
 			case 'sapdb':
183 183
 			case 'maxdb':
@@ -194,7 +194,7 @@  discard block
 block discarded – undo
194 194
 	 * @param $reading =false opening for reading ('rb') or writing ('wb')
195 195
 	 * @return string/resource/zip error-msg of file-handle
196 196
 	 */
197
-	function fopen_backup($name=false,$reading=false)
197
+	function fopen_backup($name = false, $reading = false)
198 198
 	{
199 199
 		//echo "function fopen_backup($name,$reading)<br>";	// !
200 200
 		if (!$name)
@@ -203,27 +203,27 @@  discard block
 block discarded – undo
203 203
 			if (!$this->backup_dir || !is_writable($this->backup_dir))
204 204
 			{
205 205
 				//echo '   -> !$this->backup_dir || !is_writable($this->backup_dir)<br>';	// !
206
-				return lang("backupdir '%1' is not writeable by the webserver",$this->backup_dir);
206
+				return lang("backupdir '%1' is not writeable by the webserver", $this->backup_dir);
207 207
 			}
208 208
 			$name = $this->backup_dir.'/db_backup-'.date('YmdHi');
209 209
 		}
210 210
 		else	// remove the extension, to use the correct wrapper based on the extension
211 211
 		{
212 212
 			//echo '-> else<br>';	// !
213
-			$name = preg_replace('/\.(bz2|gz)$/i','',$name);
213
+			$name = preg_replace('/\.(bz2|gz)$/i', '', $name);
214 214
 		}
215 215
 		$mode = $reading ? 'rb' : 'wb';
216
-		list( , $type) = explode('.', basename($name));
217
-		if($type == 'zip' && $reading && $this->backup_files)
216
+		list(, $type) = explode('.', basename($name));
217
+		if ($type == 'zip' && $reading && $this->backup_files)
218 218
 		{
219 219
 			//echo '-> $type == "zip" && $reading<br>';	// !
220
-			if(!class_exists('ZipArchive', false))
220
+			if (!class_exists('ZipArchive', false))
221 221
 			{
222 222
 				$this->backup_files = false;
223 223
 				//echo '   -> (new ZipArchive) == NULL<br>';	// !
224 224
 				return lang("Cant open %1, needs ZipArchive", $name)."<br>\n";
225 225
 			}
226
-			if(!($f = fopen($name, $mode)))
226
+			if (!($f = fopen($name, $mode)))
227 227
 			{
228 228
 				//echo '   -> !($f = fopen($name, $mode))<br>';	// !
229 229
 				$lang_mode = $reading ? lang("reading") : lang("writing");
@@ -231,10 +231,10 @@  discard block
 block discarded – undo
231 231
 			}
232 232
 			return $f;
233 233
 		}
234
-		if(class_exists('ZipArchive', false) && !$reading && $this->backup_files)
234
+		if (class_exists('ZipArchive', false) && !$reading && $this->backup_files)
235 235
 		{
236 236
 			//echo '-> (new ZipArchive) != NULL && !$reading; '.$name.'<br>';	// !
237
-			if(!($f = fopen($name, $mode)))
237
+			if (!($f = fopen($name, $mode)))
238 238
 			{
239 239
 				//echo '   -> !($f = fopen($name, $mode))<br>';	// !
240 240
 				$lang_mode = $reading ? lang("reading") : lang("writing");
@@ -242,9 +242,9 @@  discard block
 block discarded – undo
242 242
 			}
243 243
 			return $f;
244 244
 		}
245
-		if(!($f = fopen("compress.bzip2://$name.bz2", $mode)) &&
246
-	 		!($f = fopen("compress.zlib://$name.gz",$mode)) &&
247
- 		 	!($f = fopen($name,$mode))
245
+		if (!($f = fopen("compress.bzip2://$name.bz2", $mode)) &&
246
+	 		!($f = fopen("compress.zlib://$name.gz", $mode)) &&
247
+ 		 	!($f = fopen($name, $mode))
248 248
 		)
249 249
 		{
250 250
 			//echo '-> !($f = fopen("compress.bzip2://$name.bz2", $mode))<br>';	// !
@@ -271,13 +271,13 @@  discard block
 block discarded – undo
271 271
 		/* Search the backup directory for matching files. */
272 272
 		$handle = @opendir($this->backup_dir);
273 273
 		$files = array();
274
-		while($handle && ($file = readdir($handle)))
274
+		while ($handle && ($file = readdir($handle)))
275 275
 		{
276 276
 			/* Filter for only the files with the regular name (un-renamed).
277 277
 			 * Leave special backup files (renamed) in place.
278 278
 			 * Note that this also excludes "." and "..".
279 279
 			 */
280
-			if (preg_match("/^db_backup-[0-9]{12}(\.bz2|\.gz|\.zip|)$/",$file))
280
+			if (preg_match("/^db_backup-[0-9]{12}(\.bz2|\.gz|\.zip|)$/", $file))
281 281
 			{
282 282
 				$files[filectime($this->backup_dir.'/'.$file)] = $file;
283 283
 			}
@@ -287,7 +287,7 @@  discard block
 block discarded – undo
287 287
 		/* Sort the files by ctime. */
288 288
 		krsort($files);
289 289
 		$count = 0;
290
-		foreach($files as $file)
290
+		foreach ($files as $file)
291 291
 		{
292 292
 			if ($count >= $this->backup_mincount)//
293 293
 			{
@@ -297,7 +297,7 @@  discard block
 block discarded – undo
297 297
 					array_push($files_return, $file);
298 298
 				}
299 299
 			}
300
-			$count ++;
300
+			$count++;
301 301
 		}
302 302
 	}
303 303
 
@@ -307,13 +307,13 @@  discard block
 block discarded – undo
307 307
 	 * @param int $minCount Minimum number of backups to keep.
308 308
 	 * @param boolean $backupFiles include files in backup or not, default dont change!
309 309
 	 */
310
-	function saveConfig($minCount,$backupFiles=null)
310
+	function saveConfig($minCount, $backupFiles = null)
311 311
 	{
312
-		config::save_value('backup_mincount',$this->backup_mincount=(int)$minCount,'phpgwapi');
312
+		config::save_value('backup_mincount', $this->backup_mincount = (int)$minCount, 'phpgwapi');
313 313
 
314 314
 		if (!is_null($backupFiles))
315 315
 		{
316
-			config::save_value('backup_files',$this->backup_files=(boolean)$backupFiles,'phpgwapi');
316
+			config::save_value('backup_files', $this->backup_files = (boolean)$backupFiles, 'phpgwapi');
317 317
 		}
318 318
 	}
319 319
 
@@ -336,7 +336,7 @@  discard block
 block discarded – undo
336 336
 		'httpproxy_server_password',
337 337
 		'system_charset',
338 338
 		'usecookies',
339
-		'install_id',	// do not restore install_id, as that would give two systems with identical install_id
339
+		'install_id', // do not restore install_id, as that would give two systems with identical install_id
340 340
 	);
341 341
 
342 342
 	/**
@@ -350,20 +350,20 @@  discard block
 block discarded – undo
350 350
 	 *
351 351
 	 * @returns An empty string or an error message in case of failure.
352 352
 	 */
353
-	function restore($f,$convert_to_system_charset=true,$filename='',$protect_system_config=true, $insert_n_rows=10)
353
+	function restore($f, $convert_to_system_charset = true, $filename = '', $protect_system_config = true, $insert_n_rows = 10)
354 354
 	{
355 355
 		@set_time_limit(0);
356
-		ini_set('auto_detect_line_endings',true);
356
+		ini_set('auto_detect_line_endings', true);
357 357
 
358
-		if (true) $convert_to_system_charset = true;	// enforce now utf-8 as system charset restores of old backups
358
+		if (true) $convert_to_system_charset = true; // enforce now utf-8 as system charset restores of old backups
359 359
 
360 360
 		if ($protect_system_config)
361 361
 		{
362 362
 			$system_config = array();
363
-			foreach($this->db->select(self::TABLE,'*',array(
363
+			foreach ($this->db->select(self::TABLE, '*', array(
364 364
 				'config_app' => 'phpgwapi',
365 365
 				'config_name' => self::$system_config,
366
-			),__LINE__,__FILE__) as $row)
366
+			), __LINE__, __FILE__) as $row)
367 367
 			{
368 368
 				$system_config[] = $row;
369 369
 			}
@@ -372,7 +372,7 @@  discard block
 block discarded – undo
372 372
 		// as MySQL 5.7+ has sql_mode STRICT_(ALL|TRANS)_TABLES enabled by default,
373 373
 		// it will no longer restore '0000-00-00 00:00:00' in timestamps it created before,
374 374
 		// so switching strict-mode off temporary for the restore (we dont create these!)
375
-		if (substr($this->db->Type,0,5) == 'mysql')
375
+		if (substr($this->db->Type, 0, 5) == 'mysql')
376 376
 		{
377 377
 			$this->db->query("SET SESSION sql_mode=(SELECT REPLACE(REPLACE(@@sql_mode,'STRICT_ALL_TABLES',''),'STRICT_TRANS_TABLES',''))", __LINE__, __FILE__);
378 378
 		}
@@ -382,17 +382,17 @@  discard block
 block discarded – undo
382 382
 		}
383 383
 
384 384
 		// drop all existing tables
385
-		foreach($this->adodb->MetaTables('TABLES') as $table)
385
+		foreach ($this->adodb->MetaTables('TABLES') as $table)
386 386
 		{
387
-			if ($this->system_tables && preg_match($this->system_tables,$table) ||
388
-				$this->egw_tables && !preg_match($this->egw_tables,$table))
387
+			if ($this->system_tables && preg_match($this->system_tables, $table) ||
388
+				$this->egw_tables && !preg_match($this->egw_tables, $table))
389 389
 			{
390 390
 				 continue;
391 391
 			}
392 392
 			$this->schema_proc->DropTable($table);
393 393
 		}
394 394
 		// it could be an old backup
395
-		list( , $type) = explode('.', basename($filename));
395
+		list(, $type) = explode('.', basename($filename));
396 396
 		$dir = $this->files_dir; // $GLOBALS['egw_info']['server']['files_dir'];
397 397
 		// we may have to clean up old backup - left overs
398 398
 		if (is_dir($dir.'/database_backup'))
@@ -405,20 +405,20 @@  discard block
 block discarded – undo
405 405
 		$name = "";
406 406
 		$zip = NULL;
407 407
 		$_f = NULL;
408
-		if($type == 'zip')
408
+		if ($type == 'zip')
409 409
 	    {
410 410
 			// has already been verified to be available in fopen_backup
411 411
 			$zip = new ZipArchive;
412
-			if(($zip->open($filename)) !== TRUE)
412
+			if (($zip->open($filename)) !== TRUE)
413 413
 			{
414 414
 				return lang("Cant open '%1' for %2", $filename, lang("reading"))."<br>\n";
415 415
 			}
416
-			self::remove_dir_content($dir);  // removes the files-dir
416
+			self::remove_dir_content($dir); // removes the files-dir
417 417
 			$zip->extractTo($dir);
418 418
 			$_f = $f;
419 419
 			$list = $this->get_file_list($dir.'/database_backup/');
420 420
 			$name = $dir.'/database_backup/'.basename($list[0]);
421
-			if(!($f = fopen($name, 'rb')))
421
+			if (!($f = fopen($name, 'rb')))
422 422
 			{
423 423
 				return lang("Cant open '%1' for %2", $filename, lang("reading"))."<br>\n";
424 424
 			}
@@ -435,46 +435,46 @@  discard block
 block discarded – undo
435 435
 		{
436 436
 			$this->db->insert(config::TABLE, array(
437 437
 				'config_value' => $this->schema_proc->system_charset,
438
-			),array(
438
+			), array(
439 439
 				'config_app' => 'phpgwapi',
440 440
 				'config_name' => 'system_charset',
441
-			),__LINE__,__FILE__);
441
+			), __LINE__, __FILE__);
442 442
 		}
443 443
 		// restore protected system config
444 444
 		if ($protect_system_config)
445 445
 		{
446
-			foreach($system_config as $row)
446
+			foreach ($system_config as $row)
447 447
 			{
448
-				$this->db->insert(self::TABLE,array('config_value'=>$row['config_value']),array(
448
+				$this->db->insert(self::TABLE, array('config_value'=>$row['config_value']), array(
449 449
 					'config_name' => $row['config_name'],
450 450
 					'config_app'  => $row['config_app'],
451
-				),__LINE__,__FILE__);
451
+				), __LINE__, __FILE__);
452 452
 			}
453 453
 			// check and reset cookie configuration, if it does not match current enviroment
454 454
 			// if $_SERVER[HTTP_HOST] does not end with cookiedomain --> delete cookiedomain
455
-			if (($cookiedomain = $this->db->select(self::TABLE,'config_value',array(
455
+			if (($cookiedomain = $this->db->select(self::TABLE, 'config_value', array(
456 456
 					'config_app' => 'phpgwapi',
457 457
 					'config_name' => 'cookiedomain',
458
-				),__LINE__,__FILE__)->fetchColumn()) && isset($_SERVER['HTTP_HOST']) &&
459
-				(list($hostname) = explode(':',$_SERVER['HTTP_HOST'])) &&
460
-				substr($hostname,-strlen($cookiedomain) !== $cookiedomain))
458
+				), __LINE__, __FILE__)->fetchColumn()) && isset($_SERVER['HTTP_HOST']) &&
459
+				(list($hostname) = explode(':', $_SERVER['HTTP_HOST'])) &&
460
+				substr($hostname, -strlen($cookiedomain) !== $cookiedomain))
461 461
 			{
462
-				$this->db->delete(self::TABLE,array(
462
+				$this->db->delete(self::TABLE, array(
463 463
 					'config_app' => 'phpgwapi',
464 464
 					'config_name' => 'cookiedomain',
465
-				),__LINE__,__FILE__);
465
+				), __LINE__, __FILE__);
466 466
 			}
467 467
 			// if configured webserver_url does NOT start with cookiepath --> delete cookiepath
468
-			if (($cookiepath = $this->db->select(self::TABLE,'config_value',array(
468
+			if (($cookiepath = $this->db->select(self::TABLE, 'config_value', array(
469 469
 					'config_app' => 'phpgwapi',
470 470
 					'config_name' => 'cookiepath',
471
-				),__LINE__,__FILE__)->fetchColumn()) &&
472
-				substr(parse_url($system_config['webserver_url'], PHP_URL_PATH),0,strlen($cookiepath) !== $cookiepath))
471
+				), __LINE__, __FILE__)->fetchColumn()) &&
472
+				substr(parse_url($system_config['webserver_url'], PHP_URL_PATH), 0, strlen($cookiepath) !== $cookiepath))
473 473
 			{
474
-				$this->db->delete(self::TABLE,array(
474
+				$this->db->delete(self::TABLE, array(
475 475
 					'config_app' => 'phpgwapi',
476 476
 					'config_name' => 'cookiepath',
477
-				),__LINE__,__FILE__);
477
+				), __LINE__, __FILE__);
478 478
 			}
479 479
 		}
480 480
 		// restore original Halt_On_Error state (if changed)
@@ -483,13 +483,13 @@  discard block
 block discarded – undo
483 483
 			$this->db->Halt_On_Error = $backup_db_halt_on_error;
484 484
 		}
485 485
 		// zip?
486
-		if($type == 'zip')
486
+		if ($type == 'zip')
487 487
 		{
488 488
 			fclose($f);
489 489
 			unlink($name);
490 490
 			rmdir($dir.'/database_backup');
491 491
 		}
492
-		if (substr($this->db->Type,0,5) != 'mysql')
492
+		if (substr($this->db->Type, 0, 5) != 'mysql')
493 493
 		{
494 494
 			if (!$this->db->transaction_commit())
495 495
 			{
@@ -524,26 +524,26 @@  discard block
 block discarded – undo
524 524
 	 * @param int|string $insert_n_rows =10 how many rows to insert in one sql statement, or string with column-name used as unique key for insert
525 525
 	 * @returns int number of rows read from csv file
526 526
 	 */
527
-	function db_restore($f, $insert_n_rows=10)
527
+	function db_restore($f, $insert_n_rows = 10)
528 528
 	{
529 529
 		$convert_to_system_charset = true;
530 530
 		$table = False;
531 531
 		$n = 0;
532 532
 		$rows = array();
533
-		while(!feof($f))
533
+		while (!feof($f))
534 534
 		{
535 535
 			$line = trim(fgets($f)); ++$n;
536 536
 
537 537
 			if (empty($line)) continue;
538 538
 
539
-			if (substr($line,0,9) == 'version: ')
539
+			if (substr($line, 0, 9) == 'version: ')
540 540
 			{
541
-				$api_version = trim(substr($line,9));
541
+				$api_version = trim(substr($line, 9));
542 542
 				continue;
543 543
 			}
544
-			if (substr($line,0,9) == 'charset: ')
544
+			if (substr($line, 0, 9) == 'charset: ')
545 545
 			{
546
-				$charset = trim(substr($line,9));
546
+				$charset = trim(substr($line, 9));
547 547
 				// needed if mbstring.func_overload > 0, else eg. substr does not work with non ascii chars
548 548
 				$ini_default_charset = version_compare(PHP_VERSION, '5.6', '<') ? 'mbstring.internal_encoding' : 'default_charset';
549 549
 				@ini_set($ini_default_charset, $charset);
@@ -551,29 +551,29 @@  discard block
 block discarded – undo
551 551
 				// check if we really need to convert the charset, as it's not perfect and can do some damage
552 552
 				if ($convert_to_system_charset && !strcasecmp($this->schema_proc->system_charset, $charset))
553 553
 				{
554
-					$convert_to_system_charset = false;	// no conversation necessary
554
+					$convert_to_system_charset = false; // no conversation necessary
555 555
 				}
556 556
 				// set the DB's client encoding (for mysql only if api_version >= 1.0.1.019)
557 557
 				if ((!$convert_to_system_charset || $this->db->capabilities['client_encoding']) &&
558
-					(substr($this->db->Type,0,5) != 'mysql' || !is_object($GLOBALS['egw_setup']) ||
559
-					$api_version && !$GLOBALS['egw_setup']->alessthanb($api_version,'1.0.1.019')))
558
+					(substr($this->db->Type, 0, 5) != 'mysql' || !is_object($GLOBALS['egw_setup']) ||
559
+					$api_version && !$GLOBALS['egw_setup']->alessthanb($api_version, '1.0.1.019')))
560 560
 				{
561 561
 					$this->db->Link_ID->SetCharSet($charset);
562 562
 					if (!$convert_to_system_charset)
563 563
 					{
564
-						$this->schema_proc->system_charset = $charset;	// so schema_proc uses it for the creation of the tables
564
+						$this->schema_proc->system_charset = $charset; // so schema_proc uses it for the creation of the tables
565 565
 					}
566 566
 				}
567 567
 				continue;
568 568
 			}
569
-			if (substr($line,0,8) == 'schema: ')
569
+			if (substr($line, 0, 8) == 'schema: ')
570 570
 			{
571 571
 				// create the tables in the backup set
572
-				$this->schemas = json_php_unserialize(trim(substr($line,8)));
573
-				foreach($this->schemas as $table_name => $schema)
572
+				$this->schemas = json_php_unserialize(trim(substr($line, 8)));
573
+				foreach ($this->schemas as $table_name => $schema)
574 574
 				{
575 575
 					// if column is longtext in current schema, convert text to longtext, in case user already updated column
576
-					foreach($schema['fd'] as $col => &$def)
576
+					foreach ($schema['fd'] as $col => &$def)
577 577
 					{
578 578
 						if ($def['type'] == 'text' && $this->db->get_column_attribute($col, $table_name, true, 'type') == 'longtext')
579 579
 						{
@@ -585,27 +585,27 @@  discard block
 block discarded – undo
585 585
 				}
586 586
 				continue;
587 587
 			}
588
-			if (substr($line,0,7) == 'table: ')
588
+			if (substr($line, 0, 7) == 'table: ')
589 589
 			{
590 590
 				if ($rows)	// flush pending rows of last table
591 591
 				{
592 592
 					$this->insert_multiple($table, $rows, $this->schemas[$table]);
593 593
 				}
594 594
 				$rows = array();
595
-				$table = substr($line,7);
595
+				$table = substr($line, 7);
596 596
 				if (!isset($this->schemas[$table])) $this->schemas[$table] = $this->db->get_table_definitions(true, $table);
597 597
 				$auto_id = count($this->schemas[$table]['pk']) == 1 ? $this->schemas[$table]['pk'][0] : null;
598 598
 
599
-				$cols = self::csv_split($line=fgets($f)); ++$n;
599
+				$cols = self::csv_split($line = fgets($f)); ++$n;
600 600
 				$blobs = array();
601
-				foreach($this->schemas[$table]['fd'] as $col => $data)
601
+				foreach ($this->schemas[$table]['fd'] as $col => $data)
602 602
 				{
603 603
 					if ($data['type'] == 'blob') $blobs[] = $col;
604 604
 				}
605 605
 				// check if we have an old PostgreSQL backup useing 't'/'f' for bool values
606 606
 				// --> convert them to MySQL and our new PostgreSQL format of 1/0
607 607
 				$bools = array();
608
-				foreach($this->schemas[$table]['fd'] as $col => $def)
608
+				foreach ($this->schemas[$table]['fd'] as $col => $def)
609 609
 				{
610 610
 					if ($def['type'] === 'bool') $bools[] = $col;
611 611
 				}
@@ -629,15 +629,15 @@  discard block
 block discarded – undo
629 629
 				$import = true;
630 630
 				$data = self::csv_split($line, $cols, $blobs, $bools);
631 631
 
632
-				if ($table == 'egw_async' && in_array('##last-check-run##',$data))
632
+				if ($table == 'egw_async' && in_array('##last-check-run##', $data))
633 633
 				{
634 634
 					//echo '<p>'.lang("Line %1: '%2'<br><b>csv data does contain ##last-check-run## of table %3 ==> ignored</b>",$n,$line,$table)."</p>\n";
635 635
 					//echo 'data=<pre>'.print_r($data,true)."</pre>\n";
636 636
 					$import = false;
637 637
 				}
638
-				if (in_array($table,$this->exclude_tables))
638
+				if (in_array($table, $this->exclude_tables))
639 639
 				{
640
-					echo '<p><b>'.lang("Table %1 is excluded from backup and restore. Data will not be restored.",$table)."</b></p>\n";
640
+					echo '<p><b>'.lang("Table %1 is excluded from backup and restore. Data will not be restored.", $table)."</b></p>\n";
641 641
 					$import = false; // dont restore data of excluded tables
642 642
 				}
643 643
 				if ($import)
@@ -646,7 +646,7 @@  discard block
 block discarded – undo
646 646
 					{
647 647
 						if ($convert_to_system_charset && !$this->db->capabilities['client_encoding'])
648 648
 						{
649
-							$data = translation::convert($data,$charset);
649
+							$data = translation::convert($data, $charset);
650 650
 						}
651 651
 						if ($insert_n_rows > 1)
652 652
 						{
@@ -658,27 +658,27 @@  discard block
 block discarded – undo
658 658
 							}
659 659
 						}
660 660
 						// update existing table using given unique key in $insert_n_rows (also removing auto-id/sequence)
661
-						elseif(!is_numeric($insert_n_rows))
661
+						elseif (!is_numeric($insert_n_rows))
662 662
 						{
663 663
 							$where = array($insert_n_rows => $data[$insert_n_rows]);
664 664
 							unset($data[$insert_n_rows]);
665 665
 							if ($auto_id) unset($data[$auto_id]);
666
-							$this->db->insert($table,$data,$where,__LINE__,__FILE__,false,false,$this->schemas[$table]);
666
+							$this->db->insert($table, $data, $where, __LINE__, __FILE__, false, false, $this->schemas[$table]);
667 667
 						}
668 668
 						else
669 669
 						{
670 670
 							try {
671
-								$this->db->insert($table,$data,False,__LINE__,__FILE__,false,false,$this->schemas[$table]);
671
+								$this->db->insert($table, $data, False, __LINE__, __FILE__, false, false, $this->schemas[$table]);
672 672
 							}
673
-							catch(egw_exception_db_invalid_sql $e) {
673
+							catch (egw_exception_db_invalid_sql $e) {
674 674
 								echo "<p>".$e->getMessage()."</p>\n";
675 675
 							}
676 676
 						}
677 677
 					}
678 678
 					else
679 679
 					{
680
-						echo '<p>'.lang("Line %1: '%2'<br><b>csv data does not match column-count of table %3 ==> ignored</b>",$n,$line,$table)."</p>\n";
681
-						echo 'data=<pre>'.print_r($data,true)."</pre>\n";
680
+						echo '<p>'.lang("Line %1: '%2'<br><b>csv data does not match column-count of table %3 ==> ignored</b>", $n, $line, $table)."</p>\n";
681
+						echo 'data=<pre>'.print_r($data, true)."</pre>\n";
682 682
 					}
683 683
 				}
684 684
 			}
@@ -688,14 +688,14 @@  discard block
 block discarded – undo
688 688
 			$this->insert_multiple($table, $rows, $this->schemas[$table]);
689 689
 		}
690 690
 		// updated the sequences, if the DB uses them
691
-		foreach($this->schemas as $table => $schema)
691
+		foreach ($this->schemas as $table => $schema)
692 692
 		{
693
-			foreach($schema['fd'] as $column => $definition)
693
+			foreach ($schema['fd'] as $column => $definition)
694 694
 			{
695 695
 				if ($definition['type'] == 'auto')
696 696
 				{
697
-					$this->schema_proc->UpdateSequence($table,$column);
698
-					break;	// max. one per table
697
+					$this->schema_proc->UpdateSequence($table, $column);
698
+					break; // max. one per table
699 699
 				}
700 700
 			}
701 701
 		}
@@ -718,15 +718,15 @@  discard block
 block discarded – undo
718 718
 		try {
719 719
 			$this->db->insert($table, $rows, False, __LINE__, __FILE__, false, false, $schema);
720 720
 		}
721
-		catch(egw_exception_db_invalid_sql $e)
721
+		catch (egw_exception_db_invalid_sql $e)
722 722
 		{
723 723
 			// try inserting them one by one, ignoring doublicates
724
-			foreach($rows as $data)
724
+			foreach ($rows as $data)
725 725
 			{
726 726
 				try {
727 727
 					$this->db->insert($table, $data, False, __LINE__, __FILE__, false, false, $schema);
728 728
 				}
729
-				catch(egw_exception_db_invalid_sql $e) {
729
+				catch (egw_exception_db_invalid_sql $e) {
730 730
 					echo "<p>".$e->getMessage()."</p>\n";
731 731
 				}
732 732
 			}
@@ -741,11 +741,11 @@  discard block
 block discarded – undo
741 741
 	private static function remove_dir_content($dir)
742 742
 	{
743 743
 		$list = scandir($dir);
744
-		while($file = $list[0])
744
+		while ($file = $list[0])
745 745
 		{
746
-			if(is_dir($file) && $file != '.' && $file != '..')
746
+			if (is_dir($file) && $file != '.' && $file != '..')
747 747
 			    self::remove_dir_content($dir.'/'.$file);
748
-			if(is_file($file) && $file != '.' && $file != '..')
748
+			if (is_file($file) && $file != '.' && $file != '..')
749 749
 			    unlink($dir.'/'.$file);
750 750
 			array_shift($list);
751 751
 		}
@@ -770,7 +770,7 @@  discard block
 block discarded – undo
770 770
 	 * @param array $bools =array() bool columns, values might be 't'/'f' for old PostgreSQL backups
771 771
 	 * @return array
772 772
 	 */
773
-	public static function csv_split($line, $keys=null, $blobs=array(), $bools=array())
773
+	public static function csv_split($line, $keys = null, $blobs = array(), $bools = array())
774 774
 	{
775 775
 		if (function_exists('str_getcsv'))	// php5.3+
776 776
 		{
@@ -783,9 +783,9 @@  discard block
 block discarded – undo
783 783
 				'\\n'    => "\n",
784 784
 				'\\r'    => "\r")), ',', '"', '\0');
785 785
 			// replace NULL-token again with 'NULL', 'NULL' with null and BACKSLASH-token with a backslash
786
-			foreach($fields as &$field)
786
+			foreach ($fields as &$field)
787 787
 			{
788
-				switch($field)
788
+				switch ($field)
789 789
 				{
790 790
 					case self::NULL_TOKEN:
791 791
 						$field = 'NULL';
@@ -802,7 +802,7 @@  discard block
 block discarded – undo
802 802
 			{
803 803
 				$fields = array_combine($keys, $fields);
804 804
 				// base64-decode blob columns, if they are base64 encoded
805
-				foreach($blobs as $key)
805
+				foreach ($blobs as $key)
806 806
 				{
807 807
 					if (!is_null($fields[$key]) && ($tmp = base64_decode($fields[$key], true)) !== false)
808 808
 					{
@@ -810,7 +810,7 @@  discard block
 block discarded – undo
810 810
 					}
811 811
 				}
812 812
 				// decode bool columns, they might be 't'/'f' for old PostgreSQL backups
813
-				foreach($bools as $key)
813
+				foreach ($bools as $key)
814 814
 				{
815 815
 					$fields[$key] = egw_db::from_bool($fields[$key]);
816 816
 				}
@@ -818,11 +818,11 @@  discard block
 block discarded – undo
818 818
 			return $fields;
819 819
 		}
820 820
 		// pre 5.3 implementation
821
-		$fields = explode(',',trim($line));
821
+		$fields = explode(',', trim($line));
822 822
 
823 823
 		$str_pending = False;
824 824
 		$n = 0;
825
-		foreach($fields as $field)
825
+		foreach ($fields as $field)
826 826
 		{
827 827
 			if ($str_pending !== False)
828 828
 			{
@@ -833,12 +833,12 @@  discard block
 block discarded – undo
833 833
 
834 834
 			if ($field[0] == '"')
835 835
 			{
836
-				if (substr($field,-1) !== '"' || $field === '"' || !preg_match('/[^\\\\]+(\\\\\\\\)*"$/',$field))
836
+				if (substr($field, -1) !== '"' || $field === '"' || !preg_match('/[^\\\\]+(\\\\\\\\)*"$/', $field))
837 837
 				{
838 838
 					$str_pending = $field;
839 839
 					continue;
840 840
 				}
841
-				$arr[$key] = str_replace(self::BACKSLASH_TOKEN,'\\',str_replace(array('\\\\','\\n','\\r','\\"'),array(self::BACKSLASH_TOKEN,"\n","\r",'"'),substr($field,1,-1)));
841
+				$arr[$key] = str_replace(self::BACKSLASH_TOKEN, '\\', str_replace(array('\\\\', '\\n', '\\r', '\\"'), array(self::BACKSLASH_TOKEN, "\n", "\r", '"'), substr($field, 1, -1)));
842 842
 			}
843 843
 			elseif ($keys && strlen($field) > 26)
844 844
 			{
@@ -860,7 +860,7 @@  discard block
 block discarded – undo
860 860
 	/**
861 861
 	 * escape data for csv
862 862
 	 */
863
-	public static function escape_data(&$data,$col,$defs)
863
+	public static function escape_data(&$data, $col, $defs)
864 864
 	{
865 865
 		if (is_null($data))
866 866
 		{
@@ -868,7 +868,7 @@  discard block
 block discarded – undo
868 868
 		}
869 869
 		else
870 870
 		{
871
-			switch($defs[$col]['type'])
871
+			switch ($defs[$col]['type'])
872 872
 			{
873 873
 				case 'int':
874 874
 				case 'auto':
@@ -883,7 +883,7 @@  discard block
 block discarded – undo
883 883
 					$data = (int)egw_db::from_bool($data);
884 884
 					break;
885 885
 				default:
886
-					$data = '"'.str_replace(array('\\',"\n","\r",'"'),array('\\\\','\\n','\\r','\\"'),$data).'"';
886
+					$data = '"'.str_replace(array('\\', "\n", "\r", '"'), array('\\\\', '\\n', '\\r', '\\"'), $data).'"';
887 887
 					break;
888 888
 			}
889 889
 		}
@@ -915,15 +915,15 @@  discard block
 block discarded – undo
915 915
 		$name = $this->backup_dir.'/db_backup-'.date('YmdHi');
916 916
 		$filename = $name.'.zip';
917 917
 		$zippresent = false;
918
-		if(class_exists('ZipArchive') && $this->backup_files)
918
+		if (class_exists('ZipArchive') && $this->backup_files)
919 919
 		{
920 920
 			$zip = new ZipArchive;
921
-			if(is_object($zip))
921
+			if (is_object($zip))
922 922
 			{
923 923
 				$zippresent = true;
924 924
 				//echo '-> is_object($zip); '.$filename.'<br>';	// !
925 925
 				$res = $zip->open($filename, ZIPARCHIVE::CREATE);
926
-				if($res !== TRUE)
926
+				if ($res !== TRUE)
927 927
 				{
928 928
 					//echo '   -> !$res<br>';	// !
929 929
 					return lang("Cant open '%1' for %2", $filename, lang("writing"))."<br>\n";
@@ -931,19 +931,19 @@  discard block
 block discarded – undo
931 931
 				$file_list = $this->get_file_list($dir);
932 932
 			}
933 933
 		}
934
-		fwrite($f,"EGroupware backup from ".date('Y-m-d H:i:s')."\n\n");
934
+		fwrite($f, "EGroupware backup from ".date('Y-m-d H:i:s')."\n\n");
935 935
 
936
-		fwrite($f,"version: $this->api_version\n\n");
936
+		fwrite($f, "version: $this->api_version\n\n");
937 937
 
938
-		fwrite($f,"charset: $this->charset\n\n");
938
+		fwrite($f, "charset: $this->charset\n\n");
939 939
 
940
-		$this->schema_backup($f);	// add the schema in a human readable form too
940
+		$this->schema_backup($f); // add the schema in a human readable form too
941 941
 
942
-		fwrite($f,"\nschema: ".json_encode($this->schemas)."\n");
942
+		fwrite($f, "\nschema: ".json_encode($this->schemas)."\n");
943 943
 
944
-		foreach($this->schemas as $table => $schema)
944
+		foreach ($this->schemas as $table => $schema)
945 945
 		{
946
-			if (in_array($table,$this->exclude_tables)) continue;	// dont backup
946
+			if (in_array($table, $this->exclude_tables)) continue; // dont backup
947 947
 
948 948
 			// do we have a primary key?
949 949
 			// --> use it to order and limit rows, to kope with rows being added during backup
@@ -955,26 +955,26 @@  discard block
 block discarded – undo
955 955
 			do {
956 956
 				$num_rows = 0;
957 957
 				// querying only chunks for 10000 rows, to not run into memory limit on huge tables
958
-				foreach($this->db->select($table, '*',
958
+				foreach ($this->db->select($table, '*',
959 959
 					// limit by maximum primary key already received
960 960
 					empty($pk) || !$max ? false : $pk.' > '.$this->db->quote($max, $schema['fd'][$pk]['type']),
961 961
 					__LINE__, __FILE__,
962
-					empty($pk) ? false : 0,					// if no primary key, query all rows
963
-					empty($pk) ? '' : 'ORDER BY '.$this->db->name_quote($pk).' ASC',	// order by primary key
962
+					empty($pk) ? false : 0, // if no primary key, query all rows
963
+					empty($pk) ? '' : 'ORDER BY '.$this->db->name_quote($pk).' ASC', // order by primary key
964 964
 					false, self::ROW_CHUNK) as $row)
965 965
 				{
966 966
 					if (!empty($pk)) $max = $row[$pk];
967
-					if ($total === 0) fwrite($f,"\ntable: $table\n".implode(',',array_keys($row))."\n");
967
+					if ($total === 0) fwrite($f, "\ntable: $table\n".implode(',', array_keys($row))."\n");
968 968
 
969
-					array_walk($row,array('db_backup','escape_data'),$schema['fd']);
970
-					fwrite($f,implode(',',$row)."\n");
969
+					array_walk($row, array('db_backup', 'escape_data'), $schema['fd']);
970
+					fwrite($f, implode(',', $row)."\n");
971 971
 					++$total;
972 972
 					++$num_rows;
973 973
 				}
974 974
 			}
975
-			while(!empty($pk) && !($total % self::ROW_CHUNK) && $num_rows);
975
+			while (!empty($pk) && !($total % self::ROW_CHUNK) && $num_rows);
976 976
 		}
977
-		if(!$zippresent)  // save without files
977
+		if (!$zippresent)  // save without files
978 978
 		{
979 979
 			if ($this->backup_files)
980 980
 			{
@@ -989,16 +989,16 @@  discard block
 block discarded – undo
989 989
 		//echo $name.'<br>';
990 990
 		$zip->addFile($name, 'database_backup/'.basename($name));
991 991
 		$count = 1;
992
-		foreach($file_list as $file)
992
+		foreach ($file_list as $file)
993 993
 		{
994 994
 			//echo substr($file,strlen($dir)+1).'<br>';
995 995
 			//echo $file.'<br>';
996
-			$zip->addFile($file,substr($file,strlen($dir)+1));//,substr($file);
997
-			if(($count++) == 100) { // the file descriptor limit
996
+			$zip->addFile($file, substr($file, strlen($dir) + 1)); //,substr($file);
997
+			if (($count++) == 100) { // the file descriptor limit
998 998
 				$zip->close();
999
-				if(($zip = new ZipArchive())) {
999
+				if (($zip = new ZipArchive())) {
1000 1000
 					$zip->open($filename);
1001
-					$count =0;
1001
+					$count = 0;
1002 1002
 				}
1003 1003
 			}
1004 1004
 		}
@@ -1021,17 +1021,17 @@  discard block
 block discarded – undo
1021 1021
 	{
1022 1022
 		//chdir($f);
1023 1023
 		//echo "Processing $f <br>";
1024
-		if ($path_name =='') $path_name = $f;
1024
+		if ($path_name == '') $path_name = $f;
1025 1025
 		$tlist = scandir($f);
1026 1026
 		$list = array();
1027 1027
 		$i = $cnt;
1028
-		while($file = $tlist[0]) // remove all '.' and '..' and transfer to $list
1028
+		while ($file = $tlist[0]) // remove all '.' and '..' and transfer to $list
1029 1029
 		{
1030
-			if($file == '.' || $file == '..')
1030
+			if ($file == '.' || $file == '..')
1031 1031
 			{
1032 1032
 				array_shift($tlist);
1033 1033
 			}
1034
-			elseif ($file == 'debug.txt' && stripos($f,'activesync')!==false)
1034
+			elseif ($file == 'debug.txt' && stripos($f, 'activesync') !== false)
1035 1035
 			{
1036 1036
 				// skip activesync debug.txt on backupFiles
1037 1037
 				//error_log(__METHOD__.__LINE__.'->'.$f.'/'.$file);
@@ -1039,7 +1039,7 @@  discard block
 block discarded – undo
1039 1039
 			}
1040 1040
 			else
1041 1041
 			{
1042
-				if(is_dir($f.'/'.$file))
1042
+				if (is_dir($f.'/'.$file))
1043 1043
 				{
1044 1044
 					$nlist = $this->get_file_list($f.'/'.$file, $i);
1045 1045
 					$list += $nlist;
@@ -1060,12 +1060,12 @@  discard block
 block discarded – undo
1060 1060
 	 *
1061 1061
 	 * @param resource|boolean $f
1062 1062
 	 */
1063
-	function schema_backup($f=False)
1063
+	function schema_backup($f = False)
1064 1064
 	{
1065
-		foreach($this->adodb->MetaTables('TABLES') as $table)
1065
+		foreach ($this->adodb->MetaTables('TABLES') as $table)
1066 1066
 		{
1067
-			if ($this->system_tables && preg_match($this->system_tables,$table) ||
1068
-				$this->egw_tables && !preg_match($this->egw_tables,$table))
1067
+			if ($this->system_tables && preg_match($this->system_tables, $table) ||
1068
+				$this->egw_tables && !preg_match($this->egw_tables, $table))
1069 1069
 			{
1070 1070
 				continue;
1071 1071
 			}
@@ -1084,17 +1084,17 @@  discard block
 block discarded – undo
1084 1084
 			}
1085 1085
 		}
1086 1086
 		$def = "\t\$phpgw_baseline = ";
1087
-		$def .= self::write_array($this->schemas,1);
1087
+		$def .= self::write_array($this->schemas, 1);
1088 1088
 		$def .= ";\n";
1089 1089
 
1090 1090
 		if ($f)
1091 1091
 		{
1092
-			fwrite($f,$def);
1092
+			fwrite($f, $def);
1093 1093
 		}
1094 1094
 		else
1095 1095
 		{
1096 1096
 			$def = "<?php\n\t/* EGroupware schema-backup from ".date('Y-m-d H:i:s')." */\n\n".$def;
1097
-			html::content_header('schema-backup-'.date('YmdHi').'.inc.php','text/plain',bytes($def));
1097
+			html::content_header('schema-backup-'.date('YmdHi').'.inc.php', 'text/plain', bytes($def));
1098 1098
 			echo $def;
1099 1099
 		}
1100 1100
 	}
@@ -1104,9 +1104,9 @@  discard block
 block discarded – undo
1104 1104
 	 *
1105 1105
 	 * copied from etemplate/inc/class.db_tools.inc.php
1106 1106
 	 */
1107
-	private static function write_array($arr,$depth,$parent='')
1107
+	private static function write_array($arr, $depth, $parent = '')
1108 1108
 	{
1109
-		if (in_array($parent,array('pk','fk','ix','uc')))
1109
+		if (in_array($parent, array('pk', 'fk', 'ix', 'uc')))
1110 1110
 		{
1111 1111
 			$depth = 0;
1112 1112
 		}
@@ -1122,7 +1122,7 @@  discard block
 block discarded – undo
1122 1122
 		$def = "array($tabs".($tabs ? "\t" : '');
1123 1123
 
1124 1124
 		$n = 0;
1125
-		foreach($arr as $key => $val)
1125
+		foreach ($arr as $key => $val)
1126 1126
 		{
1127 1127
 			if (!is_int($key))
1128 1128
 			{
@@ -1130,7 +1130,7 @@  discard block
 block discarded – undo
1130 1130
 			}
1131 1131
 			if (is_array($val))
1132 1132
 			{
1133
-				$def .= self::write_array($val,$parent == 'fd' ? 0 : $depth,$key);
1133
+				$def .= self::write_array($val, $parent == 'fd' ? 0 : $depth, $key);
1134 1134
 			}
1135 1135
 			else
1136 1136
 			{
@@ -1143,7 +1143,7 @@  discard block
 block discarded – undo
1143 1143
 					$def .= "'$val'";
1144 1144
 				}
1145 1145
 			}
1146
-			if ($n < count($arr)-1)
1146
+			if ($n < count($arr) - 1)
1147 1147
 			{
1148 1148
 				$def .= ",$tabs".($tabs ? "\t" : '');
1149 1149
 			}
Please login to merge, or discard this patch.
Braces   +123 added lines, -34 removed lines patch added patch discarded remove patch
@@ -97,23 +97,32 @@  discard block
 block discarded – undo
97 97
 		{
98 98
 			$GLOBALS['egw_setup']->loaddb();	// we run inside setup, but db object is not loaded
99 99
 		}
100
-		if (isset($GLOBALS['egw_setup']->oProc) && is_object($GLOBALS['egw_setup']->oProc))	// schema_proc already instanciated, use it
100
+		if (isset($GLOBALS['egw_setup']->oProc) && is_object($GLOBALS['egw_setup']->oProc))
101
+		{
102
+			// schema_proc already instanciated, use it
101 103
 		{
102 104
 			$this->schema_proc = $GLOBALS['egw_setup']->oProc;
103 105
 		}
106
+		}
104 107
 		else
105 108
 		{
106 109
 			$this->schema_proc = new schema_proc();
107 110
 		}
108 111
 
109 112
 		$this->db = $this->schema_proc->m_odb;
110
-		if (!$this->db->Link_ID) $this->db->connect();
113
+		if (!$this->db->Link_ID)
114
+		{
115
+			$this->db->connect();
116
+		}
111 117
 		$this->adodb = $this->db->Link_ID;
112
-		if (isset($GLOBALS['egw_setup']) && is_object($GLOBALS['egw_setup']))		// called from setup
118
+		if (isset($GLOBALS['egw_setup']) && is_object($GLOBALS['egw_setup']))
119
+		{
120
+			// called from setup
113 121
 		{
114 122
 			if ($GLOBALS['egw_setup']->config_table && $GLOBALS['egw_setup']->table_exist(array($GLOBALS['egw_setup']->config_table)))
115 123
 			{
116 124
 				$this->db->query("SELECT config_value FROM {$GLOBALS['egw_setup']->config_table} WHERE config_app='phpgwapi' AND config_name='backup_dir'",__LINE__,__FILE__);
125
+		}
117 126
 				$this->db->next_record();
118 127
 				if (!($this->backup_dir = $this->db->f(0)))
119 128
 				{
@@ -147,7 +156,10 @@  discard block
 block discarded – undo
147 156
 				$this->db->next_record();
148 157
 				$this->backup_files = (bool)$this->db->f(0);
149 158
 			}
150
-			if (!$this->charset) $this->charset = 'iso-8859-1';
159
+			if (!$this->charset)
160
+			{
161
+				$this->charset = 'iso-8859-1';
162
+			}
151 163
 		}
152 164
 		else	// called from eGW
153 165
 		{
@@ -245,8 +257,7 @@  discard block
 block discarded – undo
245 257
 		if(!($f = fopen("compress.bzip2://$name.bz2", $mode)) &&
246 258
 	 		!($f = fopen("compress.zlib://$name.gz",$mode)) &&
247 259
  		 	!($f = fopen($name,$mode))
248
-		)
249
-		{
260
+		) {
250 261
 			//echo '-> !($f = fopen("compress.bzip2://$name.bz2", $mode))<br>';	// !
251 262
 			$lang_mode = $reading ? lang("reading") : lang("writing");
252 263
 			return lang("Cant open '%1' for %2", $name, $lang_mode)."<br>";
@@ -282,16 +293,22 @@  discard block
 block discarded – undo
282 293
 				$files[filectime($this->backup_dir.'/'.$file)] = $file;
283 294
 			}
284 295
 		}
285
-		if ($handle) closedir($handle);
296
+		if ($handle)
297
+		{
298
+			closedir($handle);
299
+		}
286 300
 
287 301
 		/* Sort the files by ctime. */
288 302
 		krsort($files);
289 303
 		$count = 0;
290 304
 		foreach($files as $file)
291 305
 		{
292
-			if ($count >= $this->backup_mincount)//
306
+			if ($count >= $this->backup_mincount)
307
+			{
308
+				//
293 309
 			{
294 310
 				$ret = unlink($this->backup_dir.'/'.$file);
311
+			}
295 312
 				if (($ret) && (is_array($files_return)))
296 313
 				{
297 314
 					array_push($files_return, $file);
@@ -355,7 +372,11 @@  discard block
 block discarded – undo
355 372
 		@set_time_limit(0);
356 373
 		ini_set('auto_detect_line_endings',true);
357 374
 
358
-		if (true) $convert_to_system_charset = true;	// enforce now utf-8 as system charset restores of old backups
375
+		if (true)
376
+		{
377
+			$convert_to_system_charset = true;
378
+		}
379
+		// enforce now utf-8 as system charset restores of old backups
359 380
 
360 381
 		if ($protect_system_config)
361 382
 		{
@@ -406,7 +427,7 @@  discard block
 block discarded – undo
406 427
 		$zip = NULL;
407 428
 		$_f = NULL;
408 429
 		if($type == 'zip')
409
-	    {
430
+		{
410 431
 			// has already been verified to be available in fopen_backup
411 432
 			$zip = new ZipArchive;
412 433
 			if(($zip->open($filename)) !== TRUE)
@@ -431,7 +452,9 @@  discard block
 block discarded – undo
431 452
 		}
432 453
 		$this->db_restore($f, $insert_n_rows);
433 454
 
434
-		if ($convert_to_system_charset)	// store the changed charset
455
+		if ($convert_to_system_charset)
456
+		{
457
+			// store the changed charset
435 458
 		{
436 459
 			$this->db->insert(config::TABLE, array(
437 460
 				'config_value' => $this->schema_proc->system_charset,
@@ -440,6 +463,7 @@  discard block
 block discarded – undo
440 463
 				'config_name' => 'system_charset',
441 464
 			),__LINE__,__FILE__);
442 465
 		}
466
+		}
443 467
 		// restore protected system config
444 468
 		if ($protect_system_config)
445 469
 		{
@@ -534,7 +558,10 @@  discard block
 block discarded – undo
534 558
 		{
535 559
 			$line = trim(fgets($f)); ++$n;
536 560
 
537
-			if (empty($line)) continue;
561
+			if (empty($line))
562
+			{
563
+				continue;
564
+			}
538 565
 
539 566
 			if (substr($line,0,9) == 'version: ')
540 567
 			{
@@ -587,31 +614,49 @@  discard block
 block discarded – undo
587 614
 			}
588 615
 			if (substr($line,0,7) == 'table: ')
589 616
 			{
590
-				if ($rows)	// flush pending rows of last table
617
+				if ($rows)
618
+				{
619
+					// flush pending rows of last table
591 620
 				{
592 621
 					$this->insert_multiple($table, $rows, $this->schemas[$table]);
593 622
 				}
623
+				}
594 624
 				$rows = array();
595 625
 				$table = substr($line,7);
596
-				if (!isset($this->schemas[$table])) $this->schemas[$table] = $this->db->get_table_definitions(true, $table);
626
+				if (!isset($this->schemas[$table]))
627
+				{
628
+					$this->schemas[$table] = $this->db->get_table_definitions(true, $table);
629
+				}
597 630
 				$auto_id = count($this->schemas[$table]['pk']) == 1 ? $this->schemas[$table]['pk'][0] : null;
598 631
 
599 632
 				$cols = self::csv_split($line=fgets($f)); ++$n;
600 633
 				$blobs = array();
601 634
 				foreach($this->schemas[$table]['fd'] as $col => $data)
602 635
 				{
603
-					if ($data['type'] == 'blob') $blobs[] = $col;
636
+					if ($data['type'] == 'blob')
637
+					{
638
+						$blobs[] = $col;
639
+					}
604 640
 				}
605 641
 				// check if we have an old PostgreSQL backup useing 't'/'f' for bool values
606 642
 				// --> convert them to MySQL and our new PostgreSQL format of 1/0
607 643
 				$bools = array();
608 644
 				foreach($this->schemas[$table]['fd'] as $col => $def)
609 645
 				{
610
-					if ($def['type'] === 'bool') $bools[] = $col;
646
+					if ($def['type'] === 'bool')
647
+					{
648
+						$bools[] = $col;
649
+					}
650
+				}
651
+				if ($table == 'egw_cal_dates')
652
+				{
653
+					error_log(__METHOD__."() $table: bools=".array2string($bools).", schema[fd]=".array2string($this->schemas[$table]['fd']));
611 654
 				}
612
-				if ($table == 'egw_cal_dates') error_log(__METHOD__."() $table: bools=".array2string($bools).", schema[fd]=".array2string($this->schemas[$table]['fd']));
613 655
 
614
-				if (feof($f)) break;
656
+				if (feof($f))
657
+				{
658
+					break;
659
+				}
615 660
 				continue;
616 661
 			}
617 662
 			if ($convert_to_system_charset && !$this->db->capabilities['client_encoding'])
@@ -624,9 +669,12 @@  discard block
 block discarded – undo
624 669
 					}
625 670
 				}
626 671
 			}
627
-			if ($table)	// do we already reached the data part
672
+			if ($table)
673
+			{
674
+				// do we already reached the data part
628 675
 			{
629 676
 				$import = true;
677
+			}
630 678
 				$data = self::csv_split($line, $cols, $blobs, $bools);
631 679
 
632 680
 				if ($table == 'egw_async' && in_array('##last-check-run##',$data))
@@ -662,7 +710,10 @@  discard block
 block discarded – undo
662 710
 						{
663 711
 							$where = array($insert_n_rows => $data[$insert_n_rows]);
664 712
 							unset($data[$insert_n_rows]);
665
-							if ($auto_id) unset($data[$auto_id]);
713
+							if ($auto_id)
714
+							{
715
+								unset($data[$auto_id]);
716
+							}
666 717
 							$this->db->insert($table,$data,$where,__LINE__,__FILE__,false,false,$this->schemas[$table]);
667 718
 						}
668 719
 						else
@@ -683,10 +734,13 @@  discard block
 block discarded – undo
683 734
 				}
684 735
 			}
685 736
 		}
686
-		if ($rows)	// flush pending rows
737
+		if ($rows)
738
+		{
739
+			// flush pending rows
687 740
 		{
688 741
 			$this->insert_multiple($table, $rows, $this->schemas[$table]);
689 742
 		}
743
+		}
690 744
 		// updated the sequences, if the DB uses them
691 745
 		foreach($this->schemas as $table => $schema)
692 746
 		{
@@ -744,9 +798,13 @@  discard block
 block discarded – undo
744 798
 		while($file = $list[0])
745 799
 		{
746 800
 			if(is_dir($file) && $file != '.' && $file != '..')
747
-			    self::remove_dir_content($dir.'/'.$file);
801
+			{
802
+						    self::remove_dir_content($dir.'/'.$file);
803
+			}
748 804
 			if(is_file($file) && $file != '.' && $file != '..')
749
-			    unlink($dir.'/'.$file);
805
+			{
806
+						    unlink($dir.'/'.$file);
807
+			}
750 808
 			array_shift($list);
751 809
 		}
752 810
 		//rmdir($dir);  // dont remove own dir
@@ -772,7 +830,9 @@  discard block
 block discarded – undo
772 830
 	 */
773 831
 	public static function csv_split($line, $keys=null, $blobs=array(), $bools=array())
774 832
 	{
775
-		if (function_exists('str_getcsv'))	// php5.3+
833
+		if (function_exists('str_getcsv'))
834
+		{
835
+			// php5.3+
776 836
 		{
777 837
 			// we need to take care of literal "NULL" values, replacing them we a special token as str_getcsv removes enclosures around strings
778 838
 			// str_getcsv uses '""' for '"' instead of '\\"' and does not unescape '\\n', '\\r' or '\\\\' (two backslashes)
@@ -782,6 +842,7 @@  discard block
 block discarded – undo
782 842
 				'\\"'    => '""',
783 843
 				'\\n'    => "\n",
784 844
 				'\\r'    => "\r")), ',', '"', '\0');
845
+		}
785 846
 			// replace NULL-token again with 'NULL', 'NULL' with null and BACKSLASH-token with a backslash
786 847
 			foreach($fields as &$field)
787 848
 			{
@@ -798,9 +859,12 @@  discard block
 block discarded – undo
798 859
 						break;
799 860
 				}
800 861
 			}
801
-			if ($keys)	// if string keys are to be used --> combine keys and values
862
+			if ($keys)
863
+			{
864
+				// if string keys are to be used --> combine keys and values
802 865
 			{
803 866
 				$fields = array_combine($keys, $fields);
867
+			}
804 868
 				// base64-decode blob columns, if they are base64 encoded
805 869
 				foreach($blobs as $key)
806 870
 				{
@@ -943,7 +1007,11 @@  discard block
 block discarded – undo
943 1007
 
944 1008
 		foreach($this->schemas as $table => $schema)
945 1009
 		{
946
-			if (in_array($table,$this->exclude_tables)) continue;	// dont backup
1010
+			if (in_array($table,$this->exclude_tables))
1011
+			{
1012
+				continue;
1013
+			}
1014
+			// dont backup
947 1015
 
948 1016
 			// do we have a primary key?
949 1017
 			// --> use it to order and limit rows, to kope with rows being added during backup
@@ -963,8 +1031,14 @@  discard block
 block discarded – undo
963 1031
 					empty($pk) ? '' : 'ORDER BY '.$this->db->name_quote($pk).' ASC',	// order by primary key
964 1032
 					false, self::ROW_CHUNK) as $row)
965 1033
 				{
966
-					if (!empty($pk)) $max = $row[$pk];
967
-					if ($total === 0) fwrite($f,"\ntable: $table\n".implode(',',array_keys($row))."\n");
1034
+					if (!empty($pk))
1035
+					{
1036
+						$max = $row[$pk];
1037
+					}
1038
+					if ($total === 0)
1039
+					{
1040
+						fwrite($f,"\ntable: $table\n".implode(',',array_keys($row))."\n");
1041
+					}
968 1042
 
969 1043
 					array_walk($row,array('db_backup','escape_data'),$schema['fd']);
970 1044
 					fwrite($f,implode(',',$row)."\n");
@@ -974,15 +1048,21 @@  discard block
 block discarded – undo
974 1048
 			}
975 1049
 			while(!empty($pk) && !($total % self::ROW_CHUNK) && $num_rows);
976 1050
 		}
977
-		if(!$zippresent)  // save without files
1051
+		if(!$zippresent)
1052
+		{
1053
+			// save without files
978 1054
 		{
979 1055
 			if ($this->backup_files)
980 1056
 			{
981 1057
 				echo '<center>'.lang("Cant open %1, needs ZipArchive", $name)."<br>\n".'</center>';
1058
+		}
982 1059
 			}
983 1060
 
984 1061
 		    fclose($f);
985
-		    if (file_exists($name)) unlink($name);
1062
+		    if (file_exists($name))
1063
+		    {
1064
+		    	unlink($name);
1065
+		    }
986 1066
 			return TRUE;
987 1067
 		}
988 1068
 		// save files ....
@@ -994,9 +1074,12 @@  discard block
 block discarded – undo
994 1074
 			//echo substr($file,strlen($dir)+1).'<br>';
995 1075
 			//echo $file.'<br>';
996 1076
 			$zip->addFile($file,substr($file,strlen($dir)+1));//,substr($file);
997
-			if(($count++) == 100) { // the file descriptor limit
1077
+			if(($count++) == 100)
1078
+			{
1079
+// the file descriptor limit
998 1080
 				$zip->close();
999
-				if(($zip = new ZipArchive())) {
1081
+				if(($zip = new ZipArchive()))
1082
+				{
1000 1083
 					$zip->open($filename);
1001 1084
 					$count =0;
1002 1085
 				}
@@ -1021,15 +1104,21 @@  discard block
 block discarded – undo
1021 1104
 	{
1022 1105
 		//chdir($f);
1023 1106
 		//echo "Processing $f <br>";
1024
-		if ($path_name =='') $path_name = $f;
1107
+		if ($path_name =='')
1108
+		{
1109
+			$path_name = $f;
1110
+		}
1025 1111
 		$tlist = scandir($f);
1026 1112
 		$list = array();
1027 1113
 		$i = $cnt;
1028
-		while($file = $tlist[0]) // remove all '.' and '..' and transfer to $list
1114
+		while($file = $tlist[0])
1115
+		{
1116
+			// remove all '.' and '..' and transfer to $list
1029 1117
 		{
1030 1118
 			if($file == '.' || $file == '..')
1031 1119
 			{
1032 1120
 				array_shift($tlist);
1121
+		}
1033 1122
 			}
1034 1123
 			elseif ($file == 'debug.txt' && stripos($f,'activesync')!==false)
1035 1124
 			{
Please login to merge, or discard this patch.