-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest-secure.php.dist
executable file
·81 lines (72 loc) · 2.81 KB
/
rest-secure.php.dist
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
<?php
header('Content-Type: application/json; charset=utf-8');
// initialization script
require_once 'init.php';
class AdiantiRestServer
{
public static function run($request)
{
$ini = AdiantiApplicationConfig::get();
$input = json_decode(file_get_contents("php://input"), true);
$request = array_merge($request, (array) $input);
$class = isset($request['class']) ? $request['class'] : '';
$method = isset($request['method']) ? $request['method'] : '';
$headers = AdiantiCoreApplication::getHeaders();
$response = NULL;
$headers['Authorization'] = $headers['Authorization'] ?? ($headers['authorization'] ?? null); // for clientes that send in lowercase (Ex. futter)
try
{
if (empty($headers['Authorization']))
{
throw new Exception( _t('Authorization error') );
}
else
{
if (substr($headers['Authorization'], 0, 5) == 'Basic')
{
if (empty($ini['general']['rest_key']))
{
throw new Exception( _t('REST key not defined') );
}
if ($ini['general']['rest_key'] !== substr($headers['Authorization'], 6))
{
http_response_code(401);
return json_encode( array('status' => 'error', 'data' => _t('Authorization error')));
}
}
else if (substr($headers['Authorization'], 0, 6) == 'Bearer')
{
ApplicationAuthenticationService::fromToken( substr($headers['Authorization'], 7) );
}
else
{
http_response_code(403);
throw new Exception( _t('Authorization error') );
}
}
$response = AdiantiCoreApplication::execute($class, $method, $request, 'rest');
if (is_array($response))
{
array_walk_recursive($response, ['AdiantiStringConversion', 'assureUnicode']);
}
return json_encode( array('status' => 'success', 'data' => $response));
}
catch (Exception $e)
{
if(200 === http_response_code())
{
http_response_code(500);
}
return json_encode( array('status' => 'error', 'data' => $e->getMessage()));
}
catch (Error $e)
{
if(200 === http_response_code())
{
http_response_code(500);
}
return json_encode( array('status' => 'error', 'data' => $e->getMessage()));
}
}
}
print AdiantiRestServer::run($_REQUEST);