reset(); // notify modules that wants to do something at the start of a request $GLOBALS["bus"]->notify(REQUEST_ENTRYID, REQUEST_START); // Check if the JSON is parsed correctly into an array $data = $data["grommunio"] ?: false; // @TODO throw exception if grommunio tag is not present if (is_array($data)) { // iterate over all module names foreach ($data as $moduleName => $modules) { // each module can contain multiple requests using different module ids foreach ($modules as $moduleId => $moduleData) { // Create the module via the Dispatcher $moduleObj = $GLOBALS["dispatcher"]->loadModule($moduleName, $moduleId, $moduleData); // Check if the module is loaded if (is_object($moduleObj)) { if (!$GLOBALS["bus"]->synchronizePersistentState()) { throw new RuntimeException('Unable to publish notifier state'); } try { $moduleObj->loadSessionData(); // Execute the actions in the module $moduleObj->execute(); $moduleObj->saveSessionData(); } finally { $moduleObj->closeSessionData(); } } } } } // notify modules that wants to do something at the end of a request $GLOBALS["bus"]->notify(REQUEST_ENTRYID, REQUEST_END); // Build the JSON and return it return json_encode(["grommunio" => $GLOBALS["bus"]->getData()], $jsonflags); } catch (GrommunioException $e) { if (!$e->isHandled) { $data = [ "error" => [ "type" => ERROR_GROMMUNIO, "info" => [ "file" => $e->getFileLine(), "display_message" => $e->getDisplayMessage(), "original_message" => $e->getMessage(), ], ], ]; return json_encode(["grommunio" => $data], $jsonflags); } } catch (GrommunioErrorException $e) { if (!$e->isHandled) { $data = [ "error" => [ "type" => ERROR_GENERAL, "info" => [ "file" => $e->getFileLine(), "display_message" => $e->getDisplayMessage(), "original_message" => $e->getMessage(), ], ], ]; return json_encode(["grommunio" => $data], $jsonflags); } } catch (Exception $e) { // handle exceptions that are not handled by modules dump($e); $data = [ "error" => [ "type" => ERROR_GENERAL, "info" => [ "file" => basename($e->getFile()) . ':' . $e->getLine(), "display_message" => _('An unexpected error has occurred'), "original_message" => $e->getMessage(), ], ], ]; return json_encode(["grommunio" => $data], $jsonflags); } catch (Throwable $e) { // Catch PHP Errors/TypeErrors as JSON to avoid HTTP 500s dump($e); $data = [ "error" => [ "type" => ERROR_GENERAL, "info" => [ "file" => basename($e->getFile()) . ':' . $e->getLine(), "display_message" => _('An unexpected error has occurred'), "original_message" => $e->getMessage(), ], ], ]; return json_encode(["grommunio" => $data], $jsonflags); } // A handled exception already put its error response on the bus. return json_encode(["grommunio" => $GLOBALS["bus"]->getData()], $jsonflags); } }