1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if (!function_exists('rgb2hex')) { |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @param int $red |
7
|
|
|
* @param int $green |
8
|
|
|
* @param int $blue |
9
|
|
|
* @return string |
10
|
|
|
* @see https://github.com/spatie-custom/blender/blob/master/app/Foundation/helpers.php |
11
|
|
|
*/ |
12
|
|
|
function rgb2hex(int $red, int $green, int $blue): string |
13
|
|
|
{ |
14
|
|
|
return '#' . collect([$red, $green, $blue]) |
15
|
|
|
->map(function (int $decimal): string { |
16
|
|
|
return str_pad(dechex($decimal), 2, STR_PAD_LEFT); |
17
|
|
|
}) |
18
|
|
|
->implode(''); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param float $val |
24
|
|
|
* @param int $precision |
25
|
|
|
* @param string $simbol |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
function format_money(float $val = 0, int $precision = 2, string $simbol = "") : string |
29
|
|
|
{ |
30
|
|
|
return "$simbol " . number_format($val, $precision, ',', '.'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Format float 1125.86 into string '&euro 1.125,86' |
35
|
|
|
* @param float $val |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
function format_euro(float $val = 0) : string |
39
|
|
|
{ |
40
|
|
|
return format_money($val, 2, '€ '); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!function_exists('ordinal')) { |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Given a number, return the number + 'th' or 'rd' etc |
47
|
|
|
* @param $cdnl |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
function ordinal($cdnl) |
51
|
|
|
{ |
52
|
|
|
$test_c = abs($cdnl) % 10; |
53
|
|
|
$ext = ((abs($cdnl) % 100 < 21 && abs($cdnl) % 100 > 4) ? 'th' |
54
|
|
|
: (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1) |
55
|
|
|
? 'th' : 'st' : 'nd' : 'rd' : 'th')); |
56
|
|
|
return $cdnl . $ext; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!function_exists('value')) { |
61
|
|
|
/** |
62
|
|
|
* Return the default value of the given value. |
63
|
|
|
* |
64
|
|
|
* @param mixed $value |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
function value($value) |
68
|
|
|
{ |
69
|
|
|
return $value instanceof Closure ? $value() : $value; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
if (!function_exists('with')) { |
73
|
|
|
/** |
74
|
|
|
* Return the given object. Useful for chaining. |
75
|
|
|
* |
76
|
|
|
* @param mixed $object |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
function with($object) |
80
|
|
|
{ |
81
|
|
|
return $object; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set the default configuration of erro reporting for production. |
87
|
|
|
*/ |
88
|
|
|
function setErrorReportingForProduction() |
89
|
|
|
{ |
90
|
|
|
if (version_compare(PHP_VERSION, '5.4.0') >= 0) { |
91
|
|
|
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT); |
92
|
|
|
} elseif (version_compare(PHP_VERSION, '5.3.0') >= 0) { |
93
|
|
|
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED); |
94
|
|
|
} else { |
95
|
|
|
error_reporting(E_ALL ^ E_NOTICE); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Check if PHP script was executed by shell. |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
function isExecutedByCLI() : bool |
104
|
|
|
{ |
105
|
|
|
return php_sapi_name() == 'cli'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Convert the output of PHP's filesize() function |
110
|
|
|
* to a nice format with PB, TB, GB, MB, kB, bytes. |
111
|
|
|
* @param $bytes |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
function bytes2HumanSize($bytes) |
115
|
|
|
{ |
116
|
|
|
if ($bytes >= 1125899906842624) { |
117
|
|
|
$bytes = number_format($bytes / 1073741824, 2) . ' PB'; |
118
|
|
|
} elseif ($bytes >= 1099511627776) { |
119
|
|
|
$bytes = number_format($bytes / 1073741824, 2) . ' TB'; |
120
|
|
|
} elseif ($bytes >= 1073741824) { |
121
|
|
|
$bytes = number_format($bytes / 1073741824, 2) . ' GB'; |
122
|
|
|
} elseif ($bytes >= 1048576) { |
123
|
|
|
$bytes = number_format($bytes / 1048576, 2) . ' MB'; |
124
|
|
|
} elseif ($bytes >= 1024) { |
125
|
|
|
$bytes = number_format($bytes / 1024, 2) . ' kB'; |
126
|
|
|
} elseif ($bytes > 1) { |
127
|
|
|
$bytes .= ' bytes'; |
128
|
|
|
} elseif ($bytes == 1) { |
129
|
|
|
$bytes .= ' byte'; |
130
|
|
|
} else { |
131
|
|
|
$bytes = '0 bytes'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $bytes; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* This function transforms the php.ini notation for numbers (like '2M') |
139
|
|
|
* to an integer (2*1024*1024 in this case) |
140
|
|
|
* @param string $sSize |
141
|
|
|
* @return int|string |
142
|
|
|
*/ |
143
|
|
|
function convertPHPSizeToBytes($sSize) |
144
|
|
|
{ |
145
|
|
|
if (is_numeric($sSize)) { |
146
|
|
|
return $sSize; |
147
|
|
|
} |
148
|
|
|
$sSuffix = substr($sSize, -1); |
149
|
|
|
$iValue = substr($sSize, 0, -1); |
150
|
|
|
|
151
|
|
|
switch (strtoupper($sSuffix)) { |
152
|
|
|
case 'P': |
153
|
|
|
$iValue *= 1024; |
154
|
|
|
//PB multiplier |
155
|
|
|
case 'T': |
156
|
|
|
$iValue *= 1024; |
157
|
|
|
//TB multiplier |
158
|
|
|
case 'G': |
159
|
|
|
$iValue *= 1024; |
160
|
|
|
//GB multiplier |
161
|
|
|
case 'M': |
162
|
|
|
$iValue *= 1024; |
163
|
|
|
//MB multiplier |
164
|
|
|
case 'K': |
165
|
|
|
$iValue *= 1024; |
166
|
|
|
//KB multiplier |
167
|
|
|
break; |
168
|
|
|
} |
169
|
|
|
return $iValue; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Return the Max upload size in bytes. |
174
|
|
|
* @param bool $humanFormat if set to true return size in human format (MB, kB, etc..) otherwise return in bytes. |
175
|
|
|
* @return int |
176
|
|
|
*/ |
177
|
|
|
function getMaximumFileUploadSize(bool $humanFormat = false) |
178
|
|
|
{ |
179
|
|
|
$size = min(convertPHPSizeToBytes(ini_get('post_max_size')), convertPHPSizeToBytes(ini_get('upload_max_filesize'))); |
180
|
|
|
|
181
|
|
|
if (!$humanFormat) { |
182
|
|
|
return $size; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return bytes2HumanSize($size); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Encrypt string. |
190
|
|
|
* @param string $string to encrypt. |
191
|
|
|
* @param string $chiave the key to encrypt. if empty generate a random key on the fly. |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
function encryptString(string $string, string $chiave = '') |
195
|
|
|
{ |
196
|
|
|
if ($chiave == '') { |
197
|
|
|
$chiave = str_random(64); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$key = pack('H*', $chiave); |
201
|
|
|
|
202
|
|
|
$plaintext = $string; |
203
|
|
|
|
204
|
|
|
# create a random IV to use with CBC encoding |
205
|
|
|
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); |
206
|
|
|
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
207
|
|
|
|
208
|
|
|
# creates a cipher text compatible with AES (Rijndael block size = 128) |
209
|
|
|
# to keep the text confidential |
210
|
|
|
# only suitable for encoded input that never ends with value 00h |
211
|
|
|
# (because of default zero padding) |
212
|
|
|
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_CBC, $iv); |
213
|
|
|
|
214
|
|
|
# prepend the IV for it to be available for decryption |
215
|
|
|
$ciphertext = $iv . $ciphertext; |
216
|
|
|
|
217
|
|
|
# encode the resulting cipher text so it can be represented by a string |
218
|
|
|
$ciphertext_base64 = base64_encode($ciphertext); |
219
|
|
|
|
220
|
|
|
return $ciphertext_base64; |
221
|
|
|
} |
222
|
|
|
|