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;
28 const SESSION_PREFIX = "GITManagedWebpage_";
31 private $workdir, $localdir;
32 private $ready = false;
33 private $loopedcall = false;
34 private $config = null;
35 private $config_changed = true;
36 private $activeSession = null;
38 public function __construct($giturl, $workdir = null, $localdir = null) {
39 if(session_status() != PHP_SESSION_ACTIVE) {
43 if(defined("GITMANAGED_EXECUTED")) {
44 $this->loopedcall = true;
48 if($workdir === null) {
49 $workdir = dirname(__FILE__);
50 if(substr($workdir, -1) != "/")
52 $workdir .= ".gitmanaged/";
54 else if(substr($workdir, -1) != "/")
57 if($localdir === null) {
58 $localdir = dirname(__FILE__);
59 if(substr($localdir, -1) != "/")
62 else if(substr($localdir, -1) != "/")
65 $this->giturl = $giturl;
66 $this->workdir = $workdir;
67 $this->localdir = $localdir;
69 if(!file_exists($this->workdir) || !is_dir($this->workdir)) {
70 if(file_exists($this->workdir) && !is_dir($this->workdir)) {
71 $this->error(self::ERROR_CRITICAL, "local workdir (".htmlspecialchars($this->workdir).") is not a directory.");
74 $this->setupWorkdir();
79 /* private function gitcmd(...)
80 * Execute a git command and return output
82 private function gitcmd() {
83 $args = func_get_args();
85 foreach($args as $arg) {
87 foreach($arg as $subarg) {
88 $argstr .= " ".escapeshellarg($subarg);
91 $argstr .= " ".escapeshellarg($arg);
93 $gitcmd = 'git '.escapeshellarg('--git-dir='.$this->workdir.'repository/.git').$argstr;
94 $output = shell_exec($gitcmd);
98 /* private function setConfig($name, $value)
99 * store a option in the configuration
101 private function setConfig($name, $value) {
102 $this->config[strtolower($name)] = $value;
103 $this->config_changed = true;
106 /* private function getConfig($name)
107 * get an option from the configuration
109 private function getConfig($name) {
110 if($this->config == null) {
113 if(file_exists($this->workdir."config.txt")) {
114 $config_txt = @file_get_contents($this->workdir."config.txt");
115 $this->config = unserialize($config_txt);
117 $this->config = array();
121 if(array_key_exists(strtolower($name), $this->config))
122 return $this->config[strtolower($name)];
127 private function saveConfig() {
128 if($this->config_changed && $this->ready) {
129 $fp = fopen($this->workdir."config.txt", "w");
130 fwrite($fp, serialize($this->config));
135 /* private function setupWorkdir()
136 * Setup local GITManagedWebpage Work directory with git repository
138 private function setupWorkdir() {
139 // check requirements
140 $git_exec = shell_exec('which git');
141 if(!preg_match('#git#', $git_exec)) {
142 $this->error(self::ERROR_CRITICAL, "git not installed locally.");
146 mkdir($this->workdir);
147 mkdir($this->workdir.'repository');
148 shell_exec('git clone '.escapeshellarg($this->giturl).' '.escapeshellarg($this->workdir.'repository'));
149 $gitok = $this->gitcmd("status");
150 if(preg_match("#Not a git repository#", $gitok)) {
151 rmdir($this->workdir.'repository');
152 rmdir($this->workdir);
153 $this->error(self::ERROR_CRITICAL, "error cloning git repository.");
158 $default_branch = str_replace(array("\r", "\n"), array("", ""), $this->gitcmd("rev-parse", "--abbrev-ref", "HEAD"));
159 $this->setConfig("defaultbranch", $default_branch);
163 private function getActiveBranch() {
164 if($this->activeSession)
165 return $this->activeSession;
166 else if(isset($_SESSION[self::SESSION_PREFIX.'branch']))
167 return $_SESSION[self::SESSION_PREFIX.'branch'];
169 return $this->getConfig("defaultbranch");
172 private function setActiveBranch($branch, $remember) {
173 $this->activeSession = $branch;
175 $_SESSION[self::SESSION_PREFIX.'branch'] = $branch;
178 private function getLocalUntrackedFiles() {
179 $default_branch = $this->getConfig("defaultbranch");
180 $tracked_files = $this->gitcmd("ls-tree", $default_branch, "--full-name", "--name-only");
181 $local_files = shell_exec("find ".escapeshellarg($this->localdir));
182 $untracked_files = array();
184 $tracked_files = explode("\n", str_replace(array("\r"), array(""), $tracked_files));
185 $local_files = explode("\n", str_replace(array("\r"), array(""), $local_files));
187 foreach($local_files as $local_file) {
190 if($strip_local || (($strip_local = strlen($this->localdir)) && substr($local_file, 0, $strip_local) == $this->localdir)) {
191 $local_file = substr($local_file, $strip_local);
194 foreach($tracked_files as $tracked_file) {
195 if($tracked_file == $local_file) {
201 $untracked_files[] = $local_file;
204 return $untracked_files;
207 private function branchExists($branch) {
208 //check if branch exists
209 $gitret = $this->gitcmd("rev-list", "--max-count=1", $branch);
210 if(!preg_match("#([a-z0-9]{40})#", $gitret, $match))
216 private function localBranchPath($branch, $create = false) {
217 $default_branch = $this->getConfig("defaultbranch");
218 if($branch == $default_branch)
219 $dir = $this->localdir;
221 $dir = $this->workdir.'branch_'.$branch.'/';
222 if(file_exists($dir))
231 private function updateBranch($branch, $path) {
232 if(substr($path, -1) != '/')
234 $current_branch = str_replace(array("\r", "\n"), array("", ""), $this->gitcmd("rev-parse", "--abbrev-ref", "HEAD"));
235 if($current_branch != $branch)
236 $this->gitcmd("checkout", $branch);
237 $this->gitcmd("pull");
238 $gitret = $this->gitcmd("rev-list", "--max-count=1", $branch);
239 preg_match("#([a-z0-9]{40})#", $gitret, $match);
240 $newest_version = $match[1];
242 $deleted_files = array();
243 if(($current_version = $this->getConfig('version_'.$branch))) {
244 echo $current_version . " == " . $newest_version;
245 if($current_version == $newest_version)
248 $override_all = true;
249 $delfiles = $this->gitcmd("diff", "--diff-filter=D", "--name-only", $current_version, $newest_version);
250 $delfiles = explode("\n", str_replace(array("\r"), array(""), $delfiles));
251 foreach($delfiles as $file) {
254 $deleted_files[] = $file;
258 $override_all = true;
260 $rsync_present = preg_match("#rsync#", `which rsync`);
262 shell_exec('rsync -avz --exclude ".git" '.escapeshellarg($this->workdir."repository/").' '.escapeshellarg($path));
264 shell_exec('tar -c --exclude ".git" -C '.escapeshellarg($this->workdir."repository").' . | tar -x -C '.escapeshellarg($path));
266 // remove deleted files
267 foreach($deleted_files as $file) {
271 $this->setConfig('version_'.$branch, $newest_version);
275 /* public function update()
276 * Pulls latest commit of active branch and overwrites files in branch folder
278 public function update() {
279 if($this->loopedcall)
282 $active_branch = $this->getActiveBranch();
284 if(!$this->branchExists($active_branch))
286 $dir = $this->localBranchPath($active_branch, true);
287 $this->updateBranch($active_branch, $dir);
290 public function setBranch($branch, $remember = false) {
291 if($this->loopedcall)
294 if(!$this->branchExists($branch))
296 $this->setActiveBranch($branch, $remember);
298 if(!$this->localBranchPath($branch)) {
299 $dir = $this->localBranchPath($branch, true);
300 $this->updateBranch($branch, $dir);
304 public function execute($file = null) {
305 if($this->loopedcall)
307 define("GITMANAGED_EXECUTED", true);
310 $file = $_SERVER['PHP_SELF'];
312 $file = substr($file, 1);
314 $default_branch = $this->getConfig("defaultbranch");
315 $active_branch = $this->getActiveBranch();
316 if($active_branch != $default_branch) {
317 if(!($dir = $this->localBranchPath($branch))) {
318 $dir = $this->localBranchPath($active_branch, true);
319 $this->updateBranch($active_branch, $dir);
321 include_once($dir.$file);