// Mirror-List:
//
// Takes three arguments:
//
// release (mandatory) = single digit indicating the OS version. 4 means CentOS4, 5 means CentOS5, etc.
//
// arch (mandatory) = Architecture. Like i386.
//
// testing (optional) = If specified, returns the URL to the testing repository.
//
// Returns list of mirrors
//
// GET /mirror.php?release=4&arch=i386
// Find out if user agent is YUM or something else (browser):
if (preg_match("/yum/i", $_SERVER['HTTP_USER_AGENT'])) {
$is_yum = "1";
}
else {
$is_yum = "0";
}
$InstalledFQDN = 'updates.blueonyx.it';
$release = $_GET["release"];
$arch = $_GET["arch"];
$testing = $_GET["testing"];
$os = $_GET["os"];
// Start -- Piwik Tracking API init --
require_once "/home/sites/updates.blueonyx.it/web/PiwikTracker.php";
PiwikTracker::$URL = 'http://stats.blueonyx.it/';
$remote = $_SERVER['REMOTE_ADDR'];
$myref = $_SERVER['HTTP_REFERER'];
$token_auth="52c5530e13172a4109ff40adca5315c6";
$piwikTracker = new PiwikTracker( $idSite=2, 'http://stats.blueonyx.it/' );
$piwikTracker->setTokenAuth( '52c5530e13172a4109ff40adca5315c6' );
if ($release == "5") {
$uagent = "(5106R)";
$pf = "5106R";
}
elseif ($release == "6.0") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6.1") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6.2") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6.3") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6.4") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6.5") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6.6") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6.7") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6.8") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6.9") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6.10") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6.11") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6.12") {
$uagent = "(5107R)";
$pf = "5107R";
}
elseif ($release == "6Server") {
$uagent = "(5107R)";
$pf = "5107R";
}
else {
$uagent = "(unknown)";
}
if (($arch != "i386") && ($release != "5")) {
$uagent = "(5108R)";
$pf = "5108R";
}
$piwikTracker->doTrackPageView("YUM Update - $uagent");
$piwikTracker->setUrl( 'http://updates.blueonyx.it/mirror.php' );
$piwikTracker->disableCookieSupport();
if ($is_yum == "1") { $piwikTracker->setUserAgent( "YUM" ); }
$piwikTracker->setIp( $remote );
$piwikTracker->setUrlReferer($myref);
//$useragent = "YUM - (" . $arch . " - " . $remote . " - " . $myref . ")";
$useragent = "YUM - (" . $arch . ")";
$piwikTracker->setUserAgent($useragent);
$piwikTracker->setForceVisitDateTime( 'now' );
// End -- Piwik Tracking API init --
$PRODUCTDIR = "blueonyx";
$user="bxser";
$password="audi#4712";
$database="bxdist";
$Debug = "0";
$today = date("Ymd");
// Get IP:
$ip=$_SERVER['REMOTE_ADDR'];
//------------------
// Get Master repos timestamp:
$file = get_url("http://devel.blueonyx.it/pub/.timestamp");
if (!$file) {
$masterTS = "";
}
else {
$masterTS = $file;
}
//------------------
if (!$release || !$arch) {
// Handle Errors:
echo "Error:
";
echo "You must specifiy the release AND the architecture
";
}
else {
// Sent header to text/plain:
header("Content-type: text/plain");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0");
header("Pragma: no-cache");
// Determine if we serve the 'bluequartz' or the 'testing' repository:
if ($testing == "testing") {
$branch = "testing";
}
else {
$branch = $PRODUCTDIR;
if (($branch != "blueonyx") && ($branch != "testing")) {
echo "Unsupported request: $branch";
exit;
}
}
if (($arch != "i386") && ($arch != "x86_64")) {
echo "Unsupported request: $arch";
exit;
}
if (($os == "scientific") || ($os == "sl-updates" )){
$product_dir = "scientific";
}
else {
$product_dir = "BlueOnyx";
}
// Based on $release and $arch we complete the path:
if ($os == "scientific") {
$url = "$release/";
$sl_filler = "os/";
}
elseif ($os == "sl-updates") {
$url = "$release/";
$sl_filler = "updates/security/";
}
elseif ($release == "3") {
$url = "5100R/CentOS3/$branch/";
}
elseif ($release == "4") {
$url = "5100R/CentOS4/$branch/";
}
elseif ($release == "5") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "5.1") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "5.2") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "5.3") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "5.4") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "5.5") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "5.6") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "5.7") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "5.8") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "5.9") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "5.10") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "5.11") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "5.12") {
$url = "5106R/CentOS5/$branch/";
}
elseif ($release == "6.0") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6.1") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6.2") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6.3") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6.4") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6.5") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6.6") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6.7") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6.8") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6.9") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6.10") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6.11") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6.12") {
$url = "5106R/CentOS6/$branch/";
}
elseif ($release == "6Server") {
$url = "5106R/CentOS6/$branch/";
}
//
//-- Start: Handle IP blocks:
//
// Open the file 'blocked_ips.txt' that contains the list of blocked IPs:
$blocked_ips = array();
$fp = fopen ( "/home/sites/$InstalledFQDN/web/blocked_ips.txt", 'r' );
while ( ! feof ( $fp ) ) {
$IPinhalt = fgets ( $fp, 300 );
if ($IPinhalt != "") {
trim($IPinhalt);
$blocked_ips[] = chop($IPinhalt);
}
}
pclose ( $fp );
// Get User-IP:
$userip = get_client_ip();
// Check if the visitor IP is within the range of blocked IPs:
$ip_is_blocked = "0";
foreach ($blocked_ips as $key => $ip_value) {
if ($ip_value != "") {
$ipcheck = ip_in_range($userip, $ip_value);
if ($ipcheck == "1") {
$ip_is_blocked = "1";
}
}
}
if ($ip_is_blocked == "1") {
// Redirect to mirror for bad boys:
echo "http://blacklist.blueonyx.it/tos-violator-repo/" . "\n";
}
else {
// Open the file 'mirrors.txt' that contains the list of mirrors:
$fp = fopen ( 'mirrors.txt', 'r' );
while ( ! feof ( $fp ) ) {
$inhalt = fgets ( $fp, 300 );
if ($inhalt != "") {
trim($inhalt);
$inh = chop($inhalt);
// mirror.blueonyx.it is not carrying the SL repos due to HD space restrictions!
if (($inh != 'http://mirror.blueonyx.de/pub/') || ($product_dir != "scientific")) {
$checkmirrorTS = $inh . ".timestamp";
$file = get_url($checkmirrorTS);
if ($masterTS != "") {
if ($file == $masterTS) {
echo chop($inhalt) . $product_dir . "/" . $url . $arch . "/" . $sl_filler . "\n";
}
}
else {
echo chop($inhalt) . $product_dir . "/" . $url . $arch . "/" . $sl_filler . "\n";
}
}
}
}
pclose ( $fp );
}
}
// -- Handle MySQL storage of vistor info:
// Fiddle date string together:
$date_pieces = explode("/", $date);
$year = $date_pieces[0];
$month = $date_pieces[1];
$day = $date_pieces[2];
if ($Debug == "1") {
echo "IP: $ip \n";
echo "YUM: $is_yum \n";
echo "Date: $today \n";
}
// Do we have all we need?
$good_data = "0";
if (($ip != "") && ($release != "") && ($is_yum == "1") && ($pf != "")) {
$good_data = "1";
}
if ($good_data == "1") {
$found_already = "0";
$how_often = "0";
$foundnum = "0";
mysql_connect("sql.blueonyx.it",$user,$password);
mysql_select_db($database) or die('Select DB failed: ' . mysql_error());
$query = "SELECT `ID`, `RELEASE`, `IP`, `AUTODATE` FROM `bxdata`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
//echo "ID : {$row[0]} \n" .
"RELEASE : {$row[1]} \n" .
"IP : {$row[2]} \n" .
"AUTODATE : {$row[3]} \n";
// Compare by IP address:
if ($row[2] == $ip) {
// OK, we found an entry in the database for this IP address already.
$found_already = "1";
}
}
if ($found_already == "0") {
if ($Debug > "0") {
echo "Doing DB insert \n";
}
// Normal behaviour. Entry not present in database yet, submit it:
$query = "INSERT INTO `bxdata` (`ID`, `RELEASE`, `IP`, `AUTODATE`) VALUES ('','$pf','$ip','$today')\n";
$result=mysql_query($query) or die('Query failed: ' . mysql_error());
mysql_close();
}
if ($found_already == "1") {
if ($Debug > "0") {
echo "Doing DB update
\n";
}
// Update Autodate field and other fields for existing database entry - but for this IP only:
$query = "UPDATE `$database`.`bxdata` SET
`AUTODATE` = '$today',
`RELEASE` = '$pf',
`IP` = '$ip'
WHERE `bxdata`.`IP` ='$ip' LIMIT 1;\n";
$result=mysql_query($query) or die('Query failed: ' . mysql_error());
mysql_close();
}
}
function get_url($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,"3");
curl_setopt($ch, CURLOPT_USERAGENT, 'BlueOnyx Mirror Sync Check');
$error = curl_error($ch);
$data = curl_exec($ch);
if($data === false) {
$data = $error;
}
curl_close($ch);
return $data;
}
// ip_in_range
// This function takes 2 arguments, an IP address and a "range" in several
// different formats.
// Network ranges can be specified as:
// 1. Wildcard format: 1.2.3.*
// 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0
// 3. Start-End IP format: 1.2.3.0-1.2.3.255
// The function will return true if the supplied IP is within the range.
// Note little validation is done on the range inputs - it expects you to
// use one of the above 3 formats.
function ip_in_range($ip, $range) {
if (strpos($range, '/') !== false) {
// $range is in IP/NETMASK format
list($range, $netmask) = explode('/', $range, 2);
if (strpos($netmask, '.') !== false) {
// $netmask is a 255.255.0.0 format
$netmask = str_replace('*', '0', $netmask);
$netmask_dec = ip2long($netmask);
return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
} else {
// $netmask is a CIDR size block
// fix the range argument
$x = explode('.', $range);
while(count($x)<4) $x[] = '0';
list($a,$b,$c,$d) = $x;
$range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
$range_dec = ip2long($range);
$ip_dec = ip2long($ip);
# Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
#$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));
# Strategy 2 - Use math to create it
$wildcard_dec = pow(2, (32-$netmask)) - 1;
$netmask_dec = ~ $wildcard_dec;
return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
}
} else {
// range might be 255.255.*.* or 1.2.3.0-1.2.3.255
if (strpos($range, '*') !==false) { // a.b.*.* format
// Just convert to A-B format by setting * to 0 for A and 255 for B
$lower = str_replace('*', '0', $range);
$upper = str_replace('*', '255', $range);
$range = "$lower-$upper";
}
if (strpos($range, '-')!==false) { // A-B format
list($lower, $upper) = explode('-', $range, 2);
$lower_dec = (float)sprintf("%u",ip2long($lower));
$upper_dec = (float)sprintf("%u",ip2long($upper));
$ip_dec = (float)sprintf("%u",ip2long($ip));
return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
}
echo 'Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format';
return false;
}
}
// Function to get the client IP address
function get_client_ip() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
?>