diff --git a/lib/moodlelib.php b/lib/moodlelib.php
index aabfa86..f5be824 100644
--- a/lib/moodlelib.php
+++ b/lib/moodlelib.php
@@ -8331,31 +8331,7 @@ function object_property_exists( $obj, $property ) {
 function custom_script_path($urlpath='') {
     global $CFG;
 
-    // set default $urlpath, if necessary
-    if (empty($urlpath)) {
-        $urlpath = qualified_me(); // e.g. http://www.this-server.com/moodle/this-script.php
-    }
-
-    // $urlpath is invalid if it is empty or does not start with the Moodle wwwroot
-    if (empty($urlpath) or (strpos($urlpath, $CFG->wwwroot) === false )) {
-        return false;
-    }
-
-    // replace wwwroot with the path to the customscripts folder and clean path
-    $scriptpath = $CFG->customscripts . clean_param(substr($urlpath, strlen($CFG->wwwroot)), PARAM_PATH);
-
-    // remove the query string, if any
-    if (($strpos = strpos($scriptpath, '?')) !== false) {
-        $scriptpath = substr($scriptpath, 0, $strpos);
-    }
-
-    // remove trailing slashes, if any
-    $scriptpath = rtrim($scriptpath, '/\\');
-
-    // append index.php, if necessary
-    if (is_dir($scriptpath)) {
-        $scriptpath .= '/index.php';
-    }
+    $scriptpath = str_replace($CFG->dirroot, $CFG->customscripts, $_SERVER['SCRIPT_FILENAME']);
 
     // check the custom script exists
     if (file_exists($scriptpath)) {
