Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Send an "X-Ajax-Location" header to redirect upon Ajax requests (see #…
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Sep 26, 2013
1 parent 3503e2e commit 6f8d8ac
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 29 deletions.
8 changes: 4 additions & 4 deletions assets/contao/js/core-uncompressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,10 @@ var AjaxRequest =
// Send request
if (publish) {
image.src = image.src.replace('invisible.gif', 'visible.gif');
new Request({'url':window.location.href}).get({'tid':id, 'state':1});
new Request.Contao({'url':window.location.href}).get({'tid':id, 'state':1});
} else {
image.src = image.src.replace('visible.gif', 'invisible.gif');
new Request({'url':window.location.href}).get({'tid':id, 'state':0});
new Request.Contao({'url':window.location.href}).get({'tid':id, 'state':0});
}

return false;
Expand Down Expand Up @@ -1184,13 +1184,13 @@ var Backend =
pid = el.getPrevious('li').get('id').replace(/li_/, ''),
req = window.location.search.replace(/id=[0-9]*/, 'id=' + id) + '&act=cut&mode=1&pid=' + pid,
href = window.location.href.replace(/\?.*$/, '');
new Request({'url':href+req}).get();
new Request.Contao({'url':href+req}).get();
} else if (el.getParent('ul')) {
var id = el.get('id').replace(/li_/, ''),
pid = el.getParent('ul').get('id').replace(/ul_/, ''),
req = window.location.search.replace(/id=[0-9]*/, 'id=' + id) + '&act=cut&mode=2&pid=' + pid,
href = window.location.href.replace(/\?.*$/, '');
new Request({'url':href+req}).get();
new Request.Contao({'url':href+req}).get();
}
});
},
Expand Down
2 changes: 1 addition & 1 deletion assets/contao/js/core.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion assets/mootools/mootao/Mootao-uncompressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ Request.Contao = new Class(
},

success: function(text) {
var json;
var url = this.getHeader('X-Ajax-Location'),
json;

if (url) {
location.replace(url);
return;
}

// Support both plain text and JSON responses
try {
Expand Down
2 changes: 1 addition & 1 deletion assets/mootools/mootao/Mootao.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions system/docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Contao Open Source CMS Changelog
Version 3.2.beta1 (2013-XX-XX)
------------------------------

### Changed
Send an "X-Ajax-Location" header to redirect upon Ajax requests (see #5647).

### New
Added new DCA table config flags (see #5254):

Expand Down
3 changes: 2 additions & 1 deletion system/initialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ class_alias('Contao\\ModuleLoader', 'ModuleLoader');
// Force a JavaScript redirect upon Ajax requests (IE requires absolute link)
if (Environment::get('isAjaxRequest'))
{
echo '<script>location.replace("' . Environment::get('base') . 'contao/")</script>';
header('X-Ajax-Location: ' . Environment::get('base') . 'contao/');
exit;
}
else
{
Expand Down
7 changes: 0 additions & 7 deletions system/modules/core/classes/BackendUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,6 @@ public function authenticate()
$strRedirect .= '?referer=' . base64_encode(\Environment::get('request'));
}

// Force JavaScript redirect on Ajax requests (IE requires an absolute link)
if (\Environment::get('isAjaxRequest'))
{
echo '<script>location.replace("' . \Environment::get('base') . $strRedirect . '")</script>';
exit;
}

\Controller::redirect($strRedirect);
}

Expand Down
27 changes: 13 additions & 14 deletions system/modules/core/library/Contao/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2078,15 +2078,16 @@ public static function addToUrl($strRequest)
*/
public static function reload()
{
$strLocation = \Environment::get('url') . \Environment::get('requestUri');
$strLocation = \Environment::get('uri');

// Ajax request
if (\Environment::get('isAjaxRequest'))
{
echo $strLocation;
header('X-Ajax-Location: ' . $strLocation);
exit;
}

// Headers have been sent before
if (headers_sent())
{
exit;
Expand All @@ -2107,19 +2108,26 @@ public static function redirect($strLocation, $intStatus=303)
{
$strLocation = str_replace('&amp;', '&', $strLocation);

// Make the location an absolute URL
if (!preg_match('@^https?://@i', $strLocation))
{
$strLocation = \Environment::get('base') . $strLocation;
}

// Ajax request
if (\Environment::get('isAjaxRequest'))
{
echo $strLocation;
header('X-Ajax-Location: ' . $strLocation);
exit;
}

// Headers have been sent before
if (headers_sent())
{
exit;
}

// Header
// Add the HTTP header
switch ($intStatus)
{
case 301:
Expand All @@ -2139,16 +2147,7 @@ public static function redirect($strLocation, $intStatus=303)
break;
}

// Check the target address
if (preg_match('@^https?://@i', $strLocation))
{
header('Location: ' . $strLocation);
}
else
{
header('Location: ' . \Environment::get('base') . $strLocation);
}

header('Location: ' . $strLocation);
exit;
}

Expand Down
11 changes: 11 additions & 0 deletions system/modules/core/library/Contao/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,17 @@ protected static function url()
}


/**
* Return the current URL with path or query string
*
* @return string The URL
*/
protected static function uri()
{
return static::get('url') . static::get('requestUri');
}


/**
* Return the real REMOTE_ADDR even if a proxy server is used
*
Expand Down

0 comments on commit 6f8d8ac

Please sign in to comment.