From ed49f8844629df216f3512344d2eaff0f156f008 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Mon, 31 Mar 2014 00:19:31 +0200
Subject: [PATCH] Fix: deleting files into backup system tools. Fix: Dump using
 php not not include lock on tables that are deleted.

---
 htdocs/admin/tools/dolibarr_export.php |  2 +-
 htdocs/admin/tools/export.php          | 85 ++++++++++++++------------
 2 files changed, 47 insertions(+), 40 deletions(-)

diff --git a/htdocs/admin/tools/dolibarr_export.php b/htdocs/admin/tools/dolibarr_export.php
index 31a11f260f9..847b20783b2 100644
--- a/htdocs/admin/tools/dolibarr_export.php
+++ b/htdocs/admin/tools/dolibarr_export.php
@@ -50,7 +50,7 @@ if (! $user->admin)
 
 if ($action == 'delete')
 {
-	$file=$conf->admin->dir_output.'/backup/'.GETPOST('urlfile');
+	$file=$conf->admin->dir_output.'/'.GETPOST('urlfile');
     $ret=dol_delete_file($file, 1);
     if ($ret) setEventMessage($langs->trans("FileWasRemoved", GETPOST('urlfile')));
     else setEventMessage($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), 'errors');
diff --git a/htdocs/admin/tools/export.php b/htdocs/admin/tools/export.php
index 8dbd7a1912e..4e85a4d3096 100644
--- a/htdocs/admin/tools/export.php
+++ b/htdocs/admin/tools/export.php
@@ -58,7 +58,7 @@ if ($file && ! $what)
 
 if ($action == 'delete')
 {
-	$file=$conf->admin->dir_output.'/backup/'.GETPOST('urlfile');
+	$file=$conf->admin->dir_output.'/'.GETPOST('urlfile');
 	$ret=dol_delete_file($file, 1);
 	if ($ret) setEventMessage($langs->trans("FileWasRemoved", GETPOST('urlfile')));
 	else setEventMessage($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), 'errors');
@@ -451,46 +451,53 @@ function backup_tables($outputfile, $tables='*')
         //fwrite($handle,"/*!40101 SET character_set_client = utf8 */;\n");
         $resqldrop=$db->query('SHOW CREATE TABLE '.$table);
         $row2 = $db->fetch_row($resqldrop);
-        fwrite($handle,$row2[1].";\n");
-        //fwrite($handle,"/*!40101 SET character_set_client = @saved_cs_client */;\n\n");
-
-
-        // Dumping the data (locking the table and disabling the keys check while doing the process)
-        fwrite($handle, "\n--\n-- Dumping data for table `".$table."`\n--\n");
-        if (!GETPOST("nobin_nolocks")) fwrite($handle, "LOCK TABLES `".$table."` WRITE;\n"); // Lock the table before inserting data (when the data will be imported back)
-        if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` DISABLE KEYS;\n");
-
-        $sql='SELECT * FROM '.$table;
-        $result = $db->query($sql);
-        $num_fields = $db->num_rows($result);
-        while($row = $db->fetch_row($result)) {
-            // For each row of data we print a line of INSERT
-            fwrite($handle,'INSERT '.$delayed.$ignore.'INTO `'.$table.'` VALUES (');
-            $columns = count($row);
-            for($j=0; $j<$columns; $j++) {
-                // Processing each columns of the row to ensure that we correctly save the value (eg: add quotes for string - in fact we add quotes for everything, it's easier)
-                if ($row[$j] == null and !is_string($row[$j])) {
-                    // IMPORTANT: if the field is NULL we set it NULL
-                    $row[$j] = 'NULL';
-                } elseif(is_string($row[$j]) and $row[$j] == '') {
-                    // if it's an empty string, we set it as an empty string
-                    $row[$j] = "''";
-                } elseif(is_numeric($row[$j]) and !strcmp($row[$j], $row[$j]+0) ) { // test if it's a numeric type and the numeric version ($nb+0) == string version (eg: if we have 01, it's probably not a number but rather a string, else it would not have any leading 0)
-                    // if it's a number, we return it as-is
-                    $row[$j] = $row[$j];
-                } else { // else for all other cases we escape the value and put quotes around
-                    $row[$j] = addslashes($row[$j]);
-                    $row[$j] = preg_replace("#\n#", "\\n", $row[$j]);
-                    $row[$j] = "'".$row[$j]."'";
-                }
-            }
-            fwrite($handle,implode(',', $row).");\n");
+        if (empty($row2[1]))
+        {
+        	fwrite($handle, "\n-- WARNING: Show create table ".$table." return empy string when it should not.\n");
         }
-        if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` ENABLE KEYS;\n"); // Enabling back the keys/index checking
-        if (!GETPOST("nobin_nolocks")) fwrite($handle, "UNLOCK TABLES;\n"); // Unlocking the table
-        fwrite($handle,"\n\n\n");
+        else
+        {
+        	fwrite($handle,$row2[1].";\n");
+	        //fwrite($handle,"/*!40101 SET character_set_client = @saved_cs_client */;\n\n");
+
+	        // Dumping the data (locking the table and disabling the keys check while doing the process)
+	        fwrite($handle, "\n--\n-- Dumping data for table `".$table."`\n--\n");
+	        if (!GETPOST("nobin_nolocks")) fwrite($handle, "LOCK TABLES `".$table."` WRITE;\n"); // Lock the table before inserting data (when the data will be imported back)
+	        if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` DISABLE KEYS;\n");
+        
+	        $sql='SELECT * FROM '.$table;
+	        $result = $db->query($sql);
+	        $num_fields = $db->num_rows($result);
+	        while($row = $db->fetch_row($result)) 
+	        {
+	            // For each row of data we print a line of INSERT
+	            fwrite($handle,'INSERT '.$delayed.$ignore.'INTO `'.$table.'` VALUES (');
+	            $columns = count($row);
+	            for($j=0; $j<$columns; $j++) {
+	                // Processing each columns of the row to ensure that we correctly save the value (eg: add quotes for string - in fact we add quotes for everything, it's easier)
+	                if ($row[$j] == null and !is_string($row[$j])) {
+	                    // IMPORTANT: if the field is NULL we set it NULL
+	                    $row[$j] = 'NULL';
+	                } elseif(is_string($row[$j]) and $row[$j] == '') {
+	                    // if it's an empty string, we set it as an empty string
+	                    $row[$j] = "''";
+	                } elseif(is_numeric($row[$j]) and !strcmp($row[$j], $row[$j]+0) ) { // test if it's a numeric type and the numeric version ($nb+0) == string version (eg: if we have 01, it's probably not a number but rather a string, else it would not have any leading 0)
+	                    // if it's a number, we return it as-is
+	                    $row[$j] = $row[$j];
+	                } else { // else for all other cases we escape the value and put quotes around
+	                    $row[$j] = addslashes($row[$j]);
+	                    $row[$j] = preg_replace("#\n#", "\\n", $row[$j]);
+	                    $row[$j] = "'".$row[$j]."'";
+	                }
+	            }
+	            fwrite($handle,implode(',', $row).");\n");
+	        }
+	        if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` ENABLE KEYS;\n"); // Enabling back the keys/index checking
+	        if (!GETPOST("nobin_nolocks")) fwrite($handle, "UNLOCK TABLES;\n"); // Unlocking the table
+	        fwrite($handle,"\n\n\n");
+	    }
     }
-
+    
     /* Backup Procedure structure*/
     /*
      $result = $db->query('SHOW PROCEDURE STATUS');
-- 
GitLab