1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * VSCode require extension php intelephense. |
||||
5 | */ |
||||
6 | require_once __DIR__ . '/vendor/autoload.php'; |
||||
7 | |||||
8 | // set root into current directory |
||||
9 | define('ROOT', __DIR__); |
||||
10 | resolve_dir(ROOT . '/tmp'); |
||||
11 | resolve_dir(ROOT . '/src/Session/sessions'); |
||||
12 | |||||
13 | // define php error file |
||||
14 | define('PHP_ERROR_FILE', ROOT . '/tmp/php-error.log'); |
||||
15 | |||||
16 | // define cors detector |
||||
17 | define('CORS', \MVC\helper::cors()); |
||||
18 | |||||
19 | // define localhost detector |
||||
20 | define('LOCAL', \MVC\helper::isLocal('/\.io$/s')); |
||||
21 | |||||
22 | // define PAGE UNIQUE ID |
||||
23 | $uri = \MVC\helper::get_clean_uri(); |
||||
24 | $uid = md5($uri . \MVC\helper::getRequestIP() . \MVC\helper::useragent()); |
||||
25 | define('UID', $uid); |
||||
26 | |||||
27 | // set default timezone |
||||
28 | date_default_timezone_set('Asia/Jakarta'); |
||||
29 | |||||
30 | $session = new \Session\session(3600, folder_session()); |
||||
31 | $router = new \MVC\router(); |
||||
32 | $router->session = $session; |
||||
33 | //$router->shutdown('telkomsel'); |
||||
34 | |||||
35 | // start environtment as development for debugging |
||||
36 | $env = 'production'; |
||||
37 | $debug_pdo = 1; |
||||
38 | |||||
39 | // this dimaslanjaka's localhost |
||||
40 | if (in_array($_SERVER['HTTP_HOST'], ['dev.ns.webmanajemen.com', 'localhost']) || LOCAL) { |
||||
41 | $env = 'development'; |
||||
42 | $debug_pdo = 3; |
||||
43 | } |
||||
44 | |||||
45 | // set framework environtment |
||||
46 | $router->environtment($env); |
||||
47 | define('ENVIRONMENT', $router->get_env()); |
||||
48 | // force debug when development mode |
||||
49 | if (ENVIRONMENT == 'development') { |
||||
50 | show_error(); |
||||
51 | } |
||||
52 | // set PDO Debug |
||||
53 | if (!defined('PDO_DEBUG')) { |
||||
54 | define('PDO_DEBUG', (string) $debug_pdo); |
||||
55 | } |
||||
56 | |||||
57 | $config = \Filemanager\file::get(__DIR__ . '/config.json', true); |
||||
58 | |||||
59 | if (!CORS) { |
||||
60 | // extends cache key for cache revisioning |
||||
61 | // [cache][ext] for development mode to disable browser caching without damaging interface or other |
||||
62 | $config['cache']['key'] .= $config['cache']['ext']; |
||||
63 | } |
||||
64 | |||||
65 | define('CONFIG', $config); |
||||
66 | |||||
67 | // ====== helper |
||||
68 | |||||
69 | $GLOBALS['config'] = $config; |
||||
70 | /** |
||||
71 | * Get config as array from config.json. |
||||
72 | * |
||||
73 | * @return array |
||||
74 | */ |
||||
75 | function get_conf() |
||||
76 | { |
||||
77 | if (!$GLOBALS['config']) { |
||||
78 | $GLOBALS['config'] = \Filemanager\file::get(ROOT . '/config.json', true); |
||||
79 | } |
||||
80 | |||||
81 | return $GLOBALS['config']; |
||||
82 | } |
||||
83 | /** |
||||
84 | * Save array of config to config.json. |
||||
85 | * |
||||
86 | * @return void |
||||
87 | */ |
||||
88 | function save_conf(array $newdata) |
||||
89 | { |
||||
90 | \Filemanager\file::file(__DIR__ . '/config.json', array_replace(get_conf(), $newdata), true); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
91 | } |
||||
92 | |||||
93 | /** |
||||
94 | * Get config database. |
||||
95 | * |
||||
96 | * @return array |
||||
97 | */ |
||||
98 | function get_db() |
||||
99 | { |
||||
100 | return $GLOBALS['config']['database']; |
||||
101 | } |
||||
102 | |||||
103 | //======== begin conf [DO NOT EDIT] |
||||
104 | |||||
105 | // ignore limitation if exists |
||||
106 | if (function_exists('set_time_limit')) { |
||||
107 | call_user_func('set_time_limit', 0); |
||||
108 | } |
||||
109 | |||||
110 | // ignore user abort execution to false |
||||
111 | if (function_exists('ignore_user_abort')) { |
||||
112 | call_user_func('ignore_user_abort', false); |
||||
113 | } |
||||
114 | |||||
115 | // set output buffering to zero |
||||
116 | ini_set('output_buffering', 0); |
||||
117 | |||||
118 | function useFB() |
||||
119 | { |
||||
120 | include ROOT . '/config-fb.php'; |
||||
121 | } |
||||
122 | |||||
123 | function useTsel() |
||||
124 | { |
||||
125 | if (!function_exists('telkomsel_api')) { |
||||
126 | include ROOT . '/config-tsel.php'; |
||||
127 | } |
||||
128 | } |
||||
129 | |||||
130 | function usem3() |
||||
131 | { |
||||
132 | if (!function_exists('m3')) { |
||||
133 | include ROOT . '/config-m3.php'; |
||||
134 | } |
||||
135 | } |
||||
136 | |||||
137 | function useGoogle() |
||||
138 | { |
||||
139 | if (!function_exists('google')) { |
||||
140 | include ROOT . '/config-google.php'; |
||||
141 | } |
||||
142 | } |
||||
143 | |||||
144 | //======== end conf |
||||
145 | /** |
||||
146 | * @var \User\user |
||||
147 | */ |
||||
148 | $user = new \User\user(CONFIG['database']['user'], CONFIG['database']['pass'], CONFIG['database']['dbname'], CONFIG['database']['host']); |
||||
149 | $pdo = $user->pdo_instance(); |
||||
150 | |||||
151 | /** |
||||
152 | * user instance. |
||||
153 | * |
||||
154 | * @return \User\user |
||||
155 | */ |
||||
156 | function user() |
||||
157 | { |
||||
158 | global $user; |
||||
159 | |||||
160 | return $user; |
||||
161 | } |
||||
162 | |||||
163 | $GLOBALS['office_instance'] = null; |
||||
164 | /** |
||||
165 | * Office instance. |
||||
166 | * |
||||
167 | * @return \Office\loader |
||||
168 | */ |
||||
169 | function office() |
||||
170 | { |
||||
171 | if (!$GLOBALS['office_instance']) { |
||||
172 | $GLOBALS['office_instance'] = new \Office\loader(pdo()); |
||||
173 | } |
||||
174 | |||||
175 | return $GLOBALS['office_instance']; |
||||
176 | } |
||||
177 | /** |
||||
178 | * user instance. |
||||
179 | * |
||||
180 | * @return \DB\pdo |
||||
181 | */ |
||||
182 | function pdo() |
||||
183 | { |
||||
184 | global $pdo; |
||||
185 | |||||
186 | return $pdo; |
||||
187 | } |
||||
188 | |||||
189 | // file scanner |
||||
190 | $scanner = new Filemanager\scan(); |
||||
191 | |||||
192 | function scan($dir) |
||||
0 ignored issues
–
show
The parameter
$dir is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
193 | { |
||||
194 | } |
||||
195 | |||||
196 | /** |
||||
197 | * Check if output buffering on. |
||||
198 | * |
||||
199 | * @return ob_get_level |
||||
0 ignored issues
–
show
The type
ob_get_level was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
200 | */ |
||||
201 | function isob() |
||||
202 | { |
||||
203 | return ob_get_level(); |
||||
0 ignored issues
–
show
|
|||||
204 | } |
||||
205 | |||||
206 | /** |
||||
207 | * Base URL router. |
||||
208 | * |
||||
209 | * @return void |
||||
210 | */ |
||||
211 | function base(string $path) |
||||
212 | { |
||||
213 | return (isset($_SERVER['HTTPS']) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $path; |
||||
0 ignored issues
–
show
|
|||||
214 | } |
||||
215 | |||||
216 | |||||
217 | /** |
||||
218 | * Filemanager Instance. |
||||
219 | * |
||||
220 | * @return void |
||||
221 | */ |
||||
222 | function filemanager() |
||||
223 | { |
||||
224 | return new \Filemanager\file(); |
||||
0 ignored issues
–
show
|
|||||
225 | } |
||||
226 | |||||
227 | /** |
||||
228 | * Get current environtment |
||||
229 | * |
||||
230 | * @return string |
||||
231 | */ |
||||
232 | function get_env() |
||||
233 | { |
||||
234 | global $router; |
||||
235 | |||||
236 | return $router->get_env(); |
||||
237 | } |
||||
238 | |||||
239 | /** |
||||
240 | * Get current router instance |
||||
241 | * |
||||
242 | * @return \MVC\router |
||||
243 | */ |
||||
244 | function router() |
||||
245 | { |
||||
246 | global $router; |
||||
247 | return $router; |
||||
248 | } |
||||
249 | |||||
250 | function Map($arr, $callback) |
||||
251 | { |
||||
252 | if (!is_callable($callback)) { |
||||
253 | throw new Exception('Callback must be function', 1); |
||||
254 | } |
||||
255 | |||||
256 | return array_map(function ($key, $val) use ($callback) { |
||||
257 | return call_user_func($callback, $key, $val); |
||||
258 | }, array_keys($arr), $arr); |
||||
259 | } |
||||
260 | |||||
261 | function strcond($first, $two, $success = null, $error = null) |
||||
262 | { |
||||
263 | $src = $first; |
||||
264 | if (!file_exists($src)) { |
||||
265 | $src = $two; |
||||
266 | } |
||||
267 | if (file_exists($src)) { |
||||
268 | if (is_callable($success)) { |
||||
269 | return call_user_func($success, $src); |
||||
270 | } else { |
||||
271 | return $src; |
||||
272 | } |
||||
273 | } else { |
||||
274 | if (is_callable($error)) { |
||||
275 | return call_user_func($error, $src); |
||||
276 | } |
||||
277 | } |
||||
278 | } |
||||
279 | |||||
280 | /** |
||||
281 | * echo print_r in pretext. |
||||
282 | * |
||||
283 | * @param mixed $str |
||||
284 | */ |
||||
285 | function printr($str, $str1 = 0, $str2 = 0) |
||||
286 | { |
||||
287 | echo '<pre>'; |
||||
288 | print_r($str); |
||||
289 | if ($str1) { |
||||
290 | print_r($str1); |
||||
291 | } |
||||
292 | if ($str2) { |
||||
293 | print_r($str2); |
||||
294 | } |
||||
295 | echo '</pre>'; |
||||
296 | } |
||||
297 | |||||
298 | /** |
||||
299 | * echo json_encode in pretext. |
||||
300 | */ |
||||
301 | function precom(...$str) |
||||
302 | { |
||||
303 | $D = json_encode($str, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
||||
304 | if (headers_sent()) { |
||||
305 | echo '<pre class="notranslate">'; |
||||
306 | echo $D; |
||||
307 | echo '</pre>'; |
||||
308 | } else { |
||||
309 | return $D; |
||||
310 | } |
||||
311 | } |
||||
312 | |||||
313 | /** |
||||
314 | * cURL shooter request. |
||||
315 | * |
||||
316 | * @param array $opt |
||||
317 | * |
||||
318 | * @return array |
||||
319 | */ |
||||
320 | function req($opt) |
||||
321 | { |
||||
322 | return \Extender\request::static_request($opt); |
||||
323 | } |
||||
324 | |||||
325 | /** |
||||
326 | * Limitation start. |
||||
327 | */ |
||||
328 | function getLimit(int $id_user = 0) |
||||
329 | { |
||||
330 | global $user; |
||||
331 | |||||
332 | if (!$user) { |
||||
333 | $user = new \User\user(CONFIG['database']['user'], CONFIG['database']['pass'], CONFIG['database']['dbname'], CONFIG['database']['host']); |
||||
334 | } |
||||
335 | if (0 == $id_user) { |
||||
336 | $id_user = $user->userdata('id'); |
||||
337 | } |
||||
338 | $limit = []; |
||||
339 | if ($user->is_login()) { |
||||
340 | $limit = pdo()->select('limitation')->where([ |
||||
341 | 'user_id' => $id_user, |
||||
342 | ])->row_array(); |
||||
343 | if (empty($limit)) { |
||||
344 | pdo()->insert_not_exists('limitation', [ |
||||
345 | 'user_id' => $id_user, |
||||
346 | ])->exec(); |
||||
347 | |||||
348 | return getLimit(); |
||||
349 | } |
||||
350 | } |
||||
351 | |||||
352 | return $limit; |
||||
353 | } |
||||
354 | |||||
355 | function getLimitRemaining() |
||||
356 | { |
||||
357 | $limit = (array) getLimit(); |
||||
358 | |||||
359 | if (isset($limit['max']) && isset($limit['success'])) { |
||||
360 | $max = (int) $limit['max']; |
||||
361 | $suc = (int) $limit['success']; |
||||
362 | $remaining = (int) ($max - $suc); |
||||
363 | //var_dump($max, $suc, ($max - $suc)); |
||||
364 | return $remaining; |
||||
365 | } |
||||
366 | |||||
367 | return 0; |
||||
368 | } |
||||
369 | |||||
370 | function getLimitSuccess() |
||||
371 | { |
||||
372 | $limit = (array) getLimit(); |
||||
373 | if (isset($limit['success']) && isset($limit['success'])) { |
||||
374 | $max = (int) $limit['success']; |
||||
375 | |||||
376 | return $max; |
||||
377 | } |
||||
378 | |||||
379 | return 0; |
||||
380 | } |
||||
381 | |||||
382 | function getLimitMax() |
||||
383 | { |
||||
384 | $limit = (array) getLimit(); |
||||
385 | if (isset($limit['max']) && isset($limit['success'])) { |
||||
386 | $max = (int) $limit['max']; |
||||
387 | |||||
388 | return $max; |
||||
389 | } |
||||
390 | |||||
391 | return 0; |
||||
392 | } |
||||
393 | |||||
394 | function getLimitBanned() |
||||
395 | { |
||||
396 | //var_dump(getLimitRemaining()); |
||||
397 | if (user()->is_login()) { |
||||
398 | return getLimitRemaining() <= 0; |
||||
399 | } |
||||
400 | } |
||||
401 | |||||
402 | function addLimitSuccess(int $id_user = 0) |
||||
403 | { |
||||
404 | global $user; |
||||
405 | if (0 == $id_user) { |
||||
406 | $id_user = $user->userdata('id'); |
||||
407 | } |
||||
408 | |||||
409 | return pdo()->sum('limitation', [ |
||||
410 | 'success' => 1, |
||||
411 | ], [ |
||||
412 | 'user_id' => $id_user, |
||||
413 | ])->exec(); |
||||
414 | } |
||||
415 | |||||
416 | function addLimitMax(int $id_user = 0, int $value) |
||||
417 | { |
||||
418 | global $user; |
||||
419 | if (0 == $id_user) { |
||||
420 | $id_user = $user->userdata('id'); |
||||
421 | } |
||||
422 | |||||
423 | return pdo()->update('limitation', [ |
||||
424 | 'max' => $value, |
||||
425 | ], [ |
||||
426 | 'user_id' => $id_user, |
||||
427 | ])->exec(); |
||||
428 | } |
||||
429 |