Passed
Push — master ( 81a617...f7060c )
by Goffy
02:56 queued 52s
created

Utility::FileSizeConvert()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 5
nop 1
dl 0
loc 18
rs 9.4555
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Tdmdownloads;
4
5
/*
6
 Utility Class Definition
7
8
 You may not change or alter any portion of this comment or credits of
9
 supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit
11
 authors.
12
13
 This program is distributed in the hope that it will be useful, but
14
 WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
18
/**
19
 * Class Utility
20
 */
21
class Utility
22
{
23
    use Common\VersionChecks; //checkVerXoops, checkVerPhp Traits
0 ignored issues
show
introduced by
The trait XoopsModules\Tdmdownloads\Common\VersionChecks requires some properties which are not provided by XoopsModules\Tdmdownloads\Utility: $tag_name, $prerelease
Loading history...
24
25
    use Common\ServerStats; // getServerStats Trait
26
27
    use Common\FilesManagement; // Files Management Trait
28
29
    //--------------- Custom module methods -----------------------------
30
31
    /**
32
     * @param $permtype
33
     * @param $dirname
34
     * @return mixed
35
     */
36
    public function getItemIds($permtype, $dirname)
37
    {
38
        global $xoopsUser;
39
        static $permissions = [];
40
        if (is_array($permissions) && array_key_exists($permtype, $permissions)) {
41
            return $permissions[$permtype];
42
        }
43
        /** @var \XoopsModuleHandler $moduleHandler */
44
        $moduleHandler = xoops_getHandler('module');
45
        $tdmModule     = $moduleHandler->getByDirname($dirname);
46
        $groups        = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
47
48
        /** @var \XoopsGroupPermHandler $grouppermHandler */
49
        $grouppermHandler = xoops_getHandler('groupperm');
50
        $categories       = $grouppermHandler->getItemIds($permtype, $groups, $tdmModule->getVar('mid'));
51
52
        return $categories;
53
    }
54
55
    /**
56
     * retourne le nombre de téléchargements dans le catégories enfants d'une catégorie
57
     * @param \XoopsModules\Tdmdownloads\Tree        $mytree
58
     * @param                                        $categories
59
     * @param                                        $entries
60
     * @param                                        $cid
61
     * @return int
62
     */
63
    public function getNumbersOfEntries($mytree, $categories, $entries, $cid)
64
    {
65
        $count     = 0;
66
        $child_arr = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $child_arr is dead and can be removed.
Loading history...
67
        if (in_array($cid, $categories, true)) {
68
            $child = $mytree->getAllChild($cid);
69
            foreach (array_keys($entries) as $i) {
70
                /** @var \XoopsModules\Tdmdownloads\Downloads[] $entries */
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
82
        return $count;
83
    }
84
85
    /**
86
     * retourne une image "nouveau" ou "mise à jour"
87
     * @param $time
88
     * @param $status
89
     * @return string
90
     */
91
    public function getStatusImage($time, $status)
92
    {
93
        global $xoopsModuleConfig;
94
        $count     = 7;
95
        $new       = '';
96
        $startdate = (time() - (86400 * $count));
97
        if (1 == $xoopsModuleConfig['showupdated']) {
98
            if ($startdate < $time) {
99
                $language = $GLOBALS['xoopsConfig']['language'];
100
                if (!is_dir(XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/')) {
101
                    $language = 'english';
102
                }
103
                $img_path = XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/';
104
                $img_url  = XOOPS_URL . '/modules/tdmdownloads/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/tdmdownloads/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/tdmdownloads/language/english/updated.png" alt="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '" title="' . _MD_TDMDOWNLOADS_INDEX_UPTHISWEEK . '">';
116
                    }
117
                }
118
            }
119
        }
120
121
        return $new;
122
    }
123
124
    /**
125
     * retourne une image "populaire"
126
     * @param $hits
127
     * @return string
128
     */
129
    public function getPopularImage($hits)
130
    {
131
        global $xoopsModuleConfig;
132
        $pop = '';
133
        if ($hits >= $xoopsModuleConfig['popular']) {
134
            $language = $GLOBALS['xoopsConfig']['language'];
135
            if (!is_dir(XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/')) {
136
                $language = 'english';
137
            }
138
            $img_path = XOOPS_ROOT_PATH . '/modules/tdmdownloads/language/' . $language . '/';
139
            $img_url  = XOOPS_URL . '/modules/tdmdownloads/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/tdmdownloads/language/english/popular.png" alt="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '" title="' . _MD_TDMDOWNLOADS_INDEX_POPULAR . '">';
144
            }
145
        }
146
147
        return $pop;
148
    }
149
150
    /**
151
     * @param        $mytree
152
     * @param        $key
153
     * @param        $category_array
154
     * @param        $title
155
     * @param string $prefix
156
     * @return string
157
     */
158
    public function getPathTree($mytree, $key, $category_array, $title, $prefix = '')
159
    {
160
        $categoryParent = $mytree->getAllParent($key);
161
        $categoryParent = array_reverse($categoryParent);
162
        $path            = '';
163
        foreach (array_keys($categoryParent) as $j) {
164
            /** @var \XoopsModules\Tdmdownloads\Category[] $categoryParent */
165
            $path .= $categoryParent[$j]->getVar($title) . $prefix;
166
        }
167
        if (array_key_exists($key, $category_array)) {
168
            /** @var \XoopsModules\Tdmdownloads\Category[] $category_array */
169
            $firstCategory = $category_array[$key]->getVar($title);
170
        } else {
171
            $firstCategory = '';
172
        }
173
        $path .= $firstCategory;
174
175
        return $path;
176
    }
177
178
    /**
179
     * @param \XoopsModules\Tdmdownloads\Tree $mytree
180
     * @param                                 $key
181
     * @param                                 $category_array
182
     * @param                                 $title
183
     * @param string                          $prefix
184
     * @param bool                            $link
185
     * @param string                          $order
186
     * @param bool                            $lasturl
187
     * @return string
188
     */
189
    public function getPathTreeUrl($mytree, $key, $category_array, $title, $prefix = '', $link = false, $order = 'ASC', $lasturl = false)
190
    {
191
        global $xoopsModule;
192
        $categoryParent = $mytree->getAllParent($key);
193
        if ('ASC' === $order) {
194
            $categoryParent = array_reverse($categoryParent);
195
            if (true === $link) {
196
                $path = '<a href="index.php">' . $xoopsModule->name() . '</a>' . $prefix;
197
            } else {
198
                $path = $xoopsModule->name() . $prefix;
199
            }
200
        } else {
201
            if (array_key_exists($key, $category_array)) {
202
                /** @var \XoopsModules\Tdmdownloads\Category[] $category_array */
203
                $firstCategory = $category_array[$key]->getVar($title);
204
            } else {
205
                $firstCategory = '';
206
            }
207
            $path = $firstCategory . $prefix;
208
        }
209
        foreach (array_keys($categoryParent) as $j) {
210
            /** @var \XoopsModules\Tdmdownloads\Category[] $categoryParent */
211
            if (true === $link) {
212
                $path .= '<a href="viewcat.php?cid=' . $categoryParent[$j]->getVar('cat_cid') . '">' . $categoryParent[$j]->getVar($title) . '</a>' . $prefix;
213
            } else {
214
                $path .= $categoryParent[$j]->getVar($title) . $prefix;
215
            }
216
        }
217
        if ('ASC' === $order) {
218
            if (array_key_exists($key, $category_array)) {
219
                if (true === $lasturl) {
220
                    $firstCategory = '<a href="viewcat.php?cid=' . $category_array[$key]->getVar('cat_cid') . '">' . $category_array[$key]->getVar($title) . '</a>';
221
                } else {
222
                    $firstCategory = $category_array[$key]->getVar($title);
223
                }
224
            } else {
225
                $firstCategory = '';
226
            }
227
            $path .= $firstCategory;
228
        } else {
229
            if (true === $link) {
230
                $path .= '<a href="index.php">' . $xoopsModule->name() . '</a>';
231
            } else {
232
                $path .= $xoopsModule->name();
233
            }
234
        }
235
236
        return $path;
237
    }
238
	 /**
239
     * Utility::StringSizeConvert()
240
     *
241
     * @param mixed $stringSize
242
     * @return mixed|int
243
     */
244
	public static function StringSizeConvert($stringSize){
245
        if ($stringSize != '') {
246
            $kb = 1024;
247
            $mb = 1024*1024;
248
            $gb = 1024*1024*1024;
249
			$size_value_arr = explode(' ', $stringSize);
250
			
251
            if ($size_value_arr[1] == 'B') {
252
                $mysize = $size_value_arr[0];
253
            } elseif ($size_value_arr[1] == 'K') {
254
                $mysize = $size_value_arr[0] * $kb;
255
            } elseif ($size_value_arr[1] == 'M') {
256
                $mysize = $size_value_arr[0] * $mb;
257
            } else {
258
                $mysize = $size_value_arr[0] * $gb;
259
            }
260
            return $mysize;
261
        } else {
262
            return 0;
263
        }
264
    }
265
	 /**
266
     * Utility::SizeConvertString()
267
     *
268
     * @param mixed $sizeString
269
     * @return mixed|string
270
     */
271
	public static function SizeConvertString($sizeString){
272
		$mysizeString = '';
273
		if ($sizeString != '') {
274
			$size_value_arr = explode(' ', $sizeString);
275
			if (array_key_exists (0, $size_value_arr) == true && array_key_exists (1, $size_value_arr) == true){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
276
				if ($size_value_arr[0] != ''){
277
					$mysizeString = '';
278
					switch ($size_value_arr[1]) {
279
						case 'B':
280
							$mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_BYTES;
281
							break;
282
							
283
						case 'K':
284
							$mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_KBYTES;
285
							break;
286
							
287
						case 'M':
288
							$mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_MBYTES;
289
							break;
290
							
291
						case 'G':
292
							$mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_GBYTES;
293
							break;
294
							
295
						case 'T':
296
							$mysizeString = $size_value_arr[0] . ' ' . _AM_TDMDOWNLOADS_TBYTES;
297
							break;
298
					}					
299
					return $mysizeString;
300
				}
301
			}
302
		}		
303
		return $mysizeString;
304
    }
305
	
306
	 /**
307
     * Utility::GetFileSize()
308
     *
309
     * @param mixed $url
310
     * @return mixed|string
311
     */
312
    public static function GetFileSize($url)
313
    {
314
		if (function_exists('curl_init') && false !== ($curlHandle  = curl_init($url))) {
315
			curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, TRUE);
316
			curl_setopt($curlHandle, CURLOPT_HEADER, TRUE);
317
			curl_setopt($curlHandle, CURLOPT_NOBODY, TRUE);	
318
			curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);			
319
			$curlReturn = curl_exec($curlHandle);
320
			if (false === $curlReturn) {
321
				trigger_error(curl_error($curlHandle));
322
				$size = 0;
323
			} else {
324
				$size = curl_getinfo($curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
325
			}
326
			curl_close($curlHandle);
327
			if ($size <= 0){
328
				return 0;
329
			} else {			
330
				return Utility::FileSizeConvert($size);
331
			}
332
		} else {
333
			return 0;
334
		}
335
    }
336
	
337
	/**
338
     * Utility::FileSizeConvert()
339
     *
340
     * @param mixed $size
341
     * @return mixed|string
342
     */
343
	public static function FileSizeConvert($size){
344
        if ($size > 0) {
345
            $kb = 1024;
346
            $mb = 1024*1024;
347
            $gb = 1024*1024*1024;
348
            if ($size >= $gb) {
349
                $mysize = sprintf ("%01.2f",$size/$gb) . " " . 'G';
350
            } elseif ($size >= $mb) {
351
                $mysize = sprintf ("%01.2f",$size/$mb) . " " . 'M';
352
            } elseif ($size >= $kb) {
353
                $mysize = sprintf ("%01.2f",$size/$kb) . " " . 'K';
354
            } else {
355
                $mysize = sprintf ("%01.2f",$size) . " " . 'B';
356
            }
357
358
            return $mysize;
359
        } else {
360
            return '';
361
        }
362
    }
363
}
364