Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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  // Allow only functions available for testing.
  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  
  85  // Allow only protocols supporting the test client.
  86  if (!isset($protocols[$protocol])) {
  87      $protocol = '';
  88  }
  89  
  90  if (!$function or !$protocol) {
  91      $mform = new webservice_test_client_form(null, array($functions, $protocols));
  92      echo $OUTPUT->header();
  93      echo $OUTPUT->heading(get_string('testclient', 'webservice'));
  94      echo $OUTPUT->box_start();
  95      $url = new moodle_url('/' . $CFG->admin . '/settings.php?section=debugging');
  96      $atag =html_writer::start_tag('a', array('href' => $url)).get_string('debug', 'admin').html_writer::end_tag('a');
  97      $descparams = new stdClass();
  98      $descparams->atag = $atag;
  99      $descparams->mode = get_string('debugnormal', 'admin');
 100      echo get_string('testclientdescription', 'webservice', $descparams);
 101      echo $OUTPUT->box_end();
 102  
 103      $mform->display();
 104      echo $OUTPUT->footer();
 105      die;
 106  }
 107  
 108  $class = $function.'_testclient_form';
 109  
 110  $mform = new $class(null, array('authmethod' => $authmethod));
 111  $mform->set_data(array('function'=>$function, 'protocol'=>$protocol));
 112  
 113  if ($mform->is_cancelled()) {
 114      redirect('testclient.php');
 115  
 116  } else if ($data = $mform->get_data()) {
 117  
 118      $functioninfo = external_api::external_function_info($function);
 119  
 120      // first load lib of selected protocol
 121      require_once("$CFG->dirroot/webservice/$protocol/locallib.php");
 122  
 123      $testclientclass = "webservice_{$protocol}_test_client";
 124      if (!class_exists($testclientclass)) {
 125          throw new coding_exception('Missing WS test class in protocol '.$protocol);
 126      }
 127      $testclient = new $testclientclass();
 128  
 129      $serverurl = "$CFG->wwwroot/webservice/$protocol/";
 130      if ($authmethod == 'simple') {
 131          $serverurl .= 'simpleserver.php';
 132          $serverurl .= '?wsusername='.urlencode($data->wsusername);
 133          $serverurl .= '&wspassword='.urlencode($data->wspassword);
 134      } else if ($authmethod == 'token') {
 135          $serverurl .= 'server.php';
 136          $serverurl .= '?wstoken='.urlencode($data->token);
 137      }
 138  
 139      // now get the function parameters
 140      $params = $mform->get_params();
 141  
 142      // now test the parameters, this also fixes PHP data types
 143      $params = external_api::validate_parameters($functioninfo->parameters_desc, $params);
 144  
 145      echo $OUTPUT->header();
 146      echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
 147  
 148      echo 'URL: '.s($serverurl);
 149      echo $OUTPUT->box_start();
 150  
 151      try {
 152          $response = $testclient->simpletest($serverurl, $function, $params);
 153          echo str_replace("\n", '<br />', s(var_export($response, true)));
 154      } catch (Exception $ex) {
 155          //TODO: handle exceptions and faults without exposing of the sensitive information such as debug traces!
 156          echo str_replace("\n", '<br />', s($ex));
 157      }
 158  
 159      echo $OUTPUT->box_end();
 160      $mform->display();
 161      echo $OUTPUT->footer();
 162      die;
 163  
 164  } else {
 165      echo $OUTPUT->header();
 166      echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
 167      $mform->display();
 168      echo $OUTPUT->footer();
 169      die;
 170  }