Elgg  Version 1.10
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 
130  public function runLocalhostAccessTest() {
131  $url = _elgg_services()->config->getSiteUrl();
132  if (ini_get('allow_url_fopen')) {
133  $ctx = stream_context_create(array(
134  'http' => array(
135  'follow_location' => 0,
136  'timeout' => 5,
137  ),
138  ));
139  $response = file_get_contents($url, null, $ctx);
140  } elseif (function_exists('curl_init')) {
141  // try curl if installed
142  $ch = curl_init();
143  curl_setopt($ch, CURLOPT_URL, $url);
144  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
145  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
146  $response = curl_exec($ch);
147  curl_close($ch);
148  }
149 
150  return $response !== false;
151  }
152 
161  public function createHtaccess($url, $path) {
162  $filename = "{$path}.htaccess";
163  if (file_exists($filename)) {
164  // check that this is the Elgg .htaccess
165  $data = file_get_contents($filename);
166  if ($data === FALSE) {
167  // don't have permission to read the file
168  $this->htaccessIssue = 'read_permission';
169  return FALSE;
170  }
171  if (strpos($data, 'Elgg') === FALSE) {
172  $this->htaccessIssue = 'non_elgg_htaccess';
173  return FALSE;
174  } else {
175  // check if this is an old Elgg htaccess
176  if (strpos($data, 'RewriteRule ^rewrite.php$ install.php') == FALSE) {
177  $this->htaccessIssue = 'old_elgg_htaccess';
178  return FALSE;
179  }
180  return TRUE;
181  }
182  }
183 
184  if (!is_writable($path)) {
185  $this->htaccessIssue = 'write_permission';
186  return FALSE;
187  }
188 
189  // create the .htaccess file
190  $result = copy("{$path}install/config/htaccess.dist", $filename);
191  if (!$result) {
192  $this->htaccessIssue = 'cannot_copy';
193  return FALSE;
194  }
195 
196  // does default RewriteBase work already?
197  if (!$this->runRewriteTest($url)) {
198  //try to rewrite to guessed subdirectory
199  if ($subdir = $this->guessSubdirectory($url)) {
200  $contents = file_get_contents($filename);
201  $contents = preg_replace("/#RewriteBase \/(\r?\n)/", "RewriteBase $subdir\$1", $contents);
202  if ($contents) {
203  file_put_contents($filename, $contents);
204  }
205  }
206  }
207 
208  return TRUE;
209  }
210 
218  protected function returnStatus($url) {
219  if ($this->rewriteTestPassed) {
220  return array(
221  'severity' => 'pass',
222  'message' => _elgg_services()->translator->translate('install:check:rewrite:success'),
223  );
224  }
225 
226  if ($this->serverSupportsRemoteRead == FALSE) {
227  $msg = _elgg_services()->translator->translate('install:warning:rewrite:unknown', array($url));
228  $msg .= elgg_view('install/js_rewrite_check', array('url' => $url));
229 
230  return array(
231  'severity' => 'warning',
232  'message' => $msg,
233  );
234  }
235 
236  if ($this->webserver == 'apache') {
237  $serverString = _elgg_services()->translator->translate('install:error:rewrite:apache');
238  $msg = "$serverString\n\n";
239  if (!isset($this->htaccessIssue)) {
240  $msg .= _elgg_services()->translator->translate('install:error:rewrite:allowoverride');
241  $msg .= elgg_view('install/js_rewrite_check', array('url' => $url));
242 
243  return array(
244  'severity' => 'failure',
245  'message' => $msg,
246  );
247  }
248  $msg .= _elgg_services()->translator->translate("install:error:rewrite:htaccess:{$this->htaccessIssue}");
249  return array(
250  'severity' => 'failure',
251  'message' => $msg,
252  );
253  }
254 
255  if ($this->webserver != 'unknown') {
256  $serverString = _elgg_services()->translator->translate("install:error:rewrite:{$this->webserver}");
257  $msg = "$serverString\n\n";
258  $msg .= _elgg_services()->translator->translate("install:error:rewrite:altserver");
259  return array(
260  'severity' => 'failure',
261  'message' => $msg,
262  );
263  }
264 
265  return array(
266  'severity' => 'failure',
267  'message' => _elgg_services()->translator->translate('install:error:rewrite:unknown'),
268  );
269  }
270 }
if($footer) $contents
Definition: module.php:48
runLocalhostAccessTest()
Check whether the site homepage can be fetched via curl.
$data
Definition: opendd.php:13
if(!$autoload_available) _elgg_services()
Definition: autoloader.php:20
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.
returnStatus($url)
Create the status array required by the ElggInstaller.
guessSubdirectory($url)
Guess if url contains subdirectory or not.
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