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