From b553b2bf146cf20bd3ddfeba52ec5cc5b70a1da9 Mon Sep 17 00:00:00 2001
From: Marius Cramer <m.cramer@pixcept.de>
Date: Thu, 27 Nov 2014 02:31:46 -0500
Subject: [PATCH] Merge remote-tracking branch 'ispc/master' into new-layout-3.1

---
 interface/web/login/lib/lang/en.lng               |    3 
 interface/web/login/password_reset.php            |   75 ++++++------
 interface/web/admin/lib/lang/de_users.lng         |    1 
 interface/web/admin/form/users.tform.php          |   13 ++
 server/lib/classes/cron.d/500-backup_mail.inc.php |   38 ++---
 interface/web/mail/mail_domain_dkim_create.php    |    2 
 interface/web/admin/lib/lang/en_users.lng         |    1 
 server/plugins-available/apache2_plugin.inc.php   |   18 +--
 interface/web/login/lib/lang/de.lng               |    1 
 server/plugins-available/mail_plugin.inc.php      |   36 +----
 server/plugins-available/nginx_plugin.inc.php     |   22 +++
 interface/web/dns/dns_soa_edit.php                |    6 
 server/lib/classes/cron.d/500-backup.inc.php      |   66 +++++-----
 interface/web/admin/templates/users_user_edit.htm |    8 +
 server/lib/classes/system.inc.php                 |   16 ++
 server/plugins-available/backup_plugin.inc.php    |   38 -----
 16 files changed, 175 insertions(+), 169 deletions(-)

diff --git a/interface/web/admin/form/users.tform.php b/interface/web/admin/form/users.tform.php
index 9ee2970..6a23559 100644
--- a/interface/web/admin/form/users.tform.php
+++ b/interface/web/admin/form/users.tform.php
@@ -260,6 +260,19 @@
 			'maxlength' => '2',
 			'rows'  => '',
 			'cols'  => ''
+		),
+		'lost_password_function' => array (
+			'datatype' => 'INTEGER',
+			'formtype' => 'CHECKBOX',
+			'regex'  => '',
+			'errmsg' => '',
+			'default' => 1,
+			'value'  => array(0 => 0, 1 => 1),
+			'separator' => '',
+			'width'  => '30',
+			'maxlength' => '255',
+			'rows'  => '',
+			'cols'  => ''
 		)
 		//#################################
 		// ENDE Datenbankfelder
diff --git a/interface/web/admin/lib/lang/de_users.lng b/interface/web/admin/lib/lang/de_users.lng
index da26db3..db37a60 100644
--- a/interface/web/admin/lib/lang/de_users.lng
+++ b/interface/web/admin/lib/lang/de_users.lng
@@ -31,4 +31,5 @@
 $wb['password_match_txt'] = 'Die Passwörter stimmen überein.';
 $wb['username_error_collision'] = 'Der Benutzername darf nicht <b>web<b> oder <b>web<b> gefolgt von einer Zahl sein.';
 $wb['client_not_admin_err'] = 'A user that belongs to a client can not be set to type: admin';
+$wb['lost_password_function_txt'] = 'Passwort vergessen Funktion steht zur Verfügung';
 ?>
diff --git a/interface/web/admin/lib/lang/en_users.lng b/interface/web/admin/lib/lang/en_users.lng
index 09b8ac3..9c57f1d 100644
--- a/interface/web/admin/lib/lang/en_users.lng
+++ b/interface/web/admin/lib/lang/en_users.lng
@@ -31,4 +31,5 @@
 $wb['password_match_txt'] = 'The passwords do match.';
 $wb['username_error_collision'] = 'The username may not be web or web plus a number."';
 $wb['client_not_admin_err'] = 'A user that belongs to a client can not be set to type: admin';
+$wb['lost_password_function_txt'] = 'Forgot password function is available';
 ?>
diff --git a/interface/web/admin/templates/users_user_edit.htm b/interface/web/admin/templates/users_user_edit.htm
index b973a34..91c35bb 100644
--- a/interface/web/admin/templates/users_user_edit.htm
+++ b/interface/web/admin/templates/users_user_edit.htm
@@ -60,6 +60,12 @@
                     {tmpl_var name='language'}
                 </select>
             </div>
