1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
// This file is part of Moodle - http://moodle.org/
|
|
|
4 |
//
|
|
|
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.
|
|
|
9 |
//
|
|
|
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.
|
|
|
14 |
//
|
|
|
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/>.
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Web service test client.
|
|
|
20 |
*
|
|
|
21 |
* @package webservice
|
|
|
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
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
require('../../config.php');
|
|
|
28 |
require_once($CFG->libdir.'/adminlib.php');
|
|
|
29 |
require_once($CFG->dirroot . "/" . $CFG->admin . "/webservice/testclient_forms.php");
|
|
|
30 |
|
|
|
31 |
$function = optional_param('function', '', PARAM_PLUGIN);
|
|
|
32 |
$protocol = optional_param('protocol', '', PARAM_ALPHA);
|
|
|
33 |
$authmethod = optional_param('authmethod', '', PARAM_ALPHA);
|
|
|
34 |
|
|
|
35 |
$PAGE->set_url('/' . $CFG->admin . '/webservice/testclient.php');
|
|
|
36 |
$PAGE->navbar->ignore_active(true);
|
|
|
37 |
$PAGE->navbar->add(get_string('administrationsite'));
|
|
|
38 |
$PAGE->navbar->add(get_string('development', 'admin'));
|
|
|
39 |
$PAGE->navbar->add(get_string('testclient', 'webservice'),
|
|
|
40 |
new moodle_url('/' . $CFG->admin . '/webservice/testclient.php'));
|
|
|
41 |
if (!empty($function)) {
|
|
|
42 |
$PAGE->navbar->add($function);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
admin_externalpage_setup('testclient');
|
|
|
46 |
|
|
|
47 |
// list of all available functions for testing
|
|
|
48 |
$allfunctions = $DB->get_records('external_functions', array(), 'name ASC');
|
|
|
49 |
$functions = array();
|
|
|
50 |
foreach ($allfunctions as $f) {
|
|
|
51 |
$finfo = \core_external\external_api::external_function_info($f);
|
|
|
52 |
if (!empty($finfo->testclientpath) and file_exists($CFG->dirroot.'/'.$finfo->testclientpath)) {
|
|
|
53 |
//some plugins may want to have own test client forms
|
|
|
54 |
include_once($CFG->dirroot.'/'.$finfo->testclientpath);
|
|
|
55 |
}
|
|
|
56 |
$class = $f->name.'_testclient_form';
|
|
|
57 |
if (class_exists($class)) {
|
|
|
58 |
$functions[$f->name] = $f->name;
|
|
|
59 |
continue;
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// Allow only functions available for testing.
|
|
|
64 |
if (!isset($functions[$function])) {
|
|
|
65 |
$function = '';
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
// list all enabled webservices
|
|
|
69 |
$available_protocols = core_component::get_plugin_list('webservice');
|
|
|
70 |
$active_protocols = empty($CFG->webserviceprotocols) ? array() : explode(',', $CFG->webserviceprotocols);
|
|
|
71 |
$protocols = array();
|
|
|
72 |
foreach ($active_protocols as $p) {
|
|
|
73 |
if (empty($available_protocols[$p])) {
|
|
|
74 |
continue;
|
|
|
75 |
}
|
|
|
76 |
include_once($available_protocols[$p].'/locallib.php');
|
|
|
77 |
if (!class_exists('webservice_'.$p.'_test_client')) {
|
|
|
78 |
// test client support not implemented
|
|
|
79 |
continue;
|
|
|
80 |
}
|
|
|
81 |
$protocols[$p] = get_string('pluginname', 'webservice_'.$p);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
// Allow only protocols supporting the test client.
|
|
|
85 |
if (!isset($protocols[$protocol])) {
|
|
|
86 |
$protocol = '';
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
if (!$function or !$protocol) {
|
|
|
90 |
$mform = new webservice_test_client_form(null, array($functions, $protocols));
|
|
|
91 |
echo $OUTPUT->header();
|
|
|
92 |
echo $OUTPUT->heading(get_string('testclient', 'webservice'));
|
|
|
93 |
echo $OUTPUT->box_start();
|
|
|
94 |
$url = new moodle_url('/' . $CFG->admin . '/settings.php?section=debugging');
|
|
|
95 |
$atag =html_writer::start_tag('a', array('href' => $url)).get_string('debug', 'admin').html_writer::end_tag('a');
|
|
|
96 |
$descparams = new stdClass();
|
|
|
97 |
$descparams->atag = $atag;
|
|
|
98 |
$descparams->mode = get_string('debugnormal', 'admin');
|
|
|
99 |
echo get_string('testclientdescription', 'webservice', $descparams);
|
|
|
100 |
echo $OUTPUT->box_end();
|
|
|
101 |
|
|
|
102 |
$mform->display();
|
|
|
103 |
echo $OUTPUT->footer();
|
|
|
104 |
die;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
$class = $function.'_testclient_form';
|
|
|
108 |
|
|
|
109 |
$mform = new $class(null, array('authmethod' => $authmethod));
|
|
|
110 |
$mform->set_data(array('function'=>$function, 'protocol'=>$protocol));
|
|
|
111 |
|
|
|
112 |
if ($mform->is_cancelled()) {
|
|
|
113 |
redirect('testclient.php');
|
|
|
114 |
|
|
|
115 |
} else if ($data = $mform->get_data()) {
|
|
|
116 |
|
|
|
117 |
$functioninfo = \core_external\external_api::external_function_info($function);
|
|
|
118 |
|
|
|
119 |
// first load lib of selected protocol
|
|
|
120 |
require_once("$CFG->dirroot/webservice/$protocol/locallib.php");
|
|
|
121 |
|
|
|
122 |
$testclientclass = "webservice_{$protocol}_test_client";
|
|
|
123 |
if (!class_exists($testclientclass)) {
|
|
|
124 |
throw new coding_exception('Missing WS test class in protocol '.$protocol);
|
|
|
125 |
}
|
|
|
126 |
$testclient = new $testclientclass();
|
|
|
127 |
|
|
|
128 |
$serverurl = "$CFG->wwwroot/webservice/$protocol/";
|
|
|
129 |
if ($authmethod == 'simple') {
|
|
|
130 |
$serverurl .= 'simpleserver.php';
|
|
|
131 |
$serverurl .= '?wsusername='.urlencode($data->wsusername);
|
|
|
132 |
$serverurl .= '&wspassword='.urlencode($data->wspassword);
|
|
|
133 |
} else if ($authmethod == 'token') {
|
|
|
134 |
$serverurl .= 'server.php';
|
|
|
135 |
$serverurl .= '?wstoken='.urlencode($data->token);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
// now get the function parameters
|
|
|
139 |
$params = $mform->get_params();
|
|
|
140 |
|
|
|
141 |
// now test the parameters, this also fixes PHP data types
|
|
|
142 |
$params = \core_external\external_api::validate_parameters($functioninfo->parameters_desc, $params);
|
|
|
143 |
|
|
|
144 |
echo $OUTPUT->header();
|
|
|
145 |
echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
|
|
|
146 |
|
|
|
147 |
echo 'URL: '.s($serverurl);
|
|
|
148 |
echo $OUTPUT->box_start();
|
|
|
149 |
|
|
|
150 |
try {
|
|
|
151 |
$response = $testclient->simpletest($serverurl, $function, $params);
|
|
|
152 |
echo str_replace("\n", '<br />', s(var_export($response, true)));
|
|
|
153 |
} catch (Exception $ex) {
|
|
|
154 |
//TODO: handle exceptions and faults without exposing of the sensitive information such as debug traces!
|
|
|
155 |
echo str_replace("\n", '<br />', s($ex));
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
echo $OUTPUT->box_end();
|
|
|
159 |
$mform->display();
|
|
|
160 |
echo $OUTPUT->footer();
|
|
|
161 |
die;
|
|
|
162 |
|
|
|
163 |
} else {
|
|
|
164 |
echo $OUTPUT->header();
|
|
|
165 |
echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
|
|
|
166 |
$mform->display();
|
|
|
167 |
echo $OUTPUT->footer();
|
|
|
168 |
die;
|
|
|
169 |
}
|