1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Koch Framework |
5
|
|
|
* Jens-André Koch © 2005 - onwards. |
6
|
|
|
* |
7
|
|
|
* This file is part of "Koch Framework". |
8
|
|
|
* |
9
|
|
|
* License: GNU/GPL v2 or any later version, see LICENSE file. |
10
|
|
|
* |
11
|
|
|
* This program is free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Koch\Files; |
26
|
|
|
|
27
|
|
|
class Directory |
28
|
|
|
{ |
29
|
|
|
private $filtername = 'ImagesOnly'; |
30
|
|
|
private $directory = ''; |
31
|
|
|
|
32
|
|
|
public function __construct($directory = null) |
33
|
|
|
{ |
34
|
|
|
if ($directory !== null) { |
35
|
|
|
$this->setDirectory($directory); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Available Filter types: image. |
43
|
|
|
*/ |
44
|
|
|
public function setFilter($filtername) |
45
|
|
|
{ |
46
|
|
|
$this->filtername = $filtername; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setDirectory($directory) |
52
|
|
|
{ |
53
|
|
|
// slash fix |
54
|
|
|
$directory = str_replace('/', DIRECTORY_SEPARATOR, $directory); |
55
|
|
|
$directory = str_replace('\\', DIRECTORY_SEPARATOR, $directory); |
56
|
|
|
|
57
|
|
|
// prefix directory with ROOT for security purposes |
58
|
|
|
if (stristr($directory, ROOT) === false) { |
59
|
|
|
$directory = APPLICATION_PATH . $directory; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->directory = $directory; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getDirectory($directory = '') |
68
|
|
|
{ |
69
|
|
|
if ($directory !== '') { |
70
|
|
|
$this->directory = $directory; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (empty($this->directory) === false) { |
74
|
|
|
return $this->directory; |
75
|
|
|
} else { // default path |
76
|
|
|
|
77
|
|
|
return APPLICATION_PATH . 'uploads/images/gallery'; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getFiles($return_as_array = false) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
// compose the full name of the filter class |
84
|
|
|
$classname = 'Koch_' . $this->filtername . 'FilterIterator'; |
85
|
|
|
|
86
|
|
|
// wrap the iterator in a filter class, when looking for a specific file type, like imagesOnly |
87
|
|
|
$iterator = new $classname(new \DirectoryIterator($this->getDirectory())); |
88
|
|
|
|
89
|
|
|
// return objects |
90
|
|
|
if ($return_as_array === false) { |
|
|
|
|
91
|
|
|
// create new array to take the SPL FileInfo Objects |
92
|
|
|
$data = new \ArrayObject(); |
93
|
|
|
|
94
|
|
|
// while iterating |
95
|
|
|
foreach ($iterator as $file) { |
96
|
|
|
/* |
97
|
|
|
* push the SPL FileInfo Objects into the array |
98
|
|
|
* @see http://www.php.net/~helly/php/ext/spl/classSplFileInfo.html |
99
|
|
|
*/ |
100
|
|
|
$data[$file->getFilename()] = $file->getFileInfo(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$data->ksort(); |
104
|
|
|
} else { // return array |
105
|
|
|
// create array |
106
|
|
|
$data = []; |
107
|
|
|
|
108
|
|
|
// while iterating |
109
|
|
|
foreach ($iterator as $file) { |
110
|
|
|
$wwwpath = WWW_ROOT . '/' . $this->getDirectory() . '/' . $file->getFilename(); |
111
|
|
|
$wwwpath = str_replace('//', '/', $wwwpath); |
112
|
|
|
$data[$wwwpath] = $file->getFilename(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// return the array with SPL FileInfo Objects |
117
|
|
|
return $data; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the filePath with filename. |
122
|
|
|
* |
123
|
|
|
* @return array pathinfo |
124
|
|
|
*/ |
125
|
|
|
public function filePath($filePath) |
126
|
|
|
{ |
127
|
|
|
$fileParts = pathinfo($filePath); |
128
|
|
|
|
129
|
|
|
if (!isset($fileParts['filename'])) { |
130
|
|
|
$fileParts['filename'] = mb_substr($fileParts['basename'], 0, mb_strrpos($fileParts['basename'], '.')); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $fileParts; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Calculates the size of a directory (recursiv). |
138
|
|
|
* |
139
|
|
|
* @param $dir Directory Path |
140
|
|
|
* |
141
|
|
|
* @return $size size of directory in bytes |
|
|
|
|
142
|
|
|
*/ |
143
|
|
|
public static function size($dir) |
144
|
|
|
{ |
145
|
|
|
if (is_dir($dir) === false) { |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$size = 0; |
150
|
|
|
$dh = opendir($dir); |
151
|
|
|
while (($entry = readdir($dh)) !== false) { |
152
|
|
|
// exclude ./.. |
153
|
|
|
if ($entry === '.' or $entry === '..') { |
|
|
|
|
154
|
|
|
continue; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$direntry = $dir . '/' . $entry; |
158
|
|
|
|
159
|
|
|
if (is_dir($direntry) === false) { |
160
|
|
|
// recursion |
161
|
|
|
$size += self::size($direntry); |
162
|
|
|
} else { |
163
|
|
|
$size += filesize($direntry); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
unset($direntry); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
closedir($dh); |
170
|
|
|
|
171
|
|
|
return $size; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Copy a directory recursively. |
176
|
|
|
* |
177
|
|
|
* @param $source |
178
|
|
|
* @param $destination |
179
|
|
|
* @param $overwrite boolean |
180
|
|
|
*/ |
181
|
|
|
public function dirCopy($source, $destination, $overwrite = true) |
182
|
|
|
{ |
183
|
|
|
$folder_path = ''; |
|
|
|
|
184
|
|
|
|
185
|
|
|
$handle = opendir($source); |
186
|
|
|
|
187
|
|
|
if ($handle) { |
188
|
|
|
while (false !== ($file = readdir($handle))) { |
189
|
|
|
if (mb_substr($file, 0, 1) !== '.') { |
190
|
|
|
$source_path = $source . $file; |
|
|
|
|
191
|
|
|
$target_path = $destination . $file; |
|
|
|
|
192
|
|
|
|
193
|
|
|
if (is_file($target_path) === false or $overwrite) { |
|
|
|
|
194
|
|
|
if ([mb_strstr($target_path, '.') === true]) { |
|
|
|
|
195
|
|
|
$folder_path = dirname($target_path); |
|
|
|
|
196
|
|
|
} else { |
197
|
|
|
$folder_path = $target_path; |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
while (is_dir(dirname(end($folder_path))) |
|
|
|
|
201
|
|
|
and dirname(end($folder_path)) !== '/' |
|
|
|
|
202
|
|
|
and dirname(end($folder_path)) !== '.' |
|
|
|
|
203
|
|
|
and dirname(end($folder_path)) !== '' |
|
|
|
|
204
|
|
|
and !preg_match('#^[A-Za-z]+\:\\\$#', dirname(end($folder_path)))) { |
|
|
|
|
205
|
|
|
array_push($folder_path, dirname(end($folder_path))); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
while ($parent_folder_path = array_pop($folder_path)) { |
|
|
|
|
209
|
|
|
if (false === is_dir($parent_folder_path) and |
|
|
|
|
210
|
|
|
false === @mkdir($parent_folder_path, fileperms($parent_folder_path))) { |
|
|
|
|
211
|
|
|
throw new \Exception( |
212
|
|
|
_('Could not create the directory that should be copied (destination).' . |
213
|
|
|
'Probably a permission problem.') |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$old = ini_set('error_reporting', 0); |
219
|
|
|
if (copy($source_path, $target_path) === false) { |
|
|
|
|
220
|
|
|
throw new \Exception('Could not copy the directory. Probably a permission problem.'); |
221
|
|
|
} |
222
|
|
|
ini_set('error_reporting', $old); |
223
|
|
|
} elseif (is_dir($source_path)) { |
|
|
|
|
224
|
|
|
if (is_dir($target_path) === false) { |
|
|
|
|
225
|
|
|
if (@mkdir($target_path, fileperms($source_path)) === false) { |
|
|
|
|
226
|
|
|
// nope, not an empty if statement :) |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
$this->dir_copy($source_path, $target_path, $overwrite); |
|
|
|
|
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
closedir($handle); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Recursively delete directory using PHP iterators. |
239
|
|
|
* |
240
|
|
|
* Uses a CHILD_FIRST RecursiveIteratorIterator to sort files |
241
|
|
|
* before directories, creating a single non-recursive loop |
242
|
|
|
* to delete files/directories in the correct order. |
243
|
|
|
* |
244
|
|
|
* @param string $directory |
245
|
|
|
* @param bool $delete_dir_itself |
246
|
|
|
* |
247
|
|
|
* @return bool|null |
248
|
|
|
*/ |
249
|
|
|
public function deleteDir($directory, $delete_dir_itself = false) |
|
|
|
|
250
|
|
|
{ |
251
|
|
|
$it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS); |
252
|
|
|
$ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); |
253
|
|
|
|
254
|
|
|
foreach ($ri as $file) { |
255
|
|
|
if ($file->isDir()) { |
256
|
|
|
rmdir($file->getPathname()); |
257
|
|
|
} else { |
258
|
|
|
unlink($file->getPathname()); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
if ($delete_dir_itself === true) { |
|
|
|
|
263
|
|
|
return rmdir($directory); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Performs a chmod operation. |
269
|
|
|
* |
270
|
|
|
* @param $path |
271
|
|
|
* @param $chmod |
272
|
|
|
* @param $recursive |
273
|
|
|
*/ |
274
|
|
|
public function chmod($path = '', $chmod = '755', $recursive = false) |
|
|
|
|
275
|
|
|
{ |
276
|
|
|
if (is_dir($path) === false) { |
277
|
|
|
$file_mode = '0' . $chmod; |
|
|
|
|
278
|
|
|
$file_mode = octdec($file_mode); |
|
|
|
|
279
|
|
|
|
280
|
|
|
if (chmod($path, $file_mode) === false) { |
|
|
|
|
281
|
|
|
return false; |
282
|
|
|
} |
283
|
|
|
} else { |
284
|
|
|
$dir_mode_r = '0' . $chmod; |
|
|
|
|
285
|
|
|
$dir_mode_r = octdec($dir_mode_r); |
|
|
|
|
286
|
|
|
|
287
|
|
|
if (chmod($path, $dir_mode_r) === false) { |
|
|
|
|
288
|
|
|
return false; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
if ($recursive === false) { |
292
|
|
|
$dh = opendir($path); |
293
|
|
|
while ($file = readdir($dh)) { |
294
|
|
|
if (mb_substr($file, 0, 1) !== '.') { |
295
|
|
|
$fullpath = $path . '/' . $file; |
296
|
|
|
if (!is_dir($fullpath)) { |
297
|
|
|
$mode = '0' . $chmod; |
298
|
|
|
$mode = octdec($mode); |
299
|
|
|
if (chmod($fullpath, $mode) === false) { |
300
|
|
|
return false; |
301
|
|
|
} |
302
|
|
|
} else { |
303
|
|
|
if ($this->chmod($fullpath, $chmod, true) === false) { |
304
|
|
|
return false; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
closedir($dh); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return true; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|