Elgg  Version 4.3
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 (elgg_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  return (bool) $this->fetchUrl(_elgg_services()->config->wwwroot);
110  }
111 
119  private function fetchUrl($url) {
120  $response = '';
121 
122  if (ini_get('allow_url_fopen')) {
123  $ctx = stream_context_create([
124  'http' => [
125  'follow_location' => 0,
126  'timeout' => 5,
127  ],
128  ]);
129  $response = @file_get_contents($url, false, $ctx);
130  }
131 
132  if (!$response && function_exists('curl_init')) {
133  $ch = curl_init();
134  curl_setopt($ch, CURLOPT_URL, $url);
135  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
136  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
137  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
138  $response = curl_exec($ch);
139  curl_close($ch);
140  }
141 
142  return (string) $response;
143  }
144 
152  public function createHtaccess($url) {
153  $root = ElggDirectory\Local::projectRoot();
154  $file = $root->getFile(".htaccess");
155 
156  if ($file->exists()) {
157  // check that this is the Elgg .htaccess
158  $data = $file->getContents();
159  if (empty($data)) {
160  // don't have permission to read the file
161  $this->htaccessIssue = 'read_permission';
162  return false;
163  }
164 
165  if (elgg_strpos($data, 'Elgg') === false) {
166  $this->htaccessIssue = 'non_elgg_htaccess';
167  return false;
168  }
169 
170  // check if this is an old Elgg htaccess
171  if (elgg_strpos($data, 'RewriteRule ^rewrite.php$ install.php') === false) {
172  $this->htaccessIssue = 'old_elgg_htaccess';
173  return false;
174  }
175  return true;
176  }
177 
178  if (!is_writable($root->getPath())) {
179  $this->htaccessIssue = 'write_permission';
180  return false;
181  }
182 
183  // create the .htaccess file
184  $result = copy(Paths::elgg() . "install/config/htaccess.dist", $file->getPath());
185  if (!$result) {
186  $this->htaccessIssue = 'cannot_copy';
187  return false;
188  }
189 
190  // does default RewriteBase work already?
191  if (!$this->runRewriteTest($url)) {
192  //try to rewrite to guessed subdirectory
193  if ($subdir = $this->guessSubdirectory($url)) {
194  $contents = $file->getContents();
195  $contents = preg_replace("/#RewriteBase \/(\r?\n)/", "RewriteBase $subdir\$1", $contents);
196  if ($contents) {
197  $file->putContents($contents);
198  }
199  }
200  }
201 
202  return true;
203  }
204 
212  protected function returnStatus($url) {
213  if ($this->rewriteTestPassed) {
214  return [
215  'severity' => 'success',
216  'message' => _elgg_services()->translator->translate('install:check:rewrite:success'),
217  ];
218  }
219 
220  if ($this->serverSupportsRemoteRead == false) {
221  $msg = _elgg_services()->translator->translate('install:warning:rewrite:unknown', [$url]);
222  $msg .= elgg_view('install/js_rewrite_check', ['url' => $url]);
223 
224  return [
225  'severity' => 'warning',
226  'message' => $msg,
227  ];
228  }
229 
230  if ($this->webserver == 'apache') {
231  $serverString = _elgg_services()->translator->translate('install:error:rewrite:apache');
232  $msg = "$serverString\n\n";
233  if (!isset($this->htaccessIssue)) {
234  $msg .= _elgg_services()->translator->translate('install:error:rewrite:allowoverride');
235  $msg .= elgg_view('install/js_rewrite_check', ['url' => $url]);
236 
237  return [
238  'severity' => 'warning',
239  'message' => $msg,
240  ];
241  }
242  $msg .= _elgg_services()->translator->translate("install:error:rewrite:htaccess:{$this->htaccessIssue}");
243  return [
244  'severity' => 'warning',
245  'message' => $msg,
246  ];
247  }
248 
249  if ($this->webserver != 'unknown') {
250  $serverString = _elgg_services()->translator->translate("install:error:rewrite:{$this->webserver}");
251  $msg = "$serverString\n\n";
252  $msg .= _elgg_services()->translator->translate("install:error:rewrite:altserver");
253  return [
254  'severity' => 'warning',
255  'message' => $msg,
256  ];
257  }
258 
259  return [
260  'severity' => 'warning',
261  'message' => _elgg_services()->translator->translate('install:error:rewrite:unknown'),
262  ];
263  }
264 }
$server
Elgg RewriteTester.
runLocalhostAccessTest()
Check whether the site homepage can be fetched via curl.
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:53
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:135
run($url, $path=null)
Run the rewrite test and return a status array.
$path
Definition: details.php:68
createHtaccess($url)
Create Elgg&#39;s .htaccess file or confirm that it exists.
elgg_strpos()
Wrapper function for mb_strpos().
Definition: mb_wrapper.php:69
returnStatus($url)
Create the status array required by the ElggInstaller.
guessSubdirectory($url)
Guess if url contains subdirectory or not.
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
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
__construct()
Set the webserver as unknown.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
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:179
if(!empty($title)&&!empty($icon_name)) if(!empty($title)) if(!empty($menu)) if(!empty($header)) if(!empty($body)) $contents
Definition: message.php:73
var elgg
Definition: elgglib.js:4