-
Notifications
You must be signed in to change notification settings - Fork 3
/
pasteprocess.php
79 lines (72 loc) · 1.94 KB
/
pasteprocess.php
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
<?php
require ("common.php");
if (!empty($_POST)) {
if (empty($_POST['paste'])) {
header("Location: index.php");
die("Please insert text in the paste.");
}
if (empty($_POST['password'])) {
$password = "";
} else {
$password = $_POST['password'];
}
if (empty($_POST['title'])) {
$title = "Untitled";
} else {
$title = $_POST['title'];
}
$username = "guest";
if (isset($_SESSION['user'])) {
$query = "
SELECT
username
FROM
users
WHERE
username = :username
";
try {
$stmt = $db->prepare($query);
$stmt->bindParam(':username', $_SESSION['user']['username']);
$stmt->execute();
}
catch (PDOException $ex) {
die("Failed to run query");
}
$row = $stmt->fetch();
if ($row) {
$username = $row['username'];
}
}
$query = "
INSERT INTO pastes (
username,
password,
content,
timestamp,
title
) VALUES (
:username,
:password,
:content,
:timestamp,
:title
)
";
try {
$time = time();
$stmt = $db->prepare($query);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$stmt->bindParam(':content', $_POST['paste']);
$stmt->bindParam(':timestamp', $time);
$stmt->bindParam(':title', $title);
$result = $stmt->execute();
$id = $db->lastInsertId();
header("Location: paste.php?id=$id");
die("Paste created, redirecting to paste.");
}
catch (PDOException $ex) {
die("Failed to run query");
}
}