Fixed 2.0 bugs
[moodle.git] / mnet / testclient.php
CommitLineData
1d422980 1<?php
71558f85 2/**
4ce054f3 3 * A service browser for remote Moodles
b16393cb 4 *
4ce054f3 5 * This script 'remotely' executes the reflection methods on a remote Moodle,
6 * and publishes the details of the available services
b16393cb 7 *
71558f85 8 * @author Donal McMullan donal@catalyst.net.nz
9 * @version 0.0.1
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 * @package mnet
12 */
13require_once(dirname(dirname(__FILE__)) . '/config.php');
14require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';
15
600be062 16if ($CFG->mnet_dispatcher_mode === 'off') {
17 print_error('mnetdisabled', 'mnet');
18}
19
4ce054f3 20// Site admins only, thanks.
ebff4779 21require_login();
ef35441d 22$context = get_context_instance(CONTEXT_SYSTEM);
4ce054f3 23require_capability('moodle/site:config', $context);
71558f85 24
4ce054f3 25error_reporting(E_ALL);
71558f85 26
27// Some HTML sugar
28echo '<?xml version="1.0" encoding="utf-8"?>';
29?>
30<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
31<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
32<head><title>Moodle MNET Test Client</title></head><body>
4ce054f3 33<H1>Hosts</H1>
71558f85 34<?php
35
cc38ff5d 36$hosts = $DB->get_records('mnet_host');
71558f85 37
4ce054f3 38foreach ($hosts as $id => $host) {
39 // Skip the 'all hosts' option
40 if(empty($host->wwwroot)) continue;
41 // Skip localhost
42 if($host->wwwroot == $CFG->wwwroot) continue;
b3f0a052 43 // Skip non-moodle non-mahara hosts
44 if($host->applicationid != 1 && $host->applicationid != 2) continue; //TODO: get rid of magic numbers.
4ce054f3 45 echo '<p><a href="testclient.php?hostid='.$host->id.'">'.$host->wwwroot."</a></p>\n";
71558f85 46}
47
4ce054f3 48if (!empty($_GET['hostid']) && array_key_exists($_GET['hostid'], $hosts)) {
49 $host = $hosts[$_GET['hostid']];
50 $mnet_peer = new mnet_peer();
51 $mnet_peer->set_wwwroot($host->wwwroot);
52
53 $mnet_request = new mnet_xmlrpc_client();
54
55 // Tell it the path to the method that we want to execute
56 $mnet_request->set_method('system/listServices');
57 $mnet_request->send($mnet_peer);
58 $services = $mnet_request->response;
59 $yesno = array('No', 'Yes');
60 $servicenames = array();
61
62 echo '<hr /><br /><h3>Services available on host: '.$host->wwwroot .'</h3><table><tr valign="top"><th>&nbsp;&nbsp;Service ID&nbsp;&nbsp;</th><th>&nbsp;&nbsp;Service&nbsp;&nbsp;</th><th>&nbsp;&nbsp;Version&nbsp;&nbsp;</th><th>&nbsp;&nbsp;They Publish&nbsp;&nbsp;</th><th>&nbsp;&nbsp;They Subscribe&nbsp;&nbsp;</th><th></th></tr>';
63 foreach ($services as $id => $service) {
cc38ff5d 64 $sql = 'select c.id, c.parent_type, c.parent from {mnet_service2rpc} a, {mnet_service} b, {mnet_rpc} c where a.serviceid = b.id and b.name=? and c.id = a.rpcid ';
4ce054f3 65
66 echo '<tr valign="top">
67 <td>'.$service['name'].'</td>';
cc38ff5d 68 if ($detail = $DB->get_record_sql($sql, array($service['name']))) {
4ce054f3 69 $service['humanname'] = get_string($service['name'].'_name', $detail->parent_type.'_'.$detail->parent);
70 echo '<td>'.$service['humanname'].'</td>';
71 } else {
72 $service['humanname'] = $service['name'];
73 echo '<td> unknown </td>';
74 }
75 echo '
76 <td>'.$service['apiversion'].'</td>
77 <td>'.$yesno[$service['publish']].'</td>
78 <td>'.$yesno[$service['subscribe']].'</td>
79 <td><a href="testclient.php?hostid='.$host->id.'&service='.$service['name'].'">List methods</a></td>
80 </tr>'."\n";
81 $servicenames[$service['name']] = $service;
82 }
83 echo '</table>';
84
85
86
87 if (isset($_GET['service']) && array_key_exists($_GET['service'], $servicenames)) {
88 $service = $servicenames[$_GET['service']];
89 // Tell it the path to the method that we want to execute
90 $mnet_request->set_method('system/listMethods');
91 $mnet_request->add_param($service['name'], 'string');
92 $mnet_request->send($mnet_peer);
93 $methods = $mnet_request->response;
94
95 echo '<hr /><br /><h3>Methods in the '.$service['humanname'] .' service</h3><table><th>Method</th><th colspan="2">Options</th>';
96 foreach ($methods as $id => $method) {
97 echo '<tr><td>'.$method.'</td><td> <a href="testclient.php?hostid='.$host->id.'&service='.$service['name'].'&method='.$id.'&show=sig">Inspect</a></td></tr>'."\n";
98 }
99 echo '</table>';
100 } else {
101 // Tell it the path to the method that we want to execute
102 $mnet_request->set_method('system/listMethods');
103 $mnet_request->send($mnet_peer);
104 $methods = $mnet_request->response;
1d422980 105
4ce054f3 106 echo '<hr /><br /><h3>Methods '.$host->wwwroot .'</h3><table><th>Method</th><th colspan="2">Options</th>';
107 foreach ($methods as $id => $method) {
108 echo '<tr><td>'.$method.'</td><td> <a href="testclient.php?hostid='.$host->id.'&method='.$id.'&show=sig">Inspect</a></td></tr>'."\n";
109 }
110 echo '</table>';
71558f85 111 }
71558f85 112
4ce054f3 113 if (isset($_GET['method']) && array_key_exists($_GET['method'], $methods)) {
114 $method = $methods[$_GET['method']];
115
116 $mnet_request = new mnet_xmlrpc_client();
117
118 // Tell it the path to the method that we want to execute
119 $mnet_request->set_method('system/methodSignature');
120 $mnet_request->add_param($method, 'string');
121 $mnet_request->send($mnet_peer);
122 $signature = $mnet_request->response;
123 echo '<hr /><br /><h3>Method signature for '.$method.':</h3><table border="1"><th>Position</th><th>Type</th><th>Description</th>';
124 $params = array_pop($signature);
125 foreach ($params as $pos => $details) {
126 echo '<tr><td>'.$pos.'</td><td>'.$details['type'].'</td><td>'.$details['description'].'</td></tr>';
127 }
128 echo '</table>';
129
130 // Tell it the path to the method that we want to execute
131 $mnet_request->set_method('system/methodHelp');
132 $mnet_request->add_param($method, 'string');
133 $mnet_request->send($mnet_peer);
134 $help = $mnet_request->response;
135 echo '<hr /><br /><h3>Help details from docblock for '.$method.':</h3>';
136 echo(str_replace('\n', '<br />',$help));
137 echo '</pre>';
138 }
139}
71558f85 140
71558f85 141
71558f85 142?>
4ce054f3 143</body>
144</html>