Skip to content

Commit

Permalink
MDL-36641 Missing HTTP check when using an external resource
Browse files Browse the repository at this point in the history
  • Loading branch information
scara committed Nov 26, 2012
1 parent f42c34a commit f833171
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions mod/scorm/lang/en/scorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
$string['interactionslearnerresponse'] = 'Learner\'s Response';
$string['invalidactivity'] = 'Scorm activity is incorrect';
$string['invalidurl'] = 'Invalid URL specified';
$string['invalidurlhttpcheck'] = 'Invalid URL specified. Debug message:<pre>{$a->cmsg}</pre>';
$string['invalidhacpsession'] = 'Invalid HACP Session';
$string['invalidmanifestresource'] = 'WARNING: The following resources were referenced in your manifest but couldn\'t be found:';
$string['last'] = 'Last accessed on';
Expand Down
24 changes: 24 additions & 0 deletions mod/scorm/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1864,3 +1864,27 @@ function scorm_get_adlnav_json ($scoes, &$adlnav = array(), $parentscoid = null)
}
return json_encode($adlnav);
}

/**
* Check for the availability of a resource by URL.
*
* Check is performed using an HTTP HEAD call.
*
* @param $url string A valid URL
* @return bool|string True if no issue is found. The error string message, otherwise
*/
function scorm_check_url($url) {
$curl = new curl;

if (!ini_get('open_basedir') and !ini_get('safe_mode')) {
// Same options as in {@link download_file_content()}, used in {@link scorm_parse_scorm()}.
$curl->setopt(array('CURLOPT_FOLLOWLOCATION' => true, 'CURLOPT_MAXREDIRS' => 5));
}
$cmsg = $curl->head($url);
$info = $curl->get_info();
if (empty($info['http_code']) || $info['http_code'] != 200) {
return get_string('invalidurlhttpcheck', 'scorm', array('cmsg' => $cmsg));
}

return true;
}
23 changes: 23 additions & 0 deletions mod/scorm/mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,26 +393,49 @@ function validation($data, $files) {

} else if ($type === SCORM_TYPE_EXTERNAL) {
$reference = $data['packageurl'];
// Syntax check.
if (!preg_match('/(http:\/\/|https:\/\/|www).*\/imsmanifest.xml$/i', $reference)) {
$errors['packageurl'] = get_string('invalidurl', 'scorm');
} else {
// Availability check.
$result = scorm_check_url($reference);
if (is_string($result)) {
$errors['packageurl'] = $result;
}
}

} else if ($type === 'packageurl') {
$reference = $data['reference'];
// Syntax check.
if (!preg_match('/(http:\/\/|https:\/\/|www).*(\.zip|\.pif)$/i', $reference)) {
$errors['packageurl'] = get_string('invalidurl', 'scorm');
} else {
// Availability check.
$result = scorm_check_url($reference);
if (is_string($result)) {
$errors['packageurl'] = $result;
}
}

} else if ($type === SCORM_TYPE_IMSREPOSITORY) {
$reference = $data['packageurl'];
if (stripos($reference, '#') !== 0) {
$errors['packageurl'] = get_string('invalidurl', 'scorm');
}

} else if ($type === SCORM_TYPE_AICCURL) {
$reference = $data['packageurl'];
// Syntax check.
if (!preg_match('/(http:\/\/|https:\/\/|www).*/', $reference)) {
$errors['packageurl'] = get_string('invalidurl', 'scorm');
} else {
// Availability check.
$result = scorm_check_url($reference);
if (is_string($result)) {
$errors['packageurl'] = $result;
}
}

}

return $errors;
Expand Down

0 comments on commit f833171

Please sign in to comment.