forked from nledez/php-git-hooker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooker.php
72 lines (61 loc) · 1.91 KB
/
hooker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
$base = __DIR__;
$filename = __FILE__;
$dest = "dest";
$repo_url = $_GET['repo_url'];
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
if ($repo_url == '') {
echo "<html><body>";
echo "Give me a repo_url: \n";
echo curPageURL() . "?repo_url=http://gitbut/username/repository.git\n";
echo "<form>";
echo "<input type='text' name='repo_url'>";
echo "<br>";
echo "<input type='submit' value='Git clone & Co.'>";
echo "</form>";
echo "</body></html>";
} elseif(file_exists(".git")) {
header("Content-Type: text/plain");
echo "Allready installed\n";
} else {
header("Content-Type: text/plain");
echo "git clone $repo_url $dest\n";
echo `git clone $repo_url $dest 2>&1`;
if (file_exists("index.html")) {
echo "rm index.html\n";
unlink("index.html");
}
echo "mv $dest/* $dest/.* $base/\n";
echo `mv $dest/* $dest/.* $base/`;
echo "rmdir $dest\n";
echo `rmdir $dest`;
echo "rm $filename\n";
echo `rm $filename`;
$hooker = 'hooker-' . generateRandomString(41) . '.php';
echo "Use this URL in repository hook:\n";
echo 'http://' . $_SERVER["SERVER_NAME"] . '/' . $hooker;
$content = '<?php' . "\n";
$content .= 'header("Content-Type: text/plain");' . "\n";
$content .= 'echo `/usr/bin/git pull origin master 2>&1`;' . "\n";
$content .= '?>';
file_put_contents($hooker, $content);
}
?>