File indexing completed on 2024-12-22 05:33:23
0001 <?php 0002 0003 /* 0004 * GFX 4 0005 * 0006 * support: happy.snizzo@gmail.com 0007 * website: http://trt-gfx.googlecode.com 0008 * credits: Claudio Desideri 0009 * 0010 * This software is released under the MIT License. 0011 * http://opensource.org/licenses/mit-license.php 0012 */ 0013 0014 /* 0015 * This class aims to do some quite simple URL Rewriting. 0016 */ 0017 0018 class ERewriter{ 0019 0020 private static $rewritable = false; 0021 private static $oldurl = false; 0022 0023 public static function oldurl() 0024 { 0025 return ERewriter::$oldurl; 0026 } 0027 0028 /* 0029 * some setters, mainly used during loading 0030 */ 0031 public static function enable() 0032 { 0033 ERewriter::$rewritable = true; 0034 } 0035 0036 public static function disable() 0037 { 0038 ERewriter::$rewritable = false; 0039 } 0040 0041 0042 /* 0043 * Rewrite url if needed. 0044 */ 0045 public static function load() 0046 { 0047 //treat url erasing extra parts 0048 $current_uri = $_SERVER['REQUEST_URI']; 0049 //keeping a local copy 0050 ERewriter::$oldurl = $current_uri; 0051 0052 $matches = array(); 0053 0054 foreach(EConfig::$data['rewrite'] as $key => $value){ 0055 if(!is_array($value)){ 0056 ELog::warning("You need to specify a second value as a rewrite rule in rewrite.conf.php"); 0057 return; 0058 } 0059 0060 //handle with normal rewrite that 0061 //permits to have parameters 0062 if($value[1]=="normal"){ 0063 $pos = strpos($current_uri,$key); 0064 0065 //if we found a rewrite rule that can be applied 0066 if ($pos !== false) { 0067 //rewrite only at the very start of the string 0068 if($pos==0){ 0069 //record the allowed key 0070 $matches[strlen($key)] = array($key, $value[0]); 0071 } 0072 } 0073 } 0074 //handle with exact match, that works only if the url is exactly that one 0075 else if($value[1]=="exact"){ 0076 $current_uri_t = explode("?", $current_uri)[0]; 0077 if($current_uri_t==$key){ 0078 if(ERewriter::$rewritable){ 0079 $rewritten = str_replace($key, $value[0], $current_uri); //TODO FIX HERE 0080 //$rewritten = preg_replace("$key", $value, $current_uri, 1); 0081 $_SERVER['REQUEST_URI'] = $rewritten; 0082 //setting rewritable to false 0083 ERewriter::$rewritable = false; 0084 break; 0085 } 0086 } 0087 } 0088 } 0089 0090 if(ERewriter::$rewritable){ 0091 ksort($matches); 0092 $matches = array_reverse(array_values($matches)); 0093 0094 if(isset($matches[0])){ 0095 $rule = $matches[0]; 0096 0097 $rewritten = str_replace($rule[0], $rule[1], $current_uri); //TODO FIX HERE FIRST OCCURRENCE 0098 //$rewritten = preg_replace("$key", $value, $current_uri, 1); 0099 $_SERVER['REQUEST_URI'] = $rewritten; 0100 } 0101 } 0102 } 0103 0104 /** 0105 * Used to make url pretty in order to SEO 0106 */ 0107 public static function prettify($string){ 0108 $string = preg_replace("/[^a-z_\-0-9]/i", "-", $string); 0109 $string = preg_replace("/[-]+/", "-", $string); 0110 $string = trim($string, "-"); 0111 return $string; 0112 } 0113 } 0114 0115 ?>