Skip to content

Commit

Permalink
Bug 10332 - Import of incorrectly formatted Moodle XML is not handle…
Browse files Browse the repository at this point in the history
…d well
  • Loading branch information
mkassaei committed Sep 27, 2010
1 parent 831febe commit acfbfec
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions lang/en_utf8/error.php
Expand Up @@ -41,6 +41,7 @@
$string['errorreadingfile'] = 'Error reading file \"$a\"';
$string['errorsavingrequest'] = 'An error occurred when trying to save your request.';
$string['errorunzippingfiles'] = 'Error unzipping files';
$string['errorxmlfile'] = 'Incorrect xml file (<strong>$a->errorstring</strong>, line <strong>$a->errorline</strong>, character <strong>$a->errorchar</strong>)';
$string['fieldrequired'] = '\"$a\" is a required field';
$string['filenotfound'] = 'Sorry, the requested file could not be found';
$string['forumblockingtoomanyposts'] = 'You have exceeded the posting threshold set for this forum';
Expand Down
32 changes: 30 additions & 2 deletions lib/xmlize.php
@@ -1,4 +1,20 @@
<?php
/**
* Exception indicating errors in xml file
*
* @package moodlecore
* @copyright 2010 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class xml_format_exception extends moodle_exception {
function __construct($errorstring, $errorline, $errorchar) {
$errorinfo = new stdClass;
$errorinfo->errorstring = $errorstring;
$errorinfo->errorline = $errorline;
$errorinfo->errorchar = $errorchar;
parent::__construct('errorxmlfile', 'error', '', $errorinfo);
}
}

/**
* xmlize.php - xmlize() is by Hans Anderson, {@link http://www.hansanderson.com/contact/}
Expand Down Expand Up @@ -41,16 +57,28 @@
* @param array $data The array to be converted
* @param int $WHITE If set to 1 allows the parser to skip "space" characters in xml document. Default is 1
* @param string $encoding Specify an OUTPUT encoding. If not specified, it defaults to UTF-8.
* @param bool $error if set on true it checks whether xml-file is well-formed and throw an exception when xml is not well-formed,
* @return array
*/
function xmlize($data, $WHITE=1, $encoding='UTF-8') {
function xmlize($data, $WHITE=1, $encoding='UTF-8', $error=false) {

$data = trim($data);
$vals = $index = $array = array();
$parser = xml_parser_create($encoding);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);
xml_parse_into_struct($parser, $data, $vals, $index);
xml_parse_into_struct($parser, $data, &$vals, &$index);

// Error handling when the xml file is not well-formed
if ($error) {
$errorcode = xml_get_error_code($parser);
if ($errorcode) {
$errorstring = xml_error_string($errorcode);
$errorline = xml_get_current_line_number($parser);
$errorchar = xml_get_current_column_number($parser);
throw new xml_format_exception($errorstring, $errorline, $errorchar);
}
}
xml_parser_free($parser);

$i = 0;
Expand Down
3 changes: 3 additions & 0 deletions question/format.php
Expand Up @@ -242,6 +242,9 @@ function importprocess() {
}

if (! $questions = $this->readquestions($lines)) { // Extract all the questions
if (!is_array($questions)) {
return false;
}
notify( get_string('noquestionsinfile','quiz') );
return false;
}
Expand Down
8 changes: 6 additions & 2 deletions question/format/xml/format.php
Expand Up @@ -652,8 +652,12 @@ function readquestions($lines) {

// This converts xml to big nasty data structure
// the 0 means keep white space as it is (important for markdown format)
$xml = xmlize($text, 0);

try {
$xml = xmlize($text, 0, 'UTF-8', true);
} catch (xml_format_exception $e){
$this->error ($e->getMessage(), '');
return false;
}
// Set up array to hold all our questions
$questions = array();

Expand Down

0 comments on commit acfbfec

Please sign in to comment.