Passed
Pull Request — master (#100)
by Arman
03:20
created

crypto_encode()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.8.0
13
 */
14
15
use Quantum\Libraries\Transformer\TransformerInterface;
16
use Quantum\Libraries\Transformer\TransformerManager;
17
use Quantum\Exceptions\StopExecutionException;
18
use Quantum\Libraries\Encryption\Cryptor;
19
use Quantum\Libraries\Asset\AssetManager;
20
use Quantum\Exceptions\CryptorException;
21
use Quantum\Exceptions\AppException;
22
use Quantum\Libraries\Csrf\Csrf;
23
24
/**
25
 * Generates the CSRF token
26
 * @return string|null
27
 * @throws \Quantum\Exceptions\AppException
28
 * @throws \Quantum\Exceptions\DatabaseException
29
 * @throws \Quantum\Exceptions\SessionException
30
 */
31
function csrf_token(): ?string
32
{
33
    $appKey = env('APP_KEY');
34
35
    if (!$appKey) {
36
        throw AppException::missingAppKey();
37
    }
38
39
    return Csrf::generateToken(session(), $appKey);
40
}
41
42
/**
43
 * _message
44
 * @param string $subject
45
 * @param string|array $params
46
 * @return string
47
 */
48
function _message(string $subject, $params): string
49
{
50
    if (is_array($params)) {
51
        return preg_replace_callback('/{%\d+}/', function () use (&$params) {
52
            return array_shift($params);
53
        }, $subject);
54
    } else {
55
        return preg_replace('/{%\d+}/', $params, $subject);
56
    }
57
}
58
59
/**
60
 * Validates base64 string
61
 * @param string $string
62
 * @return boolean
63
 */
64
function valid_base64(string $string): bool
65
{
66
    $decoded = base64_decode($string, true);
67
68
    // Check if there is no invalid character in string
69
    if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) {
70
        return false;
71
    }
72
73
    // Decode the string in strict mode and send the response
74
    if (!base64_decode($string, true)) {
75
        return false;
76
    }
77
78
    // Encode and compare it to original one
79
    if (base64_encode($decoded) != $string) {
80
        return false;
81
    }
82
83
    return true;
84
}
85
86
/**
87
 * Gets directory classes
88
 * @param string $path
89
 * @return array
90
 */
91
function get_directory_classes(string $path): array
92
{
93
    $class_names = [];
94
95
    if (is_dir($path)) {
96
        $allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
97
        $phpFiles = new RegexIterator($allFiles, '/\.php$/');
98
99
        foreach ($phpFiles as $file) {
100
            $class = pathinfo($file->getFilename());
101
            $class_names[] = $class['filename'];
102
        }
103
    }
104
105
    return $class_names;
106
}
107
108
/**
109
 * Gets the caller class
110
 * @param integer $index
111
 * @return string|null
112
 */
113
function get_caller_class(int $index = 2): ?string
114
{
115
    $caller = debug_backtrace();
116
    $caller = $caller[$index];
117
118
    return $caller['class'] ?? null;
119
}
120
121
/**
122
 * Gets the caller function
123
 * @param integer $index
124
 * @return string|null
125
 */
126
function get_caller_function(int $index = 2): ?string
127
{
128
    $caller = debug_backtrace();
129
    $caller = $caller[$index];
130
131
    return $caller['function'] ?? null;
132
}
133
134
/**
135
 * Stops the execution
136
 * @param Closure|null $closure
137
 * @throws StopExecutionException
138
 */
139
function stop(Closure $closure = null)
140
{
141
    if ($closure) {
142
        $closure();
143
    }
144
145
    throw StopExecutionException::executionTerminated();
146
}
147
148
/**
149
 * Generates random number sequence
150
 * @param int $length
151
 * @return int
152
 */
153
function random_number(int $length = 10): int
154
{
155
    $randomString = '';
156
    for ($i = 0; $i < $length; $i++) {
157
        $randomString .= rand(0, 9);
158
    }
159
    return (int)$randomString;
160
}
161
162
/**
163
 * Slugify the string
164
 * @param string $text
165
 * @return string
166
 */
167
function slugify(string $text): string
168
{
169
    $text = trim($text, ' ');
170
    $text = preg_replace('/[^\p{L}\p{N}]/u', ' ', $text);
171
    $text = preg_replace('/\s+/', '-', $text);
172
    $text = trim($text, '-');
173
    $text = mb_strtolower($text);
174
175
    if (empty($text)) {
176
        return 'n-a';
177
    }
178
    return $text;
179
}
180
181
/**
182
 * Dumps the assets
183
 * @param string $type
184
 * @throws \Quantum\Exceptions\AssetException
185
 */
186
function assets(string $type)
187
{
188
    $assetTypes = [
189
        'css' => 1,
190
        'js' => 2
191
    ];
192
193
    AssetManager::getInstance()->dump($assetTypes[$type]);
194
}
195
196
/**
197
 * Checks if the entity is closure
198
 * @param mixed $entity
199
 * @return bool
200
 */
201
function is_closure($entity): bool
202
{
203
    return $entity instanceof Closure;
204
}
205
206
/**
207
 * Transforms the data by given transformer signature
208
 * @param array $data
209
 * @param TransformerInterface $transformer
210
 * @return array
211
 */
212
function transform(array $data, TransformerInterface $transformer): array
213
{
214
    return TransformerManager::transform($data, $transformer);
215
}
216
217
/**
218
 * Encodes the data cryptographically
219
 * @param mixed $data
220
 * @return string
221
 * @throws CryptorException
222
 */
223
function crypto_encode($data): string
224
{
225
    $data = (is_array($data) || is_object($data)) ? serialize($data) : $data;
226
    return Cryptor::getInstance()->encrypt($data);
227
}
228
229
/**
230
 * Decodes the data cryptographically
231
 * @param string $value
232
 * @return mixed|string
233
 * @throws CryptorException
234
 */
235
function crypto_decode(string $value)
236
{
237
    if (empty($value)) {
238
        return $value;
239
    }
240
241
    $decrypted = Cryptor::getInstance()->decrypt($value);
242
243
    if ($data = @unserialize($decrypted)) {
244
        $decrypted = $data;
245
    }
246
247
    return $decrypted;
248
}
249