go.php 1.47 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__ . '/vendor/autoload.php';
17 18
require_once __DIR__ . '/config.php';

19 20 21 22 23
define('PODUPTIME', microtime(true));

// Set up global DB connection.
R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true);
R::testConnection() || die('Error in DB connection');
David Morley's avatar
David Morley committed
24
R::usePartialBeans(true);
25 26

try {
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
    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);
    }
52 53

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