Skip to content

Commit

Permalink
MDL-15097 mnet conversion revisited
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jun 5, 2008
1 parent f67cab3 commit cc38ff5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 56 deletions.
14 changes: 8 additions & 6 deletions mnet/environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function mnet_environment() {
}

function init() {
global $CFG;
global $CFG, $DB;

if (empty($CFG->mnet_dispatcher_mode)) {
set_config('mnet_dispatcher_mode', 'off');
Expand All @@ -45,17 +45,17 @@ function init() {
$this->ip_address = $_SERVER['SERVER_ADDR'];
}

if ($existingrecord = get_record('mnet_host', 'ip_address', $this->ip_address)) {
if ($existingrecord = $DB->get_record('mnet_host', array('ip_address'=>$this->ip_address))) {
$this->id = $existingrecord->id;
} else { // make a new one
$this->id = insert_record('mnet_host', $this, true);
$this->id = $DB->insert_record('mnet_host', $this);
}

set_config('mnet_localhost_id', $this->id);
$this->get_keypair();
}
} else {
$hostobject = get_record('mnet_host','id', $CFG->mnet_localhost_id);
$hostobject = $DB->get_record('mnet_host', array('id'=>$CFG->mnet_localhost_id));
if(is_object($hostobject)) {
$temparr = get_object_vars($hostobject);
foreach($temparr as $key => $value) {
Expand Down Expand Up @@ -85,7 +85,7 @@ function init() {
$hostobject->deleted = 0;
$hostobject->name = 'All Hosts';

$hostobject->id = insert_record('mnet_host',$hostobject, true);
$hostobject->id = $DB->insert_record('mnet_host',$hostobject);
set_config('mnet_all_hosts_id', $hostobject->id);
$CFG->mnet_all_hosts_id = $hostobject->id;
unset($hostobject);
Expand Down Expand Up @@ -150,6 +150,8 @@ function get_keypair() {
}

function replace_keys() {
global $DB;

$this->keypair = array();
$this->keypair = mnet_generate_keypair();
$this->public_key = $this->keypair['certificate'];
Expand All @@ -158,7 +160,7 @@ function replace_keys() {

set_config('openssl', implode('@@@@@@@@', $this->keypair), 'mnet');

update_record('mnet_host', $this);
$DB->update_record('mnet_host', $this);
error_log('New public key has been generated. It expires ' . date('Y/m/d h:i:s', $this->public_key_expires));
}

Expand Down
30 changes: 16 additions & 14 deletions mnet/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function mnet_get_hostname_from_uri($uri = null) {
* @return string A PEM formatted SSL Certificate.
*/
function mnet_get_public_key($uri, $application=null) {
global $CFG, $MNET;
global $CFG, $MNET, $DB;
// The key may be cached in the mnet_set_public_key function...
// check this first
$key = mnet_set_public_key($uri);
Expand All @@ -55,7 +55,7 @@ function mnet_get_public_key($uri, $application=null) {
}

if (empty($application)) {
$application = get_record('mnet_application', 'name', 'moodle');
$application = $DB->get_record('mnet_application', array('name'=>'moodle'));
}

$rq = xmlrpc_encode_request('system/keyswap', array($CFG->wwwroot, $MNET->public_key, $application->name), array("encoding" => "utf-8"));
Expand Down Expand Up @@ -316,13 +316,13 @@ function mnet_get_keypair() {
* @return string The signature over that text
*/
function mnet_generate_keypair($dn = null, $days=28) {
global $CFG, $USER;
global $CFG, $USER, $DB;
$host = strtolower($CFG->wwwroot);
$host = ereg_replace("^http(s)?://",'',$host);
$break = strpos($host.'/' , '/');
$host = substr($host, 0, $break);

if ($result = get_record_select('course'," id ='".SITEID."' ")) {
if ($result = $DB->get_record('course', array("id"=>SITEID))) {
$organization = $result->fullname;
} else {
$organization = 'None';
Expand Down Expand Up @@ -443,17 +443,17 @@ function mnet_permit_rpc_call($includefile, $functionname, $class=false) {
SELECT
count(r.id)
FROM
{$CFG->prefix}mnet_host2service h2s,
{$CFG->prefix}mnet_service2rpc s2r,
{$CFG->prefix}mnet_rpc r
{mnet_host2service} h2s,
{mnet_service2rpc} s2r,
{mnet_rpc} r
WHERE
h2s.serviceid = s2r.serviceid AND
s2r.rpcid = r.id AND
r.xmlrpc_path = '$callprefix/$functionname' AND
r.xmlrpc_path = ? AND
h2s.hostid in ($id_list) AND
h2s.publish = '1'";

$permissionobj = record_exists_sql($sql);
$params = array("$callprefix/$functionname");
$permissionobj = $DB->record_exists_sql($sql, $params);

if ($permissionobj === false && 'dangerous' != $CFG->mnet_dispatcher_mode) {
return RPC_FORBIDDENMETHOD;
Expand Down Expand Up @@ -512,11 +512,13 @@ function mnet_permit_rpc_call($includefile, $functionname, $class=false) {
}

function mnet_update_sso_access_control($username, $mnet_host_id, $accessctrl) {
$mnethost = get_record('mnet_host', 'id', $mnet_host_id);
if ($aclrecord = get_record('mnet_sso_access_control', 'username', $username, 'mnet_host_id', $mnet_host_id)) {
global $DB;

$mnethost = $DB->get_record('mnet_host', array('id'=>$mnet_host_id));
if ($aclrecord = $DB->get_record('mnet_sso_access_control', array('username'=>$username, 'mnet_host_id'=>$mnet_host_id))) {
// update
$aclrecord->accessctrl = $accessctrl;
if (update_record('mnet_sso_access_control', $aclrecord)) {
if ($DB->update_record('mnet_sso_access_control', $aclrecord)) {
add_to_log(SITEID, 'admin/mnet', 'update', 'admin/mnet/access_control.php',
"SSO ACL: $accessctrl user '$username' from {$mnethost->name}");
} else {
Expand All @@ -528,7 +530,7 @@ function mnet_update_sso_access_control($username, $mnet_host_id, $accessctrl) {
$aclrecord->username = $username;
$aclrecord->accessctrl = $accessctrl;
$aclrecord->mnet_host_id = $mnet_host_id;
if ($id = insert_record('mnet_sso_access_control', $aclrecord)) {
if ($id = $DB->insert_record('mnet_sso_access_control', $aclrecord)) {
add_to_log(SITEID, 'admin/mnet', 'add', 'admin/mnet/access_control.php',
"SSO ACL: $accessctrl user '$username' from {$mnethost->name}");
} else {
Expand Down
6 changes: 3 additions & 3 deletions mnet/peer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function mnet_peer() {
}

function bootstrap($wwwroot, $pubkey = null, $application) {
global $DB;

if (substr($wwwroot, -1, 1) == '/') {
$wwwroot = substr($wwwroot, 0, -1);
Expand Down Expand Up @@ -63,9 +64,9 @@ function bootstrap($wwwroot, $pubkey = null, $application) {
$this->ip_address = $ip_address;
$this->deleted = 0;

$this->application = get_record('mnet_application', 'name', $application);
$this->application = $DB->get_record('mnet_application', array('name'=>$application));
if (empty($this->application)) {
$this->application = get_record('mnet_application', 'name', 'moodle');
$this->application = $DB->get_record('mnet_application', array('name'=>'moodle'));
}

$this->applicationid = $this->application->id;
Expand Down Expand Up @@ -131,7 +132,6 @@ function delete_expired_sessions() {
function delete_all_sessions() {
global $CFG, $DB;
// TODO: Expires each PHP session individually
// $sessions = get_records('mnet_session', 'mnethostid', $this->id);
$sessions = $DB->get_records('mnet_session', array('mnethostid'=>$this->id));

if (count($sessions) > 0 && file_exists($CFG->dirroot.'/auth/mnet/auth.php')) {
Expand Down
6 changes: 3 additions & 3 deletions mnet/testclient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<H1>Hosts</H1>
<?php

$hosts = get_records('mnet_host');
$hosts = $DB->get_records('mnet_host');

foreach ($hosts as $id => $host) {
// Skip the 'all hosts' option
Expand Down Expand Up @@ -56,11 +56,11 @@

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 ';
$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 ';

echo '<tr valign="top">
<td>'.$service['name'].'</td>';
if ($detail = get_record_sql($sql)) {
if ($detail = $DB->get_record_sql($sql, array($service['name']))) {
$service['humanname'] = get_string($service['name'].'_name', $detail->parent_type.'_'.$detail->parent);
echo '<td>'.$service['humanname'].'</td>';
} else {
Expand Down
14 changes: 7 additions & 7 deletions mnet/xmlrpc/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function add_param($argument, $type = 'string') {
* remote function
*/
function send($mnet_peer) {
global $CFG, $MNET;
global $CFG, $MNET, $DB;

$this->uri = $mnet_peer->wwwroot.$mnet_peer->application->xmlrpc_server_url;

Expand Down Expand Up @@ -151,17 +151,17 @@ function send($mnet_peer) {
SELECT
*
FROM
{$CFG->prefix}mnet_rpc r,
{$CFG->prefix}mnet_service2rpc s2r,
{$CFG->prefix}mnet_host2service h2s
{mnet_rpc} r,
{mnet_service2rpc} s2r,
{mnet_host2service} h2s
WHERE
r.xmlrpc_path = '{$this->method}' AND
r.xmlrpc_path = ? AND
s2r.rpcid = r.id AND
s2r.serviceid = h2s.serviceid AND
h2s.subscribe = '1' AND
h2s.hostid in ({$id_list})";

$permission = get_record_sql($sql);
$permission = $DB->get_record_sql($sql, array($this->method));
if ($permission == false) {
global $USER;
$this->error[] = '7:User with ID '. $USER->id .
Expand Down Expand Up @@ -310,7 +310,7 @@ function send($mnet_peer) {
$details = openssl_x509_parse($record->public_key);
if(is_array($details) && isset($details['validTo_time_t'])) {
$record->public_key_expires = $details['validTo_time_t'];
update_record('mnet_host', $record);
$DB->update_record('mnet_host', $record);
$mnet_peer2 = new mnet_peer();
$mnet_peer2->set_id($record->id);
$mnet_peer2->re_key = true;
Expand Down
51 changes: 28 additions & 23 deletions mnet/xmlrpc/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ function mnet_server_dispatch($payload) {
* @return mixed Response data - any kind of PHP variable
*/
function mnet_system($method, $params, $hostinfo) {
global $CFG;
global $CFG, $DB;

if (empty($hostinfo)) return array();

Expand All @@ -538,16 +538,17 @@ function mnet_system($method, $params, $hostinfo) {
rpc.help,
rpc.profile
FROM
'.$CFG->prefix.'mnet_host2service h2s,
'.$CFG->prefix.'mnet_service2rpc s2r,
'.$CFG->prefix.'mnet_rpc rpc
{mnet_host2service} h2s,
{mnet_service2rpc} s2r,
{mnet_rpc} rpc
WHERE
s2r.rpcid = rpc.id AND
h2s.serviceid = s2r.serviceid AND
h2s.hostid in ('.$id_list .') AND
h2s.publish =\'1\'
ORDER BY
rpc.xmlrpc_path ASC';
$params = array();

} else {
$query = '
Expand All @@ -558,22 +559,23 @@ function mnet_system($method, $params, $hostinfo) {
rpc.help,
rpc.profile
FROM
'.$CFG->prefix.'mnet_host2service h2s,
'.$CFG->prefix.'mnet_service2rpc s2r,
'.$CFG->prefix.'mnet_service svc,
'.$CFG->prefix.'mnet_rpc rpc
{mnet_host2service} h2s,
{mnet_service2rpc} s2r,
{mnet_service} svc,
{mnet_rpc} rpc
WHERE
s2r.rpcid = rpc.id AND
h2s.serviceid = s2r.serviceid AND
h2s.hostid in ('.$id_list .') AND
h2s.publish =\'1\' AND
svc.id = h2s.serviceid AND
svc.name = \''.$params[0].'\'
svc.name = ?
ORDER BY
rpc.xmlrpc_path ASC';
$params = array($params[0]);

}
$resultset = array_values(get_records_sql($query));
$resultset = array_values($DB->get_records_sql($query, $params));
$methods = array();
foreach($resultset as $result) {
$methods[] = $result->xmlrpc_path;
Expand All @@ -588,17 +590,18 @@ function mnet_system($method, $params, $hostinfo) {
rpc.help,
rpc.profile
FROM
'.$CFG->prefix.'mnet_host2service h2s,
'.$CFG->prefix.'mnet_service2rpc s2r,
'.$CFG->prefix.'mnet_rpc rpc
{mnet_host2service} h2s,
{mnet_service2rpc} s2r,
{mnet_rpc rpc}
WHERE
rpc.xmlrpc_path = \''.$params[0].'\' AND
rpc.xmlrpc_path = ? AND
s2r.rpcid = rpc.id AND
h2s.serviceid = s2r.serviceid AND
h2s.publish =\'1\' AND
h2s.hostid in ('.$id_list .')';
$params = array($params[0]);

$result = get_records_sql($query);
$result = $DB->get_records_sql($query, $params);
$methodsigs = array();

if (is_array($result)) {
Expand All @@ -617,17 +620,18 @@ function mnet_system($method, $params, $hostinfo) {
rpc.help,
rpc.profile
FROM
'.$CFG->prefix.'mnet_host2service h2s,
'.$CFG->prefix.'mnet_service2rpc s2r,
'.$CFG->prefix.'mnet_rpc rpc
{mnet_host2service} h2s,
{mnet_service2rpc} s2r,
{mnet_rpc} rpc
WHERE
rpc.xmlrpc_path = \''.$params[0].'\' AND
rpc.xmlrpc_path = ? AND
s2r.rpcid = rpc.id AND
h2s.publish =\'1\' AND
h2s.serviceid = s2r.serviceid AND
h2s.hostid in ('.$id_list .')';
$params = array($params[0]);

$result = get_record_sql($query);
$result = $DB->get_record_sql($query, $params);

if (is_object($result)) {
return $result->help;
Expand All @@ -641,16 +645,17 @@ function mnet_system($method, $params, $hostinfo) {
h2s.publish,
h2s.subscribe
FROM
'.$CFG->prefix.'mnet_host2service h2s,
'.$CFG->prefix.'mnet_service s
{mnet_host2service} h2s,
{mnet_service} s
WHERE
h2s.serviceid = s.id AND
(h2s.publish =\'1\' OR h2s.subscribe =\'1\') AND
h2s.hostid in ('.$id_list .')
ORDER BY
s.name ASC';
$params = array();

$result = get_records_sql($query);
$result = $DB->get_records_sql($query, $params);
$services = array();

if (is_array($result)) {
Expand Down

0 comments on commit cc38ff5

Please sign in to comment.