From ebd0e986ed11f2a34fb58cdd33efbfab192083ad Mon Sep 17 00:00:00 2001
From: Till Brehm <tbrehm@ispconfig.org>
Date: Fri, 22 Apr 2016 05:26:17 -0400
Subject: [PATCH] Added PHP 7 check in installer and updater.

---
 server/cron_daily.php |  538 ++++++++++++++++++++++++++++++++---------------------------
 1 files changed, 288 insertions(+), 250 deletions(-)

diff --git a/server/cron_daily.php b/server/cron_daily.php
index 60f3c21..7ba4f61 100644
--- a/server/cron_daily.php
+++ b/server/cron_daily.php
@@ -32,6 +32,8 @@
 require SCRIPT_PATH."/lib/config.inc.php";
 require SCRIPT_PATH."/lib/app.inc.php";
 
+$app->setCaller('cron_daily');
+
 set_time_limit(0);
 ini_set('error_reporting', E_ALL & ~E_NOTICE);
 
@@ -562,7 +564,6 @@
 
 $sql = "SELECT domain_id, domain, document_root, system_user FROM web_domain WHERE server_id = ".$conf['server_id'];
 $records = $app->db->queryAllRecords($sql);
-$app->uses('system');
 if(is_array($records)) {
 	foreach($records as $rec){
 		$tmp_path = realpath(escapeshellcmd($rec['document_root'].'/tmp'));
@@ -648,7 +649,7 @@
 // function for sending notification emails
 //########
 function send_notification_email($template, $placeholders, $recipients) {
-	global $conf;
+	global $conf, $app;
 
 	if(!is_array($recipients) || count($recipients) < 1) return false;
 	if(!is_array($placeholders)) $placeholders = array();
@@ -669,7 +670,7 @@
 	$mailSubject = '';
 	$inHeader = true;
 	for($l = 0; $l < count($lines); $l++) {
-		if($lines[$l] == '') {
+		if(trim($lines[$l]) == '') {
 			$inHeader = false;
 			continue;
 		}
@@ -809,10 +810,10 @@
 			$domain = $rec['domain'];
 
 			$username = $rec['system_user'];
-			$rec['used'] = $monitor_data['user'][$username]['used'];
-			$rec['soft'] = $monitor_data['user'][$username]['soft'];
-			$rec['hard'] = $monitor_data['user'][$username]['hard'];
-			$rec['files'] = $monitor_data['user'][$username]['files'];
+			$rec['used'] = @$monitor_data['user'][$username]['used'];
+			$rec['soft'] = @$monitor_data['user'][$username]['soft'];
+			$rec['hard'] = @$monitor_data['user'][$username]['hard'];
+			$rec['files'] = @$monitor_data['user'][$username]['files'];
 
 			if (!is_numeric($rec['used'])){
 				if ($rec['used'][0] > $rec['used'][1]){
@@ -887,42 +888,41 @@
 					}
 					send_notification_email('web_quota_ok_notification', $placeholders, $recipients);
 				}
+			} else {
 
-				continue;
-			}
+				// could a notification be sent?
+				$send_notification = false;
+				if(!$rec['last_quota_notification']) $send_notification = true; // not yet notified
+				elseif($web_config['overquota_notify_freq'] > 0 && $rec['notified_before'] >= $web_config['overquota_notify_freq']) $send_notification = true;
 
-			// could a notification be sent?
-			$send_notification = false;
-			if(!$rec['last_quota_notification']) $send_notification = true; // not yet notified
-			elseif($web_config['overquota_notify_freq'] > 0 && $rec['notified_before'] >= $web_config['overquota_notify_freq']) $send_notification = true;
+				//* Send quota notifications
+				if(($web_config['overquota_notify_admin'] == 'y' || $web_config['overquota_notify_client'] == 'y') && $send_notification == true) {
+					$app->dbmaster->datalogUpdate('web_domain', "last_quota_notification = CURDATE()", 'domain_id', $rec['domain_id']);
 
-			//* Send quota notifications
-			if(($web_config['overquota_notify_admin'] == 'y' || $web_config['overquota_notify_client'] == 'y') && $send_notification == true) {
-				$app->dbmaster->datalogUpdate('web_domain', "last_quota_notification = CURDATE()", 'domain_id', $rec['domain_id']);
+					$placeholders = array('{domain}' => $rec['domain'],
+						'{admin_mail}' => ($global_config['admin_mail'] != ''? $global_config['admin_mail'] : 'root'),
+						'{used}' => $rec['used'],
+						'{soft}' => $rec['soft'],
+						'{hard}' => $rec['hard'],
+						'{ratio}' => $rec['ratio']);
 
-				$placeholders = array('{domain}' => $rec['domain'],
-					'{admin_mail}' => ($global_config['admin_mail'] != ''? $global_config['admin_mail'] : 'root'),
-					'{used}' => $rec['used'],
-					'{soft}' => $rec['soft'],
-					'{hard}' => $rec['hard'],
-					'{ratio}' => $rec['ratio']);
+					$recipients = array();
 
-				$recipients = array();
-
-				//* send email to admin
-				if($global_config['admin_mail'] != '' && $web_config['overquota_notify_admin'] == 'y') {
-					$recipients[] = $global_config['admin_mail'];
-				}
-
-				//* Send email to client
-				if($web_config['overquota_notify_client'] == 'y') {
-					$client_group_id = $rec["sys_groupid"];
-					$client = $app->db->queryOneRecord("SELECT client.email FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
-					if($client['email'] != '') {
-						$recipients[] = $client['email'];
+					//* send email to admin
+					if($global_config['admin_mail'] != '' && $web_config['overquota_notify_admin'] == 'y') {
+						$recipients[] = $global_config['admin_mail'];
 					}
+
+					//* Send email to client
+					if($web_config['overquota_notify_client'] == 'y') {
+						$client_group_id = $rec["sys_groupid"];
+						$client = $app->db->queryOneRecord("SELECT client.email FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
+						if($client['email'] != '') {
+							$recipients[] = $client['email'];
+						}
+					}
+					send_notification_email('web_quota_notification', $placeholders, $recipients);
 				}
-				send_notification_email('web_quota_notification', $placeholders, $recipients);
 			}
 		}
 	}
@@ -1017,42 +1017,41 @@
 
 					send_notification_email('mail_quota_ok_notification', $placeholders, $recipients);
 				}
+			} else {
 
-				continue;
-			}
+				//* Send quota notifications
+				// could a notification be sent?
+				$send_notification = false;
+				if(!$rec['last_quota_notification']) $send_notification = true; // not yet notified
+				elseif($mail_config['overquota_notify_freq'] > 0 && $rec['notified_before'] >= $mail_config['overquota_notify_freq']) $send_notification = true;
 
-			//* Send quota notifications
-			// could a notification be sent?
-			$send_notification = false;
-			if(!$rec['last_quota_notification']) $send_notification = true; // not yet notified
-			elseif($mail_config['overquota_notify_freq'] > 0 && $rec['notified_before'] >= $mail_config['overquota_notify_freq']) $send_notification = true;
+				if(($mail_config['overquota_notify_admin'] == 'y' || $mail_config['overquota_notify_client'] == 'y') && $send_notification == true) {
+					$app->dbmaster->datalogUpdate('mail_user', "last_quota_notification = CURDATE()", 'mailuser_id', $rec['mailuser_id']);
 
-			if(($mail_config['overquota_notify_admin'] == 'y' || $mail_config['overquota_notify_client'] == 'y') && $send_notification == true) {
-				$app->dbmaster->datalogUpdate('mail_user', "last_quota_notification = CURDATE()", 'mailuser_id', $rec['mailuser_id']);
+					$placeholders = array('{email}' => $rec['email'],
+						'{admin_mail}' => ($global_config['admin_mail'] != ''? $global_config['admin_mail'] : 'root'),
+						'{used}' => $rec['used'],
+						'{name}' => $rec['name'],
+						'{quota}' => $rec['quota'],
+						'{ratio}' => $rec['ratio']);
 
-				$placeholders = array('{email}' => $rec['email'],
-					'{admin_mail}' => ($global_config['admin_mail'] != ''? $global_config['admin_mail'] : 'root'),
-					'{used}' => $rec['used'],
-					'{name}' => $rec['name'],
-					'{quota}' => $rec['quota'],
-					'{ratio}' => $rec['ratio']);
-
-				$recipients = array();
-				//* send email to admin
-				if($global_config['admin_mail'] != '' && $mail_config['overquota_notify_admin'] == 'y') {
-					$recipients[] = $global_config['admin_mail'];
-				}
-
-				//* Send email to client
-				if($mail_config['overquota_notify_client'] == 'y') {
-					$client_group_id = $rec["sys_groupid"];
-					$client = $app->db->queryOneRecord("SELECT client.email FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
-					if($client['email'] != '') {
-						$recipients[] = $client['email'];
+					$recipients = array();
+					//* send email to admin
+					if($global_config['admin_mail'] != '' && $mail_config['overquota_notify_admin'] == 'y') {
+						$recipients[] = $global_config['admin_mail'];
 					}
-				}
 
-				send_notification_email('mail_quota_notification', $placeholders, $recipients);
+					//* Send email to client
+					if($mail_config['overquota_notify_client'] == 'y') {
+						$client_group_id = $rec["sys_groupid"];
+						$client = $app->db->queryOneRecord("SELECT client.email FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
+						if($client['email'] != '') {
+							$recipients[] = $client['email'];
+						}
+					}
+
+					send_notification_email('mail_quota_notification', $placeholders, $recipients);
+				}
 			}
 		}
 	}
@@ -1104,213 +1103,252 @@
 	} else {
 		chmod(escapeshellcmd($backup_dir), $backup_dir_permissions);
 	}
-
-	$sql = "SELECT * FROM web_domain WHERE server_id = ".$conf['server_id']." AND (type = 'vhost' OR type = 'vhostsubdomain')";
-	$records = $app->db->queryAllRecords($sql);
-	if(is_array($records)) {
-		foreach($records as $rec) {
-
-			//* Do the website backup
-			if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
-
-				$web_path = $rec['document_root'];
-				$web_user = $rec['system_user'];
-				$web_group = $rec['system_group'];
-				$web_id = $rec['domain_id'];
-				$web_backup_dir = $backup_dir.'/web'.$web_id;
-				if(!is_dir($web_backup_dir)) mkdir($web_backup_dir, 0750);
-				chmod($web_backup_dir, 0750);
-				//if(isset($server_config['backup_dir_ftpread']) && $server_config['backup_dir_ftpread'] == 'y') {
-				chown($web_backup_dir, $rec['system_user']);
-				chgrp($web_backup_dir, $rec['system_group']);
-				/*} else {
-					chown($web_backup_dir, 'root');
-					chgrp($web_backup_dir, 'root');
-				}*/
-				if($backup_mode == 'userzip') {
-					//* Create a .zip backup as web user and include also files owned by apache / nginx user
-					$web_backup_file = 'web'.$web_id.'_'.date('Y-m-d_H-i').'.zip';
-					exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -group '.escapeshellarg($web_group).' -print 2> /dev/null | zip -b /tmp --exclude=backup\* --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
-					if($retval == 0) exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -user '.escapeshellarg($http_server_user).' -print 2> /dev/null | zip -b /tmp --exclude=backup\* --update --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
-				} else {
-					//* Create a tar.gz backup as root user
-					$web_backup_file = 'web'.$web_id.'_'.date('Y-m-d_H-i').'.tar.gz';
-					exec('tar pczf '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' --exclude=backup\* --directory '.escapeshellarg($web_path).' .', $tmp_output, $retval);
-				}
-				if($retval == 0 || $backup_mode != 'userzip'){ // tar can return 1 (due to harmless warings) and still create valid backups
-					if(is_file($web_backup_dir.'/'.$web_backup_file)){
-						chown($web_backup_dir.'/'.$web_backup_file, 'root');
-						chgrp($web_backup_dir.'/'.$web_backup_file, 'root');
-						chmod($web_backup_dir.'/'.$web_backup_file, 0750);
-
-						//* Insert web backup record in database
-						//$insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
-						//$app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
-						$sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
-						$app->db->query($sql);
-						if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
-					}
-				} else {
-					if(is_file($web_backup_dir.'/'.$web_backup_file)) unlink($web_backup_dir.'/'.$web_backup_file);
-				}
-
-				//* Remove old backups
-				$backup_copies = intval($rec['backup_copies']);
-
-				$dir_handle = dir($web_backup_dir);
-				$files = array();
-				while (false !== ($entry = $dir_handle->read())) {
-					if($entry != '.' && $entry != '..' && substr($entry, 0, 3) == 'web' && is_file($web_backup_dir.'/'.$entry)) {
-						$files[] = $entry;
-					}
-				}
-				$dir_handle->close();
-
-				rsort($files);
-
-				for ($n = $backup_copies; $n <= 10; $n++) {
-					if(isset($files[$n]) && is_file($web_backup_dir.'/'.$files[$n])) {
-						unlink($web_backup_dir.'/'.$files[$n]);
-						//$sql = "SELECT backup_id FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($files[$n])."'";
-						//$tmp = $app->dbmaster->queryOneRecord($sql);
-						//$app->dbmaster->datalogDelete('web_backup', 'backup_id', $tmp['backup_id']);
-						//$sql = "DELETE FROM web_backup WHERE backup_id = ".intval($tmp['backup_id']);
-						$sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($files[$n])."'";
-						$app->db->query($sql);
-						if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
-					}
-				}
-
-				unset($files);
-				unset($dir_handle);
-
-				//* Remove backupdir symlink and create as directory instead
-				$app->uses('system');
-				$app->system->web_folder_protection($web_path, false);
-
-				if(is_link($web_path.'/backup')) {
-					unlink($web_path.'/backup');
-				}
-				if(!is_dir($web_path.'/backup')) {
-					mkdir($web_path.'/backup');
-					chown($web_path.'/backup', $rec['system_user']);
-					chgrp($web_path.'/backup', $rec['system_group']);
-				}
-
-				$app->system->web_folder_protection($web_path, true);
-			}
-
-			/* If backup_interval is set to none and we have a
-			backup directory for the website, then remove the backups */
-			if($rec['backup_interval'] == 'none' || $rec['backup_interval'] == '') {
-				$web_id = $rec['domain_id'];
-				$web_user = $rec['system_user'];
-				$web_backup_dir = realpath($backup_dir.'/web'.$web_id);
-				if(is_dir($web_backup_dir)) {
-					exec('sudo -u '.escapeshellarg($web_user).' rm -f '.escapeshellarg($web_backup_dir.'/*'));
-				}
-			}
+	
+	//* mount backup directory, if necessary
+	$run_backups = true;
+	$backup_dir_mount_cmd = '/usr/local/ispconfig/server/scripts/backup_dir_mount.sh';
+	if(	$server_config['backup_dir_is_mount'] == 'y' && 
+		is_file($backup_dir_mount_cmd) && 
+		is_executable($backup_dir_mount_cmd) &&
+		fileowner($backup_dir_mount_cmd) === 0
+		){
+		if(!$app->system->is_mounted($backup_dir)){
+			exec($backup_dir_mount_cmd);
+			sleep(1);
+			if(!$app->system->is_mounted($backup_dir)) $run_backups = false;
 		}
 	}
 
-	$sql = "SELECT * FROM web_database WHERE server_id = ".$conf['server_id']." AND backup_interval != 'none' AND backup_interval != ''";
-	$records = $app->db->queryAllRecords($sql);
-	if(is_array($records)) {
+	if($run_backups){
+		$sql = "SELECT * FROM web_domain WHERE server_id = ".$conf['server_id']." AND (type = 'vhost' OR type = 'vhostsubdomain')";
+		$records = $app->db->queryAllRecords($sql);
+		if(is_array($records)) {
+			foreach($records as $rec) {
 
-		include 'lib/mysql_clientdb.conf';
+				//* Do the website backup
+				if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
 
-		foreach($records as $rec) {
-
-			//* Do the database backup
-			if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
-
-				$web_id = $rec['parent_domain_id'];
-				$db_backup_dir = $backup_dir.'/web'.$web_id;
-				if(!is_dir($db_backup_dir)) mkdir($db_backup_dir, 0750);
-				chmod($db_backup_dir, 0750);
-				chown($db_backup_dir, 'root');
-				chgrp($db_backup_dir, 'root');
-
-				//* Do the mysql database backup with mysqldump
-				$db_id = $rec['database_id'];
-				$db_name = $rec['database_name'];
-				$db_backup_file = 'db_'.$db_name.'_'.date('Y-m-d_H-i').'.sql';
-				//$command = "mysqldump -h '".escapeshellcmd($clientdb_host)."' -u '".escapeshellcmd($clientdb_user)."' -p'".escapeshellcmd($clientdb_password)."' -c --add-drop-table --create-options --quick --result-file='".$db_backup_dir.'/'.$db_backup_file."' '".$db_name."'";
-				$command = "mysqldump -h ".escapeshellarg($clientdb_host)." -u ".escapeshellarg($clientdb_user)." -p".escapeshellarg($clientdb_password)." -c --add-drop-table --create-options --quick --result-file='".$db_backup_dir.'/'.$db_backup_file."' '".$db_name."'";
-				exec($command, $tmp_output, $retval);
-
-				//* Compress the backup with gzip
-				if($retval == 0) exec("gzip -c '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file)."' > '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file).".gz'", $tmp_output, $retval);
-
-				if($retval == 0){
-					if(is_file($db_backup_dir.'/'.$db_backup_file.'.gz')){
-						chmod($db_backup_dir.'/'.$db_backup_file.'.gz', 0750);
-						chown($db_backup_dir.'/'.$db_backup_file.'.gz', fileowner($db_backup_dir));
-						chgrp($db_backup_dir.'/'.$db_backup_file.'.gz', filegroup($db_backup_dir));
-
-						//* Insert web backup record in database
-						//$insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
-						//$app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
-						$sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
-						$app->db->query($sql);
-						if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+					$web_path = $rec['document_root'];
+					$web_user = $rec['system_user'];
+					$web_group = $rec['system_group'];
+					$web_id = $rec['domain_id'];
+					$web_backup_dir = $backup_dir.'/web'.$web_id;
+					if(!is_dir($web_backup_dir)) mkdir($web_backup_dir, 0750);
+					chmod($web_backup_dir, 0750);
+					//if(isset($server_config['backup_dir_ftpread']) && $server_config['backup_dir_ftpread'] == 'y') {
+					chown($web_backup_dir, $rec['system_user']);
+					chgrp($web_backup_dir, $rec['system_group']);
+					/*} else {
+						chown($web_backup_dir, 'root');
+						chgrp($web_backup_dir, 'root');
+					}*/
+				
+					$backup_excludes = '';
+					$b_excludes = explode(',', trim($rec['backup_excludes']));
+					if(is_array($b_excludes) && !empty($b_excludes)){
+						foreach($b_excludes as $b_exclude){
+							$b_exclude = trim($b_exclude);
+							if($b_exclude != ''){
+								$backup_excludes .= ' --exclude='.escapeshellarg($b_exclude);
+							}
+						}
 					}
-				} else {
-					if(is_file($db_backup_dir.'/'.$db_backup_file.'.gz')) unlink($db_backup_dir.'/'.$db_backup_file.'.gz');
-				}
-				//* Remove the uncompressed file
-				if(is_file($db_backup_dir.'/'.$db_backup_file)) unlink($db_backup_dir.'/'.$db_backup_file);
-
-				//* Remove old backups
-				$backup_copies = intval($rec['backup_copies']);
-
-				$dir_handle = dir($db_backup_dir);
-				$files = array();
-				while (false !== ($entry = $dir_handle->read())) {
-					if($entry != '.' && $entry != '..' && preg_match('/^db_(.*?)_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}\.sql.gz$/', $entry, $matches) && is_file($db_backup_dir.'/'.$entry)) {
-						if(array_key_exists($matches[1], $files) == false) $files[$matches[1]] = array();
-						$files[$matches[1]][] = $entry;
+				
+					if($backup_mode == 'userzip') {
+						//* Create a .zip backup as web user and include also files owned by apache / nginx user
+						$web_backup_file = 'web'.$web_id.'_'.date('Y-m-d_H-i').'.zip';
+						exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -group '.escapeshellarg($web_group).' -print 2> /dev/null | zip -b /tmp --exclude=./backup\*'.$backup_excludes.' --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
+						if($retval == 0 || $retval == 12) exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -user '.escapeshellarg($http_server_user).' -print 2> /dev/null | zip -b /tmp --exclude=./backup\*'.$backup_excludes.' --update --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
+					} else {
+						//* Create a tar.gz backup as root user
+						$web_backup_file = 'web'.$web_id.'_'.date('Y-m-d_H-i').'.tar.gz';
+						exec('tar pczf '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' --exclude=./backup\*'.$backup_excludes.' --directory '.escapeshellarg($web_path).' .', $tmp_output, $retval);
 					}
-				}
-				$dir_handle->close();
+					if($retval == 0 || ($backup_mode != 'userzip' && $retval == 1) || ($backup_mode == 'userzip' && $retval == 12)) { // tar can return 1, zip can return 12(due to harmless warings) and still create valid backups  
+						if(is_file($web_backup_dir.'/'.$web_backup_file)){
+							chown($web_backup_dir.'/'.$web_backup_file, 'root');
+							chgrp($web_backup_dir.'/'.$web_backup_file, 'root');
+							chmod($web_backup_dir.'/'.$web_backup_file, 0750);
 
-				reset($files);
-				foreach($files as $db_name => $filelist) {
-					rsort($filelist);
+							//* Insert web backup record in database
+							//$insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
+							//$app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
+							$sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
+							$app->db->query($sql);
+							if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+						}
+					} else {
+						if(is_file($web_backup_dir.'/'.$web_backup_file)) unlink($web_backup_dir.'/'.$web_backup_file);
+					}
+
+					//* Remove old backups
+					$backup_copies = intval($rec['backup_copies']);
+
+					$dir_handle = dir($web_backup_dir);
+					$files = array();
+					while (false !== ($entry = $dir_handle->read())) {
+						if($entry != '.' && $entry != '..' && substr($entry, 0, 3) == 'web' && is_file($web_backup_dir.'/'.$entry)) {
+							$files[] = $entry;
+						}
+					}
+					$dir_handle->close();
+
+					rsort($files);
+
 					for ($n = $backup_copies; $n <= 10; $n++) {
-						if(isset($filelist[$n]) && is_file($db_backup_dir.'/'.$filelist[$n])) {
-							unlink($db_backup_dir.'/'.$filelist[$n]);
-							//$sql = "SELECT backup_id FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($filelist[$n])."'";
+						if(isset($files[$n]) && is_file($web_backup_dir.'/'.$files[$n])) {
+							unlink($web_backup_dir.'/'.$files[$n]);
+							//$sql = "SELECT backup_id FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($files[$n])."'";
 							//$tmp = $app->dbmaster->queryOneRecord($sql);
+							//$app->dbmaster->datalogDelete('web_backup', 'backup_id', $tmp['backup_id']);
 							//$sql = "DELETE FROM web_backup WHERE backup_id = ".intval($tmp['backup_id']);
-							$sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($filelist[$n])."'";
+							$sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($files[$n])."'";
 							$app->db->query($sql);
 							if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
 						}
 					}
+
+					unset($files);
+					unset($dir_handle);
+
+					//* Remove backupdir symlink and create as directory instead
+					$app->system->web_folder_protection($web_path, false);
+
+					if(is_link($web_path.'/backup')) {
+						unlink($web_path.'/backup');
+					}
+					if(!is_dir($web_path.'/backup')) {
+						mkdir($web_path.'/backup');
+						chown($web_path.'/backup', $rec['system_user']);
+						chgrp($web_path.'/backup', $rec['system_group']);
+					}
+
+					$app->system->web_folder_protection($web_path, true);
 				}
 
-				unset($files);
-				unset($dir_handle);
+				/* If backup_interval is set to none and we have a
+				backup directory for the website, then remove the backups */
+				if($rec['backup_interval'] == 'none' || $rec['backup_interval'] == '') {
+					$web_id = $rec['domain_id'];
+					$web_user = $rec['system_user'];
+					$web_backup_dir = realpath($backup_dir.'/web'.$web_id);
+					if(is_dir($web_backup_dir)) {
+						exec('sudo -u '.escapeshellarg($web_user).' rm -f '.escapeshellarg($web_backup_dir.'/*'));
+						$sql = "DELETE FROM web_backup WHERE server_id = ".intval($conf['server_id'])." AND parent_domain_id = ".intval($web_id);
+						$app->db->query($sql);
+						if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+					}
+				}
 			}
 		}
 
-		unset($clientdb_host);
-		unset($clientdb_user);
-		unset($clientdb_password);
+		$sql = "SELECT * FROM web_database WHERE server_id = ".$conf['server_id']." AND backup_interval != 'none' AND backup_interval != ''";
+		$records = $app->db->queryAllRecords($sql);
+		if(is_array($records)) {
 
-	}
+			include 'lib/mysql_clientdb.conf';
 
-	// remove non-existing backups from database
-	$backups = $app->db->queryAllRecords("SELECT * FROM web_backup WHERE server_id = ".$conf['server_id']);
-	if(is_array($backups) && !empty($backups)){
-		foreach($backups as $backup){
-			$backup_file = $backup_dir.'/web'.$backup['parent_domain_id'].'/'.$backup['filename'];
-			if(!is_file($backup_file)){
-				$sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = ".$backup['parent_domain_id']." AND filename = '".$backup['filename']."'";
-				$app->db->query($sql);
-				if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+			foreach($records as $rec) {
+
+				//* Do the database backup
+				if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
+
+					$web_id = $rec['parent_domain_id'];
+					$db_backup_dir = $backup_dir.'/web'.$web_id;
+					if(!is_dir($db_backup_dir)) mkdir($db_backup_dir, 0750);
+					chmod($db_backup_dir, 0750);
+					chown($db_backup_dir, 'root');
+					chgrp($db_backup_dir, 'root');
+
+					//* Do the mysql database backup with mysqldump
+					$db_id = $rec['database_id'];
+					$db_name = $rec['database_name'];
+					$db_backup_file = 'db_'.$db_name.'_'.date('Y-m-d_H-i').'.sql';
+					//$command = "mysqldump -h '".escapeshellcmd($clientdb_host)."' -u '".escapeshellcmd($clientdb_user)."' -p'".escapeshellcmd($clientdb_password)."' -c --add-drop-table --create-options --quick --result-file='".$db_backup_dir.'/'.$db_backup_file."' '".$db_name."'";
+					$command = "mysqldump -h ".escapeshellarg($clientdb_host)." -u ".escapeshellarg($clientdb_user)." -p".escapeshellarg($clientdb_password)." -c --add-drop-table --create-options --quick --result-file='".$db_backup_dir.'/'.$db_backup_file."' '".$db_name."'";
+					exec($command, $tmp_output, $retval);
+
+					//* Compress the backup with gzip
+					if($retval == 0) exec("gzip -c '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file)."' > '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file).".gz'", $tmp_output, $retval);
+
+					if($retval == 0){
+						if(is_file($db_backup_dir.'/'.$db_backup_file.'.gz')){
+							chmod($db_backup_dir.'/'.$db_backup_file.'.gz', 0750);
+							chown($db_backup_dir.'/'.$db_backup_file.'.gz', fileowner($db_backup_dir));
+							chgrp($db_backup_dir.'/'.$db_backup_file.'.gz', filegroup($db_backup_dir));
+
+							//* Insert web backup record in database
+							//$insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
+							//$app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
+							$sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
+							$app->db->query($sql);
+							if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+						}
+					} else {
+						if(is_file($db_backup_dir.'/'.$db_backup_file.'.gz')) unlink($db_backup_dir.'/'.$db_backup_file.'.gz');
+					}
+					//* Remove the uncompressed file
+					if(is_file($db_backup_dir.'/'.$db_backup_file)) unlink($db_backup_dir.'/'.$db_backup_file);
+
+					//* Remove old backups
+					$backup_copies = intval($rec['backup_copies']);
+
+					$dir_handle = dir($db_backup_dir);
+					$files = array();
+					while (false !== ($entry = $dir_handle->read())) {
+						if($entry != '.' && $entry != '..' && preg_match('/^db_(.*?)_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}\.sql.gz$/', $entry, $matches) && is_file($db_backup_dir.'/'.$entry)) {
+							if(array_key_exists($matches[1], $files) == false) $files[$matches[1]] = array();
+							$files[$matches[1]][] = $entry;
+						}
+					}
+					$dir_handle->close();
+
+					reset($files);
+					foreach($files as $db_name => $filelist) {
+						rsort($filelist);
+						for ($n = $backup_copies; $n <= 10; $n++) {
+							if(isset($filelist[$n]) && is_file($db_backup_dir.'/'.$filelist[$n])) {
+								unlink($db_backup_dir.'/'.$filelist[$n]);
+								//$sql = "SELECT backup_id FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($filelist[$n])."'";
+								//$tmp = $app->dbmaster->queryOneRecord($sql);
+								//$sql = "DELETE FROM web_backup WHERE backup_id = ".intval($tmp['backup_id']);
+								$sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($filelist[$n])."'";
+								$app->db->query($sql);
+								if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+							}
+						}
+					}
+
+					unset($files);
+					unset($dir_handle);
+				}
 			}
+
+			unset($clientdb_host);
+			unset($clientdb_user);
+			unset($clientdb_password);
+
+		}
+
+		// remove non-existing backups from database
+		$backups = $app->db->queryAllRecords("SELECT * FROM web_backup WHERE server_id = ".$conf['server_id']);
+		if(is_array($backups) && !empty($backups)){
+			foreach($backups as $backup){
+				$backup_file = $backup_dir.'/web'.$backup['parent_domain_id'].'/'.$backup['filename'];
+				if(!is_file($backup_file)){
+					$sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = ".$backup['parent_domain_id']." AND filename = '".$backup['filename']."'";
+					$app->db->query($sql);
+					if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+				}
+			}
+		}
+	} else {
+		//* send email to admin that backup directory could not be mounted
+		$global_config = $app->getconf->get_global_config('mail');
+		if($global_config['admin_mail'] != ''){
+			$subject = 'Backup directory '.$backup_dir.' could not be mounted';
+			$message = "Backup directory ".$backup_dir." could not be mounted.\n\nThe command\n\n".$server_config['backup_dir_mount_cmd']."\n\nfailed.";
+			mail($global_config['admin_mail'], $subject, $message);
 		}
 	}
 }

--
Gitblit v1.9.1