wrote this file. * As long as you retain this notice you can do whatever you want with * this stuff. If we meet some day, and you think this stuff is worth it, * you can buy me a beer in return. * * @author Sven Strittmatter * @copyright Copyright (c) 2010, Sven Strittmatter. * @version 0.2.0 * @license http://www.weltraumschaf.de/the-beer-ware-license.txt */ /** * @see Api_Kwick_Config */ require_once 'Api/Kwick/Config.php'; /** * @see Api_Kwick_Request */ require_once 'Api/Kwick/Request.php'; /** * @see Api_Log */ require_once 'Api/Log.php'; /** * Simple front controller implementation. * * HAndles all requests from the Ajax client and passes them via * Api_Kwick_Requets to the KWICK! API. Returns the original result as JSON * added with some logging. * * @see htdocs/service.php */ class Api_Controller { /** * Prefix for special purpose GET/POST parameter names. * * @var string */ const RESERVED_PARAMETER_PREFIX = '__kapit__'; /** * Object which performs the API request. * * @var Api_Kwick_Request */ private $apiRequest; /** * Initializes the controller with a request object. * * @param Api_Kwick_Request $request Request obejct. */ public function __construct(Api_Kwick_Request $request) { $this->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); } }