This repository was archived by the owner on Jun 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.php
More file actions
179 lines (143 loc) · 4.51 KB
/
model.php
File metadata and controls
179 lines (143 loc) · 4.51 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
// The database connection object
$conc = mysqli_connect('localhost', 'root', '', 'mysql');
// Fetch the posts
function fetch_posts()
{
global $conc;
$sql = "SELECT * FROM ProjectPosts ORDER BY Time DESC";
$result = mysqli_query($conc, $sql);
$data = [];
while ($row = mysqli_fetch_assoc($result))
$data[] = [$row['Id'], $row['Username'], $row['Title'], $row['Description'], $row['Time']];
return $data;
}
// Create a post
function create_post($user, $title, $description)
{
global $conc;
$date_time = date('Y/m/d H:i:s');
$sql = "INSERT INTO ProjectPosts VALUES (NULL, '$user', '$title', '$description', '$date_time')";
return mysqli_query($conc, $sql);
}
// Verify username and password
function verify_user($username, $password)
{
global $conc;
$sql = "SELECT * FROM ProjectUsers WHERE Username = '$username' AND Password = '$password'";
$result = mysqli_query($conc, $sql);
return (mysqli_num_rows($result) > 0);
}
// Verify if user exists
function verify_user_exists($username)
{
global $conc;
$sql = "SELECT * FROM ProjectUsers WHERE Username = '$username'";
$result = mysqli_query($conc, $sql);
return (mysqli_num_rows($result) > 0);
}
// Sign Up a new user
function signup_user($username, $password, $email)
{
global $conc;
$date = date("Ymd");
$sql = "INSERT INTO ProjectUsers VALUES (NULL, '$username', '$password', '$date', '$email', 'Available')";
return mysqli_query($conc, $sql);
}
// Fetch email and status
function fetch_email_status($username)
{
global $conc;
$sql = "SELECT * FROM ProjectUsers WHERE Username = '$username'";
$result = mysqli_query($conc, $sql);
$data = [];
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row['Email'];
$data[] = $row['Status'];
}
return $data;
}
// Update the profile with new email and status
function update_profile($username, $email, $status)
{
global $conc;
$sql = "UPDATE ProjectUsers SET Email = '$email', Status = '$status' WHERE Username = '$username'";
mysqli_query($conc, $sql);
}
// Create a comment
function create_comment($user, $comment, $postid)
{
global $conc;
$sql = "INSERT INTO ProjectComments VALUES (NULL, '$user', '$comment', '$postid')";
return mysqli_query($conc, $sql);
}
// Fetch the comments for a post
function fetch_comments($postid)
{
global $conc;
$sql = "SELECT * FROM ProjectComments WHERE PostId = '$postid'";
$result = mysqli_query($conc, $sql);
$data = [];
while ($row = mysqli_fetch_assoc($result))
$data[] = [$row['Id'], $row['Username'], $row['Comment'], $row['PostId']];
return $data;
}
// Like a post
function like_post($user, $postid)
{
// Send error if the post is already liked
if (verify_post_liked($user, $postid)) {
return 'post_liked_error';
} else {
global $conc;
$sql = "INSERT INTO ProjectLikes VALUES (NULL, '$user', '$postid')";
return mysqli_query($conc, $sql);
}
}
// Verify if the post is liked
function verify_post_liked($user, $postid)
{
global $conc;
$sql = "SELECT * FROM ProjectLikes WHERE Username = '$user' AND PostId = '$postid'";
$result = mysqli_query($conc, $sql);
return (mysqli_num_rows($result) > 0);
}
// Unsubscribe a user
function unsubscribe_user($user, $block)
{
if (verify_unsubscribe($user, $block)) {
return 'already_unsubscribed';
} else {
global $conc;
$sql = "INSERT INTO ProjectBlockList VALUES (NULL, '$user', '$block')";
return mysqli_query($conc, $sql);
}
}
// Verify if a user is unsubscribed by some other user
function verify_unsubscribe($user, $block)
{
global $conc;
$sql = "SELECT * FROM ProjectBlockList WHERE Username = '$user' AND Blockname = '$block'";
$result = mysqli_query($conc, $sql);
return (mysqli_num_rows($result) > 0);
}
// Delete a post
function delete_post($id)
{
global $conc;
$sql = "DELETE FROM ProjectPosts WHERE Id='$id'";
mysqli_query($conc, $sql);
}
// Fetch the users unsubscribed by a user
function fetch_unsubscribed_users($user)
{
global $conc;
$sql = "SELECT * FROM ProjectBlockList WHERE Username = '$user'";
$result = mysqli_query($conc, $sql);
$data = [];
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row['Blockname'];
}
return $data;
}
?>