function gu_user_is_in_university_network() { // you may have to use HTTP_X_FORWARDED_FOR here dependening // on your PHP and proxy setup $client_ip = $_SERVER['REMOTE_ADDR']; $block = '141.2.0.0/16'; // Goethe University Frankfurt return cidr_match($client_ip, $block); }
function in_ip_range($range, $ip = false) { //Get Client IP if ($ip == false and isset($_SERVER["REMOTE_ADDR"])) { $ip = $_SERVER["REMOTE_ADDR"]; } //Check Client IP if (cidr_match($ip, $range)) { return true; } return false; }
function binnen_school() { // testing? if (config('SHOWHIDE_STUDENTNAMES') > 0) { return true; } else { if (config('SHOWHIDE_STUDENTNAMES') < 0) { return false; } } foreach (config('INTERNAL_IPS') as $range) { if (cidr_match($_SERVER['REMOTE_ADDR'], $range)) { return true; } } return false; }
/** * a helper function for test_ip * @param string $ip the ip address of most any form to test * @param string $match the ip to perform a matching truth test * @return bool return true/false depends on if the first ip match the second ip */ function ip_match($ip, $match) { if (ip2long($match)) { //is an actual IP if ($ip == $match) { return true; } } else { //is something weird if (strpos($match, '/')) { //if it's a cidr notation if (cidr_match($ip, $match)) { return true; } } else { //is a random string (let's say a host name) $match = gethostbyname($match); if ($ip == $match) { return true; } else { return false; } } } return false; }
<?php // github-post-receive.php 20130810 // https://gist.github.com/samdark/3752870 error_log(var_export($_REQUEST, true)); 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)) { exec("git pull"); exec("php buildindex.php"); echo 'Done.'; } else { header('HTTP/1.1 404 Not Found'); echo '404 Not Found.'; exit; }
<?php //Check that the requests originates from Github //IP addresses are derived from Github's Web Hook. //IF THIS BREAKS: check the public IPs for the hooks //on GH! $server = $_SERVER['REMOTE_ADDR']; //From: http://stackoverflow.com/questions/594112/matching-an-ip-to-a-cidr-mask-in-php5 function cidr_match($ip, $range) { list($subnet, $bits) = explode('/', $range); $ip = ip2long($ip); $subnet = ip2long($subnet); $mask = -1 << 32 - $bits; $subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned return ($ip & $mask) == $subnet; } if (cidr_match($server, '204.232.175.64/27') || cidr_match($server, '192.30.252.0/22')) { require_once 'issues.php'; fetch_issues('open'); fetch_issues('closed'); } else { //otherwise, it is: Forbidden 403! header(':', true, 403); echo '<h1>403 - Forbidden'; }
$b = new Bourbon(); function cidr_match($ip, $range) { list($subnet, $bits) = explode('/', $range); $ip = ip2long($ip); $subnet = ip2long($subnet); $mask = -1 << 32 - $bits; $subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned return ($ip & $mask) == $subnet; } // Make sure either an authenticated user or GitHub is running this script if (!WEB::_req('POST')) { $b->auth(null); } else { if (!cidr_match($_SERVER['REMOTE_ADDR'], '192.30.252.0/22')) { header('Location: ' . realpath() . '/index.php'); } } /** * GIT DEPLOYMENT SCRIPT * * Used for automatically deploying websites via github or bitbucket, more deets here: * * https://gist.github.com/1809044 */ // The commands $commands = array('echo $PWD', 'whoami', 'git pull', 'git status', 'git submodule sync', 'git submodule update', 'git submodule status', 'mysqladmin -uroot -proot -f drop bourbon', 'mysqladmin -uroot -proot -f create bourbon', 'mysql -uroot -proot bourbon < /srv/cpsc471-bourbon/sql/latest.sql'); // Run the commands for output $output = ''; foreach ($commands as $command) {
<?php include_once 'config.php'; include_once 'util.php'; session_start(); $error = ""; if (!empty($options['whitelist_subnet'])) { $subnet = $options['whitelist_subnet']; $ipaddress = $_SERVER['REMOTE_ADDR']; if (cidr_match($ipaddress, $subnet)) { $_SESSION['Logged_In'] = "yes"; } } if (!empty($options['password'])) { if (isset($_POST['login_password'])) { if ($_POST['login_password'] == $options['password']) { if (!isset($_SESSION['Logged_In'])) { $_SESSION['Logged_In'] = "yes"; } } else { $error = "An error occurred. Password incorrect."; } } if (!isset($_SESSION['Logged_In']) || $_SESSION['Logged_In'] != "yes") { ?> <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="main.css">