+            <div class="ctrlHolder">
+                <p class="label">{tmpl_var name='lost_password_function_txt'}</p>
+                <div class="multiField">
+                    {tmpl_var name='lost_password_function'}
+                </div>
+            </div>
         </fieldset>
             
         <input type="hidden" name="id" value="{tmpl_var name='id'}">
@@ -70,4 +76,4 @@
         </div>
     </div>
   
-</div>
\ No newline at end of file
+</div>
diff --git a/interface/web/dns/dns_soa_edit.php b/interface/web/dns/dns_soa_edit.php
index 96088ef..e39c377 100644
--- a/interface/web/dns/dns_soa_edit.php
+++ b/interface/web/dns/dns_soa_edit.php
@@ -283,7 +283,11 @@
 	if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
 		//* We do not allow users to change a domain which has been created by the admin
 		$rec = $app->db->queryOneRecord("SELECT origin from dns_soa WHERE id = ".$this->id);
-		if(isset($this->dataRecord["origin"]) && $rec['origin'] != $this->dataRecord["origin"] && $app->tform->checkPerm($this->id, 'u')) {
+		$drOrigin = (isset($this->dataRecord['origin']))
+			? $app->functions->idn_encode($this->dataRecord['origin'])
+			: false;
+
+		if($rec['origin'] !== $drOrigin && $app->tform->checkPerm($this->id, 'u')) {
 			//* Add a error message and switch back to old server
 			$app->tform->errorMessage .= $app->lng('The Zone (soa) can not be changed. Please ask your Administrator if you want to change the Zone name.');
 			$this->dataRecord["origin"] = $rec['origin'];
diff --git a/interface/web/login/lib/lang/de.lng b/interface/web/login/lib/lang/de.lng
index 4ece1b0..44f677b 100644
--- a/interface/web/login/lib/lang/de.lng
+++ b/interface/web/login/lib/lang/de.lng
@@ -24,4 +24,5 @@
 $wb['back_txt'] = 'Zur&uuml;ck';
 $wb['stay_logged_in_txt'] = 'Dauerhaft eingeloggt bleiben';
 $wb['email_error'] = 'Email contains unallowed characters or has a invalid format.';
+$wb['lost_password_function_disabled_txt'] = 'Die Passwort vergessen Funktion steht für diesen Benutzer nicht zur Verfügung.';
 ?>
diff --git a/interface/web/login/lib/lang/en.lng b/interface/web/login/lib/lang/en.lng
index e540ef2..e7487da 100644
--- a/interface/web/login/lib/lang/en.lng
+++ b/interface/web/login/lib/lang/en.lng
@@ -24,4 +24,5 @@
 $wb['error_maintenance_mode'] = 'This ISPConfig installation is currently under maintenance. We should be back shortly. Thank you for your patience.';
 $wb['theme_not_compatible'] = 'The chosen theme is not compatible with the current ISPConfig version. Please check for a new version of the theme.<br />The default theme as been activated automatically.';
 $wb['stay_logged_in_txt'] = 'Keep me logged in';
-?>
\ No newline at end of file
+$wb['lost_password_function_disabled_txt'] = 'The lost password function is not available for this user.';
+?>
diff --git a/interface/web/login/password_reset.php b/interface/web/login/password_reset.php
index c466ac8..f2e4e95 100644
--- a/interface/web/login/password_reset.php
+++ b/interface/web/login/password_reset.php
@@ -7,14 +7,14 @@
 Redistribution and use in source and binary forms, with or without modification,
 are permitted provided that the following conditions are met:
 
-    * Redistributions of source code must retain the above copyright notice,
-      this list of conditions and the following disclaimer.
-    * Redistributions in binary form must reproduce the above copyright notice,
-      this list of conditions and the following disclaimer in the documentation
-      and/or other materials provided with the distribution.
-    * Neither the name of ISPConfig nor the names of its contributors
-      may be used to endorse or promote products derived from this software without
-      specific prior written permission.
+	* Redistributions of source code must retain the above copyright notice,
+	  this list of conditions and the following disclaimer.
+	* Redistributions in binary form must reproduce the above copyright notice,
+	  this list of conditions and the following disclaimer in the documentation
+	  and/or other materials provided with the distribution.
+	* Neither the name of ISPConfig nor the names of its contributors
+	  may be used to endorse or promote products derived from this software without
+	  specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
@@ -26,7 +26,7 @@
 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 
 require_once '../../lib/config.inc.php';
 require_once '../../lib/app.inc.php';
@@ -54,36 +54,39 @@
 	$username = $app->db->quote($_POST['username']);
 	$email = $app->db->quote($_POST['email']);
 
-	$client = $app->db->queryOneRecord("SELECT * FROM client WHERE username = '$username' AND email = '$email'");
+	$client = $app->db->queryOneRecord("SELECT client.*, sys_user.lost_password_function FROM client,sys_user WHERE client.username = '$username' AND client.email = '$email' AND client.client_id = sys_user.client_id");
 
-	if($client['client_id'] > 0) {
-		$new_password = $app->auth->get_random_password();
-		$new_password_encrypted = $app->auth->crypt_password($new_password);
-		$new_password_encrypted = $app->db->quote($new_password_encrypted);
-
-		$username = $app->db->quote($client['username']);
-		$app->db->query("UPDATE sys_user SET passwort = '$new_password_encrypted' WHERE username = '$username'");
-		$app->db->query("UPDATE client SET password = '$new_password_encrypted' WHERE username = '$username'");
-		$app->tpl->setVar("message", $wb['pw_reset']);
-
-		$app->uses('getconf,ispcmail');
-		$mail_config = $app->getconf->get_global_config('mail');
-		if($mail_config['smtp_enabled'] == 'y') {
-			$mail_config['use_smtp'] = true;
-			$app->ispcmail->setOptions($mail_config);
-		}
-		$app->ispcmail->setSender($mail_config['admin_mail'], $mail_config['admin_name']);
-		$app->ispcmail->setSubject($wb['pw_reset_mail_title']);
-		$app->ispcmail->setMailText($wb['pw_reset_mail_msg'].$new_password);
-		$app->ispcmail->send(array($client['contact_name'] => $client['email']));
-		$app->ispcmail->finish();
-
-		$app->plugin->raiseEvent('password_reset', true);
-		$app->tpl->setVar("msg", $wb['pw_reset']);
+	if($client['lost_password_function'] == 0) {
+		$app->tpl->setVar("error", $wb['lost_password_function_disabled_txt']);
 	} else {
-		$app->tpl->setVar("error", $wb['pw_error']);
-	}
+		if($client['client_id'] > 0) {
+			$new_password = $app->auth->get_random_password();
+			$new_password_encrypted = $app->auth->crypt_password($new_password);
+			$new_password_encrypted = $app->db->quote($new_password_encrypted);
 
+			$username = $app->db->quote($client['username']);
+			$app->db->query("UPDATE sys_user SET passwort = '$new_password_encrypted' WHERE username = '$username'");
+			$app->db->query("UPDATE client SET password = '$new_password_encrypted' WHERE username = '$username'");
+			$app->tpl->setVar("message", $wb['pw_reset']);
+
+			$app->uses('getconf,ispcmail');
+			$mail_config = $app->getconf->get_global_config('mail');
+			if($mail_config['smtp_enabled'] == 'y') {
+				$mail_config['use_smtp'] = true;
+				$app->ispcmail->setOptions($mail_config);
+			}
+			$app->ispcmail->setSender($mail_config['admin_mail'], $mail_config['admin_name']);
+			$app->ispcmail->setSubject($wb['pw_reset_mail_title']);
+			$app->ispcmail->setMailText($wb['pw_reset_mail_msg'].$new_password);
+			$app->ispcmail->send(array($client['contact_name'] => $client['email']));
+			$app->ispcmail->finish();
+
+			$app->plugin->raiseEvent('password_reset', true);
+			$app->tpl->setVar("msg", $wb['pw_reset']);
+		} else {
+			$app->tpl->setVar("error", $wb['pw_error']);
+		}
+	}
 } else {
 	$app->tpl->setVar("msg", $wb['pw_error_noinput']);
 }
diff --git a/interface/web/mail/mail_domain_dkim_create.php b/interface/web/mail/mail_domain_dkim_create.php
index bd9ff9c..4b59596 100644
--- a/interface/web/mail/mail_domain_dkim_create.php
+++ b/interface/web/mail/mail_domain_dkim_create.php
@@ -163,7 +163,7 @@
 echo "<privatekey>".$private_key."</privatekey>\n";
 echo "<publickey>".$public_key."</publickey>\n";
 if ( validate_domain($_POST['domain']) ) {
-	echo '<dns_record>'.$selector.'_domainkey.'.$_POST['domain'].'. 3600	TXT	"v=DKIM1; t=s; p='.$dns_record.'"</dns_record>';
+	echo '<dns_record>'.$selector.'._domainkey.'.$_POST['domain'].'. 3600	TXT	"v=DKIM1; t=s; p='.$dns_record.'"</dns_record>';
 }
 echo "</formatname>\n";
 ?>
diff --git a/server/lib/classes/cron.d/500-backup.inc.php b/server/lib/classes/cron.d/500-backup.inc.php
index abca144..f87e4db 100644
--- a/server/lib/classes/cron.d/500-backup.inc.php
+++ b/server/lib/classes/cron.d/500-backup.inc.php
@@ -71,22 +71,13 @@
 			} else {
 				chmod(escapeshellcmd($backup_dir), $backup_dir_permissions);
 			}
