From d4e764ab2822d962d186ba978fc5f08afec2104e Mon Sep 17 00:00:00 2001 From: skodak Date: Mon, 19 Oct 2009 23:14:26 +0000 Subject: [PATCH] MDL-12886 rewritten xmlrpc client, not finished yet - need to solve error sending when exception occures outside of the executed method; the test client needs a lot of refactoring too --- webservice/lib.php | 11 +- webservice/xmlrpc/db/access.php | 12 ++ webservice/xmlrpc/lib.php | 116 ------------------ webservice/xmlrpc/locallib.php | 43 +++++++ webservice/xmlrpc/server.php | 62 ++++------ webservice/xmlrpc/simpleserver.php | 50 ++++++++ webservice/xmlrpc/testclient/index.php | 109 ++++++++++++++++ .../xmlrpc/testclient/zend_xmlrpc_client.php | 70 ----------- webservice/xmlrpc/version.php | 3 + 9 files changed, 253 insertions(+), 223 deletions(-) create mode 100644 webservice/xmlrpc/db/access.php delete mode 100644 webservice/xmlrpc/lib.php create mode 100644 webservice/xmlrpc/locallib.php create mode 100644 webservice/xmlrpc/simpleserver.php create mode 100644 webservice/xmlrpc/testclient/index.php delete mode 100644 webservice/xmlrpc/testclient/zend_xmlrpc_client.php create mode 100644 webservice/xmlrpc/version.php diff --git a/webservice/lib.php b/webservice/lib.php index 82fe83d85a..09faa37199 100644 --- a/webservice/lib.php +++ b/webservice/lib.php @@ -112,10 +112,16 @@ abstract class webservice_zend_server implements webservice_server { // make a list of all functions user is allowed to excecute $this->init_service_class(); + // TODO: solve debugging level somehow + Zend_XmlRpc_Server_Fault::attachFaultException('moodle_exception'); + // start the server $this->zend_server->setClass($this->service_class); $response = $this->zend_server->handle(); + //$grrr = ob_get_clean(); + //error_log($grrr); + // session cleanup $this->session_cleanup(); @@ -213,7 +219,6 @@ class '.$classname.' { '; // load the virtual class definition into memory eval($code); -echo "".$code.""; $this->service_class = $classname; } @@ -305,7 +310,7 @@ echo "".$code.""; } $return = ' * @return '.$type.' '.$function->returns_desc->desc; } - + // now crate a virtual method that calls the ext implemenation // TODO: add PHP docs and all missing info here @@ -365,10 +370,12 @@ echo "".$code.""; $this->restricted_context = get_context_instance(CONTEXT_SYSTEM); if (!is_enabled_auth('webservice')) { + error_log('WS auth not enabled'); die('WS auth not enabled'); } if (!$auth = get_auth_plugin('webservice')) { + error_log('WS auth missing'); die('WS auth missing'); } diff --git a/webservice/xmlrpc/db/access.php b/webservice/xmlrpc/db/access.php new file mode 100644 index 0000000000..1c58213e31 --- /dev/null +++ b/webservice/xmlrpc/db/access.php @@ -0,0 +1,12 @@ + array( + 'captype' => 'read', // in fact this may be considered read and write at the same time + 'contextlevel' => CONTEXT_COURSE, // the context level should be probably CONTEXT_MODULE + 'legacy' => array( + ), + ), + +); diff --git a/webservice/xmlrpc/lib.php b/webservice/xmlrpc/lib.php deleted file mode 100644 index 2a90c4192d..0000000000 --- a/webservice/xmlrpc/lib.php +++ /dev/null @@ -1,116 +0,0 @@ -dirroot.'/webservice/lib.php'); - -final class xmlrpc_server extends webservice_server { - - public function __construct() { - - $this->set_protocolname("XML-RPC"); - $this->set_protocolid("xmlrpc"); - } - - public function run() { - $enable = $this->get_enable(); - if (empty($enable)) { - die; - } - include "Zend/Loader.php"; - Zend_Loader::registerAutoload(); - - Zend_XmlRpc_Server_Fault::attachFaultException('moodle_exception'); - - // retrieve the token from the url - // if the token doesn't exist, set a class containing only get_token() - $token = optional_param('token',null,PARAM_ALPHANUM); - if (empty($token)) { - $server = new Zend_XmlRpc_Server(); - $server->setClass("ws_authentication", "authentication"); - // Create a request object - $request = new Zend_XmlRpc_Request_Http(); - $params = $request->getParams(); - $this->convertXmlrpcParams($params); - $request->setParams($params); - echo $server->handle($request); - } else { // if token exist, do the authentication here - /// TODO: following function will need to be modified - $user = webservice_lib::mock_check_token($token); - if (empty($user)) { - throw new moodle_exception('wrongidentification'); - } else { - /// TODO: probably change this - global $USER; - $USER = $user; - } - - //retrieve the api name - $classpath = optional_param('classpath', null, PARAM_SAFEDIR); - require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php'); - - /// run the server - $server = new Zend_XmlRpc_Server(); - $server->setClass($classpath."_external", $classpath); - $request = new Zend_XmlRpc_Request_Http(); - $params = $request->getParams(); - $this->convertXmlrpcParams($params); - $request->setParams($params); - echo $server->handle($request); - } - } - - private function convertXmlrpcParams(&$params) { - if (is_array($params)) { - //get the first key - $key = key($params); - - reset($params); - - //if first key == 0 so do not change the params - if (strcmp($key, "0") == 0) { - foreach ($params as &$param) { - $this->convertXmlrpcParams($param); - } - } - - //first key is a string, params need to be converted into an object - //first go into - else { - foreach ($params as $paramkey => &$param) { - $this->convertXmlrpcParams($param); - } - $params = (object) $params; - } - } - } - -} - - -?> diff --git a/webservice/xmlrpc/locallib.php b/webservice/xmlrpc/locallib.php new file mode 100644 index 0000000000..38afe3bcb0 --- /dev/null +++ b/webservice/xmlrpc/locallib.php @@ -0,0 +1,43 @@ +. + +/** + * XML-RPC web service implementation classes and methods. + * + * @package webservice + * @copyright 2009 Moodle Pty Ltd (http://moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once("$CFG->dirroot/webservice/lib.php"); + +/** + * XML-RPC service server implementation. + * @author Petr Skoda (skodak) + */ +class webservice_xmlrpc_server extends webservice_zend_server { + /** + * Contructor + */ + public function __construct() { + parent::__construct('Zend_XmlRpc_Server'); + $this->wsname = 'xmlrpc'; + } + + +} + diff --git a/webservice/xmlrpc/server.php b/webservice/xmlrpc/server.php index 427f8b523d..0a8bde0251 100644 --- a/webservice/xmlrpc/server.php +++ b/webservice/xmlrpc/server.php @@ -1,46 +1,38 @@ . + /** - * Moodle - Modular Object-Oriented Dynamic Learning Environment - * http://moodle.com - * - * LICENSE - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details: - * - * http://www.gnu.org/copyleft/gpl.html + * XML-RPC web service entry point. The authentication is done via tokens. * - * @category Moodle * @package webservice - * @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL License - */ -/** - * Main script - XML-RPC server - * - * @author Jerome Mouneyrac - * @version 1.0 - * @package webservices + * @copyright 2009 Moodle Pty Ltd (http://moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -/* - * Zend XML-RPC server - */ -require_once(dirname(__FILE__) . '/../../config.php'); -require_once('lib.php'); +define('NO_MOODLE_COOKIES', true); + +require('../../config.php'); +require_once("$CFG->dirroot/webservice/xmlrpc/locallib.php"); -if (empty($CFG->enablewebservices)) { +if (!webservice_protocol_is_enabled('xmlrpc')) { die; } -$server = new xmlrpc_server(); -$server->run(); +$server = new webservice_xmlrpc_server(); +$server->run(false); +die; -?> \ No newline at end of file diff --git a/webservice/xmlrpc/simpleserver.php b/webservice/xmlrpc/simpleserver.php new file mode 100644 index 0000000000..2020162867 --- /dev/null +++ b/webservice/xmlrpc/simpleserver.php @@ -0,0 +1,50 @@ +. + +/** + * XML-RPC web service entry point. The authentication is done via tokens. + * + * @package webservice + * @copyright 2009 Moodle Pty Ltd (http://moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +define('NO_MOODLE_COOKIES', true); + +require('../../config.php'); +require_once("$CFG->dirroot/webservice/xmlrpc/locallib.php"); + +//ob_start(); + +//TODO: for now disable all mess in xml +ini_set('display_errors', '0'); +ini_set('log_errors', '1'); +$CFG->debug = 0; +$CFG->debugdisplay = false; + +//error_log('yy'); +//error_log(var_export($_SERVER, true)); + +if (!webservice_protocol_is_enabled('xmlrpc')) { + die; +} + +$server = new webservice_xmlrpc_server(); +$server->run(true); +die; + + diff --git a/webservice/xmlrpc/testclient/index.php b/webservice/xmlrpc/testclient/index.php new file mode 100644 index 0000000000..3126ac7cbc --- /dev/null +++ b/webservice/xmlrpc/testclient/index.php @@ -0,0 +1,109 @@ +. + +/** + * XML-RPC web service test client. + * + * @package webservice + * @copyright 2009 Moodle Pty Ltd (http://moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require('../../../config.php'); +require_once("$CFG->dirroot/webservice/testclient_forms.php"); +require_once("$CFG->dirroot/webservice/xmlrpc/locallib.php"); + +$function = optional_param('function', '', PARAM_SAFEDIR); + +$PAGE->set_url('webservice/xmlrpc/testclient/index.php'); + +require_login(); +require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); // TODO: do we need some new capability? + +$functions = array('moodle_group_get_groups'); +$functions = array_combine($functions, $functions); + +if (!isset($functions[$function])) { + $function = ''; +} + +if (!$function) { + $mform = new webservice_test_client_form(null, $functions); + echo $OUTPUT->header(); + echo $OUTPUT->heading(get_string('pluginname', 'webservice_xmlrpc')); + $mform->display(); + echo $OUTPUT->footer(); + die; +} + +$class = $function.'_form'; + +$mform = new $class(); + +if ($mform->is_cancelled()) { + redirect('index.php'); + +} else if ($data = $mform->get_data()) { + unset($data->submitbutton); + $serverurl = "$CFG->wwwroot/webservice/xmlrpc/simpleserver.php"; + $serverurl .= '?wsusername='.urlencode($data->wsusername); + unset($data->wsusername); + $serverurl .= '&wspassword='.urlencode($data->wspassword); + unset($data->wspassword); + unset($data->function); + + // now get the function parameters + $params = array(); + if ($function === 'moodle_group_get_groups') { + $params[0] = array(); + //note: this could be placed into separate function lib file in the same dir + for ($i=0; $i<10; $i++) { + if (empty($data->groupids[$i])) { + continue; + } + $params[0][] = $data->groupids[$i]; + } + } else { + die('notimplemented'); + } + + echo $OUTPUT->header(); + echo $OUTPUT->heading(get_string('pluginname', 'webservice_xmlrpc').': '.$function); + + echo 'URL: '.s($serverurl); + echo $OUTPUT->box_start(); + echo ''; + + include "Zend/Loader.php"; + Zend_Loader::registerAutoload(); + $client = new Zend_XmlRpc_Client($serverurl); + $response = $client->call($function, $params); + echo str_replace("\n", '
', s(var_export($response, true))); + + echo '
'; + echo $OUTPUT->box_end(); + $mform->display(); + echo $OUTPUT->footer(); + die; + +} else { + echo $OUTPUT->header(); + echo $OUTPUT->heading(get_string('pluginname', 'webservice_xmlrpc').': '.$function); + $mform->display(); + echo $OUTPUT->footer(); + die; +} \ No newline at end of file diff --git a/webservice/xmlrpc/testclient/zend_xmlrpc_client.php b/webservice/xmlrpc/testclient/zend_xmlrpc_client.php deleted file mode 100644 index 7b09b8f8a5..0000000000 --- a/webservice/xmlrpc/testclient/zend_xmlrpc_client.php +++ /dev/null @@ -1,70 +0,0 @@ -wwwroot."/webservice/xmlrpc/server.php"); -$params = new stdClass(); -$params->username = 'wsuser'; -$params->password = 'wspassword'; -$token = $client->call('authentication.get_token', $params); -var_dump($token); - -//2. test functions -$client = new Zend_XmlRpc_Client($CFG->wwwroot."/webservice/xmlrpc/server.php?classpath=user&token=".$token); -$params = new stdClass(); -$params->search = "admin"; -var_dump($users = $client->call('user.get_users', $params)); -print "

\n"; -$user = new stdClass(); -$user->password = "password6"; -$user->email = "mockuser6@mockuser6.com"; -$user->username = "mockuser66"; -$user->firstname = "firstname6"; -$user->lastname = "lastname6"; -$params = new stdClass(); -$params->users = array($user); -var_dump($users = $client->call('user.create_users', $params)); -print "

\n"; -$usertoupdate = new stdClass(); -$usertoupdate->email = "mockuser6@mockuser6.com"; -$usertoupdate->username = "mockuser66"; -$usertoupdate->newusername = 'mockuser6b'; -$usertoupdate->firstname = "firstname6b"; -$params = new stdClass(); -$params->users = array($usertoupdate); -var_dump($users = $client->call('user.update_users', $params)); -print "

\n"; -$params = new stdClass(); -$params->usernames = array("mockuser6b"); -var_dump($users = $client->call('user.delete_users', $params)); diff --git a/webservice/xmlrpc/version.php b/webservice/xmlrpc/version.php new file mode 100644 index 0000000000..f15e31b9c5 --- /dev/null +++ b/webservice/xmlrpc/version.php @@ -0,0 +1,3 @@ +version = 2009101900; -- 2.39.5