[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/mnet/ -> testclient.php (source)

   1  <?php
   2  /**
   3   * A service browser for remote Moodles
   4   *
   5   * This script 'remotely' executes the reflection methods on a remote Moodle,
   6   * and publishes the details of the available services
   7   *
   8   * @package    core
   9   * @subpackage mnet
  10   * @author  Donal McMullan  donal@catalyst.net.nz
  11   * @version 0.0.1
  12   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  13   * @package mnet
  14   */
  15  require(__DIR__.'/../../config.php');
  16  require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';
  17  require_once($CFG->libdir.'/adminlib.php');
  18  include_once($CFG->dirroot.'/mnet/lib.php');
  19  
  20  if ($CFG->mnet_dispatcher_mode === 'off') {
  21      print_error('mnetdisabled', 'mnet');
  22  }
  23  
  24  require_login();
  25  admin_externalpage_setup('mnettestclient');
  26  
  27  $context = context_system::instance();
  28  require_capability('moodle/site:config', $context);
  29  
  30  error_reporting(DEBUG_ALL);
  31  
  32  echo $OUTPUT->header();
  33  if (!extension_loaded('openssl')) {
  34      print_error('requiresopenssl', 'mnet', '', NULL, true);
  35  }
  36  
  37  // optional drilling down parameters
  38  $hostid = optional_param('hostid', 0, PARAM_INT);
  39  $servicename = optional_param('servicename', '', PARAM_SAFEDIR);
  40  $methodid = optional_param('method', 0, PARAM_INT);
  41  
  42  $hosts = $DB->get_records('mnet_host');
  43  $moodleapplicationid = $DB->get_field('mnet_application', 'id', array('name' => 'moodle'));
  44  
  45  $url = new moodle_url('/admin/mnet/testclient.php');
  46  $PAGE->set_url($url);
  47  
  48  echo $OUTPUT->heading(get_string('hostlist', 'mnet'));
  49  foreach ($hosts as $id => $host) {
  50      if (empty($host->wwwroot) || $host->wwwroot == $CFG->wwwroot) {
  51          continue;
  52      }
  53      $newurl = new moodle_url($url, array('hostid' => $host->id));
  54      echo '<p>' . html_writer::link($newurl, $host->wwwroot) . '</p>';
  55  }
  56  
  57  if (!empty($hostid) && array_key_exists($hostid, $hosts)) {
  58      $host = $hosts[$hostid];
  59      if ($host->applicationid != $moodleapplicationid) {
  60          echo $OUTPUT->notification(get_string('notmoodleapplication', 'mnet'));
  61      }
  62      $mnet_peer = new mnet_peer();
  63      $mnet_peer->set_wwwroot($host->wwwroot);
  64  
  65      $mnet_request = new mnet_xmlrpc_client();
  66  
  67      $mnet_request->set_method('system/listServices');
  68      $mnet_request->send($mnet_peer);
  69  
  70      $services = $mnet_request->response;
  71      $yesno = array('No', 'Yes');
  72      $servicenames = array();
  73  
  74      echo $OUTPUT->heading(get_string('servicesavailableonhost', 'mnet', $host->wwwroot));
  75  
  76      if (!empty($mnet_request->error)) {
  77          echo $OUTPUT->heading(get_string('error'), 3);
  78          echo html_writer::alist($mnet_request->error);
  79          $services = array();
  80      }
  81  
  82      $table = new html_table();
  83      $table->head = array(
  84          get_string('serviceid', 'mnet'),
  85          get_string('service', 'mnet'),
  86          get_string('version', 'mnet'),
  87          get_string('theypublish', 'mnet'),
  88          get_string('theysubscribe', 'mnet'),
  89          get_string('options', 'mnet'),
  90      );
  91      $table->data = array();
  92  
  93      $yesno = array(get_string('no'), get_string('yes'));
  94  
  95      // this query is horrible and has to be remapped afterwards, because of the non-uniqueness
  96      // of the remoterep service (it has two plugins so far that use it)
  97      // it's possible to get a unique list back using a subquery with LIMIT but that would break oracle
  98      // so it's best to just do this small query and then remap the results afterwards
  99      $sql = '
 100          SELECT DISTINCT
 101              ' . $DB->sql_concat('r.plugintype', "'_'", 'r.pluginname', "'_'", 's.name')  . ' AS uniqueid,
 102               s.name,
 103               r.plugintype,
 104               r.pluginname
 105          FROM
 106              {mnet_service} s
 107         JOIN {mnet_remote_service2rpc} s2r ON s2r.serviceid = s.id
 108         JOIN {mnet_remote_rpc} r ON r.id = s2r.rpcid';
 109  
 110      $serviceinfo = array();
 111  
 112      foreach ($DB->get_records_sql($sql) as $result) {
 113          $serviceinfo[$result->name] = $result->plugintype . '_' . $result->pluginname;
 114      }
 115  
 116      foreach ($services as $id => $servicedata) {
 117          if (array_key_exists($servicedata['name'], $serviceinfo)) {
 118              $service = $serviceinfo[$servicedata['name']];
 119              $servicedata['humanname'] = get_string($servicedata['name'].'_name', $service);
 120          } else {
 121              $servicedata['humanname'] = get_string('unknown', 'mnet');
 122          }
 123          $newurl = new moodle_url($url, array('hostid' => $host->id, 'servicename' => $servicedata['name']));
 124          $table->data[] = array(
 125              $servicedata['name'],
 126              $servicedata['humanname'],
 127              $servicedata['apiversion'],
 128              $yesno[$servicedata['publish']],
 129              $yesno[$servicedata['subscribe']],
 130              html_writer::link($newurl, get_string('listservices', 'mnet'))
 131          );
 132  
 133      }
 134      echo html_writer::table($table);
 135  
 136  
 137      $mnet_request = new mnet_xmlrpc_client();
 138      $mnet_request->set_method('system/listMethods');
 139      if (isset($servicename) && array_key_exists($servicename, $serviceinfo)) {
 140          echo $OUTPUT->heading(get_string('methodsavailableonhostinservice', 'mnet', (object)array('host' => $host->wwwroot, 'service' => $servicename)));
 141          $service = $serviceinfo[$servicename];
 142          $mnet_request->add_param($servicename, 'string');
 143      } else {
 144          echo $OUTPUT->heading(get_string('methodsavailableonhost', 'mnet', $host->wwwroot));
 145      }
 146  
 147      $mnet_request->send($mnet_peer);
 148      $methods = $mnet_request->response;
 149  
 150      if (!empty($mnet_request->error)) {
 151          echo $OUTPUT->heading(get_string('error'), 3);
 152          echo html_writer::alist($mnet_request->error);
 153          $methods = array();
 154      }
 155  
 156      $table = new html_table();
 157      $table->head = array(
 158          get_string('method', 'mnet'),
 159          get_string('options', 'mnet'),
 160      );
 161      $table->data = array();
 162  
 163      foreach ($methods as $id => $method) {
 164          $params = array('hostid' => $host->id, 'method' => $id+1);
 165          if (isset($servicename)) {
 166              $params['servicename'] = $servicename;
 167          }
 168          $newurl = new moodle_url($url, $params);
 169          $table->data[] = array(
 170              $method,
 171              html_writer::link($newurl, get_string('inspect', 'mnet'))
 172          );
 173      }
 174      echo html_writer::table($table);
 175  
 176      if (isset($methodid) && array_key_exists($methodid-1, $methods)) {
 177          $method = $methods[$methodid-1];
 178  
 179          $mnet_request = new mnet_xmlrpc_client();
 180          $mnet_request->set_method('system/methodSignature');
 181          $mnet_request->add_param($method, 'string');
 182          $mnet_request->send($mnet_peer);
 183          $signature = $mnet_request->response;
 184  
 185          echo $OUTPUT->heading(get_string('methodsignature', 'mnet', $method));
 186  
 187          if (!empty($mnet_request->error)) {
 188              echo $OUTPUT->heading(get_string('error'), 3);
 189              echo html_writer::alist($mnet_request->error);
 190              $signature = array();
 191          }
 192  
 193          $table = new html_table();
 194          $table->head = array(
 195              get_string('position', 'mnet'),
 196              get_string('name', 'mnet'),
 197              get_string('type', 'mnet'),
 198              get_string('description', 'mnet'),
 199          );
 200          $table->data = array();
 201  
 202          $params = $signature['parameters'];
 203          foreach ($params as $pos => $details) {
 204              $table->data[] = array(
 205                  $pos,
 206                  $details['name'],
 207                  $details['type'],
 208                  $details['description'],
 209              );
 210          }
 211          $table->data[] = array(
 212              get_string('returnvalue', 'mnet'),
 213              '',
 214              $signature['return']['type'],
 215              $signature['return']['description']
 216          );
 217  
 218          echo html_writer::table($table);
 219  
 220          $mnet_request->set_method('system/methodHelp');
 221          $mnet_request->add_param($method, 'string');
 222          $mnet_request->send($mnet_peer);
 223          $help = $mnet_request->response;
 224  
 225          echo $OUTPUT->heading(get_string('methodhelp', 'mnet', $method));
 226          echo(str_replace('\n', '<br />',$help));
 227      }
 228  }
 229  
 230  echo $OUTPUT->footer();
 231  ?>


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1