aboutsummaryrefslogtreecommitdiff
path: root/install/ElggRewriteTester.php
diff options
context:
space:
mode:
authorcash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>2010-10-11 12:00:02 +0000
committercash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>2010-10-11 12:00:02 +0000
commit393ca13a6d57579569926662d7882cdf09c57ab5 (patch)
tree1b0c8704aa0558b4c816905a6d7678e3edb17891 /install/ElggRewriteTester.php
parent478fe8d514ee9b75bd552b2894c744a04196fb24 (diff)
downloadelgg-393ca13a6d57579569926662d7882cdf09c57ab5.tar.gz
elgg-393ca13a6d57579569926662d7882cdf09c57ab5.tar.bz2
Pulled rewrite test out into separate class
git-svn-id: http://code.elgg.org/elgg/trunk@7052 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'install/ElggRewriteTester.php')
-rw-r--r--install/ElggRewriteTester.php181
1 files changed, 181 insertions, 0 deletions
diff --git a/install/ElggRewriteTester.php b/install/ElggRewriteTester.php
new file mode 100644
index 000000000..7f3501059
--- /dev/null
+++ b/install/ElggRewriteTester.php
@@ -0,0 +1,181 @@
+<?php
+/**
+ * Elgg RewriteTester.
+ * Test if URL rewriting is working.
+ *
+ * @package Elgg
+ * @subpackage Installer
+ */
+
+
+class ElggRewriteTester {
+ protected $webserver;
+ protected $serverSupportsRemoteRead;
+ protected $rewriteTestPassed;
+ protected $htaccessIssue;
+
+ public function __construct() {
+ $this->webserver = 'unknown';
+ }
+
+ /**
+ * Run the rewrite test and return a status array
+ *
+ * @param string $url URL of rewrite test
+ * @param string $path Root directory of Elgg with trailing slash
+ * @return array
+ */
+ public function run($url, $path) {
+
+ $this->guessWebServer();
+
+ $this->rewriteTestPassed = $this->runRewriteTest($url);
+
+ if ($this->rewriteTestPassed == FALSE) {
+ if ($this->webserver == 'apache' || $this->webserver == 'unknown') {
+ if ($this->createHtaccess($path)) {
+ $this->rewriteTestPassed = $this->runRewriteTest($url);
+ }
+ }
+ }
+
+ return $this->returnStatus($url);
+ }
+
+ /**
+ * Guess the web server from $_SERVER['SERVER_SOFTWARE']
+ */
+ protected function guessWebServer() {
+ $serverString = strtolower($_SERVER['SERVER_SOFTWARE']);
+ $possibleServers = array('apache', 'nginx', 'lighttpd', 'iis');
+ foreach ($possibleServers as $server) {
+ if (strpos($serverString, $server) !== FALSE) {
+ $this->webserver = $server;
+ return;
+ }
+ }
+ }
+
+ /**
+ * Hit the rewrite test URL to determine if the rewrite rules are working
+ *
+ * @param string $url Rewrite test URL
+ * @return bool
+ */
+ protected function runRewriteTest($url) {
+
+ $this->serverSupportsRemoteRead = TRUE;
+
+ if (function_exists('curl_init')) {
+ // try curl if installed
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_TIMEOUT, 10);
+ $response = curl_exec($ch);
+ curl_close($ch);
+ return $response === 'success';
+ } else if (ini_get('allow_url_fopen')) {
+ // use file_get_contents as fallback
+ $response = file_get_contents($url);
+ return $response === 'success';
+ } else {
+ $this->serverSupportsRemoteRead = FALSE;
+ return FALSE;
+ }
+ }
+
+ /**
+ * Create Elgg's .htaccess file or confirm that it exists
+ *
+ * @param string $path Elgg's root directory with trailing slash
+ * @return bool
+ */
+ protected function createHtaccess($path) {
+ $filename = "{$path}.htaccess";
+ if (file_exists($filename)) {
+ // check that this is the Elgg .htaccess
+ $data = file_get_contents($filename);
+ if ($data === FALSE) {
+ // don't have permission to read the file
+ $this->htaccessIssue = 'read_permission';
+ return FALSE;
+ }
+ if (strpos($data, 'Elgg') === FALSE) {
+ $this->htaccessIssue = 'non_elgg_htaccess';
+ return FALSE;
+ } else {
+ return TRUE;
+ }
+ }
+
+ if (!is_writable($path)) {
+ $this->htaccessIssue = 'write_permission';
+ return FALSE;
+ }
+
+ // create the .htaccess file
+ $result = copy("{$path}htaccess_dist", $filename);
+ if (!$result) {
+ $this->htaccessIssue = 'cannot_copy';
+ return FALSE;
+ }
+
+ return TRUE;
+ }
+
+ /**
+ * Create the status array required by the ElggInstaller
+ *
+ * @param string $url Rewrite test URL
+ * @return array
+ */
+ protected function returnStatus($url) {
+ if ($this->rewriteTestPassed) {
+ return array(
+ 'severity' => 'pass',
+ 'message' => elgg_echo('install:check:rewrite:success'),
+ );
+ }
+
+ if ($this->serverSupportsRemoteRead == FALSE) {
+ $msg = sprintf(elgg_echo('install:warning:rewrite:unknown'), $url);
+ return array(
+ 'severity' => 'warning',
+ 'message' => $msg,
+ );
+ }
+
+ if ($this->webserver == 'apache') {
+ $serverString = elgg_echo('install:error:rewrite:apache');
+ $msg = "$serverString\n\n";
+ if (!isset($this->htaccessIssue)) {
+ $msg .= elgg_echo('install:error:rewrite:allowoverride');
+ return array(
+ 'severity' => 'failure',
+ 'message' => $msg,
+ );
+ }
+ $msg .= elgg_echo("install:error:rewrite:htaccess:{$this->htaccessIssue}");
+ return array(
+ 'severity' => 'failure',
+ 'message' => $msg,
+ );
+ }
+
+ if ($this->webserver != 'unknown') {
+ $serverString = elgg_echo("install:error:rewrite:{$this->webserver}");
+ $msg = "$serverString\n\n";
+ $msg .= elgg_echo("install:error:rewrite:altserver");
+ return array(
+ 'severity' => 'failure',
+ 'message' => $msg,
+ );
+ }
+
+ return array(
+ 'severity' => 'failure',
+ 'message' => elgg_echo('install:error:rewrite:unknown'),
+ );
+ }
+} \ No newline at end of file