Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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->libdir/externallib.php");
  30  require_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'));
  42  if (!empty($function)) {
  43      $PAGE->navbar->add($function);
  44  }
  45  
  46  admin_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();
  51  foreach ($allfunctions as $f) {
  52      $finfo = external_api::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.'_testclient_form';
  58      if (class_exists($class)) {
  59          $functions[$f->name] = $f->name;
  60          continue;
  61      }
  62  }
  63  
  64  // whitelisting security
  65  if (!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();
  73  foreach ($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  }
  84  if (!isset($protocols[$protocol])) { // whitelisting security
  85      $protocol = '';
  86  }
  87  
  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      echo get_string('testclientdescription', 'webservice', $descparams);
  99      echo $OUTPUT->box_end();
 100  
 101      $mform->display();
 102      echo $OUTPUT->footer();
 103      die;
 104  }
 105  
 106  $class = $function.'_testclient_form';
 107  
 108  $mform = new $class(null, array('authmethod' => $authmethod));
 109  $mform->set_data(array('function'=>$function, 'protocol'=>$protocol));
 110  
 111  if ($mform->is_cancelled()) {
 112      redirect('testclient.php');
 113  
 114  } else if ($data = $mform->get_data()) {
 115  
 116      $functioninfo = external_api::external_function_info($function);
 117  
 118      // first load lib of selected protocol
 119      require_once("$CFG->dirroot/webservice/$protocol/locallib.php");
 120  
 121      $testclientclass = "webservice_{$protocol}_test_client";
 122      if (!class_exists($testclientclass)) {
 123          throw new coding_exception('Missing WS test class in protocol '.$protocol);
 124      }
 125      $testclient = new $testclientclass();
 126  
 127      $serverurl = "$CFG->wwwroot/webservice/$protocol/";
 128      if ($authmethod == 'simple') {
 129          $serverurl .= 'simpleserver.php';
 130          $serverurl .= '?wsusername='.urlencode($data->wsusername);
 131          $serverurl .= '&wspassword='.urlencode($data->wspassword);
 132      } else if ($authmethod == 'token') {
 133          $serverurl .= 'server.php';
 134          $serverurl .= '?wstoken='.urlencode($data->token);
 135      }
 136  
 137      // now get the function parameters
 138      $params = $mform->get_params();
 139  
 140      // now test the parameters, this also fixes PHP data types
 141      $params = external_api::validate_parameters($functioninfo->parameters_desc, $params);
 142  
 143      echo $OUTPUT->header();
 144      echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
 145  
 146      echo 'URL: '.s($serverurl);
 147      echo $OUTPUT->box_start();
 148  
 149      try {
 150          $response = $testclient->simpletest($serverurl, $function, $params);
 151          echo str_replace("\n", '<br />', s(var_export($response, true)));
 152      } catch (Exception $ex) {
 153          //TODO: handle exceptions and faults without exposing of the sensitive information such as debug traces!
 154          echo str_replace("\n", '<br />', s($ex));
 155      }
 156  
 157      echo $OUTPUT->box_end();
 158      $mform->display();
 159      echo $OUTPUT->footer();
 160      die;
 161  
 162  } else {
 163      echo $OUTPUT->header();
 164      echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
 165      $mform->display();
 166      echo $OUTPUT->footer();
 167      die;
 168  }