1 : <?php
2 : require_once 'Api/Kwick/Config.php';
3 : require_once 'Api/Kwick/Request.php';
4 : require_once 'Api/Log.php';
5 :
6 : class Api_Controller {
7 : private $apiRequest;
8 :
9 : public function __construct(Api_Kwick_Request $request) {
10 5 : $this->apiRequest = $request;
11 5 : }
12 :
13 : public function getService() {
14 3 : return $this->getRequestParameter('service', Api_Kwick_Request::DEFAULT_SERVICE);
15 : }
16 :
17 : public function getMethod() {
18 3 : return $this->getRequestParameter('method', Api_Kwick_Request::DEFAULT_METHOD);
19 : }
20 :
21 : public function getSecret() {
22 2 : return $this->getRequestParameter('secret');
23 : }
24 :
25 : public function getRequestParameter($name, $default = null) {
26 5 : $parameter = $default;
27 :
28 5 : if (isset($_GET[$name]) && !empty($_GET[$name])) {
29 4 : $parameter = $_GET[$name];
30 4 : }
31 :
32 5 : if (isset($_POST[$name]) && !empty($_POST[$name])) {
33 3 : $parameter = $_POST[$name];
34 3 : }
35 :
36 5 : return $parameter;
37 : }
38 :
39 : public function prepareParametersOnRequest() {
40 2 : foreach (array($_GET, $_POST) as $params) {
41 2 : foreach ($params as $name => $value) {
42 1 : if (in_array($name, array('service', 'method', 'secret')) || empty($value)) {
43 1 : continue;
44 : }
45 :
46 0 : $this->apiRequest->setParameter($name, $value);
47 2 : }
48 2 : }
49 2 : }
50 :
51 : public function dispatch() {
52 2 : Api_Log::log('Dispatching ' . $this->getService() . '.' . $this->getMethod());
53 2 : $this->apiRequest->setService($this->getService());
54 2 : $this->apiRequest->setMethod($this->getMethod());
55 2 : $secret = $this->getSecret();
56 :
57 2 : if (null !== $secret) {
58 0 : Api_Log::log('Setting user secret: ' . $secret);
59 0 : $this->apiRequest->setSecret($secret);
60 0 : }
61 :
62 2 : $this->prepareParametersOnRequest();
63 :
64 : try {
65 2 : $data = array('response' => json_decode($this->apiRequest->send()));
66 2 : } catch (Exception $e) {
67 1 : $data = array('exception' => $e->__toString());
68 : }
69 :
70 2 : $data['log'] = Api_Log::get();
71 : // echo str_replace('\\', '\\\\', json_encode($data));
72 2 : echo json_encode($data);
73 2 : }
|