3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Web service test client.
22 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
23 * @author Petr Skoda (skodak)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require('../../config.php');
28 require_once($CFG->libdir
.'/adminlib.php');
29 require_once("$CFG->libdir/externallib.php");
30 require_once($CFG->dirroot
. "/" . $CFG->admin
. "/webservice/testclient_forms.php");
32 $function = optional_param('function', '', PARAM_SAFEDIR
);
33 $protocol = optional_param('protocol', '', PARAM_SAFEDIR
);
34 $authmethod = optional_param('authmethod', '', PARAM_SAFEDIR
);
36 $PAGE->set_url('/' . $CFG->admin
. '/webservice/testclient.php');
37 $PAGE->navbar
->ignore_active(true);
38 $PAGE->navbar
->add(get_string('administrationsite'));
39 $PAGE->navbar
->add(get_string('development', 'admin'));
40 $PAGE->navbar
->add(get_string('testclient', 'webservice'),
41 new moodle_url('/' . $CFG->admin
. '/webservice/testclient.php'));
42 if (!empty($function)) {
43 $PAGE->navbar
->add($function);
46 admin_externalpage_setup('testclient');
48 // list of all available functions for testing
49 $allfunctions = $DB->get_records('external_functions', array(), 'name ASC');
51 foreach ($allfunctions as $f) {
52 $finfo = external_function_info($f);
53 if (!empty($finfo->testclientpath
) and file_exists($CFG->dirroot
.'/'.$finfo->testclientpath
)) {
54 //some plugins may want to have own test client forms
55 include_once($CFG->dirroot
.'/'.$finfo->testclientpath
);
57 $class = $f->name
.'_form';
58 if (class_exists($class)) {
59 $functions[$f->name
] = $f->name
;
64 // whitelisting security
65 if (!isset($functions[$function])) {
69 // list all enabled webservices
70 $available_protocols = get_plugin_list('webservice');
71 $active_protocols = empty($CFG->webserviceprotocols
) ?
array() : explode(',', $CFG->webserviceprotocols
);
73 foreach ($active_protocols as $p) {
74 if (empty($available_protocols[$p])) {
77 include_once($available_protocols[$p].'/locallib.php');
78 if (!class_exists('webservice_'.$p.'_test_client')) {
79 // test client support not implemented
82 $protocols[$p] = get_string('pluginname', 'webservice_'.$p);
84 if (!isset($protocols[$protocol])) { // whitelisting security
88 if (!$function or !$protocol) {
89 $mform = new webservice_test_client_form(null, array($functions, $protocols));
90 echo $OUTPUT->header();
91 echo $OUTPUT->heading(get_string('testclient', 'webservice'));
92 echo $OUTPUT->box_start();
93 $url = new moodle_url('/' . $CFG->admin
. '/settings.php?section=debugging');
94 $atag =html_writer
::start_tag('a', array('href' => $url)).get_string('debug', 'admin').html_writer
::end_tag('a');
95 $descparams = new stdClass();
96 $descparams->atag
= $atag;
97 $descparams->mode
= get_string('debugnormal', 'admin');
98 $amfclienturl = new moodle_url('/webservice/amf/testclient/index.php');
99 $amfclientatag =html_writer
::tag('a', get_string('amftestclient', 'webservice'),
100 array('href' => $amfclienturl));
101 $descparams->amfatag
= $amfclientatag;
102 echo get_string('testclientdescription', 'webservice', $descparams);
103 echo $OUTPUT->box_end();
106 echo $OUTPUT->footer();
110 $class = $function.'_form';
112 $mform = new $class(null, array('authmethod' => $authmethod));
113 $mform->set_data(array('function'=>$function, 'protocol'=>$protocol));
115 if ($mform->is_cancelled()) {
116 redirect('testclient.php');
118 } else if ($data = $mform->get_data()) {
120 $functioninfo = external_function_info($function);
122 // first load lib of selected protocol
123 require_once("$CFG->dirroot/webservice/$protocol/locallib.php");
125 $testclientclass = "webservice_{$protocol}_test_client";
126 if (!class_exists($testclientclass)) {
127 throw new coding_exception('Missing WS test class in protocol '.$protocol);
129 $testclient = new $testclientclass();
131 $serverurl = "$CFG->wwwroot/webservice/$protocol/";
132 if ($authmethod == 'simple') {
133 $serverurl .= 'simpleserver.php';
134 $serverurl .= '?wsusername='.urlencode($data->wsusername
);
135 $serverurl .= '&wspassword='.urlencode($data->wspassword
);
136 } else if ($authmethod == 'token') {
137 $serverurl .= 'server.php';
138 $serverurl .= '?wstoken='.urlencode($data->token
);
141 // now get the function parameters
142 $params = $mform->get_params();
144 // now test the parameters, this also fixes PHP data types
145 $params = external_api
::validate_parameters($functioninfo->parameters_desc
, $params);
147 echo $OUTPUT->header();
148 echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
150 echo 'URL: '.s($serverurl);
151 echo $OUTPUT->box_start();
154 $response = $testclient->simpletest($serverurl, $function, $params);
155 echo str_replace("\n", '<br />', s(var_export($response, true)));
156 } catch (Exception
$ex) {
157 //TODO: handle exceptions and faults without exposing of the sensitive information such as debug traces!
158 echo str_replace("\n", '<br />', s($ex));
161 echo $OUTPUT->box_end();
163 echo $OUTPUT->footer();
167 echo $OUTPUT->header();
168 echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
170 echo $OUTPUT->footer();