From 761b2d05d49722fdaa2c26a9a90296a9124eeac7 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 30 Apr 2013 17:24:11 +0200
Subject: [PATCH] Fix: delete of files with [ and glob.

---
 build/debian/dolibarr.postinst | 22 +++++++++++-----------
 htdocs/core/lib/files.lib.php  |  6 ++++--
 test/phpunit/FilesLibTest.php  | 12 +++++++++++-
 3 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/build/debian/dolibarr.postinst b/build/debian/dolibarr.postinst
index 92112e29e50..3f83dfede1d 100644
--- a/build/debian/dolibarr.postinst
+++ b/build/debian/dolibarr.postinst
@@ -94,17 +94,17 @@ case "$1" in
 		else
 			# File already exist. We add params not found.
 			echo Add new params to overwrite path to use shared libraries/fonts
-			grep -q -c "dolibarr_lib_ADODB_PATH" $config    || [ ! -d "/usr/share/php/adodb" ]  || echo "<?php \$dolibarr_lib_ADODB_PATH='/usr/share/php/adodb'; ?>" >> $config
-			grep -q -c "dolibarr_lib_FPDI_PATH" $config     || [ ! -d "/usr/share/php/fpdi" ]   || echo "<?php \$dolibarr_lib_FPDI_PATH='/usr/share/php/fpdi'; ?>" >> $config
-			#grep -q -c "dolibarr_lib_GEOIP_PATH" $config    || echo "<?php \$dolibarr_lib_GEOIP_PATH=''; ?>" >> $config
-			grep -q -c "dolibarr_lib_NUSOAP_PATH" $config   || [ ! -d "/usr/share/php/nusoap" ] || echo "<?php \$dolibarr_lib_NUSOAP_PATH='/usr/share/php/nusoap'; ?>" >> $config
-			grep -q -c "dolibarr_lib_ODTPHP_PATHTOPCLZIP" $config || [ ! -d "/usr/share/php/libphp-pclzip" ]  || echo "<?php \$dolibarr_lib_ODTPHP_PATHTOPCLZIP='/usr/share/php/libphp-pclzip'; ?>" >> $config
-			#grep -q -c "dolibarr_lib_PHPEXCEL_PATH" $config || echo "<?php \$dolibarr_lib_PHPEXCEL_PATH=''; ?>" >> $config
-			#grep -q -c "dolibarr_lib_TCPDF_PATH" $config    || echo "<?php \$dolibarr_lib_TCPDF_PATH=''; ?>" >> $config
-			grep -q -c "dolibarr_js_CKEDITOR" $config       || [ ! -d "/usr/share/javascript/ckeditor" ]  || echo "<?php \$dolibarr_js_CKEDITOR='/javascript/ckeditor'; ?>" >> $config
-			grep -q -c "dolibarr_js_JQUERY" $config         || [ ! -d "/usr/share/javascript/jquery" ]    || echo "<?php \$dolibarr_js_JQUERY='/javascript/jquery'; ?>" >> $config
-			grep -q -c "dolibarr_js_JQUERY_UI" $config      || [ ! -d "/usr/share/javascript/jquery-ui" ] || echo "<?php \$dolibarr_js_JQUERY_UI='/javascript/jquery-ui'; ?>" >> $config
-			grep -q -c "dolibarr_js_JQUERY_FLOT" $config    || [ ! -d "/usr/share/javascript/flot" ]      || echo "<?php \$dolibarr_js_JQUERY_FLOT='/javascript/flot'; ?>" >> $config
+			grep -q -c "dolibarr_lib_ADODB_PATH" $config            || [ -d "/usr/share/dolibarr/htdocs/includes/adodbtime" ]  || echo "<?php \$dolibarr_lib_ADODB_PATH='/usr/share/php/adodb'; ?>" >> $config
+			grep -q -c "dolibarr_lib_FPDI_PATH" $config             || [ -d "/usr/share/dolibarr/htdocs/includes/fpdfi" ]   || echo "<?php \$dolibarr_lib_FPDI_PATH='/usr/share/php/fpdi'; ?>" >> $config
+			#grep -q -c "dolibarr_lib_GEOIP_PATH" $config           || echo "<?php \$dolibarr_lib_GEOIP_PATH=''; ?>" >> $config
+			grep -q -c "dolibarr_lib_NUSOAP_PATH" $config           || [ -d "/usr/share/dolibarr/htdocs/includes/nusoap" ] || echo "<?php \$dolibarr_lib_NUSOAP_PATH='/usr/share/php/nusoap'; ?>" >> $config
+			grep -q -c "dolibarr_lib_ODTPHP_PATHTOPCLZIP" $config   || [ ! -d "/usr/share/dolibarr/htdocs/includes/odtphp/zip/pclzip ]  || echo "<?php \$dolibarr_lib_ODTPHP_PATHTOPCLZIP='/usr/share/php/libphp-pclzip'; ?>" >> $config
+			#grep -q -c "dolibarr_lib_PHPEXCEL_PATH" $config        || echo "<?php \$dolibarr_lib_PHPEXCEL_PATH=''; ?>" >> $config
+			#grep -q -c "dolibarr_lib_TCPDF_PATH" $config           || echo "<?php \$dolibarr_lib_TCPDF_PATH=''; ?>" >> $config
+			grep -q -c "dolibarr_js_CKEDITOR" $config               || [ -d "/usr/share/dolibarr/htdocs/includes/ckeditor" ]  || echo "<?php \$dolibarr_js_CKEDITOR='/javascript/ckeditor'; ?>" >> $config
+			grep -q -c "dolibarr_js_JQUERY" $config                 || [ -d "/usr/share/dolibarr/htdocs/includes/jquery/js" ]    || echo "<?php \$dolibarr_js_JQUERY='/javascript/jquery'; ?>" >> $config
+			grep -q -c "dolibarr_js_JQUERY_UI" $config              || [ -d "/usr/share/dolibarr/htdocs/includes/jquery/js" ] || echo "<?php \$dolibarr_js_JQUERY_UI='/javascript/jquery-ui'; ?>" >> $config
+			grep -q -c "dolibarr_js_JQUERY_FLOT" $config            || [ -d "/usr/share/dolibarr/htdocs/includes/jquery/plugins/flot" ]      || echo "<?php \$dolibarr_js_JQUERY_FLOT='/javascript/flot'; ?>" >> $config
 			grep -q -c "dolibarr_font_DOL_DEFAULT_TTF_BOLD" $config || echo "<?php \$dolibarr_font_DOL_DEFAULT_TTF_BOLD='/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf'; ?>" >> $config			
 		fi
 	
diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php
index 02d3cc16a76..05f2c9009ae 100644
--- a/htdocs/core/lib/files.lib.php
+++ b/htdocs/core/lib/files.lib.php
@@ -729,12 +729,14 @@ function dol_delete_file($file,$disableglob=0,$nophperrors=0,$nohook=0,$object=n
 	{
 		$error=0;
 
-		//print "x".$file." ".$disableglob;
+		//print "x".$file." ".$disableglob;exit;
 		$ok=true;
 		$file_osencoded=dol_osencode($file);    // New filename encoded in OS filesystem encoding charset
 		if (empty($disableglob) && ! empty($file_osencoded))
 		{
-			foreach (glob($file_osencoded) as $filename)
+			$globencoded=str_replace('[','\[',$file_osencoded);
+			$globencoded=str_replace(']','\]',$globencoded);
+			foreach (glob($globencoded) as $filename)
 			{
 				if ($nophperrors) $ok=@unlink($filename);  // The unlink encapsulated by dolibarr
 				else $ok=unlink($filename);  // The unlink encapsulated by dolibarr
diff --git a/test/phpunit/FilesLibTest.php b/test/phpunit/FilesLibTest.php
index 858e096aa36..771635f34eb 100644
--- a/test/phpunit/FilesLibTest.php
+++ b/test/phpunit/FilesLibTest.php
@@ -326,7 +326,7 @@ class FilesLibTest extends PHPUnit_Framework_TestCase
         // Again to test with overwriting=1
         $result=dol_copy($file, $conf->admin->dir_temp.'/file.csv',0,1);
         print __METHOD__." result=".$result."\n";
-        $this->assertGreaterThanOrEqual(1,$result,'copy destination already eists, overwrite');    // Should be 1
+        $this->assertGreaterThanOrEqual(1,$result,'copy destination already exists, overwrite');    // Should be 1
 
         // Again to test with overwriting=1
         $result=dol_move($conf->admin->dir_temp.'/file.csv',$conf->admin->dir_temp.'/file2.csv',0,1);
@@ -341,6 +341,16 @@ class FilesLibTest extends PHPUnit_Framework_TestCase
         $result=dol_delete_file($conf->admin->dir_temp.'/file2.csv');
         print __METHOD__." result=".$result."\n";
         $this->assertTrue($result,'delete file that does not exists');
+
+        // Test copy with special char / delete with blob
+        $result=dol_copy($file, $conf->admin->dir_temp.'/file with [x] and é.csv',0,1);
+        print __METHOD__." result=".$result."\n";
+        $this->assertGreaterThanOrEqual(1,$result,'copy destination already exists, overwrite');    // Should be 1
+        
+        // Try to delete using a glob criteria
+        $result=dol_delete_file($conf->admin->dir_temp.'/file with [x]*é.csv');
+        print __METHOD__." result=".$result."\n";
+        $this->assertTrue($result,'delete file that does not exists');
     }
 
     /**
-- 
GitLab