Completed
Push — master ( 98b2f1...a418ff )
by Dimas
08:55
created

router()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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 cors detector
14
define('CORS', \MVC\helper::cors());
15
16
// define localhost detector
17
define('LOCAL', \MVC\helper::isLocal('/\.io$/s'));
18
19
// define PAGE UNIQUE ID
20
$uri = \MVC\helper::get_clean_uri();
21
$uid = md5($uri . \MVC\helper::getRequestIP() . \MVC\helper::useragent());
22
define('UID', $uid);
23
24
// set default timezone
25
date_default_timezone_set('Asia/Jakarta');
26
27
$session = new \Session\session(3600, folder_session());
28
$router = new \MVC\router();
29
$router->session = $session;
30
//$router->shutdown('telkomsel');
31
32
// start environtment as development for debugging
33
$env = 'production';
34
$debug_pdo = 1;
35
36
// this dimaslanjaka's localhost
37
if (in_array($_SERVER['HTTP_HOST'], ['dev.ns.webmanajemen.com', 'localhost']) || LOCAL) {
38
  $env = 'development';
39
  $debug_pdo = 3;
40
}
41
42
// set framework environtment
43
$router->environtment($env);
44
define('ENVIRONMENT', $router->get_env());
45
// force debug when development mode
46
if (ENVIRONMENT == 'development') {
47
  show_error();
48
}
49
// set PDO Debug
50
if (!defined('PDO_DEBUG')) {
51
  define('PDO_DEBUG', (string) $debug_pdo);
52
}
53
54
$config = \Filemanager\file::get(__DIR__ . '/config.json', true);
55
56
if (!CORS) {
57
  // extends cache key for cache revisioning
58
  // [cache][ext] for development mode to disable browser caching without damaging interface or other
59
  $config['cache']['key'] .= $config['cache']['ext'];
60
}
61
62
define('CONFIG', $config);
63
64
// ====== helper
65
66
$GLOBALS['config'] = $config;
67
/**
68
 * Get config as array from config.json.
69
 *
70
 * @return array
71
 */
72
function get_conf()
73
{
74
  if (!$GLOBALS['config']) {
75
    $GLOBALS['config'] = \Filemanager\file::get(ROOT . '/config.json', true);
76
  }
77
78
  return $GLOBALS['config'];
79
}
80
/**
81
 * Save array of config to config.json.
82
 *
83
 * @return void
84
 */
