From 433657ce646df0cdb37e7bfdcc11056ef22b8547 Mon Sep 17 00:00:00 2001 From: Diego Escalante Urrelo Date: Sun, 30 Mar 2014 19:51:25 -0500 Subject: [PATCH] htaccess: Add install step for a proper .htaccess MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Install a new writeHtaccess() function in the install process that takes /htaccess-dist and replaces {BASE_URL} for the appropiate value used by RewriteBase to find Chamilo files. This solves the problem of Chamilo installations that are not hosted on a web server root. Namely, all the http://domain/chamilo-lms installs. The method follows similar practice by WordPress where the .htaccess file is generated by WordPress on demand when the user activates pretty permalinks. A follow–up commit enhances the safety of this process by making sure the site URL is clean enough for RewriteBase. --- .htaccess | 39 ------------------ htaccess-dist | 9 +--- .../Command/Installation/CommonCommand.php | 41 +++++++++++++++++++ .../Command/Installation/InstallCommand.php | 1 + 4 files changed, 44 insertions(+), 46 deletions(-) delete mode 100644 .htaccess diff --git a/.htaccess b/.htaccess deleted file mode 100644 index f658bbff0b..0000000000 --- a/.htaccess +++ /dev/null @@ -1,39 +0,0 @@ - - Options -MultiViews - Options +FollowSymLinks - - RewriteEngine On - RewriteBase / - - # Courses home - RewriteCond %{QUERY_STRING} ^id_session=0 - RewriteRule ^courses/(.*)/(.*)$ web/courses/$1? [R,L] - - RewriteCond %{QUERY_STRING} ^id_session=([0-9]*) - RewriteRule ^courses/(.*)/(.*)$ web/courses/$1/%1/? [R,L] - - RewriteRule ^courses/(.*)/index.php$ web/courses/$1? [R,L] - RewriteRule ^courses/(.*)/$ web/courses/$1? [R,L] - - # PHP Main files are redirected to the "web/main" zone - - RewriteRule ^main/(.*)\.php web/main/$1.php [R,L] - - # Courses documents - # courses/MATHS/document/folder1/picture.jpg --> web/data/courses/MATHS/document/folder1/picture.jpg - RewriteRule ^courses/(.*)/document/(.*)$ web/data/courses/$1/document/$2 [R,L] - - # SCORM documents - # courses/MATHS/scorm/folder1/picture.jpg --> web/data/courses/MATHS/scorm/folder1/picture.jpg - RewriteRule ^courses/(.*)/scorm/(.*)$ web/data/courses/$1/scorm/$2 [R,L] - - # Certificates - # Redirection: certificates/index.php?id=123 -> web/certificates/123 - RewriteCond %{QUERY_STRING} ^id=([0-9]*) - RewriteRule ^certificates/(.*)$ web/certificates/%1? [R,L] - - # Portal news - # news_list.php?id=5 --> web/news/5 - RewriteCond %{QUERY_STRING} ^id=([0-9]*) - RewriteRule ^news_list.php?$ web/news/%1? [R,L] - diff --git a/htaccess-dist b/htaccess-dist index e9a5205c1d..1f9d9ec83d 100644 --- a/htaccess-dist +++ b/htaccess-dist @@ -3,7 +3,7 @@ Options +FollowSymLinks RewriteEngine On - RewriteBase / + RewriteBase {BASE_URL} # Courses home RewriteCond %{QUERY_STRING} ^id_session=0 @@ -16,7 +16,7 @@ RewriteRule ^courses/(.*)/$ web/courses/$1? [R,L] # PHP Main files are redirected to the "web/main" zone - RewriteCond %{REQUEST_URI} !main/newscorm/scorm_api.php + RewriteRule ^main/(.*)\.php web/main/$1.php [R,L] # Courses documents @@ -32,11 +32,6 @@ RewriteCond %{QUERY_STRING} ^id=([0-9]*) RewriteRule ^certificates/(.*)$ web/certificates/%1? [R,L] - # User profile - # Redirection: user.php?jmontoya -> web/user/jmontoya - RewriteCond %{QUERY_STRING} ^([a-z0-9A-z]*) - RewriteRule ^user.php?$ web/user/%1? [R,L] - # Portal news # news_list.php?id=5 --> web/news/5 RewriteCond %{QUERY_STRING} ^id=([0-9]*) diff --git a/vendor/chamilo/chash/src/Chash/Command/Installation/CommonCommand.php b/vendor/chamilo/chash/src/Chash/Command/Installation/CommonCommand.php index 29d5391594..b50c2ff839 100644 --- a/vendor/chamilo/chash/src/Chash/Command/Installation/CommonCommand.php +++ b/vendor/chamilo/chash/src/Chash/Command/Installation/CommonCommand.php @@ -576,6 +576,47 @@ class CommonCommand extends AbstractCommand } } + /** + * Writes an .htaccess file with the right RewriteBase path. + * @param string $path + * @return bool + * + */ + public function writeHtaccess($path) + { + $portalSettings = $this->getPortalSettings(); + + /* This (almost) always returns a valid RewriteBase path. + * + * A few examples: + * http://localhost/ -> / + * http://localhost/chm -> /chm + * + * "Invalid" cases: + * http://localhost -> null + * http://localhost/// -> /// + * + * We'll make sure that the path is never NULL, but we can't + * do much if the admin has allowed a bogus URL like any of the + * last two. + * + * There's another check for this in + * $app->match('/portal-settings', ...) + * in main/install/index.php. + */ + $baseUrl = parse_url($portalSettings['institution_url'], PHP_URL_PATH); + + // The second case should never happen, but let's play safe. + if ($baseUrl == null or substr($baseUrl, 0, 1) != '/') { + $baseUrl = '/'; + } + + $htaccessSrc = file_get_contents($path.'/htaccess-dist'); + $htaccessFixed = str_replace('{BASE_URL}', $baseUrl, $htaccessSrc); + + return file_put_contents($path.'/.htaccess', $htaccessFixed); + } + /** * Writes the configuration file for the first time (install command) * @param string $version diff --git a/vendor/chamilo/chash/src/Chash/Command/Installation/InstallCommand.php b/vendor/chamilo/chash/src/Chash/Command/Installation/InstallCommand.php index 6b5ab47315..1405caade9 100644 --- a/vendor/chamilo/chash/src/Chash/Command/Installation/InstallCommand.php +++ b/vendor/chamilo/chash/src/Chash/Command/Installation/InstallCommand.php @@ -379,6 +379,7 @@ class InstallCommand extends CommonCommand // Generating config files (auth, profile, etc) $this->generateConfFiles($output); + $this->writeHtaccess($path); $output->writeln("Chamilo was successfully installed here: ".$this->getRootSys()." "); return 1;