XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
htaccess.class.php
Go to the documentation of this file.
1 <?php
2 // $Id$
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
15 {
16  private $rules = '';
17  private $module = '';
18  private $file = '';
19  private $content = '';
20  private $bCode = '';
21  private $base = '/';
22  private $apache = true;
23  private $rewrite = true;
24 
25  function __construct($module){
26 
27  if(trim($module)=='')
28  return false;
29 
30  $this->module = $module;
31  $this->file = XOOPS_ROOT_PATH.'/.htaccess';
32 
33  if(strpos($_SERVER['SERVER_SOFTWARE'], 'Apache')!==FALSE){
34 
35  if(!file_exists($this->file))
36  file_put_contents($this->file, '');
37 
38  }
39 
40  $this->content = file_get_contents($this->file);
41 
42  $this->checkHealth();
43 
44  }
45 
49  private function checkHealth(){
50 
51  if(strpos($_SERVER['SERVER_SOFTWARE'], 'Apache')===FALSE)
52  $this->apache = false;
53 
54  if(function_exists('apache_get_modules')){
55  $mods = apache_get_modules();
56  if(!in_array('mod_rewrite', $mods))
57  $this->rewrite = false;
58  }
59 
60  if(!$this->apache || !$this->rewrite){
61  showMessage(__('URL rewriting requires an Apache Server and mod_rewrite enabled!','dtransport'), RMMSG_WARN);
62  return false;
63  }
64 
65  if(!preg_match("/RewriteEngine\s{1,}On/", $this->content))
66  $this->content .= "\nRewriteEngine On\n";
67 
68  $base = parse_url(XOOPS_URL.'/');
69  $this->base = isset($base['path']) ? rtrim($base['path'], '/').'/' : '/';
70  $rb = "RewriteBase ".$this->base."\n";
71 
72  if(strpos($this->content, $rb)===false){
73  if(preg_match("/RewriteBase/", $this->content))
74  preg_replace("/RewriteBase\s{1,}(.*)\n",$rb, $this->content);
75  else
76  $this->content .= $rb."\n";
77  }
78 
79  if(!preg_match("/RewriteCond\s{1,}\%\{REQUEST_URI\}\s{1,}\!\/\[A\-Z\]\+\-/", $this->content))
80  $this->content .= "RewriteCond %{REQUEST_URI} !/[A-Z]+-\n";
81 
82  if(!preg_match("/RewriteCond\s{1,}\%\{REQUEST_FILENAME\}\s{1,}\!\-f/", $this->content))
83  $this->content .= "RewriteCond %{REQUEST_FILENAME} !-f\n";
84 
85  if(!preg_match("/RewriteCond\s{1,}\%\{REQUEST_FILENAME\}\s{1,}\!\-d/", $this->content))
86  $this->content .= "RewriteCond %{REQUEST_FILENAME} !-d\n";
87 
88 
89 
90  }
91 
92  public function verifyCode($code = ''){
93 
94  if($code!=''){
95 
96  if(strpos($this->content, $code)!==FALSE)
97  return true;
98  else
99  return false;
100 
101  }
102 
103  if(!preg_match("/\# begin $this->module\n(.*)\n\# end $this->module/i", $this->content, $match))
104  return false;
105  else
106  return $match[0];
107 
108  }
109 
110  private function makeCode(){
111 
112  $code = "# begin $this->module\n$this->rules\n# end $this->module\n";
113  return $code;
114 
115  }
116 
117  public function write($rules){
118 
119  if($rules=='') return false;
120 
121  if(!$this->apache || !$this->rewrite)
122  return;
123 
124  $this->rules = $rules;
125  $code = $this->makeCode($rules);
126 
127  // Verificamos si existe el código generado
128  $exists = $this->verifyCode($code);
129  $oldExists = false;
130 
131  if(!$exists){
132  // Si no existe verificamos la existencia de código antiguo
133  // para el módulo actual
134  $oldExists = $this->verifyCode();
135  } else {
136 
137  return true;
138 
139  }
140 
141  if(!is_writable($this->file))
142  return $this->bCode.$code;
143 
144  if($oldExists!==false)
145  $this->content = str_replace($oldExists, $code, $this->content);
146  else
147  $this->content .= "\n$code";
148 
149  if(file_put_contents($this->file, $this->content))
150  return true;
151  else
152  return $code;
153 
154  }
155 
156 }