-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
eq.lib.php
1483 lines (1378 loc) · 67.8 KB
/
eq.lib.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
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This file is part of the eQual framework.
* https://github.com/equalframework/equal
*
* Some Rights Reserved, The eQual Framework, 2010-2024
* Original Author: Cedric Francoys
* License: GNU LGPL 3 license <http://www.gnu.org/licenses/>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace {
global $last_context;
global $constants_schema;
define('__EQ_LIB', true) or die('fatal error: __EQ_LIB already defined or cannot be defined');
/**
* All constants required by the core are prefixed with EQ_
* (in addition, user might define its own constants following his own formatting rules)
*/
/**
* Current version of eQual
*/
define('EQ_VERSION', '2.0.0');
/**
* Root directory of current install
*/
define('EQ_BASEDIR', realpath(dirname(__FILE__)));
// equivalence for constant names migration
// #deprecated
define('QN_BASEDIR', EQ_BASEDIR);
/**
* Error codes
* we use negative values to make it possible to distinguish error codes from object ids
*/
define('EQ_ERROR_UNKNOWN', -1); // something went wrong (check the logs)
define('EQ_ERROR_MISSING_PARAM', -2); // one or more mandatory parameters are missing
define('EQ_ERROR_INVALID_PARAM', -4); // one or more parameters have invalid or incompatible value
define('EQ_ERROR_SQL', -8); // error while building SQL query or processing it (check that object class matches DB schema)
define('EQ_ERROR_UNKNOWN_OBJECT', -16); // unknown resource (class, object, view, ...)
define('EQ_ERROR_NOT_ALLOWED', -32); // action violates some rule (including UPLOAD_MAX_FILE_SIZE for binary fields) or user don't have required permissions
define('EQ_ERROR_LOCKED_OBJECT', -64); // object is currently locked by another process
define('EQ_ERROR_CONFLICT_OBJECT', -128); // version conflict
define('EQ_ERROR_INVALID_USER', -256); // auth failure
define('EQ_ERROR_UNKNOWN_SERVICE', -512); // server error : missing service
define('EQ_ERROR_INVALID_CONFIG', -1024); // server error : faulty configuration
// equivalence map for constant names migration
// #deprecated
define('QN_ERROR_UNKNOWN', EQ_ERROR_UNKNOWN);
define('QN_ERROR_MISSING_PARAM', EQ_ERROR_MISSING_PARAM);
define('QN_ERROR_INVALID_PARAM', EQ_ERROR_INVALID_PARAM);
define('QN_ERROR_SQL', EQ_ERROR_SQL);
define('QN_ERROR_UNKNOWN_OBJECT', EQ_ERROR_UNKNOWN_OBJECT);
define('QN_ERROR_NOT_ALLOWED', EQ_ERROR_NOT_ALLOWED);
define('QN_ERROR_LOCKED_OBJECT', EQ_ERROR_LOCKED_OBJECT);
define('QN_ERROR_CONFLICT_OBJECT', EQ_ERROR_CONFLICT_OBJECT);
define('QN_ERROR_INVALID_USER', EQ_ERROR_INVALID_USER);
define('QN_ERROR_UNKNOWN_SERVICE', EQ_ERROR_UNKNOWN_SERVICE);
define('QN_ERROR_INVALID_CONFIG', EQ_ERROR_INVALID_CONFIG);
/**
* Debugging modes and levels
*/
// debugging modes
define('EQ_MODE_PHP', 1); // low-level logs (code)
define('EQ_MODE_SQL', 2); // DB related logs
define('EQ_MODE_ORM', 4); // ORM entities & manipulations logs
define('EQ_MODE_API', 8); // routes & controllers related logs
define('EQ_MODE_APP', 16); // application logic logs
define('EQ_MODE_AAA', 32); // authentication, authorization & accounting logs
define('EQ_MODE_NET', 64); // network logs (tcp/ip, http)
// equivalence map for constant names migration
// #deprecated
define('QN_MODE_PHP', EQ_MODE_PHP);
define('QN_MODE_SQL', EQ_MODE_SQL);
define('QN_MODE_ORM', EQ_MODE_ORM);
define('QN_MODE_API', EQ_MODE_API);
define('QN_MODE_APP', EQ_MODE_APP);
define('QN_MODE_AAA', EQ_MODE_AAA);
define('QN_MODE_NET', EQ_MODE_NET);
// debugging levels
define('EQ_REPORT_DEBUG', E_USER_DEPRECATED); // 16384
define('EQ_REPORT_INFO', E_USER_NOTICE); // 1024
define('EQ_REPORT_WARNING', E_USER_WARNING); // 512
define('EQ_REPORT_ERROR', E_USER_ERROR); // 256
define('EQ_REPORT_FATAL', E_ERROR); // 1
define('EQ_REPORT_SYSTEM', 0); // 0
// equivalence map for constant names migration
// #deprecated
define('QN_REPORT_DEBUG', EQ_REPORT_DEBUG);
define('QN_REPORT_INFO', EQ_REPORT_INFO);
define('QN_REPORT_WARNING', EQ_REPORT_WARNING);
define('QN_REPORT_ERROR', EQ_REPORT_ERROR);
define('QN_REPORT_FATAL', EQ_REPORT_FATAL);
define('QN_REPORT_SYSTEM', EQ_REPORT_SYSTEM);
/**
* Logs storage directory
*
* Note: ensure http service has read/write permissions on this directory
*/
define('QN_LOG_STORAGE_DIR', EQ_BASEDIR.'/log');
// EventHandler will deal with error and debug messages depending on debug source value
ini_set('html_errors', false); // prevent HTML in logs
ini_set('error_log', QN_LOG_STORAGE_DIR.'/error.log'); // force PHP logs output to specific file
/**
* Users & Groups permissions masks
*/
define('EQ_R_CREATE', 1);
define('EQ_R_READ', 2);
define('EQ_R_UPDATE', 4);
define('EQ_R_DELETE', 8);
define('EQ_R_MANAGE', 16);
define('EQ_R_ALL', 31);
// equivalence map for constant names migration
// #deprecated
define('EQ_R_WRITE', EQ_R_UPDATE);
define('QN_R_CREATE', EQ_R_CREATE);
define('QN_R_READ', EQ_R_READ);
define('QN_R_WRITE', EQ_R_WRITE);
define('QN_R_DELETE', EQ_R_DELETE);
define('QN_R_MANAGE', EQ_R_MANAGE);
define('QN_R_ALL', EQ_R_ALL);
/**
* Built-in Users and Groups
*
* Note : make sure that the ids in DB are set and matching these
*/
define('EQ_GUEST_USER_ID', 0);
define('EQ_ROOT_USER_ID', 1);
// equivalence map for constant names migration
// #deprecated
define('QN_GUEST_USER_ID', EQ_GUEST_USER_ID);
define('QN_ROOT_USER_ID', EQ_ROOT_USER_ID);
// default group (all users are members of the default group)
define('EQ_ROOT_GROUP_ID', 1);
define('EQ_DEFAULT_GROUP_ID', 2);
// equivalence map for constant names migration
// #deprecated
define('QN_ROOT_GROUP_ID', EQ_ROOT_GROUP_ID);
define('QN_DEFAULT_GROUP_ID', EQ_DEFAULT_GROUP_ID);
/*
Error-utility functions (global namespace)
*/
/**
* Mapper from internal error codes to string constants
*/
function qn_error_name($error_id) {
switch($error_id) {
case QN_ERROR_MISSING_PARAM: return 'MISSING_PARAM';
case QN_ERROR_INVALID_PARAM: return 'INVALID_PARAM';
case QN_ERROR_SQL: return 'SQL_ERROR';
case QN_ERROR_NOT_ALLOWED: return 'NOT_ALLOWED';
case QN_ERROR_UNKNOWN_OBJECT: return 'UNKNOWN_OBJECT';
case QN_ERROR_INVALID_CONFIG: return 'INVALID_CONFIG';
case QN_ERROR_UNKNOWN_SERVICE: return 'UNKNOWN_SERVICE';
case QN_ERROR_LOCKED_OBJECT: return 'LOCKED_OBJECT';
case QN_ERROR_CONFLICT_OBJECT: return 'CONFLICT_OBJECT';
case QN_ERROR_INVALID_USER: return 'INVALID_CREDENTIALS';
}
return 'UNKNOWN_ERROR';
}
/**
* Mapper from string constants to internal error codes
*/
function qn_error_code($error_name) {
switch($error_name) {
case 'MISSING_PARAM': return QN_ERROR_MISSING_PARAM;
case 'INVALID_PARAM': return QN_ERROR_INVALID_PARAM;
case 'SQL_ERROR': return QN_ERROR_SQL;
case 'NOT_ALLOWED': return QN_ERROR_NOT_ALLOWED;
case 'UNKNOWN_OBJECT': return QN_ERROR_UNKNOWN_OBJECT;
case 'INVALID_CONFIG': return QN_ERROR_INVALID_CONFIG;
case 'UNKNOWN_SERVICE': return QN_ERROR_UNKNOWN_SERVICE;
case 'LOCKED_OBJECT': return QN_ERROR_LOCKED_OBJECT;
case 'CONFLICT_OBJECT': return QN_ERROR_CONFLICT_OBJECT;
case 'INVALID_CREDENTIALS': return QN_ERROR_INVALID_USER;
}
return QN_ERROR_UNKNOWN;
}
/**
* Mapper from internal error codes to HTTP codes
*/
function qn_error_http($error_id) {
switch($error_id) {
case 0: return 200;
case QN_ERROR_MISSING_PARAM: return 400; // 'Bad Request' missing data or invalid format for mandatory parameter
case QN_ERROR_INVALID_PARAM: return 400;
case QN_ERROR_SQL: return 456; // 'Unrecoverable Error' an unhandled scenario append and operation could not be performed
case QN_ERROR_NOT_ALLOWED: return 403; // 'Forbidden' user has not enough privilege to perform requested operation (includes method not allowed-
case QN_ERROR_UNKNOWN_OBJECT: return 404; // 'Not Found' object or route does not exist
case QN_ERROR_LOCKED_OBJECT: return 423; // 'Locked' resource is currently locked
case QN_ERROR_CONFLICT_OBJECT: return 409; // 'Conflict' version conflict or broken 'unique' rule
case QN_ERROR_INVALID_USER: return 401; // 'Unauthorized' auth required or auth failure
// server errors
case QN_ERROR_UNKNOWN:
case QN_ERROR_INVALID_CONFIG: return 500;
case QN_ERROR_UNKNOWN_SERVICE: return 503;
}
// fallback to 'Internal Server Error': something went wrong (details should be available in the log)
return 500;
}
function qn_debug_code_name($code) {
switch($code) {
case QN_REPORT_DEBUG: return 'DEBUG';
case QN_REPORT_INFO: return 'INFO';
case QN_REPORT_WARNING: return 'WARNING';
case QN_REPORT_ERROR: return 'ERROR';
case QN_REPORT_FATAL: return 'FATAL';
case QN_REPORT_SYSTEM: return 'SYSTEM';
}
return 'UNKNOWN';
}
function qn_debug_mode_name($mode) {
switch($mode) {
case QN_MODE_PHP: return 'PHP';
case QN_MODE_SQL: return 'SQL';
case QN_MODE_ORM: return 'ORM';
case QN_MODE_API: return 'API';
case QN_MODE_APP: return 'APP';
case QN_MODE_AAA: return 'AAA';
case QN_MODE_NET: return 'NET';
}
return 'UNKNOWN';
}
/**
* Add global system-related constants
* (which cannot be modified by other scripts)
*/
$constants_schema = [];
if(!file_exists(EQ_BASEDIR.'/config/schema.json')) {
die('Missing mandatory config schema.');
}
else {
$data = file_get_contents(EQ_BASEDIR.'/config/schema.json');
if(!($constants_schema = json_decode($data, true))) {
die('Invalid config schema.');
}
// pass-1 - process properties defined in config file
if(file_exists(EQ_BASEDIR.'/config/config.json')) {
$data = file_get_contents(EQ_BASEDIR.'/config/config.json');
if(($config = json_decode($data, true))) {
foreach($config as $property => $value) {
config\define($property, $value);
}
}
else {
die('Invalid config file.');
}
}
// pass-2 - process instant properties not present in config
foreach($constants_schema as $property => $descriptor) {
if(isset($descriptor['instant']) && $descriptor['instant']) {
if(!config\defined($property)) {
$value = null;
if(isset($descriptor['environment'])) {
if(($env = getenv($descriptor['environment'])) !== false) {
$value = $env;
}
}
// #todo - null value should be accepted as assignment in config.json
if(is_null($value) && isset($descriptor['default'])) {
$value = $descriptor['default'];
}
config\define($property, $value);
}
config\export($property);
}
elseif(!config\defined($property) && isset($descriptor['default'])) {
config\define($property, $descriptor['default']);
}
}
}
}
namespace config {
use equal\services\Container;
use equal\error\Reporter;
use equal\orm\Field;
/*
* This section adds some config-utility functions to the 'config' namespace.
*/
function encrypt($string) {
$output = false;
if(\defined('CIPHER_KEY')) {
$cipher_algo = "AES-256-CBC";
$secret_key = \constant('CIPHER_KEY');
$key = hash('sha256', $secret_key);
$iv = substr(implode('', range(0, 12)), 0, openssl_cipher_iv_length($cipher_algo));
$output = openssl_encrypt($string, $cipher_algo, $key, 0, $iv);
$output = base64_encode($output);
}
return $output;
}
function decrypt($string) {
$output = false;
if(\defined('CIPHER_KEY')) {
$cipher_algo = "AES-256-CBC";
$secret_key = \constant('CIPHER_KEY');
$key = hash('sha256', $secret_key);
$iv = substr(implode('', range(0, 12)), 0, openssl_cipher_iv_length($cipher_algo));
$output = openssl_decrypt(base64_decode($string), $cipher_algo, $key, 0, $iv);
}
return $output;
}
function strtoint($value, $usage = '') {
if(is_string($value)) {
if($value == 'null') {
$value = null;
}
elseif(empty($value)) {
$value = 0;
}
elseif(in_array($value, ['TRUE', 'true'])) {
$value = 1;
}
elseif(in_array($value, ['FALSE', 'false'])) {
$value = 0;
}
}
// arg represents a numeric value (numeric type or string)
if(is_numeric($value)) {
$value = intval($value);
}
elseif(is_scalar($value)) {
// fallback suffixes coefficients (defaults)
$suffixes = [
'B' => 1,
'KB' => 1024,
'MB' => 1048576,
'GB' => 1073741824,
's' => 1,
'm' => 60,
'h' => 3600,
'd' => 3600*24,
'w' => 3600*24*7,
'M' => 3600*24*30,
'Y' => 3600*24*365
];
// #todo - replicate this in DataAdapterJsonInteger
switch($usage) {
case 'amount/data':
$suffixes = [
'b' => 1,
'B' => 1,
'k' => 1000,
'K' => 1000,
'kb' => 1000,
'KB' => 1000,
'kib' => 1024,
'KiB' => 1024,
'm' => 1000000,
'M' => 1000000,
'mb' => 1000000,
'MB' => 1000000,
'mib' => 1048576,
'MiB' => 1048576,
'g' => 1000000000,
'gb' => 1000000000,
'gib' => 1073741824,
'GiB' => 1073741824
];
break;
case 'time/duration':
$suffixes = [
'ms' => 0.001,
's' => 1,
'm' => 60,
'h' => 3600,
'd' => 3600*24,
'D' => 3600*24,
'w' => 3600*24*7,
'M' => 3600*24*30,
'y' => 3600*24*365,
'Y' => 3600*24*365
];
break;
}
$val = (string) $value;
$intval = intval($val);
foreach($suffixes as $suffix => $factor) {
if(strval($intval).$suffix == $val) {
$value = $intval * $factor;
break;
}
}
}
return $value;
}
/**
* Adds a parameter to the configuration array
*/
function define($name, $value) {
if(!isset($GLOBALS['QN_CONFIG_ARRAY'])) {
$GLOBALS['QN_CONFIG_ARRAY'] = [];
}
$GLOBALS['QN_CONFIG_ARRAY'][$name] = $value;
}
/**
* Checks if a parameter has already been defined
*/
function defined($name) {
return \defined($name) || isset($GLOBALS['QN_CONFIG_ARRAY'][$name]);
}
/**
* Retrieve a configuration parameter as a constant.
*/
function constant($name, $default=null) {
return (isset($GLOBALS['QN_CONFIG_ARRAY'][$name]))?$GLOBALS['QN_CONFIG_ARRAY'][$name]:$default;
}
/**
* Register a service by assigning an identifier (name) to a class (stored under `/lib`).
*
* This method can be invoked in local config files to register a custom service and/or to override any existing service,
* and uses the `EQ_SERVICES_POOL` global array which is used by the root container.
*
*/
function register($name, $class=null) {
if(!isset($GLOBALS['EQ_SERVICES_POOL'])) {
$GLOBALS['EQ_SERVICES_POOL'] = [];
}
if(is_array($name)) {
foreach($name as $service => $class) {
$GLOBALS['EQ_SERVICES_POOL'][$service] = $class;
}
}
else {
$GLOBALS['EQ_SERVICES_POOL'][$name] = $class;
}
}
/**
* Retrieve a configuration parameter.
* @deprecated
*/
function config($name, $default=null) {
return (isset($GLOBALS['QN_CONFIG_ARRAY'][$name]))?$GLOBALS['QN_CONFIG_ARRAY'][$name]:$default;
}
/**
* Exports a property as constant to the global scope.
*/
function export($property) {
global $constants_schema;
// retrieve current value
$value = constant($property);
// handle shorthand notations
if(isset($constants_schema[$property]) && $constants_schema[$property]['type'] == 'integer') {
// handle binary masks on arbitrary values or pre-defined constants
if( is_string($value) && (strpos($value, '|') !== false || strpos($value, '&') !== false) ) {
try {
$value = eval("return $value;");
}
catch(\Exception $e) {
// ignore parse errors
}
}
else {
$value = strtoint($value, $constants_schema[$property]['usage'] ?? '');
}
}
// handle encrypted values
elseif(is_string($value) && substr($value, 0, 7) == 'cipher:') {
$value = decrypt(substr($value, 7));
}
if(!\defined($property)) {
\define($property, $value);
}
else {
// property $property already declared
}
}
/**
* Export all parameters declared with `config\define()` function, as constants.
*
* After a call to this method, these params will be accessible in global scope.
* @deprecated
*/
function export_config() {
if(isset($GLOBALS['QN_CONFIG_ARRAY'])) {
foreach($GLOBALS['QN_CONFIG_ARRAY'] as $name => $value) {
if(!\defined($name)) {
// handle encrypted values
if(is_string($value) && substr($value, 0, 7) == 'cipher:') {
$value = decrypt(substr($value, 7));
}
\define($name, $value);
}
unset($GLOBALS['QN_CONFIG_ARRAY'][$name]);
}
}
$GLOBALS['QN_CONFIG_EXPORTED'] = true;
}
/** @var \equal\php\Context */
$last_context = null;
class eQual {
/**
* Initialize eQual.
*
* Adds the library folder to the include path (library folder should contain the Zend framework if required).
* This is the bootstrap method for setting everything in place.
*
* @static
*/
public static function init() {
chdir(EQ_BASEDIR.'/');
// enable inclusion and autoload of external classes
if(file_exists(EQ_BASEDIR.'/vendor/autoload.php')) {
include_once(EQ_BASEDIR.'/vendor/autoload.php');
}
// register own class loader
spl_autoload_register(__NAMESPACE__.'\eQual::load_class');
// check service container availability
if(!is_callable('equal\services\Container::getInstance')) {
throw new \Exception('eQual::init - Mandatory Container service is missing or cannot be instantiated.', QN_REPORT_FATAL);
}
// instantiate service container
$container = Container::getInstance();
// register names for common services and assign default classes
// (these can be overridden in the `config.json` of invoked package)
$container->register([
'report' => 'equal\error\Reporter',
'auth' => 'equal\auth\AuthenticationManager',
'access' => 'equal\access\AccessController',
'context' => 'equal\php\Context',
'validate' => 'equal\data\DataValidator',
'adapt' => 'equal\data\adapt\DataAdapterProvider',
'orm' => 'equal\orm\ObjectManager',
'route' => 'equal\route\Router',
'log' => 'equal\log\Logger',
'cron' => 'equal\cron\Scheduler',
'dispatch' => 'equal\dispatch\Dispatcher',
'db' => 'equal\db\DBConnector'
]);
try {
// make mandatory dependencies available
$container->get(['report', 'context']);
// register ORM classes auto-loader
$om = $container->get('orm');
// init collections provider
$container->get('equal\orm\Collections');
spl_autoload_register([$om, 'getModel']);
}
catch(\Throwable $e) {
// fallback to a manual HTTP 500
header("HTTP/1.1 503 Service Unavailable");
header('Content-type: application/json; charset=UTF-8');
echo json_encode([
'errors' => ['UNKNOWN_SERVICE' => $e->getMessage()]
],
JSON_PRETTY_PRINT);
// and raise an exception (will be output in PHP error log)
throw new \Exception("missing_mandatory_dependency", QN_REPORT_FATAL);
}
}
public static function getLastContext() {
global $last_context;
return $last_context;
}
public static function inject(array $providers) {
$result = [];
// retrieve service container
$container = Container::getInstance();
// retrieve providers
foreach($providers as $name) {
$result[$name] = $container->get($name);
}
return $result;
}
/**
* Announce the definition of an operation.
*
* Retrieve, adapt and validate expected parameters from the HTTP request and provide requested dependencies.
* Also ensures that required parameters have been transmitted and sets default values for missing optional params.
*
* Accepted types for parameters types are PHP basic types: int, bool, float, string, array
* Note: un-announced parameters in HTTP request are ignored (dropped)
*
* @static
* @param array $announcement Array holding the description of the script and its parameters.
* @return array parameters and their final values
* @throws Exception raises an exception if: a dependency failed to load, OR a mandatory param is missing OR a param has invalid value
*/
public static function announce(array $announcement) {
// 0) init vars
$result = [];
$mandatory_params = [];
// retrieve service container
$container = Container::getInstance();
// retrieve required services
/**
* @var \equal\php\Context $context
* @var \equal\auth\AuthenticationManager $auth
* @var \equal\error\Reporter $reporter
*/
list($context, $auth, $reporter) = $container->get(['context', 'auth', 'report']);
// fetch body and method from HTTP request
$request = $context->httpRequest();
$body = (array) $request->body();
$method = $request->method();
$response = $context->httpResponse();
// set Response default Content Type to JSON
$response->headers()->setContentType('application/json');
// normalize $announcement array
if(!isset($announcement['params'])) {
$announcement['params'] = [];
}
$operation = $context->get('operation');
// handle controller inheritance
if(isset($announcement['extends'])) {
// retrieve params from parent controller
try {
$data = \eQual::run($operation['type'], $announcement['extends'], ['announce' => true], false);
if(!is_null($data) && isset($data['announcement']['params'])) {
$announcement['params'] = array_merge($data['announcement']['params'], $announcement['params']);
}
}
catch(\Exception $e) {
// ignore errors (non existing controller ?)
}
}
// check response headers
if(isset($announcement['response'])) {
if(isset($announcement['response']['location']) && !isset($body['announce'])) {
header('Location: '.$announcement['response']['location']);
exit;
}
if(isset($announcement['response']['content-type'])) {
$response->headers()->setContentType($announcement['response']['content-type']);
$header_accept = $request->getHeader('Accept');
// #todo - this is not the right place for that check
// the original request might not be relevant for the current context (cascade run() calls)
if($header_accept) {
$accepted = [];
$parts = explode(',', $header_accept);
foreach($parts as $part) {
$items = explode(';', $part);
$accepted[] = trim(reset($items));
}
// #todo - when should we implement strict check on accepted content type ?
if(!in_array($announcement['response']['content-type'], $accepted) && !in_array('*/*', $accepted)) {
/*
// send "406 Not Acceptable"
$response
->status(406)
->send();
throw new \Exception('', 0);
*/
}
}
}
if(isset($announcement['response']['content-disposition'])) {
$response->headers()->set('content-disposition', $announcement['response']['content-disposition']);
}
if(isset($announcement['response']['charset'])) {
$response->headers()->setCharset($announcement['response']['charset']);
}
if(isset($announcement['response']['accept-origin'])) {
// force param as an array
// elements of `accept-origin` are expected to be valid origins (@see https://tools.ietf.org/html/rfc6454#section-7)
$announcement['response']['accept-origin'] = (array) $announcement['response']['accept-origin'];
// retrieve origin from header
$request_origin = $request->header('origin');
// `Access-Control-Allow-Origin` must be a single URI (@see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin)
// so we check for a list of allowed URI
foreach($announcement['response']['accept-origin'] as $origin) {
// #todo use a compare method to handle explicit/implicit port notation
if(in_array($origin, ['*', $request_origin])) {
$response->header('Access-Control-Allow-Origin', $request_origin);
break;
}
}
// prevent requests from non-allowed origins (for non-https requests, this can be bypassed by manually setting requests header)
if($origin != '*' && $origin != $request_origin) {
// raise an exception with error details
throw new \Exception('origin_not_allowed', EQ_ERROR_NOT_ALLOWED);
}
// set headers accordingly to response definition
// #todo allow to customize (override) these values
$response->header('Access-Control-Allow-Methods', 'GET,POST,PUT,PATCH,DELETE,OPTIONS,HEAD,TRACE');
$response->header('Access-Control-Allow-Headers', '*');
// expose headers specific to eQual (#memo - mind case-match)
// (CORS defaults are: Cache-Control, Content-Language, Content-Length, Content-Type, Expires, Last-Modified, Pragma.)
$response->header('Access-Control-Expose-Headers', 'X-Total-Count');
$response->header('Access-Control-Allow-Credentials', 'true');
}
if(isset($announcement['response']['content-disposition'])) {
$response->header('Content-Disposition', $announcement['response']['content-disposition']);
}
// handle caching options, if any
/*
Caching is only available for GET methods,
and offers support at URL level only (params in body are not considered).
Controllers can use cache-vary to tell which elements influence their resulting content.
Accepted values for cache-vary array : uri (default), user, origin, body.
// #todo export this part of the logic to a cache manager
*/
if( $method == 'GET'
&& isset($announcement['response']['cacheable'])
&& $announcement['response']['cacheable']) {
// compute the cache ID
// remove 'cache' param from URI, if present
$request_id = trim(preg_replace('/&cache=[^&]*&/', '&', $request->uri().'&'), '&');
if(isset($announcement['response']['cache-vary'])) {
$vary = (array) $announcement['response']['cache-vary'];
if(in_array('user', $vary)) {
$request_id .= '-'.$auth->userId();
}
if(in_array('origin', $vary)) {
$request_id .= '-'.$request->header('origin');
}
if(in_array('body', $vary)) {
$request_id .= '-'.$request->body();
}
}
$cache_id = md5($request_id);
// retrieve related filename
$cache_filename = EQ_BASEDIR.'/cache/'.$cache_id;
// update context for further processing
$context->set('cache', true);
$context->set('cache-id', $cache_id);
// check if a validity expiration (in seconds) is defined for client-side
if(isset($announcement['response']['client-max-age'])) {
$context->set('client-max-age', intval($announcement['response']['client-max-age']));
}
// check if a validity expiration (in seconds) is defined for server-side
$serve_from_cache = true;
if(isset($announcement['response']['expires'])) {
$expires = intval($announcement['response']['expires']);
$age = time() - filemtime(realpath($cache_filename));
if($age >= $expires) {
$reporter->debug("API::expired cache-id {$cache_id}");
$serve_from_cache = false;
}
}
// handle manual request for invalidating the cache
if(isset($body['cache'])) {
if(in_array($body['cache'], [null, false, 0, '0'])) {
$reporter->debug("API::manual reset cache-id {$cache_id}");
$serve_from_cache = false;
}
// cache is a reserved parameter: no further process
unset($body['cache']);
}
// request is already present in cache
if(file_exists($cache_filename)) {
// cache was invalidated: remove related file
if(!$serve_from_cache) {
$reporter->debug("API::invalidating cache-id {$cache_id}");
unlink($cache_filename);
}
// cache is still valid: serve from cache
else {
// handle client cache expiry (no change)
if($request->header('If-None-Match') == $cache_id) {
// send "304 Not Modified"
$response
->status(304)
->send();
throw new \Exception('', 0);
}
$reporter->debug("API::serving from cache-id {$cache_id}");
list($headers, $result) = unserialize(file_get_contents($cache_filename));
// build response with cached headers
foreach($headers as $header => $value) {
// discard unwanted headers
if(in_array($header, ['Set-Cookie', 'Refresh'])) {
continue;
}
$response->header($header, $value);
}
$response
// inject raw body
->body($result, true)
// set status and body according to raised exception
->status(200)
// send HTTP response
->send();
exit();
}
}
}
}
if(!isset($announcement['access'])) {
$announcement['access'] = [];
}
if( !isset($announcement['access']['visibility'])
|| !in_array($announcement['access']['visibility'], ['public', 'protected', 'private']) ) {
$announcement['access']['visibility'] = 'protected';
}
// check access restrictions
if($announcement['access']['visibility'] != 'public' && php_sapi_name() != 'cli') {
// private is only allowed in CLI
if($announcement['access']['visibility'] == 'private') {
throw new \Exception('private_operation', EQ_ERROR_NOT_ALLOWED);
}
$user_id = $auth->userId();
// user must be authenticated for protected
if($user_id <= 0) {
throw new \Exception('protected_operation', EQ_ERROR_NOT_ALLOWED);
}
// check Security Policies
/** @var \equal\access\AccessController */
$access = $container->get('access');
if(!$access->isRequestCompliant($user_id, $request->getHeaders()->getIpAddress())) {
Reporter::errorHandler(EQ_REPORT_SYSTEM, "AAA::".json_encode(['type' => 'policy', 'status' => 'denied']));
throw new \Exception("Request rejected by Security Policies", EQ_ERROR_NOT_ALLOWED);
}
else {
Reporter::errorHandler(EQ_REPORT_SYSTEM, "AAA::".json_encode(['type' => 'policy', 'status' => 'accepted', 'policy_id' => $access->getComplyingPolicyId()]));
}
if(isset($announcement['access']['users'])) {
// disjunctions on users
$current_user_id = $auth->userId();
if($current_user_id != EQ_ROOT_USER_ID) {
// #todo - add support for checks on login
$allowed = false;
$users = (array) $announcement['access']['users'];
foreach($users as $user_id) {
if($user_id == $current_user_id) {
$allowed = true;
break;
}
}
if(!$allowed) {
throw new \Exception('restricted_operation', EQ_ERROR_NOT_ALLOWED);
}
}
}
if(isset($announcement['access']['groups'])) {
$current_user_id = $auth->userId();
if($current_user_id != EQ_ROOT_USER_ID) {
$allowed = $access->hasGroup(EQ_ROOT_GROUP_ID);
$groups = (array) $announcement['access']['groups'];
foreach($groups as $group) {
if($allowed) {
break;
}
if($access->hasGroup($group)) {
$allowed = true;
}
}
if(!$allowed) {
throw new \Exception('restricted_operation', EQ_ERROR_NOT_ALLOWED);
}
}
}
}
/** @var \equal\data\adapt\DataAdapterProvider */
$dap = $container->get('adapt');
// #todo - use the adapter based on content-type header, if any
/** @var \equal\data\adapt\DataAdapter */
$adapter = $dap->get('json');
// 1) check if all required parameters have been received
// build mandatory fields array
foreach($announcement['params'] as $param => $config) {
if(isset($config['required']) && $config['required']) {
$mandatory_params[] = $param;
}
}
// if at least one mandatory param is missing, reply with announcement
$missing_params = array_values(array_diff($mandatory_params, array_keys($body)));
if( count($missing_params) || isset($body['announce']) || $method == 'OPTIONS' ) {
// #memo - we don't remove anything from the schema, so it can be returned as is for the UI
// (for public and protected controllers this might be considered as security issue as it may reveals a part of the configuration)
// if 'help' is amongst the params and request was made through CLI
if(php_sapi_name() == 'cli' && isset($body['help'])) {
$help = 'Help about ';
$help .= strtoupper($operation['type']).' '.$operation['operation']." :\n\n";
$help .= "Description:\n";
$help .= $announcement["description"]."\n\n";
$help .= "Parameters:\n";
foreach($announcement['params'] as $name => $info) {
$help .= str_pad("--".$name, 20, ' ', STR_PAD_RIGHT);
$required = (isset($info['required']))?'(required)':'';
$help .= str_pad($required, 12, ' ');
$type = $info['type'].( (isset($info['usage']))?' > '.$info['usage']:'');
$help .= str_pad($type, 28, ' ');
$help .= $info['description']."\n";
}
$help.= "\nMore Info :\n";
foreach($body as $key => $value) {
if(isset($announcement['params'][$key])) {
$help.= "--".$key." :\n";
if(isset($announcement['params'][$key]['help'])) {
$help .= ' help: ';
$help .= preg_replace("/ {2,}/", str_pad('', 9, ' '), $announcement['params'][$key]['help'])."\n";
}
if(isset($announcement['params'][$key]['default'])) {
$help .= ' default value: ';
$help .= json_encode($announcement['params'][$key]['default'], true)."\n";
}
if(isset($announcement['params'][$key]['selection'])) {
$help .= ' selection: ';
$help .= json_encode($announcement['params'][$key]['selection'], true)."\n";
}
}
}
$response->status(200)
->header('Content-Type', 'text/plain')
->body($help)
->send();
throw new \Exception('', 0);
}
// add announcement to response body
if(isset($announcement['params'])) {
// default values must be adapted to JSON
foreach((array) $announcement['params'] as $param => $config) {
if(isset($config['default'])) {
$f = new Field($config, $param);
$default_value = $config['default'];
// #memo - array can be used as callable descriptor but are not considered here
if( (is_string($default_value) || is_object($default_value)) && is_callable($default_value)) {
// either a php function (or a function from the global scope) or a closure object
if(is_object($default_value)) {
// default is a closure
$default_value = $default_value();
}
}
elseif(is_string($default_value) && strpos($default_value, '::')) {
list($class_name, $method_name) = explode('::', $default_value);
if(method_exists($class_name, $method_name)) {
/** @var \equal\orm\ObjectManager */
$orm = $container->get('orm');
$default_value = $orm->callonce($class_name, $method_name);
}
}
$announcement['params'][$param]['default'] = $adapter->adaptOut($default_value, $f->getUsage());
}
}