This repository was archived by the owner on Dec 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdeploy.php
More file actions
157 lines (144 loc) · 5.74 KB
/
deploy.php
File metadata and controls
157 lines (144 loc) · 5.74 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<HEAD><meta charset="utf-8"> </HEAD>
<pre><?php
define("DS", DIRECTORY_SEPARATOR);
function logg($text) {
$t=file_get_contents("log.txt");
if (strlen($t)>"100000") $t= substr($t, 50000);
file_put_contents("log.txt", $t."\n".$text);
};
$salt="ewfu\x00Hjf8()\x04";
if( md5($_GET['pwd'].$salt)!= "..." ||
!(cidr_match($_SERVER['REMOTE_ADDR'], Array("192.30.252.0/22")) ||
$_SERVER['REMOTE_ADDR']== '127.0.0.1') ) die("Not access");
$payload= json_decode($_POST['payload']);
if ($payload->head_commit->author->name=="www-data") die(); // чтобы не зацикливало
$build= intval(file_get_contents("build.txt"));
$build++;
file_put_contents("build.txt", $build);
$branch= preg_replace("/^.*?([^\/]++)$/", "$1", $payload->ref);
if ($_SERVER['REMOTE_ADDR']== '127.0.0.1') {
$path= "c:/Users/ReinRaus/git/HashCodeUserJS/";
} else {
$path= "/home/git/repositories/apachereps/HashCodeUserJS/";
loggedExec("cd $path && git pull && git checkout $branch");
};
$path= str_replace("/", DS, $path);
$addons= getAddons($path);
$joinedFiles= joinFiles($path, $addons);
createForChrome ($path, $joinedFiles);
createForFirefox($path, $joinedFiles);
createForOpera ($path, $joinedFiles);
if ($_SERVER['REMOTE_ADDR']!= '127.0.0.1') {
loggedExec("cd $path && git commit -am \"Automatic build $build\"");
if ($branch!="master") {
loggedExec("cd $path && git push origin $branch && git checkout master && git branch -D $branch");
} else {
loggedExec("cd $path && git push");
}
}
function loggedExec($cmd) {
$s=exec($cmd." 2>&1", $output, $v);
logg(var_export($output, true));
logg($v." ".$s);
}
function getAddons($path) {
$fullPath= filterRegexArray(getFolderList($path."addons"), "/[\\".DS."]addons[\\".DS."][^\\".DS."]+\\.js$/i");
return array_map(function($text){return preg_replace('/^.*?([^\\'.DS.']+)$/', "$1", $text);}, $fullPath);
}
function joinFiles($path, $addons) {
$DS= DS;
$__addons= array_map(function($text){return preg_replace("/\\.js$/i", "", "__".$text);}, $addons);
$result="\nvar __addons=['".implode("', '", $__addons)."'];\n";
$result.= file_get_contents($path."userjsloader.js")."\n\n__addons=[\n\n";
foreach ($addons as $k=>$v) {
$content= file_get_contents("$path${DS}addons$DS$v");
if (mb_detect_encoding($content)=='UTF-8' && substr($content, 0, 3)!="\xef\xbb\xbf") {
$content="\xef\xbb\xbf".$content; // + BOM
file_put_contents("$path${DS}addons$DS$v", $content);
}
$result.= $content.",\n\n";
};
$result.= "]; // end addons\naddonsLoader.initStorage();\naddonsLoader.callEventIterator('beforeInit');\n\n";
$result= preg_replace_callback("/\[DEPLOY:image64\](.*?)\[\/DEPLOY\]/is",
function ($match) {
global $path;
return getDataURL($path.str_replace("/", DS, $match[1]));
}, $result);
$result= preg_replace_callback("/\[DEPLOY:build\](.*?)\[\/DEPLOY\]/is", function($match){
global $build;
return $build;
}, $result);
return $result;
};
function createForChrome($path, $joinedFiles) {
$json= json_decode(file_get_contents($path."chromejson.txt"));
$json->html= "<script>\n".$joinedFiles."\n</script>";
file_put_contents($path."release".DS."ChromeDump.json", json_encode($json));
}
function createForFirefox($path, $joinedFiles) {
$result= file_get_contents($path."userjsheaders.txt")
."\nfunction __extension__wrapper__(){\n"
.$joinedFiles
."\n};\nvar script = document.createElement('script');\n"
."script.innerHTML = __extension__wrapper__.toString().replace(/^.*?\{|\}.*?$/g, '');\n"
."document.getElementsByTagName('head')[0].appendChild(script);\n";
file_put_contents($path.DS."release".DS."FireFox.user.js", $result);
};
function createForOpera($path, $joinedFiles) {
$result= file_get_contents($path.DS."release".DS."FireFox.user.js");
file_put_contents($path.DS."release".DS."Opera.user.js", $result);
}
function getDataURL($imgPath) {
if (preg_match("/(?:^|\/)\.\.(?:$|\/)/", $imgPath)) return "403";
return "data:image/".array_pop(explode(".", $imgPath)).";base64,".
base64_encode(file_get_contents($imgPath));
}
function cidr_match($ip, $ranges) {
$ranges = (array) $ranges;
foreach ($ranges as $range) {
list($subnet, $mask) = explode('/', $range);
if((ip2long($ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
return true;
}
}
return false;
}
function getFolderList($path) {
$result= Array();
if (is_dir($path)) {
if ($dh=opendir($path)) {
while (($f=readdir($dh)) !==false) if ($f!="." && $f!="..") {
$result[]= $path.DS.$f;
if (is_dir($path.DS.$f)) {
$recur= getFolderList($path.DS.$f);
foreach ($recur as $k=>$v) {
$result[]= $v;
};
};
};
};
};
return $result;
};
function filterRegexArray($arr, $regexValue=null, $regexKey=null, $keySafe=false, $operator="&&") {
$result= Array();
foreach ($arr as $k=>$v) {
if ($regexValue) {
$fValue= preg_match($regexValue, $v);
} else {
$fValue= true;
};
if ($regexKey) {
$fKey= preg_match($regexKey, $k);
} else {
$fKey= true;
};
if (eval("return $fValue $operator $fKey;")) if ($keySafe) {
$result[$k]=$v;
} else {
$result[]= $v;
};
};
return $result;
};
?></pre>