-			
-			//* mount backup directory, if necessary
-			$run_backups = true;
-			$server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
-			if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
-				if(!$app->system->is_mounted($backup_dir)){
-					exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
-					sleep(1);
-					if(!$app->system->is_mounted($backup_dir)) $run_backups = false;
-				}
-			}
-
+            $run_backups = true;
+            //* mount backup directory, if necessary
+            if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $run_backups = false;
 			if($run_backups){
 				//* backup only active domains
-				$sql = "SELECT * FROM web_domain WHERE server_id = '".$conf['server_id']."' AND (type = 'vhost' OR type = 'vhostsubdomain' OR type = 'vhostalias') AND active = 'y'";
-				$records = $app->db->queryAllRecords($sql);
+				$sql = "SELECT * FROM web_domain WHERE server_id = ? AND (type = 'vhost' OR type = 'vhostsubdomain' OR type = 'vhostalias') AND active = 'y'";
+				$records = $app->db->queryAllRecords($sql, $conf['server_id']);
 				if(is_array($records)) {
 					foreach($records as $rec) {
 
@@ -138,9 +129,12 @@
 									//* 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);
+									$filesize = $app->functions->formatBytes(filesize($web_backup_dir.'/'.$web_backup_file));
+									$sql = "INSERT INTO web_backup (server_id, parent_domain_id, backup_type, backup_mode, tstamp, filename, filesize) VALUES (?, ?, ?, ?, ?, ?, ?)";
+									$app->db->query($sql, $conf['server_id'], $web_id, 'web', $backup_mode, time(), $web_backup_file, $filesize);
+									if($app->db->dbHost != $app->dbmaster->dbHost) 
+										$app->dbmaster->query($sql, $conf['server_id'], $web_id, 'web', $backup_mode, time(), $web_backup_file, $filesize);
+									unset($filesize);
 								}
 							} else {
 								if(is_file($web_backup_dir.'/'.$web_backup_file)) unlink($web_backup_dir.'/'.$web_backup_file);
@@ -167,9 +161,9 @@
 									//$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);
+									$sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ? AND filename = ?";
+									$app->db->query($sql, $conf['server_id'], $web_id, $files[$n]);
+									if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'],  $web_id, $files[$n]);
 								}
 							}
 
