Move the code to add a slash into one place
Move the code to add a slash into one place

--- /dev/null
+++ b/include/Util.class.php
@@ -1,1 +1,45 @@
+<?php
+/**
+ * GitPHP Util
+ *
+ * Utility functions
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2010 Christopher Han
+ * @package GitPHP
+ */
 
+/**
+ * Util class
+ *
+ * @package GitPHP
+ */
+class GitPHP_Util
+{
+
+	/**
+	 * AddSlash
+	 *
+	 * Adds a trailing slash to a directory path if necessary
+	 *
+	 * @access public
+	 * @static
+	 * @param string $path path to add slash to
+	 * @param $backslash true to also check for backslash (windows paths)
+	 * @return string $path with a trailing slash
+	 */
+	public static function AddSlash($path, $backslash = true)
+	{
+		if (empty($path))
+			return $path;
+
+		$end = substr($path, -1);
+
+		if (!(($end == '/') || ($backslash && (strtoupper(substr(PHP_OS, 0, 3))) && ($end == '\\'))))
+			$path .= '/';
+
+		return $path;
+	}
+
+}
+

--- a/include/git/Project.class.php
+++ b/include/git/Project.class.php
@@ -203,9 +203,7 @@
 	 */
 	private function SetProject($project)
 	{
-		$projectRoot = GitPHP_Config::GetInstance()->GetValue('projectroot');
-		if (substr($projectRoot, -1) != '/')
-			$projectRoot .= '/';
+		$projectRoot = GitPHP_Util::AddSlash(GitPHP_Config::GetInstance()->GetValue('projectroot'));
 
 		$realProjectRoot = realpath($projectRoot);
 		$path = $projectRoot . $project;
@@ -316,9 +314,7 @@
 	 */
 	public function GetPath()
 	{
-		$projectRoot = GitPHP_Config::GetInstance()->GetValue('projectroot');
-		if (substr($projectRoot, -1) != '/')
-			$projectRoot .= '/';
+		$projectRoot = GitPHP_Util::AddSlash(GitPHP_Config::GetInstance()->GetValue('projectroot'));
 
 		return $projectRoot . $this->project;
 	}
@@ -411,13 +407,9 @@
 		if ($this->cloneUrl !== null)
 			return $this->cloneUrl;
 
-		$cloneurl = GitPHP_Config::GetInstance()->GetValue('cloneurl', '');
-		if (!empty($cloneurl)) {
-			if (substr($cloneurl, -1) != '/') {
-				$cloneurl .= '/';
-			}
+		$cloneurl = GitPHP_Util::AddSlash(GitPHP_Config::GetInstance()->GetValue('cloneurl', ''), false);
+		if (!empty($cloneurl))
 			$cloneurl .= $this->project;
-		}
 
 		return $cloneurl;
 	}
@@ -448,13 +440,10 @@
 		if ($this->pushUrl !== null)
 			return $this->pushUrl;
 
-		$pushurl = GitPHP_Config::GetInstance()->GetValue('pushurl', '');
-		if (!empty($pushurl)) {
-			if (substr($pushurl, -1) != '/') {
-				$pushurl .= '/';
-			}
+		$pushurl = GitPHP_Util::AddSlash(GitPHP_Config::GetInstance()->GetValue('pushurl', ''), false);
+		if (!empty($pushurl))
 			$pushurl .= $this->project;
-		}
+
 		return $pushurl;
 	}
 

--- a/include/git/ProjectListDirectory.class.php
+++ b/include/git/ProjectListDirectory.class.php
@@ -47,10 +47,7 @@
 			throw new Exception(sprintf(__('%1$s is not a directory'), $projectDir));
 		}
 
-		$this->projectDir = $projectDir;
-
-		if (substr($this->projectDir, -1) != '/')
-			$this->projectDir .= '/';
+		$this->projectDir = GitPHP_Util::AddSlash($projectDir);
 
 		parent::__construct();
 	}

--- a/include/git/ProjectListFile.class.php
+++ b/include/git/ProjectListFile.class.php
@@ -57,9 +57,7 @@
 			throw new Exception(sprintf(__('Failed to open project list file %1$s'), $this->projectConfig));
 		}
 
-		$projectRoot = GitPHP_Config::GetInstance()->GetValue('projectroot');
-		if (substr($projectRoot, -1) != '/')
-			$projectRoot .= '/';
+		$projectRoot = GitPHP_Util::AddSlash(GitPHP_Config::GetInstance()->GetValue('projectroot'));
 
 		while (!feof($fp) && ($line = fgets($fp))) {
 			if (preg_match('/^([^\s]+)(\s.+)?$/', $line, $regs)) {

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -23,6 +23,8 @@
 define('GITPHP_LOCALEDIR', GITPHP_BASEDIR . 'locale/');
 
 include_once(GITPHP_INCLUDEDIR . 'version.php');
+
+require_once(GITPHP_INCLUDEDIR . 'Util.class.php');
 
 require_once(GITPHP_INCLUDEDIR . 'Config.class.php');
 

comments