. * * * ************************************************************************ * * GITManagedWebpage.class.php * * Functions to manage a Website using GIT Version Control * */ class GITManagedWebpage { const ERROR_CRITICAL = 1; private $giturl; private $workdir; public function __construct($giturl, $workdir = null) { if($workdir === null) { $workdir = dirname(__FILE__); if(!substr($workdir, -1) == "/") $workdir .= "/"; $workdir .= ".gitmanaged/"; } else if(!substr($workdir, -1) == "/") $workdir .= "/"; $this->giturl = $giturl; $this->workdir = $workdir; //if(!file_exists($this->workdir) || !is_dir($this->workdir)) { if(file_exists($this->workdir) && !is_dir($this->workdir)) { $this->error(self::ERROR_CRITICAL, "local workdir (".htmlspecialchars($this->workdir).") is not a directory."); return; } $this->setupWorkdir(); //} } /* private function gitcmd(...) * Execute a git command and return output */ private function gitcmd() { $args = func_get_args(); $argstr = ""; foreach($args as $arg) { if(is_array($arg)) { foreach($arg as $subarg) { $argstr .= " ".escapeshellarg($subarg); } } else $argstr .= " ".escapeshellarg($arg); } $gitcmd = 'git '.escapeshellarg('--git-dir='.$this->workdir.'/repository').$argstr; $output = shell_exec($gitcmd); return $output; } /* private function setupWorkdir() * Setup local GITManagedWebpage Work directory with git repository */ private function setupWorkdir() { // check requirements $git_exec = shell_exec('which git'); if(!preg_match('#git#', $git_exec)) { $this->error(self::ERROR_CRITICAL, "git not installed locally."); return; } mkdir($this->workdir); mkdir($this->workdir.'/repository'); shell_exec('git clone '.escapeshellarg($this->giturl).' '.escapeshellarg($this->workdir.'/repository')); $gitok = $this->gitcmd("status"); if(preg_match("#Not a git repository#", $gitok)) { rmdir($this->workdir.'/repository'); rmdir($this->workdir); $this->error(self::ERROR_CRITICAL, "error cloning git repository."); return; } } /* public function update() * Pulls latest commit of active branch and overwrites local files */ public function update() { } } ?>