@@ -199,16 +193,15 @@
 							$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);
+								$sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ?";
+								$app->db->query($sql, $conf['server_id'], $web_id);
+								if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $web_id);
 							}
 						}
 					}
 				}
 
-				$sql = "SELECT * FROM web_database WHERE server_id = ".$conf['server_id']." AND backup_interval != 'none' AND backup_interval != ''";
-				$records = $app->db->queryAllRecords($sql);
+				$records = $app->db->queryAllRecords("SELECT * FROM web_database WHERE server_id = ? AND backup_interval != 'none' AND backup_interval != ''", $conf['server_id']);
 				if(is_array($records)) {
 
 					include 'lib/mysql_clientdb.conf';
@@ -245,9 +238,12 @@
 									//* 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);
+									$filesize = $app->functions->formatBytes(filesize($db_backup_dir.'/'.$db_backup_file.'.gz'));
+									$sql = "INSERT INTO web_backup (server_id, parent_domain_id, backup_type, backup_mode, tstamp, filename, filesize) VALUES (?, ?, ?, ?, ?, ?, ?)";
+									$app->db->query($sql, $conf['server_id'], $web_id, 'mysql', 'sqlgz', time(), $db_backup_file.'.gz', $filesize);
+									if($app->db->dbHost != $app->dbmaster->dbHost) 
+										$app->dbmaster->query($sql, $conf['server_id'], $web_id, 'mysql', 'sqlgz', time(), $db_backup_file.'.gz', $filesize);
+									unset($filesize);
 								}
 							} else {
 								if(is_file($db_backup_dir.'/'.$db_backup_file.'.gz')) unlink($db_backup_dir.'/'.$db_backup_file.'.gz');
@@ -277,9 +273,9 @@
 										//$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);
+										$sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ? AND filename = ?";
+										$app->db->query($sql, $conf['server_id'], $web_id, $filelist[$n]);
+										if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $web_id, $filelist[$n]);
 									}
 								}
 							}
