Browse files

MDL-36641 Missing HTTP check when using an external resource

  • Loading branch information...
1 parent ab1f9aa commit f1c403dcec42b739323514ddf5a86b8e757253bb @scara scara committed with stronk7 Nov 15, 2012
Showing with 48 additions and 0 deletions.
  1. +1 −0 mod/scorm/lang/en/scorm.php
  2. +24 −0 mod/scorm/locallib.php
  3. +23 −0 mod/scorm/mod_form.php
View
1 mod/scorm/lang/en/scorm.php
@@ -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';
View
24 mod/scorm/locallib.php
@@ -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;
+}
View
23 mod/scorm/mod_form.php
@@ -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;

0 comments on commit f1c403d

Please sign in to comment.