Utilities::cloneFolder()   B
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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

307
            /** @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...
308
        }
309
        \chmod($path, $mode);
310
    }
311
312
    /**
313
     * @param $pathSource
314
     * @param $pathTarget
315
     * @throws \RuntimeException
316
     */
317
    public static function cloneFolder($pathSource, $pathTarget)
318
    {
319
        if (\is_dir($pathSource)) {
320
            // Create new dir
321
            if (!\mkdir($pathTarget) && !\is_dir($pathTarget)) {
322
                throw new \RuntimeException(\sprintf('Unable to create the %s directory', $pathTarget));
323
            }
324
            // check all files in dir, and process it
325
            $handle = \opendir($pathSource);
326
            if ($handle) {
0 ignored issues
show
introduced by
$handle is of type resource, thus it always evaluated to false.
Loading history...
327
                while ($file = \readdir($handle)) {
328
                    if ('.' !== $file && '..' !== $file) {
329
                        self::cloneFolder("$pathSource/$file", "$pathTarget/$file");
330
                    }
331
                }
332
                \closedir($handle);
333
            }
334
        } else {
335
            \copy($pathSource, $pathTarget);
336
        }
337
    }
338
339
    /**
340
     * Function responsible for checking if a directory exists, we can also write in and create an index.html file
341
     *
342
     * @param string $folder Le chemin complet du répertoire à vérifier
343
     *
344
     * @throws \RuntimeException
345
     */
346
    public static function prepareFolder($folder)
347
    {
348
        if (!@\mkdir($folder) && !\is_dir($folder)) {
349
            throw new \RuntimeException(\sprintf('Unable to create the %s directory', $folder));
350
        }
351
        file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>');
352
    }
353
}
354