Skip to content

Commit

Permalink
Kill some copypasta to include an http_build_url method, while still …
Browse files Browse the repository at this point in the history
…protecting ourselves from that imgix signature idiosynracy
  • Loading branch information
kellysutton committed May 30, 2015
1 parent c5b0174 commit 44aaa64
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 45 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"imgix"
],
"require": {
"php": ">=5.3"
"php": ">=5.3",
"jakeasmith/http_build_url": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "*"
Expand All @@ -16,4 +17,4 @@
"Imgix\\": "src/"
}
}
}
}
41 changes: 39 additions & 2 deletions composer.lock

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

48 changes: 7 additions & 41 deletions src/Imgix/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,55 +55,21 @@ public function getURL() {

$url_parts = array('scheme' => $this->scheme, 'host' => $this->domain, 'path' => $this->path, 'query' => $query);

return self::join_url($url_parts);
return self::joinURL($url_parts);
}

public static function encodeURIComponent($str) {
$revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
return strtr(rawurlencode($str), $revert);
}

// TODO: This is almost entirely garbage. We should not be manually building URLs
public static function join_url($parts, $encode=true) {
$url = '';
if (!empty($parts['scheme'])) {
$url .= $parts['scheme'] . ':';
}
if (isset($parts['host'])) {
$url .= '//';
if (isset($parts['user'])) {
$url .= $parts['user'];
if (isset($parts['pass']))
$url .= ':' . $parts['pass'];
$url .= '@';
}
if (preg_match('!^[\da-f]*:[\da-f.:]+$!ui', $parts['host'])) {
$url .= '[' . $parts['host'] . ']';
} else {
$url .= $parts['host'];
}
if (isset($parts['port'])) {
$url .= ':' . $parts['port'];
}
if (!empty($parts['path']) && $parts['path'][0] != '/') {
$url .= '/';
}
}
if (!empty($parts['path'])) {
$url .= $parts['path'];
}
if (isset($parts['query']) && strlen($parts['query']) > 0) {
if (substr($parts['query'], 0, 2) === "s=") {
$url .= '?&' . $parts['query']; // imgix idiosyncracy for signing URLs when only the signature exists. Our query string must begin with '?&s='
} else {
$url .= '?' . $parts['query'];
}
}
if (isset($parts['fragment'])) {
$url .= '#' . $parts['fragment'];
public static function joinURL($parts) {

// imgix idiosyncracy for signing URLs when only the signature exists. Our query string must begin with '?&s='
if (substr($parts['query'], 0, 2) === "s=") {
$parts['query'] = "&" . $parts['query'];
}

return $url;
return http_build_url($parts);
}

}

0 comments on commit 44aaa64

Please sign in to comment.