-
Notifications
You must be signed in to change notification settings - Fork 22
/
post_db.php
129 lines (118 loc) · 5.25 KB
/
post_db.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
require_once 'connect.php';
require_once 'post_func.php';
if (isset($_POST['title'])) $_POST['title'] = trim($_POST['title']);
if (isset($_POST['text'])) $_POST['text'] = trim($_POST['text']);
if ($_POST['method'] == 'get')
{
if (!isset($_POST['topic'])) $_POST['topic'] = '';
if (!isset($_POST['begin'])) $_POST['begin'] = '';
post_get($db, $_POST['topic'], $_POST['begin']); die();
}
else if (($_POST['method'] == 'new') && is_numeric($_POST['topic']))
{
if (!isset($_SESSION['user_id'])) die('Sign in first.');
if ($_POST['topic'] < 0)
{
if (!$_POST['title'])
{
die('Fill in title.');
}
$title_len = mb_strlen($_POST['title'],'UTF8');
if (($title_len < 1) || ($title_len > 200))
{
die('Your title can\'t be longer than 200 characters. Right now your title has '.$title_len .'.');
}
$_POST['title'] = nl2br(htmlspecialchars($_POST['title']));
}
if (!$_POST['text'])
{
die('Fill in content.');
}
$text_len = mb_strlen($_POST['text'],'UTF8');
if (($text_len < 1) || ($text_len > 5000))
{
die('Your post can\'t be longer than 5000 characters. Right now your post has '.$text_len .'.');
}
if (isset($_POST['stick']) || isset($_POST['lock'])) {
if ((!isset($_SESSION['permissions']) || ($_SESSION['permissions'] == 0))) {die('You need to be a moderator to play with sticky or locked topics!');}
else {}
}
$_POST['text'] = nl2br(htmlspecialchars($_POST['text']));
$_POST['text'] = str_replace(" ", " ", $_POST['text']);
if (isset($_POST['stick']) && $_POST['stick'] == 'ystick') {$_POST['stick'] = '1';} elseif (isset($_POST['stick'])) {$_POST['stick'] = '0';} else{}
if (isset($_POST['lock']) && $_POST['lock'] == 'ylock') {$_POST['lock'] = '1';} elseif (isset($_POST['lock'])) {$_POST['lock'] = '0';} else{}
if ($_POST['topic'] < 0)
{
if (isset($_POST['stick']) && isset($_POST['lock'])) {
$query = $db->prepare("INSERT INTO topics(topic_title,topic_text,topic_date,topic_by,topic_score,sticky,locked) VALUES(?,?,NOW(),?,UNIX_TIMESTAMP((NOW())),?,?)");
$query->execute(array($_POST['title'], $_POST['text'], $_SESSION['user_id'],$_POST['stick'],$_POST['lock']));
if($query->rowCount() < 1)
{
die('Cannot create topic.');
}
$_POST['topic'] = $db->lastInsertId();
}
elseif (isset($_POST['stick']) && !isset($_POST['lock'])) {
$query = $db->prepare("INSERT INTO topics(topic_title,topic_text,topic_date,topic_by,topic_score,sticky,locked) VALUES(?,?,NOW(),?,UNIX_TIMESTAMP((NOW())),?,0)");
$query->execute(array($_POST['title'], $_POST['text'], $_SESSION['user_id'],$_POST['stick']));
if($query->rowCount() < 1)
{
die('Cannot create topic.');
}
$_POST['topic'] = $db->lastInsertId();
}
elseif (!isset($_POST['stick']) && isset($_POST['lock'])) {
$query = $db->prepare("INSERT INTO topics(topic_title,topic_text,topic_date,topic_by,topic_score,sticky,locked) VALUES(?,?,NOW(),?,UNIX_TIMESTAMP((NOW())),0,?)");
$query->execute(array($_POST['title'], $_POST['text'], $_SESSION['user_id'],$_POST['lock']));
if($query->rowCount() < 1)
{
die('Cannot create topic.');
}
$_POST['topic'] = $db->lastInsertId();
}
else {
$query = $db->prepare("INSERT INTO topics(topic_title,topic_text,topic_date,topic_by,topic_score,sticky,locked) VALUES(?,?,NOW(),?,UNIX_TIMESTAMP((NOW())),0,0)");
$query->execute(array($_POST['title'], $_POST['text'], $_SESSION['user_id']));
if($query->rowCount() < 1)
{
die('Cannot create topic.');
}
$_POST['topic'] = $db->lastInsertId();
}
}
else
{
if (isset($_SESSION['permissions']) && ($_SESSION['permissions'] == '1')) {
if (isset($_POST['stick'])) {
$query = $db->prepare("UPDATE topics SET sticky = ? WHERE topic_id = ?");
$query->execute(array($_POST['stick'],$_POST['topic']));
}
if (isset($_POST['lock'])) {
$query = $db->prepare("UPDATE topics SET locked = ? WHERE topic_id = ?"); //max(((topic_score + NOW() )/2),(NOW() - 3600))
$query->execute(array($_POST['lock'],$_POST['topic']));
}
}
// can't post in locked topics
$query = $db->prepare('SELECT locked FROM topics WHERE topic_id = ?');
$query->execute(array($_POST['topic']));
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row && $row['locked'] == 1)
{
if ((!isset($_SESSION['permissions']) || ($_SESSION['permissions'] == 0))) die("You can not post in a locked topic unless you are a moderator!");
}
//
$query = $db->prepare("INSERT INTO posts(post_text,post_date,post_by,post_topic) VALUES(?,NOW(),?,?)");
$query->execute(array($_POST['text'], $_SESSION['user_id'], $_POST['topic']));
if($query->rowCount() < 1)
{
die('Cannot reply.');
}
$query = $db->prepare("UPDATE topics SET topic_replies = topic_replies + 1 WHERE topic_id = ?");
$query->execute(array($_POST['topic']));
$query = $db->prepare("UPDATE topics SET topic_score = UNIX_TIMESTAMP(NOW()) WHERE topic_id = ?"); //max(((topic_score + NOW() )/2),(NOW() - 3600))
$query->execute(array($_POST['topic']));
}
die('SUCCESS'.$_POST['topic']);
}
?>