· 9 years ago · Nov 21, 2016, 05:08 PM
1<?php
2/**
3 * You are now in the UA messages plugin file.
4 * Hello! Welcome!
5 *
6 * Nice you are interested to our code! Maybe you want to
7 * join the team?
8 *
9 * Go to:
10 * http://www.ua-plugins.com/Team/Join
11 *
12 * Problems or questions? Go to:
13 * www.ua-plugins.com
14 *
15 * ===============================================================
16 * ORGET !!!! DON'T FORGET !!!! DON'T FORGET !!!! DON
17 * ===============================================================
18 *
19 * Copyright (c) 2015 UA-PLUGINS.COM MIT-LICENCE
20 * Permission is hereby granted, free of charge, to any person
21 * obtaining a copy of this software and associated documentation
22 * files (the "Software"), to deal in the Software without
23 * restriction, including without limitation the rights to use,
24 * copy, modify, merge, publish, distribute, sublicense, and/or sell
25 * copies of the Software, and to permit persons to whom the
26 * Software is furnished to do so, subject to the following
27 * conditions:
28 *
29 * The above copyright notice and this permission notice shall be
30 * included in all copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
34 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
36 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
37 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
38 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
39 * OTHER DEALINGS IN THE SOFTWARE.
40 *
41 * ===============================================================
42 * ORGET !!!! DON'T FORGET !!!! DON'T FORGET !!!! DON
43 * ===============================================================
44 */
45$plugins->add_hook('global_start', 'ua_messages_start');
46$plugins->add_hook('xmlhttp', 'ua_messages_xmlhttp');
47$plugins->add_hook('newreply_do_newreply_end', 'ua_messages_checkQuoted');
48$plugins->add_hook('newreply_do_newreply_end', 'ua_messages_threadAuthor');
49$plugins->add_hook("global_start", 'ua_messages_info');
50$plugins->add_hook('newreply_do_newreply_end', 'ua_messages_getMentioned');
51$plugins->add_hook('newthread_do_newthread_end', 'ua_messages_getMentioned');
52$plugins->add_hook('private_do_send_end', 'ua_messages_privateMessage');
53
54function ua_messages_info()
55{
56 return array(
57 "name" => "UA Plugins - Messages",
58 "description" => "UA Messages sents ALERTS to people who got Quoted, Tagged, when someone replies to a thread of a person or sents a PM to someone.",
59 "website" => "http://www.ua-plugins.com/Plugins/1",
60 "author" => "UA-Plugins",
61 "authorsite" => "http://www.ua-plugins.com",
62 "version" => "1",
63 "guid" => "UAplugins87d887t46qst74sdhy",
64 "compatibility" => "18*"
65 );
66}
67
68function ua_messages_install()
69{
70 global $db;
71
72 if (!$db->table_exists('ua_messages')) {
73 $db->write_query("
74 CREATE TABLE IF NOT EXISTS `" . TABLE_PREFIX . "ua_messages` (
75 `id` int(11) NOT NULL auto_increment,
76 `uid` int(11) NOT NULL,
77 `from_uid` int(11) NOT NULL,
78 `type` int(1) NOT NULL,
79 `msg_read` enum('1','0') NOT NULL DEFAULT '0',
80 `topic` int(11) DEFAULT NULL,
81 `pid` int(11) DEFAULT NULL,
82 `pm` int(11) DEFAULT NULL,
83 PRIMARY KEY (`id`)
84 ) ENGINE=InnoDB " . $db->build_create_table_collation() . ";
85 ");
86 }
87}
88
89function ua_messages_uninstall()
90{
91 global $db;
92 $db->drop_table('ua_messages');
93}
94
95function ua_messages_is_installed()
96{
97 global $db;
98 return $db->table_exists('ua_messages');
99}
100
101function ua_messages_activate()
102{
103 global $db, $UA_userInfoNeeded, $plugins, $cache, $lang;
104 $information = ua_messages_info();
105
106 if(!isset($UA_userInfoNeeded) || $UA_userInfoNeeded == "") {
107 $UA_userInfoNeeded = '<script>var ua_messages_rooturl = \'{$mybb->asset_url}\'; var ua_messages_uid = {$mybb->user[\'uid\']};</script>';
108 }
109
110 require MYBB_ROOT.'/inc/adminfunctions_templates.php';
111 find_replace_templatesets('headerinclude', '#{\$stylesheets}#', '{$stylesheets}
112<!-- UA - User Messages -->'. $UA_userInfoNeeded .'<script>
113 var ua_messages_language = {
114 browserUnsupported: \'{$lang->ua_messages_browserUnsupported}\',
115 permissionRequest: \'{$lang->ua_messages_permissionRequest}\',
116 permissionBlocked: \'{$lang->ua_messages_permissionBlocked}\',
117 permissionNoNotif: \'{$lang->ua_messages_permissionNoNotif}\',
118 permissionRequestNotif: \'{$lang->ua_messages_permissionRequestNotif}\',
119 blockedCloseMessage: \'{$lang->ua_messages_blockedCloseMessage}\',
120 information: \'{$lang->ua_messages_information}\',
121 requestTitle: \'{$lang->ua_messages_requestTitle}\'
122 };</script>
123 <script type="text/javascript" src="{$mybb->asset_url}/jscripts/ua-plugins/messages.js?v='. $information["version"] .'"></script><!-- /UA - User Messages -->');
124
125 // Create and activate the UA-Messages task
126 $taskExists = $db->simple_select(
127 'tasks',
128 'tid',
129 'file = \'ua_messages\'',
130 array('limit' => '1')
131 );
132
133 if ($db->num_rows($taskExists) == 0) {
134 require_once MYBB_ROOT . '/inc/functions_task.php';
135 $myTask = array(
136 'title' => "UA Messages: Removes messages",
137 'file' => 'ua_messages',
138 'description' => "All the messages that were read by the user will be deleted every night.",
139 'minute' => 0,
140 'hour' => 1,
141 'day' => '*',
142 'weekday' => '*',
143 'month' => '*',
144 'nextrun' => TIME_NOW + 3600,
145 'lastrun' => 0,
146 'enabled' => 1,
147 'logging' => 1,
148 'locked' => 0,
149 );
150 $task_id = $db->insert_query('tasks', $myTask);
151 $theTask = $db->fetch_array(
152 $db->simple_select('tasks', '*', 'tid = ' . (int) $task_id, 1)
153 );
154 $nextrun = fetch_next_run($theTask);
155 $db->update_query(
156 'tasks',
157 'nextrun = ' . $nextrun,
158 'tid = ' . (int) $task_id
159 );
160 $plugins->run_hooks('admin_tools_tasks_add_commit');
161 $cache->update_tasks();
162 } else {
163 require_once MYBB_ROOT . '/inc/functions_task.php';
164 $theTask = $db->fetch_array(
165 $db->simple_select('tasks', '*', 'file = \'ua_messages\'', 1)
166 );
167 $db->update_query(
168 'tasks',
169 array(
170 'enabled' => 1,
171 'nextrun' => fetch_next_run($theTask)
172 ),
173 'file = \'ua_messages\''
174 );
175 $cache->update_tasks();
176 }
177}
178
179function ua_messages_deactivate()
180{
181 global $db;
182 require MYBB_ROOT.'/inc/adminfunctions_templates.php';
183 find_replace_templatesets('headerinclude', '#\<!--\sUA - User Messages\s--\>(.+)\<!--\s/UA - User Messages\s--\>#is', '', 0);
184
185 // De-activate the UA-Messages task
186 $db->update_query('tasks', array('enabled' => 0), 'file = \'ua_messages\'');
187}
188
189function ua_messages_xmlhttp() {
190 global $mybb, $charset, $parser;
191
192 switch ($mybb->input['action']) {
193 case 'ua_createRequests':
194 header('Content-type: application/json');
195 echo ua_messages_getAll($mybb->user['uid']);
196 break;
197 }
198}
199
200/**
201 * =======================================================
202 *
203 * [LANGUAGE FILE]
204 * Searching for the language file, to translate it to
205 * your own language? No problem, simply copy:
206 *
207 * /inc/language/english/ua_messages.lang.php
208 *
209 * to:
210 *
211 * /inc/language/[YOUR LANGUAGE]/
212 *
213 * and edit the text.
214 *
215 * [BUGS]
216 * If there are any BUGS, you can report them to our
217 * website:
218 * www.ua-plugins.com
219 *
220 * /----------------------------------------------------\
221 * [COPYING]
222 * Like anyone knows, this plugin uses PHP, and it is
223 * very easy to copy this plugin, re-name it and say
224 * it is your own plugin. Hell… Please show some respect
225 * and don’t copy this :(
226 *
227 * We worked very hard to this plugin, and hope everyone
228 * like iit. But we don't want you to copy our scripts
229 * and use it as your own plugin. Thanks in advance
230 *
231 * IF you don't care, and still want to copy something,
232 * please set a link into the code to the website URL:
233 * http://www.ua-plugins.com
234 *
235 * When you do that, we'll be glad that you use our
236 * plugin :D
237 *
238 * ===============================================================
239 * ORGET !!!! DON'T FORGET !!!! DON'T FORGET !!!! DON
240 * ===============================================================
241 *
242 * Copyright (c) 2015 UA-PLUGINS.COM MIT-LICENCE
243 * Permission is hereby granted, free of charge, to any person
244 * obtaining a copy of this software and associated documentation
245 * files (the "Software"), to deal in the Software without
246 * restriction, including without limitation the rights to use,
247 * copy, modify, merge, publish, distribute, sublicense, and/or sell
248 * copies of the Software, and to permit persons to whom the
249 * Software is furnished to do so, subject to the following
250 * conditions:
251 *
252 * The above copyright notice and this permission notice shall be
253 * included in all copies or substantial portions of the Software.
254 *
255 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
256 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
257 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
258 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
259 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
260 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
261 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
262 * OTHER DEALINGS IN THE SOFTWARE.
263 *
264 * ===============================================================
265 * ORGET !!!! DON'T FORGET !!!! DON'T FORGET !!!! DON
266 * ===============================================================
267 */
268
269function ua_messages_start() {
270 global $lang;
271
272 $lang->load("ua_messages");
273}
274
275function ua_messages_getAll($userId) {
276 global $mybb, $db, $lang;
277
278 $lang->load('ua_messages');
279
280 $uaMessagesArray = array();
281
282 $createDBRequest = $db->query(" SELECT
283 id,
284 uid,
285 from_uid,
286 type,
287 topic,
288 pid,
289 pm
290 FROM
291 ". TABLE_PREFIX ."ua_messages
292
293 WHERE
294 ". TABLE_PREFIX ."ua_messages.uid = '". (int)$userId ."' AND
295 ". TABLE_PREFIX ."ua_messages.msg_read = '0'
296 ") or die($db->error);
297
298 while($info = $db->fetch_array($createDBRequest)) {
299 $messageType = $info['type'];
300 $postID = $mybb->settings['bburl'] . "/showthread.php?pid=". $info["pid"] ."#pid". $info["pid"];
301 $topicID = $mybb->settings['bburl'] . "/showthread.php?tid=". $info["topic"];
302 $pmID = $mybb->settings['bburl'] . "/private.php?action=read&pmid=". $info["pm"];
303 $getUser = get_user($info['from_uid']);
304
305 // Update directly topic to READ
306 $db->query("UPDATE ". TABLE_PREFIX ."ua_messages SET msg_read = '1' WHERE id = '". $info["id"] ."'");
307
308 if($messageType == 1)
309 {
310 // Quoted
311 $msgQuery = $db->query("SELECT subject FROM ". TABLE_PREFIX ."threads WHERE tid = '". $info["topic"] ."' LIMIT 1");
312
313 $msgExtraInfo = $msgQuery->fetch_assoc();
314
315 $title = $lang->ua_messages_beenQuoted_title;
316 $message = $lang->ua_messages_beenQuoted_body;
317
318 $message = str_replace("{UA_USERNAME}", $getUser['username'], $message);
319 $message = str_replace("{UA_TOPICTITLE}", $msgExtraInfo['subject'], $message);
320 $url = $postID;
321 }
322 else if($messageType == 2)
323 {
324 // Tagget/Mentioned
325 $msgQuery = $db->query("SELECT subject FROM ". TABLE_PREFIX ."threads WHERE tid = '". $info["topic"] ."' LIMIT 1");
326
327 $msgExtraInfo = $msgQuery->fetch_assoc();
328
329 $title = $lang->ua_messages_beenTagged_title;
330 $message = $lang->ua_messages_beenTagged_body;
331
332 $message = str_replace("{UA_USERNAME}", $getUser['username'], $message);
333 $message = str_replace("{UA_TOPICTITLE}", $msgExtraInfo['subject'], $message);
334 $url = $postID;
335 }
336 else if($messageType == 3)
337 {
338 // Replied to your thread
339 $msgQuery = $db->query("SELECT subject FROM ". TABLE_PREFIX ."threads WHERE tid = '". $info["topic"] ."' LIMIT 1");
340
341 $msgExtraInfo = $msgQuery->fetch_assoc();
342
343 $title = $lang->ua_messages_beenReplied_title;
344 $message = $lang->ua_messages_beenReplied_body;
345
346 $message = str_replace("{UA_USERNAME}", $getUser['username'], $message);
347 $message = str_replace("{UA_TOPICTITLE}", $msgExtraInfo['subject'], $message);
348 $url = $postID;
349 }
350 else if($messageType == 4)
351 {
352 // Sent PM
353 $msgQuery = $db->query("SELECT subject FROM ". TABLE_PREFIX ."privatemessages WHERE pmid = '". $info["pm"] ."' LIMIT 1");
354
355 $msgExtraInfo = $msgQuery->fetch_assoc();
356
357 $title = $lang->ua_messages_beenPMed_title;
358 $message = $lang->ua_messages_beenPMed_body;
359
360 $message = str_replace("{UA_USERNAME}", $getUser['username'], $message);
361 $message = str_replace("{UA_PM_TITLE}", $msgExtraInfo['subject'], $message);
362 $url = $pmID;
363 }
364 else if($messageType == 5)
365 {
366 // Subscribed
367 /**
368 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
369 * NOT WORKING YET
370 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
371
372 $msgQuery = $db->query("SELECT subject FROM ". TABLE_PREFIX ."threads WHERE tid = '". $info["topic"] ."' LIMIT 1");
373
374 $msgExtraInfo = $msgQuery->fetch_assoc();
375
376 $title = $lang->ua_messages_beenSubs_title;
377 $message = $lang->ua_messages_beenSubs_body;
378
379 $message = str_replace("{UA_USERNAME}", $getUser['username'], $message);
380 $message = str_replace("{UA_TOPICTITLE}", $msgExtraInfo['subject'], $message);
381 $url = $postID;*/
382 }
383 else
384 {
385 $title = $lang->ua_messages_unknown_title;
386 $message = $lang->ua_messages_unknown_body;
387 //$url = 'javascript:void()';
388 }
389
390 $newMessage = array(
391 "title" => $title,
392 "icon" => "messages_img",
393 "body" => $message,
394 "webUrl" => $url
395 );
396
397 array_push($uaMessagesArray, $newMessage);
398 }
399
400 //return json_encode($uaMessagesArray);
401
402 return json_encode($uaMessagesArray);
403}
404
405function ua_message_createNewMessage($userId, $fromId, $type, $topic, $pid, $pm) {
406 global $db;
407
408 $db->query("
409 INSERT INTO ". TABLE_PREFIX ."ua_messages
410 (
411 uid,
412 from_uid,
413 type,
414 topic,
415 pid,
416 pm
417 )
418
419 VALUES
420 (
421 ". (int)$userId .",
422 ". (int)$fromId .",
423 ". (int)$type .",
424 ". (int)$topic .",
425 ". (int)$pid .",
426 ". (int)$pm ."
427 )");
428}
429
430// When someone got quoted by someone else.
431function ua_messages_checkQuoted() {
432 global $post, $db, $mybb, $pid;
433
434 $message = $post["message"];
435
436 $patern = "#\\[quote=(?:\"|'|"|)(?<username>.*?)(?:\"|'|"|)(?: pid=(?:\"|'|"|)[\\d]*(?:\"|'|"|))?(?:\"|'|"|)(?: dateline=(?:\"|'|"|)[\\d]*(?:\"|'|"|))?(?:\"|'|"|)\](?<message>.*?)\\[\\/quote\\]#si";
437
438 preg_match($patern, $message, $matchArray);
439 $matchArray = array_filter($matchArray);
440
441 if (isset($matchArray['username'])) {
442 // Quote has username
443 $query = $db->simple_select('users', 'uid', 'LOWER(username)=\''.$matchArray['username'].'\'', array('limit' => '1'));
444 $uid = $db->fetch_field($query, 'uid');
445
446 ua_message_createNewMessage(
447 $uid,
448 $mybb->user['uid'],
449 1,
450 $post['tid'],
451 $pid,
452 0
453 );
454 }
455}
456
457// When someone replies to a topic, notify the topic author
458function ua_messages_threadAuthor() {
459 global $post, $mybb, $db, $pid;
460
461 $query = $db->simple_select('threads', 'uid', 'tid=\''.$post['tid'] .'\'', array('limit' => '1'));
462 $threadAuthorId = $db->fetch_field($query, 'uid');
463
464 if ($threadAuthorId != $mybb->user['uid']) {
465 // Only other people, and not when the person itself replys to his/hers topic
466
467 $query2 = $db->query("SELECT id FROM ". TABLE_PREFIX ."ua_messages WHERE uid = '". $threadAuthorId ."' AND type = '3' AND msg_read = '0' AND topic = '". $post['tid'] ."' LIMIT 1");
468
469 if($query2->num_rows == 0) {
470 // User has no alert yet from that topic!
471 ua_message_createNewMessage(
472 $threadAuthorId,
473 $mybb->user['uid'],
474 3,
475 $post['tid'],
476 $pid, // Post ID of the NEWEST post
477 0
478 );
479 }
480 }
481}
482
483// Something like @Jasper in posts
484function ua_messages_getMentioned() {
485 global $post, $mybb, $db, $pid;
486
487 $allreadyMentioned = array();
488
489 preg_match_all('/(^|\s)(@\w+)/', $post["message"], $matchArray);
490
491 foreach ($matchArray[2] as $getMentionedUsername) {
492 $getUsername = str_replace("@", "", $getMentionedUsername);
493
494 // User is not the user him/her-self
495 if($getUsername !== $post["username"] && !in_array($getUsername, $allreadyMentioned)) {
496 // Directly add to allreadyMentioned array
497 array_push($allreadyMentioned, $getUsername);
498
499 // Check username exists
500 $query = $db->simple_select('users', 'uid', 'LOWER(username)=\''.$getUsername.'\'', array('limit' => '1'));
501
502 // User exists
503 if($query->num_rows == 1) {
504 $uid = $db->fetch_field($query, 'uid');
505
506 ua_message_createNewMessage(
507 $uid,
508 $mybb->user['uid'],
509 2,
510 $post['tid'],
511 $pid,
512 0
513 );
514 }
515 }
516 }
517}
518
519// Get a PM
520function ua_messages_privateMessage() {
521 global $mybb, $db, $pm, $pmhandler;
522
523 $pmCounter = 0;
524
525 if($pm['saveasdraft'] != 1) {
526 foreach($pmhandler->data["recipients"] as $value) {
527 ua_message_createNewMessage(
528 $value['uid'],
529 $mybb->user['uid'],
530 4,
531 0,
532 0,
533 $pmhandler->pmid[$pmCounter]
534 );
535 $pmCounter++;
536 }
537 }
538}
539
540?>