source: moodle/trunk/fuentes/admin/webservice/testclient.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 6.1 KB
Line 
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
27require('../../config.php');
28require_once($CFG->libdir.'/adminlib.php');
29require_once("$CFG->libdir/externallib.php");
30require_once($CFG->dirroot . "/" . $CFG->admin . "/webservice/testclient_forms.php");
31
32$function = optional_param('function', '', PARAM_PLUGIN);
33$protocol = optional_param('protocol', '', PARAM_ALPHA);
34$authmethod = optional_param('authmethod', '', PARAM_ALPHA);
35
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'));
42if (!empty($function)) {
43    $PAGE->navbar->add($function);
44}
45
46admin_externalpage_setup('testclient');
47
48// list of all available functions for testing
49$allfunctions = $DB->get_records('external_functions', array(), 'name ASC');
50$functions = array();
51foreach ($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);
56    }
57    $class = $f->name.'_form';
58    if (class_exists($class)) {
59        $functions[$f->name] = $f->name;
60        continue;
61    }
62}
63
64// whitelisting security
65if (!isset($functions[$function])) {
66    $function = '';
67}
68
69// list all enabled webservices
70$available_protocols = core_component::get_plugin_list('webservice');
71$active_protocols = empty($CFG->webserviceprotocols) ? array() : explode(',', $CFG->webserviceprotocols);
72$protocols = array();
73foreach ($active_protocols as $p) {
74    if (empty($available_protocols[$p])) {
75        continue;
76    }
77    include_once($available_protocols[$p].'/locallib.php');
78    if (!class_exists('webservice_'.$p.'_test_client')) {
79        // test client support not implemented
80        continue;
81    }
82    $protocols[$p] = get_string('pluginname', 'webservice_'.$p);
83}
84if (!isset($protocols[$protocol])) { // whitelisting security
85    $protocol = '';
86}
87
88if (!$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();
104
105    $mform->display();
106    echo $OUTPUT->footer();
107    die;
108}
109
110$class = $function.'_form';
111
112$mform = new $class(null, array('authmethod' => $authmethod));
113$mform->set_data(array('function'=>$function, 'protocol'=>$protocol));
114
115if ($mform->is_cancelled()) {
116    redirect('testclient.php');
117
118} else if ($data = $mform->get_data()) {
119
120    $functioninfo = external_function_info($function);
121
122    // first load lib of selected protocol
123    require_once("$CFG->dirroot/webservice/$protocol/locallib.php");
124
125    $testclientclass = "webservice_{$protocol}_test_client";
126    if (!class_exists($testclientclass)) {
127        throw new coding_exception('Missing WS test class in protocol '.$protocol);
128    }
129    $testclient = new $testclientclass();
130
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);
139    }
140
141    // now get the function parameters
142    $params = $mform->get_params();
143
144    // now test the parameters, this also fixes PHP data types
145    $params = external_api::validate_parameters($functioninfo->parameters_desc, $params);
146
147    echo $OUTPUT->header();
148    echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
149
150    echo 'URL: '.s($serverurl);
151    echo $OUTPUT->box_start();
152
153    try {
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));
159    }
160
161    echo $OUTPUT->box_end();
162    $mform->display();
163    echo $OUTPUT->footer();
164    die;
165
166} else {
167    echo $OUTPUT->header();
168    echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
169    $mform->display();
170    echo $OUTPUT->footer();
171    die;
172}
Note: See TracBrowser for help on using the repository browser.