MDL-38456 Allow colons in paths passed to mdeploy.php utility
authorDavid Mudrák <david@moodle.com>
Wed, 13 Mar 2013 11:08:38 +0000 (12:08 +0100)
committerDavid Mudrák <david@moodle.com>
Wed, 13 Mar 2013 11:08:38 +0000 (12:08 +0100)
The input_manager now accepts colons (:) in TYPE_PATH options, but only
in cases where the colon is used as a part of Windows drive labels, as
in C:\apache.

mdeploy.php
mdeploytest.php

index 8d7884b..303d3d1 100644 (file)
@@ -264,8 +264,17 @@ class input_manager extends singleton_pattern {
                 if (strpos($raw, '~') !== false) {
                     throw new invalid_option_exception('Using the tilde (~) character in paths is not supported');
                 }
+                $colonpos = strpos($raw, ':');
+                if ($colonpos !== false) {
+                    if ($colonpos !== 1 or strrpos($raw, ':') !== 1) {
+                        throw new invalid_option_exception('Using the colon (:) character in paths is supported for Windows drive labels only.');
+                    }
+                    if (preg_match('/^[a-zA-Z]:/', $raw) !== 1) {
+                        throw new invalid_option_exception('Using the colon (:) character in paths is supported for Windows drive labels only.');
+                    }
+                }
                 $raw = str_replace('\\', '/', $raw);
-                $raw = preg_replace('~[[:cntrl:]]|[&<>"`\|\':]~u', '', $raw);
+                $raw = preg_replace('~[[:cntrl:]]|[&<>"`\|\']~u', '', $raw);
                 $raw = preg_replace('~\.\.+~', '', $raw);
                 $raw = preg_replace('~//+~', '/', $raw);
                 $raw = preg_replace('~/(\./)+~', '/', $raw);
index 4724172..76800eb 100644 (file)
@@ -144,7 +144,9 @@ class mdeploytest extends PHPUnit_Framework_TestCase {
             array('0', input_manager::TYPE_FLAG, true),
             array('muhehe', input_manager::TYPE_FLAG, true),
 
-            array('C:\\WINDOWS\\user.dat', input_manager::TYPE_PATH, 'C/WINDOWS/user.dat'),
+            array('C:\\WINDOWS\\user.dat', input_manager::TYPE_PATH, 'C:/WINDOWS/user.dat'),
+            array('D:\xampp\htdocs\24_integration/mdeploy.php', input_manager::TYPE_PATH, 'D:/xampp/htdocs/24_integration/mdeploy.php'),
+            array('d:/xampp/htdocs/24_integration/mdeploy.php', input_manager::TYPE_PATH, 'd:/xampp/htdocs/24_integration/mdeploy.php'),
             array('../../../etc/passwd', input_manager::TYPE_PATH, '/etc/passwd'),
             array('///////.././public_html/test.php', input_manager::TYPE_PATH, '/public_html/test.php'),
 
@@ -163,6 +165,30 @@ class mdeploytest extends PHPUnit_Framework_TestCase {
         );
     }
 
+    /**
+     * @expectedException invalid_option_exception
+     */
+    public function test_input_type_path_multiple_colons() {
+        $input = testable_input_manager::instance();
+        $input->cast_value('C:\apache\log:file', input_manager::TYPE_PATH); // must throw exception
+    }
+
+    /**
+     * @expectedException invalid_option_exception
+     */
+    public function test_input_type_path_invalid_drive_label() {
+        $input = testable_input_manager::instance();
+        $input->cast_value('0:/srv/moodledata', input_manager::TYPE_PATH); // must throw exception
+    }
+
+    /**
+     * @expectedException invalid_option_exception
+     */
+    public function test_input_type_path_invalid_colon() {
+        $input = testable_input_manager::instance();
+        $input->cast_value('/var/www/moodle:2.5', input_manager::TYPE_PATH); // must throw exception
+    }
+
     /**
      * @expectedException invalid_coding_exception
      */