Skip to content

Commit

Permalink
Merge branch 'MDL-57775-master-xmlrpc' of git://github.com/mudrd8mz/m…
Browse files Browse the repository at this point in the history
…oodle
  • Loading branch information
junpataleta committed Aug 22, 2017
2 parents 474c7ee + 8bfd82b commit b5d6f7d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
5 changes: 4 additions & 1 deletion mnet/lib.php
Expand Up @@ -56,7 +56,10 @@ function mnet_get_public_key($uri, $application=null) {
$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"));
$rq = xmlrpc_encode_request('system/keyswap', array($CFG->wwwroot, $mnet->public_key, $application->name), array(
'encoding' => 'utf-8',
'escaping' => 'markup',
));
$ch = curl_init($uri . $application->xmlrpc_server_url);

curl_setopt($ch, CURLOPT_TIMEOUT, 60);
Expand Down
35 changes: 23 additions & 12 deletions webservice/xmlrpc/lib.php
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.


/**
* Moodle XML-RPC library
*
Expand All @@ -23,11 +22,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Moodle XML-RPC client
*
* It has been implemented for unit testing purpose (all protocols have similar client)
*
* @package webservice_xmlrpc
* @copyright 2010 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Expand Down Expand Up @@ -73,15 +72,7 @@ public function call($functionname, $params = array()) {
$this->serverurl->param('wstoken', $this->token);
}

// Set output options.
$outputoptions = array(
'encoding' => 'utf-8'
);

// Encode the request.
// See MDL-53962 - needed for backwards compatibility on <= 3.0
$params = array_values($params);
$request = xmlrpc_encode_request($functionname, $params, $outputoptions);
$request = $this->encode_request($functionname, $params);

// Set the headers.
$headers = array(
Expand All @@ -102,4 +93,24 @@ public function call($functionname, $params = array()) {

return $result;
}

/**
* Generates XML for a method request.
*
* @param string $functionname Name of the method to call.
* @param mixed $params Method parameters compatible with the method signature.
* @return string
*/
protected function encode_request($functionname, $params) {

$outputoptions = array(
'encoding' => 'utf-8',
'escaping' => 'markup',
);

// See MDL-53962 - needed for backwards compatibility on <= 3.0.
$params = array_values($params);

return xmlrpc_encode_request($functionname, $params, $outputoptions);
}
}
32 changes: 32 additions & 0 deletions webservice/xmlrpc/tests/lib_test.php
Expand Up @@ -88,6 +88,27 @@ public function test_client_with_fault_response() {
$this->expectException('moodle_exception');
$client->call('testfunction');
}

/**
* Test the XML-RPC request encoding.
*/
public function test_encode_request() {

$client = new webservice_xmlrpc_client_mock('/webservice/xmlrpc/server.php', 'anytoken');

// Encode the request with the proper encoding and escaping options.
$xml = $client->encode_request('do_it', ['foo' => '<bar>ŠČŘŽÝÁÍÉ</bar>']);

// Assert that decoding with explicit encoding will work. This appeared
// to fail if the markup escaping was not set.
$this->assertEquals(['<bar>ŠČŘŽÝÁÍÉ</bar>'], xmlrpc_decode($xml, 'UTF-8'));

// Our experiments show that even with default/implicit encoding,
// requests encoded with markup escaping set are also decoded
// correctly. This is known to be used in some servers so we test it
// here, too.
$this->assertEquals(['<bar>ŠČŘŽÝÁÍÉ</bar>'], xmlrpc_decode($xml));
}
}

/**
Expand Down Expand Up @@ -137,4 +158,15 @@ public function call($functionname, $params = array()) {

return $result;
}

/**
* Allows to test the request encoding.
*
* @param string $functionname Name of the method to call.
* @param mixed $params Method parameters compatible with the method signature.
* @return string
*/
public function encode_request($functionname, $params) {
return parent::encode_request($functionname, $params);
}
}

0 comments on commit b5d6f7d

Please sign in to comment.