|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* JSON Request handler. |
|
5
|
|
|
* |
|
6
|
|
|
* This class handles all incoming JSON requests from the client. In short, it receives the JSON, |
|
7
|
|
|
* decodes JSON data, then sends the requests to the correct modules, and builds the reply JSON. The reply |
|
8
|
|
|
* JSON is encoded and then returned for reply. |
|
9
|
|
|
*/ |
|
10
|
|
|
class JSONRequest { |
|
11
|
|
|
public function __construct() {} |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Execute incoming JSON request. |
|
15
|
|
|
* |
|
16
|
|
|
* This function executes the actions in the JSON, which are received from |
|
17
|
|
|
* the client. The entire JSON request is processed at once here, and this function |
|
18
|
|
|
* is therefore called only once for each HTTP request to the server. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $json the json string which is received by the client |
|
21
|
|
|
* |
|
22
|
|
|
* @return string the built json which will be sent back to the client |
|
23
|
|
|
* |
|
24
|
|
|
* @todo Reduce overhead by outputting created JSON by outputting directly to php://output instead of returning a |
|
25
|
|
|
* (possibly huge) string containing the serialized JSON |
|
26
|
|
|
*/ |
|
27
|
|
|
public function execute($json) { |
|
28
|
|
|
/* If json_encode produces an empty string, wa-js shows an absolutely |
|
29
|
|
|
* worthless "Invalid data received from the server" dialog. */ |
|
30
|
|
|
$jsonflags = JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE; |
|
31
|
|
|
|
|
32
|
|
|
try { |
|
33
|
|
|
// decode JSON data |
|
34
|
|
|
$data = json_decode_data($json, true); |
|
35
|
|
|
|
|
36
|
|
|
// Reset the bus |
|
37
|
|
|
$GLOBALS["bus"]->reset(); |
|
38
|
|
|
|
|
39
|
|
|
// notify modules that wants to do something at the start of a request |
|
40
|
|
|
$GLOBALS["bus"]->notify(REQUEST_ENTRYID, REQUEST_START); |
|
41
|
|
|
|
|
42
|
|
|
// Check if the JSON is parsed correctly into an array |
|
43
|
|
|
$data = $data["zarafa"] ?: false; |
|
44
|
|
|
|
|
45
|
|
|
// @TODO throw exception if zarafa tag is not present |
|
46
|
|
|
if (is_array($data)) { |
|
47
|
|
|
// iterate over all module names |
|
48
|
|
|
foreach ($data as $moduleName => $modules) { |
|
49
|
|
|
// each module can contain multiple requests using different module ids |
|
50
|
|
|
foreach ($modules as $moduleId => $moduleData) { |
|
51
|
|
|
// Create the module via the Dispatcher |
|
52
|
|
|
$moduleObj = $GLOBALS["dispatcher"]->loadModule($moduleName, $moduleId, $moduleData); |
|
53
|
|
|
|
|
54
|
|
|
// Check if the module is loaded |
|
55
|
|
|
if (is_object($moduleObj)) { |
|
56
|
|
|
$moduleObj->loadSessionData(); |
|
57
|
|
|
|
|
58
|
|
|
// Execute the actions in the module |
|
59
|
|
|
$moduleObj->execute(); |
|
60
|
|
|
|
|
61
|
|
|
$moduleObj->saveSessionData(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// notify modules that wants to do something at the end of a request |
|
68
|
|
|
$GLOBALS["bus"]->notify(REQUEST_ENTRYID, REQUEST_END); |
|
69
|
|
|
|
|
70
|
|
|
// Build the JSON and return it |
|
71
|
|
|
return json_encode(["zarafa" => $GLOBALS["bus"]->getData()], $jsonflags); |
|
72
|
|
|
} |
|
73
|
|
|
catch (ZarafaException $e) { |
|
74
|
|
|
if (!$e->isHandled) { |
|
75
|
|
|
$data = [ |
|
76
|
|
|
"error" => [ |
|
77
|
|
|
"type" => ERROR_ZARAFA, |
|
78
|
|
|
"info" => [ |
|
79
|
|
|
"file" => $e->getFileLine(), |
|
80
|
|
|
"display_message" => $e->getDisplayMessage(), |
|
81
|
|
|
"original_message" => $e->getMessage(), |
|
82
|
|
|
], |
|
83
|
|
|
], |
|
84
|
|
|
]; |
|
85
|
|
|
|
|
86
|
|
|
return json_encode(["zarafa" => $data], $jsonflags); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
catch (ZarafaErrorException $e) { |
|
90
|
|
|
if (!$e->isHandled) { |
|
91
|
|
|
$data = [ |
|
92
|
|
|
"error" => [ |
|
93
|
|
|
"type" => ERROR_GENERAL, |
|
94
|
|
|
"info" => [ |
|
95
|
|
|
"file" => $e->getFileLine(), |
|
96
|
|
|
"display_message" => $e->getDisplayMessage(), |
|
97
|
|
|
"original_message" => $e->getMessage(), |
|
98
|
|
|
], |
|
99
|
|
|
], |
|
100
|
|
|
]; |
|
101
|
|
|
|
|
102
|
|
|
return json_encode(["zarafa" => $data], $jsonflags); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
catch (Exception $e) { |
|
106
|
|
|
// handle exceptions that are not handled by modules |
|
107
|
|
|
dump($e); |
|
108
|
|
|
|
|
109
|
|
|
$data = [ |
|
110
|
|
|
"error" => [ |
|
111
|
|
|
"type" => ERROR_GENERAL, |
|
112
|
|
|
"info" => [ |
|
113
|
|
|
"file" => basename($e->getFile()) . ':' . $e->getLine(), |
|
114
|
|
|
"display_message" => _('An unexpected error has occurred'), |
|
115
|
|
|
"original_message" => $e->getMessage(), |
|
116
|
|
|
], |
|
117
|
|
|
], |
|
118
|
|
|
]; |
|
119
|
|
|
|
|
120
|
|
|
return json_encode(["zarafa" => $data], $jsonflags); |
|
121
|
|
|
} |
|
122
|
|
|
catch (Throwable $e) { |
|
123
|
|
|
// Catch PHP Errors/TypeErrors as JSON to avoid HTTP 500s |
|
124
|
|
|
dump($e); |
|
125
|
|
|
|
|
126
|
|
|
$data = [ |
|
127
|
|
|
"error" => [ |
|
128
|
|
|
"type" => ERROR_GENERAL, |
|
129
|
|
|
"info" => [ |
|
130
|
|
|
"file" => basename($e->getFile()) . ':' . $e->getLine(), |
|
131
|
|
|
"display_message" => _('An unexpected error has occurred'), |
|
132
|
|
|
"original_message" => $e->getMessage(), |
|
133
|
|
|
], |
|
134
|
|
|
], |
|
135
|
|
|
]; |
|
136
|
|
|
|
|
137
|
|
|
return json_encode(["zarafa" => $data], $jsonflags); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|