This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
116 lines (88 loc) · 3.24 KB
/
index.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
<?php
require('lib/Crowdmap.php');
$applicationPublicKey = null;
$applicationPrivateKey = null;
$requestMethod = null;
$requestResource = null;
$requestDataFormat = null;
$requestData = null;
$responseStatus = null;
$responseMessage = null;
$responseDataFormat = null;
$responseData = null;
$requestMethodValues = array('GET', 'POST', 'PUT', 'DELETE');
$requestDataFormatValues = array('JSON', 'Query String');
$responseDataFormatValues = array('JSON', 'PHP');
if (is_file('lib/spyc.php')) {
$requestDataFormatValues[] = 'YAML';
$responseDataFormatValues[] = 'YAML';
}
if (is_file('config.php')) {
include('config.php');
}
if ($_POST) {
$applicationPublicKey = $_POST['application']['public_key'];
$applicationPrivateKey = $_POST['application']['private_key'];
$requestMethod = $_POST['request']['method'];
$requestResource = $_POST['request']['resource'];
$requestDataFormat = $_POST['request']['data_format'];
$requestData = $_POST['request']['data'];
$responseDataFormat = $_POST['response']['data_format'];
$Crowdmap = new Crowdmap($applicationPublicKey, $applicationPrivateKey);
switch ($requestDataFormat) {
case 'JSON':
$requestDataParsed = json_decode($requestData, true);
break;
case 'YAML':
if (!is_file('lib/spyc.php')) {
throw new Exception('YAML format may not be used because Spyc parsing library is not present.');
}
require_once('lib/spyc.php');
$requestDataParsed = Spyc::YAMLLoadString($requestData);
break;
case 'Query String':
$requestDataParsed = array();
parse_str($requestData, $requestDataParsed);
break;
default:
throw new Exception(sprintf('Invalid request data format: "%s"', $requestDataFormat));
break;
}
$response = null;
try {
$response = $Crowdmap->call($requestMethod, $requestResource, $requestDataParsed);
if (isset($response->error) && $response->error) {
$responseStatus = 'error';
$responseMessage = $response->error;
} else {
$responseStatus = 'success';
$responseMessage = 'Success';
}
} catch (Exception $exception) {
$responseStatus = 'error';
$responseMessage = $exception->getMessage();
}
if ($response) {
switch ($responseDataFormat) {
case 'JSON':
$responseData = json_encode($response);
break;
case 'PHP':
$responseData = print_r($response, true);
break;
case 'YAML':
if (!is_file('lib/spyc.php')) {
throw new Exception('YAML format may not be used because Spyc parsing library is not present.');
}
require_once('lib/spyc.php');
$responseObject = json_encode($response);
$responseArray = json_decode($responseObject, true);
$responseData = Spyc::YAMLDump($responseArray, true);
break;
default:
throw new Exception(sprintf('Invalid response data format: "%s"', $responseDataFormat));
break;
}
}
}
include('index.phtml');