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