go.php 1.75 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);

dmorley's avatar
dmorley committed
9
use Carbon\Carbon;
David Morley's avatar
David Morley committed
10
use Jaybizzle\CrawlerDetect\CrawlerDetect;
11
use RedBeanPHP\R;
dmorley's avatar
dmorley committed
12
use Poduptime\PodStatus;
noplanman's avatar
noplanman committed
13

14
// Other parameters.
dmorley's avatar
dmorley committed
15 16
$_domain   = $_GET['domain'] ?? '';
$_software = $_GET['software'] ?? '';
17

noplanman's avatar
noplanman committed
18
require_once __DIR__ . '/boot.php';
noplanman's avatar
noplanman committed
19 20

try {
21 22 23 24
    if ($_domain) {
        $click  = 'manualclick';
        $domain = R::getCell('SELECT domain FROM pods WHERE domain LIKE ?', [$_domain]);
        $domain || die('unknown domain');
dmorley's avatar
dmorley committed
25 26 27 28 29 30 31 32 33
    } elseif ($_software) {
        $click  = 'autoclick';
        $domain = R::getCell('
            SELECT domain
            FROM pods
            WHERE signup
                AND uptime_alltime > 95
                AND monthsmonitored > 1
                AND softwarename = ?
dmorley's avatar
dmorley committed
34
                AND status = ?
dmorley's avatar
dmorley committed
35 36
            ORDER BY random()
            LIMIT 1
dmorley's avatar
dmorley committed
37
        ', [$_software, PodStatus::UP]);
dmorley's avatar
dmorley committed
38
        $domain || die('no domains exist');
39 40 41 42 43 44 45 46 47
    } else {
        $click  = 'autoclick';
        $domain = R::getCell('
            SELECT domain
            FROM pods
            WHERE signup
                AND uptime_alltime > 96
                AND monthsmonitored > 2
                AND pods.masterversion = shortversion
dmorley's avatar
dmorley committed
48
                AND status = ?
49 50
            ORDER BY random()
            LIMIT 1
dmorley's avatar
dmorley committed
51
        ', [PodStatus::UP]);
52 53 54 55 56 57 58 59 60
        $domain || die('no domains exist');
    }

    $c           = R::dispense('clicks');
    $c['domain'] = $domain;
    $c[$click]   = 1;
    if (!(new CrawlerDetect())->isCrawler()) {
        R::store($c);
    }
dmorley's avatar
dmorley committed
61 62

    setcookie('domain', $domain, Carbon::now()->addCentury()->timestamp);
63
    header('Location: https://' . $domain);
noplanman's avatar
noplanman committed
64
} catch (\RedBeanPHP\RedException $e) {
65
    die('Error in SQL query: ' . $e->getMessage());
66
}