go.php 1.2 KB
Newer Older
dmorley's avatar
dmorley committed
1
<?php
2

3 4 5 6 7 8
/**
 * Redirect to a given pod or find a good fit.
 */

declare(strict_types=1);

9
use Carbon\Carbon;
David Morley's avatar
David Morley committed
10
use Jaybizzle\CrawlerDetect\CrawlerDetect;
11
use RedBeanPHP\R;
12

13
// Other parameters.
14
$_domain = $_GET['domain'] ?? '';
15

16
require_once __DIR__ . '/boot.php';
17 18

try {
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
    if ($_domain) {
        $click  = 'manualclick';
        $domain = R::getCell('SELECT domain FROM pods WHERE domain LIKE ?', [$_domain]);
        $domain || die('unknown domain');
    } else {
        $click  = 'autoclick';
        $domain = R::getCell('
            SELECT domain
            FROM pods
            WHERE signup
                AND uptime_alltime > 96
                AND monthsmonitored > 2
                AND pods.masterversion = shortversion
            ORDER BY random()
            LIMIT 1
        ');
        $domain || die('no domains exist');
    }

    $c           = R::dispense('clicks');
    $c['domain'] = $domain;
    $c[$click]   = 1;
    if (!(new CrawlerDetect())->isCrawler()) {
        R::store($c);
    }
44 45

    setcookie('domain', $domain, Carbon::now()->addCentury()->timestamp);
46
    header('Location: https://' . $domain);
47
} catch (\RedBeanPHP\RedException $e) {
48
    die('Error in SQL query: ' . $e->getMessage());
49
}