summaryrefslogtreecommitdiff
path: root/pool.php
blob: ff26e5b01bbbeea5120261fef5b5ac5c1fa855fd (plain)
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
<?php

require_once "init.php";

require_once BASE . "/lib/mysql.php";
require_once BASE . "/lib/http.php";

$cutoff = 24*60*60;

if (array_key_exists('HTTPS',$_SERVER) && ($_SERVER['HTTPS']=='on'))
  $protocol = 'https';
else
  $protocol = 'http';

$ipv_filter = '';
if (filter_var($_SERVER['SERVER_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
  $ipv_filter .= ' AND `mirror_statuses`.`ipv4`';
if (filter_var($_SERVER['SERVER_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
  $ipv_filter .= ' AND `mirror_statuses`.`ipv6`';

$result = mysql_run_query(
  'SELECT `mirror_statuses`.`url` FROM `mirror_statuses`' .
  ' WHERE `mirror_statuses`.`active`' .
  ' AND `mirror_statuses`.`last_sync` > UNIX_TIMESTAMP(NOW())-' . $cutoff .
  ' AND `mirror_statuses`.`start` > UNIX_TIMESTAMP(NOW())-' . $cutoff .
  ' AND `mirror_statuses`.`protocol` = "' . $protocol . '"' .
  ' AND `mirror_statuses`.`url` NOT LIKE "%.oss/%"' .
  $ipv_filter .
  ' GROUP BY `mirror_statuses`.`url`' .
  ' ORDER BY SHA2(' .
    'CONCAT(' .
      '`mirror_statuses`.`url`,' .
      'NOW(),' .
      '"' . base64_encode(
        $_SERVER['REMOTE_PORT'] . ' ' .
        $_SERVER['REMOTE_ADDR']
      ) . '"' .
    '),' .
    '256' .
  ')' .
  ' LIMIT 1'
);

$mirror = $result -> fetch_assoc();
if (! array_key_exists('url',$mirror))
  die_500('no mirror reachable');

$request_uri = $_SERVER['REQUEST_URI'];

if (substr($request_uri, 0, 1) == '/')
  $request_uri = substr($request_uri, 1);

redirect_temporarily($mirror['url'] . $request_uri);