diff --git a/mashapebase/http/urlUtils.php b/mashapebase/http/urlUtils.php new file mode 100644 index 0000000..d16d559 --- /dev/null +++ b/mashapebase/http/urlUtils.php @@ -0,0 +1,134 @@ +. + * + * + * The author of this software is Mashape, Inc. + * For any question or feedback please contact us at: support@mashape.com + * + */ + +require_once(dirname(__FILE__) . "/../init/init.php"); + +class UrlUtils { + + private static function addRouteParameter($url, $parameterName) { + $result = $url; + $pos = strpos($url, "?"); + if ($pos === false) { + $result .= "?"; + } + if (substr($result, strlen($result) - 1, 1) != "?") { + $result .= "&"; + } + $result .= $parameterName . "={" . $parameterName . "}"; + return $result; + } + + public static function addClientParameters($url) { + $result = self::addRouteParameter($url, TOKEN); + $result = self::addRouteParameter($result, LANGUAGE); + $result = self::addRouteParameter($result, VERSION); + return $result; + } + + public static function getCleanUrl($url, $parameters) { + if ($parameters == null) { + $parameters = array(); + } + + $finalUrl = ""; + + for($i=0;$i < strlen($url);$i++) { + $curchar = substr($url, $i, 1); + + if ($curchar == "{") { + // It may be a placeholder + + $pos = strpos($url, "}", $i); + if ($pos !== false) { + // It's a placeholder + + $placeHolder = substr($url, $i + 1, $pos - 1 - $i); // Get the placeholder name without {..} + if (array_key_exists($placeHolder, $parameters) === false) { + // If it doesn't exist in the array, remove it + + if (substr($url, $i - 1, 1) == "=") { + // It's a query string placeholder, remove also its name + + for ($t = strlen($finalUrl) - 1;$t>=0;$t--) { + $backChar = substr($finalUrl, $t, 1); + if ($backChar == "?" || $backChar == "&") { + $finalUrl = substr($finalUrl, 0, ($backChar == "?") ? $t + 1 : $t); + break; + } + } + + } + + $i = $pos; + continue; + + } + } + } + $finalUrl .= $curchar; + + } + + return str_replace("?&", "?", $finalUrl); + + } + + public static function removeQueryString($url) { + $urlParts = explode("?", $url); + return $urlParts[0]; + } + + public static function getQueryStringParameters($url) { + $result = array(); + $urlParts = explode("?", $url); + if (count($urlParts) > 1) { + $queryString = $urlParts[1]; + $parameters = explode("&", $queryString); + foreach($parameters as $parameter) { + $p = explode("=", $parameter); + if (count($p) > 1) { + if (self::isPlaceHolder($p[1]) == false) { + $result[$p[0]] = $p[1]; + } + } + } + } + return $result; + } + + private static function isPlaceholder($val) { + if (!empty($val)) { + if (strlen($val) >= 2) { + if (substr($val, 0, 1) == "{" && substr($val, strlen($val) - 1, 1) == "}") { + return true; + } + } + } + return false; + } + +} +?> \ No newline at end of file