initial commit
authorpk910 <philipp@zoelle1.de>
Sat, 5 Oct 2013 20:40:38 +0000 (22:40 +0200)
committerpk910 <philipp@zoelle1.de>
Sat, 5 Oct 2013 20:40:38 +0000 (22:40 +0200)
GITManagedWebpage.class.php [new file with mode: 0644]

diff --git a/GITManagedWebpage.class.php b/GITManagedWebpage.class.php
new file mode 100644 (file)
index 0000000..9093b8a
--- /dev/null
@@ -0,0 +1,108 @@
+<?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