Elgg  Version 2.3
ElggRewriteTester.php
Go to the documentation of this file.
1 <?php
2 
4 
13  protected $webserver;
15  protected $rewriteTestPassed;
16  protected $htaccessIssue;
17 
21  public function __construct() {
22  $this->webserver = 'unknown';
23  }
24 
33  public function run($url, $path) {
34 
35  $this->webserver = \ElggRewriteTester::guessWebServer();
36 
37  $this->rewriteTestPassed = $this->runRewriteTest($url);
38 
39  if ($this->rewriteTestPassed == FALSE) {
40  if ($this->webserver == 'apache' || $this->webserver == 'unknown') {
41  if ($this->createHtaccess($url, $path)) {
42  $this->rewriteTestPassed = $this->runRewriteTest($url);
43  }
44  }
45  }
46 
47  return $this->returnStatus($url);
48  }
49 
55  public static function guessWebServer() {
56  $serverString = strtolower($_SERVER['SERVER_SOFTWARE']);
57  $possibleServers = array('apache', 'nginx', 'lighttpd', 'iis');
58  foreach ($possibleServers as $server) {
59  if (strpos($serverString, $server) !== FALSE) {
60  return $server;
61  }
62  }
63  return 'unknown';
64  }
65 
74  public function guessSubdirectory($url) {
75  $elements = parse_url($url);
76  if (!$elements || !isset($elements['path'])) {
77  return false;
78  }
79  $subdir = trim(dirname($elements['path']), '/');
80  if (!$subdir) {
81  return false;
82  } else {
83  return "/$subdir/";
84  }
85  }
86 
94  public function runRewriteTest($url) {
95  $this->serverSupportsRemoteRead = ($this->fetchUrl($url) === \Elgg\Application::REWRITE_TEST_OUTPUT);
97  }
98 
104  public function runLocalhostAccessTest() {
105  $url = _elgg_services()->config->getSiteUrl();
106  return (bool)$this->fetchUrl($url);
107  }
108 
116  private function fetchUrl($url) {
117  $response = '';
118 
119  if (ini_get('allow_url_fopen')) {
120  $ctx = stream_context_create(array(
121  'http' => array(
122  'follow_location' => 0,
123  'timeout' => 5,
124  ),
125  ));
126  $response = @file_get_contents($url, null, $ctx);
127  }
128 
129  if (!$response && function_exists('curl_init')) {
130  $ch = curl_init();
131  curl_setopt($ch, CURLOPT_URL, $url);
132  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
133  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
134  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
135  $response = curl_exec($ch);
136  curl_close($ch);
137  }
138 
139  return (string)$response;
140  }
141 
149  public function createHtaccess($url) {
150  $root = Directory\Local::root();
151  $file = $root->getFile(".htaccess");
152  if ($file->exists()) {
153  // check that this is the Elgg .htaccess
154  $data = $file->getContents();
155  if ($data === FALSE) {
156  // don't have permission to read the file
157  $this->htaccessIssue = 'read_permission';
158  return FALSE;
159  }
160  if (strpos($data, 'Elgg') === FALSE) {
161  $this->htaccessIssue = 'non_elgg_htaccess';
162  return FALSE;
163  } else {
164  // check if this is an old Elgg htaccess
165  if (strpos($data, 'RewriteRule ^rewrite.php$ install.php') == FALSE) {
166  $this->htaccessIssue = 'old_elgg_htaccess';
167  return FALSE;
168  }
169  return TRUE;
170  }
171  }
172 
173  if (!is_writable($root->getPath())) {
174  $this->htaccessIssue = 'write_permission';
175  return FALSE;
176  }
177 
178  // create the .htaccess file
179  $result = copy(\Elgg\Application::elggDir()->getPath("install/config/htaccess.dist"), $file->getPath());
180  if (!$result) {
181  $this->htaccessIssue = 'cannot_copy';
182  return FALSE;
183  }
184 
185  // does default RewriteBase work already?
186  if (!$this->runRewriteTest($url)) {
187  //try to rewrite to guessed subdirectory
188  if ($subdir = $this->guessSubdirectory($url)) {
189  $contents = $file->getContents();
190  $contents = preg_replace("/#RewriteBase \/(\r?\n)/", "RewriteBase $subdir\$1", $contents);
191  if ($contents) {
192  $file->putContents($contents);
193  }
194  }
195  }
196 
197  return TRUE;
198  }
199 
207  protected function returnStatus($url) {
208  if ($this->rewriteTestPassed) {
209  return array(
210  'severity' => 'pass',
211  'message' => _elgg_services()->translator->translate('install:check:rewrite:success'),
212  );
213  }
214 
215  if ($this->serverSupportsRemoteRead == FALSE) {
216  $msg = _elgg_services()->translator->translate('install:warning:rewrite:unknown', array($url));
217  $msg .= elgg_view('install/js_rewrite_check', array('url' => $url));
218 
219  return array(
220  'severity' => 'warning',
221  'message' => $msg,
222  );
223  }
224 
225  if ($this->webserver == 'apache') {
226  $serverString = _elgg_services()->translator->translate('install:error:rewrite:apache');
227  $msg = "$serverString\n\n";
228  if (!isset($this->htaccessIssue)) {
229  $msg .= _elgg_services()->translator->translate('install:error:rewrite:allowoverride');
230  $msg .= elgg_view('install/js_rewrite_check', array('url' => $url));
231 
232  return array(
233  'severity' => 'failure',
234  'message' => $msg,
235  );
236  }
237  $msg .= _elgg_services()->translator->translate("install:error:rewrite:htaccess:{$this->htaccessIssue}");
238  return array(
239  'severity' => 'failure',
240  'message' => $msg,
241  );
242  }
243 
244  if ($this->webserver != 'unknown') {
245  $serverString = _elgg_services()->translator->translate("install:error:rewrite:{$this->webserver}");
246  $msg = "$serverString\n\n";
247  $msg .= _elgg_services()->translator->translate("install:error:rewrite:altserver");
248  return array(
249  'severity' => 'failure',
250  'message' => $msg,
251  );
252  }
253 
254  return array(
255  'severity' => 'failure',
256  'message' => _elgg_services()->translator->translate('install:error:rewrite:unknown'),
257  );
258  }
259 }
if(!array_key_exists($filename, $text_files)) $file
if($footer) $contents
Definition: module.php:43
runLocalhostAccessTest()
Check whether the site homepage can be fetched via curl.
$data
Definition: opendd.php:13
$path
Definition: details.php:88
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:450
$url
Definition: exceptions.php:24
run($url, $path)
Run the rewrite test and return a status array.
createHtaccess($url)
Create Elgg&#39;s .htaccess file or confirm that it exists.
Save menu items.
returnStatus($url)
Create the status array required by the ElggInstaller.
guessSubdirectory($url)
Guess if url contains subdirectory or not.
elgg_view($view, $vars=array(), $ignore1=false, $ignore2=false, $viewtype= '')
Return a parsed view.
Definition: views.php:336
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
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: GPL-LICENSE.txt:87
__construct()
Set the webserver as unknown.
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.
const REWRITE_TEST_OUTPUT
Definition: Application.php:25
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5