From 4ce054f34821beac8b7253820710333e7c165870 Mon Sep 17 00:00:00 2001 From: donal72 Date: Wed, 17 Oct 2007 08:12:06 +0000 Subject: [PATCH] Rewrite testclient.php to be a service browser for remote Moodles. This 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 --- mnet/testclient.php | 225 +++++++++++++++++++++----------------------- 1 file changed, 109 insertions(+), 116 deletions(-) diff --git a/mnet/testclient.php b/mnet/testclient.php index d8cdeecc73..4816e1f7bb 100644 --- a/mnet/testclient.php +++ b/mnet/testclient.php @@ -1,11 +1,9 @@ dirroot.'/mnet/xmlrpc/client.php'; -error_reporting(E_ALL); - -if (isset($_GET['func']) && is_numeric($_GET['func'])) { - $func = $_GET['func']; +// Site admins only, thanks. +$context = get_context_instance(CONTEXT_SYSTEM, SITEID); +require_capability('moodle/site:config', $context); +error_reporting(E_ALL); // Some HTML sugar echo ''; @@ -27,120 +25,115 @@ echo ''; Moodle MNET Test Client +

Hosts

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 '. $wwwroot .".
\n"; -echo "We will use this as the local and remote hosts.

\n"; -flush(); +$hosts = get_records('mnet_host'); -// mnet_peer pulls information about a remote host from the database. -$mnet_peer = new mnet_peer(); -$mnet_peer->set_wwwroot($wwwroot); - -echo "Your \$mnet_peer from the database looks like:
\n
";
-$h2 = get_object_vars($mnet_peer);
-while(list($key, $val) = each($h2)) {
-    if (!is_numeric($key)) echo ''.$key.': '. $val."\n";
-}
-echo "

It's ok if that info is not complete - the required field is:
\nwwwroot: {$mnet_peer->wwwroot}.

\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 '.$mnet_peer->transport.' which represents '.$t[$mnet_peer->transport]."

\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]); +foreach ($hosts as $id => $host) { + // Skip the 'all hosts' option + if(empty($host->wwwroot)) continue; + // Skip localhost + if($host->wwwroot == $CFG->wwwroot) continue; + // Skip non-moodle hosts + if($host->applicationid != 1) continue; + echo '

'.$host->wwwroot."

\n"; } -if (count($mnet_request->params)) { - echo 'Your parameters are:
'; - while(list($key, $val) = each($mnet_request->params)) { - echo '   '.$key.': '. $val."
\n"; +if (!empty($_GET['hostid']) && array_key_exists($_GET['hostid'], $hosts)) { + $host = $hosts[$_GET['hostid']]; + $mnet_peer = new mnet_peer(); + $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 '

Services available on host: '.$host->wwwroot .'

'; + 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 ' + '; + if ($detail = get_record_sql($sql)) { + $service['humanname'] = get_string($service['name'].'_name', $detail->parent_type.'_'.$detail->parent); + echo ''; + } else { + $service['humanname'] = $service['name']; + echo ''; + } + echo ' + + + + + '."\n"; + $servicenames[$service['name']] = $service; + } + echo '
  Service ID    Service    Version    They Publish    They Subscribe  
'.$service['name'].''.$service['humanname'].' unknown '.$service['apiversion'].''.$yesno[$service['publish']].''.$yesno[$service['subscribe']].'List methods
'; + + + + 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 '

Methods in the '.$service['humanname'] .' service

'; + foreach ($methods as $id => $method) { + echo ''."\n"; + } + echo '
MethodOptions
'.$method.' Inspect
'; + } 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 '

Methods '.$host->wwwroot .'

'; + foreach ($methods as $id => $method) { + echo ''."\n"; + } + echo '
MethodOptions
'.$method.' Inspect
'; } -} -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 '

Method signature for '.$method.':

'; + $params = array_pop($signature); + foreach ($params as $pos => $details) { + echo ''; + } + echo '
PositionTypeDescription
'.$pos.''.$details['type'].''.$details['description'].'
'; + + // 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 '

Help details from docblock for '.$method.':

'; + echo(str_replace('\n', '
',$help)); + echo ''; + } +} -A var_dump of the decoded response:
response); ?>

-params)) { ?> - A var_dump of the parameters you sent:
params); ?>

- -

- Choose a function to call:
- system/listMethods
- system/methodSignature
- system/methodHelp
- listServices
- system/listMethods(SSO)
- system/listMethods(concatenate)
- - + + -- 2.39.5