source: pro-violet-viettel/www/deploy/20150304/application/modules/ajax/controllers/download.php

Last change on this file was 871, checked in by quyenla, 10 years ago

download

  • Property svn:executable set to *
File size: 7.5 KB
RevLine 
[862]1<?php
[831]2
[862]3if (!defined('BASEPATH'))
4    exit('No direct script access allowed');
[831]5
[862]6class Download extends MX_Controller {
[831]7
[862]8    public function __construct() {
9        parent::__construct();
10        $user = $this->session->userdata('userInfo');
11        $this->vservices->setApiUrl($this->config->item('api_url'));
12        $this->vservices->setConnection($this->curl);
13        $this->vservices->setUserId($user['us_id']);
14    }
15
16    public function getFile() {
[864]17
[862]18        $user = $this->session->userdata('userInfo');
19        $data = $this->input->post('data');
20        $treeArray = json_decode($data, TRUE);
21
[871]22        foreach ($treeArray as $index=>$tree)
[867]23        {
24            $treeArray[$index]['name']=$this->convert_vi_to_en($treeArray[$index]['name']);
25            $treeArray[$index]['fileurl']=$this->encodePath($treeArray[$index]['fileurl']);
26            foreach ($tree['childFiles'] as $index2=>$file)
27            {
28                $treeArray[$index]['childFiles'][$index2]['name']=$this->convert_vi_to_en($treeArray[$index]['childFiles'][$index2]['name']);
29                $treeArray[$index]['childFiles'][$index2]['fileurl']=$this->encodePath($treeArray[$index]['childFiles'][$index2]['fileurl']);
30            }
31            foreach ($tree['childDirs'] as $index2=>$file)
32            {
33                $treeArray[$index]['childDirs'][$index2]['name']=$this->convert_vi_to_en($treeArray[$index]['childDirs'][$index2]['name']);
34                $treeArray[$index]['childDirs'][$index2]['fileurl']=$this->encodePath($treeArray[$index]['childDirs'][$index2]['fileurl']);
35            }
36           
37        }
38       
[862]39        if ((count($treeArray) == 1) && ($treeArray[0]['type'] == 'file')) {
[864]40           
[862]41            $file = $treeArray[0];
42            $local_file = "downloads/" . $file['name'];
43            $download_file = $file['name'];
44            $filecontent = file_get_contents($file['fileurl']);
45            file_put_contents($local_file, $filecontent);
46        } else {
[868]47            // create parent folder
[864]48            $download_file = $user['username'] . "_" . date("d-m-Y");
[862]49            $folder = "downloads/" . $download_file;
50            mkdir($folder, 0777);
[864]51            foreach ($treeArray as $idx=>$tree) {
52               
53                if ($tree['type'] == 'file') {
54                    $path = $folder . "/" . $tree['name'];
55                    $filecontent = file_get_contents($tree['fileurl']);
[862]56                    file_put_contents($path, $filecontent);
[858]57                }
[864]58                if ($tree['type'] == 'directory') {
[868]59                    // create subfolder
[864]60                    mkdir($folder . "/" . $tree['name']);
61                    $childDirs = $this->aasort($tree['childDirs'], "id");
62                    foreach ($childDirs as $index => $dir) {
63                   
64                        if ($dir['parentID'] == $tree['id']) {
65                            $path = $folder . "/" . $tree['name'] . "/" . $dir['name'];
66                            mkdir($path, 0777);
67                            $childDirs[$index]['path'] = $path;
68                        } else {
69                            foreach ($childDirs as $index2 => $dir2) {
70                                if ($dir2['id'] == $dir['parentID']) {
71                                    $path = $childDirs[$index2]['path'] . "/" . $dir['name'];
72                                    mkdir($path, 0777);
73                                    $childDirs[$index]['path'] = $path;
[862]74                                }
75                            }
76                        }
77                    }
78                    $childFiles = $tree['childFiles'];
[864]79                    foreach ($childFiles as $index => $file) {
[868]80                        // add file to folder
[864]81                        if ($file['parentID'] == $tree['id']) {
82                            $path = $folder . "/" . $tree['name'] . "/" . $file['name'];
[867]83                           
[862]84                            $filecontent = file_get_contents($file['fileurl']);
85                            file_put_contents($path, $filecontent);
[864]86                        } else {
87                            foreach ($childDirs as $index2 => $dir2) {
88                                if ($dir2['id'] == $file['parentID']) {
89                                    $path = $childDirs[$index2]['path'] . "/" . $file['name'];
90                                    $filecontent = file_get_contents($file['fileurl']);
91                                    file_put_contents($path, $filecontent);
[862]92                                }
93                            }
94                        }
95                    }
96                }
97            }
[868]98            // zip folder
[864]99            $command = "tar -C downloads -cvf " . $folder . ".zip" . " $download_file";
100
[863]101            exec($command);
[864]102            $local_file = $folder . ".zip";
103            $download_file = $download_file . ".zip";
[862]104        }
[864]105        $mime = "application/octet-stream";
106        if (get_mime_by_extension($local_file) != "") {
107            $mime = get_mime_by_extension($local_file);
108        }
[862]109        header('Cache-control: private');
[864]110        header('Content-Type: ' . $mime);
[862]111        header('Content-Length: ' . filesize($local_file));
[864]112        header('Content-Disposition: attachment; filename=' . $download_file);
113        $download_rate = 2048;
[862]114        flush();
115        $file = fopen($local_file, "r");
116
117        while (!feof($file)) {
118            print fread($file, round($download_rate * 1024));
119            flush();
120            sleep(1);
121        }
122        fclose($file);
[864]123        unlink($local_file);
124        if (isset($folder)) {
125            exec("rm -fr $folder");
[863]126        }
[862]127        exit;
[841]128    }
[831]129
[864]130    private function aasort($array, $key) {
[862]131        $sorter = array();
132        $ret = array();
133        reset($array);
134        foreach ($array as $ii => $va) {
135            $sorter[$ii] = $va[$key];
136        }
137        asort($sorter);
138        foreach ($sorter as $ii => $va) {
139            $ret[$ii] = $array[$ii];
140        }
141        $array = $ret;
142        return $array;
143    }
144
[864]145    private function encodePath($path) {
146        $fileName = $path;
147        $fileNames = explode("/", $fileName);
148        $name = $fileNames[(count($fileNames) - 1)];
149        $fileName = str_replace($name, "", $fileName) . str_replace("+", "%20", urlencode($name));
150        return $fileName;
151    }
152
153    private function convert_vi_to_en($str) {
154        $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", "a", $str);
155        $str = preg_replace("/(Ú|é|ẹ|ẻ|ẜ|ê|ề|ế|ệ|ể|ễ)/", "e", $str);
156        $str = preg_replace("/(ì|í|ị|ỉ|Ä©)/", "i", $str);
157        $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ÃŽ|ồ|ố|ộ|ổ|ỗ|Æ¡|ờ|ớ|ợ|ở|ỡ)/", "o", $str);
158        $str = preg_replace("/(ù|ú|ụ|á»§|Å©|ư|ừ|ứ|á»±|á»­|ữ)/", "u", $str);
159        $str = preg_replace("/(ỳ|Ü|ỵ|á»·|ỹ)/", "y", $str);
160        $str = preg_replace("/(đ)/", "d", $str);
161        $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ẋ|Ẁ|Ậ|ẚ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẏ)/", "A", $str);
162        $str = preg_replace("/(È|É|ẞ|Ẻ|Ẍ|Ê|Ề|ẟ|Ệ|Ể|Ễ)/", "E", $str);
163        $str = preg_replace("/(Ì|Í|Ị|Ỉ|Äš)/", "I", $str);
164        $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Æ |Ờ|Ớ|Ợ|Ở|á» )/", "O", $str);
165        $str = preg_replace("/(Ù|Ú|Ề|Ị|Åš|Ư|Ừ|Ớ|á»°|Ử|á»®)/", "U", $str);
166        $str = preg_replace("/(Ỳ|Ý|Ỏ|á»¶|Ở)/", "Y", $str);
167        $str = preg_replace("/(Đ)/", "D", $str);
168        $str=  str_replace(" ", "_", $str);
169        return $str;
170    }
171
[831]172}
173
174/* End of file privatecontent.php */
175/* Location: ./application/modules/ajax/controllers/privatecontent.php */
Note: See TracBrowser for help on using the repository browser.