--- /dev/null
+<?php
+/*************************** GITManagedWebpage **************************
+ * Copyright (C) 2013 Philipp Kreil (pk910) *
+ * *
+ * This program is free software: you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation, either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+ * *
+ ************************************************************************
+ *
+ * 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