85
function save_conf(array $newdata)
86
{
87
  \Filemanager\file::file(__DIR__ . '/config.json', array_replace(get_conf(), $newdata), true);
0 ignored issues
show
Bug introduced by
array_replace(get_conf(), $newdata) of type array is incompatible with the type boolean expected by parameter $create of Filemanager\file::file(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
  \Filemanager\file::file(__DIR__ . '/config.json', /** @scrutinizer ignore-type */ array_replace(get_conf(), $newdata), true);
Loading history...
88
}
89
90
/**
91
 * Get config database.
92
 *
93
 * @return array
94
 */
95
function get_db()
96
{
97
  return $GLOBALS['config']['database'];
98
}
99
100
//======== begin conf [DO NOT EDIT]
101
102
// ignore limitation if exists
103
if (function_exists('set_time_limit')) {
104
  call_user_func('set_time_limit', 0);
105
}
106
107
// ignore user abort execution to false
108
if (function_exists('ignore_user_abort')) {
109
  call_user_func('ignore_user_abort', false);
110
}
111
112
// set output buffering to zero
113
ini_set('output_buffering', 0);
114
115
function useFB()
116
{
117
  include ROOT . '/config-fb.php';
118
}
119
120
function useTsel()
121
{
122
  if (!function_exists('telkomsel_api')) {
123
    include ROOT . '/config-tsel.php';
124
  }
125
}
126
127
function usem3()
128
{
129
  if (!function_exists('m3')) {
130
    include ROOT . '/config-m3.php';
131
  }
132
}
133
134
function useGoogle()
135
{
136
  if (!function_exists('google')) {
137
    include ROOT . '/config-google.php';
138
  }
139
}
140
141
//======== end conf
142
/**
143
 * @var \User\user
144
 */
145
$user = new \User\user(CONFIG['database']['user'], CONFIG['database']['pass'], CONFIG['database']['dbname'], CONFIG['database']['host']);
146
$pdo = $user->pdo_instance();
147
148
/**
149
 * user instance.
150
 *
151
 * @return \User\user
152
 */
153
function user()
154
{
155
  global $user;
156
157
  return $user;
158
}
159
160
$GLOBALS['office_instance'] = null;
161
/**
162
 * Office instance.
163
 *
164
 * @return \Office\loader
165
 */
166
function office()
167
{
168
  if (!$GLOBALS['office_instance']) {
169
    $GLOBALS['office_instance'] = new \Office\loader(pdo());
170
  }
171
172
  return $GLOBALS['office_instance'];
173
}
174
/**
175
 * user instance.
176
 *
177
 * @return \DB\pdo
178
 */
179
function pdo()
180
{
181
  global $pdo;
182
183
  return $pdo;
184
}
185
186
// file scanner
187
$scanner = new Filemanager\scan();
188
189
function scan($dir)
0 ignored issues
show
Unused Code introduced by
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 ignore-unused  annotation

189
function scan(/** @scrutinizer ignore-unused */ $dir)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
190
{
191
}
192
193
/**
194
 * Check if output buffering on.
195
 *
196
 * @return ob_get_level
0 ignored issues
show
Bug introduced by
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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
197
 */
198
function isob()
199
{
200
  return ob_get_level();
0 ignored issues
show
Bug Best Practice introduced by
The expression return ob_get_level() returns the type integer which is incompatible with the documented return type ob_get_level.
Loading history...
201
}
202
203
/**
204
 * Base URL router.
205
 *
206
 * @return void
207
 */
208
function base(string $path)
209
{
210
  return (isset($_SERVER['HTTPS']) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $path;
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode && 'on'...ER['HTTP_HOST'] . $path returns the type string which is incompatible with the documented return type void.
Loading history...
211
}
212
213
214
/**
215
 * Filemanager Instance.
216
 *
217
 * @return void
218
 */
219
function filemanager()
220
{
221
  return new \Filemanager\file();
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Filemanager\file() returns the type Filemanager\file which is incompatible with the documented return type void.
Loading history...
222
}
223
224
/**
225
 * Get current environtment
226
 *
227
 * @return string
228
 */
229
function get_env()
230
{
231
  global $router;
232
233
  return $router->get_env();
234
}
235
236
/**
237
 * Get current router instance
238
 *
239
 * @return \MVC\router
240
 */
241
function router()
242
{
243
  global $router;
244
  return $router;
245
}
246
247
function Map($arr, $callback)
248
{
249
  if (!is_callable($callback)) {
250
    throw new Exception('Callback must be function', 1);
251
  }
252
253
  return array_map(function ($key, $val) use ($callback) {
254
    return call_user_func($callback, $key, $val);
255
  }, array_keys($arr), $arr);
256
}
257
258
function strcond($first, $two, $success = null, $error = null)
259
{
260
  $src = $first;
261
  if (!file_exists($src)) {
262
    $src = $two;
263
  }
264
  if (file_exists($src)) {
265
    if (is_callable($success)) {
266
      return call_user_func($success, $src);
267
    } else {
268
      return $src;
269
    }
270
  } else {
271
    if (is_callable($error)) {
272
      return call_user_func($error, $src);
273
    }
274
  }
275
}
276
277
/**
278
 * echo print_r in pretext.
279
 *
280
 * @param mixed $str
281
 */
282
function printr($str, $str1 = 0, $str2 = 0)
283
{
284
  echo '<pre>';
285
  print_r($str);
286
  if ($str1) {
287
    print_r($str1);
288
  }
289
  if ($str2) {
290
    print_r($str2);
291
  }
292
  echo '</pre>';
293
}
294
295
/**
296
 * echo json_encode in pretext.
297
 */
298
function precom(...$str)
299
{
300
  $D = json_encode($str, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
301
  if (headers_sent()) {
302
    echo '<pre class="notranslate">';
303
    echo $D;
304
    echo '</pre>';
305
  } else {
306
    return $D;
307
  }
308
}
309
310
/**
311
 * cURL shooter request.
312
 *
313
 * @param array $opt
314
 *
315
 * @return array
316
 */
317
function req($opt)
318
{
319
  return \Extender\request::static_request($opt);
320
}
321
322
/**
323
 *  Limitation start.
324
 */
325
function getLimit(int $id_user = 0)
326
{
327
  global $user;
328
329
  if (!$user) {
330
    $user = new \User\user(CONFIG['database']['user'], CONFIG['database']['pass'], CONFIG['database']['dbname'], CONFIG['database']['host']);
331
  }
332
  if (0 == $id_user) {
333
    $id_user = $user->userdata('id');
334
  }
335
  $limit = [];
336
  if ($user->is_login()) {
337
    $limit = pdo()->select('limitation')->where([
338
      'user_id' => $id_user,
339
    ])->row_array();
340
    if (empty($limit)) {
341
      pdo()->insert_not_exists('limitation', [
342
        'user_id' => $id_user,
343
      ])->exec();
344
345
      return getLimit();
346
    }
347
  }
348
349
  return $limit;
350
}
351
352
function getLimitRemaining()
353
{
354
  $limit = (array) getLimit();
355
356
  if (isset($limit['max']) && isset($limit['success'])) {
357
    $max = (int) $limit['max'];
358
    $suc = (int) $limit['success'];
359
    $remaining = (int) ($max - $suc);
360
    //var_dump($max, $suc, ($max - $suc));
361
    return $remaining;
362
  }
363
364
  return 0;
365
}
366
367
function getLimitSuccess()
368
{
369
  $limit = (array) getLimit();
370
  if (isset($limit['success']) && isset($limit['success'])) {
371
    $max = (int) $limit['success'];
372
373
    return $max;
374
  }
375
376
  return 0;
377
}
378
379
function getLimitMax()
380
{
381
  $limit = (array) getLimit();
382
  if (isset($limit['max']) && isset($limit['success'])) {
383
    $max = (int) $limit['max'];
384
385
    return $max;
386
  }
387
388
  return 0;
389
}
390
391
function getLimitBanned()
392
{
393
  //var_dump(getLimitRemaining());
394
  if (user()->is_login()) {
395
    return getLimitRemaining() <= 0;
396
  }
397
}
398
399
function addLimitSuccess(int $id_user = 0)
400
{
401
  global $user;
402
  if (0 == $id_user) {
403
    $id_user = $user->userdata('id');
404
  }
405
406
  return pdo()->sum('limitation', [
407
    'success' => 1,
408
  ], [
409
    'user_id' => $id_user,
410
  ])->exec();
411
}
412
413
function addLimitMax(int $id_user = 0, int $value)
414
{
415
  global $user;
416
  if (0 == $id_user) {
417
    $id_user = $user->userdata('id');
418
  }
419
420
  return pdo()->update('limitation', [
421
    'max' => $value,
422
  ], [
423
    'user_id' => $id_user,
424
  ])->exec();
425
}
426