Get rid of git exe singleton
[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
@@ -95,11 +95,32 @@
 	protected $observers = array();
 
 	/**
+	 * 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
 	 */
-	public function __construct($binary)
+	public function __construct($binary = '')
 	{
 		if (empty($binary)) {
 			$binary = GitPHP_GitExe::DefaultBinary();
@@ -117,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 . '"');
@@ -140,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);
@@ -157,7 +192,7 @@
 	{
 		$gitDir = '';
 		if (!empty($projectPath)) {
-			$gitDir = '--git-dir=' . $projectPath;
+			$gitDir = '--git-dir=' . escapeshellarg($projectPath);
 		}
 		
 		return $this->binary . ' ' . $gitDir . ' ' . $command . ' ' . implode(' ', $args);
@@ -191,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 = '';
@@ -275,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