Passed
Pull Request — master (#36)
by
unknown
07:45
created

get_db()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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']) || 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
226
/**
227
 * Get current environtment
228
 *
229
 * @return string
230
 */
231
function get_env()
232
{
233
  global $router;
234
235
  return $router->get_env();
236
}
237
238
/**
239
 * Get current router instance
240
 *
241
 * @return \MVC\router
242
 */
243
function router()
244
{
245
  global $router;
246
  return $router;
247
}
248
249
function Map($arr, $callback)
250
{
251
  if (!is_callable($callback)) {
252
    throw new Exception('Callback must be function', 1);
253
  }
254
255
  return array_map(function ($key, $val) use ($callback) {
256
    return call_user_func($callback, $key, $val);
257
  }, array_keys($arr), $arr);
258
}
259
260
function strcond($first, $two, $success = null, $error = null)
261
{
262
  $src = $first;
263
  if (!file_exists($src)) {
264
    $src = $two;
265
  }
266
  if (file_exists($src)) {
267
    if (is_callable($success)) {
268
      return call_user_func($success, $src);
269
    } else {
270
      return $src;
271
    }
272
  } else {
273
    if (is_callable($error)) {
274
      return call_user_func($error, $src);
275
    }
276
  }
277
}
278
279
/**
280
 * echo print_r in pretext.
281
 *
282
 * @param mixed $str
283
 */
284
function printr($str, $str1 = 0, $str2 = 0)
285
{
286
  echo '<pre>';
287
  print_r($str);
288
  if ($str1) {
289
    print_r($str1);
290
  }
291
  if ($str2) {
292
    print_r($str2);
293
  }
294
  echo '</pre>';
295
}
296
297
/**
298
 * echo json_encode in pretext.
299
 */
300
function precom(...$str)
301
{
302
  $D = json_encode($str, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
303
  if (headers_sent()) {
304
    echo '<pre class="notranslate">';
305
    echo $D;
306
    echo '</pre>';
307
  } else {
308
    return $D;
309
  }
310
}
311
312
/**
313
 * cURL shooter request.
314
 *
315
 * @param array $opt
316
 *
317
 * @return array
318
 */
319
function req($opt)
320
{
321
  return \Extender\request::static_request($opt);
322
}
323
324
/**
325
 *  Limitation start.
326
 */
327
function getLimit(int $id_user = 0)
328
{
329
  global $user;
330
331
  if (!$user) {
332
    $user = new \User\user(CONFIG['database']['user'], CONFIG['database']['pass'], CONFIG['database']['dbname'], CONFIG['database']['host']);
333
  }
334
  if (0 == $id_user) {
335
    $id_user = $user->userdata('id');
336
  }
337
  $limit = [];
338
  if ($user->is_login()) {
339
    $limit = pdo()->select('limitation')->where([
340
      'user_id' => $id_user,
341
    ])->row_array();
342
    if (empty($limit)) {
343
      pdo()->insert_not_exists('limitation', [
344
        'user_id' => $id_user,
345
      ])->exec();
346
347
      return getLimit();
348
    }
349
  }
350
351
  return $limit;
352
}
353
354
function getLimitRemaining()
355
{
356
  $limit = (array) getLimit();
357
358
  if (isset($limit['max']) && isset($limit['success'])) {
359
    $max = (int) $limit['max'];
360
    $suc = (int) $limit['success'];
361
    $remaining = (int) ($max - $suc);
362
    //var_dump($max, $suc, ($max - $suc));
363
    return $remaining;
364
  }
365
366
  return 0;
367
}
368
369
function getLimitSuccess()
370
{
371
  $limit = (array) getLimit();
372
  if (isset($limit['success']) && isset($limit['success'])) {
373
    $max = (int) $limit['success'];
374
375
    return $max;
376
  }
377
378
  return 0;
379
}
380
381
function getLimitMax()
382
{
383
  $limit = (array) getLimit();
384
  if (isset($limit['max']) && isset($limit['success'])) {
385
    $max = (int) $limit['max'];
386
387
    return $max;
388
  }
389
390
  return 0;
391
}
392
393
function getLimitBanned()
394
{
395
  //var_dump(getLimitRemaining());
396
  if (user()->is_login()) {
397
    return getLimitRemaining() <= 0;
398
  }
399
}
400
401
function addLimitSuccess(int $id_user = 0)
402
{
403
  global $user;
404
  if (0 == $id_user) {
405
    $id_user = $user->userdata('id');
406
  }
407
408
  return pdo()->sum('limitation', [
409
    'success' => 1,
410
  ], [
411
    'user_id' => $id_user,
412
  ])->exec();
413
}
414
415
function addLimitMax(int $id_user = 0, int $value)
416
{
417
  global $user;
418
  if (0 == $id_user) {
419
    $id_user = $user->userdata('id');
420
  }
421
422
  return pdo()->update('limitation', [
423
    'max' => $value,
424
  ], [
425
    'user_id' => $id_user,
426
  ])->exec();
427
}
428