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.0.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
use Quantum\Exceptions\StopExecutionException; |
16
|
|
|
use Quantum\Libraries\Session\SessionManager; |
17
|
|
|
use Quantum\Libraries\Encryption\Cryptor; |
18
|
|
|
use Quantum\Exceptions\ExceptionMessages; |
19
|
|
|
use Quantum\Libraries\Auth\AuthManager; |
20
|
|
|
use Quantum\Libraries\Mailer\Mailer; |
21
|
|
|
use Quantum\Libraries\Cookie\Cookie; |
22
|
|
|
use Quantum\Libraries\Csrf\Csrf; |
23
|
|
|
use Quantum\Dumper\Dumper; |
24
|
|
|
|
25
|
|
|
if (!function_exists('session')) { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Gets session handler |
29
|
|
|
* @return \Quantum\Libraries\Session\Session |
30
|
|
|
*/ |
31
|
|
|
function session() |
32
|
|
|
{ |
33
|
|
|
return (new SessionManager())->getSessionHandler(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (!function_exists('cookie')) { |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Gets cookie handler |
42
|
|
|
* @return Quantum\Libraries\Cookie\Cookie |
43
|
|
|
*/ |
44
|
|
|
function cookie() |
45
|
|
|
{ |
46
|
|
|
return new Cookie($_COOKIE, new Cryptor); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!function_exists('auth')) { |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets the Auth instance |
55
|
|
|
* @return \Quantum\Libraries\Auth\WebAuth|Quantum\Libraries\Auth\ApiAuth|Quantum\Libraries\Auth\AuthenticableInterface |
56
|
|
|
*/ |
57
|
|
|
function auth() |
58
|
|
|
{ |
59
|
|
|
return (new AuthManager())->get(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!function_exists('mailer')) { |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Gets the Mail instance |
68
|
|
|
* @return Mailer |
69
|
|
|
*/ |
70
|
|
|
function mailer() |
71
|
|
|
{ |
72
|
|
|
return new Mailer(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!function_exists('csrf_token')) { |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Outputs generated CSRF token |
81
|
|
|
* @return void |
82
|
|
|
* @throws CsrfException |
83
|
|
|
*/ |
84
|
|
|
function csrf_token() |
85
|
|
|
{ |
86
|
|
|
echo Csrf::generateToken(session(), env('APP_KEY')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (!function_exists('_message')) { |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* _message |
95
|
|
|
* @param string $subject |
96
|
|
|
* @param string|array $params |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
function _message($subject, $params) |
100
|
|
|
{ |
101
|
|
|
if (is_array($params)) { |
102
|
|
|
return preg_replace_callback('/{%\d+}/', function () use (&$params) { |
103
|
|
|
return array_shift($params); |
104
|
|
|
}, $subject); |
105
|
|
|
} else { |
106
|
|
|
return preg_replace('/{%\d+}/', $params, $subject); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (!function_exists('out')) { |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Outputs the dump of variable |
116
|
|
|
* @param mixed $var |
117
|
|
|
* @param bool |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
function out($var, $die = false) |
121
|
|
|
{ |
122
|
|
|
Dumper::dump($var, $die); |
123
|
|
|
|
124
|
|
|
if ($die) { |
125
|
|
|
die; |
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (!function_exists('valid_base64')) { |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Validates base64 string |
135
|
|
|
* @param string $string |
136
|
|
|
* @return boolean |
137
|
|
|
*/ |
138
|
|
|
function valid_base64($string) |
139
|
|
|
{ |
140
|
|
|
$decoded = base64_decode($string, true); |
141
|
|
|
|
142
|
|
|
// Check if there is no invalid character in string |
143
|
|
|
if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) { |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// Decode the string in strict mode and send the response |
148
|
|
|
if (!base64_decode($string, true)) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// Encode and compare it to original one |
153
|
|
|
if (base64_encode($decoded) != $string) { |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (!function_exists('get_directory_classes')) { |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Gets directory classes |
166
|
|
|
* @param string $path |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
function get_directory_classes($path) |
170
|
|
|
{ |
171
|
|
|
$class_names = []; |
172
|
|
|
|
173
|
|
|
if (is_dir($path)) { |
174
|
|
|
$allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); |
175
|
|
|
$phpFiles = new RegexIterator($allFiles, '/\.php$/'); |
176
|
|
|
|
177
|
|
|
foreach ($phpFiles as $file) { |
178
|
|
|
$class = pathinfo($file->getFilename()); |
179
|
|
|
array_push($class_names, $class['filename']); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $class_names; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if (!function_exists('get_caller_class')) { |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Gets the caller class |
192
|
|
|
* @param integer $index |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
function get_caller_class($index = 2) |
196
|
|
|
{ |
197
|
|
|
$caller = debug_backtrace(); |
198
|
|
|
$caller = $caller[$index]; |
199
|
|
|
|
200
|
|
|
return $caller['class'] ?? null; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if (!function_exists('get_caller_function')) { |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Gets the caller function |
209
|
|
|
* @param integer $index |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
function get_caller_function($index = 2) |
213
|
|
|
{ |
214
|
|
|
$caller = debug_backtrace(); |
215
|
|
|
$caller = $caller[$index]; |
216
|
|
|
|
217
|
|
|
return $caller['function'] ?? null; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if (!function_exists('stop')) { |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Throws Stop Execution Exception |
226
|
|
|
*/ |
227
|
|
|
function stop() |
228
|
|
|
{ |
229
|
|
|
throw new StopExecutionException(ExceptionMessages::EXECUTION_TERMINATED); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.