summaryrefslogtreecommitdiff
path: root/pool.php
blob: 16da13ae546a8b40d091510a283821d8bdf62945 (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
<?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';

$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/%"' .
  ' 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);