-
Notifications
You must be signed in to change notification settings - Fork 1
/
add.php
75 lines (55 loc) · 1.78 KB
/
add.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
<?php
$_TITLE = 'New post';
require('partials/header.php');
if(!$logged_in) {
header('Location: index.php');
}
if(!empty($_POST['submit'])) {
// Begin Register sequence
// Various checks
$title = htmlspecialchars($_POST['title']);
$text = htmlspecialchars($_POST['text']);
$err = [];
if(empty($title) || strlen($title) < 3 || strlen($title) > 255)
$err[] = 'Please input a valid title<br/>';
if(empty($text) || strlen($text) < 8 || strlen($text > 5000))
$err[] = 'Please input a valid text<br/>';
if(empty($err)) {
// Proceed to add text
$uuid = uniqid();
$uid = intval($_SESSION['sess_userid']);
$data = [
'title' => $title,
'content' => $text,
'poster' => $uid,
'uuid' => $uuid
];
$sql = "INSERT INTO posts (title, content, poster, post_date, uuid) VALUES (:title, :content, :poster, now(), :uuid)";
$stmt = $db->prepare($sql);
try {
$stmt->execute($data);
header('Location: view.php?p=' . $uuid);
} catch(PDOException $e) {
print($e->getMessage());
}
} else {
foreach($err as $e) {
print($e);
}
}
}
?>
<h1>New post</h1>
<p><a href="index.php">Return to index</a></p>
<form action="add.php" method="post">
<label>Title (255 chars)</label>
<input type="text" name="title" id="title"/>
<br/>
<label>Text</label>
<textarea name="text" id="text"></textarea>
<br/>
<input type="submit" name="submit" id="submit">
</form>
<?php
require('partials/footer.php');
?>