9093b8a4e2c0795848076be94474529ae703db01
[GITManagedWebpage.git] / GITManagedWebpage.class.php
1 <?php
2 /*************************** GITManagedWebpage **************************
3  * Copyright (C) 2013       Philipp Kreil (pk910)                       *
4  *                                                                      *
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.                                  *
9  *                                                                      *
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.                         *
14  *                                                                      *
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/>. *
17  *                                                                      *
18  ************************************************************************
19  *
20  *  GITManagedWebpage.class.php
21  *
22  * Functions to manage a Website using GIT Version Control
23  *
24  */
25
26 class GITManagedWebpage {
27     const ERROR_CRITICAL = 1;
28     
29
30     private $giturl;
31     private $workdir;
32     
33     public function __construct($giturl, $workdir = null) {
34         if($workdir === null) {
35             $workdir = dirname(__FILE__);
36             if(!substr($workdir, -1) == "/")
37                 $workdir .= "/";
38             $workdir .= ".gitmanaged/";
39         }
40         else if(!substr($workdir, -1) == "/")
41             $workdir .= "/";
42         
43         $this->giturl = $giturl;
44         $this->workdir = $workdir;
45         
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.");
49                 return;
50             }
51             $this->setupWorkdir();
52         //}
53     }
54     
55     /* private function gitcmd(...)
56     * Execute a git command and return output
57     */
58     private function gitcmd() {
59         $args = func_get_args();
60         $argstr = "";
61         foreach($args as $arg) {
62             if(is_array($arg)) {
63                 foreach($arg as $subarg) {
64                     $argstr .= " ".escapeshellarg($subarg);
65                 }
66             } else
67                 $argstr .= " ".escapeshellarg($arg);
68         }
69         $gitcmd = 'git '.escapeshellarg('--git-dir='.$this->workdir.'/repository').$argstr;
70         $output = shell_exec($gitcmd);
71         return $output;
72     }
73     
74     /* private function setupWorkdir()
75     * Setup local GITManagedWebpage Work directory with git repository
76     */
77     private function setupWorkdir() {
78         // check requirements
79         $git_exec = shell_exec('which git');
80         if(!preg_match('#git#', $git_exec)) {
81             $this->error(self::ERROR_CRITICAL, "git not installed locally.");
82             return;
83         }
84         
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.");
93             return;
94         }
95         
96         
97     }
98     
99     /* public function update()
100     * Pulls latest commit of active branch and overwrites local files
101     */
102     public function update() {
103     
104     }
105     
106 }
107
108 ?>