-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageDAO.php
358 lines (333 loc) · 21.3 KB
/
messageDAO.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
class messageDAO {
private static $db_host = "localhost";
private static $db_user = "PUadmin";
private static $db_passwd = "1234";
private static $db_name = "Project_Unknown";
function __construct() {
session_start();
}
function validate_form($receiver, $writer, $title, $text){
$result = true;
// Secondly validate receiver is exist on friendList
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
$query = "SELECT relation_seq FROM relation WHERE (first_user_email = '".$writer."' AND second_user_email = '".$receiver."') OR (first_user_email = '".$receiver."' AND second_user_email = '".$writer."')";
$result = mysqli_query($db_connect, $query);
$row = mysqli_fetch_array($result);
if(!$row[0]){
$_SESSION['messageException'] = 'receiver';
$result = false;
}
// Thirdly validate title and text
if(strlen($title)>51){
$_SESSION['messageException'] = 'title';
$result = false;
}else if(strlen($text)>210){
$_SESSION['messageException'] = 'text';
$result = false;
}else if(strpos($title, "'") !== false){
$_SESSION['messageException'] = 'titleUnexpected';
$result = false;
}else if(strpos($text, "'") !== false){
$_SESSION['messageException'] = 'textUnexpected';
$result = false;
}else if(strpos($title, '"') !== false){
$_SESSION['messageException'] = 'titleUnexpected';
$result = false;
}else if(strpos($text, '"') !== false){
$_SESSION['messageException'] = 'textUnexpected';
$result = false;
}
return $result;
}
function send_Message($receiver, $receiver_name, $writer, $writer_name, $title, $text){
// 줄바꿈 처리!!!
$send_date = date('Y-m-d H:i:s', time());
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
$query = "INSERT INTO MESSAGE(title, text, receiver_email, receiver_name, writer_email, writer_name, send_date, read_YN, receiver_Delete_YN, writer_Delete_YN, receiver_Important_YN, writer_Important_YN) VALUES('".$title."', '".$text."', '".$receiver."', '".$receiver_name."', '".$writer."', '".$writer_name."', '".$send_date."', 'N', 'N', 'N', 'N', 'N')";
mysqli_query($db_connect, $query);
}
function messageAccessCheck($message_seq, $user_email, $user_name){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
$query = "SELECT message_seq FROM MESSAGE WHERE message_seq = '".$message_seq."' AND ((receiver_email='".$user_email."' AND receiver_name='".$user_name."') OR (writer_email='".$user_email."' AND writer_name='".$user_name."'))";
$result = mysqli_query($db_connect, $query);
$row = mysqli_fetch_array($result);
if($row[0] != null){
return true;
}else{
return false;
}
}
function read_Message($message_seq){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
// Update read_YN column value
$modifyReadYN = "UPDATE MESSAGE SET read_YN = 'Y' WHERE message_seq = '".$message_seq."' AND receiver_name = '".$_SESSION['user_name']."'";
mysqli_query($db_connect, $modifyReadYN);
// Update read_date column value
$read_date = date('Y-m-d H:i:s', time());
$modifyReadDate = "UPDATE MESSAGE SET read_date = '".$read_date."' WHERE (message_seq = '".$message_seq."' AND read_date IS NULL AND receiver_name = '".$_SESSION['user_name']."')";
mysqli_query($db_connect, $modifyReadDate);
// Take message data
$getMessage = "SELECT * FROM MESSAGE WHERE message_seq = '".$message_seq."'";
$result = mysqli_query($db_connect, $getMessage);
return $result;
}
function get_allMessage($owner_email){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
$query = "SELECT * FROM MESSAGE WHERE (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'N') OR (writer_email = '".$owner_email."' AND writer_Delete_YN = 'N')ORDER BY send_date DESC";
$result = mysqli_query($db_connect, $query);
return $result;
}
function get_receiveMessage($owner_email){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
$query = "SELECT * FROM MESSAGE WHERE (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'N') ORDER BY send_date DESC";
$result = mysqli_query($db_connect, $query);
return $result;
}
function get_sendMessage($owner_email){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
$query = "SELECT * FROM MESSAGE WHERE (writer_email = '".$owner_email."' AND writer_Delete_YN = 'N') ORDER BY send_date DESC";
$result = mysqli_query($db_connect, $query);
return $result;
}
function get_importantMessage($owner_email){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
$query = "SELECT * FROM MESSAGE WHERE (writer_email = '".$owner_email."' AND writer_Delete_YN = 'N' AND writer_Important_YN = 'Y') OR (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'N' AND receiver_Important_YN = 'Y') ORDER BY send_date DESC";
$result = mysqli_query($db_connect, $query);
return $result;
}
function get_deleteMessage($owner_email){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
$query="SELECT * FROM MESSAGE WHERE (writer_email = '".$owner_email."' AND writer_Delete_YN = 'Y') OR (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'Y') ORDER BY send_date DESC";
$result = mysqli_query($db_connect, $query);
return $result;
}
function select_delete($owner_email, $targetArr, $selected){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
if($selected === 'allMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'N') OR (writer_email = '".$owner_email."' AND writer_Delete_YN = 'N')ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
for($i=0; $i<count($targetArr); $i++){
if($index == $targetArr[$i]){
$delQuery = "UPDATE MESSAGE SET receiver_Delete_YN = IF(receiver_email = '".$owner_email."', 'Y', receiver_Delete_YN), writer_Delete_YN = IF(writer_email = '".$owner_email."', 'Y', writer_Delete_YN) WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $delQuery);
break;
}
}
}
}else if($selected === 'receiveMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'N') ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
for($i=0; $i<count($targetArr); $i++){
if($index == $targetArr[$i]){
$delQuery = "UPDATE MESSAGE SET receiver_Delete_YN = 'Y' WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $delQuery);
break;
}
}
}
}else if($selected === 'sendMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (writer_email = '".$owner_email."' AND writer_Delete_YN = 'N') ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
for($i=0; $i<count($targetArr); $i++){
if($index == $targetArr[$i]){
$delQuery = "UPDATE MESSAGE SET writer_Delete_YN = 'Y' WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $delQuery);
break;
}
}
}
}else if($selected === 'importantMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (writer_email = '".$owner_email."' AND writer_Delete_YN = 'N' AND writer_Important_YN = 'Y') OR (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'N' AND receiver_Important_YN = 'Y') ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
for($i=0; $i<count($targetArr); $i++){
if($index == $targetArr[$i]){
$delQuery = "UPDATE MESSAGE SET receiver_Delete_YN = IF(receiver_email = '".$owner_email."', 'Y', receiver_Delete_YN), writer_Delete_YN = IF(writer_email = '".$owner_email."', 'Y', writer_Delete_YN) WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $delQuery);
break;
}
}
}
}
}
function select_important($owner_email, $targetArr, $selected){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
if($selected === 'allMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'N') OR (writer_email = '".$owner_email."' AND writer_Delete_YN = 'N')ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
for($i=0; $i<count($targetArr); $i++){
if($index == $targetArr[$i]){
$setQuery = "UPDATE MESSAGE SET receiver_Important_YN = IF(receiver_email = '".$owner_email."', 'Y', receiver_Important_YN), writer_Important_YN = IF(writer_email = '".$owner_email."', 'Y', writer_Important_YN) WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $setQuery);
break;
}
}
}
}else if($selected === 'receiveMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'N') ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
for($i=0; $i<count($targetArr); $i++){
if($index == $targetArr[$i]){
$setQuery = "UPDATE MESSAGE SET receiver_Important_YN = 'Y' WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $setQuery);
break;
}
}
}
}else if($selected === 'sendMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (writer_email = '".$owner_email."' AND writer_Delete_YN = 'N') ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
for($i=0; $i<count($targetArr); $i++){
if($index == $targetArr[$i]){
$setQuery = "UPDATE MESSAGE SET writer_Important_YN = 'Y' WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $setQuery);
break;
}
}
}
}
}
function select_remove($owner_email, $targetArr, $selected){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
if($selected === 'deleteMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'Y') OR (writer_email = '".$owner_email."' AND writer_Delete_YN = 'Y')ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
for($i=0; $i<count($targetArr); $i++){
if($index == $targetArr[$i]){
$delQuery = "UPDATE MESSAGE SET receiver_Delete_YN = IF(receiver_email = '".$owner_email."', 'R', receiver_Delete_YN), writer_Delete_YN = IF(writer_email = '".$owner_email."', 'R', writer_Delete_YN) WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $delQuery);
break;
}
}
}
}
}
function select_recover($owner_email, $targetArr, $selected){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
if($selected === 'deleteMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'Y') OR (writer_email = '".$owner_email."' AND writer_Delete_YN = 'Y')ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
for($i=0; $i<count($targetArr); $i++){
if($index == $targetArr[$i]){
$delQuery = "UPDATE MESSAGE SET receiver_Delete_YN = IF(receiver_email = '".$owner_email."', 'N', receiver_Delete_YN), writer_Delete_YN = IF(writer_email = '".$owner_email."', 'N', writer_Delete_YN) WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $delQuery);
break;
}
}
}
}
}
function all_delete($owner_email){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
$selectSection = $_SESSION['selected'];
if($selectSection === 'allMessage'){
$query = "UPDATE MESSAGE SET receiver_Delete_YN = IF(receiver_email = '".$owner_email."', 'Y', receiver_Delete_YN), writer_Delete_YN = IF(writer_email = '".$owner_email."', 'Y', writer_Delete_YN)
WHERE (receiver_email = '".$owner_email."' and receiver_Delete_YN = 'N') OR (writer_email = '".$owner_email."' and writer_Delete_YN = 'N')";
}else if($selectSection === 'receiveMessage'){
$query = "UPDATE MESSAGE SET receiver_Delete_YN = 'Y' WHERE (receiver_email = '".$owner_email."' and receiver_Delete_YN = 'N')";
}else if($selectSection === 'sendMessage'){
$query = "UPDATE MESSAGE SET writer_Delete_YN = 'Y' WHERE (writer_email = '".$owner_email."' and writer_Delete_YN = 'N')";
}
mysqli_query($db_connect, $query);
}
function all_remove($owner_email){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
$selectSection = $_SESSION['selected'];
if($selectSection === 'deleteMessage'){
$query = "UPDATE MESSAGE SET receiver_Delete_YN = IF(receiver_email = '".$owner_email."', 'R', receiver_Delete_YN), writer_Delete_YN = IF(writer_email = '".$owner_email."', 'R', writer_Delete_YN)
WHERE (receiver_email = '".$owner_email."' and receiver_Delete_YN = 'Y') OR (writer_email = '".$owner_email."' and writer_Delete_YN = 'Y')";
mysqli_query($db_connect, $query);
}
}
function toggleImportant($owner_email, $target, $selected){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
if($selected === 'allMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'N') OR (writer_email = '".$owner_email."' AND writer_Delete_YN = 'N')ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
if($index == $target){
$toggleQuery = "UPDATE MESSAGE SET receiver_Important_YN = IF(receiver_email = '".$owner_email."' AND receiver_Important_YN='N', 'Y', IF(receiver_email = '".$owner_email."' AND receiver_Important_YN='Y', 'N', receiver_Important_YN)), writer_Important_YN = IF(writer_email = '".$owner_email."' and writer_Important_YN='N', 'Y', IF(writer_email = '".$owner_email."' and writer_Important_YN='Y', 'N' ,writer_Important_YN)) WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $toggleQuery);
break;
}
}
}else if($selected === 'receiveMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'N') ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
if($index == $target){
$toggleQuery = "UPDATE MESSAGE SET receiver_Important_YN = IF(receiver_Important_YN='N', 'Y', 'N') WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $toggleQuery);
break;
}
}
}else if($selected === 'sendMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (writer_email = '".$owner_email."' AND writer_Delete_YN = 'N') ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
if($index == $target){
$toggleQuery = "UPDATE MESSAGE SET writer_Important_YN = IF(writer_Important_YN='N', 'Y', 'N') WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $toggleQuery);
break;
}
}
}else if($selected === 'importantMessage'){
$selQuery = "SELECT message_seq FROM MESSAGE WHERE (writer_email = '".$owner_email."' AND writer_Delete_YN = 'N' AND writer_Important_YN = 'Y') OR (receiver_email = '".$owner_email."' AND receiver_Delete_YN = 'N' AND receiver_Important_YN = 'Y') ORDER BY send_date DESC";
$selResult = mysqli_query($db_connect, $selQuery);
$index = 0;
while($row = mysqli_fetch_array($selResult)){
$index++;
if($index == $target){
$toggleQuery = "UPDATE MESSAGE SET receiver_Important_YN = IF(receiver_email = '".$owner_email."', 'N', receiver_Delete_YN), writer_Important_YN = IF(writer_email = '".$owner_email."', 'N', writer_Delete_YN) WHERE message_seq = '".$row[0]."'";
mysqli_query($db_connect, $toggleQuery);
break;
}
}
}
}
function existNewMessage($user_email, $user_name){
$db_connect = mysqli_connect(self::$db_host, self::$db_user, self::$db_passwd, self::$db_name);
$query = "SELECT message_seq FROM MESSAGE WHERE receiver_email = '".$user_email."' AND receiver_name = '".$user_name."' AND read_YN = 'N' AND receiver_Delete_YN = 'N'";
$result = mysqli_query($db_connect, $query);
$row = mysqli_fetch_array($result);
if($row != null){
return true;
}else{
return false;
}
}
}
?>