/
posts.php
151 lines (107 loc) · 4.35 KB
/
posts.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Database Tables: posts
* Table <posts>: id (int, auto_increment),
* topic_id (int), author_id (int),
* message (text), rating (int), created (str),
* flags (str), edited (str)
*/
require_once 'config.php';
require_once 'core.php';
require_once 'users.php';
require_once 'topics.php';
databaseConnect();
function postGetById($id)
{
global $database_cfg;
$res = databaseQuery("select * from " . $database_cfg["prefix"] . "posts where id='" . stringEncode($id) . "'", "Post not found");
return $res[0];
}
function postGetByAuthorId($author_id)
{
global $database_cfg;
return databaseQuery("select * from " . $database_cfg["prefix"] . "posts where author_id='" . stringEncode($author_id) . "'");
}
function postGetByTopicId($topic_id)
{
global $database_cfg;
return databaseQuery("select * from " . $database_cfg["prefix"] . "posts where topic_id='" . stringEncode($topic_id) . "'");
}
function postGetByRating($rating)
{
global $database_cfg;
return databaseQuery("select * from " . $database_cfg["prefix"] . "posts where rating='" . stringEncode($rating) . "'");
}
function postSearchByMessage($message)
{
global $database_cfg;
$res = databaseQuery("select * from " . $database_cfg["prefix"] . "posts where locate('" . stringEncode($message) . "', message) > 0");
return $res;
}
function postSearchByAuthorNickname($nickname)
{
global $database_cfg;
$res = databaseQuery("select * from " . $database_cfg["prefix"] . "posts where author_id in (select id from " . $database_cfg["prefix"] . "users where locate('" . stringEncode($nickname) . "', nickname) > 0)");
return $res;
}
function postExists($topic_id, $message)
{
global $database_cfg;
$posts = databaseQuery("select * from " . $database_cfg["prefix"] . "posts where message='" . stringEncode($message) . "' and topic_id='" . intval($topic_id) . "'");
if (is_array($posts) && is_array($posts[0]))
return true; else
return false;
}
function postExistsById($id)
{
$post = postGetById($id);
if (is_array($post) && isset($post['id']))
return true; else
return false;
}
function postCreate($topic_id, $author_id, $message, $flags = array())
{
global $database_cfg;
$errors = array();
if (!userExistsById($author_id))
$errors[] = "User marked as author (" . $author_id . ") not found";
if (!topicExistsById($topic_id))
$errors[] = "Topic marked as parent not found";
if (postExists($topic_id, $message))
$errors[] = "This post already exists in this topic";
if (count($errors) > 0)
return $errors;
$flags_str = "";
if (is_array($flags))
$flags_str = stringAddTokens($flags_str, $flags);
databaseQuery("insert into " . $database_cfg["prefix"] . "posts (topic_id, author_id, message, flags, created) values ('" . intval($topic_id) . "', '" . intval($author_id) . "', '" . stringEncode($message) . "', '" . $flags_str . "', '" . stringEncode(date("H:i, d.m.Y")) . "')");
databaseQuery("update " . $database_cfg["prefix"] . "topics set edited='" . stringEncode(date("H:i, d.m.Y")) . "' where id='" . intval($topic_id) . "'", "Can't update topic");
}
function postRate($post_id, $points)
{
global $database_cfg;
$post = postGetById($post_id);
$rating = intval($post["rating"]) + intval($points);
databaseQuery("update " . $database_cfg["prefix"] . "posts set rating='" . $rating . "' where id='" . intval($post_id) . "'", "Can't update post");
}
function postSetParams($post_id, $params)
{
global $database_cfg;
if (!is_array($params))
return "Wrong parameters type";
$post = postGetById($post_id);
if (isset($params["topic"]))
$post["topic_id"] = intval($params["topic"]);
if (isset($params["message"]))
$post["message"] = stringEncode($params["message"]);
if (isset($params["flags"]))
$post["flags"] = $params["flags"];
databaseQuery("update " . $database_cfg["prefix"] . "posts set topic_id='" . $post['topic_id'] . "', edited='" . stringEncode(date("H:i, d.m.Y")) . "', message='" . $post['message'] . "', flags='" . $post['flags'] . "' where id='" . intval($post_id) . "'", "Can't update post");
}
function postDrop($post_id)
{
global $database_cfg;
if (postExistsById($post_id))
databaseQuery("delete from " . $database_cfg["prefix"] . "posts where id='" . intval($post_id) . "'", "Can't delete post");
}
?>