aboutsummaryrefslogtreecommitdiff
blob: 357e25d10289b7fbb45ac43532355b8aa08c7c7f (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
 *  Main handler.
 *  @package mirror
 *	@subpackage pub
 */
ob_start();
require_once('./cfg/config.php');  // config file that defines constants

// if we don't have an os, make it windows, playing the odds
if (empty($_GET['os'])) {
    $_GET['os'] = 'Any';
}
try{
// do we even have an os or product?
if (!empty($_GET['os'])&&!empty($_GET['product'])) {
    require_once(LIB.'/db.php');  // core mysql wrappers
    DB::connect(DBHOST,DBUSER,DBPASS,DBNAME);  // open persistent connection to db

    // clean in os and product strings
    $os_name = trim(strtolower($_GET['os']));
    $product_name = trim(strtolower($_GET['product']));

    // get os and product IDs
    $os_id = DB::name_to_id('mirror_os','os_id','os_name',$os_name);
    $product_id = DB::name_to_id('mirror_products','product_id','product_name',$product_name);

    // From pure HTTP request, you might get upgraded
    // From HTTPS request, you should NOT be downgraded.
    $baseurl_prefix = $_SERVER['HTTPS'] === 'on' ? 'https%' : 'http%';

    // do we have a valid os and product?
    if (!empty($os_id)&&!empty($product_id)) {
        $location = DB::get_one("SELECT location_id, location_path FROM mirror_locations WHERE product_id=:product_id AND os_id=:os_id",
                                PDO::FETCH_ASSOC,
                                array(':product_id' => $product_id, ':os_id' => $os_id));

        // did we get a valid location?
        if (!empty($location)) {
			$mirror = DB::get_one("SELECT
				mirror_mirrors.mirror_id, mirror_baseurl
                FROM mirror_mirrors
                JOIN mirror_location_mirror_map ON mirror_mirrors.mirror_id = mirror_location_mirror_map.mirror_id
                WHERE
                    mirror_location_mirror_map.location_id = :location_id
                    AND mirror_active='1'
                    AND location_active ='1'
                    AND mirror_baseurl LIKE :baseurl_prefix
                ORDER BY
                    rand()*(1.0/mirror_rating)",
                PDO::FETCH_ASSOC,
                array(
                    ':location_id' => $location['location_id'],
                    ':baseurl_prefix' => $baseurl_prefix,
                ));

            // did we get a valid mirror?
            if (!empty($mirror)) {

                // if logging is enabled, insert log
                if (LOGGING) {
                    DB::query("UPDATE mirror_mirrors SET mirror_count=mirror_count+1 WHERE mirror_id=?", [$mirror['mirror_id']]);
                    DB::query("UPDATE mirror_products SET product_count=product_count+1 WHERE product_id=?", [$product_id]);
                }

                // LANGUAGE HACK
                if (!empty($_GET['lang'])) {
                    //// $location['location_path'] = str_replace('x86',$_GET['lang'],$location['location_path']);
                    $location['location_path'] = str_replace('en-US',$_GET['lang'],$location['location_path']);
                }

                // BitTorrent HACK - robbat2
                if (!empty($_GET['extra'])) {
                    $extra = $_GET['extra'];
                    $location['location_path'] .= preg_replace('/\?.*|&.*/','',$extra);
                }

                // if we are just testing, then just print and exit.
                if (!empty($_GET['print'])) {
                    print('Location: '.$mirror['mirror_baseurl'].$location['location_path']);
                    exit;
                }

                // otherwise, by default, redirect them and exit
                header('Location: '.$mirror['mirror_baseurl'].$location['location_path']);
                exit;
            }
        }
    }
}
}
catch (Exception $ex) {
        header("Status: 500", true, 500);
        echo "An unexpected error has occurred.";
        trigger_error($ex->getMessage() . ' ' . $ex->getTraceAsString(), E_USER_WARNING);
	exit();
}


// if we get here, the request was invalid; redirect to Gentoo home
header('Location: https://www.gentoo.org/');