|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The file contains: Utils class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package App |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright YetiForce Sp. z o.o. |
|
8
|
|
|
* @license YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com) |
|
9
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace App; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Utils class. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Utils |
|
18
|
|
|
{ |
|
19
|
|
|
public static function recurseDelete(string $src) |
|
20
|
|
|
{ |
|
21
|
|
|
$vendorDir = \dirname(__FILE__, 2); |
|
22
|
|
|
$rootDir = \dirname($vendorDir, 2) . \DIRECTORY_SEPARATOR; |
|
23
|
|
|
if (!file_exists($rootDir . $src)) { |
|
24
|
|
|
return; |
|
25
|
|
|
} |
|
26
|
|
|
$dirs = []; |
|
27
|
|
|
if (is_dir($src)) { |
|
28
|
|
|
$dirs[] = $rootDir . $src; |
|
29
|
|
|
} |
|
30
|
|
|
@chmod($rootDir . $src, 0777); |
|
|
|
|
|
|
31
|
|
|
if (is_dir($src)) { |
|
32
|
|
|
foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) { |
|
33
|
|
|
if ($item->isDir()) { |
|
34
|
|
|
$dirs[] = $rootDir . $src . \DIRECTORY_SEPARATOR . $iterator->getSubPathName(); |
|
35
|
|
|
} else { |
|
36
|
|
|
unlink($rootDir . $src . \DIRECTORY_SEPARATOR . $iterator->getSubPathName()); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
arsort($dirs); |
|
40
|
|
|
foreach ($dirs as $dir) { |
|
41
|
|
|
rmdir($dir); |
|
42
|
|
|
} |
|
43
|
|
|
} else { |
|
44
|
|
|
unlink($rootDir . $src); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get absolute URL for Portal2. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $url |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function absoluteUrl(string $url): string |
|
56
|
|
|
{ |
|
57
|
|
|
return \App\Config::get('portalUrl') . $url; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get public url from file. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $name |
|
64
|
|
|
* @param bool $full |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function getPublicUrl($name, $full = false): string |
|
69
|
|
|
{ |
|
70
|
|
|
$basePath = ''; |
|
71
|
|
|
if ($full) { |
|
72
|
|
|
$basePath .= \App\Config::get('portalUrl'); |
|
73
|
|
|
} |
|
74
|
|
|
$basePath .= PUBLIC_DIRECTORY; |
|
75
|
|
|
return $basePath . $name; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Replacement for the ucfirst function for proper Multibyte String operation. |
|
80
|
|
|
* Delete function will exist as mb_ucfirst. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $string |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function mbUcfirst(string $string): string |
|
87
|
|
|
{ |
|
88
|
|
|
return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Parse bytes. |
|
93
|
|
|
* |
|
94
|
|
|
* @param mixed $str |
|
95
|
|
|
* |
|
96
|
|
|
* @return float |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function parseBytes($str): float |
|
99
|
|
|
{ |
|
100
|
|
|
if (is_numeric($str)) { |
|
101
|
|
|
return (float) $str; |
|
102
|
|
|
} |
|
103
|
|
|
$bytes = 0; |
|
104
|
|
|
if (preg_match('/([0-9\.]+)\s*([a-z]*)/i', $str, $regs)) { |
|
105
|
|
|
$bytes = (float) ($regs[1]); |
|
106
|
|
|
switch (strtolower($regs[2])) { |
|
107
|
|
|
case 'g': |
|
108
|
|
|
case 'gb': |
|
109
|
|
|
$bytes *= 1073741824; |
|
110
|
|
|
break; |
|
111
|
|
|
case 'm': |
|
112
|
|
|
case 'mb': |
|
113
|
|
|
$bytes *= 1048576; |
|
114
|
|
|
break; |
|
115
|
|
|
case 'k': |
|
116
|
|
|
case 'kb': |
|
117
|
|
|
$bytes *= 1024; |
|
118
|
|
|
break; |
|
119
|
|
|
default: |
|
120
|
|
|
break; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
return (float) $bytes; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Show bytes. |
|
128
|
|
|
* |
|
129
|
|
|
* @param mixed $bytes |
|
130
|
|
|
* @param string|null $unit |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public static function showBytes($bytes, &$unit = null): string |
|
135
|
|
|
{ |
|
136
|
|
|
$bytes = self::parseBytes($bytes); |
|
|
|
|
|
|
137
|
|
|
if ($bytes >= 1073741824) { |
|
138
|
|
|
$unit = 'GB'; |
|
139
|
|
|
$gb = $bytes / 1073741824; |
|
140
|
|
|
$str = sprintf($gb >= 10 ? '%d ' : '%.2f ', $gb) . $unit; |
|
141
|
|
|
} elseif ($bytes >= 1048576) { |
|
142
|
|
|
$unit = 'MB'; |
|
143
|
|
|
$mb = $bytes / 1048576; |
|
144
|
|
|
$str = sprintf($mb >= 10 ? '%d ' : '%.2f ', $mb) . $unit; |
|
145
|
|
|
} elseif ($bytes >= 1024) { |
|
146
|
|
|
$unit = 'KB'; |
|
147
|
|
|
$str = sprintf('%d ', round($bytes / 1024)) . $unit; |
|
148
|
|
|
} else { |
|
149
|
|
|
$unit = 'B'; |
|
150
|
|
|
$str = sprintf('%d ', $bytes) . $unit; |
|
151
|
|
|
} |
|
152
|
|
|
return $str; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: