. /** * Web service test client. * * @package webservice * @copyright 2009 Moodle Pty Ltd (http://moodle.com) * @author Petr Skoda (skodak) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require('../config.php'); require_once("$CFG->libdir/externallib.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 $allfunctions = $DB->get_records('external_functions', array(), 'name ASC'); $functions = array(); foreach ($allfunctions as $f) { $finfo = external_function_info($f); if (!empty($finfo->testclientpath) and file_exists($CFG->dirroot.'/'.$finfo->testclientpath)) { //some plugins may want to have own test client forms include_once($CFG->dirroot.'/'.$finfo->testclientpath); } $class = $f->name.'_form'; if (class_exists($class)) { $functions[$f->name] = $f->name; continue; } } // whitelisting security if (!isset($functions[$function])) { $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; } include_once($available_protocols[$p].'/locallib.php'); if (!class_exists('webservice_'.$p.'_test_client')) { // test client support not implemented 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()) { $functioninfo = external_function_info($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); $serverurl .= '&wspassword='.urlencode($data->wspassword); // now get the function parameters $params = $mform->get_params(); // now test the parameters, this also fixes PHP data types $params = external_api::validate_parameters($functioninfo->parameters_desc, $params); 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; }