PluginProbe
Front End PM / 1.2
Front End PM v1.2
trunk 1.1 1.2 1.3 10.1.1 10.1.2 10.1.3 10.1.4 10.1.5 10.1.6 10.1.7 10.2.1 11.1.1 11.2.1 11.2.2 11.2.3 11.3.1 11.3.3 11.3.4 11.3.5 11.3.6 11.3.7 11.3.8 11.3.9 11.4.1 All 58 releases
front-end-pm / fep-class.php

fep-class.php in Front End PM 1.2, at fep-class.php

1,581 lines 65.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 include_once('bbcode.php');
3 //clFEPm CLASS
4 if (!class_exists("clFEPm"))
5 {
6 class clFEPm
7 {
8 /******************************************SETUP BEGIN******************************************/
9 //Constructor
10 function clFEPm()
11 {
12 $this->setupLinks();
13 $this->adminOps = $this->getAdminOps();
14 }
15
16 function fepActivate()
17 {
18 global $wpdb;
19
20 $charset_collate = '';
21 if( $wpdb->has_cap('collation'))
22 {
23 if(!empty($wpdb->charset))
24 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
25 if(!empty($wpdb->collate))
26 $charset_collate .= " COLLATE $wpdb->collate";
27 }
28 $installed_ver = get_option( "fep_db_version" );
29 $fep_db_version = 1.1;
30
31 if( $installed_ver != $fep_db_version ) {
32
33 $sqlMsgs = "CREATE TABLE ".$this->fepTable."(
34 `id` int(11) NOT NULL auto_increment,
35 `parent_id` int(11) NOT NULL default '0',
36 `from_user` int(11) NOT NULL default '0',
37 `to_user` int(11) NOT NULL default '0',
38 `last_sender` int(11) NOT NULL default '0',
39 `date` datetime NOT NULL default '0000-00-00 00:00:00',
40 `last_date` datetime NOT NULL default '0000-00-00 00:00:00',
41 `message_title` varchar(65) NOT NULL,
42 `message_contents` longtext NOT NULL,
43 `message_read` int(11) NOT NULL default '0',
44 `to_del` int(11) NOT NULL default '0',
45 `from_del` int(11) NOT NULL default '0',
46 PRIMARY KEY (`id`))
47 {$charset_collate};";
48
49 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
50
51 dbDelta($sqlMsgs);
52 update_option( "fep_db_version", $fep_db_version );
53 }
54 }
55
56 function translation()
57 {
58 //SETUP TEXT DOMAIN FOR TRANSLATIONS
59 $plugin_dir = basename(dirname(__FILE__));
60 load_plugin_textdomain('fep', false, $plugin_dir.'/languages/');
61 }
62
63 function widget($args)
64 {
65 global $user_ID;
66 $uData = get_userdata($user_ID);
67 $this->setPageURLs();
68 echo $args['before_widget'];
69 if (!$uData)
70 echo __("Login to view your messages", "fep");
71 else
72 {
73 $numNew = $this->getNewMsgs_btn();
74 $numAnn = $this->getAnnouncementsNum_btn();
75 $numNewadm = $this->getNewMsgs_admin();
76 echo "<a class='fep-button' href='".$this->pageURL."'>".__("Inbox", "fep")."".$numNew."</a>
77 <a class='fep-button' href='".$this->actionURL."viewannouncements'>".__("Announcement", "fep")."".$numAnn."</a>";
78 if (current_user_can('manage_options'))
79 echo "<a class='fep-button' href='".$this->actionURL."viewallmgs'>".__("Other's Message", "fep")."".$numNewadm."</a>";
80 }
81 echo $args['after_widget'];
82 }
83
84 function widget_text($args)
85 {
86 global $user_ID;
87 $uData = get_userdata($user_ID);
88 $this->setPageURLs();
89 echo $args['before_widget'];
90 echo $args['before_title'].__("Messages", "fep").$args['after_title'];
91 if (!$uData)
92 echo __("Login to view your messages", "fep");
93 else
94 {
95 $numNew = $this->getNewMsgs();
96 $numAnn = $this->getAnnouncementsNum();
97 $numNewadm = $this->getNewMsgs_admin();
98 echo __("Hi", "fep")." ".$uData->display_name.",<br/>".
99 __("You have", "fep")." <a href='".$this->pageURL."'>(<font color='red'>".$numNew."</font>) ".__("new message(s)", "fep")."</a><br/>".
100 __("There are", "fep")." <a href='".$this->actionURL."viewannouncements'>(<font color='red'>".$numAnn."</font>) ".__("announcement(s)", "fep")."</a><br/>";
101 if (current_user_can('manage_options'))
102 echo "<a href='".$this->actionURL."viewallmgs'>".__("Other's Message(s)", "fep")."".$numNewadm."</a><br/>";
103 echo "<a href='".$this->pageURL."'>".__("View Message Box", "fep")."</a><br/>";
104
105 }
106 echo $args['after_widget'];
107 }
108
109 //Setup some variables
110 var $adminOpsName = "FEP_options";
111 var $adminOps = array();
112 var $userOpsName = "FEP_uOptions";
113 var $userOps = array();
114
115 var $error = "";
116 var $success = "";
117
118 var $pluginDir = "";
119 var $pluginURL = "";
120 var $styleDir = "";
121 var $styleURL = "";
122 var $pageURL = "";
123 var $actionURL = "";
124 var $jsURL = "";
125
126 var $fepTable = "";
127
128 function jsInit()
129 {
130 if (isset($_GET['fepjscript']))
131 if($_GET['fepjscript'] == '1')
132 {
133 global $wpdb, $user_ID;
134 require_once('js/search.php');
135 }
136 }
137
138 function setupLinks() //And DB table name too :)
139 {
140 global $wpdb;
141 $this->pluginDir = plugin_dir_path( __FILE__ )."/";
142 $this->pluginURL = plugins_url()."/front-end-pm/";
143 $this->styleDir = $this->pluginDir."style/";
144 $this->styleURL = $this->pluginURL."style/";
145 $this->jsURL = $this->pluginURL."js/";
146
147 $this->fepTable = $wpdb->prefix."fep_messages";
148 }
149
150 function fep_enqueue_scripts()
151 {
152 wp_enqueue_style( 'fep-style', $this->styleURL . 'style.css' );
153 wp_enqueue_script( 'fep-script', $this->jsURL . 'script.js', array(), '1.0.0', true );
154 }
155
156 function getPageID()
157 {
158 global $wpdb;
159 return $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%[front-end-pm]%' AND post_status = 'publish' AND post_type = 'page' LIMIT 1");
160 }
161
162 function setPageURLs()
163 {
164 global $wp_rewrite;
165 if($wp_rewrite->using_permalinks())
166 $delim = "?";
167 else
168 $delim = "&";
169 $this->pageURL = get_permalink($this->getPageID());
170 $this->actionURL = $this->pageURL.$delim."fepaction=";
171 }
172 /******************************************SETUP END******************************************/
173
174 /******************************************ADMIN SETTINGS PAGE BEGIN******************************************/
175 function addAdminPage()
176 {
177 add_menu_page('Front End PM', 'Front End PM', 'manage_options', 'fep-admin-settings', array(&$this, "dispAdminPage"),plugins_url( 'front-end-pm/images/msgBox.gif' ));
178 add_submenu_page('fep-admin-settings', 'Front End PM - ' .__('Settings','cp'), __('Settings','cp'), 'manage_options', 'fep-admin-settings', array(&$this, "dispAdminPage"));
179 add_submenu_page('fep-admin-settings', 'Front End PM - ' .__('Instruction','cp'), __('Instruction','cp'), 'manage_options', 'fep-instruction', array(&$this, "dispInstructionPage"));
180 }
181
182 function dispAdminPage()
183 {
184 if ($this->pmAdminSave())
185 echo "<div id='message' class='updated fade'><p>".__("Options successfully saved", "fep")."</p></div>";
186 $viewAdminOps = $this->getAdminOps(); //Get current options
187 $url = 'http://www.banglardokan.com/blog/recent/project/front-end-pm-2215/';
188 echo "<div class='wrap'>
189 <h2>".__("Front End PM Settings", "fep")."</h2>
190 <form action='https://www.paypal.com/cgi-bin/webscr' method='post' target='_top'>
191 <input type='hidden' name='cmd' value='_donations'>
192 <input type='hidden' name='business' value='4HKBQ3QFSCPHJ'>
193 <input type='hidden' name='lc' value='US'>
194 <input type='hidden' name='item_name' value='Front End PM'>
195 <input type='hidden' name='item_number' value='Front End PM'>
196 <input type='hidden' name='currency_code' value='USD'>
197 <input type='hidden' name='bn' value='PP-DonationsBF:btn_donateCC_LG.gif:NonHosted'>
198 <input type='image' src='https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif' border='0' name='submit' alt='PayPal - The safer, easier way to pay online!'>
199 <img alt='' border='0' src='https://www.paypalobjects.com/en_US/i/scr/pixel.gif' width='1' height='1'>
200 </form>
201 <form id='fep-admin-save-form' name='fep-admin-save-form' method='post' action=''>
202 <table class='widefat'>
203 <thead>
204 <tr><th width='30%'>".__("Setting", "fep")."</th><th width='70%'>".__("Value", "fep")."</th></tr>
205 </thead>
206 <tr><td>".__("Max messages a user can keep in box? (0 = Unlimited)", "fep")."<br /><small>".__("Admins always have Unlimited", "fep")."</small></td><td><input type='text' size='10' name='num_messages' value='".$viewAdminOps['num_messages']."' /><br/> ".__("Default","fep").": 50</td></tr>
207 <tr><td>".__("Messages to show per page", "fep")."<br/><small>".__("Do not set this to 0!", "fep")."</small></td><td><input type='text' size='10' name='messages_page' value='".$viewAdminOps['messages_page']."' /><br/> ".__("Default","fep").": 15</td></tr>
208 <tr><td>".__("Maximum user per page in Directory", "fep")."<br/><small>".__("Do not set this to 0!", "fep")."</small></td><td><input type='text' size='10' name='user_page' value='".$viewAdminOps['user_page']."' /><br/> ".__("Default","fep").": 50</td></tr>
209 <tr><td>".__("Time delay between two messages send by a user in minutes (0 = No delay required)", "fep")."<br/><small>".__("Admins have no restriction", "fep")."</small></td><td><input type='text' size='10' name='time_delay' value='".$viewAdminOps['time_delay']."' /><br/> ".__("Default","fep").": 5</td></tr>
210 <tr><td>".__("Block Username", "fep")."<br /><small>".__("Separated by comma", "fep")."</small></td><td><input type='text' size='30' name='have_permission' value='".$viewAdminOps['have_permission']."' /></td></tr>
211 <tr><td>".__("Valid email address for \"to\" field of announcement email", "fep")."<br /><small>".__("All users email will be in \"Bcc\" field", "fep")."</small></td><td><input type='text' size='30' name='ann_to' value='".$viewAdminOps['ann_to']."' /></td></tr>
212 <tr><td colspan='2'><input type='checkbox' name='notify_ann' ".checked($viewAdminOps['notify_ann'], 'on', false)." /> ".__("Send email to all users when a new announcement is published?", "fep")."</td></tr>
213 <tr><td colspan='2'><input type='checkbox' name='hide_directory' ".checked($viewAdminOps['hide_directory'], 'on', false)." /> ".__("Hide Directory from front end?", "fep")."<br /><small>".__("Always shown to Admins", "fep")."</small></td></tr>
214 <tr><td colspan='2'><input type='checkbox' name='hide_autosuggest' ".checked($viewAdminOps['hide_autosuggest'], 'on', false)." /> ".__("Hide Autosuggestion when typing recipient name?", "fep")."<br /><small>".__("Always shown to Admins", "fep")."</small></td></tr>
215 <tr><td colspan='2'><input type='checkbox' name='disable_new' ".checked($viewAdminOps['disable_new'], 'on', false)." /> ".__("Disable \"send new message\" for all users except admins?", "fep")."<br /><small>".__("Users can send reply", "fep")."</small></td></tr>
216 <tr><td colspan='2'><input type='checkbox' name='hide_branding' ".checked($viewAdminOps['hide_branding'], 'on', false)." /> ".__("Hide Branding Footer?", "fep")."</td></tr>
217 <tr><td colspan='2'><span><input class='button-primary' type='submit' name='fep-admin-save' value='".__("Save Options", "fep")."' /></span></td></tr>
218 </table>
219 </form>
220 <ul>".sprintf(__("For more info or report bug pleasse visit <a href='%s' target='_blank'>Front End PM</a>", "fep"),esc_url($url))."</ul>
221 </div>";
222 }
223
224 function dispInstructionPage()
225 {
226 $url = 'http://www.banglardokan.com/blog/recent/project/front-end-pm-2215/';
227 echo "<div class='wrap'>
228 <h2>".__("Front End PM Setup Instruction", "fep")."</h2>
229 <p><ul><li>".__("Create a new page.", "fep")."</li>
230 <li>".__("Paste following code under the HTML tab of the page editor", "fep")."<code>[front-end-pm]</code></li>
231 <li>".__("Publish the page.", "fep")."</li>
232 <li>".__("Or you can create a page below.", "fep")."</li>
233 <li>".sprintf(__("For more info or report bug pleasse visit <a href='%s' target='_blank'>Front End PM</a>", "fep"),esc_url($url))."</li>
234 </ul></p>
235 <h2>".__("Create Page For \"Front End PM\"", "fep")."</h2>
236 ".$this->fep_createPage()."</div>";
237 }
238
239 function pmAdminSave()
240 {
241 if (isset($_POST['fep-admin-save']))
242 {
243 if (!is_email($_POST['ann_to'])) {
244 echo "<div id='message' class='error'><p>".__("Please enter a valid email address!", "fep")."</p></div>";
245 return;}
246 if (!ctype_digit($_POST['num_messages']) || !$this->is_positive($_POST['messages_page']) || !$this->is_positive($_POST['user_page']) || !ctype_digit($_POST['time_delay'])) {
247 echo "<div id='message' class='error'><p>".__("First four fields support only positive numbers!", "fep")."</p></div>";
248 return;}
249 $saveAdminOps = array('num_messages' => $_POST['num_messages'],
250 'messages_page' => $_POST['messages_page'],
251 'user_page' => $_POST['user_page'],
252 'time_delay' => $_POST['time_delay'],
253 'hide_branding' => $_POST['hide_branding'],
254 'hide_directory' => $_POST['hide_directory'],
255 'hide_autosuggest' => $_POST['hide_autosuggest'],
256 'disable_new' => $_POST['disable_new'],
257 'ann_to' => $_POST['ann_to'],
258 'notify_ann' => $_POST['notify_ann'],
259 'have_permission' => $_POST['have_permission']
260 );
261 update_option($this->adminOpsName, $saveAdminOps);
262 return true;
263 }
264 return false;
265 }
266
267 function getAdminOps()
268 {
269 $pmAdminOps = array('num_messages' => 50,
270 'messages_page' => 15,
271 'user_page' => 50,
272 'time_delay' => 5,
273 'hide_directory' => false,
274 'ann_to' => get_bloginfo("admin_email"),
275 'notify_ann' => false,
276 'hide_autosuggest' => false,
277 'disable_new' => false,
278 'hide_branding' => false,
279 'have_permission' => ''
280 );
281
282 //Get old values if they exist
283 $adminOps = get_option($this->adminOpsName);
284 if (!empty($adminOps))
285 {
286 foreach ($adminOps as $key => $option)
287 $pmAdminOps[$key] = $option;
288 }
289
290 update_option($this->adminOpsName, $pmAdminOps);
291 $this->adminOps = $pmAdminOps;
292 return $pmAdminOps;
293 }
294
295 function fep_createPage(){
296 $token = $this->getToken();
297 $form = "<p>
298 <form name='fep-create-page' action='".$this->fep_createPage_action()."' method='post'>
299 ".__("Title of \"Front End PM\" Page", "fep").":<br/>
300 <input type='text' name='fep-create-page-title' value='' /><br/>
301 <strong>".__("Slug", "fep")."</strong>: <em>".__("If blank, slug will be automatically created based on Title", "fep")."</em><br/>
302 <input type='text' name='fep-create-page-slug' value='' /><br/>
303 <input type='hidden' name='token' value='".$token."' /><br/>
304 <input class='button-primary' type='submit' name='fep-create-page' value='".__("Create Page", "fep")."' />
305 </form></p>";
306
307 return $form;
308 }
309
310 function fep_createPage_action(){
311 if (isset($_POST['fep-create-page'])){
312 $titlePre = wp_strip_all_tags($_POST['fep-create-page-title']);
313 $title = utf8_encode($titlePre);
314 $slugPre = wp_strip_all_tags($_POST['fep-create-page-slug']);
315 $slug = utf8_encode($slugPre);
316
317 if ($this->getPageID() !=''){
318 echo "<div id='message' class='error'><p>" .sprintf(__("Already created page <a href='%s'>%s </a> for \"Front End PM\". Please use that page instead!", "fep"),get_permalink($this->getPageID()),get_the_title($this->getPageID()))."</p></div>";
319 return;}
320 if (!$title){
321 echo "<div id='message' class='error'><p>" .__("You must enter a valid Title!", "fep")."</p></div>";
322 return;}
323 // Check if a form has been sent
324 $postedToken = filter_input(INPUT_POST, 'token');
325 if (empty($postedToken))
326 {
327 echo "<div id='message' class='error'><p>" .__("Invalid Token. Please try again!", "fep")."</p></div>";
328 return;
329 }
330 if(!$this->isTokenValid($postedToken)){
331 // Actually This is not first form submission. First Submission Pass this condition and inserted into db.
332 echo "<div id='message' class='updated'><p>" .__("Page for \"Front End PM\" successfully created!", "fep")."</p></div>";
333 return;
334 }
335
336 $fep_page = array(
337 'post_title' => $title,
338 'post_name' => $slug,
339 'post_content' => '[front-end-pm]',
340 'post_status' => 'publish',
341 'post_type' => 'page'
342 );
343 $pageID = wp_insert_post( $fep_page );
344 if($pageID == 0){
345 echo "<div id='message' class='error'><p>" .__("Something wrong.Please try again to create page!", "fep")."</p></div>";
346 return;
347 } else {
348 echo "<div id='message' class='updated'><p>" .sprintf(__("Page <a href='%s'>%s </a> for \"Front End PM\" successfully created!", "fep"),get_permalink($pageID),get_the_title($pageID))."</p></div>";
349 return;}
350
351 }
352 }
353 /******************************************ADMIN SETTINGS PAGE END******************************************/
354
355 /******************************************USER SETTINGS PAGE BEGIN******************************************/
356 function dispUserPage()
357 {
358 global $user_ID;
359 if ($this->pmUserSave())
360 $this->success = __("Your settings have been saved!", "fep");
361 $viewUserOps = $this->getUserOps($user_ID); //Get current options
362 $prefs = "<p><strong>".__("Set your preferences below", "fep").":</strong></p>
363 <form id='fep-user-save-form' name='fep-user-save-form' method='post' action=''>
364 <input type='checkbox' name='allow_messages' value='true'";
365 if($viewUserOps['allow_messages'] == 'true')
366 $prefs .= "checked='checked'";
367 $prefs .= "/> <i>".__("Allow others to send me messages?", "fep")."</i><br/>
368
369 <input type='checkbox' name='allow_emails' value='true'";
370 if($viewUserOps['allow_emails'] == 'true')
371 $prefs .= "checked='checked'";
372 $prefs .= "/> <i>".__("Email me when I get new messages?", "fep")."</i><br/>
373
374 <input type='checkbox' name='allow_ann' value='true'";
375 if($viewUserOps['allow_ann'] == 'true')
376 $prefs .= "checked='checked'";
377 $prefs .= "/> <i>".__("Email me when New announcement is published?", "fep")."</i><br/>
378
379 <input class='button' type='submit' name='fep-user-save' value='".__("Save Options", "fep")."' />
380 </form>";
381 return $prefs;
382 }
383
384 function pmUserSave()
385 {
386 global $user_ID;
387 if (isset($_POST['fep-user-save']))
388 {
389 $saveUserOps = array( 'allow_emails' => $_POST['allow_emails'],
390 'allow_messages' => $_POST['allow_messages'],
391 'allow_ann' => $_POST['allow_ann']
392 );
393 update_user_meta($user_ID, $this->userOpsName, $saveUserOps);
394 return true;
395 }
396 return false;
397 }
398
399 function getUserOps($ID)
400 {
401 $pmUserOps = array( 'allow_emails' => 'true',
402 'allow_messages' => 'true',
403 'allow_ann' => 'true'
404 );
405
406 //Get old values if they exist
407 $userOps = get_user_meta($ID, $this->userOpsName, true);
408 if (!empty($userOps))
409 {
410 foreach ($userOps as $key => $option)
411 $pmUserOps[$key] = $option;
412 }
413
414 update_user_meta($ID, $this->userOpsName, $pmUserOps);
415 return $pmUserOps;
416 }
417 /******************************************USER SETTINGS PAGE END******************************************/
418
419 /******************************************NEW MESSAGE PAGE BEGIN******************************************/
420 function dispNewMsg()
421 {
422 global $user_ID;
423 $token = $this->getToken();
424 $adminOps = $this->getAdminOps();
425 if (isset($_GET['to'])){
426 $to = $_GET['to'];
427 }else{ $to = '';}
428 if (!$this->have_permission())
429 {
430 $this->error = __("You cannot send messages because you are blocked by administrator!", "fep");
431 return;
432 }
433 if ($this->adminOps['disable_new'] == 'on' && !current_user_can('manage_options'))
434 {
435 $this->error = __("Send new message is disabled for users!", "fep");
436 return;
437 }
438 if (!$this->isBoxFull($user_ID, $adminOps['num_messages'], '1'))
439 {
440 if(isset($_REQUEST['message_to'])){
441 $message_to = $_REQUEST['message_to'];
442 }
443 else{
444 $message_to = '';
445 }
446 if(isset($_REQUEST['message_top'])){
447 $message_top = $_REQUEST['message_top'];
448 }
449 else{
450 $message_top = '';
451 }
452 if(isset($_REQUEST['message_title'])){
453 $message_title = $_REQUEST['message_title'];
454 }
455 else{
456 $message_title = '';
457 }
458 if(isset($_REQUEST['message_content'])){
459 $message_content = $_REQUEST['message_content'];
460 }
461 else{
462 $message_content = '';
463 }
464 $newMsg = "<p><strong>".__("Create New Message", "fep").":</strong></p>";
465 $newMsg .= "<form name='message' action='".$this->actionURL."checkmessage' method='post'>".
466 __("To", "fep")."<font color='red'>*</font>: ";
467 if($this->adminOps['hide_autosuggest'] != 'on' || current_user_can('manage_options')) {
468 $newMsg .="<noscript>Username of recipient</noscript><br/>";
469 $newMsg .="<input type='hidden' id='search-qq' name='message_to' autocomplete='off' value='".$this->convertToUser($to)."".$message_to."' />
470 <input type='text' id='search-q' onkeyup='javascript:autosuggest(\"".$this->actionURL."\")' name='message_top' placeholder='Name of recipient' autocomplete='off' value='".$this->convertToDisplay($to)."".$message_top."' /><br/>
471 <div id='result'></div>";
472 } else {
473 $newMsg .="<br/><input type='text' name='message_to' placeholder='Username of recipient' autocomplete='off' value='".$this->convertToUser($to)."".$message_to."' /><br/>";}
474
475 $newMsg .= __("Subject", "fep")."<font color='red'>*</font>:<br/>
476 <input type='text' name='message_title' placeholder='Subject' maxlength='65' value='".$message_title."' /><br/>".
477 __("Message", "fep")."<font color='red'>*</font>:<br/>".$this->get_form_buttons()."<br/>
478 <textarea name='message_content' placeholder='Message Content'>".$message_content."</textarea>
479 <input type='hidden' name='message_from' value='".$user_ID."' />
480 <input type='hidden' name='message_date' value='".current_time('mysql')."' />
481 <input type='hidden' name='parent_id' value='0' />
482 <input type='hidden' name='token' value='".$token."' /><br/>
483 <input type='submit' id='submit' value='".__("Send Message", "fep")."' />
484 </form>";
485
486 return $newMsg;
487 }
488 else
489 {
490 $this->error = __("You cannot send messages because your message box is full! Please delete some messages.", "fep");
491 return;
492 }
493 }
494 /******************************************NEW MESSAGE PAGE END******************************************/
495
496 /******************************************READ MESSAGE PAGE BEGIN******************************************/
497 function dispReadMsg()
498 {
499 global $wpdb, $user_ID;
500
501 $pID = $_GET['id'];
502 $wholeThread = $this->getWholeThread($pID);
503 $token = $this->getToken();
504
505 $threadOut = "<p><strong>".__("Message Thread", "fep").":</strong></p>
506 <table><tr><th width='15%'>".__("Sender", "fep")."</th><th width='85%'>".__("Message", "fep")."</th></tr>";
507
508 foreach ($wholeThread as $post)
509 {
510 //Check for privacy errors first
511 if ($post->to_user != $user_ID && $post->from_user != $user_ID && !current_user_can( 'manage_options' ))
512 {
513 $this->error = __("You do not have permission to view this message!", "fep");
514 return;
515 }
516
517 //setup info for the reply form
518 if ($post->parent_id == 0) //If it is the parent message
519 {
520 $to = $post->from_user;
521 if ($to == $user_ID) //Make sure user doesn't send a message to himself
522 $to = $post->to_user;
523 $message_title = $this->output_filter($post->message_title);
524 if (substr_count($message_title, __("Re:", "fep")) < 1) //Prevent all the Re:'s from happening
525 $re = __("Re:", "fep");
526 else
527 $re = "";
528 }
529
530 $uData = get_userdata($post->from_user);
531 $threadOut .= "<tr><td><a href='".get_author_posts_url( $uData->ID )."'>".$uData->display_name."</a><br/><small>".$this->formatDate($post->date)."</small><br/>".get_avatar($post->from_user, 60)."</td>";
532
533 if ($post->parent_id == 0) //If it is the parent message
534 {
535 $threadOut .= "<td class='pmtext'><strong>".__("Subject", "fep").": </strong>".$this->output_filter($post->message_title)."<hr/>".apply_filters("comment_text", $this->autoembed($this->output_filter($post->message_contents)))."</td></tr>";
536 }
537 else
538 {
539 $threadOut .= "<td class='pmtext'>".apply_filters("comment_text", $this->autoembed($this->output_filter($post->message_contents)))."</td></tr>";
540 }
541 }
542
543 $threadOut .= "</table>";
544
545 //SHOW THE REPLY FORM
546 if ($this->have_permission()){
547 $threadOut .= "
548 <p><strong>".__("Add Reply", "fep").":</strong></p>
549 <form name='message' action='".$this->actionURL."checkmessage' method='post'>".
550 $this->get_form_buttons()."<br/>
551 <textarea name='message_content'></textarea>
552 <input type='hidden' name='message_to' value='".get_userdata($to)->user_login."' />
553 <input type='hidden' name='message_top' value='".get_userdata($to)->display_name."' />
554 <input type='hidden' name='message_title' value='".$re.$message_title."' />
555 <input type='hidden' name='message_from' value='".$user_ID."' />
556 <input type='hidden' name='message_date' value='".current_time('mysql')."' />
557 <input type='hidden' name='parent_id' value='".$pID."' />
558 <input type='hidden' name='token' value='".$token."' /><br/>
559 <input type='submit' value='".__("Send Message", "fep")."' />
560 </form>";
561 } else {
562 $this->error = __("You cannot send messages because you are blocked by administrator!", "fep");
563 }
564
565 if ($user_ID != $post->from_user) //Update only if the reader is not the sender ???
566 $wpdb->query($wpdb->prepare("UPDATE {$this->fepTable} SET message_read = 1 WHERE id = %d", $pID));
567
568 return $threadOut;
569 }
570
571 function dispReadMsg_admin()
572 {
573 global $wpdb, $user_ID;
574
575 $pID = $_GET['id'];
576 $wholeThread = $this->getWholeThread($pID);
577 $token = $this->getToken();
578
579 $threadOut = "<p><strong>".__("Message Thread", "fep").":</strong></p>
580 <table><tr><th width='15%'>".__("Sender", "fep")."</th><th width='85%'>".__("Message", "fep")."</th></tr>";
581
582 foreach ($wholeThread as $post)
583 {
584 //Check for privacy errors first
585 if (!current_user_can( 'manage_options' ))
586 {
587 $this->error = __("You do not have permission to view this message!", "fep");
588 return;
589 }
590
591 //setup info for the reply form
592 if ($post->parent_id == 0) //If it is the parent message
593 {
594 $to = $post->from_user;
595 if ($to == $user_ID) //Make sure user doesn't send a message to himself
596 $to = $post->to_user;
597 $message_title = $this->output_filter($post->message_title);
598 if (substr_count($message_title, __("Re:", "fep")) < 1) //Prevent all the Re:'s from happening
599 $re = __("Re:", "fep");
600 else
601 $re = "";
602 }
603
604 $uData = get_userdata($post->from_user);
605 $threadOut .= "<tr><td><a href='".get_author_posts_url( $uData->ID )."'>".$uData->display_name."</a><br/><small>".$this->formatDate($post->date)."</small><br/>".get_avatar($post->from_user, 60)."</td>";
606
607 if ($post->parent_id == 0) //If it is the parent message
608 {
609 $threadOut .= "<td class='pmtext'><strong>".__("Subject", "fep").": </strong>".$this->output_filter($post->message_title)."<hr/>".apply_filters("comment_text", $this->autoembed($this->output_filter($post->message_contents)))."</td></tr>";
610 }
611 else
612 {
613 $threadOut .= "<td class='pmtext'>".apply_filters("comment_text", $this->autoembed($this->output_filter($post->message_contents)))."</td></tr>";
614 }
615 }
616
617 //SHOW THE REPLY FORM
618 $threadOut .= "</table>
619 <p><strong>".__("Add Reply", "fep").":</strong></p>
620 <form name='message' action='".$this->actionURL."checkmessage' method='post'>".
621 $this->get_form_buttons()."<br/>
622 <textarea name='message_content'></textarea>
623 <input type='hidden' name='message_to' value='".get_userdata($to)->user_login."' />
624 <input type='hidden' name='message_top' value='".get_userdata($to)->display_name."' />
625 <input type='hidden' name='message_title' value='".$re.$message_title."' />
626 <input type='hidden' name='message_from' value='".$user_ID."' />
627 <input type='hidden' name='message_date' value='".current_time('mysql')."' />
628 <input type='hidden' name='parent_id' value='".$pID."' />
629 <input type='hidden' name='token' value='".$token."' /><br/>
630 <input type='submit' value='".__("Send Message", "fep")."' />
631 </form>";
632
633 return $threadOut;
634 }
635
636 function getWholeThread($id)
637 {
638 global $wpdb;
639 $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$this->fepTable} WHERE id = %d OR parent_id = %d ORDER BY id ASC", $id, $id));
640 return $results;
641 }
642
643 function convertToUser($to)
644 {
645 $user = get_user_by( 'login' , $to );
646 $result = $user->user_login;
647 return $result;
648 }
649 function convertToDisplay($to)
650 {
651 $user = get_user_by( 'login' , $to );
652 $result = $user->display_name;
653 return $result;
654 }
655 /******************************************READ MESSAGE PAGE END******************************************/
656
657 /******************************************CHECK MESSAGE PAGE BEGIN******************************************/
658 function dispCheckMsg()
659 {
660 global $wpdb, $user_ID;
661 $from = $_POST['message_from'];
662 if ($_POST['message_to']) {
663 $preTo = $_POST['message_to'];
664 } else {
665 $preTo = $_POST['message_top']; }
666 $to = $this->convertToID($preTo);
667 $title = $this->input_filter($_POST['message_title']);
668 $content = $this->input_filter($_POST['message_content']);
669 $parentID = $_POST['parent_id'];
670 $date = $_POST['message_date'];
671
672 $adminOps = $this->getAdminOps();
673 if ($to)
674 $toUserOps = $this->getUserOps($to);
675
676 //Check for errors first
677 if (!$to || !$title || !$content || ($from != $user_ID))
678 {
679 if (!$to)
680 $theError = __("You must enter a valid recipient!", "fep");
681 if (!$title)
682 $theError = __("You must enter a valid subject!", "fep");
683 if (!$content)
684 $theError = __("You must enter some message content!", "fep");
685 if ($from != $user_ID)
686 $theError = __("You do not have permission to send this message!", "fep");
687 $this->error = $theError;
688 return $this->dispNewMsg();
689 }
690 if ($toUserOps['allow_messages'] != 'true')
691 {
692 $this->error = __("This user does not want to receive messages!", "fep");
693 return;
694 }
695 if ($this->isBoxFull($to, $adminOps['num_messages'], $parentID))
696 {
697 $this->error = __("Your or Recipients Message Box Is Full!", "fep");
698 return;
699 }
700 if (!$this->have_permission())
701 {
702 $this->error = __("You cannot send messages because you are blocked by administrator!", "fep");
703 return;
704 }
705 $timeDelay = $this->TimeDelay($adminOps['time_delay']);
706 if ($timeDelay['diffr'] < $adminOps['time_delay'] && !current_user_can('manage_options'))
707 {
708 $this->error = sprintf(__("Please wait at least more %s to send another message!", "fep"),$timeDelay['time']);
709 return;
710 }
711 // Check if a form has been sent
712 $postedToken = filter_input(INPUT_POST, 'token');
713 if (empty($postedToken))
714 {
715 $this->error = __("Invalid Token. Please try again!", "fep");
716 return;
717 }
718 if(!$this->isTokenValid($postedToken)){
719 // Actually This is not first form submission. First Submission Pass this condition and inserted into db.
720 $this->success = __("Your message was successfully sent!", "fep");
721 return;
722 }
723
724 //If no errors then continue on
725 if ($parentID == 0)
726 $wpdb->query($wpdb->prepare("INSERT INTO {$this->fepTable} (from_user, to_user, message_title, message_contents, parent_id, last_sender, date, last_date) VALUES ( %d, %d, %s, %s, %d, %d, %s, %s )", $from, $to, $title, $content, $parentID, $from, $date, $date));
727 else
728 {
729 $wpdb->query($wpdb->prepare("INSERT INTO {$this->fepTable} (from_user, to_user, message_title, message_contents, parent_id, date) VALUES ( %d, %d, %s, %s, %d, %s)", $from, $to, $title, $content, $parentID, $date));
730 $wpdb->query($wpdb->prepare("UPDATE {$this->fepTable} SET message_read = 0,last_sender = %d,last_date = %s, to_del = 0, from_del = 0 WHERE id = %d", $from, $date, $parentID));
731 }
732
733 $this->success = __("Your message was successfully sent!", "fep");
734
735 $this->sendEmail($to, $from);
736
737 return;
738 }
739
740 function isBoxFull($to, $boxSize, $parentID)
741 {
742 global $wpdb;
743
744 $get_messages = $wpdb->get_results($wpdb->prepare("SELECT id FROM {$this->fepTable} 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));
745 $num = $wpdb->num_rows;
746
747 if ($boxSize == 0 || $num < $boxSize || $parentID != 0 || current_user_can('manage_options') || user_can( $to, 'manage_options' ))
748 return false;
749 else
750 return true;
751 }
752
753 function sendEmail($to, $from)
754 {
755 $toOptions = $this->getUserOps($to);
756 $notify = $toOptions['allow_emails'];
757 if ($notify == 'true')
758 {
759 $sendername = get_bloginfo("name");
760 $sendermail = get_bloginfo("admin_email");
761 $uData = get_userdata($from);
762 $sendfrom = $uData->user_login;
763 $headers = "MIME-Version: 1.0\r\n" .
764 "From: ".$sendername." "."<".$sendermail.">\n" .
765 "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\r\n";
766 $mailMessage = __("You have received a new message from", "fep")." ".$sendfrom.", ".__("follow this link to view it", "fep").": ".$this->pageURL;
767 $mUser = get_userdata($to);
768 $mailTo = $mUser->user_email;
769 wp_mail($mailTo, __("New Message", "fep"), $mailMessage);
770 }
771 }
772
773 function convertToID($preTo)
774 {
775 global $user_ID;
776 $user = get_user_by( 'login' , $preTo );
777 $result = $user->ID;
778 if ($result != $user_ID && $result)
779 return $result;
780 else
781 return 0;
782 }
783 /******************************************CHECK MESSAGE PAGE END******************************************/
784
785 /******************************************MESSAGE-BOX PAGE BEGIN******************************************/
786 function dispMsgBox()
787 {
788 global $wpdb, $user_ID;
789
790 $adminOps = $this->getAdminOps();
791 $numMsgs = $this->getUserNumMsgs();
792 if ($numMsgs)
793 {
794 $msgsOut = "<p><strong>".__("Your Messages", "fep").":</strong></p>";
795 $numPgs = $numMsgs / $adminOps['messages_page'];
796 if ($numPgs > 1)
797 {
798 $msgsOut .= "<p><strong>".__("Page", "fep").": </strong> ";
799 for ($i = 0; $i < $numPgs; $i++)
800 if ($_GET['pmpage'] != $i)
801 $msgsOut .= "<a href='".$this->actionURL."messagebox&pmpage=".$i."'>".($i+1)."</a> ";
802 else
803 $msgsOut .= "[<b>".($i+1)."</b>] ";
804 $msgsOut .= "</p>";
805 }
806
807 $msgsOut .= "<table><tr class='head'>
808 <th width='20%'>".__("Started By", "fep")."</th>
809 <th width='20%'>".__("To", "fep")."</th>
810 <th width='30%'>".__("Subject", "fep")."</th>
811 <th width='20%'>".__("Last Reply By", "fep")."</th>
812 <th width='10%'>".__("Delete", "fep")."</th></tr>";
813 $msgs = $this->getMsgs();
814 $a = 0;
815 foreach ($msgs as $msg)
816 {
817 if ($msg->message_read == 0 && $msg->last_sender != $user_ID)
818 $read = "<font color='#FF0000'>".__("Unread", "fep")."</font>";
819 else
820 $read = __("Read", "fep");
821 $uSend = get_userdata($msg->from_user);
822 $uLast = get_userdata($msg->last_sender);
823 $toUser = get_userdata($msg->to_user);
824 $msgsOut .= "<tr class='trodd".$a."'>";
825 if ($uSend->ID != $user_ID){
826 $msgsOut .= "<td><a href='".get_author_posts_url( $uSend->ID )."'>" .$uSend->display_name. "</a><br/><small>".$this->formatDate($msg->date)."</small></td>"; }
827 else {
828 $msgsOut .= "<td>" .$uSend->display_name. "<br/><small>".$this->formatDate($msg->date)."</small></td>"; }
829 if ($toUser->ID != $user_ID){
830 $msgsOut .= "<td><a href='".get_author_posts_url( $toUser->ID )."'>" .$toUser->display_name. "</a></td>";}
831 else {
832 $msgsOut .= "<td>" .$toUser->display_name. "</td>";}
833 $msgsOut .= "<td><a href='".$this->actionURL."viewmessage&id=".$msg->id."'>".$this->output_filter($msg->message_title)."</a><br/><small>".$read."</small></td>";
834 $msgsOut .= "<td>" .$uLast->display_name. "<br/><small>".$this->formatDate($msg->last_date)."</small></td>";
835 $msgsOut .= "<td><a href='".$this->actionURL."deletemessage&id=".$msg->id."' onclick='return confirm(\"".__('Are you sure?', 'fep')."\");'>".__("Delete", "fep")."</a></td>
836 </tr>";
837 //Alternate table colors
838 if ($a) $a = 0; else $a = 1;
839 }
840 $msgsOut .= "</table>";
841
842 return $msgsOut;
843 }
844 else
845 {
846 $this->error = __("Your message box is empty!", "fep");
847 return;
848 }
849 }
850
851 function getUserNumMsgs_admin()
852 {
853 global $wpdb, $user_ID;
854
855 $get_messages = $wpdb->get_results($wpdb->prepare("SELECT id FROM {$this->fepTable} WHERE to_user <> %d AND from_user <> %d AND message_read <> 2 AND parent_id = 0", $user_ID, $user_ID));
856 $num = $wpdb->num_rows;
857 return $num;
858 }
859 function dispMsgBox_admin()
860 {
861 global $wpdb, $user_ID;
862
863 $adminOps = $this->getAdminOps();
864 $numMsgs = $this->getUserNumMsgs_admin();
865 if ($numMsgs)
866 {
867 $msgsOut = "<p><strong>".__("All Messages", "fep").":</strong></p>";
868 $numPgs = $numMsgs / $adminOps['messages_page'];
869 if ($numPgs > 1)
870 {
871 $msgsOut .= "<p><strong>".__("Page", "fep").": </strong> ";
872 for ($i = 0; $i < $numPgs; $i++)
873 if ($_GET['apmpage'] != $i)
874 $msgsOut .= "<a href='".$this->actionURL."viewallmgs&apmpage=".$i."'>".($i+1)."</a> ";
875 else
876 $msgsOut .= "[<b>".($i+1)."</b>] ";
877 $msgsOut .= "</p>";
878 }
879
880 $msgsOut .= "<table><tr class='head'>
881 <th width='20%'>".__("Started By", "fep")."</th>
882 <th width='20%'>".__("To", "fep")."</th>
883 <th width='30%'>".__("Subject", "fep")."</th>
884 <th width='20%'>".__("Last Reply By", "fep")."</th>
885 <th width='10%'>".__("Delete", "fep")."</th></tr>";
886 $msgs = $this->getMsgs_admin();
887 $a = 0;
888 foreach ($msgs as $msg)
889 {
890 if ($msg->message_read == 0 && $msg->last_sender != $user_ID)
891 $read = "<font color='#FF0000'>".__("Unread", "fep")."</font>";
892 else
893 $read = __("Read", "fep");
894 $uSend = get_userdata($msg->from_user);
895 $uLast = get_userdata($msg->last_sender);
896 $toUser = get_userdata($msg->to_user);
897 $msgsOut .= "<tr class='trodd".$a."'>";
898 $msgsOut .= "<td><a href='".get_author_posts_url( $uSend->ID )."'>" .$uSend->display_name. "</a><br/><small>".$this->formatDate($msg->date)."</small></td>";
899 $msgsOut .= "<td><a href='".get_author_posts_url( $toUser->ID )."'>" .$toUser->display_name. "</a></td>";
900 $msgsOut .= "<td><a href='".$this->actionURL."viewmessageadmin&id=".$msg->id."'>".$this->output_filter($msg->message_title)."</a><br/><small>".$read."</small></td>";
901 $msgsOut .= "<td>" .$uLast->display_name. "<br/><small>".$this->formatDate($msg->last_date)."</small></td>";
902 $msgsOut .= "<td><a href='".$this->actionURL."deletemessageadmin&id=".$msg->id."' onclick='return confirm(\"".__('Are you sure?', 'fep')."\");'>".__("Delete", "fep")."</a></td>
903 </tr>";
904 //Alternate table colors
905 if ($a) $a = 0; else $a = 1;
906 }
907 $msgsOut .= "</table>";
908
909 return $msgsOut;
910 }
911 else
912 {
913 $this->error = __("Message box is empty!", "fep");
914 return;
915 }
916 }
917
918 function getMsgs()
919 {
920 global $wpdb, $user_ID;
921 if (isset($_GET['pmpage'])){
922 $page = $_GET['pmpage'];
923 }else{$page = 0;}
924 $adminOps = $this->getAdminOps();
925 $start = $page * $adminOps['messages_page'];
926 $end = $adminOps['messages_page'];
927
928 $get_messages = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$this->fepTable} 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));
929
930 return $get_messages;
931 }
932
933 function getMsgs_admin()
934 {
935 global $wpdb, $user_ID;
936 if (isset($_GET['apmpage'])){
937 $page = $_GET['apmpage'];
938 }else{$page = 0;}
939 $adminOps = $this->getAdminOps();
940 $start = $page * $adminOps['messages_page'];
941 $end = $adminOps['messages_page'];
942 $get_messages = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$this->fepTable} WHERE to_user <> %d AND from_user <> %d AND parent_id = 0 AND message_read <> 2 ORDER BY last_date DESC LIMIT %d, %d", $user_ID, $user_ID, $start, $end));
943
944 return $get_messages;
945 }
946 /******************************************MESSAGE-BOX PAGE END******************************************/
947
948 /******************************************DELETE PAGE BEGIN******************************************/
949 function dispDelMsg()
950 {
951 global $wpdb, $user_ID;
952
953 $delID = $_GET['id'];
954 $toDuser = $wpdb->get_var($wpdb->prepare("SELECT to_user FROM {$this->fepTable} WHERE id = %d", $delID));
955 $toDel = $wpdb->get_var($wpdb->prepare("SELECT to_del FROM {$this->fepTable} WHERE id = %d", $delID));
956 $fromDel = $wpdb->get_var($wpdb->prepare("SELECT from_del FROM {$this->fepTable} WHERE id = %d", $delID));
957
958 if ($toDuser == $user_ID)
959 {
960 if ($fromDel == 0)
961 $wpdb->query($wpdb->prepare("UPDATE {$this->fepTable} SET to_del = 1 WHERE id = %d", $delID));
962 else
963 $wpdb->query($wpdb->prepare("DELETE FROM {$this->fepTable} WHERE id = %d OR parent_id = %d", $delID, $delID));
964 }
965 else
966 {
967 if ($toDel == 0)
968 $wpdb->query($wpdb->prepare("UPDATE {$this->fepTable} SET from_del = 1 WHERE id = %d", $delID));
969 else
970 $wpdb->query($wpdb->prepare("DELETE FROM {$this->fepTable} WHERE id = %d OR parent_id = %d", $delID, $delID));
971 }
972
973 $this->success = __("Your message was successfully deleted!", "fep");
974
975 return;
976 }
977
978 function dispDelMsg_admin()
979 {
980 global $wpdb, $user_ID;
981
982 $delID = $_GET['id'];
983
984 if (current_user_can('manage_options')) {
985 $wpdb->query($wpdb->prepare("DELETE FROM {$this->fepTable} WHERE id = %d OR parent_id = %d", $delID, $delID)); }
986
987 $this->success = __("Message was successfully deleted!", "fep");
988
989 return;
990 }
991 /******************************************DELETE PAGE END******************************************/
992
993 /******************************************VIEW ANNOUNCEMENTS BEGIN******************************************/
994
995 function dispAnnouncement()
996 {
997 global $wpdb, $user_ID;
998 $announcements = $this->getAnnouncements();
999 $num = $wpdb->num_rows;
1000
1001 if ($this->deleteAnnouncement()) //Deleting an announcement?
1002 {
1003 $this->success = __("The announcement was successfully deleted!", "fep");
1004 return;
1005 }
1006
1007 if (!$num) //Just viewing announcements
1008 {
1009 $announce = "<p><strong>".__("Announcements", "fep").":</strong></p>";
1010 if (current_user_can('manage_options'))
1011 {
1012 $announce .= $this->dispAnnounceForm();
1013 }
1014 $this->error = __("There are no announcements!", "fep");
1015 }
1016 else
1017 {
1018 $announce = "<p><strong>".__("Announcements", "fep").":</strong></p>";
1019 if (current_user_can('manage_options'))
1020 {
1021 $announce .= $this->dispAnnounceForm();
1022 }
1023 $announce .= "<table>";
1024 $a = 0;
1025 foreach ($announcements as $announcement)
1026 {
1027 $announce .= "<tr class='trodd".$a."'><td class='pmtext'><strong>".__("Subject", "fep").":</strong> ".$this->output_filter($announcement->message_title).
1028 "<br/><strong>".__("Date", "fep").":</strong> ".$this->formatDate($announcement->date);
1029 if (current_user_can('manage_options')) {
1030 $announce .= "<br/><strong>".__("Added by", "fep").":</strong> ".get_userdata($announcement->from_user)->display_name;
1031 $announce .= "<br/><a href='".$this->actionURL."viewannouncements&del=1&id=".$announcement->id."' onclick='return confirm(\"".__('Are you sure?', 'fep')."\");'>".__("Delete", "fep")."</a>"; }
1032 $announce .= "<hr/>";
1033 $announce .= "<strong>".__("Message", "fep").":</strong><br/>".apply_filters("comment_text", $this->output_filter($announcement->message_contents))."</td></tr>";
1034 if ($a) $a = 0; else $a = 1; //Alternate table colors
1035 }
1036 $announce .= "</table>";
1037 }
1038
1039 return $announce;
1040 }
1041
1042 function dispAnnounceForm()
1043 {
1044 global $user_ID;
1045 $token = $this->getToken();
1046 if(isset($_REQUEST['message_title'])){
1047 $message_title = $_REQUEST['message_title'];
1048 }
1049 else{
1050 $message_title = '';
1051 }
1052 if(isset($_REQUEST['message_content'])){
1053 $message_content = $_REQUEST['message_content'];
1054 }
1055 else{
1056 $message_content = '';
1057 }
1058 $form = "<p>".__("Add a new announcement below", "fep")."</p>
1059 <form name='message' action='".$this->actionURL."addannouncement' method='post'>
1060 ".__("Subject", "fep").":<br/>
1061 <input type='text' name='message_title' value='".$message_title."' /><br/>".
1062 $this->get_form_buttons()."<br/>
1063 <textarea name='message_content'>".$message_content."</textarea>
1064 <input type='hidden' name='message_date' value='".current_time('mysql')."' />
1065 <input type='hidden' name='message_from' value='".$user_ID."' />
1066 <input type='hidden' name='token' value='".$token."' /><br/>
1067 <input type='submit' name='add-announcement' value='".__("Submit", "fep")."' />
1068 </form>";
1069
1070 return $form;
1071 }
1072
1073 function getAnnouncements()
1074 {
1075 global $wpdb; //message_read = 2 indicates that the msg is an announcement :)
1076 $results = $wpdb->get_results("SELECT * FROM {$this->fepTable} WHERE message_read = 2 ORDER BY id DESC");
1077 return $results;
1078 }
1079
1080 function getAnnouncementsNum()
1081 {
1082 global $wpdb; //message_read = 2 indicates that the msg is an announcement :)
1083 $results = $wpdb->get_results("SELECT id FROM {$this->fepTable} WHERE message_read = 2 ORDER BY id DESC");
1084 return $wpdb->num_rows;
1085 }
1086 function getAnnouncementsNum_btn(){
1087 if ($this->getAnnouncementsNum()){
1088 $newmgs = " (<font color='red'>";
1089 $newmgs .= $this->getAnnouncementsNum();
1090 $newmgs .="</font>)";
1091 } else {
1092 $newmgs ="";}
1093
1094 return $newmgs;
1095 }
1096
1097 function addAnnouncement()
1098 {
1099 global $wpdb,$user_ID;
1100 $adminOps = $this->getAdminOps();
1101 $title = $this->input_filter($_POST['message_title']);
1102 $contents = $this->input_filter($_POST['message_content']);
1103 $from = $_POST['message_from'];
1104 $date = $_POST['message_date'];
1105 $read = '2';
1106
1107 if (!$title || !$contents || $from != $user_ID)
1108 {
1109 if (!$title)
1110 $theError = __("You must enter a valid subject!", "fep");
1111 if (!$contents)
1112 $theError = __("You must enter some content!", "fep");
1113 if ($from != $user_ID)
1114 $theError = __("Please try again!", "fep");
1115 $this->error = $theError;
1116 return $this->dispAnnounceForm();
1117 }
1118
1119 // Check if a form has been sent
1120 $postedToken = filter_input(INPUT_POST, 'token');
1121 if (empty($postedToken))
1122 {
1123 $this->error = __("Invalid Token. Please try again!", "fep");
1124 return;
1125 }
1126 if(!$this->isTokenValid($postedToken)){
1127 // Actually This is not first form submission. First Submission Pass this condition and inserted into db.
1128 $this->success = __("The announcement was successfully added!", "fep");
1129 return;
1130 }
1131 //if nothing wrong continue
1132 $wpdb->query($wpdb->prepare("INSERT INTO {$this->fepTable} (from_user, message_title, message_contents, date, message_read) VALUES ( %s, %s, %s, %s, %d )",$from, $title, $contents, $date, $read));
1133
1134 if ($adminOps['notify_ann'] == 'on') {
1135 $this->notify_users($title);
1136 $this->success = __("The announcement was successfully added and sent email to all users!", "fep");
1137 return;
1138 } else {
1139 $this->success = __("The announcement was successfully added!", "fep");
1140 return;
1141 }
1142 }
1143
1144 function deleteAnnouncement()
1145 {
1146 global $wpdb;
1147 if (isset($_GET['id'])){$delID = $_GET['id'];}
1148 if (isset($_GET['del'])){$delm = $_GET['del'];}else{ $delm = ''; }
1149 if (current_user_can('manage_options') && $delm) //Make sure only admins can delete announcements
1150 {
1151 $wpdb->query($wpdb->prepare("DELETE FROM {$this->fepTable} WHERE id = %d", $delID));
1152 return true;
1153 }
1154 return false;
1155 }
1156
1157 //Mass emails when announcement is created
1158 function notify_users($title) {
1159
1160 $domain_name = preg_replace('/^www\./','',$_SERVER['SERVER_NAME']);
1161 $usersarray = get_users("orderby=ID");
1162 $adminOps = $this->getAdminOps();
1163 $to = $adminOps['ann_to'];
1164 $from = 'noreply@'.$domain_name;
1165
1166 $bcc = array();
1167 foreach ($usersarray as $user) {
1168 $toOptions = $this->getUserOps($user->ID);
1169 $notify = $toOptions['allow_ann'];
1170 if (in_array($notify == 'true',$usersarray)){
1171 $bcc[] = $user->user_email;
1172 }
1173 }
1174
1175 $chunked_bcc = array_chunk($bcc, 25);
1176
1177 $subject = "" . get_bloginfo("name").": New Announcement";
1178 $message = "A new Announcement is Published in \r\n";
1179 $message .= get_bloginfo("name")."\r\n";
1180 $message .= "Title: ".$title. "\r\n";
1181 $message .= "Please Click the following link to view full Announcement. \r\n";
1182 $message .= $this->actionURL."viewannouncements \r\n";
1183 foreach($chunked_bcc as $bcc_chunk){
1184 $headers = array();
1185 $headers['From'] = 'From: '.get_bloginfo("name").'<'.$from.'>';
1186 $headers['Bcc'] = 'Bcc: '.implode(', ', $bcc_chunk);
1187 wp_mail($to , $subject, $message, $headers);
1188 }
1189 return;
1190 }
1191 /******************************************VIEW ANNOUNCEMENTS END******************************************/
1192
1193 /******************************************MAIN DISPLAY BEGIN******************************************/
1194 function dispHeader()
1195 {
1196 global $user_ID, $user_login;
1197
1198 $numNew = $this->getNewMsgs();
1199 $numAnn = $this->getAnnouncementsNum();
1200 $msgBoxSize = $this->getUserNumMsgs();
1201 $adminOps = $this->getAdminOps();
1202 if ($adminOps['num_messages'] == 0 || current_user_can('manage_options'))
1203 $msgBoxTotal = __("Unlimited", "fep");
1204 else
1205 $msgBoxTotal = $adminOps['num_messages'];
1206
1207 $header = "<div id='fep-wrapper'>";
1208 $header .= "<div id='fep-header'>";
1209 $header .= get_avatar($user_ID, 55)."<p><strong>".__("Welcome", "fep").": ".$this->convertToDisplay($user_login)."</strong><br/>";
1210 $header .= __("You have", "fep")." (<font color='red'>".$numNew."</font>) ".__("new messages", "fep").
1211 " ".__("and", "fep")." (<font color='red'>".$numAnn."</font>) ".__("announcement(s)", "fep")."<br/>";
1212 if ($msgBoxTotal == __("Unlimited", "fep") || $msgBoxSize < $msgBoxTotal)
1213 $header .= __("Message box size", "fep").": ".$msgBoxSize." ".__("of", "fep")." ".$msgBoxTotal."</p>";
1214 else
1215 $header .= "<font color='red'>".__("Your Message Box Is Full! Please delete some messages.", "fep")."</font></p>";
1216 $header .= "</div>";
1217 return $header;
1218 }
1219
1220 function dispMenu()
1221 {
1222
1223 $numNew = $this->getNewMsgs_btn();
1224 $numNewadm = $this->getNewMsgs_admin();
1225 $numAnn = $this->getAnnouncementsNum_btn();
1226
1227 $menu = "<div id='fep-menu'>";
1228 $menu .= "<a class='fep-button' href='".$this->pageURL."'>".__("Message Box".$numNew."", "fep")."</a>";
1229 $menu .= "<a class='fep-button' href='".$this->actionURL."viewannouncements'>".__("Announcements".$numAnn."", "fep")."</a>";
1230 $menu .= "<a class='fep-button' href='".$this->actionURL."newmessage'>".__("New Message", "fep")."</a>";
1231 if($this->adminOps['hide_directory'] != 'on' || current_user_can('manage_options'))
1232 $menu .= "<a class='fep-button' href='".$this->actionURL."directory'>".__("Directory", "fep")."</a>";
1233 $menu .= "<a class='fep-button' href='".$this->actionURL."settings'>".__("Settings", "fep")."</a>";
1234 if(current_user_can('manage_options'))
1235 $menu .= "<a class='fep-button' href='".$this->actionURL."viewallmgs'>".__("Other's Message".$numNewadm."", "fep") . "</a>";
1236 $menu .="</div>";
1237 $menu .= "<div id='fep-content'>";
1238 return $menu;
1239 }
1240
1241 function dispNotify()
1242 {
1243 if ($this->success != ""){
1244 $notify = "<div id='success'>".$this->success."</div>";
1245 } else if ($this->error != "") {
1246 $notify = "<div id='error'>".$this->error."</div>";
1247 }
1248 return $notify;
1249 }
1250
1251 function dispFooter()
1252 {
1253 $footer = "</div>"; //End content
1254 //Maybe Add Notify
1255 if ($this->error != "" || $this->success != "")
1256 $footer .= $this->dispNotify();
1257
1258 if($this->adminOps['hide_branding'] != 'on')
1259 $footer .= "<div id='fep-footer'><a href='http://www.banglardokan.com/blog/recent/project/front-end-pm-2215/'>Front End PM ".$this->get_version()."</a></div>";
1260
1261 $footer .= "</div>"; //End main wrapper
1262
1263 return $footer;
1264 }
1265
1266 function dispDirectory()
1267 {
1268 if($this->adminOps['hide_directory'] == 'on' && !current_user_can('manage_options'))
1269 return;
1270 $users = $this->get_users();
1271 $result = count_users();
1272 $total = $result['total_users'];
1273 $adminOps = $this->getAdminOps();
1274 if ($total)
1275 {
1276 $directory = "<p><strong>".__("Total Users", "fep").": (".$total.")</strong></p>";
1277 $numPgs = $total / $adminOps['user_page'];
1278 if ($numPgs > 1)
1279 {
1280 $directory .= "<p><strong>".__("Page", "fep").": </strong> ";
1281 for ($i = 0; $i < $numPgs; $i++)
1282 if ($_GET['upage'] != $i)
1283 $directory .= "<a href='".$this->actionURL."directory&upage=".$i."'>".($i+1)."</a> ";
1284 else
1285 $directory .= "[<b>".($i+1)."</b>] ";
1286 $directory .= "</p>";
1287 }
1288 $directory .= "<table><tr class='head'>
1289 <th width='50%'>".__("User", "fep")."</th>
1290 <th width='50%'>".__("Send Message", "fep")."</th></tr>";
1291 $a=0;
1292
1293 foreach($users as $u)
1294 {
1295 $directory .= "<tr class='trodd".$a."'><td>".$u->display_name."</td>";
1296 $directory .= "<td><a href='".$this->actionURL."newmessage&to=".$u->user_login."'>".__("Send Message", "fep")."</a></td></tr>";
1297 if ($a) $a = 0; else $a = 1;
1298 }
1299 $directory .= "</table>";
1300
1301 return $directory;
1302 }
1303 else
1304 {
1305 $this->error = __("No User!", "fep");
1306 return;
1307 }
1308 }
1309
1310 //Display the proper contents
1311 function displayAll()
1312 {
1313 global $user_ID,$wpdb;
1314 if ($user_ID)
1315 {
1316 //Finish the setup since these wouldn't work in the constructor
1317 $this->userOps = $this->getUserOps($user_ID);
1318 $this->setPageURLs();
1319
1320 //Add header
1321 $out = $this->dispHeader();
1322
1323 //Add Menu
1324 $out .= $this->dispMenu();
1325
1326 //Start the guts of the display
1327 if (isset($_GET['fepaction'])){
1328 $switch = $_GET['fepaction'];
1329 }else{ $switch = '';}
1330 switch ($switch)
1331 {
1332 case 'newmessage':
1333 $out .= $this->dispNewMsg();
1334 break;
1335 case 'checkmessage':
1336 $out .= $this->dispCheckMsg();
1337 break;
1338 case 'viewmessage':
1339 $out .= $this->dispReadMsg();
1340 break;
1341 case 'viewmessageadmin':
1342 if (current_user_can('manage_options'))
1343 $out .= $this->dispReadMsg_admin();
1344 else
1345 $out .= $this->dispReadMsg();
1346 break;
1347 case 'deletemessage':
1348 $out .= $this->dispDelMsg();
1349 break;
1350 case 'deletemessageadmin':
1351 if (current_user_can('manage_options'))
1352 $out .= $this->dispDelMsg_admin();
1353 else
1354 $out .= $this->dispDelMsg();
1355 break;
1356 case 'directory':
1357 if($this->adminOps['hide_directory'] != 'on' || current_user_can('manage_options'))
1358 $out .= $this->dispDirectory();
1359 else
1360 $out .= $this->dispMsgBox();
1361 break;
1362 case 'settings':
1363 $out .= $this->dispUserPage();
1364 break;
1365 case 'viewannouncements':
1366 $out .= $this->dispAnnouncement();
1367 break;
1368 case 'addannouncement':
1369 $out .= $this->addAnnouncement();
1370 break;
1371 case 'viewallmgs':
1372 if (current_user_can('manage_options'))
1373 $out .= $this->dispMsgBox_admin();
1374 else
1375 $out .= $this->dispMsgBox();
1376 break;
1377 default: //Message box is shown by Default
1378 $out .= $this->dispMsgBox();
1379 break;
1380 }
1381
1382 //Add footer
1383 $out .= $this->dispFooter();
1384 }
1385 else
1386 {
1387 $out = "<p><strong>".__("You must be logged-in to view your message.", "fep")."</strong></p>";
1388 }
1389 return $out;
1390 }
1391 /******************************************MAIN DISPLAY END******************************************/
1392
1393 /******************************************MISC. FUNCTIONS BEGIN******************************************/
1394
1395 /**
1396 * Creates a token usable in a form
1397 * @return string
1398 */
1399 function session(){
1400 if(!isset($_SESSION)) {
1401 session_start();
1402 }
1403 }
1404
1405 function getToken(){
1406 $token = sha1(mt_rand());
1407 if(!isset($_SESSION['tokens'])){
1408 $_SESSION['tokens'] = array($token => 1);
1409 }else{
1410 $_SESSION['tokens'][$token] = 1;
1411 }
1412 return $token;
1413 }
1414
1415 /**
1416 * Check if a token is valid. Removes it from the valid tokens list
1417 * @param string $token The token
1418 * @return bool
1419 */
1420 function isTokenValid($token){
1421 if(!empty($_SESSION['tokens'][$token])){
1422 unset($_SESSION['tokens'][$token]);
1423 return true;
1424 }
1425 return false;
1426 }
1427
1428 //Check is user blocked by admin
1429 function have_permission(){
1430 global $current_user;
1431 $adminOps = $this->getAdminOps();
1432 $wpusers = (array) explode(',', $adminOps['have_permission']);
1433 $valid_wpusers = array();
1434 foreach($wpusers as $wpuser){
1435 $wpuser = trim($wpuser);
1436 if($wpuser!=''){
1437 $user = get_user_by('login', $wpuser);
1438 if($user){
1439 $valid_wpusers[] = $user->ID;
1440 }
1441 $valid_wpusers = array_unique($valid_wpusers);
1442 if(in_array($current_user->ID, $valid_wpusers)){
1443 return false;
1444 }
1445 } }
1446 return true;
1447 }
1448
1449 function get_users()
1450 {
1451 global $wpdb;
1452 if (isset($_GET['upage'])){
1453 $page = $_GET['upage'];
1454 }else{$page = 0;}
1455 $adminOps = $this->getAdminOps();
1456 $start = $page * $adminOps['user_page'];
1457 $end = $adminOps['user_page'];
1458 $users = $wpdb->get_results($wpdb->prepare("SELECT display_name, user_login, ID FROM $wpdb->users ORDER BY display_name ASC LIMIT %d, %d",$start,$end));
1459 return $users;
1460 }
1461
1462 function get_form_buttons()
1463 {
1464 $button = '
1465 <a title="'.__("Bold", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[b]", "[/b]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/b.png" /></a>
1466 <a title="'.__("Italic", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[i]", "[/i]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/i.png" /></a>
1467 <a title="'.__("Underline", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[u]", "[/u]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/u.png" /></a>
1468 <a title="'.__("Strikethrough", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[s]", "[/s]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/s.png" /></a>
1469 <a title="'.__("Code", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[code]", "[/code]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/code.png" /></a>
1470 <a title="'.__("Quote", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[quote]", "[/quote]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/quote.png" /></a>
1471 <a title="'.__("List", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[list]", "[/list]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/list.png" /></a>
1472 <a title="'.__("List item", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[*]", "", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/li.png" /></a>
1473 <a title="'.__("Link", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[url]", "[/url]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/url.png" /></a>
1474 <a title="'.__("Image", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[img]", "[/img]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/img.png" /></a>
1475 <a title="'.__("Email", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[email]", "[/email]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/email.png" /></a>
1476 <a title="'.__("Add Hex Color", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[color=#]", "[/color]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/color.png" /></a>
1477 <a title="'.__("Embed", "fep").'" href="javascript:void(0);" onclick=\'surroundTheText("[embed]", "[/embed]", document.forms.message.message_content); return false;\'><img src="'.$this->pluginURL.'/images/bbc/embed.png" /></a>';
1478
1479 return $button;
1480 }
1481
1482 function output_filter($string)
1483 {
1484 $parser = new fepBBCParser();
1485 $html = stripslashes($parser->bbc2html($string));
1486 $htmlncr = ent2ncr($html);
1487 return stripslashes ($htmlncr);
1488 }
1489
1490 function input_filter($string)
1491 {
1492 $newStr = esc_attr($string);
1493 return strip_tags(esc_sql($newStr));
1494 }
1495
1496 function getUserNumMsgs()
1497 {
1498 global $wpdb, $user_ID;
1499 $get_messages = $wpdb->get_results($wpdb->prepare("SELECT id FROM {$this->fepTable} 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));
1500 $num = $wpdb->num_rows;
1501 return $num;
1502 }
1503
1504 function formatDate($date)
1505 {
1506 $now = current_time('mysql');
1507 //return date('M d, h:i a', strtotime($date));
1508 return human_time_diff(strtotime($date),strtotime($now)).' ago';
1509 }
1510
1511 function TimeDelay($DeTime)
1512 {
1513 global $wpdb, $user_ID;
1514 $now = current_time('mysql');
1515 $Dtime = $DeTime * 60;
1516 $Prev = $wpdb->get_var($wpdb->prepare("SELECT last_date FROM {$this->fepTable} WHERE parent_id = 0 AND last_sender = %d ORDER BY last_date DESC LIMIT 1", $user_ID));
1517 $diff = strtotime($now) - strtotime($Prev);
1518 $diffr = $diff/60;
1519 $next = strtotime($Prev) + $Dtime;
1520 $Ntime = human_time_diff(strtotime($now),$next);
1521 return array('diffr' => $diffr, 'time' => $Ntime);
1522 }
1523
1524 function is_positive($str) {
1525 return (is_numeric($str) && $str > 0 && $str == round($str));
1526 }
1527
1528 function getNewMsgs()
1529 {
1530 global $wpdb, $user_ID;
1531
1532 $get_pms = $wpdb->get_results($wpdb->prepare("SELECT id FROM {$this->fepTable} 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));
1533 return $wpdb->num_rows;
1534 }
1535 function getNewMsgs_btn(){
1536 if ($this->getNewMsgs()){
1537 $newmgs = " (<font color='red'>";
1538 $newmgs .= $this->getNewMsgs();
1539 $newmgs .="</font>)";
1540 } else {
1541 $newmgs = "";}
1542
1543 return $newmgs;
1544 }
1545
1546
1547 function getNewMsgs_admin()
1548 {
1549 global $wpdb, $user_ID;
1550
1551 $get_pmss = $wpdb->get_results($wpdb->prepare("SELECT id FROM {$this->fepTable} WHERE to_user <> %d AND from_user <> %d AND last_sender <> %d AND message_read = 0 AND parent_id = 0", $user_ID, $user_ID, $user_ID));
1552 if ($wpdb->num_rows){
1553 $newmgs = " (<font color='red'>";
1554 $newmgs .= $wpdb->num_rows;
1555 $newmgs .="</font>)";
1556 } else {
1557 $newmgs ="";}
1558
1559 return $newmgs;
1560 }
1561
1562 function autoembed($string)
1563 {
1564 global $wp_embed;
1565 if (is_object($wp_embed))
1566 return $wp_embed->autoembed($string);
1567 else
1568 return $string;
1569 }
1570
1571 function get_version()
1572 {
1573 $plugin_data = implode('', file($this->pluginDir."front-end-pm.php"));
1574 if (preg_match("|Version:(.*)|i", $plugin_data, $version))
1575 $version = $version[1];
1576 return $version;
1577 }
1578 /******************************************MISC. FUNCTIONS END******************************************/
1579 } //END CLASS
1580 } //ENDIF
1581 ?>