Skip to content

Commit

Permalink
Fixes shaarli#356
Browse files Browse the repository at this point in the history
 * adding a link should return added link's hash
* allow redirection relative urls in generateLocation
  • Loading branch information
ArthurHoaro committed Nov 4, 2015
1 parent 38bedfb commit d01c234
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
11 changes: 6 additions & 5 deletions application/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ function checkDateFormat($format, $string)
*/
function generateLocation($referer, $host, $loopTerms = array())
{
$final_referer = '?';
$finalReferer = '?';

// No referer if it contains any value in $loopCriteria.
foreach ($loopTerms as $value) {
if (strpos($referer, $value) !== false) {
return $final_referer;
return $finalReferer;
}
}

Expand All @@ -111,11 +111,12 @@ function generateLocation($referer, $host, $loopTerms = array())
$host = substr($host, 0, $pos);
}

if (!empty($referer) && strpos(parse_url($referer, PHP_URL_HOST), $host) !== false) {
$final_referer = $referer;
$refererHost = parse_url($referer, PHP_URL_HOST);
if (!empty($referer) && (strpos($refererHost, $host) !== false || startsWith('?', $refererHost))) {
$finalReferer = $referer;
}

return $final_referer;
return $finalReferer;
}

/**
Expand Down
10 changes: 7 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -1354,10 +1354,14 @@ function renderPage()
pubsubhub();

// If we are called from the bookmarklet, we must close the popup:
if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) { echo '<script>self.close();</script>'; exit; }
$returnurl = ( !empty($_POST['returnurl']) ? escape($_POST['returnurl']) : '?' );
$returnurl .= '#'.smallHash($_POST['lf_linkdate']); // Scroll to the link which has been edited.
if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) {
echo '<script>self.close();</script>';
exit;
}

$returnurl = !empty($_POST['returnurl']) ? escape($_POST['returnurl']): '?';
$location = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link'));
$location .= '#'.smallHash($_POST['lf_linkdate']); // Scroll to the link which has been edited.
header('Location: '. $location); // After saving the link, redirect to the page the user was on.
exit;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public function testGenerateLocation() {
$this->assertEquals($ref, generateLocation($ref, 'localhost'));
$ref = 'http://localhost:8080/?test';
$this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
$ref = '?localreferer#hash';
$this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
}

/**
Expand Down

1 comment on commit d01c234

@mro
Copy link

@mro mro commented on d01c234 Nov 4, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.