1 : <?php
2 : /**
3 : * LICENSE
4 : *
5 : * "THE BEER-WARE LICENSE" (Revision 42):
6 : * "Sven Strittmatter" <ich@weltraumschaf.de> wrote this file.
7 : * As long as you retain this notice you can do whatever you want with
8 : * this stuff. If we meet some day, and you think this stuff is worth it,
9 : * you can buy me a beer in return.
10 : *
11 : * @author Sven Strittmatter <ich@weltraumschaf.de>
12 : * @copyright Copyright (c) 2010, Sven Strittmatter.
13 : * @version 0.2.2
14 : * @license http://www.weltraumschaf.de/the-beer-ware-license.txt
15 : */
16 :
17 : /**
18 : * @see Api_Kwick_Config
19 : */
20 : require_once 'Api/Kwick/Config.php';
21 : /**
22 : * @see Api_Kwick_Request
23 : */
24 : require_once 'Api/Kwick/Request.php';
25 : /**
26 : * @see Api_Log
27 : */
28 : require_once 'Api/Log.php';
29 :
30 : /**
31 : * Simple front controller implementation.
32 : *
33 : * HAndles all requests from the Ajax client and passes them via
34 : * Api_Kwick_Requets to the KWICK! API. Returns the original result as JSON
35 : * added with some logging.
36 : *
37 : * @see htdocs/service.php
38 : */
39 : class Api_Controller {
40 : /**
41 : * Prefix for special purpose GET/POST parameter names.
42 : *
43 : * @var string
44 : */
45 : const RESERVED_PARAMETER_PREFIX = '__kapit__';
46 : /**
47 : * Object which performs the API request.
48 : *
49 : * @var Api_Kwick_Request
50 : */
51 : private $apiRequest;
52 :
53 : /**
54 : * Initializes the controller with a request object.
55 : *
56 : * @param Api_Kwick_Request $request Request obejct.
57 : */
58 : public function __construct(Api_Kwick_Request $request) {
59 5 : $this->apiRequest = $request;
60 5 : }
61 :
62 : public function getService() {
63 3 : return $this->getRequestParameter('service', Api_Kwick_Request::DEFAULT_SERVICE);
64 : }
65 :
66 : public function getMethod() {
67 3 : return $this->getRequestParameter('method', Api_Kwick_Request::DEFAULT_METHOD);
68 : }
69 :
70 : public function getSecret() {
71 2 : return $this->getRequestParameter('secret', null);
72 : }
73 :
74 : public function getApiSecret() {
75 2 : return $this->getRequestParameter('__kapit__secret');
76 : }
77 :
78 : public function getApiKey() {
79 2 : return $this->getRequestParameter('__kapit__apiKey');
80 : }
81 :
82 : public function getBaseUri() {
83 2 : return $this->getRequestParameter('__kapit__apiUri');
84 : }
85 :
86 : /**
87 : * Signals if the passed name is a reserved parameter name.
88 : *
89 : * Reserved parameternames start with self::RESERVED_PARAMETER_PREFIX.
90 : * @param string $name Checked parameter name.
91 : * @return boolean
92 : */
93 : public function isReservedParameter($name) {
94 0 : return strpos($name, self::RESERVED_PARAMETER_PREFIX) === 0;
95 : }
96 :
97 : /**
98 : * These parameters are not passed directly to the API.
99 : *
100 : * Signals which GET/POST parameters are special handeled/processed
101 : * and not directly passed to the API.
102 : *
103 : * @param string $name Checked parameter name.
104 : * @return bool
105 : */
106 : public function isSkippedParameter($name) {
107 1 : if (in_array($name, array('service', 'method', 'secret'))) {
108 1 : return true;
109 : }
110 :
111 0 : return false;
112 : }
113 :
114 : /**
115 : * Returns a GET/POST parameter.
116 : *
117 : * First checks GET, second POST and returns the matched name.
118 : * Neither GET nor POST matches $default will be returned.
119 : *
120 : * @param string $name
121 : * @param string $default
122 : * @return string
123 : */
124 : public function getRequestParameter($name, $default = '') {
125 5 : $parameter = $default;
126 :
127 5 : if (isset($_GET[$name]) && !empty($_GET[$name])) {
128 4 : $parameter = $_GET[$name];
129 4 : }
130 :
131 5 : if (isset($_POST[$name]) && !empty($_POST[$name])) {
132 3 : $parameter = $_POST[$name];
133 3 : }
134 :
135 5 : return $parameter;
136 : }
137 :
138 : /**
139 : * Sets all GET/POS parameters on the APIs request object.
140 : */
141 : public function prepareParametersOnRequest() {
142 2 : foreach (array($_GET, $_POST) as $params) {
143 2 : foreach ($params as $name => $value) {
144 1 : if ($this->isSkippedParameter($name) || $this->isReservedParameter($name) ||
145 0 : empty($value))
146 1 : {
147 1 : continue;
148 : }
149 :
150 0 : $this->apiRequest->setParameter($name, $value);
151 2 : }
152 2 : }
153 :
154 2 : if (!empty($_FILES)) {
155 0 : $this->apiRequest->setFiles($_FILES);
156 0 : }
157 2 : }
158 :
159 : /**
160 : * Handles the AJAX calls from KAPIT.
161 : *
162 : * @see htdocs/service.php
163 : */
164 : public function dispatch() {
165 2 : Api_Log::log('Dispatching ' . $this->getService() . '.' . $this->getMethod());
166 2 : $this->apiRequest->setService($this->getService());
167 2 : $this->apiRequest->setMethod($this->getMethod());
168 2 : $secret = $this->getSecret();
169 :
170 2 : if (null !== $secret) { // use session based secret
171 0 : Api_Log::log('Set user secret: ' . $secret);
172 0 : $this->apiRequest->setSecret($secret);
173 0 : } else { // use application based secret
174 2 : if ($this->getApiSecret() !== $this->apiRequest->getSecret()) {
175 0 : Api_Log::log('Set custom secret: ' . $this->getApiSecret());
176 0 : $this->apiRequest->setSecret($this->getApiSecret());
177 0 : }
178 : }
179 :
180 2 : if ($this->getApiKey() !== $this->apiRequest->getApiKey()) {
181 0 : Api_log::Log('Set custom api key: ' . $this->getApiKey());
182 0 : $this->apiRequest->setApiKey($this->getApiKey());
183 0 : }
184 :
185 2 : if ($this->getBaseUri() !== $this->apiRequest->getBaseUri()) {
186 0 : Api_Log::log('Setting custom base uri: ' . $this->getBaseUri());
187 0 : $this->apiRequest->setBaseUri($this->getBaseUri());
188 0 : }
189 :
190 2 : $this->prepareParametersOnRequest();
191 :
192 : try {
193 2 : $data = array('response' => json_decode($this->apiRequest->send()));
194 2 : } catch (Exception $e) {
195 1 : $data = array('exception' => $e->getMessage());
196 : }
197 :
198 2 : $data['log'] = Api_Log::get();
199 2 : echo json_encode($data);
200 2 : }
201 : }
|