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

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

download

  • Property svn:executable set to *
File size: 7.5 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 $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            // create parent folder
48            $download_file = $user['username'] . "_" . date("d-m-Y");
49            $folder = "downloads/" . $download_file;
50            mkdir($folder, 0777);
51            foreach ($treeArray as $idx=>$tree) {
52               
53                if ($tree['type'] == 'file') {
54                    $path = $folder . "/" . $tree['name'];
55                    $filecontent = file_get_contents($tree['fileurl']);
56                    file_put_contents($path, $filecontent);
57                }
58                if ($tree['type'] == 'directory') {
59                    // create subfolder
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;
74                                }
75                            }
76                        }
77                    }
78                    $childFiles = $tree['childFiles'];
79                    foreach ($childFiles as $index => $file) {
80                        // add file to folder
81                        if ($file['parentID'] == $tree['id']) {
82                            $path = $folder . "/" . $tree['name'] . "/" . $file['name'];
83                           
84                            $filecontent = file_get_contents($file['fileurl']);
85                            file_put_contents($path, $filecontent);
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);
92                                }
93                            }
94                        }
95                    }
96                }
97            }
98            // zip folder
99            $command = "tar -C downloads -cvf " . $folder . ".zip" . " $download_file";
100
101            exec($command);
102            $local_file = $folder . ".zip";
103            $download_file = $download_file . ".zip";
104        }
105        $mime = "application/octet-stream";
106        if (get_mime_by_extension($local_file) != "") {
107            $mime = get_mime_by_extension($local_file);
108        }
109        header('Cache-control: private');
110        header('Content-Type: ' . $mime);
111        header('Content-Length: ' . filesize($local_file));
112        header('Content-Disposition: attachment; filename=' . $download_file);
113        $download_rate = 2048;
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);
123        unlink($local_file);
124        if (isset($folder)) {
125            exec("rm -fr $folder");
126        }
127        exit;
128    }
129
130    private function aasort($array, $key) {
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
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
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.