Location: PHPKode > projects > SvnDashboard > 1.1.3.140/phpLib/functions/console.funcs.php
<?php

require_once( 'array.funcs.php' );

function console( $commands, &$errors = array( ))
{
	if (!is_array( $errors ))
	{
		if ($errors !== NULL)
		{
			$errors = array( $errors );
		}
		else
		{
			$errors = array( );
		}
	}

	if (!is_array( $commands ))
	{
		if ($commands !== NULL)
		{
			$commands = array( $commands );
		}
		else
		{
			$commands = array( );
		}
	}

	$success = TRUE;
	foreach ($commands as $command)
	{
		$origCommand = $command;

		$subCommands = arrayTrim( explode( ';', $command ));
		foreach ($subCommands as &$command)
		{
			$command = escapeshellcmd( trim( $command ));

			// ==================== Ensure Errors Are Routed to stdout
			$origCommand = $command;
			if (!preg_match( '/\b\d+\>\&\d+\b/', $command )) // -- redirect not present
			{
				$command .= ' 2>&1';
			}
		}
		$command = implode( '; ', $subCommands );

		// ==================== Execute Command
		unset( $output ); // -- reset command errors array
		exec( $command, $output, $result );

		// ==================== Determine Return Value
		if ($result != 0) // -- errors occurred
		{
			$success = FALSE;

			$output = arrayTrim( $output );
			$errors = array_merge( $errors, array( $origCommand => $output ));
		}
	}

	return $success;
}

function remoteIsDir( $username, $host, $path )
{
	$isDir = FALSE;
	$timeout = 5;
	$command = sprintf( 'ssh -T -o ConnectTimeout=%d %s@%s', $timeout, $username, $host );
	$subCommands = array( );

	$subCommands[] = sprintf( 'file %s', $path );
	$string = $command.' "'.implode( '; ', $subCommands ).'"';

	exec( $string, $output, $success );

	if ($success == 0)
	{
		$output = reset( $output );

		if (preg_match( '/\bdirectory\b/i', $output) && !preg_match( '/\berror\b/i', $output))
		{
			$isDir = TRUE;
		}
	}

	return $isDir;
}

function remoteFileExists( $username, $host, $path )
{
	$exists = FALSE;
	$timeout = 5;
	$command = sprintf( 'ssh -T -o ConnectTimeout=%d %s@%s', $timeout, $username, $host );
	$subCommands = array( );

	$subCommands[] = sprintf( 'file %s', $path );
	$string = $command.' "'.implode( '; ', $subCommands ).'"';

	exec( $string, $output, $success );

	if ($success == 0)
	{
		$output = reset( $output );

		if (!preg_match( '/\berror\b/i', $output))
		{
			$exists = TRUE;
		}
	}

	return $exists;
}

function remoteFileList( $username, $host, $path )
{
	$timeout = 5;
	$command = sprintf( 'ssh -T -o ConnectTimeout=%d %s@%s', $timeout, $username, $host );
	$subCommands = array( );
	
	$subCommands[] = escapeshellcmd( sprintf( 'ls -a %s', $path )).' 2>&1';
	$string = $command.' "'.implode( '; ', $subCommands ).'"';
	exec( $string, $output, $success );
	
	if ($success == 0)
	{
		return $output;
	}
	else
	{
		throw new Exception( 'remoteFileList( ): Failed: '.implode( '; -- ', $output ));
	}
}

if (!class_exists( 'ConsoleException' ))
{
	class ConsoleException extends Exception
	{
		private $_command;
		
		public function __construct( $message, $command )
		{
			parent::__construct( $message );
			
			$this->_command = $command;
		}
		
		// --
		
		public function getCommand( )
		{
			return $this->_command;
		}
	}
}

?>
Return current item: SvnDashboard