<?php

// Utility script to patch Oscommerce for use on the secure PHP Environment

// USAGE : this script should be placed in the catalog directory of OSC and
//         run through the browser

chdir( dirname(__FILE__) );
$patch_file = patch_details();

if( !$hdl = popen('/usr/bin/patch --quiet -p2','w')) {
	echo "ERROR: Unable to open patch command";
	exit;
}
if( fwrite($hdl, $patch_file) === false ) {
	echo "ERROR: Unable to write to patch functions";
	exit;
}
pclose($hdl);


?>
<b>osCommerce has been patched successfully.</b><br><br>
<b>Installation Instructions</b><br>
<li> Make sure you use the Database option as your "Session Storage" setting.</li>
<li> Once the install procedure has completed, you can set permissions on includes/configure.php and admin/includes/configure.php to 400, which is READ ONLY to OWNER.</li>
<li> It is also recommended that you rename the admin folder to a more cryptic name, as there is no password facility for the osCommerce admin area. When you rename the folder, you will need to update your admin/includes/configure.php file to reflect these changes.</li>
<li>If you choose to use the SSL option, "Enable SSL Connections", you will need to update the https:// server field to the correct SSL server.  If you are using the shared SSL certificate, the Secure Cookie Path will be : /YOUR_HOSTING_USERNAME</li>
<br><br>
<a href=index.php>Continue with Installation &gt;&gt;</a>

<?php

