From 69726b0d7b7741122ded6994692ff24173a7caee Mon Sep 17 00:00:00 2001
From: Steven Wittens <steven@10.no-reply.drupal.org>
Date: Wed, 12 Apr 2006 08:42:47 +0000
Subject: [PATCH] #56634: Resolve issues with varying $base_url - Fix locations
 links in watchdog - Fix repeated subdirectory in page cache CIDs

---
 .htaccess                    | 12 ++++++++
 CHANGELOG.txt                |  3 ++
 includes/bootstrap.inc       | 55 ++++++++++++++++++++++++++++--------
 includes/common.inc          |  4 +--
 modules/system.module        |  2 +-
 modules/system/system.module |  2 +-
 sites/default/settings.php   | 23 +++++++++------
 7 files changed, 77 insertions(+), 24 deletions(-)

diff --git a/.htaccess b/.htaccess
index 16193a2a0492..df1209ccb82a 100644
--- a/.htaccess
+++ b/.htaccess
@@ -51,6 +51,18 @@ DirectoryIndex index.php
 <IfModule mod_rewrite.c>
   RewriteEngine on
 
+  # If your site can be accessed both with and without the prefix www.
+  # you can use one of the following settings to force user to use only one option:
+  #
+  # If you want the site to be accessed WITH the www. only, adapt and comment out the following:
+  # RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
+  # RewriteRule .* http://www.example.com/ [L,R=301]
+  #
+  # If you want the site to be accessed only WITHOUT the www. , adapt and comment out the following:
+  # RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
+  # RewriteRule .* http://example.com/ [L,R=301]
+
+
   # Modify the RewriteBase if you are using Drupal in a subdirectory and
   # the rewrite rules are not working properly.
   #RewriteBase /drupal
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index f30b472c76b5..96252d4ee98b 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -61,6 +61,9 @@ Drupal x.x.x, xxxx-xx-xx (development version)
     * added support for external URLs.
 - queue module:
     * removed from core.
+- HTTP handling:
+    * added support for a tolerant Base URL.
+    * output URIs relative to the root, without a base tag.
 
 Drupal 4.6.6, 2006-03-13
 ------------------------
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 0d2fc1f9e17a..25fd7a5e3788 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -80,7 +80,7 @@ function timer_stop($name) {
 }
 
 /**
- * Locate the appropriate configuration file.
+ * Find the appropriate configuration directory.
  *
  * Try finding a matching configuration directory by stripping the website's
  * hostname from left to right and pathname from right to left. The first
@@ -108,7 +108,7 @@ function timer_stop($name) {
  *
  * 13. $confdir/default
  */
