Skip to content

Instantly share code, notes, and snippets.

@samdark
Last active October 11, 2019 10:05
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save samdark/3752870 to your computer and use it in GitHub Desktop.
Save samdark/3752870 to your computer and use it in GitHub Desktop.
Simple GitHub post-receive PHP hook
<?php
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;
}
$github_ips = array('207.97.227.253', '50.57.128.197', '108.171.174.178', '50.57.231.61');
$github_cidrs = array('204.232.175.64/27', '192.30.252.0/22');
if(in_array($_SERVER['REMOTE_ADDR'], $github_ips) || cidr_match($_SERVER['REMOTE_ADDR'], $github_cidrs)) {
$dir = '/var/www/path/to/your/git/root';
exec("cd $dir && git pull 2>&1", $output);
echo $output;
}
else {
header('HTTP/1.1 404 Not Found');
echo '404 Not Found.';
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment