-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.php
More file actions
743 lines (630 loc) · 23.7 KB
/
Driver.php
File metadata and controls
743 lines (630 loc) · 23.7 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
<?php
/*------------------------------------------------------------------------------
MIT License
Copyright (c) 2023 Willian Donadelli
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------*/
/* para efetuar testes na biblioteca (desligar na produção) */
if (0) {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
/*----------------------------------------------------------------------------*/
class Driver {
/*----------------------------------------------------------------------------*/
/* -- DADOS -- */
private $VERSION = "v1.1.0"; /* registra versão da biblioteca */
private $CONFIG; /* registra os dados de configuração */
private $STATUS; /* regista o identificador do progresso */
private $PATH = false; /* regista se método path já foi executado (só pode uma vez) */
private $EVENTS = array( /* registra as fases da requisição */
0 => "SESSION STARTED",
1 => "AUTHENTICATION REQUIRED",
2 => "AUTHENTICATION FAILED",
3 => "SUCCESSFULLY AUTHENTICATED",
4 => "PERMITTED ACCESS",
5 => "ACCESS DENIED",
6 => "PAGE NOT FOUND",
7 => "SESSION CLOSED",
8 => "SESSION EXPIRED",
9 => "MODIFIED ROUTE"
);
private $STRUCT = array( /* registra restrições de CONFIG */
"CHECK" => array( /* informa se deseja checar os dados de configuração */
"REQUIRED" => false,
"TYPE" => "boolean"
),
"HOME" => array( /* informa a página principal */
"REQUIRED" => true,
"TYPE" => "file"
),
"ID" => array( /* informa os identificadores e suas páginas secundárias */
"REQUIRED" => true,
"TYPE" => "array",
"BADKEYS" => array("HOME", "EXIT"),
"TYPELIST" => "file"
),
"LOG" => array( /* informações sobre a autenticação, se for o caso */
"REQUIRED" => false,
"TYPE" => "array",
"KEYS" => array(
"GATEWAY" => array( /* página de autenticação e de checagem */
"REQUIRED" => true,
"TYPE" => "file"
),
"EXIT" => array( /* página de encerramento de sessão */
"REQUIRED" => true,
"TYPE" => "file"
),
"DATA" => array( /* nome dos formulários que receberão os dados de autenticação */
"REQUIRED" => true,
"TYPE" => "array",
"SIZE" => 1,
"TYPELIST" => "string"
),
"LOGIN" => array( /* função que fará a autenticação */
"REQUIRED" => true,
"TYPE" => "function"
),
"ALLOW" => array( /* função que fará a checagem a cada acesso */
"REQUIRED" => false,
"TYPE" => "function"
),
"LOAD" => array( /* função a ser chamada após definição da requisição */
"REQUIRED" => false,
"TYPE" => "function"
),
"TIME" => array( /* intervalos de tempo, em segundos, permitido entre requisições */
"REQUIRED" => false,
"TYPE" => "integer",
"SIZE" => 1
)
)
),
"COOKIES" => array( /* configuração de cookies, se for o caso */
"REQUIRED" => false,
"TYPE" => "array",
"KEYS" => array(
"HTTPONLY" => array( /* The cookie can only be accessed through the HTTP protocol (sem JS?) */
"REQUIRED" => false,
"TYPE" => "boolean"
),
"SECURE" => array( /* The cookie should only be sent over secure connections (só em HTTPS?) */
"REQUIRED" => false,
"TYPE" => "boolean"
),
"SAMESITE" => array( /* Controls the cross-domain sending of the cookie */
"REQUIRED" => false,
"TYPE" => "string",
"VALUES" => array("lax", "strict", "none")
),
"LIFETIME" => array( /* The lifetime of the cookie in seconds */
"REQUIRED" => false,
"TYPE" => "integer"
),
"PATH" => array( /* The path where information is stored */
"REQUIRED" => false,
"TYPE" => "string"
),
"DOMAIN" => array( /* The domain of the cookie */
"REQUIRED" => false,
"TYPE" => "string"
)
)
)
);
private $SESSION = array( /* Dados de sessão SESSION[__DRIVER__] */
"USER" => null, /* dados do usuário (fixo) [mixed] */
"TIME" => null, /* momento do login (fixo) [integer] */
"DATE" => null, /* momento do login (fixo) [string] */
"HASH" => null, /* identificador do login (fixo) [string] */
"LAST" => null, /* último histórico/LOG registrado (variável) [array] */
"LOG" => array() /* histórico de ações (variável) [array] */
);
/*----------------------------------------------------------------------------*/
public function __construct($config) {
/* construtor do objeto */
/* conferir dados de configuração */
$this->config($config);
/* configurar cookies */
$this->cookies();
/* iniciar sessão */
$this->start();
/* definir status inicial */
$this->STATUS = 0;
return;
}
/*----------------------------------------------------------------------------*/
private function config($input) {
/* obtem e define dados da configuração conforme argumento do construtor (array ou arquivo JSON) */
/* definir direto se for um array */
if (gettype($input) === "array") {
$config = $input;
}
/* se for um arquivo, obter dados */
elseif ($this->isFile($input)) {
/* 1) tentar ler o conteúdo do arquivo */
$content = file_get_contents($input);
if ($content === false) {
$this->error("CONFIG", "Error reading configuration file");
}
/* 2) tentar decodifica o JSON para array */
$array = json_decode($content, true);
if ($array === null) {
$this->error("CONFIG", "Error in JSON file structure");
}
/* 3) checar se o dado primário do JSON é um array */
if (gettype($array) !== "array") {
$this->error("CONFIG", "Inadequate configuration data");
}
/* 4) definir config */
$config = $array;
}
/* no caso de não ser array ou arquivo JSON, relatar erro */
else {
$this->error("CONFIG", "Inappropriate argument");
}
/* checar dados de CONFIG, se assim estiver definido */
if (!isset($config["CHECK"]) || $config["CHECK"] !== false) {
$this->check($config);
}
return;
}
/*----------------------------------------------------------------------------*/
private function check($input, $ref = null) {
/* checa ou define dados de configuração */
/* obter parâmetros da estrutura */
$struct = $ref === null ? $this->STRUCT : $this->STRUCT[$ref]["KEYS"];
/* looping pelas restrições */
foreach ($struct as $id => $check) {
/* obter valor de referência para a mensagem de erro */
$msg = $ref === null ? "CONFIG.{$id}" : "CONFIG.{$ref}.{$id}";
/* chave não definida: checar obrigatoriedade */
if (!isset($input[$id])) {
if ($check["REQUIRED"]) {
$this->error($msg, "Information not provided");
} else {
continue;
}
}
/* chave definida: checar parâmetros */
else {
/* checar o tipo */
if (!$this->matchType($check["TYPE"], $input[$id])) {
$this->error($msg, "Inappropriate information");
}
/* checar valor mínimo */
if (isset($check["SIZE"])) {
$size = $check["TYPE"] === "array" ? count($input[$id]) : $input[$id];
if ($size < $check["SIZE"]) {
$this->error($msg, "Insufficient data");
}
}
/* checar identificadores proibidos */
if (isset($check["BADKEYS"])) {
foreach($input[$id] as $key => $item) {
if (in_array($key, $check["BADKEYS"])) {
$this->error($msg, "Inappropriate identifier ($key)");
}
}
}
/* checar tipos da lista */
if (isset($check["TYPELIST"])) {
foreach($input[$id] as $key => $item) {
if (!$this->matchType($check["TYPELIST"], $item)) {
$this->error($msg, "Inappropriate information ({$key})");
}
}
}
/* checar valores possíveis */
if (isset($check["VALUES"])) {
if (!in_array($input[$id], $check["VALUES"], true)) {
$this->error($msg, "Inappropriate information ({$input[$id]})");
}
}
/* passou nos parâmetros: definir CONFIG e sua chave, se necessário */
if ($ref === null && !isset($this->CONFIG)) {
$this->CONFIG = array();
} elseif ($ref !== null && !isset($this->CONFIG[$ref])) {
$this->CONFIG[$ref] = array();
}
/* se não for uma chave mãe, definir o valor */
if (!isset($check["KEYS"])) {
if ($ref === null) {
$this->CONFIG[$id] = $input[$id];
} else {
$this->CONFIG[$ref][$id] = $input[$id];
}
} else { /* caso contrário: checar chave filha */
$this->check($input[$id], $id);
}
}
}
return;
}
/*----------------------------------------------------------------------------*/
private function cookies() {
/* configurar cookies */
/*-------------------------------------------------------------------------\
| https://www.php.net/manual/en/function.session-get-cookie-params.php
| https://www.php.net/manual/en/function.session-set-cookie-params.php
| https://www.php.net/manual/en/ini.list.php
| https://developer.mozilla.org/pt-BR/docs/Web/HTTP/Headers/Set-Cookie/SameSite
\-------------------------------------------------------------------------*/
$params = session_get_cookie_params();
$https = isset($_SERVER["HTTPS"]) && !empty($_SERVER["HTTPS"]) ? true : false;
/* valores padrão da biblioteca */
$params["httponly"] = true;
$params["secure"] = $https;
$params["samesite"] = "lax";
/* definindo os valores advindos da configuração */
if (isset($this->CONFIG["COOKIES"])) {
foreach ($this->CONFIG["COOKIES"] as $key => $value) {
$id = strtolower($key);
$params[$id] = $value;
}
}
/* definindo configuração */
session_set_cookie_params($params);
return;
}
/*----------------------------------------------------------------------------*/
private function start() {
/* inicia dados da sessão */
/* iniciar se não iniciada */
if (!isset($_SESSION)) {
session_start();
}
/* definir chave principal de sessão */
if (!isset($_SESSION["__DRIVER__"])) {
$_SESSION["__DRIVER__"] = array();
}
/* definir chaves secundárias de sessão */
foreach ($this->SESSION as $key => $value) {
if (!isset($_SESSION["__DRIVER__"][$key])) {
$_SESSION["__DRIVER__"][$key] = $value;
}
}
return;
}
/*----------------------------------------------------------------------------*/
private function time($format = false) {
/* retorna a hora no formato YYYY-MM-DD HH:MM:SS ou em segundos */
$time = new DateTime();
return $format === true ? $time->format("Y-m-d H:i:s") : (int) $time->format("U");
}
/*----------------------------------------------------------------------------*/
private function isFile($path) {
/* verifica se o argumento é um caminho de arquivo válido (boolean) */
if (gettype($path) !== "string") {return false;}
return is_file($path) ? true : false;
}
/*----------------------------------------------------------------------------*/
private function error($root, $msg) {
/* exibe uma mensagem de erro e para a interpretação */
header("Content-Type: text/html");
echo "<!DOCTYPE html>";
echo "<b>Driver Error:</b> {$msg} [<code>{$root}</code>].";
echo "<br/><br/>Stopped script.";
exit;
}
/*----------------------------------------------------------------------------*/
private function isFunction($name) {
/* verifica se a informação é uma função possível de chamar (boolean) */
if (gettype($name) !== "string") {return false;}
return is_callable($name) ? true : false;
}
/*----------------------------------------------------------------------------*/
private function matchType($type, $value) {
/* verifica se o valor confere com o tipo informado (boolean) */
switch($type) {
case "file":
if ($this->isFile($value)) {return true;}
case "function":
if($this->isFunction($value)) {return true;}
default:
if ($type === gettype($value)) {return true;}
}
return false;
}
/*----------------------------------------------------------------------------*/
private function hash() {
/* define o identificador da sessão a partir dos dados do usuário */
/* checando se precisa de autenticação e os valores de sessão */
if (!isset($this->CONFIG["LOG"])) {return null;}
if ($_SESSION["__DRIVER__"]["USER"] === null) {return null;}
if ($_SESSION["__DRIVER__"]["TIME"] === null) {return null;}
if ($_SESSION["__DRIVER__"]["DATE"] === null) {return null;}
/* definindo dados do hash */
$hash = array(
"USER" => $_SESSION["__DRIVER__"]["USER"],
"TIME" => $_SESSION["__DRIVER__"]["TIME"],
"DATE" => $_SESSION["__DRIVER__"]["DATE"],
"IP" => $_SERVER["REMOTE_ADDR"],
"NAV" => $_SERVER["HTTP_USER_AGENT"]
);
return md5(json_encode($hash));
}
/*----------------------------------------------------------------------------*/
private function log() {
/* informa se o usuário está autenticado (boolean) */
/* conferir e comparar hash com os dados da sessão */
$hash = $this->hash();
if ($hash === null) {return false;}
return $hash === $_SESSION["__DRIVER__"]["HASH"] ? true : false;
}
/*----------------------------------------------------------------------------*/
private function login() {
/* informa se o usuário está pedindo autenticação (boolean) */
/* se o método não for POST, não está solicitando autenticação */
if ($_SERVER["REQUEST_METHOD"] !== "POST") {return false;}
/* se não exigir autenticação, não tem o que verificar */
if (!isset($this->CONFIG["LOG"])) {return false;}
/* se já autenticado, não há como solicitar autenticação */
if ($this->log()) {return false;}
/* checar se a última página foi de login */
if ($this->lastRequest("PATH") !== $this->CONFIG["LOG"]["GATEWAY"]) {return false;}
/* checar se o arquivo executável é o mesmo */
if ($this->lastRequest("INDEX") !== $_SERVER["SCRIPT_NAME"]) {return false;}
/* checar se os dados do POST conferem com os da autenticação (LOG.DATA) */
if (count($_POST) !== count($this->CONFIG["LOG"]["DATA"])) {return false;}
foreach ($this->CONFIG["LOG"]["DATA"] as $value) { /* checar nomes dos formulários */
if (!isset($_POST[$value])) {return false;}
/* codificar dados para evitar script imbutido */
$_POST[$value] = stripslashes($_POST[$value]);
$_POST[$value] = htmlspecialchars($_POST[$value]);
}
return true;
}
/*----------------------------------------------------------------------------*/
private function logout() {
/* encerra a sessão */
if (isset($_SESSION)) {
session_destroy();
}
return;
}
/*----------------------------------------------------------------------------*/
private function register() {
/* registra a autenticação e devolve o resultado (boolean) */
/* chamando a função para checar credenciais e obter dados do usuário em caso de sucesso */
$user = call_user_func($this->CONFIG["LOG"]["LOGIN"], $_POST);
/* conferindo se a autenticação falhou */
if ($user === null) {return false;}
/* em caso de sucesso, definir dados da sessão */
$_SESSION["__DRIVER__"]["USER"] = $user;
$_SESSION["__DRIVER__"]["TIME"] = $this->time();
$_SESSION["__DRIVER__"]["DATE"] = $this->time(true);
$_SESSION["__DRIVER__"]["HASH"] = $this->hash();
return true;
}
/*----------------------------------------------------------------------------*/
private function id() {
/* retorna o valor do id */
return isset($_GET["id"]) ? $_GET["id"] : null;
}
/*----------------------------------------------------------------------------*/
private function lastRequest($id = null) {
/* retorna os dados da última interação */
$last = count($_SESSION["__DRIVER__"]["LOG"]) - 1;
if ($last < 0) {return null;}
$item = $_SESSION["__DRIVER__"]["LOG"][$last];
if ($id === null || !isset($item[$id])) {return $item;}
return $item[$id];
}
/*----------------------------------------------------------------------------*/
private function free() {
/* retorna a rota sem autenticação */
$id = $this->id();
/* rota indefinida, HOME ou EXIT: HOME */
if (in_array($id, array(null, "HOME", "EXIT"))) {
$this->STATUS = 4;
return $this->CONFIG["HOME"];
}
/* rota incorreta: HOME */
if (!isset($this->CONFIG["ID"][$id])) {
$this->STATUS = 6;
return $this->CONFIG["HOME"];
}
/* rota correta: rota */
$this->STATUS = 4;
return $this->CONFIG["ID"][$id];
}
/*----------------------------------------------------------------------------*/
private function restricted() {
/* define a rota com autenticação */
/* usuário tentando autenticar */
if ($this->login()) {
if ($this->register()) { /* sucesso: HOME */
$this->STATUS = 3;
return $this->CONFIG["HOME"];
} else { /* falha: GATEWAY */
$this->STATUS = 2;
return $this->CONFIG["LOG"]["GATEWAY"];
}
}
/* usuário não autenticado: GATEWAY */
if (!$this->log()) {
$this->STATUS = 1;
return $this->CONFIG["LOG"]["GATEWAY"];
}
/* usuário autenticado: obter id da rota */
$id = $this->id();
/* usuário encerando sessão: EXIT */
if ($id === "EXIT") {
$this->STATUS = 7;
return $this->CONFIG["LOG"]["EXIT"];
}
/* tempo da sessão expirado: EXIT */
$time = $this->CONFIG["LOG"]["TIME"];
$past = $this->lastRequest("TIME");
if ($time !== null && $past !== null) {
if ($this->time() > ($past + $time)) { /* prazo expirado: EXIT */
$this->STATUS = 8;
return $this->CONFIG["LOG"]["EXIT"];
}
}
/* rota não definida ou HOME: HOME */
if (in_array($id, array(null, "HOME"))) {
$this->STATUS = 4;
return $this->CONFIG["HOME"];
}
/* rota inexistente: HOME */
if (!isset($this->CONFIG["ID"][$id])) {
$this->STATUS = 6;
return $this->CONFIG["HOME"];
}
/* rota existente: obter caminho */
$path = $this->CONFIG["ID"][$id];
/* acesso não permitido: HOME */
$allow = $this->CONFIG["LOG"]["ALLOW"];
if ($allow !== null) {
$user = $_SESSION["__DRIVER__"]["USER"];
$check = call_user_func($allow, $user, $id, $path);
if ($check !== true) {
$this->STATUS = 5;
return $this->CONFIG["HOME"];
}
}
/* rota permitida: rota */
$this->STATUS = 4;
return $this->CONFIG["ID"][$id];
}
/*----------------------------------------------------------------------------*/
public function path() {
/* avalia e conduz a rota a ser exibida */
/* definir chamada de PATH (só poderá chamar uma vez) */
if ($this->PATH === true) {
$this->error("path()", "Method can only be called once.");
} else {
$this->PATH = true;
}
/* alterar o identificador da sessão */
session_regenerate_id(true);
/* obtendo resultado da requisição */
$path = isset($this->CONFIG["LOG"]) ? $this->restricted() : $this->free();
/* verificando se é o caso de limpar a sessão (STATUS sessão encerrada e sessão expirada) */
if (in_array($this->STATUS, array(7, 8))) {
$this->logout();
}
/* registrando histórico de navegação no log */
$this->history($path);
/* checar necessidade de desvio da página (exceto entradas e saídas) */
if (isset($this->CONFIG["LOG"]) && isset($this->CONFIG["LOG"]["LOAD"])) {
$load = call_user_func($this->CONFIG["LOG"]["LOAD"], $this->debug());
/* se o retorno foi um arquivo válido, retorná-lo para path */
if ($this->isFile($load)) {
$this->STATUS = 9;
$this->history($load);
return $load;
}
/* se for HOME */
if ($load === "HOME") {
$this->STATUS = 9;
$this->history($this->CONFIG["HOME"]);
return $this->CONFIG["HOME"];
}
/* se for EXIT */
if ($load === "EXIT") {
$this->STATUS = 9;
$this->logout();
$this->history($this->CONFIG["LOG"]["EXIT"]);
return $this->CONFIG["LOG"]["EXIT"];
}
/* se for um identificador, retornar o arquivo correspondete como path */
if (gettype($load) === "string" && isset($this->CONFIG["ID"][$load])) {
$this->STATUS = 9;
$this->history($this->CONFIG["ID"][$load]);
return $this->CONFIG["ID"][$load];
}
}
/* retornar rota */
return $path;
}
/*----------------------------------------------------------------------------*/
private function history($path) {
/* registra o histórico de navegação */
$id = $this->id();
$ID = $this->CONFIG["ID"];
$page = ($id === null || !isset($ID[$id])) ? null : $ID[$id];
$index = $_SERVER["SCRIPT_NAME"];
$log = isset($this->CONFIG["LOG"]) ? true : false;
$items = count($_SESSION["__DRIVER__"]["LOG"]);
$_SESSION["__DRIVER__"]["LAST"] = array(
"ITEM" => $items, /* informa o sequencial do registro de log */
"INFO" => $this->status(true), /* informação do estatus */
"ID" => $id, /* identificador informado */
"PAGE" => $page, /* página desejada */
"PATH" => $path, /* página definida */
"INDEX" => $index, /* módulo do sistema utilizado */
"LOG" => $log, /* navegação exige senha */
"LOGIN" => $this->log(), /* acesso com usuário logado */
"STATUS" => $this->status(), /* número do status */
"TIME" => $this->time(), /* registro, em segundos, do momento da ação */
"DATE" => $this->time(true), /* data e hora da ação em formato ISO */
);
$_SESSION["__DRIVER__"]["LOG"][] = $_SESSION["__DRIVER__"]["LAST"];
return;
}
/*----------------------------------------------------------------------------*/
public function status($text = false) {
/* retorna o valor do progresso da requisição */
return $text === true ? $this->EVENTS[$this->STATUS] : $this->STATUS;
}
/*----------------------------------------------------------------------------*/
public function version() {
/* retorna a versão da biblioteca */
return $this->VERSION;
}
/*----------------------------------------------------------------------------*/
public function debug($print = false) {
/* retorna ou imprime os dados de sessão */
if ($print === true) {
echo "<hr/>\n<pre style=\"color: #FFFFFF; background-color: #000000; white-space: pre-wrap\">";
print_r($_SESSION["__DRIVER__"]);
echo "</pre>\n<hr/>";
}
return $_SESSION["__DRIVER__"];
}
/*----------------------------------------------------------------------------*/
public function json($print = false) {
/* retorna ou imprime dados de configuração em formato JSON */
/* decodificando para JSON */
$json = json_encode($this->CONFIG);
if ($json === false) {
$this->error("json()", "Error encoding data to JSON");
return null;
}
/* fazendo umas substituições para melhor visualização */
$json = trim($json);
$json = stripslashes($json);
$json = preg_replace("/(\"[a-z0-9-_]+\"\:)/mi", "\n\t\t$0 ", $json);
$json = preg_replace("/\t\"(CHECK|HOME|ID|LOG|COOKIES)\"/", "\"$1\"", $json);
$json = str_replace("\",\"", "\", \"", $json);
$json = str_replace("},", "\n\t},", $json);
$json = str_replace("}}", "\n\t}\n}", $json);
/* imprimindo, se for o caso, e retornando */
if ($print === true) {
echo "<hr/>\n<pre style=\"color: #FFFFFF; background-color: #000000; white-space: pre-wrap\">";
print_r($json);
echo "</pre>\n<hr/>";
}
return $json;
}
}
?>