@@ -296,14 +292,14 @@
 				}
 
 				// remove non-existing backups from database
-				$backups = $app->db->queryAllRecords("SELECT * FROM web_backup WHERE server_id = ".$conf['server_id']);
+				$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);
+							$sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ? AND filename = ?";
+							$app->db->query($sql, $conf['server_id'], $backup['parent_domain_id'], $backup['filename']);
+							if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $backup['parent_domain_id'], $backup['filename']);
 						}
 					}
 				}
diff --git a/server/lib/classes/cron.d/500-backup_mail.inc.php b/server/lib/classes/cron.d/500-backup_mail.inc.php
index 8740c55..2473afe 100644
--- a/server/lib/classes/cron.d/500-backup_mail.inc.php
+++ b/server/lib/classes/cron.d/500-backup_mail.inc.php
@@ -59,19 +59,11 @@
 		if($backup_mode == '') $backup_mode = 'userzip';
 
 		if($backup_dir != '') {
-			//* mount backup directory, if necessary
 			$run_backups = true;
-			$server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
-			if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
-				if(!$app->system->is_mounted($backup_dir)){
-					exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
-					sleep(1);
-					if(!$app->system->is_mounted($backup_dir)) $run_backups = false;
-				}
-			}
+			//* mount backup directory, if necessary
+			if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $run_backups = false;
 
