From 073273c8a1a04cd1213d6c88d4ada01223122129 Mon Sep 17 00:00:00 2001
From: mcramer <m.cramer@pixcept.de>
Date: Tue, 03 Sep 2013 12:14:51 -0400
Subject: [PATCH] - Implemented json remote handler, example:   http://yourispconfig:8080/remote/json.php?sites_web_domain_get   with POST data:   session=12345678901234567890123456789012&data=%7B%22type%22%3A%22vhost%22%2C%22active%22%3A%22y%22%2C%22php%22%3A%22fast-cgi%22%2C%22fastcgi_php_version%22%3A%22%22%2C%22%23LIMIT%23%22%3A1%7D   where data is an urlencoded json-object of the params array used in SOAP api.

---
 interface/web/remote/json.php              |   11 +++
 interface/lib/classes/json_handler.inc.php |  116 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 127 insertions(+), 0 deletions(-)

diff --git a/interface/lib/classes/json_handler.inc.php b/interface/lib/classes/json_handler.inc.php
new file mode 100644
index 0000000..d2acb22
--- /dev/null
+++ b/interface/lib/classes/json_handler.inc.php
@@ -0,0 +1,116 @@
+<?php
+
+/*
+Copyright (c) 2013, Marius Cramer, pixcept KG
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice,
+      this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright notice,
+      this list of conditions and the following disclaimer in the documentation
+      and/or other materials provided with the distribution.
+    * Neither the name of ISPConfig nor the names of its contributors
+      may be used to endorse or promote products derived from this software without
+      specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+*/
+
+
+class ISPConfigJSONHandler {
+    private $methods = array();
+    private $classes = array();
+
+    public function __construct() {
+        global $app;
+        
+        // load main remoting file
+        $app->load('remoting');
+        
+        // load all remote classes and get their methods
+        $dir = dirname(realpath(__FILE__)) . '/remote.d';
+        $d = opendir($dir);
+        while($f = readdir($d)) {
+            if($f == '.' || $f == '..') continue;
+            if(!is_file($dir . '/' . $f) || substr($f, strrpos($f, '.')) != '.php') continue;
+            
+            $name = substr($f, 0, strpos($f, '.'));
+            
+            include($dir . '/' . $f);
+            $class_name = 'remoting_' . $name;
+            if(class_exists($class_name, false)) {
+                $this->classes[$class_name] = new $class_name();
+                foreach(get_class_methods($this->classes[$class_name]) as $method) {
+                    $this->methods[$method] = $class_name;
+                }
+            }
+        }
+        closedir($d);
+        
+        // add main methods
+        $this->methods['login'] = 'remoting';
+        $this->methods['logout'] = 'remoting';
+        $this->methods['get_function_list'] = 'remoting';
+        
+        // create main class
+        $this->classes['remoting'] = new remoting(array_keys($this->methods));
+    }
+    
+    private function _return_json($code, $message, $data = false) {
+        $ret = new stdClass;
+        $ret->code = $code;
+        $ret->message = $message;
+        $ret->response = $data;
+        header('Content-Type: application/x-json; charset="utf-8"');
+        print json_encode($ret);
+        exit;
+    }
+    
+    public function run() {
+        
+        $method = reset(array_keys($_GET));
+        $params = array();
+
+        if(is_array($_POST)) {
+            foreach($_POST as $key => $val) {
+                $tmp = json_decode($val);
+                if(!$tmp) $params[] = $val;
+                else $params[] = (array)$tmp;
+            }
+        }
+        
+        if(array_key_exists($method, $this->methods) == false) {
+            $this->_return_json('invalid_method', 'Method ' . $method . ' does not exist');
+        }
+        
+        $class_name = $this->methods[$method];
+        if(array_key_exists($class_name, $this->classes) == false) {
+            $this->_return_json('invalid_class', 'Class ' . $class_name . ' does not exist');
+        }
+        
+        if(method_exists($this->classes[$class_name], $method) == false) {
+            $this->_return_json('invalid_method', 'Method ' . $method . ' does not exist in the class it was expected (' . $class_name . ')');
+        }
+        
+        try {
+            $this->_return_json('ok', '', call_user_func_array(array($this->classes[$class_name], $method), $params));
+        } catch(SoapFault $e) {
+            $this->_return_json('remote_fault', $e->getMessage());
+        }
+    }
+}
+
+?>
\ No newline at end of file
diff --git a/interface/web/remote/json.php b/interface/web/remote/json.php
new file mode 100644
index 0000000..f8e666c
--- /dev/null
+++ b/interface/web/remote/json.php
@@ -0,0 +1,11 @@
+<?php
+
+require_once('../../lib/config.inc.php');
+$conf['start_session'] = false;
+require_once('../../lib/app.inc.php');
+
+$app->load('json_handler');
+$json_handler = new ISPConfigJSONHandler();
+$json_handler->run();
+
+?>
\ No newline at end of file

--
Gitblit v1.9.1