-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
100 lines (81 loc) · 3.39 KB
/
index.php
File metadata and controls
100 lines (81 loc) · 3.39 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
<?php
include './config/const.php';
include './lib/JsonArray.php';
include './config/controllers.php';
include './lib/Validator.php';
include './entities/ControllerOutput.php';
include './entities/ShieldRequest.php';
include './lib/Auth.php';
include './vendor/autoload.php';
ORM::configure('mysql:host='.$dbHost.';dbname='.$dbName);
ORM::configure('username', $dbUser);
ORM::configure('password', $dbPass);
// $tmp = ORM::raw_execute("select * from measurement");
// $statement = ORM::get_last_statement();
// $rows = $statement->fetchAll(PDO::FETCH_ASSOC);
// exit();
// var_dump($_SERVER);
// $inputSize = (int) $_SERVER['CONTENT_LENGTH'];
// if($inputSize > MAX_INPUT_SIZE){
// JsonArray::p(false, ['error' => 'Too large request MAX_INPUT_SIZE is ' + MAX_INPUT_SIZE], true);
// }
$rawData = file_get_contents(INPUT_PATH);
$jsonData = json_decode($rawData, true);
//check default validation of inputdata
if(!isset($jsonData['action']) || $jsonData['action'] == '') {
JsonArray::p(false, ['error' => 'Action parameters could be not empty.'], true);
}
$exectRoute = [
'controller' => NULL,
'action' => NULL,
'arguments' => NULL
];
$secureArguments = [];
$path = $jsonData['action'];
$slashPosition = strpos($path, '/');
if($slashPosition < 0) {
JsonArray::p(false, ['error' => 'Invalid action parameters.'], true);
}
$controller = substr($path, 0, $slashPosition);
$action = substr($path, $slashPosition + 1);
if(!isset($controllesAction[$controller][$action])) {
JsonArray::p(false, ['error' => 'Action not exist.'], true);
}
$exectRoute['controller'] = $controller;
$exectRoute['action'] = $action;
$exectRoute['arguments'] = $controllesAction[$controller][$action];
//input argumnts validation if input arguments is defined
if($controllesAction[$exectRoute['controller']][$exectRoute['action']] != NULL) {
if(!isset($jsonData['data']) || $jsonData['data'] == '') {
JsonArray::p(false, ['error' => 'Action ' . $exectRoute['action'] . ' you must type data arguments.'], true);
}
$data = $jsonData['data'];
foreach($exectRoute['arguments'] as $argumentsName => $filtrOptions) {
//checking requiring arguments
if(isset($filtrOptions['require'])){
if(!isset($data[$argumentsName])) {
JsonArray::p(false, ['error' => $filtrOptions['require']['errorMessage']], true);
}
}
else {
if(!isset($data[$argumentsName])) {
$secureArguments[$argumentsName] = isset($filtrOptions['defaultValue']) ? $filtrOptions['defaultValue'] : NULL;
continue;
}
}
//filters validation
foreach($filtrOptions as $filterName => $arguments) {
$filtrArray = isset($arguments['param']) ? ['filtr' => $filterName] + $arguments['param'] : ['filtr' => $filterName];
$valid = Validator::secInput($data[$argumentsName], $filtrArray);
if($valid['ok'] == false) {
JsonArray::p(false, ['error' => $arguments['errorMessage']], true);
}
$secureArguments[$argumentsName] = $valid['var'];
}
}
}
$controllerName = ucfirst($exectRoute['controller']).'Controller';
$actionName = $exectRoute['action'].'Action';
include './controllers/'.$controllerName.'.php';
$controllerOutput = $controllerName::$actionName($secureArguments);
JsonArray::p($controllerOutput->status, $controllerOutput->data, true);