go.php 1.92 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;
12
use Poduptime\PodStatus;
noplanman's avatar
noplanman committed
13

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

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

dmorley's avatar
dmorley committed
20 21
$hiddensoftwares = c('hidden-softwares');

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

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