-			$sql = "SELECT * FROM mail_user WHERE server_id = '".intval($conf['server_id'])."' AND maildir <> ''";
-			$records = $app->db->queryAllRecords($sql);
+			$records = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE server_id = ? AND maildir <> ''", intval($conf['server_id']));
 
 			if(is_array($records) && $run_backups) {
 				if(!is_dir($backup_dir)) {
@@ -83,10 +75,10 @@
 				foreach($records as $rec) {
 					//* Do the mailbox backup
 					if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
-						$email = $rec['email'][1];
-						$sql="SELECT * FROM mail_domain WHERE domain = ?" . $app->db->quote(explode("@",$email))."'";
+						$email = $rec['email'];
+						$email=explode("@",$email)[1];
+						$domain_rec=$app->db->queryOneRecord("SELECT * FROM mail_domain WHERE domain = ?", $email);
 						unset($email);
-						$domain_rec=$app->db->queryOneRecord($sql);
 
 						$mail_backup_dir = $backup_dir.'/mail'.$domain_rec['domain_id'];
 						if(!is_dir($mail_backup_dir)) mkdir($mail_backup_dir, 0750);
@@ -116,9 +108,9 @@
 							chgrp($mail_backup_dir.'/'.$mail_backup_file, 'root');
 							chmod($mail_backup_dir.'/'.$mail_backup_file, 0640);
 							/* Insert mail backup record in database */
-							$sql = "INSERT INTO mail_backup (server_id,parent_domain_id,mailuser_id,backup_mode,tstamp,filename,filesize) VALUES (".$conf['server_id'].",".$domain_rec['domain_id'].",".$rec['mailuser_id'].",'".$backup_mode."',".time().",'".$app->db->quote($mail_backup_file)."','".$app->functions->formatBytes(filesize($mail_backup_dir.'/'.$mail_backup_file))."')";
-							$app->db->query($sql);	
-							if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+							$sql = "INSERT INTO mail_backup (server_id, parent_domain_id, mailuser_id, backup_mode, tstamp, filename, filesize) VALUES (?, ?, ?, ?, ?, ?, ?)";
+							$app->db->query($sql, $conf['server_id'], $domain_rec['domain_id'], $rec['mailuser_id'], $backup_mode, time(), $mail_backup_file, $app->functions->formatBytes(filesize($mail_backup_dir.'/'.$mail_backup_file)));	
+							if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $domain_rec['domain_id'], $rec['mailuser_id'], $backup_mode, time(), $mail_backup_file, $app->functions->formatBytes(filesize($mail_backup_dir.'/'.$mail_backup_file)));
 						} else {
 							/* Backup failed - remove archive */
 							if(is_file($mail_backup_dir.'/'.$mail_backup_file)) unlink($mail_backup_dir.'/'.$mail_backup_file);
@@ -138,9 +130,9 @@
 						for ($n = $backup_copies; $n <= 10; $n++) {
 							if(isset($files[$n]) && is_file($mail_backup_dir.'/'.$files[$n])) {
 								unlink($mail_backup_dir.'/'.$files[$n]);
-								$sql = "DELETE FROM mail_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = ".$domain_rec['domain_id']." AND filename = '".$app->db->quote($files[$n])."'";
-								$app->db->query($sql);
-								if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+								$sql = "DELETE FROM mail_backup WHERE server_id = ? AND parent_domain_id = ? AND filename = ?";
+								$app->db->query($sql, $conf['server_id'], $domain_rec['domain_id'], $files[$n]);
+								if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $domain_rec['domain_id'], $files[$n]);
 							}
 						}
 						unset($files);
@@ -149,9 +141,9 @@
 					/* Remove inactive backups */
 					if($rec['backup_interval'] == 'none') {
 						/* remove backups from db */
-						$sql = "DELETE FROM mail_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = ".$domain_rec['domain_id']." AND mailuser_id = ".$rec['mailuser_id'];
-						$app->db->query($sql);
-						if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+						$sql = "DELETE FROM mail_backup WHERE server_id = ? AND parent_domain_id = ? AND mailuser_id = ?";
+						$app->db->query($sql, $conf['server_id'], $domain_rec['domain_id'], $rec['mailuser_id']);
+						if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $domain_rec['domain_id'], $rec['mailuser_id']);
 						/* remove archives */
 						$mail_backup_dir = $backup_dir.'/mail'.$rec['domain_id'];	
 						$mail_backup_file = 'mail'.$rec['mailuser_id'].'_*';
diff --git a/server/lib/classes/system.inc.php b/server/lib/classes/system.inc.php
index 952df1a..75dc73d 100644
--- a/server/lib/classes/system.inc.php
+++ b/server/lib/classes/system.inc.php
@@ -1801,6 +1801,22 @@
 		return $return_var == 0 ? true : false;
 	}
 