-function conf_init() {
+function conf_path() {
   static $conf = '';
 
   if ($conf) {
@@ -131,6 +131,37 @@ function conf_init() {
   return $conf;
 }
 
+/**
+ * Loads the configuration and sets the base URL correctly.
+ */
+function conf_init() {
+  global $db_url, $db_prefix, $base_url, $base_path, $base_root;
+
+  $conf = array();
+  require_once './'. conf_path() .'/settings.php';
+
+  if (isset($base_url)) {
+    // Parse fixed base URL from settings.php.
+    $parts = parse_url($base_url);
+    $base_path = isset($parts['path']) ? $parts['path'] . '/' : '/';
+    // Build $base_root (everything until first slash after "scheme://").
+    $base_root = substr($base_url, 0, strlen($base_url) - strlen($parts['path']));
+  }
+  else {
+    // Create base URL
+    $base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
+    $base_url = $base_root .= '://'. $_SERVER['HTTP_HOST'];
+    if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {
+      $base_path = "/$dir";
+      $base_url .= $base_path;
+      $base_path .= '/';
+    }
+    else {
+      $base_path = '/';
+    }
+  }
+}
+
 /**
  * Returns and optionally sets the filename for a system item (module,
  * theme, etc.).  The filename, whether provided, cached, or retrieved
@@ -387,12 +418,12 @@ function cache_clear_all($cid = NULL, $wildcard = false) {
  * get cached either.
  */
 function page_get_cache() {
-  global $user, $base_url;
+  global $user, $base_root;
 
   $cache = NULL;
 
   if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET' && count(drupal_set_message()) == 0) {
-    $cache = cache_get($base_url . request_uri());
+    $cache = cache_get($base_root . request_uri());
 
     if (empty($cache)) {
       ob_start();
@@ -589,11 +620,14 @@ function request_uri() {
  *   A link to associate with the message.
  */
 function watchdog($type, $message, $severity = WATCHDOG_NOTICE, $link = NULL) {
-  global $user;
+  global $user, $base_root;
 
   $current_db = db_set_active();
 
-  db_query("INSERT INTO {watchdog} (uid, type, message, severity, link, location, referer, hostname, timestamp) VALUES (%d, '%s', '%s', %d, '%s', '%s', '%s', '%s', %d)", $user->uid, $type, $message, $severity, $link, request_uri(), referer_uri(), $_SERVER['REMOTE_ADDR'], time());
+  // Note: log the exact, entire absolute URL.
+  $request_uri = $base_root . request_uri();
+
+  db_query("INSERT INTO {watchdog} (uid, type, message, severity, link, location, referer, hostname, timestamp) VALUES (%d, '%s', '%s', %d, '%s', '%s', '%s', '%s', %d)", $user->uid, $type, $message, $severity, $link, $request_uri, referer_uri(), $_SERVER['REMOTE_ADDR'], time());
 
   if ($current_db) {
     db_set_active($current_db);
@@ -686,13 +720,10 @@ function _drupal_bootstrap($phase) {
 
   switch ($phase) {
     case DRUPAL_BOOTSTRAP_DATABASE:
-      global $db_url, $db_prefix, $base_url, $base_path;
-      $conf = array();
-      require_once './' . conf_init() .'/settings.php';
-      require_once './includes/database.inc';
+      // Initialize the configuration
+      conf_init();
       // Initialize the default database.
-      $parts = parse_url($base_url);
-      $base_path = (isset($parts['path']) ? $parts['path'] . '/' : '/');
+      require_once './includes/database.inc';
       db_set_active();
       break;
 
diff --git a/includes/common.inc b/includes/common.inc
index f02911b44024..368aa64fe332 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -1322,7 +1322,7 @@ function _drupal_bootstrap_full() {
  * @see drupal_page_header
  */
 function page_set_cache() {
-  global $user, $base_url;
+  global $user, $base_root;
 
   if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET') {
     // This will fail in some cases, see page_get_cache() for the explanation.
@@ -1342,7 +1342,7 @@ function page_set_cache() {
       }
       ob_end_flush();
       if ($cache && $data) {
-        cache_set($base_url . request_uri(), $data, CACHE_TEMPORARY, drupal_get_headers());
+        cache_set($base_root . request_uri(), $data, CACHE_TEMPORARY, drupal_get_headers());
       }
     }
   }
diff --git a/modules/system.module b/modules/system.module
index 08b40091dce5..8f2907882876 100644
--- a/modules/system.module
+++ b/modules/system.module
@@ -673,7 +673,7 @@ function system_default_region($theme) {
  *   An array of file objects of the specified type.
  */
 function system_listing($mask, $directory, $key = 'name', $min_depth = 1) {
-  $config = conf_init();
+  $config = conf_path();
   $searchdir = array($directory);
   $files = array();
 
diff --git a/modules/system/system.module b/modules/system/system.module
index 08b40091dce5..8f2907882876 100644
--- a/modules/system/system.module
+++ b/modules/system/system.module
@@ -673,7 +673,7 @@ function system_default_region($theme) {
  *   An array of file objects of the specified type.
  */
 function system_listing($mask, $directory, $key = 'name', $min_depth = 1) {
-  $config = conf_init();
+  $config = conf_path();
   $searchdir = array($directory);
   $files = array();
 
diff --git a/sites/default/settings.php b/sites/default/settings.php
index 96d49bc5cc78..bb8c0ab32fd1 100644
--- a/sites/default/settings.php
+++ b/sites/default/settings.php
@@ -87,9 +87,14 @@
 $db_prefix = '';
 
 /**
- * Base URL:
+ * Base URL (optional).
  *
- * The URL to your Drupal installation.
+ * If you are experiencing issues with different site domains,
+ * uncomment the Base URL statement below (remove the leading hash sign)
+ * and fill in the URL to your Drupal installation.
+ *
+ * You might also want to force users to use a given domain.
+ * See the .htaccess file for more information.
  *
  * Examples:
  *   $base_url = 'http://www.example.com';
@@ -100,7 +105,7 @@
  * It is not allowed to have a trailing slash; Drupal will add it
  * for you.
  */
-$base_url = 'http://www.example.com';  // NO trailing slash!
+# $base_url = 'http://www.example.com';  // NO trailing slash!
 
 /**
  * PHP settings:
@@ -132,10 +137,12 @@
  * useful in a configuration file for a vhost or directory, rather than
  * the default settings.php. Any configuration setting from the 'variable'
  * table can be given a new value.
+ *
+ * Remove the leading hash signs to enable.
  */
-//$conf = array(
-//  'site_name' => 'My Drupal site',
-//  'theme_default' => 'pushbutton',
-//  'anonymous' => 'Visitor'
-//);
+# $conf = array(
+#   'site_name' => 'My Drupal site',
+#   'theme_default' => 'pushbutton',
+#   'anonymous' => 'Visitor'
+# );
 
-- 
GitLab