00001 <?php
00012 class Url
00013 {
00023 public function absUrl($base_, $url_)
00024 {
00025 static $urlCache = array();
00026
00027 $urlKey = md5($base_. '___' . $url_);
00028 if (!array_key_exists($urlKey, $urlCache))
00029 {
00030 preg_match("/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/",$url_,$urlComp);
00031 list(,,$Rscheme,,$Rauthority,$Rpath,,$Rquery,,$Rfragment) = $urlComp;
00032
00033 if ( isset($Rscheme) && !empty($Rscheme) )
00034 {
00035 $Tscheme = $Rscheme;
00036 $Tauthority = $Rauthority;
00037 $Tpath = url::_removeDotSegments($Rpath);
00038 $Tquery = $Rquery;
00039
00040 }
00041 else
00042 {
00043 if ( isset($Rauthority) && !empty($Rauthority) )
00044 {
00045 $Tauthority = $Rauthority;
00046 $Tpath = url::_removeDotSegments($Rpath);
00047 $Tquery = $Rquery;
00048 }
00049 else
00050 {
00051 preg_match("/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/",$base_,$baseComp);
00052 list(,,$Bscheme,,$Bauthority,$Bpath,,$Bquery,,$Bfragment) = $baseComp;
00053
00054 if ( $Rpath == "")
00055 {
00056 $Tpath = $Bpath;
00057 if ( isset($Rquery) && !empty($Rquery) )
00058 {
00059 $Tquery = $Rquery;
00060 }
00061 else
00062 {
00063 $Tquery = $Bquery;
00064 }
00065 }
00066 else
00067 {
00068 if ( preg_match("/^\//", $Rpath) )
00069 {
00070 $Tpath = url::_removeDotSegments($Rpath);
00071 }
00072 else
00073 {
00074 $Tpath = url::_merge($Bpath,$Bauthority,$Rpath);
00075 $Tpath = url::_removeDotSegments($Tpath);
00076 }
00077
00078 $Tquery = $Rquery;
00079 }
00080
00081 $Tauthority = $Bauthority;
00082 }
00083
00084 $Tscheme = $Bscheme;
00085
00086 }
00087
00088 $Tfragment = $Rfragment;
00089
00090 $result = "";
00091
00092 if ( isset($Tscheme) && !empty($Tscheme) )
00093 {
00094 $result .= $Tscheme . ":";
00095 }
00096
00097 if ( isset($Tauthority) && !empty($Tauthority) )
00098 {
00099 $result .= "//" . $Tauthority;
00100 }
00101
00102 $result .= $Tpath;
00103
00104 if ( isset($Tquery) && !empty($Tquery) )
00105 {
00106 $result .= "?" . $Tquery;
00107 }
00108
00109 if ( isset($Tfragment) && !empty($Tfragment) )
00110 {
00111 $result .= "#" . $Tfragment;
00112 }
00113
00114 $urlCache[$urlKey] = $result;
00115 }
00116
00117 return $urlCache[$urlKey];
00118 }
00119
00120
00121 private function _removeDotSegments($path)
00122 {
00123
00124
00125
00126
00127
00128 $bInput = $path;
00129 $bOutput = "";
00130
00131
00132
00133 while( !empty($bInput) )
00134 {
00135
00136
00137
00138
00139 if ( preg_match("/^(\.\.\/|\.\/)/", $bInput, $match) )
00140 {
00141 $bInput = preg_replace("/^" . preg_quote($match[1], "/") . "/","",$bInput);
00142 }
00143
00144
00145
00146
00147
00148 else if ( preg_match("/^(\/\.\/|\/\.$)/",$bInput, $match) )
00149 {
00150 $bInput = preg_replace("/^" . preg_quote($match[1], "/") . "/","/",$bInput);
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160 else if ( preg_match("/^(\/\.\.\/|\/\.\.$)/",$bInput, $match) )
00161 {
00162 $bInput = preg_replace ( "/^" . preg_quote($match[0], "/") . "/","/",$bInput);
00163 $bOutput = preg_replace ( "/\/?[^\/]+$/", "", $bOutput);
00164 }
00165
00166
00167
00168
00169 else if ( preg_match("/^(\.\.|\.)$/",$bInput, $match) )
00170 {
00171 $bInput = preg_replace("/^" . preg_quote($match[1], "/") . "/","",$bInput);
00172 }
00173
00174
00175
00176
00177
00178
00179 else
00180 {
00181 preg_match("/^\/?[^\/]*/",$bInput,$match);
00182 $bInput = preg_replace("/^" . preg_quote($match[0], "/") . "/","",$bInput);
00183 $bOutput .= $match[0];
00184 }
00185 }
00186
00187
00188
00189
00190 return $bOutput;
00191 }
00192
00193
00194 private function _merge($Bpath,$Bauthority,$Rpath)
00195 {
00196 if ( isset($Bauthority) && !empty($Bauthority) && empty($Bpath) )
00197 {
00198 return "/" . $Rpath;
00199 }
00200 else
00201 {
00202 return preg_replace("/[^\/]*$/","",$Bpath) . $Rpath;
00203 }
00204 }
00205 }
00206 ?>