apiRequest = $request; } public function getService() { return $this->getRequestParameter('service', Api_Kwick_Request::DEFAULT_SERVICE); } public function getMethod() { return $this->getRequestParameter('method', Api_Kwick_Request::DEFAULT_METHOD); } public function getSecret() { return $this->getRequestParameter('secret', null); } public function getApiSecret() { return $this->getRequestParameter('__kapit__secret'); } public function getApiKey() { return $this->getRequestParameter('__kapit__apiKey'); } public function getBaseUri() { return $this->getRequestParameter('__kapit__apiUri'); } /** * Signals if the passed name is a reserved parameter name. * * Reserved parameternames start with self::RESERVED_PARAMETER_PREFIX. * @param string $name Checked parameter name. * @return boolean */ public function isReservedParameter($name) { return strpos($name, self::RESERVED_PARAMETER_PREFIX) === 0; } /** * These parameters are not passed directly to the API. * * Signals which GET/POST parameters are special handeled/processed * and not directly passed to the API. * * @param string $name Checked parameter name. * @return bool */ public function isSkippedParameter($name) { if (in_array($name, array('service', 'method', 'secret'))) { return true; } return false; } /** * Returns a GET/POST parameter. * * First checks GET, second POST and returns the matched name. * Neither GET nor POST matches $default will be returned. * * @param string $name * @param string $default * @return string */ public function getRequestParameter($name, $default = '') { $parameter = $default; if (isset($_GET[$name]) && !empty($_GET[$name])) { $parameter = $_GET[$name]; } if (isset($_POST[$name]) && !empty($_POST[$name])) { $parameter = $_POST[$name]; } return $parameter; } /** * Sets all GET/POS parameters on the APIs request object. */ public function prepareParametersOnRequest() { foreach (array($_GET, $_POST) as $params) { foreach ($params as $name => $value) { if ($this->isSkippedParameter($name) || $this->isReservedParameter($name) || empty($value)) { continue; } $this->apiRequest->setParameter($name, $value); } } if (!empty($_FILES)) { $this->apiRequest->setFiles($_FILES); } } /** * Handles the AJAX calls from KAPIT. * * @see htdocs/service.php */ public function dispatch() { Api_Log::log('Dispatching ' . $this->getService() . '.' . $this->getMethod()); $this->apiRequest->setService($this->getService()); $this->apiRequest->setMethod($this->getMethod()); $secret = $this->getSecret(); if (null !== $secret) { // use session based secret Api_Log::log('Set user secret: ' . $secret); $this->apiRequest->setSecret($secret); } else { // use application based secret if ($this->getApiSecret() !== $this->apiRequest->getSecret()) { Api_Log::log('Set custom secret: ' . $this->getApiSecret()); $this->apiRequest->setSecret($this->getApiSecret()); } } if ($this->getApiKey() !== $this->apiRequest->getApiKey()) { Api_log::Log('Set custom api key: ' . $this->getApiKey()); $this->apiRequest->setApiKey($this->getApiKey()); } if ($this->getBaseUri() !== $this->apiRequest->getBaseUri()) { Api_Log::log('Setting custom base uri: ' . $this->getBaseUri()); $this->apiRequest->setBaseUri($this->getBaseUri()); } $this->prepareParametersOnRequest(); try { $data = array('response' => json_decode($this->apiRequest->send())); } catch (Exception $e) { $data = array('exception' => $e->getMessage()); } $data['log'] = Api_Log::get(); echo json_encode($data); } }