Completed
Push — master ( 6f3a7a...818ba8 )
by Lorenzo
02:14
created

helpers.php ➔ getMaximumFileUploadSize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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