From 073273c8a1a04cd1213d6c88d4ada01223122129 Mon Sep 17 00:00:00 2001 From: mcramer Date: Tue, 3 Sep 2013 16:14:51 +0000 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/lib/classes/json_handler.inc.php | 116 +++++++++++++++++++++ interface/web/remote/json.php | 11 ++ 2 files changed, 127 insertions(+) create mode 100644 interface/lib/classes/json_handler.inc.php create mode 100644 interface/web/remote/json.php diff --git a/interface/lib/classes/json_handler.inc.php b/interface/lib/classes/json_handler.inc.php new file mode 100644 index 000000000..d2acb229c --- /dev/null +++ b/interface/lib/classes/json_handler.inc.php @@ -0,0 +1,116 @@ +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 000000000..f8e666c38 --- /dev/null +++ b/interface/web/remote/json.php @@ -0,0 +1,11 @@ +load('json_handler'); +$json_handler = new ISPConfigJSONHandler(); +$json_handler->run(); + +?> \ No newline at end of file -- GitLab