-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpm-class.php
933 lines (827 loc) · 39.9 KB
/
pm-class.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
<?php
include_once('bbcode.php');
//cartpaujPM CLASS
if (!class_exists("cartpaujPM"))
{
class cartpaujPM
{
/******************************************SETUP BEGIN******************************************/
//Constructor
function cartpaujPM()
{
$this->setupLinks();
$this->adminOps = $this->getAdminOps();
}
function pmActivate()
{
global $table_prefix, $wpdb;
$charset_collate = '';
if( $wpdb->has_cap('collation'))
{
if(!empty($wpdb->charset))
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if(!empty($wpdb->collate))
$charset_collate .= " COLLATE $wpdb->collate";
}
$sqlMsgs = "CREATE TABLE ".$this->tableMsgs."(
`id` int(11) NOT NULL auto_increment,
`parent_id` int(11) NOT NULL default '0',
`from_user` int(11) NOT NULL default '0',
`to_user` int(11) NOT NULL default '0',
`last_sender` int(11) NOT NULL default '0',
`date` datetime NOT NULL default '0000-00-00 00:00:00',
`last_date` datetime NOT NULL default '0000-00-00 00:00:00',
`message_title` varchar(65) NOT NULL,
`message_contents` longtext NOT NULL,
`message_read` int(11) NOT NULL default '0',
`to_del` int(11) NOT NULL default '0',
`from_del` int(11) NOT NULL default '0',
PRIMARY KEY (`id`))
{$charset_collate};";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sqlMsgs);
}
function widget($args)
{
global $user_ID;
$uData = get_userdata($user_ID);
echo $args['before_widget'];
echo $args['before_title'].__("Messages", "cartpaujpm").$args['after_title'];
if (!$uData)
echo __("Login to view your messages", "cartpaujpm");
else
{
$URL = get_permalink($this->getPageID());
$numNew = $this->getNewMsgs();
$numAnn = $this->getAnnouncementsNum();
echo __("Hi", "cartpaujpm")." ".$uData->user_login.",<br/>".
__("You have", "cartpaujpm")." (<font color='red'>".$numNew."</font>) ".__("new messages", "cartpaujpm")."<br/>".
__("There are", "cartpaujpm")." (".$numAnn.") ".__("announcement(s)", "cartpaujpm")."<br/>".
"<a href='".$URL."'>".__("View Message Box", "cartpaujpm")."</a>";
}
echo $args['after_widget'];
}
//Setup some variables
var $adminOpsName = "cartpaujPM_options";
var $adminOps = array();
var $userOpsName = "cartpaujPM_uOptions";
var $userOps = array();
var $notify = "";
var $pluginDir = "";
var $pluginURL = "";
var $styleDir = "";
var $styleURL = "";
var $pageURL = "";
var $actionURL = "";
var $jsURL = "";
var $tableMsgs = "";
function jsInit()
{
if($_GET['pmjsscript'] == '1')
{
global $wpdb, $user_ID;
require_once('js/search.php');
}
}
function setupLinks() //And DB table name too :)
{
global $table_prefix;
$this->pluginDir = ABSPATH."wp-content/plugins/cartpauj-pm/";
$this->pluginURL = WP_CONTENT_URL."/plugins/cartpauj-pm/";
$this->styleDir = $this->pluginDir."style/";
$this->styleURL = $this->pluginURL."style/";
$this->jsURL = $this->pluginURL."js/";
$this->tableMsgs = $table_prefix."cartpauj_pm_messages";
}
function addToWPHead()
{
?>
<script language="JavaScript" type="text/javascript" src="<?php echo $this->jsURL; ?>script.js"></script>
<link rel="stylesheet" type="text/css" href="<?php echo $this->styleURL; ?>style.css" />
<?php
}
function getPageID()
{
global $wpdb;
return $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%[cartpauj-pm]%' AND post_status = 'publish' AND post_type = 'page'");
}
function setPageURLs()
{
global $wp_rewrite;
if($wp_rewrite->using_permalinks())
$delim = "?";
else
$delim = "&";
$this->pageURL = get_permalink($this->getPageID());
$this->actionURL = $this->pageURL.$delim."pmaction=";
}
/******************************************SETUP END******************************************/
/******************************************ADMIN SETTINGS PAGE BEGIN******************************************/
function addAdminPage()
{
add_options_page('Cartpauj PM', 'Cartpauj PM', 9, basename(__FILE__), array(&$this, "dispAdminPage"));
}
function dispAdminPage()
{
if ($this->pmAdminSave())
echo "<div id='message' class='updated fade'><p>".__("Options successfully saved", "cartpaujpm")."</p></div>";
$viewAdminOps = $this->getAdminOps(); //Get current options
echo "<div class='wrap'>
<h2>".__("Cartpauj PM Settings", "cartpaujpm")."</h2>
<form id='pm-admin-save-form' name='pm-admin-save-form' method='post' action=''>
<table class='widefat'>
<thead>
<tr><th width='50%'>".__("Setting", "cartpaujpm")."</th><th width='50%'>".__("Value", "cartpaujpm")."</th></tr>
</thead>
<tr><td>".__("Max messages a user can keep in box? (0 = Unlimited)", "cartpaujpm")."<br /><small>".__("Admins always have Unlimited", "cartpaujpm")."</small></td><td><input type='text' size='10' name='num_messages' value='".$viewAdminOps['num_messages']."' /> ".__("Default","cartpaujpm").": 50</td></tr>
<tr><td>".__("Messages to show per page", "cartpaujpm")."<br/><small>".__("Do not set this to 0!", "cartpaujpm")."</small></td><td><input type='text' size='10' name='messages_page' value='".$viewAdminOps['messages_page']."' /> ".__("Default","cartpaujpm").": 15</td></tr>
<tr><td colspan='2'><input type='checkbox' name='hide_branding' ".checked(($viewAdminOps['hide_branding'] || $viewAdminOps['hide_branding'] == 'on'))." /> ".__("Hide \"Cartpauj PM\" Branding Footer", "cartpaujpm")."</td></tr>
<tr><td colspan='2'><span><input class='button' type='submit' name='pm-admin-save' value='".__("Save Options", "cartpaujpm")."' /></span></td></tr>
</table>
</form>
<p><strong>".__("Setup Instructions", "cartpaujpm").":</strong></p>
<p><ul><li>".__("Create a new page.", "cartpaujpm")."</li>
<li>".__("Paste [cartpauj-pm] under the HTML tab of the page editor", "cartpaujpm")."</li>
<li>".__("Publish the page", "cartpaujpm")."</li>
</ul></p>
</div>";
}
function pmAdminSave()
{
if (isset($_POST['pm-admin-save']))
{
$saveAdminOps = array('num_messages' => $_POST['num_messages'],
'messages_page' => $_POST['messages_page'],
'hide_branding' => $_POST['hide_branding']
);
update_option($this->adminOpsName, $saveAdminOps);
return true;
}
return false;
}
function getAdminOps()
{
$pmAdminOps = array('num_messages' => 50,
'messages_page' => 15,
'hide_branding' => false
);
//Get old values if they exist
$adminOps = get_option($this->adminOpsName);
if (!empty($adminOps))
{
foreach ($adminOps as $key => $option)
$pmAdminOps[$key] = $option;
}
update_option($this->adminOpsName, $pmAdminOps);
$this->adminOps = $pmAdminOps;
return $pmAdminOps;
}
/******************************************ADMIN SETTINGS PAGE END******************************************/
/******************************************USER SETTINGS PAGE BEGIN******************************************/
function dispUserPage()
{
global $user_ID;
if ($this->pmUserSave())
$this->notify = __("Your settings have been saved!", "cartpaujpm");
$viewUserOps = $this->getUserOps($user_ID); //Get current options
$prefs = "<p><strong>".__("Set your preferences below", "cartpaujpm").":</strong></p>
<form id='pm-user-save-form' name='pm-user-save-form' method='post' action=''>
<input type='checkbox' name='allow_messages' value='true'";
if($viewUserOps['allow_messages'] == 'true')
$prefs .= "checked='checked'";
$prefs .= "/> <i>".__("Allow others to send me messages?", "cartpaujpm")."</i><br/>
<input type='checkbox' name='allow_emails' value='true'";
if($viewUserOps['allow_emails'] == 'true')
$prefs .= "checked='checked'";
$prefs .= "/> <i>".__("Email me when I get new messages?", "cartpaujpm")."</i><br/>
<input class='button' type='submit' name='pm-user-save' value='".__("Save Options", "cartpaujpm")."' />
</form>";
return $prefs;
}
function pmUserSave()
{
global $user_ID;
if (isset($_POST['pm-user-save']))
{
$saveUserOps = array( 'allow_emails' => $_POST['allow_emails'],
'allow_messages' => $_POST['allow_messages']
);
update_usermeta($user_ID, $this->userOpsName, $saveUserOps);
return true;
}
return false;
}
function getUserOps($ID)
{
$pmUserOps = array( 'allow_emails' => 'true',
'allow_messages' => 'true'
);
//Get old values if they exist
$userOps = get_usermeta($ID, $this->userOpsName);
if (!empty($userOps))
{
foreach ($userOps as $key => $option)
$pmUserOps[$key] = $option;
}
update_usermeta($ID, $this->userOpsName, $pmUserOps);
return $pmUserOps;
}
/******************************************USER SETTINGS PAGE END******************************************/
/******************************************NEW MESSAGE PAGE BEGIN******************************************/
function dispNewMsg()
{
global $user_ID;
$users = $this->get_users();
$to = $_GET['to'];
if (!$to)
$to = 0;
$adminOps = $this->getAdminOps();
if (!$this->isBoxFull($user_ID, $adminOps['num_messages'], '1'))
{
$newMsg = "<p><strong>".__("Create New Message", "cartpaujpm").":</strong></p>";
$newMsg .= "<form name='message' action='".$this->actionURL."checkmessage' method='post'>".
__("To", "cartpaujpm").":<br/>".
"<input type='text' id='search-q' onkeyup='javascript:autosuggest(\"".$this->actionURL."\")' name='message_to' autocomplete='off' value='".$this->convertToUser($to)."' /><br/>
<div id='results'></div>".
__("Subject", "cartpaujpm").":<br/>
<input type='text' name='message_title' maxlength='65' value='' /><br/>".
__("Message", "cartpaujpm").":<br/>".$this->get_form_buttons()."<br/>
<textarea name='message_content'></textarea>
<input type='hidden' name='message_from' value='".$user_ID."' />
<input type='hidden' name='message_date' value='".current_time('mysql', $gmt = 1)."' />
<input type='hidden' name='parent_id' value='0' /><br/>
<input type='submit' onClick='this.disabled=true;this.form.submit();' value='".__("Send Message", "cartpaujpm")."' />
</form>";
return $newMsg;
}
else
{
$error = "<p><strong>".__("Message Error", "cartpaujpm").":</strong></p>
<p><strong><a href='".$this->pageURL."' style='color:navy;'>".__("Back To Message Box", "cartpaujpm")."</a></strong></p>";
$this->notify = __("You cannot send messages because your message box is full!", "cartpaujpm");
return $error;
}
}
/******************************************NEW MESSAGE PAGE END******************************************/
/******************************************READ MESSAGE PAGE BEGIN******************************************/
function dispReadMsg()
{
global $wpdb, $user_ID;
$pID = $_GET['id'];
$wholeThread = $this->getWholeThread($pID);
$threadOut = "<p><strong>".__("Message Thread", "cartpaujpm").":</strong></p>
<table><tr><th width='15%'>".__("Sender", "cartpaujpm")."</th><th width='85%'>".__("Message", "cartpaujpm")."</th></tr>";
foreach ($wholeThread as $post)
{
//Check for privacy errors first
if ($post->to_user != $user_ID && $post->from_user != $user_ID)
{
$error = "<p><strong>".__("Privacy Error", "cartpaujpm").":</strong></p>
<p><strong><a href='".$this->pageURL."' style='color:navy;'>".__("Back To Message Box", "cartpaujpm")."</a></strong></p>";
$this->notify = __("You do not have permission to view this message!", "cartpaujpm");
return $error;
}
//setup info for the reply form
if ($post->parent_id == 0) //If it is the parent message
{
$to = $post->from_user;
if ($to == $user_ID) //Make sure user doesn't send a message to himself
$to = $post->to_user;
$message_title = $this->output_filter($post->message_title);
if (substr_count($message_title, __("Re:", "cartpaujpm")) < 1) //Prevent all the Re:'s from happening
$re = __("Re:", "cartpaujpm");
else
$re = "";
}
$uData = get_userdata($post->from_user);
$threadOut .= "<tr><td>".$uData->user_login."<br/><small>".$this->formatDate($post->date)."</small><br/>".get_avatar($post->from_user, 60)."</td>";
if ($post->parent_id == 0) //If it is the parent message
{
$threadOut .= "<td class='pmtext'><strong>".__("Subject", "cartpaujpm").": </strong>".$this->output_filter($post->message_title)."<hr/>".apply_filters("comment_text", $this->autoembed($this->output_filter($post->message_contents)))."</td></tr>";
}
else
{
$threadOut .= "<td class='pmtext'>".apply_filters("comment_text", $this->autoembed($this->output_filter($post->message_contents)))."</td></tr>";
}
}
//SHOW THE REPLY FORM
$threadOut .= "</table>
<p><strong>".__("Add Reply", "cartpaujpm").":</strong></p>
<form name='message' action='".$this->actionURL."checkmessage' method='post'>".
$this->get_form_buttons()."<br/>
<textarea name='message_content'></textarea>
<input type='hidden' name='message_to' value='".$this->convertToUser($to)."' />
<input type='hidden' name='message_title' value='".$re.$message_title."' />
<input type='hidden' name='message_from' value='".$user_ID."' />
<input type='hidden' name='message_date' value='".current_time('mysql', $gmt = 1)."' />
<input type='hidden' name='parent_id' value='".$pID."' /><br/>
<input type='submit' onClick='this.disabled=true;this.form.submit();' value='".__("Send Message", "cartpaujpm")."' />
</form>";
if ($user_ID != $post->from_user) //Update only if the reader is not the sender ???
$wpdb->query($wpdb->prepare("UPDATE {$this->tableMsgs} SET message_read = 1 WHERE id = %d", $pID));
return $threadOut;
}
function getWholeThread($id)
{
global $wpdb;
$results = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$this->tableMsgs} WHERE id = %d OR parent_id = %d ORDER BY id ASC", $id, $id));
return $results;
}
function convertToUser($to)
{
global $wpdb;
$result = $wpdb->get_var($wpdb->prepare("SELECT user_login FROM {$wpdb->users} WHERE ID = %d", $to));
return $result;
}
/******************************************READ MESSAGE PAGE END******************************************/
/******************************************CHECK MESSAGE PAGE BEGIN******************************************/
function dispCheckMsg()
{
global $wpdb, $user_ID;
$from = $_POST['message_from'];
$preTo = $_POST['message_to'];
$to = $this->convertToID($preTo);
$myReplaceSub = array("'", "\\");//Make sure we get ' and \ out of the message title
$title = str_replace($myReplaceSub, "", $this->input_filter($_POST['message_title']));
$content = $this->input_filter($_POST['message_content']);
$parentID = $_POST['parent_id'];
$date = $_POST['message_date'];
$adminOps = $this->getAdminOps();
if ($to)
$toUserOps = $this->getUserOps($to);
//Check for errors first
if (!$to || !$title || !$content || ($from != $user_ID))
{
if (!$to)
$theError = __("You must enter a valid recipient!", "cartpaujpm");
if (!$title)
$theError = __("You must enter a valid subject!", "cartpaujpm");
if (!$content)
$theError = __("You must enter some message content!", "cartpaujpm");
if ($from != $user_ID)
$theError = __("You do not have permission to send this message!", "cartpaujpm");
$error = "<p><strong>".__("Message Error", "cartpaujpm").":</strong></p>
<p><strong><a href='".$this->pageURL."' style='color:navy;'>".__("Back To Message Box", "cartpaujpm")."</a></strong></p>";
$this->notify = $theError;
return $error;
}
if ($toUserOps['allow_messages'] != 'true')
{
$error = "<p><strong>".__("Message Error", "cartpaujpm").":</strong></p>
<p><strong><a href='".$this->pageURL."' style='color:navy;'>".__("Back To Message Box", "cartpaujpm")."</a></strong></p>";
$this->notify = __("This user does not want to receive messages!", "cartpaujpm");
return $error;
}
if ($this->isBoxFull($to, $adminOps['num_messages'], $parentID))
{
$error = "<p><strong>".__("Message Error", "cartpaujpm").":</strong></p>
<p><strong><a href='".$this->pageURL."' style='color:navy;'>".__("Back To Message Box", "cartpaujpm")."</a></strong></p>";
$this->notify = __("The Recipients Message Box Is Full!", "cartpaujpm");
return $error;
}
//If no errors then continue on
if ($parentID == 0)
$wpdb->query("INSERT INTO {$this->tableMsgs} (from_user, to_user, message_title, message_contents, parent_id, last_sender, date, last_date) VALUES ('{$from}','{$to}','{$title}','{$content}','{$parentID}','{$from}','{$date}','{$date}')");
else
{
$wpdb->query("INSERT INTO {$this->tableMsgs} (from_user, to_user, message_title, message_contents, parent_id, date) VALUES ('{$from}','{$to}','{$title}','{$content}','{$parentID}','{$date}')");
//A lot of querys but they're fairly quick and won't get called except when messages are sent
$wpdb->query($wpdb->prepare("UPDATE {$this->tableMsgs} SET message_read = 0 WHERE id = %d", $parentID));
$wpdb->query($wpdb->prepare("UPDATE {$this->tableMsgs} SET last_sender = '{$from}' WHERE id = %d", $parentID));
$wpdb->query($wpdb->prepare("UPDATE {$this->tableMsgs} SET last_date = '{$date}' WHERE id = %d", $parentID));
$wpdb->query($wpdb->prepare("UPDATE {$this->tableMsgs} SET to_del = 0 WHERE id = %d", $parentID));
$wpdb->query($wpdb->prepare("UPDATE {$this->tableMsgs} SET from_del = 0 WHERE id = %d", $parentID));
}
$check = "<p><strong>".__("Message Sent", "cartpaujpm").":</strong></p>
<p><strong><a href='".$this->pageURL."' style='color:navy;'>".__("Back To Message Box", "cartpaujpm")."</a></strong></p>";
$this->notify = __("Your message was successfully sent!", "cartpaujpm");
$this->sendEmail($to, $from);
return $check;
}
function isBoxFull($to, $boxSize, $parentID)
{
global $wpdb;
$to_user = get_userdata($to);
$get_messages = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$this->tableMsgs} WHERE (to_user = %d AND parent_id = 0 AND to_del <> 1) OR (from_user = %d AND parent_id = 0 AND from_del <> 1)", $to, $to));
$num = $wpdb->num_rows;
if ($boxSize == 0 || $num < $boxSize || $parentID != 0 || current_user_can('level_9') || $to_user->user_level >= 9)
return false;
else
return true;
}
function sendEmail($to, $from)
{
$toOptions = $this->getUserOps($to);
$notify = $toOptions['allow_emails'];
if ($notify == 'true')
{
$sendername = get_bloginfo("name");
$sendermail = get_bloginfo("admin_email");
$uData = get_userdata($from);
$sendfrom = $uData->user_login;
$headers = "MIME-Version: 1.0\r\n" .
"From: ".$sendername." "."<".$sendermail.">\n" .
"Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\r\n";
$mailMessage = __("You have received a new message from", "cartpaujpm")." ".$sendfrom.", ".__("follow this link to view it", "cartpaujpm").": ".$this->pageURL;
$mUser = get_userdata($to);
$mailTo = $mUser->user_email;
wp_mail($mailTo, __("New Message", "cartpaujpm"), $mailMessage, $headers);
}
}
function convertToID($preTo)
{
global $wpdb, $user_ID;
$result = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->users} WHERE user_login = %s", $preTo));
if ($result != $user_ID && $result)
return $result;
else
return 0;
}
/******************************************CHECK MESSAGE PAGE END******************************************/
/******************************************MESSAGE-BOX PAGE BEGIN******************************************/
function dispMsgBox()
{
global $wpdb, $user_ID;
$adminOps = $this->getAdminOps();
$numMsgs = $this->getUserNumMsgs();
if ($numMsgs)
{
$msgsOut = "<p><strong>".__("Your Messages", "cartpaujpm").":</strong></p>";
$numPgs = $numMsgs / $adminOps['messages_page'];
if ($numPgs > 1)
{
$msgsOut .= "<p><strong>".__("Page", "cartpaujpm").": </strong> ";
for ($i = 0; $i < $numPgs; $i++)
if ($_GET['pmpage'] != $i)
$msgsOut .= "<a href='".$this->actionURL."messagebox&pmpage=".$i."'>".($i+1)."</a> ";
else
$msgsOut .= "[<b>".($i+1)."</b>] ";
$msgsOut .= "</p>";
}
$msgsOut .= "<table><tr>
<th width='20%'>".__("Started By", "cartpaujpm")."</th>
<th width='50%'>".__("To: Subject", "cartpaujpm")."</th>
<th width='20%'>".__("Last Reply By", "cartpaujpm")."</th>
<th width='10%'>".__("Delete", "cartpaujpm")."</th></tr>";
$msgs = $this->getMsgs();
foreach ($msgs as $msg)
{
if ($msg->message_read == 0 && $msg->last_sender != $user_ID)
$read = "<font color='#FF0000'>".__("Unread", "cartpaujpm")."</font>";
else
$read = __("Read", "cartpaujpm");
$uSend = get_userdata($msg->from_user);
$uLast = get_userdata($msg->last_sender);
$toUser = get_userdata($msg->to_user);
$msgsOut .= "<tr><td>".$uSend->user_login."<br/><small>".$this->formatDate($msg->date)."</small></td>
<td>".$toUser->user_login.": <a href='".$this->actionURL."viewmessage&id=".$msg->id."'>".$msg->message_title."</a><br/><small>".$read."</small></td>
<td>".$uLast->user_login."<br/><small>".$this->formatDate($msg->last_date)."</small></td>
<td><a href='".$this->actionURL."deletemessage&id=".$msg->id."' onclick='return confirm(\"".__('Are you sure?', 'cartpaujpm')."\");'>".__("Delete", "cartpaujpm")."</a></td>
</tr>";
}
$msgsOut .= "</table>";
return $msgsOut;
}
else
{
$empty = "<p><strong>".__("No Messages", "cartpaujpm").":</strong></p>
<p><strong><a href='".$this->pageURL."' style='color:navy;'>".__("Refresh Message Box", "cartpaujpm")."</a></strong></p>";
$this->notify = __("Your message box is empty!", "cartpaujpm");
return $empty;
}
}
function getMsgs()
{
global $wpdb, $user_ID;
$page = $_GET['pmpage'];
$adminOps = $this->getAdminOps();
if (!$page)
$page = 0;
$start = $page * $adminOps['messages_page'];
$end = $adminOps['messages_page'];
$get_messages = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$this->tableMsgs} WHERE (to_user = %d AND parent_id = 0 AND to_del <> 1) OR (from_user = %d AND parent_id = 0 AND from_del <> 1) ORDER BY last_date DESC LIMIT %d, %d", $user_ID, $user_ID, $start, $end));
return $get_messages;
}
/******************************************MESSAGE-BOX PAGE END******************************************/
/******************************************DELETE PAGE BEGIN******************************************/
function dispDelMsg()
{
global $wpdb, $user_ID;
$delID = $_GET['id'];
$toDuser = $wpdb->get_var($wpdb->prepare("SELECT to_user FROM {$this->tableMsgs} WHERE id = %d", $delID));
$toDel = $wpdb->get_var($wpdb->prepare("SELECT to_del FROM {$this->tableMsgs} WHERE id = %d", $delID));
$fromDel = $wpdb->get_var($wpdb->prepare("SELECT from_del FROM {$this->tableMsgs} WHERE id = %d", $delID));
if ($toDuser == $user_ID)
{
if ($fromDel == 0)
$wpdb->query($wpdb->prepare("UPDATE {$this->tableMsgs} SET to_del = 1 WHERE id = %d", $delID));
else
$wpdb->query($wpdb->prepare("DELETE FROM {$this->tableMsgs} WHERE id = %d OR parent_id = %d", $delID, $delID));
}
else
{
if ($toDel == 0)
$wpdb->query($wpdb->prepare("UPDATE {$this->tableMsgs} SET from_del = 1 WHERE id = %d", $delID));
else
$wpdb->query($wpdb->prepare("DELETE FROM {$this->tableMsgs} WHERE id = %d OR parent_id = %d", $delID, $delID));
}
$deleted = "<p><strong>".__("Message Deleted", "cartpaujpm").":</strong></p>
<p><strong><a href='".$this->pageURL."' style='color:navy;'>".__("Back To Message Box", "cartpaujpm")."</a></strong></p>";
$this->notify = __("Your message was successfully deleted!", "cartpaujpm");
return $deleted;
}
/******************************************DELETE PAGE END******************************************/
/******************************************VIEW ANNOUNCEMENTS BEGIN******************************************/
/*
Announcement Feature TODO list
# Mass emails when announcement is created
# Clean-up style
*/
function dispAnnouncement()
{
global $wpdb, $user_ID;
$announcements = $this->getAnnouncements();
$num = $wpdb->num_rows;
if ($this->addAnnouncement()) //Adding a new announcement?
{
$announce = "<p><strong>".__("Announcement Added", "cartpaujpm").":</strong></p>
<p><strong><a href='".$this->actionURL."viewannouncements' style='color:navy;'>".__("Back To Announcements", "cartpaujpm")."</a></strong></p>";
$this->notify = __("The announcement was successfully added!", "cartpaujpm");
return $announce;
}
if ($this->deleteAnnouncement()) //Deleting an announcement?
{
$announce = "<p><strong>".__("Announcement Deleted", "cartpaujpm").":</strong></p>
<p><strong><a href='".$this->actionURL."viewannouncements' style='color:navy;'>".__("Back To Announcements", "cartpaujpm")."</a></strong></p>";
$this->notify = __("The announcement was successfully deleted!", "cartpaujpm");
return $announce;
}
if (!$num) //Just viewing announcements
{
$announce = "<p><strong>".__("Announcements", "cartpaujpm").":</strong></p>";
if (current_user_can('level_9'))
{
$announce .= $this->dispAnnounceForm();
}
$this->notify = __("There are no announcements!", "cartpaujpm");
}
else
{
$announce = "<p><strong>".__("Announcements", "cartpaujpm").":</strong></p>";
if (current_user_can('level_9'))
{
$announce .= $this->dispAnnounceForm();
}
$announce .= "<table>";
$a = 0;
foreach ($announcements as $announcement)
{
$announce .= "<tr class='trodd".$a."'><td class='pmtext'><strong>".__("Subject", "cartpaujpm").":</strong> ".$this->output_filter($announcement->message_title).
"<br/><strong>".__("Date", "cartpaujpm").":</strong> ".$this->formatDate($announcement->date);
if (current_user_can('level_9'))
$announce .= "<br/><a href='".$this->actionURL."viewannouncements&del=1&id=".$announcement->id."'>".__("Delete", "cartpaujpm")."</a>";
$announce .= "<hr/>";
$announce .= "<strong>".__("Message", "cartpaujpm").":</strong><br/>".apply_filters("comment_text", $this->output_filter($announcement->message_contents))."</td></tr>";
if ($a) $a = 0; else $a = 1; //Alternate table colors
}
$announce .= "</table>";
}
return $announce;
}
function dispAnnounceForm()
{
$form = "<p>".__("Add a new announcement below", "cartpaujpm")."</p>
<form name='message' action='' method='post'>
".__("Subject", "cartpaujpm").":<br/>
<input type='text' name='message_title' value='' /><br/>".
$this->get_form_buttons()."<br/>
<textarea name='message_content'></textarea>
<input type='hidden' name='message_date' value='".current_time('mysql', $gmt = 1)."' /><br/>
<input type='submit' onClick='this.disabled=true;this.form.submit();' name='add-announcement' value='".__("Submit", "cartpaujpm")."' />
</form>";
return $form;
}
function getAnnouncements()
{
global $wpdb; //message_read = 12 indicates that the msg is an announcement :)
$results = $wpdb->get_results("SELECT * FROM {$this->tableMsgs} WHERE message_read = 12 ORDER BY `id` DESC");
return $results;
}
function getAnnouncementsNum()
{
global $wpdb; //message_read = 12 indicates that the msg is an announcement :)
$results = $wpdb->get_results("SELECT * FROM {$this->tableMsgs} WHERE message_read = 12 ORDER BY `id` DESC");
return $wpdb->num_rows;
}
function addAnnouncement()
{
global $wpdb;
$title = $this->input_filter($_POST['message_title']);
$contents = $this->input_filter($_POST['message_content']);
$date = $_POST['message_date'];
$read = '12';
if ($title && $contents)
{
$wpdb->query("INSERT INTO {$this->tableMsgs} (message_title, message_contents, date, message_read) VALUES ('{$title}','{$contents}','{$date}','{$read}')");
return true;
}
return false;
}
function deleteAnnouncement()
{
global $wpdb;
$delID = $_GET['id'];
if (current_user_can('level_9') && $_GET['del']) //Make sure only admins can delete announcements
{
$wpdb->query($wpdb->prepare("DELETE FROM {$this->tableMsgs} WHERE id = %d", $delID));
return true;
}
return false;
}
/******************************************VIEW ANNOUNCEMENTS END******************************************/
/******************************************MAIN DISPLAY BEGIN******************************************/
function dispHeader()
{
global $user_ID, $user_login;
$numNew = $this->getNewMsgs();
$numAnn = $this->getAnnouncementsNum();
$msgBoxSize = $this->getUserNumMsgs();
$adminOps = $this->getAdminOps();
if ($adminOps['num_messages'] == 0 || current_user_can('level_9'))
$msgBoxTotal = __("Unlimited", "cartpaujpm");
else
$msgBoxTotal = $adminOps['num_messages'];
$header = "<div id='pm-wrapper'>";
$header .= "<div id='pm-header'>";
$header .= get_avatar($user_ID, 60)."<p><strong>".__("Welcome", "cartpaujpm").": ".$user_login."</strong><br/>";
$header .= __("You have", "cartpaujpm")." (<font color='red'>".$numNew."</font>) ".__("new messages", "cartpaujpm").
" ".__("and", "cartpaujpm")." (".$numAnn.") ".__("announcement(s)", "cartpaujpm")."<br/>";
if ($msgBoxTotal == __("Unlimited", "cartpaujpm") || $msgBoxSize < $msgBoxTotal)
$header .= __("Message box size", "cartpaujpm").": ".$msgBoxSize." ".__("of", "cartpaujpm")." ".$msgBoxTotal."</p>";
else
$header .= "<font color='red'>".__("Your Message Box Is Full!", "cartpaujpm")."</font></p>";
$header .= "</div>";
return $header;
}
function dispMenu()
{
$menu = "<div id='pm-menu'>";
$menu .= "<a href='".$this->pageURL."'>".__("Message Box", "cartpaujpm")."</a> | ";
$menu .= "<a href='".$this->actionURL."viewannouncements'>".__("Announcements", "cartpaujpm")."</a> | ";
$menu .= "<a href='".$this->actionURL."newmessage'>".__("New Message", "cartpaujpm")."</a> | ";
$menu .= "<a href='".$this->actionURL."directory'>".__("Directory", "cartpaujpm")."</a> | ";
$menu .= "<a href='".$this->actionURL."settings'>".__("Settings", "cartpaujpm")."</a>";
$menu .= "</div>";
$menu .= "<div id='pm-content'>";
return $menu;
}
function dispNotify()
{
$notify = "<div id='pm-notify'>".$this->notify."</div>";
return $notify;
}
function dispFooter()
{
$footer = "</div>"; //End content
//Maybe Add Notify
if ($this->notify != "")
$footer .= $this->dispNotify();
if($this->adminOps['hide_branding'] != 'on')
$footer .= "<div id='pm-footer'><a href='http://cartpauj.icomnow.com'>Cartpauj PM ".$this->get_version()."</a></div>";
$footer .= "</div>"; //End main wrapper
return $footer;
}
function dispDirectory()
{
$users = $this->get_users();
$directory = "";
foreach($users as $u)
{
$directory .= '<p><strong>'.$u->user_login.'</strong> - <a href="'.$this->actionURL.'newmessage&to='.$u->ID.'">'.__('Send Message', 'cartpaujpm').'</a></p>';
}
return $directory;
}
//Display the proper contents
function displayAll()
{
global $user_ID;
if ($user_ID)
{
//Finish the setup since these wouldn't work in the constructor
$this->userOps = $this->getUserOps($user_ID);
$this->setPageURLs();
//Add header
$out = $this->dispHeader();
//Add Menu
$out .= $this->dispMenu();
//Start the guts of the display
switch ($_GET['pmaction'])
{
case 'newmessage':
$out .= $this->dispNewMsg();
break;
case 'checkmessage':
$out .= $this->dispCheckMsg();
break;
case 'viewmessage':
$out .= $this->dispReadMsg();
break;
case 'deletemessage':
$out .= $this->dispDelMsg();
break;
case 'directory':
$out .= $this->dispDirectory();
break;
case 'settings':
$out .= $this->dispUserPage();
break;
case 'viewannouncements':
$out .= $this->dispAnnouncement();
break;
default: //Message box is shown by Default
$out .= $this->dispMsgBox();
break;
}
//Add footer
$out .= $this->dispFooter();
}
else
{
$out = "<p><strong>".__("You must be logged-in to view this page.", "cartpaujpm")."</strong></p>";
}
return $out;
}
/******************************************MAIN DISPLAY END******************************************/
/******************************************MISC. FUNCTIONS BEGIN******************************************/
function get_users()
{
global $wpdb, $table_prefix;
return $wpdb->get_results("SELECT user_login, ID FROM $wpdb->users ORDER BY user_login ASC");
}
function get_form_buttons()
{
$button = '
<a title="'.__("Bold", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[b]", "[/b]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/b.png" /></a>
<a title="'.__("Italic", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[i]", "[/i]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/i.png" /></a>
<a title="'.__("Underline", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[u]", "[/u]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/u.png" /></a>
<a title="'.__("Strikethrough", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[s]", "[/s]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/s.png" /></a>
<a title="'.__("Code", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[code]", "[/code]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/code.png" /></a>
<a title="'.__("Quote", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[quote]", "[/quote]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/quote.png" /></a>
<a title="'.__("List", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[list]", "[/list]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/list.png" /></a>
<a title="'.__("List item", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[*]", "", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/li.png" /></a>
<a title="'.__("Link", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[url]", "[/url]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/url.png" /></a>
<a title="'.__("Image", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[img]", "[/img]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/img.png" /></a>
<a title="'.__("Email", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[email]", "[/email]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/email.png" /></a>
<a title="'.__("Add Hex Color", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[color=#]", "[/color]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/color.png" /></a>
<a title="'.__("Embed", "cartpaujpm").'" href="javascript:void(0);" onclick=\'surroundTheText("[embed]", "[/embed]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/embed.png" /></a>';
return $button;
}
function output_filter($string)
{
$parser = new cartpaujBBCParser();
return stripslashes($parser->bbc2html($string));
}
function input_filter($string)
{
global $wpdb;
$Find = array("<", "%", "$"); //Fixes some serious issues when entering these characters, also allows code to be posted
$Replace = array("<", "%", "$");
$newStr = str_replace($Find, $Replace, $string);
return strip_tags($wpdb->escape($newStr));
}
function getUserNumMsgs()
{
global $wpdb, $user_ID;
$get_messages = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$this->tableMsgs} WHERE (to_user = %d AND parent_id = 0 AND to_del <> 1) OR (from_user = %d AND parent_id = 0 AND from_del <> 1)", $user_ID, $user_ID));
$num = $wpdb->num_rows;
return $num;
}
function formatDate($date)
{
return date('M d, h:i A', strtotime($date));
}
function getNewMsgs()
{
global $wpdb, $user_ID;
$get_pms = $wpdb->get_results($wpdb->prepare("SELECT id FROM {$this->tableMsgs} WHERE (to_user = %d AND parent_id = 0 AND to_del <> 1 AND message_read = 0 AND last_sender <> %d) OR (from_user = %d AND parent_id = 0 AND from_del <> 1 AND message_read = 0 AND last_sender <> %d)", $user_ID, $user_ID, $user_ID, $user_ID));
return $wpdb->num_rows;
}
function autoembed($string)
{
global $wp_embed;
if (is_object($wp_embed))
return $wp_embed->autoembed($string);
else
return $string;
}
function get_version()
{
$plugin_data = implode('', file(ABSPATH."wp-content/plugins/cartpauj-pm/pm-main.php"));
if (preg_match("|Version:(.*)|i", $plugin_data, $version))
$version = $version[1];
return $version;
}
/******************************************MISC. FUNCTIONS END******************************************/
} //END CLASS
} //ENDIF
?>