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

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

dowanload

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