Skip to content

Commit

Permalink
Extracted url/local path normalizing
Browse files Browse the repository at this point in the history
This would reduce code duplication and improve readability
  • Loading branch information
nitriques committed Jul 28, 2016
1 parent 64fbb4d commit f7b45c7
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions lib/class.jit.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,44 @@ public function parseParameters($parameter_string)
);
}

/**
* Given the raw image url, normally coming from the request, this function
* will ensure that the correct protocol is applied, defaulting to http.
*
* @param string $image
* The image url
* @return string
* The normalized url
*/
public function normalizeExternalImageUrl($image)
{
$image_url = null;
if (preg_match('/^https?:\/\/?/i', $image)) {
// User agent will reduce multiple slashes (//) after the protocol.
// This replacement will take this fact into account
$image_url = preg_replace('/^(https?:)\/([^\/])(.+)$/i', '$1//$2$3', $image);
}
else {
$image_url = "http://$image";
}
return $image_url;
}

/**
* Given the raw image path, normally coming from the request, this function
* will ensure that the relative path is a complete absolute path, relative
* to the `WORKSPACE` path.
*
* @param string $image
* The image relative path
* @return string
* The normalized path
*/
public function normalizeLocalImagePath($image)
{
return WORKSPACE . '/' . $image;
}

/**
* Given the parsed parameters, this function will go and grab
* the desired image, whether it be local, or external.
Expand All @@ -270,14 +308,7 @@ public function fetchImagePath(array &$parameters)
{
// Fetch external images
if ($parameters['settings']['external'] === true) {
if (preg_match('/^https?:\/\/?/i', $parameters['image'])) {
// User agent will reduce multiple slashes (//) after the protocol.
// This replacement will take this fact into account
$image_path = preg_replace('/^(https?:)\/([^\/])(.+)$/i', '$1//$2$3', $parameters['image']);
}
else {
$image_path = "http://{$parameters['image']}";
}
$image_path = $this->normalizeExternalImageUrl($parameters['image']);

// Image is external, check to see that it is a trusted source
$rules = @file(WORKSPACE . '/jit-image-manipulation/trusted-sites', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
Expand Down

0 comments on commit f7b45c7

Please sign in to comment.