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

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

dowanload

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