encapsulation = new Encapsulation($this->serverKey()); } public function execute($json) { // Check if we're activated $enabled = Platform::getInstance()->get_platform_configuration_option('frontend_enable', 0); // Is the Secret Key strong enough? $validKey = $this->serverKey(); if (!\Akeeba\Engine\Util\Complexify::isStrongEnough($validKey, false)) { $enabled = false; } $rawEncapsulation = $this->encapsulation->getEncapsulationByCode('ENCAPSULATION_RAW'); if (!$enabled) { return $this->getResponse('Access denied', 503); } // Try to JSON-decode the request's input first $request = @json_decode($json, true); if (is_null($request)) { return $this->getResponse('JSON decoding error', 500); } // Transform legacy requests if (!is_array($request)) { $request = array( 'encapsulation' => $rawEncapsulation, 'body' => $request ); } // Transform partial requests if (!isset($request['encapsulation'])) { $request['encapsulation'] = $rawEncapsulation; } // Make sure we have a request body if (!isset($request['body'])) { $request['body'] = ''; } try { $request['body'] = $this->encapsulation->decode($request['encapsulation'], $request['body']); } catch (\Exception $e) { return $this->getResponse($e->getMessage(), $e->getCode()); } // Replicate the encapsulation preferences of the client for our own output $this->encapsulationType = $request['encapsulation']; // Store the client-specified key, or use the server key if none specified and the request // came encrypted. $this->password = isset($request['body']['key']) ? $request['body']['key'] : $this->serverKey(); // Run the method $params = array(); if (isset($request['body']['data'])) { $params = (array)$request['body']['data']; } try { $taskHandler = new Task(); $data = $taskHandler->execute($request['body']['method'], $params); } catch (\RuntimeException $e) { return $this->getResponse($e->getMessage(), $e->getCode()); } return $this->getResponse($data); } /** * Packages the response to a JSON-encoded object, optionally encrypting the data part with a caller-supplied * password. * * @return string The JSON-encoded response */ private function getResponse($data, $status = 200) { // Initialize the response $response = array( 'encapsulation' => $this->encapsulationType, 'body' => array( 'status' => $status, 'data' => null ) ); if ($status != 200) { $response['encapsulation'] = $this->encapsulation->getEncapsulationByCode('ENCAPSULATION_RAW'); } try { $response['body']['data'] = $this->encapsulation->encode($response['encapsulation'], $data, $this->password); } catch (\Exception $e) { $response['encapsulation'] = $this->encapsulation->getEncapsulationByCode('ENCAPSULATION_RAW'); $response['body'] = array( 'status' => $e->getCode(), 'data' => $e->getMessage(), ); } return '###' . json_encode($response) . '###'; } /** * Get the server key, i.e. the Secret Word for the front-end backups and JSON API * * @return mixed */ private function serverKey() { static $key = null; if (is_null($key)) { $key = Platform::getInstance()->get_platform_configuration_option('frontend_secret_word', ''); } return $key; } }