1 | <?php |
---|
2 | |
---|
3 | class myFolder { |
---|
4 | public $fileList = array(); |
---|
5 | public $dirList = array(); |
---|
6 | |
---|
7 | public function addFile($fileName, $fullFileName) { |
---|
8 | $this->fileList[$fileName] = $fullFileName; |
---|
9 | } |
---|
10 | |
---|
11 | public function addFolder($dirName) { |
---|
12 | $this->dirList[$dirName] = new myFolder(); |
---|
13 | } |
---|
14 | |
---|
15 | public function &getFolder($dirName, &$folderList) { |
---|
16 | $temp = @$this->dirList[$dirName]; |
---|
17 | if ($temp==null) { |
---|
18 | $this->addFolder($dirName); |
---|
19 | $folderList[] = $dirName; |
---|
20 | } |
---|
21 | |
---|
22 | return $this->dirList[$dirName]; |
---|
23 | } |
---|
24 | }; |
---|
25 | |
---|
26 | class myListFile |
---|
27 | { |
---|
28 | public $listFile; |
---|
29 | public $listFileName = array(); |
---|
30 | public $listFileNameArray = array(); |
---|
31 | public $listFolder = array(); |
---|
32 | public $listFileSizeArray = array(); |
---|
33 | public $listFileSize = array(); |
---|
34 | |
---|
35 | |
---|
36 | public function myListFile() { |
---|
37 | $this->listFile = new myFolder(); |
---|
38 | } |
---|
39 | |
---|
40 | public function addFile($path, $att, $size = 0) |
---|
41 | { |
---|
42 | $path = str_replace("\\", '/', $path); |
---|
43 | $pathArr = split('/', $path); |
---|
44 | $fileName = ''; |
---|
45 | if ($att[0]!='D') $fileName = array_pop($pathArr); |
---|
46 | $pathObj = &$this->listFile; |
---|
47 | for ($i=0; $i<count($pathArr); $i++) |
---|
48 | $pathObj = &$pathObj->getFolder($pathArr[$i], $this->listFolder); |
---|
49 | |
---|
50 | if ($fileName!=null) { |
---|
51 | $pathObj->addFile($fileName, $path); |
---|
52 | $this->listFileName[$fileName] = $path; |
---|
53 | $this->listFileNameArray[] = $path; |
---|
54 | $this->listFileSizeArray[] = $size; |
---|
55 | $this->listFileSize[$path] = $size; |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | public function isPPT(&$fileName) { |
---|
60 | if (count($this->listFileName)==1) { |
---|
61 | foreach ($this->listFileName as $fileName) { |
---|
62 | return myUtility::getFileExt($fileName ) == 'ppt'; |
---|
63 | } |
---|
64 | } |
---|
65 | return false; |
---|
66 | } |
---|
67 | |
---|
68 | public function findFile($extension, &$findSize) |
---|
69 | { |
---|
70 | if (!is_array($extension)) $extension = array($extension); |
---|
71 | $ret = array(); |
---|
72 | $findSize = array(); |
---|
73 | $n = count($this->listFileNameArray); |
---|
74 | for ($i = 0; $i < $n; $i++) { |
---|
75 | $file = $this->listFileNameArray[$i]; |
---|
76 | if (in_array(myUtility::getFileExt($file), $extension)) { |
---|
77 | $ret[] = $file; |
---|
78 | $findSize[] = $this->listFileSizeArray[$i]; |
---|
79 | } |
---|
80 | } |
---|
81 | return $ret; |
---|
82 | } |
---|
83 | |
---|
84 | public function pregFindFile($pattern, &$findSize) { |
---|
85 | $ret = array(); |
---|
86 | $findSize = array(); |
---|
87 | $n = count($this->listFileNameArray); |
---|
88 | for ($i = 0; $i < $n; $i++) { |
---|
89 | $file = $this->listFileNameArray[$i]; |
---|
90 | if (preg_match($pattern, $file)) { |
---|
91 | $ret[] = $file; |
---|
92 | $findSize[] = $this->listFileSizeArray[$i]; |
---|
93 | } |
---|
94 | } |
---|
95 | return $ret; |
---|
96 | } |
---|
97 | }; |
---|
98 | |
---|
99 | class myZip { |
---|
100 | public static function Unzip($zipFile, $destFolder) |
---|
101 | { |
---|
102 | $zip_cmd = sfConfig::get('app_7z_exe'); |
---|
103 | $command = "$zip_cmd x -y -p123456 -o" . escapeshellarg($destFolder) . ' ' . escapeshellarg($zipFile); |
---|
104 | $descriptorspec = array( |
---|
105 | 0 => array("pipe", "r"), // stdin is a pipe that the child will read from |
---|
106 | 1 => array("pipe", "w"), // stdout is a pipe that the child will write to |
---|
107 | //2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to |
---|
108 | ); |
---|
109 | //var_dump($command); exit(); |
---|
110 | $process = proc_open($command, $descriptorspec, $pipes); |
---|
111 | |
---|
112 | if (is_resource($process)) { |
---|
113 | while (!feof($pipes[1])) { |
---|
114 | $line = fgets($pipes[1],1024); |
---|
115 | if (preg_match('@Wrong password@', $line)) { |
---|
116 | fclose($pipes[1]); |
---|
117 | proc_close($process); |
---|
118 | return false; |
---|
119 | } |
---|
120 | } |
---|
121 | fclose($pipes[1]); |
---|
122 | proc_close($process); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * Enter description here... |
---|
128 | * |
---|
129 | * @param string $zipFile |
---|
130 | * @return myListFile |
---|
131 | */ |
---|
132 | public static function ListFile($zipFile) |
---|
133 | { |
---|
134 | //$logFileName = 'zip.log'; |
---|
135 | $listFile = new myListFile(); |
---|
136 | $ext = myUtility::getFileExt($zipFile); |
---|
137 | if ($ext!='zip' && $ext!='7z' && $ext!='rar' && $ext!='gzip' && $ext!='gz') { |
---|
138 | $zipFile = str_replace("\\", '/', $zipFile); |
---|
139 | $zipArray = split('/', $zipFile); |
---|
140 | $zipFile = array_pop($zipArray); |
---|
141 | $listFile->addFile($zipFile, '.', 0); |
---|
142 | return $listFile; |
---|
143 | } |
---|
144 | |
---|
145 | $zip_cmd = sfConfig::get('app_7z_exe'); |
---|
146 | $descriptorspec = array( |
---|
147 | 0 => array("pipe", "r"), // stdin is a pipe that the child will read from |
---|
148 | 1 => array("pipe", "w"), // stdout is a pipe that the child will write to |
---|
149 | ); |
---|
150 | |
---|
151 | /////////////////////////////////////////////////////////////////////// |
---|
152 | // Check password |
---|
153 | $command = "$zip_cmd l -slt -p123456 " . escapeshellarg($zipFile); |
---|
154 | //myUtility::log("\n\n\n\n$command", $logFileName); |
---|
155 | $process = proc_open($command, $descriptorspec, $pipes); |
---|
156 | if (is_resource($process)) { |
---|
157 | while (!feof($pipes[1])) { |
---|
158 | $s = fgets($pipes[1], 1024); |
---|
159 | //myUtility::log($s, $logFileName); |
---|
160 | if (preg_match('@Wrong password|not supported archive|Encrypted = \+@', $s)) { |
---|
161 | fclose($pipes[1]); |
---|
162 | proc_close($process); |
---|
163 | return $listFile; |
---|
164 | } |
---|
165 | } |
---|
166 | fclose($pipes[1]); |
---|
167 | proc_close($process); |
---|
168 | } else return $listFile; |
---|
169 | |
---|
170 | /////////////////////////////////////////////////////////////////////// |
---|
171 | // Lay danh sach ten file |
---|
172 | $command = "$zip_cmd l -p123456 " . escapeshellarg($zipFile); |
---|
173 | //myUtility::log("\n\n\n\n$command", $logFileName); |
---|
174 | $process = proc_open($command, $descriptorspec, $pipes); |
---|
175 | |
---|
176 | if (is_resource($process)) { |
---|
177 | while (!feof($pipes[1])) { |
---|
178 | $s = fgets($pipes[1], 1024); |
---|
179 | //myUtility::log($s, $logFileName); |
---|
180 | if (preg_match('@Wrong password@', $s)) { |
---|
181 | fclose($pipes[1]); |
---|
182 | proc_close($process); |
---|
183 | return $listFile; |
---|
184 | } |
---|
185 | if (preg_match('@[0-9]+\-[0-9]+\-[0-9]+ [0-9]+:[0-9]+:[0-9]+ (.....) +([0-9]+) +([0-9]+)? (.*)$@', $s, $match)) { |
---|
186 | $match[3] = preg_replace('@\r|\n@', '', $match[3]); |
---|
187 | $listFile->addFile($match[4], $match[1], $match[2]); |
---|
188 | } |
---|
189 | } |
---|
190 | fclose($pipes[1]); |
---|
191 | proc_close($process); |
---|
192 | return $listFile; |
---|
193 | } else return $listFile; |
---|
194 | } |
---|
195 | }; |
---|