MDL-36526 calendar: Convert webcal:// to http://
authorDan Poltawski <dan@moodle.com>
Thu, 15 Nov 2012 03:17:07 +0000 (11:17 +0800)
committerDan Poltawski <dan@moodle.com>
Thu, 15 Nov 2012 07:48:24 +0000 (15:48 +0800)
Curl doesn't understand webcal, so needs to use http. Also fixed some
php warnings spotted during testing.

calendar/managesubscriptions_form.php

index 50a0903..6488847 100644 (file)
@@ -104,22 +104,38 @@ class calendar_addsubscription_form extends moodleform {
      */
     public function validation($data, $files) {
         $errors = parent::validation($data, $files);
      */
     public function validation($data, $files) {
         $errors = parent::validation($data, $files);
-        $url = $data['url'];
-        if (empty($url) && empty($data['importfile'])) {
+
+        if (empty($data['url']) && empty($data['importfile'])) {
             if (!empty($data['importfrom']) && $data['importfrom'] == CALENDAR_IMPORT_FROM_FILE) {
                 $errors['importfile'] = get_string('errorrequiredurlorfile', 'calendar');
             } else {
                 $errors['url'] = get_string('errorrequiredurlorfile', 'calendar');
             }
             if (!empty($data['importfrom']) && $data['importfrom'] == CALENDAR_IMPORT_FROM_FILE) {
                 $errors['importfile'] = get_string('errorrequiredurlorfile', 'calendar');
             } else {
                 $errors['url'] = get_string('errorrequiredurlorfile', 'calendar');
             }
-        } elseif (!empty($url)) {
-            // Url is webcal protocol which is not accepted by PARAM_URL.
-            if (stripos($url, "webcal://") === 0) {
-                $url = substr($url, strlen("webcal://"));
-            }
-            if (clean_param($url, PARAM_URL) !== $url) {
+        } else if (!empty($data['url'])) {
+            if (clean_param($data['url'], PARAM_URL) !== $data['url']) {
                 $errors['url']  = get_string('invalidurl', 'error');
             }
         }
         return $errors;
     }
                 $errors['url']  = get_string('invalidurl', 'error');
             }
         }
         return $errors;
     }
+
+    public function definition_after_data() {
+        $mform =& $this->_form;
+
+        $mform->applyFilter('url', 'calendar_addsubscription_form::strip_webcal');
+    }
+
+    /**
+     * Replace webcal:// urls with http:// as
+     * curl does not understand this protocol
+     *
+     * @param string @url url to examine
+     * @return string url with webcal:// replaced
+     */
+    public static function strip_webcal($url) {
+        if (strpos($url, 'webcal://') === 0) {
+            $url = str_replace('webcal://', 'http://', $url);
+        }
+        return $url;
+    }
 }
 }