From: skodak Date: Wed, 21 Oct 2009 18:25:00 +0000 (+0000) Subject: MDL-12886 refactored ws test client support and other minor tweaks X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=f0dafb3c858b9a061813403547dc7a78ddded364;p=moodle.git MDL-12886 refactored ws test client support and other minor tweaks --- diff --git a/lang/en_utf8/webservice.php b/lang/en_utf8/webservice.php index 028ef711ad..fadf7582fb 100644 --- a/lang/en_utf8/webservice.php +++ b/lang/en_utf8/webservice.php @@ -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'; diff --git a/lang/en_utf8/webservice_xmlrpc.php b/lang/en_utf8/webservice_xmlrpc.php index 48d1636e92..1800fd1d9a 100644 --- a/lang/en_utf8/webservice_xmlrpc.php +++ b/lang/en_utf8/webservice_xmlrpc.php @@ -1,3 +1,3 @@ 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 diff --git a/webservice/rest/locallib.php b/webservice/rest/locallib.php index b1af6b7f5f..03e3a345da 100644 --- a/webservice/rest/locallib.php +++ b/webservice/rest/locallib.php @@ -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 ''.htmlentities($returns, ENT_COMPAT, 'UTF-8').''."\n"; + if (is_null($returns)) { + return ''."\n"; + } else { + return ''.htmlentities($returns, ENT_COMPAT, 'UTF-8').''."\n"; + } } else if ($desc instanceof external_multiple_structure) { $mult = ''."\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); + } +} \ No newline at end of file diff --git a/webservice/rest/testclient/index.php b/webservice/rest/testclient/index.php deleted file mode 100644 index b5361d67fa..0000000000 --- a/webservice/rest/testclient/index.php +++ /dev/null @@ -1,100 +0,0 @@ -. - -/** - * 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 ''; - $response = download_file_content($serverurl); - echo str_replace("\n", '
', s($response)); - echo '
'; - 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; -} \ No newline at end of file diff --git a/webservice/testclient.php b/webservice/testclient.php new file mode 100644 index 0000000000..38a715ce05 --- /dev/null +++ b/webservice/testclient.php @@ -0,0 +1,139 @@ +. + +/** + * 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 ''; + + try { + $response = $testclient->simpletest($serverurl, $function, $params); + echo str_replace("\n", '
', 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", '
', s($ex)); + } + + echo '
'; + 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; +} \ No newline at end of file diff --git a/webservice/testclient_forms.php b/webservice/testclient_forms.php index e6e627a18d..ea0a6ca8d2 100644 --- a/webservice/testclient_forms.php +++ b/webservice/testclient_forms.php @@ -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')); } diff --git a/webservice/xmlrpc/locallib.php b/webservice/xmlrpc/locallib.php index 75f8c9e76a..ea4e7865d2 100644 --- a/webservice/xmlrpc/locallib.php +++ b/webservice/xmlrpc/locallib.php @@ -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); + } +} \ No newline at end of file diff --git a/webservice/xmlrpc/testclient/index.php b/webservice/xmlrpc/testclient/index.php deleted file mode 100644 index 3126ac7cbc..0000000000 --- a/webservice/xmlrpc/testclient/index.php +++ /dev/null @@ -1,109 +0,0 @@ -. - -/** - * 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