From 15ab535a9925f003333ab3180c8f660d53bb66e4 Mon Sep 17 00:00:00 2001 From: pk910 Date: Sat, 5 Oct 2013 22:40:38 +0200 Subject: [PATCH] initial commit --- GITManagedWebpage.class.php | 108 ++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 GITManagedWebpage.class.php diff --git a/GITManagedWebpage.class.php b/GITManagedWebpage.class.php new file mode 100644 index 0000000..9093b8a --- /dev/null +++ b/GITManagedWebpage.class.php @@ -0,0 +1,108 @@ +. * + * * + ************************************************************************ + * + * 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() { + + } + +} + +?> \ No newline at end of file -- 2.20.1