function patch_details () {

$out = <<<ENDOFPATCH
diff -uNbBr oscommerce_vanilla/catalog/admin/includes/application_top.php oscommerce/catalog/admin/includes/application_top.php
--- oscommerce_vanilla/catalog/admin/includes/application_top.php	Fri Sep 30 15:26:52 2005
+++ oscommerce/catalog/admin/includes/application_top.php	Tue Feb 21 12:41:52 2006
@@ -32,7 +32,11 @@
   define('PROJECT_VERSION', 'osCommerce 2.2-MS2');
 
 // set php_self in the local scope
-  \$PHP_SELF = (isset(\$HTTP_SERVER_VARS['PHP_SELF']) ? \$HTTP_SERVER_VARS['PHP_SELF'] : \$HTTP_SERVER_VARS['SCRIPT_NAME']);
+  //\$PHP_SELF = (isset(\$HTTP_SERVER_VARS['PHP_SELF']) ? \$HTTP_SERVER_VARS['PHP_SELF'] : \$HTTP_SERVER_VARS['SCRIPT_NAME']);
+  \$PHP_SELF = \$_SERVER['SCRIPT_NAME'];
+  if ('/' == \$PHP_SELF) {
+      \$PHP_SELF = '/index.php';
+  }
 
 // Used in the "Backup Manager" to compress backups
   define('LOCAL_EXE_GZIP', '/usr/bin/gzip');
diff -uNbBr oscommerce_vanilla/catalog/includes/application_top.php oscommerce/catalog/includes/application_top.php
--- oscommerce_vanilla/catalog/includes/application_top.php	Fri Sep 30 15:26:22 2005
+++ oscommerce/catalog/includes/application_top.php	Tue Feb 21 11:59:58 2006
@@ -41,7 +41,11 @@
   \$request_type = (getenv('HTTPS') == 'on') ? 'SSL' : 'NONSSL';
 
 // set php_self in the local scope
-  if (!isset(\$PHP_SELF)) \$PHP_SELF = \$HTTP_SERVER_VARS['PHP_SELF'];
+  //if (!isset(\$PHP_SELF)) \$PHP_SELF = \$HTTP_SERVER_VARS['PHP_SELF'];
+  \$PHP_SELF = \$_SERVER['SCRIPT_URL'];
+  if ('/' == \$PHP_SELF) {
+      \$PHP_SELF = '/index.php';
+  }
 
   if (\$request_type == 'NONSSL') {
     define('DIR_WS_CATALOG', DIR_WS_HTTP_CATALOG);
diff -uNbBr oscommerce_vanilla/catalog/includes/functions/general.php oscommerce/catalog/includes/functions/general.php
--- oscommerce_vanilla/catalog/includes/functions/general.php	Fri Sep 30 15:11:08 2005
+++ oscommerce/catalog/includes/functions/general.php	Tue Feb 21 12:01:52 2006
@@ -26,7 +26,8 @@
 
     if ( (ENABLE_SSL == true) && (getenv('HTTPS') == 'on') ) { // We are loading an SSL page
       if (substr(\$url, 0, strlen(HTTP_SERVER)) == HTTP_SERVER) { // NONSSL url
-        \$url = HTTPS_SERVER . substr(\$url, strlen(HTTP_SERVER)); // Change it to SSL
+        //\$url = HTTPS_SERVER . substr(\$url, strlen(HTTP_SERVER)); // Change it to SSL
+		\$url = HTTPS_SERVER . DIR_WS_HTTPS_CATALOG . substr(\$url, strlen(HTTP_SERVER . DIR_WS_HTTP_CATALOG)); // Change it to SSL
       }
     }
 
diff -uNbBr oscommerce_vanilla/catalog/install/templates/pages/install_2.php oscommerce/catalog/install/templates/pages/install_2.php
--- oscommerce_vanilla/catalog/install/templates/pages/install_2.php	Sat Jul 12 23:10:08 2003
+++ oscommerce/catalog/install/templates/pages/install_2.php	Tue Feb 21 12:03:38 2006
@@ -78,9 +78,11 @@
       if (empty(\$script_filename)) {
         \$script_filename = getenv('SCRIPT_FILENAME');
       }
+	  \$script_filename = getenv('SCRIPT_FILENAME');
 
       \$script_filename = str_replace('\\\\', '/', \$script_filename);
-      \$script_filename = str_replace('//', '/', \$script_filename);
+      //\$script_filename = str_replace('//', '/', \$script_filename);
+	  \$script_filename = preg_replace('{//+}', '/', \$script_filename);
 
       \$dir_fs_www_root_array = explode('/', dirname(\$script_filename));
       \$dir_fs_www_root = array();
diff -uNbBr oscommerce_vanilla/catalog/install/templates/pages/install_3.php oscommerce/catalog/install/templates/pages/install_3.php
--- oscommerce_vanilla/catalog/install/templates/pages/install_3.php	Sat Jul 12 05:59:02 2003
+++ oscommerce/catalog/install/templates/pages/install_3.php	Tue Feb 21 12:04:28 2006
@@ -14,9 +14,11 @@
   if (empty(\$script_filename)) {
     \$script_filename = getenv('SCRIPT_FILENAME');
   }
+  \$script_filename = getenv('SCRIPT_FILENAME');
 
   \$script_filename = str_replace('\\\\', '/', \$script_filename);
-  \$script_filename = str_replace('//', '/', \$script_filename);
+  //\$script_filename = str_replace('//', '/', \$script_filename);
+  \$script_filename = preg_replace('{//+}', '/', \$script_filename);
 
   \$dir_fs_www_root_array = explode('/', dirname(\$script_filename));
   \$dir_fs_www_root = array();
diff -uNbBr oscommerce_vanilla/catalog/install/templates/pages/install_4.php oscommerce/catalog/install/templates/pages/install_4.php
--- oscommerce_vanilla/catalog/install/templates/pages/install_4.php	Sat Jul 12 05:59:02 2003
+++ oscommerce/catalog/install/templates/pages/install_4.php	Tue Feb 21 12:06:23 2006
@@ -10,18 +10,22 @@
   Released under the GNU General Public License
 */
 
-  \$cookie_path = substr(dirname(getenv('SCRIPT_NAME')), 0, -7);
+  //\$cookie_path = substr(dirname(getenv('SCRIPT_NAME')), 0, -7);
+  \$cookie_path = substr(dirname(getenv('SCRIPT_URL')), 0, -7);
 
-  \$www_location = 'http://' . getenv('HTTP_HOST') . getenv('SCRIPT_NAME');
+  //\$www_location = 'http://' . getenv('HTTP_HOST') . getenv('SCRIPT_NAME');
+  \$www_location = 'http://' . getenv('HTTP_HOST') . getenv('SCRIPT_URL');
   \$www_location = substr(\$www_location, 0, strpos(\$www_location, 'install'));
 
   \$script_filename = getenv('PATH_TRANSLATED');
   if (empty(\$script_filename)) {
     \$script_filename = getenv('SCRIPT_FILENAME');
   }
+  \$script_filename = getenv('SCRIPT_FILENAME');
 
   \$script_filename = str_replace('\\\\', '/', \$script_filename);
-  \$script_filename = str_replace('//', '/', \$script_filename);
+  //\$script_filename = str_replace('//', '/', \$script_filename);
+  \$script_filename = preg_replace('{//+}', '/', \$script_filename);
 
   \$dir_fs_www_root_array = explode('/', dirname(\$script_filename));
   \$dir_fs_www_root = array();\

ENDOFPATCH;
                                                                                                                          
return $out;
                                                                                                                          
}

?>
