Passed
Pull Request — master (#88)
by Michael
02:56
created

Utilities::createFolder()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 6
nc 3
nop 4
dl 0
loc 13
rs 9.6111
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Tdmdownloads;
4
5
/**
6
 * Created by PhpStorm.
7
 * User: mamba
8
 * Date: 2015-09-16
9
 * Time: 01:20
10
 */
11
12
use XoopsModules\Tdmdownloads;
13
14
/**
15
 * Class Utilities
16
 * @package XoopsModules\Tdmdownloads
17
 */
18
class Utilities
19
{
20
    protected $db;
21
    protected $helper;
22
    /**
23
     * @param mixed $permtype
24
     * @param mixed $dirname
25
     */
26
27
    //    public static function __construct(\XoopsDatabase $db = null, $helper = null)
28
29
    //    {
30
31
    //        $this->db     = $db;
32
33
    //        $this->helper = $helper;
34
35
    //    }
36
37
    /**
38
     * @param $permtype
39
     * @param $dirname
40
     * @return mixed
41
     */
42
    public static function getItemIds($permtype, $dirname)
43
    {
44
        global $xoopsUser;
45
46
        $permissions = [];
47
48
        if (\is_array($permissions) && \array_key_exists($permtype, $permissions)) {
49
            return $permissions[$permtype];
50
        }
51
52
        /** @var \XoopsModuleHandler $moduleHandler */
53
        $moduleHandler = \xoops_getHandler('module');
54
55
        $tdmModule = $moduleHandler->getByDirname($dirname);
56
57
        $groups = \is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
58
59
        /** @var \XoopsGroupPermHandler $grouppermHandler */
60
        $grouppermHandler = \xoops_getHandler('groupperm');
61
62
        return $grouppermHandler->getItemIds($permtype, $groups, $tdmModule->getVar('mid'));
63
    }
64
65
    /**
66
     * returns the number of updates downloads in the categories of children category
67
     *
68
     * @param $mytree
69
     * @param $categories
70
     * @param $entries
71
     * @param $cid
72
     *
73
     * @return int
74
     */
75
    public static function getNumbersOfEntries($mytree, $categories, $entries, $cid)
76
    {
77
        $count = 0;
78
79
        $child_arr = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $child_arr is dead and can be removed.
Loading history...
80
81
        if (\in_array($cid, $categories)) {
82
            $child = $mytree->getAllChild($cid);
83
84
            foreach (\array_keys($entries) as $i) {
85
                if ($entries[$i]->getVar('cid') === $cid) {
86
                    ++$count;
87
                }
88
89
                foreach (\array_keys($child) as $j) {
90
                    if ($entries[$i]->getVar('cid') === $j) {
91
                        ++$count;
92
                    }
93
                }
94
            }
95
        }
96
97
        return $count;
98
    }
99
100
    /**
101
     * returns an image "new" or "updated"
102
     * @param $time
103
     * @param $status
104
     * @return string
105
     */
106
    public static function getStatusImage($time, $status)
107
    {
108
        $moduleDirName = \basename(\dirname(__DIR__));
109
110
        /** @var Tdmdownloads\Helper $helper */
111
112
        $helper = Tdmdownloads\Helper::getInstance();
113
114
        $count = 7;
115
116
        $new = '';
117
118
        $startdate = \time() - (86400 * $count);
119
120
        if (1 == $helper->getConfig('showupdated')) {
121
            if ($startdate < $time) {
122
                $language = $GLOBALS['xoopsConfig']['language'];
123
124
                if (!\is_dir(XOOPS_ROOT_PATH . "/modules/$moduleDirName/language/" . $language . '/')) {
125
                    $language = 'english';
126
                }
127
128
                $img_path = XOOPS_ROOT_PATH . "/modules/$moduleDirName/language/" . $language . '/';
129
130
                $img_url = XOOPS_URL . "/modules/$moduleDirName/language/" . $language . '/';
131
132
                if (1 == $status) {
133
                    if (\is_readable($img_path . 'new.png')) {
134
                        $new = '&nbsp;<img src="' . $img_url . 'new.png" alt="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '">';
135
                    } else {
136
                        $new = '&nbsp;<img src="' . XOOPS_URL . '/modules/' . $moduleDirName . '/language/english/new.png" alt="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_NEWTHISWEEK . '">';
137
                    }
138
                } elseif (2 == $status) {
139
                    if (\is_readable($img_path . 'updated.png')) {
140
                        $new = '&nbsp;<img src="' . $img_url . 'updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">';
141
                    } else {
142
                        $new = '&nbsp;<img src="' . XOOPS_URL . '/modules/' . $moduleDirName . '/language/english/updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">';
143
                    }
144
                }
145
            }
146
        }
147
148
        return $new;
149
    }
150
151
    /**
152
     * retourne une image "populaire"
153
     * @param $hits
154
     * @return string
155
     */
156
    public static function getPopularImage($hits)
157
    {
158
        /** @var Tdmdownloads\Helper $helper */
159
160
        $helper = Tdmdownloads\Helper::getInstance();
161
162
        $moduleDirName = \basename(\dirname(__DIR__));
163
164
        $pop = '';
165
166
        if ($hits >= $helper->getConfig('popular')) {
167
            $language = $GLOBALS['xoopsConfig']['language'];
168
169
            if (!\is_dir(XOOPS_ROOT_PATH . "/modules/$moduleDirName/language/" . $language . '/')) {
170
                $language = 'english';
171
            }
172
173
            $img_path = XOOPS_ROOT_PATH . "/modules/$moduleDirName/language/" . $language . '/';
174
175
            $img_url = XOOPS_URL . "/modules/$moduleDirName/language/" . $language . '/';
176
177
            if (\is_readable($img_path . 'popular.png')) {
178
                $pop = '&nbsp;<img src="' . $img_url . 'popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">';
179
            } else {
180
                $pop = '&nbsp;<img src ="' . XOOPS_URL . '/modules/' . $moduleDirName . '/language/english/popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">';
181
            }
182
        }
183
184
        return $pop;
185
    }
186
187
    /**
188
     * @param mixed $global
189
     * @param mixed $key
190
     * @param mixed $default
191
     * @param mixed $type
192
     *
193
     * @return string
194
     */
195
196
    //    public static function convertFileSize($size)
197
198
    //    {
199
200
    //        if ($size > 0) {
201
202
    //            $mb = 1024 * 1024;
203
204
    //            if ($size > $mb) {
205
206
    //                $mysize = sprintf("%01.2f", $size / $mb) . " MB";
207
208
    //            } elseif ($size >= 1024) {
209
210
    //                $mysize = sprintf("%01.2f", $size / 1024) . " KB";
211
212
    //            } else {
213
214
    //                $mysize = sprintf(_AM_TDMDOWNLOADS_NUMBYTES, $size);
215
216
    //            }
217
218
    //            return $mysize;
219
220
    //        } else {
221
222
    //            return '';
223
224
    //        }
225
226
    //    }
227
228
    /**
229
     * @param        $global
230
     * @param        $key
231
     * @param string $default
232
     * @param string $type
233
     *
234
     * @return mixed|string
235
     */
236
    public static function cleanVars($global, $key, $default = '', $type = 'int')
237
    {
238
        switch ($type) {
239
            case 'string':
240
                if (\defined('FILTER_SANITIZE_ADD_SLASHES')) {
241
                    $ret = isset($global[$key]) ? \filter_var($global[$key]) : $default;
242
                } else {
243
                    $ret = isset($global[$key]) ? \filter_var($global[$key]) : $default;
244
                }
245
                break;
246
            case 'int':
247
            default:
248
                $ret = isset($global[$key]) ? \filter_var($global[$key], \FILTER_SANITIZE_NUMBER_INT) : $default;
249
                break;
250
        }
251
252
        if (false === $ret) {
253
            return $default;
254
        }
255
256
        return $ret;
257
    }
258
259
    /**
260
     * @param        $mytree
261
     * @param        $key
262
     * @param        $category_array
263
     * @param        $title
264
     * @param string $prefix
265
     *
266
     * @return string
267
     */
268
    public static function getPathTree($mytree, $key, $category_array, $title, $prefix = '')
269
    {
270
        $category_parent = $mytree->getAllParent($key);
271
272
        $category_parent = \array_reverse($category_parent);
273
274
        $Path = '';
275
276
        foreach (\array_keys($category_parent) as $j) {
277
            $Path .= $category_parent[$j]->getVar($title) . $prefix;
278
        }
279
280
        $first_category = '';
281
282
        if (\array_key_exists($key, $category_array)) {
283
            $first_category = $category_array[$key]->getVar($title);
284
        }
285
286
        $Path .= $first_category;
287
288
        return $Path;
289
    }
290
291
    /**
292
     * @param        $mytree
293
     * @param        $key
294
     * @param        $category_array
295
     * @param        $title
296
     * @param string $prefix
297
     * @param bool   $link
298
     * @param string $order
299
     * @param bool   $lasturl
300
     *
301
     * @return string
302
     */
303
    public static function getPathTreeUrl(
304
        $mytree,
305
        $key,
306
        $category_array,
307
        $title,
308
        $prefix = '',
309
        $link = false,
310
        $order = 'ASC',
311
        $lasturl = false
312
    ) {
313
        global $xoopsModule;
314
315
        $category_parent = $mytree->getAllParent($key);
316
317
        if ('ASC' === $order) {
318
            $category_parent = \array_reverse($category_parent);
319
320
            if ($link) {
321
                $Path = '<a href="index.php">' . $xoopsModule->name() . '</a>' . $prefix;
322
            } else {
323
                $Path = $xoopsModule->name() . $prefix;
324
            }
325
        } else {
326
            $first_category = '';
327
328
            if (\array_key_exists($key, $category_array)) {
329
                $first_category = $category_array[$key]->getVar($title);
330
            }
331
332
            $Path = $first_category . $prefix;
333
        }
334
335
        foreach (\array_keys($category_parent) as $j) {
336
            if ($link) {
337
                $Path .= '<a href="viewcat.php?cid=' . $category_parent[$j]->getVar('cat_cid') . '">' . $category_parent[$j]->getVar($title) . '</a>' . $prefix;
338
            } else {
339
                $Path .= $category_parent[$j]->getVar($title) . $prefix;
340
            }
341
        }
342
343
        if ('ASC' === $order) {
344
            if (\array_key_exists($key, $category_array)) {
345
                if ($lasturl) {
346
                    $first_category = '<a href="viewcat.php?cid=' . $category_array[$key]->getVar('cat_cid') . '">' . $category_array[$key]->getVar($title) . '</a>';
347
                } else {
348
                    $first_category = $category_array[$key]->getVar($title);
349
                }
350
            } else {
351
                $first_category = '';
352
            }
353
354
            $Path .= $first_category;
355
        } else {
356
            if ($link) {
357
                $Path .= '<a href="index.php">' . $xoopsModule->name() . '</a>';
358
            } else {
359
                $Path .= $xoopsModule->name();
360
            }
361
        }
362
363
        return $Path;
364
    }
365
366
    /**
367
     * @param      $path
368
     * @param int  $mode
369
     * @param      $fileSource
370
     * @param null $fileTarget
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fileTarget is correct as it would always require null to be passed?
Loading history...
371
     * @throws \RuntimeException
372
     */
373
    public static function createFolder($path, $mode, $fileSource, $fileTarget = null)
374
    {
375
        if (!@\mkdir($path, $mode) && !\is_dir($path)) {
376
            throw new \RuntimeException(\sprintf('Unable to create the %s directory', $path));
377
        }
378
379
        file_put_contents($path . '/index.html', '<script>history.go(-1);</script>');
380
381
        if (!empty($fileSource) && !empty($fileTarget)) {
382
            @\copy($fileSource, $fileTarget);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for copy(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

382
            /** @scrutinizer ignore-unhandled */ @\copy($fileSource, $fileTarget);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
383
        }
384
385
        \chmod($path, $mode);
386
    }
387
388
    /**
389
     * @param $pathSource
390
     * @param $pathTarget
391
     * @throws \RuntimeException
392
     */
393
    public static function cloneFolder($pathSource, $pathTarget)
394
    {
395
        if (\is_dir($pathSource)) {
396
            // Create new dir
397
398
            if (!\mkdir($pathTarget) && !\is_dir($pathTarget)) {
399
                throw new \RuntimeException(\sprintf('Unable to create the %s directory', $pathTarget));
400
            }
401
402
            // check all files in dir, and process it
403
404
            $handle = \opendir($pathSource);
405
406
            if ($handle) {
0 ignored issues
show
introduced by
$handle is of type resource, thus it always evaluated to false.
Loading history...
407
                while ($file = \readdir($handle)) {
408
                    if ('.' !== $file && '..' !== $file) {
409
                        self::cloneFolder("$pathSource/$file", "$pathTarget/$file");
410
                    }
411
                }
412
413
                \closedir($handle);
414
            }
415
        } else {
416
            \copy($pathSource, $pathTarget);
417
        }
418
    }
419
420
    /**
421
     * Function responsible for checking if a directory exists, we can also write in and create an index.html file
422
     *
423
     * @param string $folder Le chemin complet du répertoire à vérifier
424
     *
425
     * @throws \RuntimeException
426
     */
427
    public static function prepareFolder($folder)
428
    {
429
        if (!@\mkdir($folder) && !\is_dir($folder)) {
430
            throw new \RuntimeException(\sprintf('Unable to create the %s directory', $folder));
431
        }
432
433
        file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>');
434
    }
435
}
436