Elgg  Version 1.9
ElggRewriteTester.php
Go to the documentation of this file.
1 <?php
2 
11  protected $webserver;
13  protected $rewriteTestPassed;
14  protected $htaccessIssue;
15 
19  public function __construct() {
20  $this->webserver = 'unknown';
21  }
22 
31  public function run($url, $path) {
32 
33  $this->webserver = ElggRewriteTester::guessWebServer();
34 
35  $this->rewriteTestPassed = $this->runRewriteTest($url);
36 
37  if ($this->rewriteTestPassed == FALSE) {
38  if ($this->webserver == 'apache' || $this->webserver == 'unknown') {
39  if ($this->createHtaccess($url, $path)) {
40  $this->rewriteTestPassed = $this->runRewriteTest($url);
41  }
42  }
43  }
44 
45  return $this->returnStatus($url);
46  }
47 
53  public static function guessWebServer() {
54  $serverString = strtolower($_SERVER['SERVER_SOFTWARE']);
55  $possibleServers = array('apache', 'nginx', 'lighttpd', 'iis');
56  foreach ($possibleServers as $server) {
57  if (strpos($serverString, $server) !== FALSE) {
58  return $server;
59  }
60  }
61  return 'unknown';
62  }
63 
72  public function guessSubdirectory($url) {
73  $elements = parse_url($url);
74  if (!$elements || !isset($elements['path'])) {
75  return false;
76  }
77  $subdir = trim(dirname($elements['path']), '/');
78  if (!$subdir) {
79  return false;
80  } else {
81  return "/$subdir/";
82  }
83  }
84 
92  public function runRewriteTest($url) {
93 
94  $this->serverSupportsRemoteRead = true;
95 
96  if (ini_get('allow_url_fopen')) {
97  $ctx = stream_context_create(array(
98  'http' => array(
99  'follow_location' => 0,
100  'timeout' => 5,
101  ),
102  ));
103  $response = file_get_contents($url, null, $ctx);
104  } elseif (function_exists('curl_init')) {
105  // try curl if installed
106  $ch = curl_init();
107  curl_setopt($ch, CURLOPT_URL, $url);
108  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
109  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
110  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
111  $response = curl_exec($ch);
112  curl_close($ch);
113  } else {
114  $response = '';
115  }
116 
117  if ($response !== 'success') {
118  $this->serverSupportsRemoteRead = false;
119  return false;
120  }
121 
122  return true;
123  }
124 
125  public function runLocalhostAccessTest() {
127  if (ini_get('allow_url_fopen')) {
128  $ctx = stream_context_create(array(
129  'http' => array(
130  'follow_location' => 0,
131  'timeout' => 5,
132  ),
133  ));
134  $response = file_get_contents($url, null, $ctx);
135  } elseif (function_exists('curl_init')) {
136  // try curl if installed
137  $ch = curl_init();
138  curl_setopt($ch, CURLOPT_URL, $url);
139  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
140  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
141  $response = curl_exec($ch);
142  curl_close($ch);
143  }
144 
145  return $response !== false;
146  }
147 
156  public function createHtaccess($url, $path) {
157  $filename = "{$path}.htaccess";
158  if (file_exists($filename)) {
159  // check that this is the Elgg .htaccess
160  $data = file_get_contents($filename);
161  if ($data === FALSE) {
162  // don't have permission to read the file
163  $this->htaccessIssue = 'read_permission';
164  return FALSE;
165  }
166  if (strpos($data, 'Elgg') === FALSE) {
167  $this->htaccessIssue = 'non_elgg_htaccess';
168  return FALSE;
169  } else {
170  // check if this is an old Elgg htaccess
171  if (strpos($data, 'RewriteRule ^rewrite.php$ install.php') == FALSE) {
172  $this->htaccessIssue = 'old_elgg_htaccess';
173  return FALSE;
174  }
175  return TRUE;
176  }
177  }
178 
179  if (!is_writable($path)) {
180  $this->htaccessIssue = 'write_permission';
181  return FALSE;
182  }
183 
184  // create the .htaccess file
185  $result = copy("{$path}htaccess_dist", $filename);
186  if (!$result) {
187  $this->htaccessIssue = 'cannot_copy';
188  return FALSE;
189  }
190 
191  // does default RewriteBase work already?
192  if (!$this->runRewriteTest($url)) {
193  //try to rewrite to guessed subdirectory
194  if ($subdir = $this->guessSubdirectory($url)) {
195  $contents = file_get_contents($filename);
196  $contents = preg_replace("/#RewriteBase \/(\r?\n)/", "RewriteBase $subdir\$1", $contents);
197  if ($contents) {
198  file_put_contents($filename, $contents);
199  }
200  }
201  }
202 
203  return TRUE;
204  }
205 
213  protected function returnStatus($url) {
214  if ($this->rewriteTestPassed) {
215  return array(
216  'severity' => 'pass',
217  'message' => elgg_echo('install:check:rewrite:success'),
218  );
219  }
220 
221  if ($this->serverSupportsRemoteRead == FALSE) {
222  $msg = elgg_echo('install:warning:rewrite:unknown', array($url));
223  $msg .= elgg_view('install/js_rewrite_check', array('url' => $url));
224 
225  return array(
226  'severity' => 'warning',
227  'message' => $msg,
228  );
229  }
230 
231  if ($this->webserver == 'apache') {
232  $serverString = elgg_echo('install:error:rewrite:apache');
233  $msg = "$serverString\n\n";
234  if (!isset($this->htaccessIssue)) {
235  $msg .= elgg_echo('install:error:rewrite:allowoverride');
236  $msg .= elgg_view('install/js_rewrite_check', array('url' => $url));
237 
238  return array(
239  'severity' => 'failure',
240  'message' => $msg,
241  );
242  }
243  $msg .= elgg_echo("install:error:rewrite:htaccess:{$this->htaccessIssue}");
244  return array(
245  'severity' => 'failure',
246  'message' => $msg,
247  );
248  }
249 
250  if ($this->webserver != 'unknown') {
251  $serverString = elgg_echo("install:error:rewrite:{$this->webserver}");
252  $msg = "$serverString\n\n";
253  $msg .= elgg_echo("install:error:rewrite:altserver");
254  return array(
255  'severity' => 'failure',
256  'message' => $msg,
257  );
258  }
259 
260  return array(
261  'severity' => 'failure',
262  'message' => elgg_echo('install:error:rewrite:unknown'),
263  );
264  }
265 }
if($footer) $contents
Definition: module.php:48
$data
Definition: opendd.php:13
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:432
$url
Definition: exceptions.php:24
run($url, $path)
Run the rewrite test and return a status array.
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
returnStatus($url)
Create the status array required by the ElggInstaller.
guessSubdirectory($url)
Guess if url contains subdirectory or not.
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
elgg_view($view, $vars=array(), $bypass=false, $ignored=false, $viewtype= '')
Return a parsed view.
Definition: views.php:354
createHtaccess($url, $path)
Create Elgg&#39;s .htaccess file or confirm that it exists.
and give any other recipients of the Program a copy of this License along with the Program You may charge a fee for the physical act of transferring a copy
Definition: LICENSE.txt:87
__construct()
Set the webserver as unknown.
$filename
Definition: crop.php:23
static guessWebServer()
Guess the web server from $_SERVER[&#39;SERVER_SOFTWARE&#39;].
runRewriteTest($url)
Hit the rewrite test URL to determine if the rewrite rules are working.
$path
Definition: invalid.php:17