From e9e567f33ce45a46c5245e5772cde8dc5e444c5b Mon Sep 17 00:00:00 2001 From: Jerome Mouneyrac Date: Tue, 4 Dec 2012 11:47:19 +0800 Subject: [PATCH] MDL-29805 add REQUIRE_CORRECT_ACCESS define + return error code when AJAX_SCRIPT fails --- lang/en/error.php | 1 + lib/setuplib.php | 21 ++++++++++++++++++--- login/token.php | 1 + 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lang/en/error.php b/lang/en/error.php index 543a49f3158..b2117881796 100644 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -435,6 +435,7 @@ $string['refoundtoorigi'] = 'Refunded to original amount: {$a}'; $string['remotedownloaderror'] = 'Download of component to your server failed, please verify proxy settings, PHP cURL extension is highly recommended.

You must download the {$a->url} file manually, copy it to "{$a->dest}" in your server and unzip it there.'; $string['remotedownloadnotallowed'] = 'Download of components to your server isn\'t allowed (allow_url_fopen is disabled).

You must download the {$a->url} file manually, copy it to "{$a->dest}" in your server and unzip it there.'; $string['reportnotavailable'] = 'This type of report is only available for the site course'; +$string['requirecorrectaccess'] = 'Invalid url or port.'; $string['requireloginerror'] = 'Course or activity not accessible.'; $string['restorechecksumfailed'] = 'Some problem happened with the restore information stored in your session. Please check your PHP memory/DB package size limits. Restore stopped.'; $string['restore_path_element_missingmethod'] = 'Restore method {$a} is missing. It must be defined by a developer.'; diff --git a/lib/setuplib.php b/lib/setuplib.php index 01c2f580282..43c6937022c 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -356,7 +356,7 @@ function default_exception_handler($ex) { } if (is_early_init($info->backtrace)) { - echo bootstrap_renderer::early_error($info->message, $info->moreinfourl, $info->link, $info->backtrace, $info->debuginfo); + echo bootstrap_renderer::early_error($info->message, $info->moreinfourl, $info->link, $info->backtrace, $info->debuginfo, $info->errorcode); } else { try { if ($DB) { @@ -370,7 +370,7 @@ function default_exception_handler($ex) { // so we just print at least something instead of "Exception thrown without a stack frame in Unknown on line 0":-( if (CLI_SCRIPT or AJAX_SCRIPT) { // just ignore the error and send something back using the safest method - echo bootstrap_renderer::early_error($info->message, $info->moreinfourl, $info->link, $info->backtrace, $info->debuginfo); + echo bootstrap_renderer::early_error($info->message, $info->moreinfourl, $info->link, $info->backtrace, $info->debuginfo, $info->errorcode); } else { echo bootstrap_renderer::early_error_content($info->message, $info->moreinfourl, $info->link, $info->backtrace, $info->debuginfo); $outinfo = get_exception_info($out_ex); @@ -761,6 +761,20 @@ function initialise_fullme() { if (!defined('NO_MOODLE_COOKIES')) { define('NO_MOODLE_COOKIES', true); } + // The login/token.php script should call the correct url/port. + if (defined('REQUIRE_CORRECT_ACCESS')) { + $wwwrootport = empty($wwwroot['port'])?'':$wwwroot['port']; + $calledurl = $rurl['host']; + if (!empty($rurl['port'])) { + $calledurl .= ':'. $rurl['port']; + } + $correcturl = $wwwroot['host']; + if (!empty($wwwrootport)) { + $correcturl .= ':'. $wwwrootport; + } + throw new moodle_exception('requirecorrectaccess', 'error', '', null, + 'You called ' . $calledurl .', you should have called ' . $correcturl); + } redirect($CFG->wwwroot, get_string('wwwrootmismatch', 'error', $CFG->wwwroot), 3); } } @@ -1473,7 +1487,7 @@ width: 80%; -moz-border-radius: 20px; padding: 15px"> * @param string $debuginfo extra information for developers * @return string */ - public static function early_error($message, $moreinfourl, $link, $backtrace, $debuginfo = null) { + public static function early_error($message, $moreinfourl, $link, $backtrace, $debuginfo = null, $errorcode = null) { global $CFG; if (CLI_SCRIPT) { @@ -1501,6 +1515,7 @@ width: 80%; -moz-border-radius: 20px; padding: 15px"> $e->stacktrace = format_backtrace($backtrace, true); } } + $e->errorcode = $errorcode; @header('Content-Type: application/json; charset=utf-8'); echo json_encode($e); return; diff --git a/login/token.php b/login/token.php index 050e714c259..f61081c8e15 100644 --- a/login/token.php +++ b/login/token.php @@ -22,6 +22,7 @@ */ define('AJAX_SCRIPT', true); +define('REQUIRE_CORRECT_ACCESS', true); define('NO_MOODLE_COOKIES', true); require_once(dirname(dirname(__FILE__)) . '/config.php');