/
acppauthorchat-process.php
287 lines (233 loc) · 11.2 KB
/
acppauthorchat-process.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
<?php
/* Author Chat Process v2.0.3 */
if (!function_exists('array_column')) {
function array_column(array $input, $columnKey, $indexKey = null) {
$array = array();
foreach ($input as $value) {
if (!isset($value[$columnKey])) {
trigger_error("Key \"$columnKey\" does not exist in array");
return false;
}
if (is_null($indexKey)) {
$array[] = $value[$columnKey];
} else {
if (!isset($value[$indexKey])) {
trigger_error("Key \"$indexKey\" does not exist in array");
return false;
}
if (!is_scalar($value[$indexKey])) {
trigger_error("Key \"$indexKey\" does not contain scalar value");
return false;
}
$array[$value[$indexKey]] = $value[$columnKey];
}
}
return $array;
}
}
if (!function_exists('wp_verify_nonce') ) {
require_once( ABSPATH . 'wp-includes/pluggable.php' );
}
if (isset($_POST['function']) && (isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'ajax-nonce') != false)) {
global $wpdb;
$author_chat_table = $wpdb->prefix . 'author_chat';
$author_chat_room_participants_table = $wpdb->prefix . 'author_chat_room_participants';
$wp_usermeta = $wpdb->prefix . 'usermeta';
$function = sanitize_text_field( $_POST['function'] );
if (isset($_POST['room_pressed_button_id'])) {
$room_pressed_button_id = strip_tags(sanitize_text_field($_POST['room_pressed_button_id']));
} else {
$room_pressed_button_id = '0';
}
if (isset($_POST['user_time_zone'])) {
$user_time_zone = strip_tags(sanitize_text_field($_POST['user_time_zone']));
} else {
$user_time_zone = '0';
}
$result = array();
switch ($function) {
case( 'getState' ):
$result = $wpdb->get_var("SELECT COUNT(*) FROM $author_chat_table");
break;
case( 'send' ):
$user_id = strip_tags(sanitize_text_field($_POST['user_id']));
$nickname = strip_tags(sanitize_text_field($_POST['nickname']));
$message = strip_tags(sanitize_text_field($_POST['message']));
$date = date('Y-m-d H:i:s');
if (( $message ) != '\n') {
//$newDate = strtotime("$date +{$user_time_zone} hours");
//$date = date('Y-m-d,H:i:s', $newDate);
$result = array(
'user_id' => $user_id,
'nickname' => $nickname,
'content' => $message,
'chat_room_id' => $room_pressed_button_id,
'date' => $date
);
$wpdb->insert($author_chat_table, $result, array('%d', '%s', '%s', '%d', '%s'));
acppauthorchat_sendFCMNotification($nickname, $message, ["room_id" => $room_pressed_button_id], "AAAAl54tLgs:APA91bFZaptLBvPyNRvReX8WYwKNMRqgt4Mx109sbteaL13t6gx6OOASiA_REWO5g9zjy1jZxqRD_X7BglRMFG3imdr4uUhJ9i94HWK3dciFbEbKi7ErD7nuKaxlFo6dVGAKrcdPjiAd");
}
break;
case( 'update' ):
$lines = $wpdb->get_results("SELECT id, user_id, nickname, content, chat_room_id, date FROM $author_chat_table ORDER BY id ASC", ARRAY_A);
$text = array();
foreach ($lines as $line) {
if ($line['chat_room_id'] == $room_pressed_button_id) { // Show only main chat room conversation
$text[] = $line;
}
}
$date = array_column($text, 'date');
array_walk_recursive($date, function( &$element ) use(&$user_time_zone) {
$element = strtotime("$element +{$user_time_zone} hours");
$element = date('Y-m-d,H:i:s', $element);
});
$result = array(
'id' => array_column($text, 'id'),
'uid' => array_column($text, 'user_id'),
'nick' => array_column($text, 'nickname'),
'msg' => array_column($text, 'content'),
'date' => $date
);
break;
case( 'initiate' ):
$lines = $wpdb->get_results("SELECT id, user_id, nickname, content, chat_room_id, date FROM $author_chat_table ORDER BY id ASC", ARRAY_A);
$text = array();
foreach ($lines as $line) {
if ($line['chat_room_id'] == $room_pressed_button_id) { // Show only main chat room conversation
$text[] = $line;
}
}
$date = array_column($text, 'date');
array_walk_recursive($date, function( &$element ) use(&$user_time_zone) {
$element = strtotime("$element +{$user_time_zone} hours");
$element = date('Y-m-d,H:i:s', $element);
});
$result = array(
'id' => array_column($text, 'id'),
'uid' => array_column($text, 'user_id'),
'nick' => array_column($text, 'nickname'),
'msg' => array_column($text, 'content'),
'date' => $date
);
break;
case( 'addRoom' ):
//Remove rooms without conversations before adding another one
$wpdb->query("
DELETE acrp
FROM $author_chat_room_participants_table acrp
LEFT JOIN $author_chat_table ac ON ac.chat_room_id = acrp.chat_room_id
WHERE ac.chat_room_id IS NULL
");
$user_id = sanitize_text_field(strip_tags(filter_var($_POST['user_id'], FILTER_SANITIZE_NUMBER_INT)));
$room_id = sanitize_text_field(strip_tags(filter_var($_POST['room_id'], FILTER_SANITIZE_NUMBER_INT)));
$result = array(
'user_id' => $user_id,
'chat_room_id' => $room_id
);
$wpdb->insert($author_chat_room_participants_table, $result, array('%d', '%d'));
break;
case( 'getRoomsForUser' ):
$user_id = sanitize_text_field(strip_tags(filter_var($_POST['user_id'], FILTER_SANITIZE_NUMBER_INT)));
$lines = $wpdb->get_results("SELECT user_id, chat_room_id FROM $author_chat_room_participants_table WHERE user_id = $user_id", ARRAY_A);
$text = array();
foreach ($lines as $line) {
$text[] = $line;
}
$result = array(
'chat_room_id' => array_column($text, 'chat_room_id')
);
break;
case( 'searchUser' ):
if (isset($_POST['search_user'])) {
$user_name = sanitize_text_field(strip_tags(filter_var($_POST['search_user'], FILTER_SANITIZE_STRING, array('flags' => FILTER_FLAG_STRIP_LOW | FILTER_FLAG_ENCODE_HIGH))));
$lines = $wpdb->get_results("SELECT user_id, meta_value FROM $wp_usermeta WHERE meta_value LIKE '%$user_name%' AND meta_key = 'nickname'", ARRAY_A);
$text = array();
foreach ($lines as $line) {
$text[] = $line;
}
$result = array(
'user_id' => array_column($text, 'user_id'),
'nickname' => array_column($text, 'meta_value')
);
}
break;
case( 'addUser' ):
$user_id = sanitize_text_field(strip_tags(filter_var($_POST['user_id'], FILTER_SANITIZE_NUMBER_INT)));
$room_id = sanitize_text_field(strip_tags(filter_var($_POST['room_id'], FILTER_SANITIZE_NUMBER_INT)));
$duplicate_check = $wpdb->get_results("SELECT user_id FROM $author_chat_room_participants_table WHERE user_id = $user_id AND chat_room_id = $room_id", ARRAY_A);
if (count($duplicate_check) === 0) {
$result = array(
'user_id' => $user_id,
'chat_room_id' => $room_id
);
$wpdb->insert($author_chat_room_participants_table, $result, array('%d', '%d'));
}
break;
case( 'getUsersForRoom' ):
$room_id = sanitize_text_field(strip_tags(filter_var($_POST['room_id'], FILTER_SANITIZE_NUMBER_INT)));
//$lines = $wpdb->get_results("SELECT DISTINCT user_id FROM $author_chat_room_participants_table WHERE chat_room_id = $room_id", ARRAY_A);
$lines = $wpdb->get_results("
SELECT acrp.user_id, um.meta_value
FROM $author_chat_room_participants_table acrp
INNER JOIN $wp_usermeta um
ON acrp.user_id = um.user_id
WHERE acrp.chat_room_id = $room_id
AND um.meta_key = 'nickname'
", ARRAY_A);
$text = array();
foreach ($lines as $line) {
$text[] = $line;
}
$result = array(
'user_id' => array_column($text, 'user_id'),
'nickname' => array_column($text, 'meta_value')
);
break;
case( 'removeUser' ):
$user_id = sanitize_text_field(strip_tags(filter_var($_POST['user_id'], FILTER_SANITIZE_NUMBER_INT)));
$room_id = sanitize_text_field(strip_tags(filter_var($_POST['room_id'], FILTER_SANITIZE_NUMBER_INT)));
$result = array(
'user_id' => $user_id,
'chat_room_id' => $room_id
);
$wpdb->delete($author_chat_room_participants_table, $result, array('%d', '%d'));
break;
case( 'whoIsChannelOwner' ):
$room_id = sanitize_text_field(strip_tags(filter_var($_POST['room_id'], FILTER_SANITIZE_NUMBER_INT)));
$lines = $wpdb->get_results("SELECT DISTINCT user_id FROM $author_chat_room_participants_table WHERE id IN(SELECT MIN(id) from $author_chat_room_participants_table WHERE chat_room_id = $room_id) AND chat_room_id = $room_id", ARRAY_A);
$text = array();
foreach ($lines as $line) {
$text[] = $line;
}
$result = array(
'user_id' => array_column($text, 'user_id'),
);
break;
}
echo wp_send_json($result);
}
function acppauthorchat_sendFCMNotification($title = "", $body = "", $customData = [], $serverKey = ""){
$topic = $_SERVER['SERVER_NAME'];
if($serverKey != ""){
ini_set("allow_url_fopen", "On");
$data = array (
"to" => "/topics/$topic",
"notification" => array(
"body" => sanitize_text_field($body),
"title" => $title,
),
"data" => $customData
);
$args = array(
'body' => json_encode($data),
'headers'=> array(
'Content-Type' => 'application/json',
'Authorization' => 'key=' . $serverKey
)
);
$result = wp_remote_post ( "https://fcm.googleapis.com/fcm/send", $args );
return json_decode( $result );
}
return false;
}
?>