Try to guess the script URL
Try to guess the script URL

--- a/config/gitphp.conf.php.example
+++ b/config/gitphp.conf.php.example
@@ -61,12 +61,15 @@
 /*
  * self
  * This is the path to the script that will be inserted
- * in urls.
+ * in urls.  If you leave this blank/commented the script
+ * will try to guess the correct URL, but you can override
+ * it here if it's not being guessed correctly.
+ *
  * If only specifying the directory path (and omitting the
  * index.php filename), make sure to include the trailing
  * slash!
  */
-$gitphp_conf['self'] = "http://localhost/gitphp/";
+//$gitphp_conf['self'] = "http://localhost/gitphp/";
 
 /*
  * smarty_prefix

--- a/include/display.git_commitdiff_plain.php
+++ b/include/display.git_commitdiff_plain.php
@@ -9,6 +9,7 @@
 
  include_once('util.prep_tmpdir.php');
  include_once('util.date_str.php');
+ include_once('util.script_url.php');
  include_once('gitutil.git_read_commit.php');
  include_once('gitutil.git_diff_tree.php');
  include_once('gitutil.git_rev_list.php');
@@ -17,7 +18,7 @@
 
 function git_commitdiff_plain($projectroot,$project,$hash,$hash_parent)
 {
-	global $gitphp_conf,$tpl;
+	global $tpl;
 	$ret = prep_tmpdir();
 	if ($ret !== TRUE) {
 		echo $ret;
@@ -47,7 +48,7 @@
 	$tpl->assign("subject",$co['title']);
 	if (isset($tagname))
 		$tpl->assign("tagname",$tagname);
-	$tpl->assign("url",$gitphp_conf['self'] . "?p=" . $project . "&a=commitdiff&h=" . $hash);
+	$tpl->assign("url",script_url() . "?p=" . $project . "&a=commitdiff&h=" . $hash);
 	$tpl->assign("comment",$co['comment']);
 	$tpl->display("diff_plaintext.tpl");
 	echo "\n\n";

--- a/include/display.git_opml.php
+++ b/include/display.git_opml.php
@@ -7,6 +7,7 @@
  *  Copyright (C) 2008 Christopher Han <xiphux@gmail.com>
  */
 
+ include_once('util.script_url.php');
  include_once('gitutil.git_read_projects.php');
  include_once('gitutil.git_read_head.php');
  include_once('gitutil.git_read_commit.php');
@@ -27,7 +28,7 @@
 				$co = git_read_commit($projectroot . $proj, $head);
 				$tpl->clear_all_assign();
 				$tpl->assign("proj",$proj);
-				$tpl->assign("self",$gitphp_conf['self']);
+				$tpl->assign("self",script_url());
 				$tpl->display("opml_item.tpl");
 				echo "\n";
 			}
@@ -36,7 +37,7 @@
 			$co = git_read_commit($projectroot . $plist, $head);
 			$tpl->clear_all_assign();
 			$tpl->assign("proj",$plist);
-			$tpl->assign("self",$gitphp_conf['self']);
+			$tpl->assign("self",script_url());
 			$tpl->display("opml_item.tpl");
 			echo "\n";
 		}

--- a/include/display.git_rss.php
+++ b/include/display.git_rss.php
@@ -8,6 +8,7 @@
  */
 
  include_once('util.date_str.php');
+ include_once('util.script_url.php');
  include_once('gitutil.git_read_head.php');
  include_once('gitutil.git_read_revlist.php');
  include_once('gitutil.git_read_commit.php');
@@ -15,12 +16,12 @@
 
 function git_rss($projectroot,$project)
 {
-	global $tpl,$gitphp_conf;
+	global $tpl;
 	$head = git_read_head($projectroot . $project);
 	$revlist = git_read_revlist($projectroot . $project, $head, 150);
 	header("Content-type: text/xml; charset=UTF-8");
 	$tpl->clear_all_assign();
-	$tpl->assign("self",$gitphp_conf['self']);
+	$tpl->assign("self",script_url());
 	$tpl->assign("project",$project);
 	$tpl->display("rss_header.tpl");
 
@@ -46,7 +47,7 @@
 		$tpl->assign("title",htmlentities($co['title']));
 		$tpl->assign("author",htmlentities($co['author']));
 		$tpl->assign("cdrfc2822",$cd['rfc2822']);
-		$tpl->assign("self",$gitphp_conf['self']);
+		$tpl->assign("self",script_url());
 		$tpl->assign("project",$project);
 		$tpl->assign("commit",$commit);
 		$tpl->assign("comment",$co['comment']);

--- /dev/null
+++ b/include/util.script_url.php
@@ -1,1 +1,28 @@
+<?php
+/*
+ *  util.script_url.php
+ *  gitphp: A PHP git repository browser
+ *  Component: Utility - get running script url
+ *
+ *  Copyright (C) 2009 Christopher Han <xiphux@gmail.com>
+ */
 
+function script_url()
+{
+	global $gitphp_conf;
+
+	if ($gitphp_conf['self'])
+		return $gitphp_conf['self'];
+
+	if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on'))
+		$scriptstr = "https://";
+	else
+		$scriptstr = "http://";
+
+	$scriptstr .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
+
+	return $scriptstr;
+}
+
+?>
+

comments