+	function mount_backup_dir($backup_dir, $mount_cmd = '/usr/local/ispconfig/server/scripts/backup_dir_mount.sh'){
+		$mounted = true;
+		if ( 	is_file($mount_cmd) &&
+				is_executable($mount_cmd) &&
+				fileowner($mount_cmd) === 0
+		) {
+			if (!$this->is_mounted($backup_dir)){
+				exec($backup_dir_mount_cmd);
+				sleep(1);
+				if (!$this->is_mounted($backup_dir)) $mounted = false;
+			}
+		} else $mounted = false;
+
+		return $mounted;
+	}
+				
 	function getinitcommand($servicename, $action, $init_script_directory = ''){
 		global $conf;
 		// upstart
diff --git a/server/plugins-available/apache2_plugin.inc.php b/server/plugins-available/apache2_plugin.inc.php
index e6ca66f..2bf4dfe 100644
--- a/server/plugins-available/apache2_plugin.inc.php
+++ b/server/plugins-available/apache2_plugin.inc.php
@@ -2048,25 +2048,19 @@
 			if($data['old']['type'] == 'vhost') {
 				$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
 				$backup_dir = $server_config['backup_dir'];
-				//* mount backup directory, if necessary
 				$mount_backup = true;
-				$server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
 				if($server_config['backup_dir'] != '' && $server_config['backup_delete'] == 'y') {
-					if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
-						if(!$app->system->is_mounted($backup_dir)){
-							exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
-							sleep(1);
-							if(!$app->system->is_mounted($backup_dir)) $mount_backup = false;
-						}
-					}
+					//* mount backup directory, if necessary
+					if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $mount_backup = false;
+
 					if($mount_backup){
 						$web_backup_dir = $backup_dir.'/web'.$data_old['domain_id'];
 						//** do not use rm -rf $web_backup_dir because database(s) may exits
 						exec(escapeshellcmd('rm -f '.$web_backup_dir.'/web'.$data_old['domain_id'].'_').'*');
 						//* cleanup database
-						$sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = ".$data_old['domain_id']." AND filename LIKE 'web".$data_old['domain_id']."_%'";
-						$app->db->query($sql);
-						if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+						$sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ? AND filename LIKE ?";
+						$app->db->query($sql, $conf['server_id'], $data_old['domain_id'], "web".$data_old['domain_id']."_%");
+						if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $data_old['domain_id'], "web".$data_old['domain_id']."_%");
 
 						$app->log('Deleted the web backup files', LOGLEVEL_DEBUG);
 					}
diff --git a/server/plugins-available/backup_plugin.inc.php b/server/plugins-available/backup_plugin.inc.php
index 42c1d77..6184dd5 100644
--- a/server/plugins-available/backup_plugin.inc.php
+++ b/server/plugins-available/backup_plugin.inc.php
@@ -72,30 +72,9 @@
 			$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
 			$backup_dir = $server_config['backup_dir'].'/web'.$web['domain_id'];
 			
-			//* mount backup directory, if necessary
-			/*
 			$backup_dir_is_ready = true;
-			$server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
-			if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
-				if(!$app->system->is_mounted($server_config['backup_dir'])){
-					exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
-					sleep(1);
-					if(!$app->system->is_mounted($server_config['backup_dir'])) $backup_dir_is_ready = false;
-				}
-			}*/
-			$backup_dir_is_ready = 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($server_config['backup_dir'])) $backup_dir_is_ready = false;
-				}
-			}
+            //* mount backup directory, if necessary
+            if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $backup_dir_is_ready = false;
 
 			if($backup_dir_is_ready){
 				//* Make backup available for download
@@ -186,17 +165,10 @@
 			$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
 			$backup_dir = $server_config['backup_dir'];
 
-			//* mount backup directory, if necessary
 			$backup_dir_is_ready = true;
-			$server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
-			if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
-				if(!$app->system->is_mounted($backup_dir)){
-					exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
-					sleep(1);
-					if(!$app->system->is_mounted($backup_dir)) $backup_dir_is_ready = false;
-				}
-			}
-			
+			//* mount backup directory, if necessary
+			if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $backup_dir_is_ready = false;
+
 			if($backup_dir_is_ready){
 				$mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
 				$domain_rec = $app->db->queryOneRecord("SELECT * FROM mail_domain WHERE domain_id = ".intval($mail_backup['parent_domain_id']));
diff --git a/server/plugins-available/mail_plugin.inc.php b/server/plugins-available/mail_plugin.inc.php
index 5ac0951..06b3017 100644
--- a/server/plugins-available/mail_plugin.inc.php
+++ b/server/plugins-available/mail_plugin.inc.php
@@ -398,17 +398,10 @@
 		//* Delete the mail-backups
 		$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
 		$backup_dir = $server_config['backup_dir'];
-		//* mount backup directory, if necessary
 		$mount_backup = true;
-		$server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
 		if($server_config['backup_dir'] != '' && $maildir_path_deleted && $server_config['backup_delete'] == 'y') {
-			if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
-				if(!$app->system->is_mounted($backup_dir)){
-					exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
-					sleep(1);
-					if(!$app->system->is_mounted($backup_dir)) $mount_backup = false;
-				}
-			}
+			//* mount backup directory, if necessary
+			if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $mount_backup = false;
 			if($mount_backup){
 				$sql = "SELECT * FROM mail_domain WHERE domain = '".explode("@",$data['old']['email'])[1]."'";
 				$domain_rec = $app->db->queryOneRecord($sql);
@@ -416,13 +409,11 @@
 				$mail_backup_files = 'mail'.$data['old']['mailuser_id'];
 				exec(escapeshellcmd('rm -f '.$mail_backup_dir.'/'.$mail_backup_files).'*');
 				//* cleanup database
-				$sql = "DELETE FROM mail_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = ".$domain_rec['domain_id']." AND mailuser_id = ".$data['old']['mailuser_id'];
-				$app->db->query($sql);
-				if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+				$sql = "DELETE FROM mail_backup WHERE server_id = ? AND parent_domain_id = ? AND mailuser_id = ?";
+				$app->db->query($sql, $conf['server_id'], $domain_rec['domain_id'], $data['old']['mailuser_id']);
+				if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $domain_rec['domain_id'], $data['old']['mailuser_id']);
 
 				$app->log('Deleted the mail backups for: '.$data['old']['email'], LOGLEVEL_DEBUG);
-				
-				
 			}
 		}
 	}
