Passed
Push — master ( 6ebac0...a0be99 )
by Michael
19:32
created

foldersize()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 13
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 20
rs 9.5222
1
<?php
2
3
if ('RESPONSIVEfilemanager' !== $_SESSION['verify']) {
4
    die('forbiden');
5
}
6
7
function deleteDir($dir)
8
{
9
    if (!file_exists($dir)) {
10
        return true;
11
    }
12
    if (!is_dir($dir)) {
13
        return unlink($dir);
14
    }
15
    foreach (scandir($dir) as $item) {
16
        if ('.' === $item || '..' === $item) {
17
            continue;
18
        }
19
        if (!deleteDir($dir . DIRECTORY_SEPARATOR . $item)) {
20
            return false;
21
        }
22
    }
23
24
    return rmdir($dir);
25
}
26
27
function duplicate_file($old_path, $name)
28
{
29
    if (file_exists($old_path)) {
30
        $info = pathinfo($old_path);
31
        $new_path = $info['dirname'] . '/' . $name . '.' . $info['extension'];
32
        if (file_exists($new_path)) {
33
            return false;
34
        }
35
36
        return copy($old_path, $new_path);
37
    }
38
}
39
40
function rename_file($old_path, $name, $transliteration)
41
{
42
    $name = fix_filename($name, $transliteration);
43
    if (file_exists($old_path)) {
44
        $info = pathinfo($old_path);
45
        $new_path = $info['dirname'] . '/' . $name . '.' . $info['extension'];
46
        if (file_exists($new_path)) {
47
            return false;
48
        }
49
50
        return rename($old_path, $new_path);
51
    }
52
}
53
54
function rename_folder($old_path, $name, $transliteration)
55
{
56
    $name = fix_filename($name, $transliteration);
57
    if (file_exists($old_path)) {
58
        $new_path = fix_dirname($old_path) . '/' . $name;
59
        if (file_exists($new_path)) {
60
            return false;
61
        }
62
63
        return rename($old_path, $new_path);
64
    }
65
}
66
67
function create_img_gd($imgfile, $imgthumb, $newwidth, $newheight = '')
68
{
69
    if (image_check_memory_usage($imgfile, $newwidth, $newheight)) {
70
        require_once('php_image_magician.php');
71
72
        try {
73
            $magicianObj = new imageLib($imgfile);
74
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
75
        }
76
77
        try {
78
            $magicianObj->resizeImage($newwidth, $newheight, 'crop');
79
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
80
        }
81
82
        try {
83
            $magicianObj->saveImage($imgthumb, 80);
84
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
85
        }
86
87
        return true;
88
    }
89
90
    return false;
91
}
92
93
function create_img($imgfile, $imgthumb, $newwidth, $newheight = '')
94
{
95
    if (image_check_memory_usage($imgfile, $newwidth, $newheight)) {
96
        require_once('php_image_magician.php');
97
98
        try {
99
            $magicianObj = new imageLib($imgfile);
100
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
101
        }
102
103
        try {
104
            $magicianObj->resizeImage($newwidth, $newheight, 'auto');
105
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
106
        }
107
108
        try {
109
            $magicianObj->saveImage($imgthumb, 80);
110
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
111
        }
112
113
        return true;
114
    } else {
115
        return false;
116
    }
117
}
118
119
function makeSize($size)
120
{
121
    $units = ['B', 'KB', 'MB', 'GB', 'TB'];
122
    $u = 0;
123
    while ((round($size / 1024) > 0) && ($u < 4)) {
124
        $size = $size / 1024;
125
        ++$u;
126
    }
127
128
    return (number_format($size, 0) . ' ' . $units[$u]);
129
}
130
131
function foldersize($path)
132
{
133
    $total_size = 0;
134
    $files = scandir($path);
135
    $cleanPath = rtrim($path, '/') . '/';
136
137
    foreach ($files as $t) {
138
        if ('.' !== $t && '..' !== $t) {
139
            $currentFile = $cleanPath . $t;
140
            if (is_dir($currentFile)) {
141
                $size = foldersize($currentFile);
142
                $total_size += $size;
143
            } else {
144
                $size = filesize($currentFile);
145
                $total_size += $size;
146
            }
147
        }
148
    }
149
150
    return $total_size;
151
}
152
153
function create_folder($path = false, $path_thumbs = false)
154
{
155
    $oldumask = umask(0);
156
    if ($path && !file_exists($path)) {
157
        mkdir($path, 0777, true);
158
    } // or even 01777 so you get the sticky bit set
159
    if ($path_thumbs && !file_exists($path_thumbs)) {
160
        mkdir($path_thumbs, 0777, true) or die("$path_thumbs cannot be found");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
161
    } // or even 01777 so you get the sticky bit set
162
    umask($oldumask);
163
}
164
165
function check_files_extensions_on_path($path, $ext)
166
{
167
    if (!is_dir($path)) {
168
        $fileinfo = pathinfo($path);
169
        if (!in_array(mb_strtolower($fileinfo['extension']), $ext)) {
170
            unlink($path);
171
        }
172
    } else {
173
        $files = scandir($path);
174
        foreach ($files as $file) {
175
            check_files_extensions_on_path(trim($path, '/') . '/' . $file, $ext);
176
        }
177
    }
178
}
179
180
function check_files_extensions_on_phar($phar, &$files, $basepath, $ext)
181
{
182
    foreach ($phar as $file) {
183
        if ($file->isFile()) {
184
            if (in_array(mb_strtolower($file->getExtension()), $ext)) {
185
                $files[] = $basepath . $file->getFileName();
186
            }
187
        } elseif ($file->isDir()) {
188
            $iterator = new DirectoryIterator($file);
189
            check_files_extensions_on_phar($iterator, $files, $basepath . $file->getFileName() . '/', $ext);
190
        }
191
    }
192
}
193
194
function fix_filename($str, $transliteration)
195
{
196
    if ($transliteration) {
197
        if (function_exists('transliterator_transliterate')) {
198
            $str = transliterator_transliterate('Accents-Any', $str);
199
        } else {
200
            $str = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);
201
        }
202
203
        $str = preg_replace("/[^a-zA-Z0-9\.\[\]_| -]/", '', $str);
204
    }
