Index: pro-violet-viettel/www/deploy/20150304/application/modules/ajax/controllers/download.php
===================================================================
--- pro-violet-viettel/www/deploy/20150304/application/modules/ajax/controllers/download.php	(revision 863)
+++ pro-violet-viettel/www/deploy/20150304/application/modules/ajax/controllers/download.php	(revision 864)
@@ -15,9 +15,36 @@
 
     public function getFile() {
+
         $user = $this->session->userdata('userInfo');
         $data = $this->input->post('data');
         $treeArray = json_decode($data, TRUE);
 
+        foreach ($treeArray as $idx=>$tree) {
+                $treeArray[$idx]['fileurl'] = $this->encodePath($treeArray[$idx]['fileurl']);
+                $treeArray[$idx]['name']= $this->convert_vi_to_en($treeArray[$idx]['name']);
+                if ($tree['type'] == 'file') {
+                    $path = $folder . "/" . $tree['name'];
+                    $filecontent = file_get_contents($tree['fileurl']);
+                    file_put_contents($path, $filecontent);
+                }
+                if ($tree['type'] == 'directory') {  
+                    $childDirs = $this->aasort($tree['childDirs'], "id");
+                    foreach ($childDirs as $index => $dir) {
+                        $treeArray[$idx]['childDirs'][$index]['fileurl'] = $this->encodePath($treeArray[$idx]['childDirs'][$index]['fileurl']);
+                        $treeArray[$idx]['childDirs'][$index]['name']= $this->convert_vi_to_en($treeArray[$idx]['childDirs'][$index]['name']);
+                }
+                $childDirs = $this->aasort($tree['childFiles'], "id");
+                    foreach ($childDirs as $index => $dir) {
+                        echo $dir['name']."<br />";
+                        $treeArray[$idx]['childDirs'][$index]['fileurl'] = $this->encodePath($treeArray[$idx]['childDirs'][$index]['fileurl']);
+                        $treeArray[$idx]['childDirs'][$index]['name']= $this->convert_vi_to_en($treeArray[$idx]['childDirs'][$index]['name']);
+                }
+                
+                    }
+                
+        }
+die();
         if ((count($treeArray) == 1) && ($treeArray[0]['type'] == 'file')) {
+           
             $file = $treeArray[0];
             $local_file = "downloads/" . $file['name'];
@@ -26,61 +53,46 @@
             file_put_contents($local_file, $filecontent);
         } else {
-            $download_file=$user['username']."_".date("d-m-Y");
+            $download_file = $user['username'] . "_" . date("d-m-Y");
             $folder = "downloads/" . $download_file;
             mkdir($folder, 0777);
-            foreach ($treeArray as $tree)
-            {
-                if($tree['type']=='file')
-                {
-                    $path=$folder."/".$tree['name'];
-                    $filecontent = file_get_contents($file['fileurl']);
+            foreach ($treeArray as $idx=>$tree) {
+                
+                if ($tree['type'] == 'file') {
+                    $path = $folder . "/" . $tree['name'];
+                    $filecontent = file_get_contents($tree['fileurl']);
                     file_put_contents($path, $filecontent);
                 }
-                if($tree['type']=='directory')
-                {
-                    mkdir($folder."/".$tree['name']);
-                    $childDirs = $this->aasort($tree['childDirs'],"id");
-                    foreach ($childDirs as $index=>$dir)
-                    {
-                        if($dir['parentID']==$tree['id'])
-                        {
-                           
-                        $path=$folder."/".$tree['name']."/".$dir['name'];
-                        mkdir($path,0777);
-                        $childDirs[$index]['path']=$path;
-                        }
-                        else
-                        {
-                            foreach ($childDirs as $index2=>$dir2)
-                            {
-                                if($dir2['id']==$dir['parentID'])
-                                {
-                                   $path=$childDirs[$index2]['path']."/".$dir['name'];
-                                   mkdir($path,0777);
-                                   $childDirs[$index]['path']=$path;
+                if ($tree['type'] == 'directory') {
+                    mkdir($folder . "/" . $tree['name']);
+                    $childDirs = $this->aasort($tree['childDirs'], "id");
+                    foreach ($childDirs as $index => $dir) {
+                   
+                        if ($dir['parentID'] == $tree['id']) {
+                            $path = $folder . "/" . $tree['name'] . "/" . $dir['name'];
+                            mkdir($path, 0777);
+                            $childDirs[$index]['path'] = $path;
+                        } else {
+                            foreach ($childDirs as $index2 => $dir2) {
+                                if ($dir2['id'] == $dir['parentID']) {
+                                    $path = $childDirs[$index2]['path'] . "/" . $dir['name'];
+                                    mkdir($path, 0777);
+                                    $childDirs[$index]['path'] = $path;
                                 }
                             }
-                        
                         }
                     }
                     $childFiles = $tree['childFiles'];
-                    foreach ($childFiles as $index=>$file)
-                    {
-                        if ($file['parentID']==$tree['id'])
-                        {
-                            $path=$folder."/".$tree['name']."/".$file['name'];
-                            
+                    foreach ($childFiles as $index => $file) {
+                        if ($file['parentID'] == $tree['id']) {
+                            $path = $folder . "/" . $tree['name'] . "/" . $file['name'];
+                            $file['fileurl'] = $this->encodePath($file['fileurl']);
                             $filecontent = file_get_contents($file['fileurl']);
                             file_put_contents($path, $filecontent);
-                        }
-                        else
-                        {
-                            foreach ($childDirs as $index2=>$dir2)
-                            {
-                                if($dir2['id']==$file['parentID'])
-                                {
-                                   $path=$childDirs[$index2]['path']."/".$file['name'];
-                                   $filecontent = file_get_contents($file['fileurl']);
-                                   file_put_contents($path, $filecontent);
+                        } else {
+                            foreach ($childDirs as $index2 => $dir2) {
+                                if ($dir2['id'] == $file['parentID']) {
+                                    $path = $childDirs[$index2]['path'] . "/" . $file['name'];
+                                    $filecontent = file_get_contents($file['fileurl']);
+                                    file_put_contents($path, $filecontent);
                                 }
                             }
@@ -89,49 +101,37 @@
                 }
             }
-            
-            $command="tar -C downloads -cvf ".$folder.".zip"." $download_file";
-           
+
+            $command = "tar -C downloads -cvf " . $folder . ".zip" . " $download_file";
+
             exec($command);
-            $local_file = $folder.".zip";
-            $download_file = $download_file.".zip"; 
-           
-            
+            $local_file = $folder . ".zip";
+            $download_file = $download_file . ".zip";
         }
-
+        $mime = "application/octet-stream";
+        if (get_mime_by_extension($local_file) != "") {
+            $mime = get_mime_by_extension($local_file);
+        }
         header('Cache-control: private');
-        header('Content-Type: application/zip');
+        header('Content-Type: ' . $mime);
         header('Content-Length: ' . filesize($local_file));
-        header('Content-Disposition: filename=' . $download_file);
-        $download_rate = 512;
-
-        // flush content
+        header('Content-Disposition: attachment; filename=' . $download_file);
+        $download_rate = 2048;
         flush();
-
-        // open file stream
         $file = fopen($local_file, "r");
 
         while (!feof($file)) {
-
-            // send the current file part to the browser
             print fread($file, round($download_rate * 1024));
-
-            // flush the content to the browser
             flush();
-
-            // sleep one second
             sleep(1);
         }
-
-        // close file stream
         fclose($file);
-            //unlink($local_file);
-        if (isset($folder))
-        {
-            //exec("rm -fr $folder");
+        unlink($local_file);
+        if (isset($folder)) {
+            exec("rm -fr $folder");
         }
         exit;
     }
 
-    public function aasort($array, $key) {
+    private function aasort($array, $key) {
         $sorter = array();
         $ret = array();
@@ -148,4 +148,33 @@
     }
 
+    private function encodePath($path) {
+        $fileName = $path;
+        $fileNames = explode("/", $fileName);
+        $name = $fileNames[(count($fileNames) - 1)];
+        $fileName = str_replace($name, "", $fileName) . str_replace("+", "%20", urlencode($name));
+        return $fileName;
+    }
+
+    private function convert_vi_to_en($str) {
+        $str = preg_replace("/(Ã |Ã¡|áº¡|áº£|Ã£|Ã¢|áº§|áº¥|áº­|áº©|áº«|Ä|áº±|áº¯|áº·|áº³|áºµ)/", "a", $str);
+        $str = preg_replace("/(Ãš|Ã©|áº¹|áº»|áºœ|Ãª|á»|áº¿|á»|á»|á»
+)/", "e", $str);
+        $str = preg_replace("/(Ã¬|Ã­|á»|á»|Ä©)/", "i", $str);
+        $str = preg_replace("/(Ã²|Ã³|á»|á»|Ãµ|ÃŽ|á»|á»|á»|á»|á»|Æ¡|á»|á»|á»£|á»|á»¡)/", "o", $str);
+        $str = preg_replace("/(Ã¹|Ãº|á»¥|á»§|Å©|Æ°|á»«|á»©|á»±|á»­|á»¯)/", "u", $str);
+        $str = preg_replace("/(á»³|Ãœ|á»µ|á»·|á»¹)/", "y", $str);
+        $str = preg_replace("/(Ä)/", "d", $str);
+        $str = preg_replace("/(Ã|Ã|áº |áº¢|Ã|Ã|áºŠ|áº€|áº¬|áºš|áºª|Ä|áº°|áº®|áº¶|áº²|áºŽ)/", "A", $str);
+        $str = preg_replace("/(Ã|Ã|áºž|áºº|áºŒ|Ã|á»|áºŸ|á»|á»|á»)/", "E", $str);
+        $str = preg_replace("/(Ã|Ã|á»|á»|Äš)/", "I", $str);
+        $str = preg_replace("/(Ã|Ã|á»|á»|Ã|Ã|á»|á»|á»|á»|á»|Æ |á»|á»|á»¢|á»|á» )/", "O", $str);
+        $str = preg_replace("/(Ã|Ã|á»€|á»Š|Åš|Æ¯|á»ª|á»š|á»°|á»¬|á»®)/", "U", $str);
+        $str = preg_replace("/(á»²|Ã|á»Ž|á»¶|á»ž)/", "Y", $str);
+        $str = preg_replace("/(Ä)/", "D", $str);
+//$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
+        $str=  str_replace(" ", "_", $str);
+        return $str;
+    }
+
 }
 
