gettoken.php 2.33 KB
Newer Older
dmorley's avatar
dmorley committed
1
<?php
dmorley's avatar
cleanup  
dmorley committed
2
$systemTimeZone = system('date +%Z');
dmorley's avatar
dmorley committed
3 4
if (!$_POST['domain']){
  echo "no pod domain given";
dmorley's avatar
cleanup  
dmorley committed
5
  die;
dmorley's avatar
dmorley committed
6 7
}
$domain = $_POST['domain'];
8 9 10

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

dmorley's avatar
cleanup  
dmorley committed
11 12 13 14 15 16 17 18 19
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
if (!$dbh) {
  die("Error in connection: " . pg_last_error());
}
$sql = "SELECT email FROM pods WHERE domain = '$domain'";
$result = pg_query($dbh, $sql);
if (!$result) {
  die("Error in SQL query: " . pg_last_error());
}
dmorley's avatar
dmorley committed
20 21 22 23
$rows = pg_num_rows($result);
if ($rows <= 0) {
echo "domain not found";die;
}
dmorley's avatar
cleanup  
dmorley committed
24 25 26 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 52 53 54 55 56 57 58
while ($row = pg_fetch_array($result)) {
  if ($_POST['email']){
    if ($row["email"] <> $_POST['email']) {
      echo "email not a match";die;
    }
    $uuid = md5(uniqid($domain, true));
    $expire = date("Y-m-d H:i:s", time() + 2700);
    $sql = "UPDATE pods SET token=$1, tokenexpire=$2 WHERE domain = '$domain'";
    $result = pg_query_params($dbh, $sql, array($uuid,$expire));
    if (!$result) {
      die("Error in SQL query: " . pg_last_error());
    }
    $to = $_POST["email"];
    $subject = "Temporary edit key for podupti.me";
    $message = "Link: https://podupti.me/db/edit.php?domain=" . $_POST["domain"] . "&token=" . $uuid . " Expires: " . $expire . " " . $systemTimeZone ."\n\n";
    $headers = "From: support@diasp.org\r\nBcc: support@diasp.org\r\n";
    @mail( $to, $subject, $message, $headers );    
    echo "Link sent to your email";
    } elseif (!$_POST['email']){
    $uuid = md5(uniqid($domain, true));
    $expire = date("Y-m-d H:i:s", time() + 9700);
    $sql = "UPDATE pods SET token=$1, tokenexpire=$2 WHERE domain = '$domain'";
    $result = pg_query_params($dbh, $sql, array($uuid,$expire));
    if (!$result) {
      die("Error in SQL query: " . pg_last_error());
    }
    $to = "support@diasp.org";
    $subject = "FORWARD REQUEST: Temporary edit key for podupti.me";
    $message = "User trying to edit pod without email address. Email found: " . $row["email"] . " Link: https://podupti.me/db/edit.php?domain=" . $_POST["domain"] . "&token=" . $uuid . " Expires: " . $expire . " " . $systemTimeZone ."\n\n";
    $headers = "From: support@diasp.org\r\nBcc: support@diasp.org\r\n";
    @mail( $to, $subject, $message, $headers );
    echo "Link sent to administrator to review and verify, if approved they will forward the edit key to you.";
  }
  pg_free_result($result);
  pg_close($dbh);
59
}
dmorley's avatar
dmorley committed
60 61

?>