Elgg  Version 3.0
ElggRewriteTester.php
Go to the documentation of this file.
1 <?php
2 
6 
12  protected $webserver;
14  protected $rewriteTestPassed;
15  protected $htaccessIssue;
16 
20  public function __construct() {
21  $this->webserver = 'unknown';
22  }
23 
32  public function run($url, $path = null) {
33 
34  $this->webserver = \ElggRewriteTester::guessWebServer();
35 
36  $this->rewriteTestPassed = $this->runRewriteTest($url);
37 
38  if ($this->rewriteTestPassed === false) {
39  if ($this->webserver == 'apache' || $this->webserver == 'unknown') {
40  if ($this->createHtaccess($url)) {
41  $this->rewriteTestPassed = $this->runRewriteTest($url);
42  }
43  }
44  }
45 
46  return $this->returnStatus($url);
47  }
48 
54  public static function guessWebServer() {
55  if (empty($_SERVER['SERVER_SOFTWARE'])) {
56  return 'unknown';
57  }
58 
59  $serverString = strtolower($_SERVER['SERVER_SOFTWARE']);
60  $possibleServers = ['apache', 'nginx', 'lighttpd', 'iis'];
61  foreach ($possibleServers as $server) {
62  if (strpos($serverString, $server) !== false) {
63  return $server;
64  }
65  }
66  return 'unknown';
67  }
68 
77  public function guessSubdirectory($url) {
78  $elements = parse_url($url);
79  if (!is_array($elements) || !isset($elements['path'])) {
80  return false;
81  }
82 
83  $subdir = trim(dirname($elements['path']), '/');
84  if (!$subdir) {
85  return false;
86  }
87 
88  return "/$subdir/";
89  }
90 
98  public function runRewriteTest($url) {
99  $this->serverSupportsRemoteRead = ($this->fetchUrl($url) === Request::REWRITE_TEST_OUTPUT);
101  }
102 
108  public function runLocalhostAccessTest() {
109  $url = _elgg_config()->wwwroot;
110  return (bool) $this->fetchUrl($url);
111  }
112 
120  private function fetchUrl($url) {
121  $response = '';
122 
123  if (ini_get('allow_url_fopen')) {
124  $ctx = stream_context_create([
125  'http' => [
126  'follow_location' => 0,
127  'timeout' => 5,
128  ],
129  ]);
130  $response = @file_get_contents($url, null, $ctx);
131  }
132 
133  if (!$response && function_exists('curl_init')) {
134  $ch = curl_init();
135  curl_setopt($ch, CURLOPT_URL, $url);
136  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
137  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
138  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
139  $response = curl_exec($ch);
140  curl_close($ch);
141  }
142 
143  return (string) $response;
144  }
145 
153  public function createHtaccess($url) {
154  $root = ElggDirectory\Local::projectRoot();
155  $file = $root->getFile(".htaccess");
156 
157  if ($file->exists()) {
158  // check that this is the Elgg .htaccess
159  $data = $file->getContents();
160  if (empty($data)) {
161  // don't have permission to read the file
162  $this->htaccessIssue = 'read_permission';
163  return false;
164  }
165 
166  if (strpos($data, 'Elgg') === false) {
167  $this->htaccessIssue = 'non_elgg_htaccess';
168  return false;
169  }
170 
171  // check if this is an old Elgg htaccess
172  if (strpos($data, 'RewriteRule ^rewrite.php$ install.php') === false) {
173  $this->htaccessIssue = 'old_elgg_htaccess';
174  return false;
175  }
176  return true;
177  }
178 
179  if (!is_writable($root->getPath())) {
180  $this->htaccessIssue = 'write_permission';
181  return false;
182  }
183 
184  // create the .htaccess file
185  $result = copy(Paths::elgg() . "install/config/htaccess.dist", $file->getPath());
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->getContents();
196  $contents = preg_replace("/#RewriteBase \/(\r?\n)/", "RewriteBase $subdir\$1", $contents);
197  if ($contents) {
198  $file->putContents($contents);
199  }
200  }
201  }
202 
203  return true;
204  }
205 
213  protected function returnStatus($url) {
214  if ($this->rewriteTestPassed) {
215  return [
216  'severity' => 'success',
217  'message' => _elgg_services()->translator->translate('install:check:rewrite:success'),
218  ];
219  }
220 
221  if ($this->serverSupportsRemoteRead == false) {
222  $msg = _elgg_services()->translator->translate('install:warning:rewrite:unknown', [$url]);
223  $msg .= elgg_view('install/js_rewrite_check', ['url' => $url]);
224 
225  return [
226  'severity' => 'warning',
227  'message' => $msg,
228  ];
229  }
230 
231  if ($this->webserver == 'apache') {
232  $serverString = _elgg_services()->translator->translate('install:error:rewrite:apache');
233  $msg = "$serverString\n\n";
234  if (!isset($this->htaccessIssue)) {
235  $msg .= _elgg_services()->translator->translate('install:error:rewrite:allowoverride');
236  $msg .= elgg_view('install/js_rewrite_check', ['url' => $url]);
237 
238  return [
239  'severity' => 'warning',
240  'message' => $msg,
241  ];
242  }
243  $msg .= _elgg_services()->translator->translate("install:error:rewrite:htaccess:{$this->htaccessIssue}");
244  return [
245  'severity' => 'warning',
246  'message' => $msg,
247  ];
248  }
249 
250  if ($this->webserver != 'unknown') {
251  $serverString = _elgg_services()->translator->translate("install:error:rewrite:{$this->webserver}");
252  $msg = "$serverString\n\n";
253  $msg .= _elgg_services()->translator->translate("install:error:rewrite:altserver");
254  return [
255  'severity' => 'warning',
256  'message' => $msg,
257  ];
258  }
259 
260  return [
261  'severity' => 'warning',
262  'message' => _elgg_services()->translator->translate('install:error:rewrite:unknown'),
263  ];
264  }
265 }
if(!array_key_exists($filename, $text_files)) $file
Elgg RewriteTester.
runLocalhostAccessTest()
Check whether the site homepage can be fetched via curl.
if(!empty($title)&&!empty($icon_name)) if(!empty($title)) if(!empty($header)) if(!empty($body)) $contents
Definition: message.php:60
$path
Definition: details.php:89
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:57
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:442
run($url, $path=null)
Run the rewrite test and return a status array.
createHtaccess($url)
Create Elgg&#39;s .htaccess file or confirm that it exists.
returnStatus($url)
Create the status array required by the ElggInstaller.
guessSubdirectory($url)
Guess if url contains subdirectory or not.
$url
Definition: default.php:33
_elgg_config()
Get the Elgg config service.
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:140
__construct()
Set the webserver as unknown.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
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.
elgg_view($view, $vars=[], $viewtype= '')
Return a parsed view.
Definition: views.php:246
var elgg
Definition: elgglib.js:4