1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Jaeger |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2016, mithra62 |
6
|
|
|
* @link http://jaeger-app.com |
7
|
|
|
* @version 1.0 |
8
|
|
|
* @filesource ./Files.php |
9
|
|
|
*/ |
10
|
|
|
namespace JaegerApp; |
11
|
|
|
|
12
|
|
|
use JaegerApp\Exceptions\FileException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Jaeger - Files Object |
16
|
|
|
* |
17
|
|
|
* Contains methods for interacting with the file system and files directly |
18
|
|
|
* |
19
|
|
|
* @package Files |
20
|
|
|
* @author Eric Lamb <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Files |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Contains the details for any file collections |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $file_data = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns the file data collection |
34
|
|
|
* |
35
|
|
|
* @return \mithra62\array |
36
|
|
|
*/ |
37
|
|
|
public function getFileData() |
38
|
|
|
{ |
39
|
|
|
return $this->file_data; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Sets the file to store for later |
44
|
|
|
* |
45
|
|
|
* @param mixed $file |
46
|
|
|
* The file to set |
47
|
|
|
* @param bool $reset |
48
|
|
|
* Whether the counter should be reset when set |
49
|
|
|
* @return \mithra62\Files |
50
|
|
|
*/ |
51
|
|
|
public function setFileData($file = false, $reset = false) |
52
|
|
|
{ |
53
|
|
|
if ($reset) { |
54
|
|
|
$this->file_data = array(); |
55
|
|
|
if ($file) { |
56
|
|
|
$this->file_data[] = $file; |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
$this->file_data[] = $file; |
60
|
|
|
} |
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Writes data to $path creating it if it doesn't exist |
66
|
|
|
* |
67
|
|
|
* @param string $path |
68
|
|
|
* The path to the file |
69
|
|
|
* @param string $data |
70
|
|
|
* The data to write |
71
|
|
|
* @param string $mode |
72
|
|
|
* The access we need to stream the file |
73
|
|
|
* @return boolean |
74
|
|
|
*/ |
75
|
|
|
public function write($path, $data, $mode = 'w+') |
76
|
|
|
{ |
77
|
|
|
if (! $fp = fopen($path, $mode)) { |
78
|
|
|
throw new FileException('Can\'t open the file for writing! ' . $path); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
flock($fp, LOCK_EX); |
82
|
|
|
fwrite($fp, $data); |
83
|
|
|
flock($fp, LOCK_UN); |
84
|
|
|
fclose($fp); |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the contents of a file |
91
|
|
|
* |
92
|
|
|
* @param string $file |
93
|
|
|
* The path to the file to read |
94
|
|
|
* @return boolean|string |
95
|
|
|
*/ |
96
|
|
|
public function read($file) |
97
|
|
|
{ |
98
|
|
|
if (! file_exists($file)) { |
99
|
|
|
return FALSE; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return file_get_contents($file); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Removes a file from the file system |
107
|
|
|
* |
108
|
|
|
* @param string $path |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function delete($path) |
112
|
|
|
{ |
113
|
|
|
if (file_exists($path) && is_writable($path)) { |
114
|
|
|
return unlink($path); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Removes all the files in the given $path |
120
|
|
|
* |
121
|
|
|
* Files have to be writable and/or owned by the system user in order to be processed |
122
|
|
|
* |
123
|
|
|
* @param string $path |
124
|
|
|
* @param string $del_dir |
125
|
|
|
* @param number $level |
126
|
|
|
* @param array $exclude |
127
|
|
|
* @return boolean |
128
|
|
|
*/ |
129
|
|
|
public function deleteDir($path, $del_dir = false, $level = 0, $exclude = array()) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
// Trim the trailing slash |
132
|
|
|
$path = rtrim($path, DIRECTORY_SEPARATOR); |
133
|
|
|
|
134
|
|
|
if (! $current_dir = opendir($path)) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$exclude[] = '.'; |
139
|
|
|
$exclude[] = '..'; |
140
|
|
|
|
141
|
|
|
while (false !== ($filename = readdir($current_dir))) { |
142
|
|
|
if (! in_array($filename, $exclude)) { |
143
|
|
|
if (is_dir($path . DIRECTORY_SEPARATOR . $filename)) { |
144
|
|
|
$this->deleteDir($path . DIRECTORY_SEPARATOR . $filename, $del_dir); |
145
|
|
|
} else { |
146
|
|
|
$this->delete($path . DIRECTORY_SEPARATOR . $filename); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
closedir($current_dir); |
151
|
|
|
|
152
|
|
|
if ($del_dir == true) { |
153
|
|
|
return rmdir($path); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Format a number of bytes into a human readable format. |
161
|
|
|
* Optionally choose the output format and/or force a particular unit |
162
|
|
|
* |
163
|
|
|
* @param string $val |
164
|
|
|
* The number to format |
165
|
|
|
* @param number $digits |
166
|
|
|
* How many digits to display |
167
|
|
|
* @param string $mode |
168
|
|
|
* Either SI or EIC to determine either 1000 or 1024 bytes |
169
|
|
|
* @param string $bB |
170
|
|
|
* Whether to use b or B formatting |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function filesizeFormat($val, $digits = 3, $mode = "IEC", $bB = "B") |
174
|
|
|
{ |
175
|
|
|
$si = array( |
176
|
|
|
"", |
177
|
|
|
"k", |
178
|
|
|
"M", |
179
|
|
|
"G", |
180
|
|
|
"T", |
181
|
|
|
"P", |
182
|
|
|
"E", |
183
|
|
|
"Z", |
184
|
|
|
"Y" |
185
|
|
|
); |
186
|
|
|
$iec = array( |
187
|
|
|
"", |
188
|
|
|
"Ki", |
189
|
|
|
"Mi", |
190
|
|
|
"Gi", |
191
|
|
|
"Ti", |
192
|
|
|
"Pi", |
193
|
|
|
"Ei", |
194
|
|
|
"Zi", |
195
|
|
|
"Yi" |
196
|
|
|
); |
197
|
|
|
switch (strtoupper($mode)) { |
198
|
|
|
case "SI": |
199
|
|
|
$factor = 1000; |
200
|
|
|
$symbols = $si; |
201
|
|
|
break; |
202
|
|
|
case "IEC": |
203
|
|
|
$factor = 1024; |
204
|
|
|
$symbols = $iec; |
205
|
|
|
break; |
206
|
|
|
default: |
207
|
|
|
$factor = 1000; |
208
|
|
|
$symbols = $si; |
209
|
|
|
break; |
210
|
|
|
} |
211
|
|
|
switch ($bB) { |
212
|
|
|
case "b": |
213
|
|
|
$val *= 8; |
214
|
|
|
break; |
215
|
|
|
default: |
216
|
|
|
$bB = "B"; |
217
|
|
|
break; |
218
|
|
|
} |
219
|
|
|
for ($i = 0; $i < count($symbols) - 1 && $val >= $factor; $i ++) { |
220
|
|
|
$val /= $factor; |
221
|
|
|
} |
222
|
|
|
$p = strpos($val, "."); |
223
|
|
|
if ($p !== false && $p > $digits) { |
224
|
|
|
$val = round($val); |
225
|
|
|
} elseif ($p !== false) { |
226
|
|
|
$val = round($val, $digits - $p); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return round($val, $digits) . " " . $symbols[$i] . $bB; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Given the full system path to a file it will force the "Save As" dialogue of browsers |
234
|
|
|
* |
235
|
|
|
* @param unknown $filename |
236
|
|
|
* @param string $force_name |
237
|
|
|
*/ |
238
|
|
|
public function fileDownload($filename, $force_name = FALSE) |
239
|
|
|
{ |
240
|
|
|
// required for IE, otherwise Content-disposition is ignored |
241
|
|
|
if (ini_get('zlib.output_compression')) { |
242
|
|
|
ini_set('zlib.output_compression', 'Off'); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$file_extension = strtolower(substr(strrchr($filename, "."), 1)); |
246
|
|
|
|
247
|
|
|
if ($filename == "") { |
248
|
|
|
throw new FileException('Download file NOT SPECIFIED! ' . $filename); |
249
|
|
|
} elseif (! file_exists($filename)) { |
250
|
|
|
throw new FileException('Download file NOT Found! ' . $filename); |
251
|
|
|
} |
252
|
|
|
; |
253
|
|
|
switch ($file_extension) { |
254
|
|
|
case "pdf": |
255
|
|
|
$ctype = "application/pdf"; |
256
|
|
|
break; |
257
|
|
|
case "exe": |
258
|
|
|
$ctype = "application/octet-stream"; |
259
|
|
|
break; |
260
|
|
|
case "zip": |
261
|
|
|
$ctype = "application/zip"; |
262
|
|
|
break; |
263
|
|
|
case "doc": |
264
|
|
|
$ctype = "application/msword"; |
265
|
|
|
break; |
266
|
|
|
case "xls": |
267
|
|
|
$ctype = "application/vnd.ms-excel"; |
268
|
|
|
break; |
269
|
|
|
case "ppt": |
270
|
|
|
$ctype = "application/vnd.ms-powerpoint"; |
271
|
|
|
break; |
272
|
|
|
case "gif": |
273
|
|
|
$ctype = "image/gif"; |
274
|
|
|
break; |
275
|
|
|
case "png": |
276
|
|
|
$ctype = "image/png"; |
277
|
|
|
break; |
278
|
|
|
case "rtf": |
279
|
|
|
$ctype = "text/rtf"; |
280
|
|
|
break; |
281
|
|
|
case "jpeg": |
282
|
|
|
case "jpg": |
283
|
|
|
$ctype = "image/jpg"; |
284
|
|
|
break; |
285
|
|
|
default: |
286
|
|
|
$ctype = "application/zip"; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$filesize = filesize($filename); |
290
|
|
|
header("Pragma: public"); // required |
291
|
|
|
header("Expires: 0"); |
292
|
|
|
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
293
|
|
|
header("Cache-Control: private", false); |
294
|
|
|
header("Content-Type: $ctype"); |
295
|
|
|
header("Content-Disposition: attachment; filename=\"" . ($force_name ? $force_name : basename($filename)) . "\";"); |
296
|
|
|
header("Content-Transfer-Encoding: binary"); |
297
|
|
|
header("Content-Length: " . $filesize); |
298
|
|
|
|
299
|
|
|
if ($fd = fopen($filename, "r")) { |
300
|
|
|
while (! feof($fd)) { |
301
|
|
|
$buffer = fread($fd, 1024 * 8); |
302
|
|
|
echo $buffer; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
fclose($fd); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Returns all the files in a directory |
310
|
|
|
* |
311
|
|
|
* @param string $source_dir |
312
|
|
|
* The path to the directory |
313
|
|
|
* @param boolean $include_path |
314
|
|
|
* Whether to include the full path or jsut file name |
315
|
|
|
* @param boolean $recursion |
316
|
|
|
* Whether to drill down into further directories |
317
|
|
|
* @return array|boolean |
318
|
|
|
*/ |
319
|
|
|
public function getFilenames($source_dir, $include_path = true, $recursion = true) |
320
|
|
|
{ |
321
|
|
|
$fp = opendir($source_dir); |
322
|
|
|
if ($fp) { |
323
|
|
|
// reset the array and make sure $source_dir has a trailing slash on the initial call |
324
|
|
|
if ($recursion === false) { |
325
|
|
|
$this->setFileData(false, true); |
326
|
|
|
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
while (false !== ($file = readdir($fp))) { |
330
|
|
|
if (is_dir($source_dir . $file) && strncmp($file, '.', 1) !== 0) { |
331
|
|
|
$this->getFilenames($source_dir . $file . DIRECTORY_SEPARATOR, $include_path, TRUE); |
332
|
|
|
} elseif (strncmp($file, '.', 1) !== 0) { |
333
|
|
|
$filedata = ($include_path === TRUE) ? $source_dir . DIRECTORY_SEPARATOR . $file : $file; |
334
|
|
|
$this->setFileData($filedata); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
return $this->getFileData(); |
339
|
|
|
} else { |
340
|
|
|
return false; |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Copies a directory to another |
346
|
|
|
* |
347
|
|
|
* @param string $dir |
348
|
|
|
* The path to copy |
349
|
|
|
* @param string $destination |
350
|
|
|
* The path to save all the files to |
351
|
|
|
*/ |
352
|
|
|
public function copyDir($dir, $destination) |
353
|
|
|
{ |
354
|
|
|
foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(trim($dir), \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) { |
355
|
|
|
|
356
|
|
|
if ($item->isDir() && !file_exists($destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName())) { |
357
|
|
|
mkdir($destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName(), '0777', true); |
358
|
|
|
} else { |
359
|
|
|
copy($item, $destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.