[dev] [commit] r1064 - phplib

automailer at dexonline.ro automailer at dexonline.ro
Mon Jan 20 09:35:18 EET 2014


Author: cata
Date: Mon Jan 20 09:35:18 2014
New Revision: 1064

Log:
Better method for determining the homepage URL (wwwRoot)

Modified:
   phplib/util.php

Modified: phplib/util.php
==============================================================================
--- phplib/util.php	Fri Jan 17 10:55:10 2014	(r1063)
+++ phplib/util.php	Mon Jan 20 09:35:18 2014	(r1064)
@@ -57,15 +57,28 @@
   return $GLOBALS['util_rootPath'];
 }
 
+/**
+ * Returns the home page URL path.
+ * Algorithm: compare the current URL with the absolute file name.
+ * Travel up both paths until we encounter /wwwbase/ in the file name.
+ **/
 function util_defineWwwRoot() {
-  $fileName = $_SERVER['SCRIPT_NAME'];
+  $scriptName = $_SERVER['SCRIPT_NAME'];
+  $fileName = realpath($_SERVER['SCRIPT_FILENAME']);
   $pos = strrpos($fileName, '/wwwbase/');
   
-  if ($pos == FALSE) {
-    $GLOBALS['util_wwwRoot'] = '/';
+  if ($pos === false) {
+    $result = '/';     // This shouldn't be the case
   } else {
-    $GLOBALS['util_wwwRoot'] = substr($fileName, 0, $pos) . '/wwwbase/';
+    $tail = substr($fileName, $pos + strlen('/wwwbase/'));
+    $lenTail = strlen($tail);
+    if ($tail == substr($scriptName, -$lenTail)) {
+      $result = substr($scriptName, 0, -$lenTail);
+    } else {
+      $result = '/';
+    }
   }
+  $GLOBALS['util_wwwRoot'] = $result;
 }
 
 /**


More information about the Dev mailing list