|
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.9.5 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
use Quantum\Libraries\Database\Exceptions\DatabaseException; |
|
16
|
|
|
use Quantum\Libraries\Transformer\TransformerInterface; |
|
17
|
|
|
use Quantum\Libraries\Transformer\TransformerManager; |
|
18
|
|
|
use Quantum\Libraries\Encryption\CryptorException; |
|
19
|
|
|
use Quantum\Exceptions\StopExecutionException; |
|
20
|
|
|
use Quantum\Libraries\Asset\AssetException; |
|
21
|
|
|
use Quantum\Libraries\Encryption\Cryptor; |
|
22
|
|
|
use Quantum\Libraries\Lang\LangException; |
|
23
|
|
|
use Quantum\Libraries\Asset\AssetManager; |
|
24
|
|
|
use Quantum\Exceptions\ViewException; |
|
25
|
|
|
use Quantum\Exceptions\AppException; |
|
26
|
|
|
use Quantum\Exceptions\DiException; |
|
27
|
|
|
use Twig\Error\RuntimeError; |
|
28
|
|
|
use Twig\Error\LoaderError; |
|
29
|
|
|
use Twig\Error\SyntaxError; |
|
30
|
|
|
use Quantum\Http\Response; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Generates the CSRF token |
|
34
|
|
|
* @return string|null |
|
35
|
|
|
* @throws AppException |
|
36
|
|
|
* @throws DatabaseException |
|
37
|
|
|
* @throws CryptorException |
|
38
|
|
|
*/ |
|
39
|
|
|
function csrf_token(): ?string |
|
40
|
|
|
{ |
|
41
|
|
|
$appKey = env('APP_KEY'); |
|
42
|
|
|
|
|
43
|
|
|
if (!$appKey) { |
|
44
|
|
|
throw AppException::missingAppKey(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return csrf()->generateToken($appKey); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Compose a message |
|
52
|
|
|
* @param string $subject |
|
53
|
|
|
* @param string|array $params |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
function _message(string $subject, $params): string |
|
57
|
|
|
{ |
|
58
|
|
|
if (is_array($params)) { |
|
59
|
|
|
return preg_replace_callback('/{%\d+}/', function () use (&$params) { |
|
60
|
|
|
return array_shift($params); |
|
61
|
|
|
}, $subject); |
|
62
|
|
|
} else { |
|
63
|
|
|
return preg_replace('/{%\d+}/', $params, $subject); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Validates base64 string |
|
69
|
|
|
* @param string $string |
|
70
|
|
|
* @return boolean |
|
71
|
|
|
*/ |
|
72
|
|
|
function valid_base64(string $string): bool |
|
73
|
|
|
{ |
|
74
|
|
|
$decoded = base64_decode($string, true); |
|
75
|
|
|
|
|
76
|
|
|
if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (!base64_decode($string, true)) { |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (base64_encode($decoded) != $string) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Gets directory classes |
|
93
|
|
|
* @param string $path |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
function get_directory_classes(string $path): array |
|
97
|
|
|
{ |
|
98
|
|
|
$class_names = []; |
|
99
|
|
|
|
|
100
|
|
|
if (is_dir($path)) { |
|
101
|
|
|
$allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); |
|
102
|
|
|
$phpFiles = new RegexIterator($allFiles, '/\.php$/'); |
|
103
|
|
|
|
|
104
|
|
|
foreach ($phpFiles as $file) { |
|
105
|
|
|
$class = pathinfo($file->getFilename()); |
|
106
|
|
|
$class_names[] = $class['filename']; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $class_names; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Gets the caller class |
|
115
|
|
|
* @param integer $index |
|
116
|
|
|
* @return string|null |
|
117
|
|
|
*/ |
|
118
|
|
|
function get_caller_class(int $index = 2): ?string |
|
119
|
|
|
{ |
|
120
|
|
|
$caller = debug_backtrace(); |
|
121
|
|
|
$caller = $caller[$index]; |
|
122
|
|
|
|
|
123
|
|
|
return $caller['class'] ?? null; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Gets the caller function |
|
128
|
|
|
* @param integer $index |
|
129
|
|
|
* @return string|null |
|
130
|
|
|
*/ |
|
131
|
|
|
function get_caller_function(int $index = 2): ?string |
|
132
|
|
|
{ |
|
133
|
|
|
$caller = debug_backtrace(); |
|
134
|
|
|
$caller = $caller[$index]; |
|
135
|
|
|
|
|
136
|
|
|
return $caller['function'] ?? null; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Stops the execution |
|
141
|
|
|
* @param Closure|null $closure |
|
142
|
|
|
* @throws StopExecutionException |
|
143
|
|
|
*/ |
|
144
|
|
|
function stop(Closure $closure = null) |
|
145
|
|
|
{ |
|
146
|
|
|
if ($closure) { |
|
147
|
|
|
$closure(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
throw StopExecutionException::executionTerminated(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Generates random number sequence |
|
155
|
|
|
* @param int $length |
|
156
|
|
|
* @return int |
|
157
|
|
|
*/ |
|
158
|
|
|
function random_number(int $length = 10): int |
|
159
|
|
|
{ |
|
160
|
|
|
$randomString = ''; |
|
161
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
162
|
|
|
$randomString .= rand(0, 9); |
|
163
|
|
|
} |
|
164
|
|
|
return (int)$randomString; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Slugify the string |
|
169
|
|
|
* @param string $text |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
function slugify(string $text): string |
|
173
|
|
|
{ |
|
174
|
|
|
$text = trim($text, ' '); |
|
175
|
|
|
$text = preg_replace('/[^\p{L}\p{N}]/u', ' ', $text); |
|
176
|
|
|
$text = preg_replace('/\s+/', '-', $text); |
|
177
|
|
|
$text = trim($text, '-'); |
|
178
|
|
|
$text = mb_strtolower($text); |
|
179
|
|
|
|
|
180
|
|
|
if (empty($text)) { |
|
181
|
|
|
return 'n-a'; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $text; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Dumps the assets |
|
189
|
|
|
* @param string $type |
|
190
|
|
|
* @throws AssetException |
|
191
|
|
|
* @throws LangException |
|
192
|
|
|
*/ |
|
193
|
|
|
function assets(string $type) |
|
194
|
|
|
{ |
|
195
|
|
|
AssetManager::getInstance()->dump(AssetManager::STORES[$type]); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Checks if the entity is closure |
|
200
|
|
|
* @param mixed $entity |
|
201
|
|
|
* @return bool |
|
202
|
|
|
*/ |
|
203
|
|
|
function is_closure($entity): bool |
|
204
|
|
|
{ |
|
205
|
|
|
return $entity instanceof Closure; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Transforms the data by given transformer signature |
|
210
|
|
|
* @param array $data |
|
211
|
|
|
* @param TransformerInterface $transformer |
|
212
|
|
|
* @return array |
|
213
|
|
|
*/ |
|
214
|
|
|
function transform(array $data, TransformerInterface $transformer): array |
|
215
|
|
|
{ |
|
216
|
|
|
return TransformerManager::transform($data, $transformer); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Checks the app debug mode |
|
221
|
|
|
* @return bool |
|
222
|
|
|
*/ |
|
223
|
|
|
function is_debug_mode(): bool |
|
224
|
|
|
{ |
|
225
|
|
|
return filter_var(config()->get('debug'), FILTER_VALIDATE_BOOLEAN); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Handles page not found |
|
230
|
|
|
* @throws ReflectionException |
|
231
|
|
|
* @throws DiException |
|
232
|
|
|
* @throws ViewException |
|
233
|
|
|
* @throws LoaderError |
|
234
|
|
|
* @throws RuntimeError |
|
235
|
|
|
* @throws SyntaxError |
|
236
|
|
|
*/ |
|
237
|
|
|
function page_not_found() |
|
238
|
|
|
{ |
|
239
|
|
|
$acceptHeader = Response::getHeader('Accept'); |
|
240
|
|
|
|
|
241
|
|
|
$isJson = $acceptHeader === 'application/json'; |
|
242
|
|
|
|
|
243
|
|
|
if ($isJson) { |
|
244
|
|
|
Response::json( |
|
245
|
|
|
['status' => 'error', 'message' => 'Page not found'], |
|
246
|
|
|
404 |
|
247
|
|
|
); |
|
248
|
|
|
} else { |
|
249
|
|
|
Response::html( |
|
250
|
|
|
partial('errors' . DS . '404'), |
|
251
|
|
|
404 |
|
252
|
|
|
); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Encodes the data cryptographically |
|
258
|
|
|
* @param mixed $data |
|
259
|
|
|
* @return string |
|
260
|
|
|
* @throws CryptorException |
|
261
|
|
|
*/ |
|
262
|
|
|
function crypto_encode($data): string |
|
263
|
|
|
{ |
|
264
|
|
|
$data = (is_array($data) || is_object($data)) ? serialize($data) : $data; |
|
265
|
|
|
return Cryptor::getInstance()->encrypt($data); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Decodes the data cryptographically |
|
270
|
|
|
* @param string $value |
|
271
|
|
|
* @return mixed|string |
|
272
|
|
|
* @throws CryptorException |
|
273
|
|
|
*/ |
|
274
|
|
|
function crypto_decode(string $value) |
|
275
|
|
|
{ |
|
276
|
|
|
if (empty($value)) { |
|
277
|
|
|
return $value; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
$decrypted = Cryptor::getInstance()->decrypt($value); |
|
281
|
|
|
|
|
282
|
|
|
if ($data = @unserialize($decrypted)) { |
|
283
|
|
|
$decrypted = $data; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return $decrypted; |
|
287
|
|
|
} |
|
288
|
|
|
|