@@ -457,24 +448,17 @@
 		//* Delete the mail-backups
 		$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
 		$backup_dir = $server_config['backup_dir'];
-		//* mount backup directory, if necessary
 		$mount_backup = true;
-		$server_config['backup_dir_mount_cmd'] = trim($server_config['backup_dir_mount_cmd']);
 		if($server_config['backup_dir'] != '' && $maildomain_path_deleted && $server_config['backup_delete'] == 'y'){
-			if($server_config['backup_dir_is_mount'] == 'y' && $server_config['backup_dir_mount_cmd'] != ''){
-				if(!$app->system->is_mounted($backup_dir)){
-					exec(escapeshellcmd($server_config['backup_dir_mount_cmd']));
-					sleep(1);
-					if(!$app->system->is_mounted($backup_dir)) $mount_backup = false;
-				}
-			}
+			//* mount backup directory, if necessary
+			if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $mount_backup = false;
 			if($mount_backup){
 				$mail_backup_dir = $backup_dir.'/mail'.$data['old']['domain_id'];
 				exec(escapeshellcmd('rm -rf '.$mail_backup_dir));
 				//* cleanup database
-				$sql = "DELETE FROM mail_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = ".$data['old']['domain_id'];
-				$app->db->query($sql);
-				if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
+				$sql = "DELETE FROM mail_backup WHERE server_id = ? AND parent_domain_id = ? AND mailuser_id = ?";
+				$app->db->query($sql, $conf['server_id'], $domain_rec['domain_id'], $data['old']['mailuser_id']);
+				if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $domain_rec['domain_id'], $data['old']['mailuser_id']);
 
 				$app->log('Deleted the mail backup directory: '.$mail_backup_dir, LOGLEVEL_DEBUG);
 			}
diff --git a/server/plugins-available/nginx_plugin.inc.php b/server/plugins-available/nginx_plugin.inc.php
index 77ac10a..57044ef 100644
--- a/server/plugins-available/nginx_plugin.inc.php
+++ b/server/plugins-available/nginx_plugin.inc.php
@@ -2046,6 +2046,28 @@
 				$this->awstats_delete($data, $web_config);
 			}
 
+			//* Delete the web-backups
+			if($data['old']['type'] == 'vhost') {
+				$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
+				$backup_dir = $server_config['backup_dir'];
+				$mount_backup = true;
+				if($server_config['backup_dir'] != '' && $server_config['backup_delete'] == 'y') {
+					//* mount backup directory, if necessary
+					if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $mount_backup = false;
+					if($mount_backup){
+						$web_backup_dir = $backup_dir.'/web'.$data_old['domain_id'];
+						//** do not use rm -rf $web_backup_dir because database(s) may exits
+						exec(escapeshellcmd('rm -f '.$web_backup_dir.'/web'.$data_old['domain_id'].'_').'*');
+						//* cleanup database
+						$sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ? AND filename LIKE ?";
+						$app->db->query($sql, $conf['server_id'], $data_old['domain_id'], "web".$data_old['domain_id']."_%");
+						if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $data_old['domain_id'], "web".$data_old['domain_id']."_%");
+
+						$app->log('Deleted the web backup files', LOGLEVEL_DEBUG);
+					}
+				}
+			}
+
 			$app->services->restartServiceDelayed('httpd', 'reload');
 
 		}

--
Gitblit v1.9.1