-
Notifications
You must be signed in to change notification settings - Fork 1
/
github_hook.php
160 lines (139 loc) · 4.06 KB
/
github_hook.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
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
158
159
<?php
/*
* Copyright 2011 Richard Bateman
*
* Licensed under the New BSD License
**/
$cathost = "127.0.0.1";
$catport = 54321;
$debug = true;
$input = $_POST["payload"];
$value = json_decode($input);
if ($debug) {
$f = fopen("/tmp/github_hook.log", "w");
fwrite($f, $input);
fclose($f);
}
if (isset($value->commits)) {
foreach ($value->commits as $commit) {
$url = new goo_gl($commit->url);
$hash = substr($commit->id, 0, 7);
if (isset($value->ref)) {
$pos = strrpos($value->ref, "/");
if ($pos === false)
$branch = $value->ref;
else
$branch = substr($value->ref, $pos+1);
} else {
$branch = "(not specified)";
}
$msg = substr($commit->message, 0, 60) . (strlen($commit->message) >= 60 ? "..." : "");
$message = "Commit $hash on $branch by ". $commit->author->name . ": \"$msg\"";
$message .= " " . $url->result();
sendToCat($message . "\n");
}
}
function sendToCat($message) {
global $cathost, $catport;
echo "Trying to connect to $cathost : $catport";
$fs = fsockopen($cathost, $catport);
fwrite($fs, $message . "\n");
fclose($fs);
}
/*
API Google URL Shortner - goo.gl
Marcus Nunes - marcusnunes.com - 9/18/2010
eg:
$googl = new goo_gl('http://marcusnunes.com');
echo $googl->result();
*/
class goo_gl{
var $url, $resul;
//goo.gl construct method
function goo_gl($url){
$this->url = $url;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://goo.gl/api/url');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'user=toolbar@google.com&url='.urlencode($this->url).'&auth_token='.$this->googlToken($url));
$saida = curl_exec($curl);
curl_close($curl);
if($saida){
$json = json_decode($saida);
$this->resul = $json->short_url;
}
}
//show url shorted by goo.gl
function result(){
return $this->resul;
}
//token code
function googlToken($b){
$i = $this->tke($b);
$i = $i >> 2 & 1073741823;
$i = $i >> 4 & 67108800 | $i & 63;
$i = $i >> 4 & 4193280 | $i & 1023;
$i = $i >> 4 & 245760 | $i & 16383;
$j = "7";
$h = $this->tkf($b);
$k = ($i >> 2 & 15) << 4 | $h & 15;
$k |= ($i >> 6 & 15) << 12 | ($h >> 8 & 15) << 8;
$k |= ($i >> 10 & 15) << 20 | ($h >> 16 & 15) << 16;
$k |= ($i >> 14 & 15) << 28 | ($h >> 24 & 15) << 24;
$j .= $this->tkd($k);
return $j;
}
function tkc(){
$l = 0;
foreach(func_get_args() as $val){
$val &= 4294967295;
$val += $val > 2147483647 ? -4294967296 : ($val < -2147483647 ? 4294967296 : 0);
$l += $val;
$l += $l > 2147483647 ? -4294967296 : ($l < -2147483647 ? 4294967296 : 0);
}
return $l;
}
function tkd($l){
$l = $l > 0 ? $l : $l + 4294967296;
$m = "$l"; //deve ser uma string
$o = 0;
$n = false;
for($p = strlen($m) - 1; $p >= 0; --$p){
$q = $m[$p];
if($n){
$q *= 2;
$o += floor($q / 10) + $q % 10;
} else {
$o += $q;
}
$n = !$n;
}
$m = $o % 10;
$o = 0;
if($m != 0){
$o = 10 - $m;
if(strlen($l) % 2 == 1){
if ($o % 2 == 1){
$o += 9;
}
$o /= 2;
}
}
return "$o$l";
}
function tke($l){
$m = 5381;
for($o = 0; $o < strlen($l); $o++){
$m = $this->tkc($m << 5, $m, ord($l[$o]));
}
return $m;
}
function tkf($l){
$m = 0;
for($o = 0; $o < strlen($l); $o++){
$m = $this->tkc(ord($l[$o]), $m << 6, $m << 16, -$m);
}
return $m;
}
}