1 | <?php |
---|
2 | |
---|
3 | if (!defined('BASEPATH')) |
---|
4 | exit('No direct script access allowed'); |
---|
5 | |
---|
6 | class Download extends MX_Controller { |
---|
7 | |
---|
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() { |
---|
17 | |
---|
18 | $user = $this->session->userdata('userInfo'); |
---|
19 | $data = $this->input->post('data'); |
---|
20 | $treeArray = json_decode($data, TRUE); |
---|
21 | |
---|
22 | foreach ($treeArray as $index=>$tree) |
---|
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 | |
---|
39 | if ((count($treeArray) == 1) && ($treeArray[0]['type'] == 'file')) { |
---|
40 | |
---|
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 { |
---|
47 | $download_file = $user['username'] . "_" . date("d-m-Y"); |
---|
48 | $folder = "downloads/" . $download_file; |
---|
49 | mkdir($folder, 0777); |
---|
50 | foreach ($treeArray as $idx=>$tree) { |
---|
51 | |
---|
52 | if ($tree['type'] == 'file') { |
---|
53 | $path = $folder . "/" . $tree['name']; |
---|
54 | $filecontent = file_get_contents($tree['fileurl']); |
---|
55 | file_put_contents($path, $filecontent); |
---|
56 | } |
---|
57 | if ($tree['type'] == 'directory') { |
---|
58 | mkdir($folder . "/" . $tree['name']); |
---|
59 | $childDirs = $this->aasort($tree['childDirs'], "id"); |
---|
60 | foreach ($childDirs as $index => $dir) { |
---|
61 | |
---|
62 | if ($dir['parentID'] == $tree['id']) { |
---|
63 | $path = $folder . "/" . $tree['name'] . "/" . $dir['name']; |
---|
64 | mkdir($path, 0777); |
---|
65 | $childDirs[$index]['path'] = $path; |
---|
66 | } else { |
---|
67 | foreach ($childDirs as $index2 => $dir2) { |
---|
68 | if ($dir2['id'] == $dir['parentID']) { |
---|
69 | $path = $childDirs[$index2]['path'] . "/" . $dir['name']; |
---|
70 | mkdir($path, 0777); |
---|
71 | $childDirs[$index]['path'] = $path; |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | $childFiles = $tree['childFiles']; |
---|
77 | foreach ($childFiles as $index => $file) { |
---|
78 | if ($file['parentID'] == $tree['id']) { |
---|
79 | $path = $folder . "/" . $tree['name'] . "/" . $file['name']; |
---|
80 | |
---|
81 | $filecontent = file_get_contents($file['fileurl']); |
---|
82 | file_put_contents($path, $filecontent); |
---|
83 | } else { |
---|
84 | foreach ($childDirs as $index2 => $dir2) { |
---|
85 | if ($dir2['id'] == $file['parentID']) { |
---|
86 | $path = $childDirs[$index2]['path'] . "/" . $file['name']; |
---|
87 | $filecontent = file_get_contents($file['fileurl']); |
---|
88 | file_put_contents($path, $filecontent); |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | $command = "tar -C downloads -cvf " . $folder . ".zip" . " $download_file"; |
---|
97 | |
---|
98 | exec($command); |
---|
99 | $local_file = $folder . ".zip"; |
---|
100 | $download_file = $download_file . ".zip"; |
---|
101 | } |
---|
102 | $mime = "application/octet-stream"; |
---|
103 | if (get_mime_by_extension($local_file) != "") { |
---|
104 | $mime = get_mime_by_extension($local_file); |
---|
105 | } |
---|
106 | header('Cache-control: private'); |
---|
107 | header('Content-Type: ' . $mime); |
---|
108 | header('Content-Length: ' . filesize($local_file)); |
---|
109 | header('Content-Disposition: attachment; filename=' . $download_file); |
---|
110 | $download_rate = 2048; |
---|
111 | flush(); |
---|
112 | $file = fopen($local_file, "r"); |
---|
113 | |
---|
114 | while (!feof($file)) { |
---|
115 | print fread($file, round($download_rate * 1024)); |
---|
116 | flush(); |
---|
117 | sleep(1); |
---|
118 | } |
---|
119 | fclose($file); |
---|
120 | unlink($local_file); |
---|
121 | if (isset($folder)) { |
---|
122 | exec("rm -fr $folder"); |
---|
123 | } |
---|
124 | exit; |
---|
125 | } |
---|
126 | |
---|
127 | private function aasort($array, $key) { |
---|
128 | $sorter = array(); |
---|
129 | $ret = array(); |
---|
130 | reset($array); |
---|
131 | foreach ($array as $ii => $va) { |
---|
132 | $sorter[$ii] = $va[$key]; |
---|
133 | } |
---|
134 | asort($sorter); |
---|
135 | foreach ($sorter as $ii => $va) { |
---|
136 | $ret[$ii] = $array[$ii]; |
---|
137 | } |
---|
138 | $array = $ret; |
---|
139 | return $array; |
---|
140 | } |
---|
141 | |
---|
142 | private function encodePath($path) { |
---|
143 | $fileName = $path; |
---|
144 | $fileNames = explode("/", $fileName); |
---|
145 | $name = $fileNames[(count($fileNames) - 1)]; |
---|
146 | $fileName = str_replace($name, "", $fileName) . str_replace("+", "%20", urlencode($name)); |
---|
147 | return $fileName; |
---|
148 | } |
---|
149 | |
---|
150 | private function convert_vi_to_en($str) { |
---|
151 | $str = preg_replace("/(à |á|ạ|ả|ã|â|ầ|ấ|áº|ẩ|ẫ|Ä|ằ|ắ|ặ|ẳ|ẵ)/", "a", $str); |
---|
152 | $str = preg_replace("/(Ú|é|ẹ|ẻ|ẜ|ê|á»|ế|á»|á»|á»
)/", "e", $str); |
---|
153 | $str = preg_replace("/(ì|Ã|á»|á»|Ä©)/", "i", $str); |
---|
154 | $str = preg_replace("/(ò|ó|á»|á»|õ|ÃŽ|á»|á»|á»|á»|á»|Æ¡|á»|á»|ợ|á»|ỡ)/", "o", $str); |
---|
155 | $str = preg_replace("/(ù|ú|ụ|á»§|Å©|ư|ừ|ứ|á»±|á»|ữ)/", "u", $str); |
---|
156 | $str = preg_replace("/(ỳ|Ü|ỵ|ỷ|ỹ)/", "y", $str); |
---|
157 | $str = preg_replace("/(Ä)/", "d", $str); |
---|
158 | $str = preg_replace("/(Ã|Ã|Ạ|Ả|Ã|Ã|Ẋ|Ẁ|Ậ|ẚ|Ẫ|Ä|Ằ|Ắ|Ặ|Ẳ|Ẏ)/", "A", $str); |
---|
159 | $str = preg_replace("/(Ã|Ã|ẞ|Ẻ|Ẍ|Ã|á»|ẟ|á»|á»|á»)/", "E", $str); |
---|
160 | $str = preg_replace("/(Ã|Ã|á»|á»|Äš)/", "I", $str); |
---|
161 | $str = preg_replace("/(Ã|Ã|á»|á»|Ã|Ã|á»|á»|á»|á»|á»|Æ |á»|á»|Ợ|á»|á» )/", "O", $str); |
---|
162 | $str = preg_replace("/(Ã|Ã|Ề|Ị|Åš|Ư|Ừ|Ớ|á»°|Ử|á»®)/", "U", $str); |
---|
163 | $str = preg_replace("/(Ỳ|Ã|Ỏ|á»¶|Ở)/", "Y", $str); |
---|
164 | $str = preg_replace("/(Ä)/", "D", $str); |
---|
165 | //$str = str_replace(" ", "-", str_replace("&*#39;","",$str)); |
---|
166 | $str= str_replace(" ", "_", $str); |
---|
167 | return $str; |
---|
168 | } |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | /* End of file privatecontent.php */ |
---|
173 | /* Location: ./application/modules/ajax/controllers/privatecontent.php */ |
---|