/oc-includes/osclass/classes/Rewrite.php

https://github.com/temperatio/OSClass · PHP · 175 lines · 131 code · 21 blank · 23 comment · 18 complexity · 26cd466bccbcf0e1700b6f1781d04c89 MD5 · raw file

  1. <?php if ( ! defined('ABS_PATH')) exit('ABS_PATH is not loaded. Direct access is not allowed.');
  2. /*
  3. * OSCLass – software for creating and publishing online classified
  4. * advertising platforms
  5. *
  6. * Copyright (C) 2010 OSCLASS
  7. *
  8. * This program is free software: you can redistribute it and/or
  9. * modify it under the terms of the GNU Affero General Public License
  10. * as published by the Free Software Foundation, either version 3 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. class Rewrite
  22. {
  23. private static $instance ;
  24. private $rules;
  25. private $request_uri;
  26. private $uri;
  27. private $location;
  28. private $section;
  29. public function __construct()
  30. {
  31. $this->rules = $this->getRules();
  32. $this->request_uri = '';
  33. $this->uri = '';
  34. $this->location = '';
  35. $this->section = '';
  36. //parent::__construct() ;
  37. }
  38. public static function newInstance()
  39. {
  40. if(!self::$instance instanceof self) {
  41. self::$instance = new self ;
  42. }
  43. return self::$instance ;
  44. }
  45. public function getTableName() {}
  46. public function getRules()
  47. {
  48. return unserialize(osc_rewrite_rules()) ;
  49. }
  50. public function setRules()
  51. {
  52. Preference::newInstance()->update(
  53. array('s_value' => serialize($this->rules))
  54. ,array('s_name' => 'rewrite_rules')
  55. );
  56. }
  57. public function listRules()
  58. {
  59. return $this->rules;
  60. }
  61. public function addRules($rules)
  62. {
  63. if(is_array($rules)) {
  64. foreach($rules as $rule) {
  65. if(is_array($rule) && count($rule)>1) {
  66. $this->addRule($rule[0], $rule[1]);
  67. }
  68. }
  69. }
  70. }
  71. public function addRule($regexp, $uri)
  72. {
  73. $regexp = trim($regexp);
  74. $uri = trim($uri);
  75. if($regexp!='' && $uri!='') {
  76. if(!in_array($regexp, $this->rules)) {
  77. $this->rules[$regexp] = $uri;
  78. }
  79. }
  80. }
  81. public function init()
  82. {
  83. // $_SERVER is not supported by Params Class... we should fix that
  84. if(isset($_SERVER['REQUEST_URI'])) {
  85. $request_uri = urldecode(preg_replace('@^' . REL_WEB_URL . '@', "", $_SERVER['REQUEST_URI']));
  86. if(osc_rewrite_enabled()) {
  87. $this->extractParams($request_uri);
  88. $tmp_ar = explode("?", $request_uri);
  89. $request_uri = $tmp_ar[0];
  90. foreach($this->rules as $match => $uri) {
  91. // UNCOMMENT TO DEBUG
  92. //echo 'Request URI: '.$request_uri." # Match : ".$match." # URI to go : ".$uri." <br />";
  93. if(preg_match('#'.$match.'#', $request_uri, $m)) {
  94. $request_uri = preg_replace('#'.$match.'#', $uri, $request_uri);
  95. break;
  96. }
  97. }
  98. }
  99. $this->extractParams($request_uri);
  100. $this->request_uri = $request_uri;
  101. if(Params::getParam('page')!='') { $this->location = Params::getParam('page'); };
  102. if(Params::getParam('action')!='') { $this->section = Params::getParam('action'); };
  103. }
  104. }
  105. public function extractURL($uri = '')
  106. {
  107. $uri_array = explode('?', str_replace('index.php', '', $uri));
  108. if(substr($uri_array[0], 0, 1)=="/") {
  109. return substr($uri_array[0], 1);
  110. } else {
  111. return $uri_array[0];
  112. }
  113. }
  114. public function extractParams($uri = '')
  115. {
  116. $uri_array = explode('?', $uri);
  117. $url = substr($uri_array[0], 1);
  118. $length_i = count($uri_array);
  119. for($var_i = 1;$var_i<$length_i;$var_i++) {
  120. if(preg_match_all('|&([^=]+)=([^&]*)|', '&'.$uri_array[$var_i].'&', $matches)) {
  121. $length = count($matches[1]);
  122. for($var_k = 0;$var_k<$length;$var_k++) {
  123. Params::setParam($matches[1][$var_k], $matches[2][$var_k]);
  124. }
  125. }
  126. }
  127. }
  128. public function removeRule($regexp)
  129. {
  130. unset($this->rules[$regexp]);
  131. }
  132. public function clearRules()
  133. {
  134. unset($this->rules);
  135. $this->rules = array();
  136. }
  137. public function get_request_uri()
  138. {
  139. return $this->request_uri;
  140. }
  141. public function set_location($location)
  142. {
  143. $this->location = $location;
  144. }
  145. public function get_location()
  146. {
  147. return $this->location;
  148. }
  149. public function get_section()
  150. {
  151. return $this->section;
  152. }
  153. }
  154. ?>