PageRenderTime 77ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/install/ElggRewriteTester.php

https://github.com/fragilbert/Elgg
PHP | 199 lines | 127 code | 22 blank | 50 comment | 28 complexity | 79363bf69d305551404148b26d10ff4b MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Elgg RewriteTester.
  4. * Test if URL rewriting is working.
  5. *
  6. * @package Elgg.Core
  7. * @subpackage Installer
  8. */
  9. class ElggRewriteTester {
  10. protected $webserver;
  11. protected $serverSupportsRemoteRead;
  12. protected $rewriteTestPassed;
  13. protected $htaccessIssue;
  14. /**
  15. * Set the webserver as unknown.
  16. */
  17. public function __construct() {
  18. $this->webserver = 'unknown';
  19. }
  20. /**
  21. * Run the rewrite test and return a status array
  22. *
  23. * @param string $url URL of rewrite test
  24. * @param string $path Root directory of Elgg with trailing slash
  25. *
  26. * @return array
  27. */
  28. public function run($url, $path) {
  29. $this->webserver = ElggRewriteTester::guessWebServer();
  30. $this->rewriteTestPassed = $this->runRewriteTest($url);
  31. if ($this->rewriteTestPassed == FALSE) {
  32. if ($this->webserver == 'apache' || $this->webserver == 'unknown') {
  33. if ($this->createHtaccess($path)) {
  34. $this->rewriteTestPassed = $this->runRewriteTest($url);
  35. }
  36. }
  37. }
  38. return $this->returnStatus($url);
  39. }
  40. /**
  41. * Guess the web server from $_SERVER['SERVER_SOFTWARE']
  42. *
  43. * @return string
  44. */
  45. public static function guessWebServer() {
  46. $serverString = strtolower($_SERVER['SERVER_SOFTWARE']);
  47. $possibleServers = array('apache', 'nginx', 'lighttpd', 'iis');
  48. foreach ($possibleServers as $server) {
  49. if (strpos($serverString, $server) !== FALSE) {
  50. return $server;
  51. }
  52. }
  53. return 'unknown';
  54. }
  55. /**
  56. * Hit the rewrite test URL to determine if the rewrite rules are working
  57. *
  58. * @param string $url Rewrite test URL
  59. *
  60. * @return bool
  61. */
  62. protected function runRewriteTest($url) {
  63. $this->serverSupportsRemoteRead = TRUE;
  64. if (function_exists('curl_init')) {
  65. // try curl if installed
  66. $ch = curl_init();
  67. curl_setopt($ch, CURLOPT_URL, $url);
  68. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  69. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  70. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  71. $response = curl_exec($ch);
  72. curl_close($ch);
  73. return $response === 'success';
  74. } else if (ini_get('allow_url_fopen')) {
  75. // use file_get_contents as fallback
  76. $response = file_get_contents($url);
  77. return $response === 'success';
  78. } else {
  79. $this->serverSupportsRemoteRead = FALSE;
  80. return FALSE;
  81. }
  82. }
  83. /**
  84. * Create Elgg's .htaccess file or confirm that it exists
  85. *
  86. * @param string $path Elgg's root directory with trailing slash
  87. *
  88. * @return bool
  89. */
  90. public function createHtaccess($path) {
  91. $filename = "{$path}.htaccess";
  92. if (file_exists($filename)) {
  93. // check that this is the Elgg .htaccess
  94. $data = file_get_contents($filename);
  95. if ($data === FALSE) {
  96. // don't have permission to read the file
  97. $this->htaccessIssue = 'read_permission';
  98. return FALSE;
  99. }
  100. if (strpos($data, 'Elgg') === FALSE) {
  101. $this->htaccessIssue = 'non_elgg_htaccess';
  102. return FALSE;
  103. } else {
  104. // check if this is an old Elgg htaccess
  105. if (strpos($data, 'RewriteRule ^rewrite.php$ install.php') == FALSE) {
  106. $this->htaccessIssue = 'old_elgg_htaccess';
  107. return FALSE;
  108. }
  109. return TRUE;
  110. }
  111. }
  112. if (!is_writable($path)) {
  113. $this->htaccessIssue = 'write_permission';
  114. return FALSE;
  115. }
  116. // create the .htaccess file
  117. $result = copy("{$path}htaccess_dist", $filename);
  118. if (!$result) {
  119. $this->htaccessIssue = 'cannot_copy';
  120. return FALSE;
  121. }
  122. return TRUE;
  123. }
  124. /**
  125. * Create the status array required by the ElggInstaller
  126. *
  127. * @param string $url Rewrite test URL
  128. *
  129. * @return array
  130. */
  131. protected function returnStatus($url) {
  132. if ($this->rewriteTestPassed) {
  133. return array(
  134. 'severity' => 'pass',
  135. 'message' => elgg_echo('install:check:rewrite:success'),
  136. );
  137. }
  138. if ($this->serverSupportsRemoteRead == FALSE) {
  139. $msg = elgg_echo('install:warning:rewrite:unknown', array($url));
  140. $msg .= elgg_view('install/js_rewrite_check', array('url' => $url));
  141. return array(
  142. 'severity' => 'warning',
  143. 'message' => $msg,
  144. );
  145. }
  146. if ($this->webserver == 'apache') {
  147. $serverString = elgg_echo('install:error:rewrite:apache');
  148. $msg = "$serverString\n\n";
  149. if (!isset($this->htaccessIssue)) {
  150. $msg .= elgg_echo('install:error:rewrite:allowoverride');
  151. $msg .= elgg_view('install/js_rewrite_check', array('url' => $url));
  152. return array(
  153. 'severity' => 'failure',
  154. 'message' => $msg,
  155. );
  156. }
  157. $msg .= elgg_echo("install:error:rewrite:htaccess:{$this->htaccessIssue}");
  158. return array(
  159. 'severity' => 'failure',
  160. 'message' => $msg,
  161. );
  162. }
  163. if ($this->webserver != 'unknown') {
  164. $serverString = elgg_echo("install:error:rewrite:{$this->webserver}");
  165. $msg = "$serverString\n\n";
  166. $msg .= elgg_echo("install:error:rewrite:altserver");
  167. return array(
  168. 'severity' => 'failure',
  169. 'message' => $msg,
  170. );
  171. }
  172. return array(
  173. 'severity' => 'failure',
  174. 'message' => elgg_echo('install:error:rewrite:unknown'),
  175. );
  176. }
  177. }