forked from theClubhouse-Augusta/southern-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
80 lines (68 loc) · 2.05 KB
/
main.php
File metadata and controls
80 lines (68 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
header("content-type: text/plain");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Headers: "origin, x-requested-with, content-type"');
header('Access-Control-Allow-Methods "GET, POST, OPTIONS"');
$host = getenv("MYSQL_HOST");
$db = getenv("MYSQL_DB");
$user = getenv("MYSQL_USER");
$pass = getenv("MYSQL_PASS");
$charset = 'utf8mb4';
$dsn = "mysql:unix_socket=/cloudsql/$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
/*
if(isset($_GET["addr"])) {
$selectAll = $pdo->query('SELECT * FROM email_addr');
$stmt = $selectAll;
while ($row = $stmt->fetch()) {
echo $row['addr'] . "<br>";
}
} */
if(isset($_POST)) {
$content = trim(file_get_contents("php://input"));
$decoded = json_decode($content, true);
$decoded = $decoded["email"];
$decoded = urldecode($decoded);
echo $decoded;
if(filter_var($decoded, FILTER_VALIDATE_EMAIL)) {
DeDup($decoded);
} else {
echo "That doesn't look like an email address to me!";
}
} else {
echo "there was a problem with the request, please let us know it broke.";
}
function DeDup($msg) {
global $pdo;
$selectAll = $pdo->query('SELECT * FROM email_addr');
$counter = 0;
while ($row = $selectAll->fetch()) {
if($msg === $row['addr']) {
$counter ++;
break;
}
}
if(!$counter > 0 ) {
DbInsert($msg);
} else {
echo "You have already registered!";
}
}
function DbInsert($msg) {
global $pdo;
$sql = "INSERT INTO email_addr(addr) VALUES (?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$msg]);
echo $msg . ", thank you for subscribing to the coolest email list!";
}
?>