mirror of
https://github.com/moodle/moodle.git
synced 2025-04-15 13:33:52 +02:00
MDL-12886 refactored ws test client support and other minor tweaks
This commit is contained in:
parent
06f0784845
commit
f0dafb3c85
@ -40,6 +40,6 @@ $string['serviceusers'] = 'Authorized users';
|
||||
$string['serviceusersmatching'] = 'Authorized users matching';
|
||||
$string['serviceuserssettings'] = 'Change settings for the Authorized users';
|
||||
$string['test'] = 'Test';
|
||||
$string['testclient'] = 'Test client';
|
||||
$string['testclient'] = 'Web service test client';
|
||||
$string['validuntil'] = 'Valid until';
|
||||
$string['webservices'] = 'Web services';
|
||||
|
@ -1,3 +1,3 @@
|
||||
<?php
|
||||
|
||||
$string['pluginname'] = 'XMLRPC protocol';
|
||||
$string['pluginname'] = 'XML-RPC protocol';
|
||||
|
@ -37,6 +37,11 @@ class webservice_access_exception extends moodle_exception {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is protocol enabled?
|
||||
* @param string $protocol name of WS protocol
|
||||
* @return bool
|
||||
*/
|
||||
function webservice_protocol_is_enabled($protocol) {
|
||||
global $CFG;
|
||||
|
||||
@ -62,6 +67,20 @@ interface webservice_server {
|
||||
public function run($simple);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mandatory test client interface.
|
||||
*/
|
||||
interface webservice_test_client_interface {
|
||||
/**
|
||||
* Execute test client WS request
|
||||
* @param string $serverurl
|
||||
* @param string $function
|
||||
* @param array $params
|
||||
* @return mixed
|
||||
*/
|
||||
public function simpletest($serverurl, $function, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Special abstraction of our srvices that allows
|
||||
* interaction with stock Zend ws servers.
|
||||
@ -229,6 +248,7 @@ class '.$classname.' {
|
||||
'.$methods.'
|
||||
}
|
||||
';
|
||||
|
||||
// load the virtual class definition into memory
|
||||
eval($code);
|
||||
$this->service_class = $classname;
|
||||
@ -506,7 +526,6 @@ abstract class webservice_base_server implements webservice_server {
|
||||
*/
|
||||
abstract protected function send_error($ex=null);
|
||||
|
||||
|
||||
/**
|
||||
* Process request from client.
|
||||
* @param bool $simple use simple authentication
|
||||
|
@ -122,8 +122,11 @@ class webservice_rest_server extends webservice_base_server {
|
||||
return '';
|
||||
|
||||
} else if ($desc instanceof external_value) {
|
||||
//TODO: there should be some way to indicate the real NULL value
|
||||
return '<VALUE>'.htmlentities($returns, ENT_COMPAT, 'UTF-8').'</VALUE>'."\n";
|
||||
if (is_null($returns)) {
|
||||
return '<VALUE null="null"/>'."\n";
|
||||
} else {
|
||||
return '<VALUE>'.htmlentities($returns, ENT_COMPAT, 'UTF-8').'</VALUE>'."\n";
|
||||
}
|
||||
|
||||
} else if ($desc instanceof external_multiple_structure) {
|
||||
$mult = '<MULTIPLE>'."\n";
|
||||
@ -153,3 +156,19 @@ class webservice_rest_server extends webservice_base_server {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* REST test client class
|
||||
*/
|
||||
class webservice_rest_test_client implements webservice_test_client_interface {
|
||||
/**
|
||||
* Execute test client WS request
|
||||
* @param string $serverurl
|
||||
* @param string $function
|
||||
* @param array $params
|
||||
* @return mixed
|
||||
*/
|
||||
public function simpletest($serverurl, $function, $params) {
|
||||
return download_file_content($serverurl.'&wsfunction='.$function, null, $params);
|
||||
}
|
||||
}
|
@ -1,100 +0,0 @@
|
||||
<?php
|
||||
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle 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.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* REST 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/rest/locallib.php");
|
||||
|
||||
$function = optional_param('function', '', PARAM_SAFEDIR);
|
||||
|
||||
$PAGE->set_url('webservice/rest/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_rest'));
|
||||
$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/rest/simpleserver.php";
|
||||
$serverurl .= '?wsusername='.urlencode($data->wsusername);
|
||||
unset($data->wsusername);
|
||||
$serverurl .= '&wspassword='.urlencode($data->wspassword);
|
||||
unset($data->wspassword);
|
||||
$serverurl .= '&wsfunction='.urlencode($data->function);
|
||||
unset($data->function);
|
||||
|
||||
if ($function === 'moodle_group_get_groups') {
|
||||
//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;
|
||||
}
|
||||
$serverurl .= "&groupids[$i]=".urlencode($data->groupids[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading(get_string('pluginname', 'webservice_rest').': '.$function);
|
||||
|
||||
echo 'URL: '.s($serverurl);
|
||||
echo $OUTPUT->box_start();
|
||||
echo '<code>';
|
||||
$response = download_file_content($serverurl);
|
||||
echo str_replace("\n", '<br />', s($response));
|
||||
echo '</code>';
|
||||
echo $OUTPUT->box_end();
|
||||
$mform->display();
|
||||
echo $OUTPUT->footer();
|
||||
die;
|
||||
|
||||
} else {
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading(get_string('pluginname', 'webservice_rest').': '.$function);
|
||||
$mform->display();
|
||||
echo $OUTPUT->footer();
|
||||
die;
|
||||
}
|
139
webservice/testclient.php
Normal file
139
webservice/testclient.php
Normal file
@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle 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.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* 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");
|
||||
|
||||
$function = optional_param('function', '', PARAM_SAFEDIR);
|
||||
$protocol = optional_param('protocol', '', PARAM_SAFEDIR);
|
||||
|
||||
$PAGE->set_url('webservice/testclient.php');
|
||||
|
||||
require_login();
|
||||
require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); // TODO: do we need some new capability?
|
||||
|
||||
// list of all available functions for testing - please note there must be explicit
|
||||
// support for testing of each functions, the parameter conversion and form is hardcoded
|
||||
$functions = array('moodle_group_get_groups');
|
||||
$functions = array_combine($functions, $functions);
|
||||
if (!isset($functions[$function])) { // whitelisting security
|
||||
$function = '';
|
||||
}
|
||||
|
||||
// list all enabled webservices
|
||||
$available_protocols = get_plugin_list('webservice');
|
||||
$active_protocols = empty($CFG->webserviceprotocols) ? array() : explode(',', $CFG->webserviceprotocols);
|
||||
$protocols = array();
|
||||
foreach ($active_protocols as $p) {
|
||||
if (empty($available_protocols[$p])) {
|
||||
continue;
|
||||
}
|
||||
$protocols[$p] = get_string('pluginname', 'webservice_'.$p);
|
||||
}
|
||||
if (!isset($protocols[$protocol])) { // whitelisting security
|
||||
$protocol = '';
|
||||
}
|
||||
|
||||
if (!$function or !$protocol) {
|
||||
$mform = new webservice_test_client_form(null, array($functions, $protocols));
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading(get_string('testclient', 'webservice'));
|
||||
$mform->display();
|
||||
echo $OUTPUT->footer();
|
||||
die;
|
||||
}
|
||||
|
||||
$class = $function.'_form';
|
||||
|
||||
$mform = new $class();
|
||||
$mform->set_data(array('function'=>$function, 'protocol'=>$protocol));
|
||||
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect('testclient.php');
|
||||
|
||||
} else if ($data = $mform->get_data()) {
|
||||
// remove unused from form data
|
||||
unset($data->submitbutton);
|
||||
unset($data->protocol);
|
||||
unset($data->function);
|
||||
|
||||
// first load lib of selected protocol
|
||||
require_once("$CFG->dirroot/webservice/$protocol/locallib.php");
|
||||
|
||||
$testclientclass = "webservice_{$protocol}_test_client";
|
||||
if (!class_exists($testclientclass)) {
|
||||
throw new coding_exception('Missing WS test class in protocol '.$protocol);
|
||||
}
|
||||
$testclient = new $testclientclass();
|
||||
|
||||
$serverurl = "$CFG->wwwroot/webservice/$protocol/simpleserver.php";
|
||||
$serverurl .= '?wsusername='.urlencode($data->wsusername);
|
||||
unset($data->wsusername);
|
||||
$serverurl .= '&wspassword='.urlencode($data->wspassword);
|
||||
unset($data->wspassword);
|
||||
|
||||
// now get the function parameters - each functions processing must be hardcoded here
|
||||
$params = array();
|
||||
if ($function === 'moodle_group_get_groups') {
|
||||
$params['groupids'] = array();
|
||||
for ($i=0; $i<10; $i++) {
|
||||
if (empty($data->groupids[$i])) {
|
||||
continue;
|
||||
}
|
||||
$params['groupids'][] = $data->groupids[$i];
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new coding_exception('Testing of function '.$function.' not implemented yet!');
|
||||
}
|
||||
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
|
||||
|
||||
echo 'URL: '.s($serverurl);
|
||||
echo $OUTPUT->box_start();
|
||||
echo '<code>';
|
||||
|
||||
try {
|
||||
$response = $testclient->simpletest($serverurl, $function, $params);
|
||||
echo str_replace("\n", '<br />', s(var_export($response, true)));
|
||||
} catch (Exception $ex) {
|
||||
//TODO: handle exceptions and faults without exposing of the sensitive information such as debug traces!
|
||||
echo str_replace("\n", '<br />', s($ex));
|
||||
}
|
||||
|
||||
echo '</code>';
|
||||
echo $OUTPUT->box_end();
|
||||
$mform->display();
|
||||
echo $OUTPUT->footer();
|
||||
die;
|
||||
|
||||
} else {
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
|
||||
$mform->display();
|
||||
echo $OUTPUT->footer();
|
||||
die;
|
||||
}
|
@ -8,10 +8,12 @@ class webservice_test_client_form extends moodleform {
|
||||
global $CFG;
|
||||
|
||||
$mform = $this->_form;
|
||||
$functions = $this->_customdata;
|
||||
list($functions, $protocols) = $this->_customdata;
|
||||
|
||||
$mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
|
||||
|
||||
$mform->addElement('select', 'protocol', get_string('protocol', 'webservice'), $protocols);
|
||||
|
||||
$mform->addElement('select', 'function', get_string('function', 'webservice'), $functions);
|
||||
|
||||
$this->add_action_buttons(false, get_string('select'));
|
||||
@ -36,7 +38,9 @@ class moodle_group_get_groups_form extends moodleform {
|
||||
|
||||
$mform->addElement('hidden', 'function');
|
||||
$mform->setType('function', PARAM_SAFEDIR);
|
||||
$mform->setDefault('function', 'moodle_group_get_groups');
|
||||
|
||||
$mform->addElement('hidden', 'protocol');
|
||||
$mform->setType('protocol', PARAM_SAFEDIR);
|
||||
|
||||
$this->add_action_buttons(true, get_string('test', 'webservice'));
|
||||
}
|
||||
|
@ -39,3 +39,24 @@ class webservice_xmlrpc_server extends webservice_zend_server {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XML-RPC test client class
|
||||
*/
|
||||
class webservice_xmlrpc_test_client implements webservice_test_client_interface {
|
||||
/**
|
||||
* Execute test client WS request
|
||||
* @param string $serverurl
|
||||
* @param string $function
|
||||
* @param array $params
|
||||
* @return mixed
|
||||
*/
|
||||
public function simpletest($serverurl, $function, $params) {
|
||||
//zend expects 0 based array with numeric indexes
|
||||
$params = array_values($params);
|
||||
|
||||
include "Zend/Loader.php";
|
||||
Zend_Loader::registerAutoload();
|
||||
$client = new Zend_XmlRpc_Client($serverurl);
|
||||
return $client->call($function, $params);
|
||||
}
|
||||
}
|
@ -1,109 +0,0 @@
|
||||
<?php
|
||||
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle 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.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* 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 '<code>';
|
||||
|
||||
include "Zend/Loader.php";
|
||||
Zend_Loader::registerAutoload();
|
||||
$client = new Zend_XmlRpc_Client($serverurl);
|
||||
$response = $client->call($function, $params);
|
||||
echo str_replace("\n", '<br />', s(var_export($response, true)));
|
||||
|
||||
echo '</code>';
|
||||
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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user