Use observer to log git commands
[gitphp.git] / include / git / GitExe.class.php
blob:a/include/git/GitExe.class.php -> blob:b/include/git/GitExe.class.php
--- a/include/git/GitExe.class.php
+++ b/include/git/GitExe.class.php
@@ -67,13 +67,6 @@
 {
 
 	/**
-	 * The singleton instance
-	 *
-	 * @var GitPHP_GitExe
-	 */
-	protected static $instance;
-
-	/**
 	 * The binary path
 	 *
 	 * @var string
@@ -102,33 +95,32 @@
 	protected $observers = array();
 
 	/**
-	 * Returns the singleton instance
-	 *
-	 * @return GitPHP_GitExe instance of git exe classe
-	 */
-	public static function GetInstance()
-	{
-		if (!self::$instance) {
-			self::$instance = new GitPHP_GitExe(GitPHP_Config::GetInstance()->GetValue('gitbin'));
-			self::$instance->AddObserver(GitPHP_DebugLog::GetInstance());
-		}
-		return self::$instance;
-	}
-
-	/**
-	 * Releases the singleton instance
-	 */
-	public static function DestroyInstance()
-	{
-		self::$instance = null;
-	}
+	 * Whether the exec function is allowed by the install
+	 *
+	 * @var null|boolean
+	 */
+	protected $execAllowed = null;
+
+	/**
+	 * Whether the shell_exec function is allowed by the install
+	 *
+	 * @var null|boolean
+	 */
+	protected $shellExecAllowed = null;
+
+	/**
+	 * Whether the popen function is allowed by the install
+	 *
+	 * @var null|boolean
+	 */
+	protected $popenAllowed = null;
 
 	/**
 	 * Constructor
 	 *
 	 * @param string $binary path to git binary
 	 */
-	protected function __construct($binary)
+	public function __construct($binary = '')
 	{
 		if (empty($binary)) {
 			$binary = GitPHP_GitExe::DefaultBinary();
@@ -146,6 +138,13 @@
 	 */
 	public function Execute($projectPath, $command, $args)
 	{
+		if ($this->shellExecAllowed === null) {
+			$this->shellExecAllowed = GitPHP_Util::FunctionAllowed('shell_exec');
+			if (!$this->shellExecAllowed) {
+				throw new GitPHP_DisabledFunctionException('shell_exec');
+			}
+		}
+
 		$fullCommand = $this->CreateCommand($projectPath, $command, $args);
 
 		$this->Log('Begin executing "' . $fullCommand . '"');
@@ -169,6 +168,13 @@
 	 */
 	public function Open($projectPath, $command, $args, $mode = 'r')
 	{
+		if ($this->popenAllowed === null) {
+			$this->popenAllowed = GitPHP_Util::FunctionAllowed('popen');
+			if (!$this->popenAllowed) {
+				throw new GitPHP_DisabledFunctionException('popen');
+			}
+		}
+
 		$fullCommand = $this->CreateCommand($projectPath, $command, $args);
 
 		return popen($fullCommand, $mode);
@@ -186,7 +192,7 @@
 	{
 		$gitDir = '';
 		if (!empty($projectPath)) {
-			$gitDir = '--git-dir=' . $projectPath;
+			$gitDir = '--git-dir=' . escapeshellarg($projectPath);
 		}
 		
 		return $this->binary . ' ' . $gitDir . ' ' . $command . ' ' . implode(' ', $args);
@@ -220,6 +226,13 @@
 	 */
 	protected function ReadVersion()
 	{
+		if ($this->shellExecAllowed === null) {
+			$this->shellExecAllowed = GitPHP_Util::FunctionAllowed('shell_exec');
+			if (!$this->shellExecAllowed) {
+				throw new GitPHP_DisabledFunctionException('shell_exec');
+			}
+		}
+
 		$this->versionRead = true;
 
 		$this->version = '';
@@ -304,6 +317,13 @@
 	 */
 	public function Valid()
 	{
+		if ($this->execAllowed === null) {
+			$this->execAllowed = GitPHP_Util::FunctionAllowed('exec');
+			if (!$this->execAllowed) {
+				throw new GitPHP_DisabledFunctionException('exec');
+			}
+		}
+
 		if (empty($this->binary))
 			return false;
 

comments