X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2FUtils.php;h=658b97bc2f03fff6d2f5580fa912165d1a422cdb;hb=775803a05cdba9d7fc1b37af4b15ecd80a8cbcc2;hp=a1e97b356c6ed85e418f7515d0697bd3b949c729;hpb=7f1dfd1c12a143b324fbe68213a49de0586febfa;p=github%2Fshaarli%2FShaarli.git diff --git a/application/Utils.php b/application/Utils.php index a1e97b35..658b97bc 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -84,4 +84,36 @@ function checkDateFormat($format, $string) $date = DateTime::createFromFormat($format, $string); return $date && $date->format($string) == $string; } -?> + +/** + * Generate a header location from HTTP_REFERER. + * Make sure the referer is Shaarli itself and prevent redirection loop. + * + * @param string $referer - HTTP_REFERER. + * @param string $host - Server HOST. + * @param array $loopTerms - Contains list of term to prevent redirection loop. + * + * @return string $referer - final referer. + */ +function generateLocation($referer, $host, $loopTerms = array()) +{ + $final_referer = '?'; + + // No referer if it contains any value in $loopCriteria. + foreach ($loopTerms as $value) { + if (strpos($referer, $value) !== false) { + return $final_referer; + } + } + + // Remove port from HTTP_HOST + if ($pos = strpos($host, ':')) { + $host = substr($host, 0, $pos); + } + + if (!empty($referer) && strpos(parse_url($referer, PHP_URL_HOST), $host) !== false) { + $final_referer = $referer; + } + + return $final_referer; +}