2 /*************************** GITManagedWebpage **************************
3 * Copyright (C) 2013 Philipp Kreil (pk910) *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 ************************************************************************
20 * GITManagedWebpage.class.php
22 * Functions to manage a Website using GIT Version Control
26 class GITManagedWebpage {
27 const ERROR_CRITICAL = 1;
33 public function __construct($giturl, $workdir = null) {
34 if($workdir === null) {
35 $workdir = dirname(__FILE__);
36 if(!substr($workdir, -1) == "/")
38 $workdir .= ".gitmanaged/";
40 else if(!substr($workdir, -1) == "/")
43 $this->giturl = $giturl;
44 $this->workdir = $workdir;
46 //if(!file_exists($this->workdir) || !is_dir($this->workdir)) {
47 if(file_exists($this->workdir) && !is_dir($this->workdir)) {
48 $this->error(self::ERROR_CRITICAL, "local workdir (".htmlspecialchars($this->workdir).") is not a directory.");
51 $this->setupWorkdir();
55 /* private function gitcmd(...)
56 * Execute a git command and return output
58 private function gitcmd() {
59 $args = func_get_args();
61 foreach($args as $arg) {
63 foreach($arg as $subarg) {
64 $argstr .= " ".escapeshellarg($subarg);
67 $argstr .= " ".escapeshellarg($arg);
69 $gitcmd = 'git '.escapeshellarg('--git-dir='.$this->workdir.'/repository').$argstr;
70 $output = shell_exec($gitcmd);
74 /* private function setupWorkdir()
75 * Setup local GITManagedWebpage Work directory with git repository
77 private function setupWorkdir() {
79 $git_exec = shell_exec('which git');
80 if(!preg_match('#git#', $git_exec)) {
81 $this->error(self::ERROR_CRITICAL, "git not installed locally.");
85 mkdir($this->workdir);
86 mkdir($this->workdir.'/repository');
87 shell_exec('git clone '.escapeshellarg($this->giturl).' '.escapeshellarg($this->workdir.'/repository'));
88 $gitok = $this->gitcmd("status");
89 if(preg_match("#Not a git repository#", $gitok)) {
90 rmdir($this->workdir.'/repository');
91 rmdir($this->workdir);
92 $this->error(self::ERROR_CRITICAL, "error cloning git repository.");
99 /* public function update()
100 * Pulls latest commit of active branch and overwrites local files
102 public function update() {