Last active
November 10, 2015 22:02
-
-
Save patricksavalle/bab1e8045777670c754c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( !defined( 'BASEPATH' ) ) | |
exit( 'No direct script access allowed' ); | |
/** | |
* Mobbr.com | |
* | |
* Social nanopayment system | |
* | |
* @package Kernel | |
* @author Mobbr Dev Team | |
* @copyright Copyright (c) 2011 Mobbr | |
* @since Version 1.0 | |
* @filesource | |
*/ | |
namespace mobbr\core; | |
class url | |
{ | |
// --------------------------------------------------------------------------- | |
/** | |
* Make URL absolute | |
* | |
* @access public | |
* @param string | |
* @param string | |
* @return string | |
*/ | |
static public function absolutize_url( $rel, $base ) | |
{ | |
// ------------------------------- | |
// return if already absolute URL | |
// ------------------------------- | |
$scheme = parse_url( $rel, PHP_URL_SCHEME ); | |
if ( !empty( $scheme) ) | |
{ | |
return $rel; | |
} | |
// --------------------- | |
// queries and anchors | |
// --------------------- | |
if ( $rel[ 0 ] === '#' || $rel[ 0 ] === '?' ) | |
{ | |
return $base . $rel; | |
} | |
// ------------------------------------------------ | |
// parse base URL and convert to local variables: | |
// $scheme, $host, $path | |
// ------------------------------------------------ | |
$parts = parse_url( $base ); | |
assert( 'empty($parts['port']) /* not yet supported */' ); | |
assert( 'empty($parts['user']) /* not yet supported */' ); | |
assert( 'empty($parts['pass']) /* not yet supported */' ); | |
$scheme = empty( $parts['scheme'] ) ? "" : $parts['scheme']; | |
$host = empty( $parts['host'] ) ? "" : $parts['host']; | |
$path = empty( $parts['path'] ) ? "" : $parts['path']; | |
// ------------------------------------------ | |
// remove non-directory element from path | |
// ------------------------------------------ | |
$path = preg_replace( '#/[^/]*$#', '', $path ); | |
// ------------------------------------------ | |
// destroy path if relative url points to root | |
// ------------------------------------------ | |
if ( $rel[ 0 ] === '/' ) | |
{ | |
$path = ''; | |
} | |
// ------------------------------------------ | |
// dirty absolute URL | |
// ------------------------------------------ | |
$abs = "{$host}{$path}/{$rel}"; | |
// ------------------------------------------------- | |
// replace '//' or '/./' or '/foo/../' with '/' */ | |
// ------------------------------------------------- | |
$re = array( '#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#' ); | |
for ( $n = 1; $n > 0; $abs = preg_replace( $re, '/', $abs, -1, $n ) ); | |
// ------------------------ | |
// absolute URL is ready! | |
// ------------------------ | |
return "{$scheme}://{$abs}"; | |
} | |
} | |
// ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment