summaryrefslogtreecommitdiffstats
path: root/lib/kolab_api_controller.php
diff options
context:
space:
mode:
authorAleksander Machniak <alec@alec.pl>2012-02-16 10:16:55 +0100
committerAleksander Machniak <alec@alec.pl>2012-02-16 10:16:55 +0100
commit6664b058c428e86511c5330f735df3d7752b6fe8 (patch)
treef8230098602e644e88ef01376479f7abe927bbdd /lib/kolab_api_controller.php
parent620c4bf621f607b21904f590e4e676f396d40362 (diff)
downloadwebadmin-6664b058c428e86511c5330f735df3d7752b6fe8.tar.gz
Added proxy functionality - allows direct API calls
Diffstat (limited to 'lib/kolab_api_controller.php')
-rw-r--r--lib/kolab_api_controller.php52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/kolab_api_controller.php b/lib/kolab_api_controller.php
index 3fcc74c..5401609 100644
--- a/lib/kolab_api_controller.php
+++ b/lib/kolab_api_controller.php
@@ -97,8 +97,17 @@ class kolab_api_controller
*/
public function dispatch($postdata)
{
+ $config = Conf::get_instance();
+
+ // Use proxy
+ if (empty($_GET['proxy']) && ($url = $config->get('kolab_wap', 'api_url'))) {
+ $this->proxy($postdata, $url);
+ return;
+ }
+
$service = $this->request['service'];
$method = $this->request['method'];
+ $postdata = @json_decode($postdata);
console("Calling method " . $method . " on service " . $service);
// validate user session
@@ -133,6 +142,49 @@ class kolab_api_controller
/**
+ * Proxies request to the API host
+ */
+ private function proxy($postdata, $url)
+ {
+ $service = $this->request['service'];
+ $method = $this->request['method'];
+ $url .= '/' . $service . '.' . $method;
+
+ console("Proxying to " . $url);
+
+ $request = new HTTP_Request2();
+ $url = new Net_URL2($url);
+ $method = strtoupper($_SERVER['REQUEST_METHOD']);
+
+ $request->setMethod($method == 'GET' ? HTTP_Request2::METHOD_GET : HTTP_Request2::METHOD_POST);
+ $request->setHeader('X-Session-Token', kolab_utils::get_request_header('X-Session-Token'));
+
+ if ($method == 'GET') {
+ $request->setBody($postdata);
+ }
+
+ try {
+ $request->setUrl($url);
+ $response = $request->send();
+ }
+ catch (Exception $e) {
+ $this->output->error("Internal error", 500);
+ }
+
+ try {
+ $body = $response->getBody();
+ }
+ catch (Exception $e) {
+ $this->output->error("Internal error", 500);
+ }
+
+ header("Content-Type: application/json");
+ echo $body;
+ exit;
+ }
+
+
+ /**
* Validate the submitted session token
*/
private function session_validate($postdata)