205
    // Empty or incorrectly transliterated filename.
206
    // Here is a point: a good file UNKNOWN_LANGUAGE.jpg could become .jpg in previous code.
207
    // So we add that default 'file' name to fix that issue.
208
    if (0 === mb_strpos($str, '.')) {
209
        $str = 'file' . $str;
210
    }
211
212
    return trim($str);
213
}
214
215
function fix_dirname($str)
216
{
217
    return str_replace('~', ' ', dirname(str_replace(' ', '~', $str)));
218
}
219
220
function fix_strtoupper($str)
221
{
222
    if (function_exists('mb_strtoupper')) {
223
        return mb_strtoupper($str);
224
    }
225
226
    return mb_strtoupper($str);
227
}
228
229
function fix_strtolower($str)
230
{
231
    if (function_exists('mb_strtoupper')) {
232
        return mb_strtolower($str);
233
    }
234
235
    return mb_strtolower($str);
236
}
237
238
function fix_path($path, $transliteration)
239
{
240
    $info = pathinfo($path);
241
    $tmp_path = $info['dirname'];
242
    $str = fix_filename($info['filename'], $transliteration);
243
    if ('' != $tmp_path) {
244
        return $tmp_path . DIRECTORY_SEPARATOR . $str;
245
    }
246
247
    return $str;
248
}
249
250
function base_url()
251
{
252
    return sprintf(
253
        '%s://%s',
254
        isset($_SERVER['HTTPS']) && 'off' !== $_SERVER['HTTPS'] ? 'https' : 'http',
255
        $_SERVER['HTTP_HOST']
256
    );
257
}
258
259
function config_loading($current_path, $fld)
260
{
261
    if (file_exists($current_path . $fld . '.config')) {
262
        require_once($current_path . $fld . '.config');
263
264
        return true;
265
    }
266
    echo '!!!!' . $parent = fix_dirname($fld);
267
    if ('.' !== $parent && !empty($parent)) {
268
        config_loading($current_path, $parent);
269
    }
270
271
    return false;
272
}
273
274
function image_check_memory_usage($img, $max_breedte, $max_hoogte)
275
{
276
    if (file_exists($img)) {
277
        $K64 = 65536;    // number of bytes in 64K
278
        $memory_usage = memory_get_usage();
279
        $memory_limit = abs((int)(str_replace('M', '', ini_get('memory_limit')) * 1024 * 1024));
280
        $image_properties = getimagesize($img);
281
        $image_width = $image_properties[0];
282
        $image_height = $image_properties[1];
283
        $image_bits = $image_properties['bits'];
284
        $image_memory_usage = $K64 + ($image_width * $image_height * ($image_bits) * 2);
285
        $thumb_memory_usage = $K64 + ($max_breedte * $max_hoogte * ($image_bits) * 2);
286
        $memory_needed = (int)($memory_usage + $image_memory_usage + $thumb_memory_usage);
287
288
        if ($memory_needed > $memory_limit) {
289
            ini_set('memory_limit', ((int)($memory_needed / 1024 / 1024) + 5) . 'M');
290
            if (ini_get('memory_limit') == ((int)($memory_needed / 1024 / 1024) + 5) . 'M') {
291
                return true;
292
            }
293
294
            return false;
295
        }
296
297
        return true;
298
    }
299
300
    return false;
301
}
302
303
function endsWith($haystack, $needle)
304
{
305
    return '' === $needle || mb_substr($haystack, -mb_strlen($needle)) === $needle;
306
}
307
308
function new_thumbnails_creation(
309
    $targetPath,
310
    $targetFile,
311
    $name,
312
    $current_path,
313
    $relative_image_creation,
314
    $relative_path_from_current_pos,
315
    $relative_image_creation_name_to_prepend,
316
    $relative_image_creation_name_to_append,
317
    $relative_image_creation_width,
318
    $relative_image_creation_height,
319
    $fixed_image_creation,
320
    $fixed_path_from_filemanager,
321
    $fixed_image_creation_name_to_prepend,
322
    $fixed_image_creation_to_append,
323
    $fixed_image_creation_width,
324
    $fixed_image_creation_height
325
) {
326
    //create relative thumbs
327
    $all_ok = true;
328
    if ($relative_image_creation) {
329
        foreach ($relative_path_from_current_pos as $k => $path) {
330
            if ('' != $path && '/' !== $path[mb_strlen($path) - 1]) {
331
                $path .= '/';
332
            }
333
            if (!file_exists($targetPath . $path)) {
334
                create_folder($targetPath . $path, false);
335
            }
336
            $info = pathinfo($name);
337
            if (!endsWith($targetPath, $path)) {
338
                if (!create_img($targetFile, $targetPath . $path . $relative_image_creation_name_to_prepend[$k] . $info['filename'] . $relative_image_creation_name_to_append[$k] . '.' . $info['extension'], $relative_image_creation_width[$k], $relative_image_creation_height[$k])) {
339
                    $all_ok = false;
340
                }
341
            }
342
        }
343
    }
344
345
    //create fixed thumbs
346
    if ($fixed_image_creation) {
347
        foreach ($fixed_path_from_filemanager as $k => $path) {
348
            if ('' != $path && '/' !== $path[mb_strlen($path) - 1]) {
349
                $path .= '/';
350
            }
351
            $base_dir = $path . substr_replace($targetPath, '', 0, mb_strlen($current_path));
0 ignored issues
show
Bug introduced by
Are you sure substr_replace($targetPa..._strlen($current_path)) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

351
            $base_dir = $path . /** @scrutinizer ignore-type */ substr_replace($targetPath, '', 0, mb_strlen($current_path));
Loading history...
352
            if (!file_exists($base_dir)) {
353
                create_folder($base_dir, false);
354
            }
355
            $info = pathinfo($name);
356
            if (!create_img($targetFile, $base_dir . $fixed_image_creation_name_to_prepend[$k] . $info['filename'] . $fixed_image_creation_to_append[$k] . '.' . $info['extension'], $fixed_image_creation_width[$k], $fixed_image_creation_height[$k])) {
357
                $all_ok = false;
358
            }
359
        }
360
    }
361
362
    return $all_ok;
363
}
364