Skip to content

Commit

Permalink
MDL-77341 mnet: Added missing class properties
Browse files Browse the repository at this point in the history
In PHP 8.2 and later, setting a value to an undeclared class property is
deprecated and emits a deprecation notice.
So we need to add missing class properties that still need to be declared.
  • Loading branch information
meirzamoodle committed May 20, 2023
1 parent 2e1c6fd commit e95540d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 21 deletions.
2 changes: 0 additions & 2 deletions admin/mnet/peers.php
Expand Up @@ -83,7 +83,6 @@
$mnet_peer->bootstrap($formdata->wwwroot, null, $application);
// bootstrap the second form straight with the data from the first form
$reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer)); // the second step (also the edit host form)
$formdata->oldpublickey = $mnet_peer->public_key; // set this so we can confirm on form post without having to recreate the mnet_peer object
$reviewform->set_data($mnet_peer);
echo $OUTPUT->header();
echo $OUTPUT->box_start();
Expand Down Expand Up @@ -115,7 +114,6 @@
}
$credentials = $mnet_peer->check_credentials($mnet_peer->public_key);
$reviewform = new mnet_review_host_form(null, array('peer' => $mnet_peer)); // the second step (also the edit host form)
$mnet_peer->oldpublickey = $mnet_peer->public_key; // set this so we can confirm on form post without having to recreate the mnet_peer object
$reviewform->set_data((object)$mnet_peer);
echo $OUTPUT->box_start();
$reviewform->display();
Expand Down
21 changes: 21 additions & 0 deletions mnet/environment.php
Expand Up @@ -17,6 +17,27 @@ class mnet_environment {
var $keypair = array();
var $deleted = 0;

/** @var string mnet host name. */
public $name;

/** @var int mnet host transport. */
public $transport;

/** @var int mnet host port number. */
public $portno;

/** @var int mnet host force theme. */
public $force_theme;

/** @var string mnet host theme. */
public $theme;

/** @var int mnet host application ID. */
public $applicationid;

/** @var int mnet host SSL verification. */
public $sslverification;

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

Expand Down
27 changes: 25 additions & 2 deletions mnet/peer.php
Expand Up @@ -39,6 +39,23 @@ class mnet_peer {
/** @var int $sslverification The level of SSL verification to apply. */
public $sslverification = self::SSL_HOST_AND_PEER;

/** @var int deleted status. */
public $deleted;

/** @var stdClass data from mnet_application table in DB. */
public $application;

/**
* Current SSL public key
*
* MNet need to compare the remote machine's SSL Cert and the public key to warn users of any mismatch.
* The property is the remote machine's SSL Cert.
*
* @see admin/mnet/peers.php
* @var string
*/
public $currentkey;

/*
* Fetch information about a peer identified by wwwroot
* If information does not preexist in db, collect it together based on
Expand Down Expand Up @@ -255,8 +272,7 @@ function set_id($id) {
global $CFG, $DB;

if (clean_param($id, PARAM_INT) != $id) {
$this->errno[] = 1;
$this->errmsg[] = 'Your id ('.$id.') is not legal';
$this->error[] = ['code' => 1, 'text' => 'Your id ('.$id.') is not legal'];
return false;
}

Expand Down Expand Up @@ -301,7 +317,14 @@ function populate($hostinfo) {
$this->bootstrapped = true;
}

/**
* Get public key.
*
* @deprecated since Moodle 4.3
* @todo MDL-78304 Final deprecation.
*/
function get_public_key() {
debugging('Function get_public_key() is deprecated.', DEBUG_DEVELOPER);
if (isset($this->public_key_ref)) return $this->public_key_ref;
$this->public_key_ref = openssl_pkey_get_public($this->public_key);
return $this->public_key_ref;
Expand Down
34 changes: 17 additions & 17 deletions mnet/xmlrpc/client.php
Expand Up @@ -124,9 +124,11 @@ public function add_param($argument, $type = 'string') {
*
* @param object $mnet_peer A mnet_peer object with details of the
* remote host we're connecting to
* @param bool $rekey The rekey attribute stops us from
* getting into a loop.
* @return mixed A PHP variable, as returned by the
*/
public function send($mnet_peer) {
public function send($mnet_peer, bool $rekey = false) {
global $CFG, $DB;

if (!$this->permission_to_call($mnet_peer)) {
Expand All @@ -135,16 +137,16 @@ public function send($mnet_peer) {
}

$request = new \PhpXmlRpc\Request($this->method, $this->params);
$this->requesttext = $request->serialize('utf-8');
$requesttext = $request->serialize('utf-8');

$this->signedrequest = mnet_sign_message($this->requesttext);
$this->encryptedrequest = mnet_encrypt_message($this->signedrequest, $mnet_peer->public_key);
$signedrequest = mnet_sign_message($requesttext);
$encryptedrequest = mnet_encrypt_message($signedrequest, $mnet_peer->public_key);

$client = $this->prepare_http_request($mnet_peer);

$timestamp_send = time();
mnet_debug("about to send the xmlrpc request");
$response = $client->send($this->encryptedrequest, $this->timeout);
$response = $client->send($encryptedrequest, $this->timeout);
mnet_debug("managed to complete a xmlrpc request");
$timestamp_receive = time();

Expand All @@ -153,13 +155,12 @@ public function send($mnet_peer) {
return false;
}

$this->rawresponse = $response->value(); // Because MNet responses ARE NOT valid xmlrpc, don't try any PhpXmlRpc facility.
$this->rawresponse = trim($this->rawresponse);
$rawresponse = trim($response->value()); // Because MNet responses ARE NOT valid xmlrpc, don't try any PhpXmlRpc facility.

$mnet_peer->touch();

$crypt_parser = new mnet_encxml_parser();
$crypt_parser->parse($this->rawresponse);
$crypt_parser->parse($rawresponse);

// If we couldn't parse the message, or it doesn't seem to have encrypted contents,
// give the most specific error msg available & return
Expand Down Expand Up @@ -254,11 +255,11 @@ public function send($mnet_peer) {
}
}

$this->xmlrpcresponse = base64_decode($sig_parser->data_object);
$xmlrpcresponse = base64_decode($sig_parser->data_object);
// Let's convert the xmlrpc back to PHP structure.
$response = null;
$encoder = new \PhpXmlRpc\Encoder();
$oresponse = $encoder->decodeXML($this->xmlrpcresponse); // First, to internal PhpXmlRpc\Response structure.
$oresponse = $encoder->decodeXML($xmlrpcresponse); // First, to internal PhpXmlRpc\Response structure.
if ($oresponse instanceof \PhpXmlRpc\Response) {
// Special handling of fault responses (because value() doesn't handle them properly).
if ($oresponse->faultCode()) {
Expand All @@ -276,8 +277,8 @@ public function send($mnet_peer) {
if (is_array($this->response) && array_key_exists('faultCode', $this->response)) {
// The faultCode 7025 means we tried to connect with an old SSL key
// The faultString is the new key - let's save it and try again
// The re_key attribute stops us from getting into a loop
if($this->response['faultCode'] == 7025 && empty($mnet_peer->re_key)) {
// The rekey attribute stops us from getting into a loop
if($this->response['faultCode'] == 7025 && empty($rekey)) {
mnet_debug('recieved an old-key fault, so trying to get the new key and update our records');
// If the new certificate doesn't come thru clean_param() unmolested, error out
if($this->response['faultString'] != clean_param($this->response['faultString'], PARAM_PEM)) {
Expand All @@ -296,8 +297,7 @@ public function send($mnet_peer) {
// Create a new peer object populated with the new info & try re-sending the request
$rekeyed_mnet_peer = new mnet_peer();
$rekeyed_mnet_peer->set_id($record->id);
$rekeyed_mnet_peer->re_key = true;
return $this->send($rekeyed_mnet_peer);
return $this->send($rekeyed_mnet_peer, true); // Re-send mnet_peer with the new key.
}
if (!empty($CFG->mnet_rpcdebug)) {
if (get_string_manager()->string_exists('error'.$this->response['faultCode'], 'mnet')) {
Expand All @@ -313,7 +313,7 @@ public function send($mnet_peer) {

// ok, it's signed, but is it signed with the right certificate ?
// do this *after* we check for an out of date key
$verified = openssl_verify($this->xmlrpcresponse, base64_decode($sig_parser->signature), $mnet_peer->public_key);
$verified = openssl_verify($xmlrpcresponse, base64_decode($sig_parser->signature), $mnet_peer->public_key);
if ($verified != 1) {
$this->error[] = 'Invalid signature';
}
Expand Down Expand Up @@ -376,11 +376,11 @@ public function permission_to_call($mnet_peer) {
* @return \PhpXmlRpc\Client handle - the almost-ready-to-send http request
*/
public function prepare_http_request ($mnet_peer) {
$this->uri = $mnet_peer->wwwroot . $mnet_peer->application->xmlrpc_server_url;
$uri = $mnet_peer->wwwroot . $mnet_peer->application->xmlrpc_server_url;

// Instantiate the xmlrpc client to be used for the client request
// and configure it the way we want.
$client = new \PhpXmlRpc\Client($this->uri);
$client = new \PhpXmlRpc\Client($uri);
$client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_ALWAYS);
$client->setUserAgent('Moodle');
$client->return_type = 'xml'; // Because MNet responses ARE NOT valid xmlrpc, don't try any validation.
Expand Down
40 changes: 40 additions & 0 deletions mnet/xmlrpc/xmlparser.php
Expand Up @@ -12,6 +12,46 @@
* Custom XML parser class for signed and/or encrypted XML Docs
*/
class mnet_encxml_parser {

/** @var resource|false|XMLParser — a resource handle for the new XML parser. */
private $parser;

/** @var int unique ID for each tag. */
private $tag_number;

/** @var string digest string. */
private $digest;

/** @var string remote_timestamp string. */
public $remote_timestamp;

/** @var string remote_wwwroot string. */
public $remote_wwwroot;

/** @var string signature string. */
public $signature;

/** @var string data_object string. */
public $data_object;

/** @var string URI value inside the RETRIEVALMETHOD xml tag. */
private $key_URI;

/** @var bool true if $chiper has a value, otherwise false. */
public $payload_encrypted;

/** @var array the chiper string. */
public $cipher = [];

/** @var array error information with code and string keys. */
public $error = [];

/** @var string The remote error string, specified in the discard_data(). */
public $remoteerror;

/** @var stdClass error started status. */
private $errorstarted;

/**
* Constructor creates and initialises parser resource and calls initialise
*
Expand Down

0 comments on commit e95540d

Please sign in to comment.