1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
<?php
namespace Wcms;
class Modelmedia extends Model
{
const MEDIA_SORTBY = ['id', 'size', 'type'];
/**
* Get the Media Object
*
* @param string $entry Id of the file
* @param string $dir Directory of media file
*
* @return Media|bool
*/
public function getmedia(string $entry, string $dir)
{
$fileinfo = pathinfo($entry);
if (isset($fileinfo['extension'])) {
$datas = array(
'id' => str_replace('.' . $fileinfo['extension'], '', $fileinfo['filename']),
'path' => $dir,
'extension' => $fileinfo['extension']
);
return new Media($datas);
} else {
return false;
}
}
public function medialistopt(Medialist $mediaopt)
{
$medialist = $this->getlistermedia($mediaopt->dir(), $mediaopt->type());
$this->medialistsort($medialist, $mediaopt->sortby(), $mediaopt->order());
return $medialist;
}
/**
* Display a list of media
*
* @param string $path
* @param array $type
*
* @return array of Media objects
*/
public function getlistermedia($dir, $type = [])
{
if (is_dir($dir)) {
if ($handle = opendir($dir)) {
$list = [];
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$media = $this->getmedia($entry, $dir);
if ($media != false) {
$media->analyse();
if (empty($type) || in_array($media->type(), $type)) {
$list[] = $media;
}
}
}
}
return $list;
}
} else {
return false;
}
}
/**
* Sort an array of media
*
* @param array $medialist
* @param string $sortby
* @param int order Can be 1 or -1
*/
public function medialistsort(array &$medialist, string $sortby = 'id', int $order = 1): bool
{
$sortby = (in_array($sortby, self::MEDIA_SORTBY)) ? $sortby : 'id';
$order = ($order === 1 || $order === -1) ? $order : 1;
return usort($medialist, $this->buildsorter($sortby, $order));
}
public function buildsorter($sortby, $order)
{
return function ($media1, $media2) use ($sortby, $order) {
$result = $this->mediacompare($media1, $media2, $sortby, $order);
return $result;
};
}
public function mediacompare($media1, $media2, $method = 'id', $order = 1)
{
$result = ($media1->$method() <=> $media2->$method());
return $result * $order;
}
public function listfavicon()
{
$faviconlist = $this->globlist(self::FAVICON_DIR, ['ico', 'png', 'jpg', 'jpeg', 'gif']);
return $faviconlist;
}
public function listthumbnail()
{
$faviconlist = $this->globlist(self::THUMBNAIL_DIR, ['ico', 'png', 'jpg', 'jpeg', 'gif']);
return $faviconlist;
}
public function listinterfacecss()
{
$listinterfacecss = $this->globlist(self::CSS_DIR, ['css']);
$listinterfacecss = array_diff($listinterfacecss, ['edit.css', 'home.css', 'tagcolors.css']);
return $listinterfacecss;
}
public function globlist (string $dir = '', array $extensions = []) : array
{
$list = [];
if(empty($extensions)) {
$glob = $dir . '*.';
} else {
foreach ($extensions as $extension ) {
$glob = $dir . '*.' . $extension;
$list = array_merge($list, glob($glob));
}
}
$list = array_map(function ($input){
return basename($input);
}, $list);
return $list;
}
/**
* Generate an reccursive array where each folder is a array and containing a filecount in each folder
*/
public function listdir(string $dir) : array
{
$result = array();
$cdir = scandir($dir);
$result['dirfilecount'] = 0;
foreach ($cdir as $key => $value) {
if (!in_array($value, array(".", ".."))) {
if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
$result[$value] = $this->listdir($dir . DIRECTORY_SEPARATOR . $value);
} else {
$result['dirfilecount']++;
}
}
}
return $result;
}
/**
* Analyse reccursive array of content to generate list of path
*
* @param array $dirlist Array generated by the listdir function
* @param string $parent used to create the strings
* @param array $pathlist used by reference, must be an empty array
*
* @return array list of path as string
*/
public function listpath(array $dirlist, string $parent = '', array &$pathlist = [])
{
foreach ($dirlist as $dir => $content) {
if(is_array($content)) {
$pathlist[] = $parent . $dir . DIRECTORY_SEPARATOR;
$this->listpath($content, $parent . $dir . DIRECTORY_SEPARATOR, $pathlist);
}
}
}
/**
* Upload single file
*
* @param string $index The file id
* @param string $destination File final destination
* @param bool|int $maxsize Max file size in octets
* @param bool|array $extensions List of authorized extensions
* @param bool $jpgrename Change the file exentension to .jpg
*
* @return bool If upload process is a succes or not
*/
function simpleupload(string $index, string $destination, $maxsize = false, $extensions = false, bool $jpgrename = false): bool
{
//Test1: if the file is corectly uploaded
if (!isset($_FILES[$index]) || $_FILES[$index]['error'] > 0) return false;
//Test2: check file size
if ($maxsize !== false && $_FILES[$index]['size'] > $maxsize) return false;
//Test3: check extension
$ext = substr(strrchr($_FILES[$index]['name'], '.'), 1);
if ($extensions !== false && !in_array($ext, $extensions)) return false;
if ($jpgrename !== false) {
$destination .= '.jpg';
} else {
$destination .= '.' . $ext;
}
//Move to dir
return move_uploaded_file($_FILES[$index]['tmp_name'], $destination);
}
/**
* Upload multiple files
*
* @param string $index Id of the file input
* @param string $target direction to save the files
*/
public function multiupload(string $index, string $target)
{
if ($target[strlen($target) - 1] != DIRECTORY_SEPARATOR)
$target .= DIRECTORY_SEPARATOR;
$count = 0;
foreach ($_FILES[$index]['name'] as $filename) {
$fileinfo = pathinfo($filename);
$extension = idclean($fileinfo['extension']);
$id = idclean($fileinfo['filename']);
$tmp = $_FILES['file']['tmp_name'][$count];
$count = $count + 1;
$temp = $target . $id . '.' . $extension;
move_uploaded_file($tmp, $temp);
$temp = '';
$tmp = '';
}
}
public function adddir($dir, $name)
{
$newdir = $dir . DIRECTORY_SEPARATOR . $name;
if (!is_dir($newdir)) {
return mkdir($newdir);
} else {
return false;
}
}
/**
* Completely delete dir and it's content
*
* @param string $dir Directory to destroy
*
* @return bool depending on operation success
*/
public function deletedir(string $dir) : bool
{
if(substr($dir, -1) !== '/') {
$dir .= '/';
}
if(is_dir($dir)) {
return $this->deltree($dir);
} else {
return false;
}
}
/**
* Function do reccursively delete a directory
*/
public function deltree(string $dir)
{
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $this->deltree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
/**
* Delete a file
*/
public function deletefile(string $filedir)
{
if(is_file($filedir)) {
return unlink($filedir);
} else {
return false;
}
}
public function multifiledelete(array $filelist)
{
foreach ($filelist as $filedir ) {
if(is_string($filedir)) {
$this->deletefile($filedir);
}
}
}
public function movefile(string $filedir, string $dir)
{
if(substr($dir, -1) !== '/') {
$dir .= '/';
}
if(is_file($filedir)) {
$newdir = $dir . basename($filedir);
return rename($filedir, $newdir);
} else {
return false;
}
}
public function multimovefile(array $filedirlist, string $dir)
{
$success = [];
foreach ($filedirlist as $filedir ) {
if(is_string($filedir)) {
$success[] = $this->movefile($filedir, $dir);
}
}
if(in_array(false, $success)) {
return false;
} else {
return true;
}
}
}
|