go.php 1.36 KB
Newer Older
dmorley's avatar
dmorley committed
1
<?php
2 3 4 5

// Other parameters.
$_url = $_GET['url'] ?? '';

6 7
require_once __DIR__ . '/config.php';

David Morley's avatar
David Morley committed
8
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
9 10
$dbh || die('Error in connection: ' . pg_last_error());

11 12 13 14
if ($_url) {
  $host   = parse_url($_url, PHP_URL_HOST);
  $sql    = 'SELECT domain FROM pods WHERE domain LIKE $1';
  $result = pg_query_params($dbh, $sql, [$host]);
15
  $result || die('Error in SQL query: ' . pg_last_error());
16

17
  $row = pg_fetch_all($result);
18 19
  $row || die('unknown url');

dmorley's avatar
dmorley committed
20 21 22 23
  $sql    = 'INSERT INTO clicks (domain, manualclick) VALUES ($1, $2)';
  $result = pg_query_params($dbh, $sql, [$host, '1']);
  $result || die('Error in SQL query: ' . pg_last_error());
  
24
  header('Location:' . $_url);
25
} else {
dmorley's avatar
dmorley committed
26
  $sql    = 'SELECT secure,domain FROM pods WHERE score > 90 AND masterversion = shortversion AND signup ORDER BY RANDOM() LIMIT 1';
27 28
  $result = pg_query($dbh, $sql);
  $result || die('Error in SQL query: ' . pg_last_error());
29 30

  $row    = pg_fetch_all($result);
31
  $scheme = $row[0]['secure'] === 't' ? 'https://' : 'http://';
dmorley's avatar
dmorley committed
32 33 34 35 36
  
  $sql    = 'INSERT INTO clicks (domain, autoclick) VALUES ($1, $2)';
  $result = pg_query_params($dbh, $sql, [$row[0]['domain'], '1']);
  $result || die('Error in SQL query: ' . pg_last_error());
  
37
  header('Location:' . $scheme . $row[0]['domain'] . '/users/sign_up');
38
}
dmorley's avatar
dmorley committed
39 40
  pg_free_result($result);
  pg_close($dbh);