Skip to content

Commit

Permalink
Rewrite testclient.php to be a service browser for remote Moodles. Th…
Browse files Browse the repository at this point in the history
…is should be useful to people who are trying to connect to the API from non-Moodle hosts, or write software to connect to Moodle. MDL-10214, MDL-10174, MDL-10172, MDL-10171
  • Loading branch information
donal72 committed Oct 17, 2007
1 parent eb1bf09 commit d029c12
Showing 1 changed file with 112 additions and 119 deletions.
231 changes: 112 additions & 119 deletions mnet/testclient.php
@@ -1,12 +1,10 @@
<?php <?php // $Id$
/** /**
* A template to test Moodle's XML-RPC feature * A service browser for remote Moodles
* *
* This script 'remotely' executes the mnet_concatenate_strings function in * This script 'remotely' executes the reflection methods on a remote Moodle,
* mnet/testlib.php * and publishes the details of the available services
* It steps through each stage of the process, printing some data as it goes *
* along. It should help you to get your remote method working.
*
* @author Donal McMullan donal@catalyst.net.nz * @author Donal McMullan donal@catalyst.net.nz
* @version 0.0.1 * @version 0.0.1
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
Expand All @@ -15,132 +13,127 @@
require_once(dirname(dirname(__FILE__)) . '/config.php'); require_once(dirname(dirname(__FILE__)) . '/config.php');
require_once $CFG->dirroot.'/mnet/xmlrpc/client.php'; require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';


error_reporting(E_ALL); // Site admins only, thanks.

$context = get_context_instance(CONTEXT_SYSTEM, SITEID);
if (isset($_GET['func']) && is_numeric($_GET['func'])) { require_capability('moodle/site:config', $context);
$func = $_GET['func'];


error_reporting(E_ALL);


// Some HTML sugar // Some HTML sugar
echo '<?xml version="1.0" encoding="utf-8"?>'; echo '<?xml version="1.0" encoding="utf-8"?>';
?> ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head><title>Moodle MNET Test Client</title></head><body> <head><title>Moodle MNET Test Client</title></head><body>
<H1>Hosts</H1>
<?php <?php


// For the demo, our 'remote' host is actually our local host. $hosts = get_records('mnet_host');
$wwwroot = $CFG->wwwroot;

// Enter the complete path to the file that contains the function you want to
// call on the remote server. In our example the function is in
// mnet/testlib/
// The function itself is added to that path to complete the $path_to_function
// variable
$path_to_function[0] = 'mnet/rpclib/mnet_concatenate_strings';
$path_to_function[1] = 'mod/scorm/rpclib/scorm_add_floats';
$path_to_function[2] = 'system/listMethods';
$path_to_function[3] = 'system/methodSignature';
$path_to_function[4] = 'system/methodHelp';
$path_to_function[5] = 'system/listServices';
$path_to_function[6] = 'system/listMethods';
$path_to_function[7] = 'system/listMethods';

$paramArray[0] = array(array('some string, ', 'string'),
array('some other string, ', 'string'),
array('and a final string', 'string'));

$paramArray[1] = array(array(5.3, 'string'),
array(7.1, 'string'),
array(8.25323, 'string'));

$paramArray[2] = array();

$paramArray[3] = array(array('auth/mnet/auth/user_authorise', 'string'));

$paramArray[4] = array(array('auth/mnet/auth/user_authorise', 'string'));

$paramArray[5] = array();

$paramArray[6] = array(array('sso', 'string'));

$paramArray[7] = array(array('concatenate', 'string'));


echo 'Your local wwwroot appears to be <strong>'. $wwwroot ."</strong>.<br />\n"; foreach ($hosts as $id => $host) {
echo "We will use this as the local <em>and</em> remote hosts.<br /><br />\n"; // Skip the 'all hosts' option
flush(); if(empty($host->wwwroot)) continue;

// Skip localhost
// mnet_peer pulls information about a remote host from the database. if($host->wwwroot == $CFG->wwwroot) continue;
$mnet_peer = new mnet_peer(); // Skip non-moodle hosts
$mnet_peer->set_wwwroot($wwwroot); if($host->applicationid != 1) continue;

echo '<p><a href="testclient.php?hostid='.$host->id.'">'.$host->wwwroot."</a></p>\n";
echo "Your \$mnet_peer from the database looks like:<br />\n<pre>";
$h2 = get_object_vars($mnet_peer);
while(list($key, $val) = each($h2)) {
if (!is_numeric($key)) echo '<strong>'.$key.':</strong> '. $val."\n";
}
echo "</pre><br/>It's ok if that info is not complete - the required field is:<br />\nwwwroot: <b>{$mnet_peer->wwwroot}</b>.<br /><br/>\n";
flush();

// The transport id is one of:
// RPC_HTTPS_VERIFIED 1
// RPC_HTTPS_SELF_SIGNED 2
// RPC_HTTP_VERIFIED 3
// RPC_HTTP_SELF_SIGNED 4

if (!$mnet_peer->transport) exit('No transport method is approved for this host in your DB table. Please enable a transport method and try again.');
$t[1] = 'http2 (port 443 encrypted) with a verified certificate.';
$t[2] = 'https (port 443 encrypted) with a self-signed certificate.';
$t[4] = 'http (port 80 unencrypted) with a verified certificate.';
$t[8] = 'http (port 80 unencrypted) with a self-signed certificate.';
$t[16] = 'http (port 80 unencrypted) unencrypted with no certificate.';

echo 'Your transportid is <strong>'.$mnet_peer->transport.'</strong> which represents <em>'.$t[$mnet_peer->transport]."</em><br /><br />\n";
flush();

// Create a new request object
$mnet_request = new mnet_xmlrpc_client();

// Tell it the path to the method that we want to execute
$mnet_request->set_method($path_to_function[$func]);
// Add parameters for your function. The mnet_concatenate_strings takes three
// parameters, like mnet_concatenate_strings($string1, $string2, $string3)
// PHP is weakly typed, so you can get away with calling most things strings,
// unless it's non-scalar (i.e. an array or object or something).
foreach($paramArray[$func] as $param) {
$mnet_request->add_param($param[0], $param[1]);
} }


if (count($mnet_request->params)) { if (!empty($_GET['hostid']) && array_key_exists($_GET['hostid'], $hosts)) {
echo 'Your parameters are:<br />'; $host = $hosts[$_GET['hostid']];
while(list($key, $val) = each($mnet_request->params)) { $mnet_peer = new mnet_peer();
echo '&nbsp;&nbsp; <strong>'.$key.':</strong> '. $val."<br/>\n"; $mnet_peer->set_wwwroot($host->wwwroot);

$mnet_request = new mnet_xmlrpc_client();

// Tell it the path to the method that we want to execute
$mnet_request->set_method('system/listServices');
$mnet_request->send($mnet_peer);
$services = $mnet_request->response;
$yesno = array('No', 'Yes');
$servicenames = array();

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>';
foreach ($services as $id => $service) {
$sql = 'select c.id, c.parent_type, c.parent from '.$CFG->prefix.'mnet_service2rpc a,'.$CFG->prefix.'mnet_service b, '.$CFG->prefix.'mnet_rpc c where a.serviceid = b.id and b.name=\''.addslashes($service['name']).'\' and c.id = a.rpcid ';

echo '<tr valign="top">
<td>'.$service['name'].'</td>';
if ($detail = get_record_sql($sql)) {
$service['humanname'] = get_string($service['name'].'_name', $detail->parent_type.'_'.$detail->parent);
echo '<td>'.$service['humanname'].'</td>';
} else {
$service['humanname'] = $service['name'];
echo '<td> unknown </td>';
}
echo '
<td>'.$service['apiversion'].'</td>
<td>'.$yesno[$service['publish']].'</td>
<td>'.$yesno[$service['subscribe']].'</td>
<td><a href="testclient.php?hostid='.$host->id.'&service='.$service['name'].'">List methods</a></td>
</tr>'."\n";
$servicenames[$service['name']] = $service;
}
echo '</table>';



if (isset($_GET['service']) && array_key_exists($_GET['service'], $servicenames)) {
$service = $servicenames[$_GET['service']];
// Tell it the path to the method that we want to execute
$mnet_request->set_method('system/listMethods');
$mnet_request->add_param($service['name'], 'string');
$mnet_request->send($mnet_peer);
$methods = $mnet_request->response;

echo '<hr /><br /><h3>Methods in the '.$service['humanname'] .' service</h3><table><th>Method</th><th colspan="2">Options</th>';
foreach ($methods as $id => $method) {
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";
}
echo '</table>';
} else {
// Tell it the path to the method that we want to execute
$mnet_request->set_method('system/listMethods');
$mnet_request->send($mnet_peer);
$methods = $mnet_request->response;

echo '<hr /><br /><h3>Methods '.$host->wwwroot .'</h3><table><th>Method</th><th colspan="2">Options</th>';
foreach ($methods as $id => $method) {
echo '<tr><td>'.$method.'</td><td> <a href="testclient.php?hostid='.$host->id.'&method='.$id.'&show=sig">Inspect</a></td></tr>'."\n";
}
echo '</table>';
} }
}
flush();

// We send the request:
$mnet_request->send($mnet_peer);


?> if (isset($_GET['method']) && array_key_exists($_GET['method'], $methods)) {
$method = $methods[$_GET['method']];

$mnet_request = new mnet_xmlrpc_client();

// Tell it the path to the method that we want to execute
$mnet_request->set_method('system/methodSignature');
$mnet_request->add_param($method, 'string');
$mnet_request->send($mnet_peer);
$signature = $mnet_request->response;
echo '<hr /><br /><h3>Method signature for '.$method.':</h3><table border="1"><th>Position</th><th>Type</th><th>Description</th>';
$params = array_pop($signature);
foreach ($params as $pos => $details) {
echo '<tr><td>'.$pos.'</td><td>'.$details['type'].'</td><td>'.$details['description'].'</td></tr>';
}
echo '</table>';

// Tell it the path to the method that we want to execute
$mnet_request->set_method('system/methodHelp');
$mnet_request->add_param($method, 'string');
$mnet_request->send($mnet_peer);
$help = $mnet_request->response;
echo '<hr /><br /><h3>Help details from docblock for '.$method.':</h3>';
echo(str_replace('\n', '<br />',$help));
echo '</pre>';
}
}


A var_dump of the decoded response: <strong><pre><?php var_dump($mnet_request->response); ?></pre></strong><br />


<?php
if (count($mnet_request->params)) {
?> ?>
A var_dump of the parameters you sent: <strong><pre><?php var_dump($mnet_request->params); ?></pre></strong><br /> </body>
<?php </html>
}
}
?>
<p>
Choose a function to call:<br />
<a href="testclient.php?func=2">system/listMethods</a><br />
<a href="testclient.php?func=3">system/methodSignature</a><br />
<a href="testclient.php?func=4">system/methodHelp</a><br />
<a href="testclient.php?func=5">listServices</a><br />
<a href="testclient.php?func=6">system/listMethods(SSO)</a><br />
<a href="testclient.php?func=7">system/listMethods(concatenate)</a><br />

</body></html>

0 comments on commit d029c12

Please sign in to comment.