PluginProbe
Front End PM / 1.3
Front End PM v1.3
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.3, at fep-class.php

1,571 lines 65.8 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 $message_to = ( isset( $_REQUEST['message_to'] ) ) ? $_REQUEST['message_to']: '';
441 $message_top = ( isset( $_REQUEST['message_top'] ) ) ? $_REQUEST['message_top']: '';
442 $message_title = ( isset( $_REQUEST['message_title'] ) ) ? $_REQUEST['message_title']: '';
443 $message_content = ( isset( $_REQUEST['message_content'] ) ) ? $_REQUEST['message_content']: '';
444
445 $newMsg = "<p><strong>".__("Create New Message", "fep").":</strong></p>";
446 $newMsg .= "<form name='message' action='".$this->actionURL."checkmessage' method='post'>".
447 __("To", "fep")."<font color='red'>*</font>: ";
448 if($this->adminOps['hide_autosuggest'] != 'on' || current_user_can('manage_options')) {
449 $newMsg .="<noscript>Username of recipient</noscript><br/>";
450 $newMsg .="<input type='hidden' id='search-qq' name='message_to' autocomplete='off' value='".$this->convertToUser($to)."".$message_to."' />
451 <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/>
452 <div id='result'></div>";
453 } else {
454 $newMsg .="<br/><input type='text' name='message_to' placeholder='Username of recipient' autocomplete='off' value='".$this->convertToUser($to)."".$message_to."' /><br/>";}
455
456 $newMsg .= __("Subject", "fep")."<font color='red'>*</font>:<br/>
457 <input type='text' name='message_title' placeholder='Subject' maxlength='65' value='".$message_title."' /><br/>".
458 __("Message", "fep")."<font color='red'>*</font>:<br/>".$this->get_form_buttons()."<br/>
459 <textarea name='message_content' placeholder='Message Content'>".$message_content."</textarea>
460 <input type='hidden' name='message_from' value='".$user_ID."' />
461 <input type='hidden' name='parent_id' value='0' />
462 <input type='hidden' name='token' value='".$token."' /><br/>
463 <input type='submit' id='submit' value='".__("Send Message", "fep")."' />
464 </form>";
465
466 return $newMsg;
467 }
468 else
469 {
470 $this->error = __("You cannot send messages because your message box is full! Please delete some messages.", "fep");
471 return;
472 }
473 }
474 /******************************************NEW MESSAGE PAGE END******************************************/
475
476 /******************************************READ MESSAGE PAGE BEGIN******************************************/
477 function dispReadMsg()
478 {
479 global $wpdb, $user_ID;
480
481 $pID = $_GET['id'];
482 $wholeThread = $this->getWholeThread($pID);
483 $token = $this->getToken();
484
485 $threadOut = "<p><strong>".__("Message Thread", "fep").":</strong></p>
486 <table><tr><th width='15%'>".__("Sender", "fep")."</th><th width='85%'>".__("Message", "fep")."</th></tr>";
487
488 foreach ($wholeThread as $post)
489 {
490 //Check for privacy errors first
491 if ($post->to_user != $user_ID && $post->from_user != $user_ID && !current_user_can( 'manage_options' ))
492 {
493 $this->error = __("You do not have permission to view this message!", "fep");
494 return;
495 }
496
497 //setup info for the reply form
498 if ($post->parent_id == 0) //If it is the parent message
499 {
500 $to = $post->from_user;
501 if ($to == $user_ID) //Make sure user doesn't send a message to himself
502 $to = $post->to_user;
503 $message_title = $this->output_filter($post->message_title);
504 if (substr_count($message_title, __("Re:", "fep")) < 1) //Prevent all the Re:'s from happening
505 $re = __("Re:", "fep");
506 else
507 $re = "";
508 }
509
510 $uData = get_userdata($post->from_user);
511 $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>";
512
513 if ($post->parent_id == 0) //If it is the parent message
514 {
515 $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>";
516 }
517 else
518 {
519 $threadOut .= "<td class='pmtext'>".apply_filters("comment_text", $this->autoembed($this->output_filter($post->message_contents)))."</td></tr>";
520 }
521 }
522
523 $threadOut .= "</table>";
524
525 //SHOW THE REPLY FORM
526 if ($this->have_permission()){
527 $threadOut .= "
528 <p><strong>".__("Add Reply", "fep").":</strong></p>
529 <form name='message' action='".$this->actionURL."checkmessage' method='post'>".
530 $this->get_form_buttons()."<br/>
531 <textarea name='message_content'></textarea>
532 <input type='hidden' name='message_to' value='".get_userdata($to)->user_login."' />
533 <input type='hidden' name='message_top' value='".get_userdata($to)->display_name."' />
534 <input type='hidden' name='message_title' value='".$re.$message_title."' />
535 <input type='hidden' name='message_from' value='".$user_ID."' />
536 <input type='hidden' name='parent_id' value='".$pID."' />
537 <input type='hidden' name='token' value='".$token."' /><br/>
538 <input type='submit' value='".__("Send Message", "fep")."' />
539 </form>";
540 } else {
541 $this->error = __("You cannot send messages because you are blocked by administrator!", "fep");
542 }
543
544 if ($user_ID != $post->from_user) //Update only if the reader is not the sender ???
545 $wpdb->query($wpdb->prepare("UPDATE {$this->fepTable} SET message_read = 1 WHERE id = %d", $pID));
546
547 return $threadOut;
548 }
549
550 function dispReadMsg_admin()
551 {
552 global $wpdb, $user_ID;
553
554 $pID = $_GET['id'];
555 $wholeThread = $this->getWholeThread($pID);
556 $token = $this->getToken();
557
558 $threadOut = "<p><strong>".__("Message Thread", "fep").":</strong></p>
559 <table><tr><th width='15%'>".__("Sender", "fep")."</th><th width='85%'>".__("Message", "fep")."</th></tr>";
560
561 foreach ($wholeThread as $post)
562 {
563 //Check for privacy errors first
564 if (!current_user_can( 'manage_options' ))
565 {
566 $this->error = __("You do not have permission to view this message!", "fep");
567 return;
568 }
569
570 //setup info for the reply form
571 if ($post->parent_id == 0) //If it is the parent message
572 {
573 $to = $post->from_user;
574 if ($to == $user_ID) //Make sure user doesn't send a message to himself
575 $to = $post->to_user;
576 $message_title = $this->output_filter($post->message_title);
577 if (substr_count($message_title, __("Re:", "fep")) < 1) //Prevent all the Re:'s from happening
578 $re = __("Re:", "fep");
579 else
580 $re = "";
581 }
582
583 $uData = get_userdata($post->from_user);
584 $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>";
585
586 if ($post->parent_id == 0) //If it is the parent message
587 {
588 $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>";
589 }
590 else
591 {
592 $threadOut .= "<td class='pmtext'>".apply_filters("comment_text", $this->autoembed($this->output_filter($post->message_contents)))."</td></tr>";
593 }
594 }
595
596 //SHOW THE REPLY FORM
597 $threadOut .= "</table>
598 <p><strong>".__("Add Reply", "fep").":</strong></p>
599 <form name='message' action='".$this->actionURL."checkmessage' method='post'>".
600 $this->get_form_buttons()."<br/>
601 <textarea name='message_content'></textarea>
602 <input type='hidden' name='message_to' value='".get_userdata($to)->user_login."' />
603 <input type='hidden' name='message_top' value='".get_userdata($to)->display_name."' />
604 <input type='hidden' name='message_title' value='".$re.$message_title."' />
605 <input type='hidden' name='message_from' value='".$user_ID."' />
606 <input type='hidden' name='parent_id' value='".$pID."' />
607 <input type='hidden' name='token' value='".$token."' /><br/>
608 <input type='submit' value='".__("Send Message", "fep")."' />
609 </form>";
610
611 return $threadOut;
612 }
613
614 function getWholeThread($id)
615 {
616 global $wpdb;
617 $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$this->fepTable} WHERE id = %d OR parent_id = %d ORDER BY id ASC", $id, $id));
618 return $results;
619 }
620
621 function getInfo($id)
622 {
623 global $wpdb;
624 $to = $wpdb->get_var($wpdb->prepare("SELECT to_user FROM {$this->fepTable} WHERE id = %d", $id));
625 $from = $wpdb->get_var($wpdb->prepare("SELECT from_user FROM {$this->fepTable} WHERE id = %d", $id));
626 return array ( 'to' => $to , 'from' => $from );
627 }
628
629 function convertToUser($to)
630 {
631 $user = get_user_by( 'login' , $to );
632 $result = $user->user_login;
633 return $result;
634 }
635 function convertToDisplay($to)
636 {
637 $user = get_user_by( 'login' , $to );
638 $result = $user->display_name;
639 return $result;
640 }
641 /******************************************READ MESSAGE PAGE END******************************************/
642
643 /******************************************CHECK MESSAGE PAGE BEGIN******************************************/
644 function dispCheckMsg()
645 {
646 global $wpdb, $user_ID;
647 $from = $_POST['message_from'];
648 if ($_POST['message_to']) {
649 $preTo = $_POST['message_to'];
650 } else {
651 $preTo = $_POST['message_top']; }
652 $to = $this->convertToID($preTo);
653 $title = $this->input_filter($_POST['message_title']);
654 $content = $this->input_filter($_POST['message_content']);
655 $parentID = $_POST['parent_id'];
656 $date = current_time('mysql');
657
658 $adminOps = $this->getAdminOps();
659 if ($to)
660 $toUserOps = $this->getUserOps($to);
661
662 //Check for errors first
663 if (!$to || !$title || !$content || ($from != $user_ID))
664 {
665 if (!$to)
666 $theError = __("You must enter a valid recipient!", "fep");
667 if (!$title)
668 $theError = __("You must enter a valid subject!", "fep");
669 if (!$content)
670 $theError = __("You must enter some message content!", "fep");
671 if ($from != $user_ID)
672 $theError = __("You do not have permission to send this message!", "fep");
673 $this->error = $theError;
674 return $this->dispNewMsg();
675 }
676 if ($toUserOps['allow_messages'] != 'true')
677 {
678 $this->error = __("This user does not want to receive messages!", "fep");
679 return;
680 }
681 if ($this->isBoxFull($to, $adminOps['num_messages'], $parentID))
682 {
683 $this->error = __("Your or Recipients Message Box Is Full!", "fep");
684 return;
685 }
686 if (!$this->have_permission())
687 {
688 $this->error = __("You cannot send messages because you are blocked by administrator!", "fep");
689 return;
690 }
691 $timeDelay = $this->TimeDelay($adminOps['time_delay']);
692 if ($timeDelay['diffr'] < $adminOps['time_delay'] && !current_user_can('manage_options'))
693 {
694 $this->error = sprintf(__("Please wait at least more %s to send another message!", "fep"),$timeDelay['time']);
695 return;
696 }
697 if ($parentID != 0) {
698 $mgsInfo = $this->getInfo($parentID);
699 if ($mgsInfo['to'] != $user_ID && $mgsInfo['from'] != $user_ID && !current_user_can( 'manage_options' ))
700 {
701 $this->error = __("You do not have permission to send this message!", "fep");
702 return;
703 }
704 }
705 // Check if a form has been sent
706 $postedToken = filter_input(INPUT_POST, 'token');
707 if (empty($postedToken))
708 {
709 $this->error = __("Invalid Token. Please try again!", "fep");
710 return;
711 }
712 if(!$this->isTokenValid($postedToken)){
713 // Actually This is not first form submission. First Submission Pass this condition and inserted into db.
714 $this->success = __("Your message was successfully sent!", "fep");
715 return;
716 }
717
718 //If no errors then continue on
719 if ($parentID == 0)
720 $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));
721 else
722 {
723 $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));
724 $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));
725 }
726
727 $this->success = __("Your message was successfully sent!", "fep");
728
729 $this->sendEmail($to, $from, $title);
730
731 return;
732 }
733
734 function isBoxFull($to, $boxSize, $parentID)
735 {
736 global $wpdb;
737
738 $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));
739 $num = $wpdb->num_rows;
740
741 if ($boxSize == 0 || $num < $boxSize || $parentID != 0 || current_user_can('manage_options') || user_can( $to, 'manage_options' ))
742 return false;
743 else
744 return true;
745 }
746
747 function sendEmail($to, $from, $title)
748 {
749 $toOptions = $this->getUserOps($to);
750 $notify = $toOptions['allow_emails'];
751 if ($notify == 'true')
752 {
753 $sendername = get_bloginfo("name");
754 $sendermail = get_bloginfo("admin_email");
755 $uData = get_userdata($from);
756 $sendfrom = $uData->display_name;
757 $headers = "MIME-Version: 1.0\r\n" .
758 "From: ".$sendername." "."<".$sendermail.">\r\n" .
759 "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\r\n";
760 $subject = "" . get_bloginfo("name").": New Message";
761 $message = "You have received a new message in \r\n";
762 $message .= get_bloginfo("name")."\r\n";
763 $message .= "From: ".$sendfrom. "\r\n";
764 $message .= "Subject: ".$title. "\r\n";
765 $message .= "Please Click the following link to view full Message. \r\n";
766 $message .= $this->pageURL."\r\n";
767 $mUser = get_userdata($to);
768 $mailTo = $mUser->user_email;
769 wp_mail($mailTo, $subject, $message);
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 message_read <> 2 AND to_del <> 1) OR (from_user = %d AND parent_id = 0 AND message_read <> 2 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
1047 $message_title = ( isset( $_REQUEST['message_title'] ) ) ? $_REQUEST['message_title']: '';
1048 $message_content = ( isset( $_REQUEST['message_content'] ) ) ? $_REQUEST['message_content']: '';
1049
1050 $form = "<p>".__("Add a new announcement below", "fep")."</p>
1051 <form name='message' action='".$this->actionURL."addannouncement' method='post'>
1052 ".__("Subject", "fep").":<br/>
1053 <input type='text' name='message_title' value='".$message_title."' /><br/>".
1054 $this->get_form_buttons()."<br/>
1055 <textarea name='message_content'>".$message_content."</textarea>
1056 <input type='hidden' name='message_from' value='".$user_ID."' />
1057 <input type='hidden' name='token' value='".$token."' /><br/>
1058 <input type='submit' name='add-announcement' value='".__("Submit", "fep")."' />
1059 </form>";
1060
1061 return $form;
1062 }
1063
1064 function getAnnouncements()
1065 {
1066 global $wpdb; //message_read = 2 indicates that the msg is an announcement :)
1067 $results = $wpdb->get_results("SELECT * FROM {$this->fepTable} WHERE message_read = 2 ORDER BY id DESC");
1068 return $results;
1069 }
1070
1071 function getAnnouncementsNum()
1072 {
1073 global $wpdb; //message_read = 2 indicates that the msg is an announcement :)
1074 $results = $wpdb->get_results("SELECT id FROM {$this->fepTable} WHERE message_read = 2 ORDER BY id DESC");
1075 return $wpdb->num_rows;
1076 }
1077 function getAnnouncementsNum_btn(){
1078 if ($this->getAnnouncementsNum()){
1079 $newmgs = " (<font color='red'>";
1080 $newmgs .= $this->getAnnouncementsNum();
1081 $newmgs .="</font>)";
1082 } else {
1083 $newmgs ="";}
1084
1085 return $newmgs;
1086 }
1087
1088 function addAnnouncement()
1089 {
1090 global $wpdb,$user_ID;
1091 $adminOps = $this->getAdminOps();
1092 $title = $this->input_filter($_POST['message_title']);
1093 $contents = $this->input_filter($_POST['message_content']);
1094 $from = $_POST['message_from'];
1095 $date = current_time('mysql');
1096 $read = '2';
1097
1098 if (!$title || !$contents || $from != $user_ID)
1099 {
1100 if (!$title)
1101 $theError = __("You must enter a valid subject!", "fep");
1102 if (!$contents)
1103 $theError = __("You must enter some content!", "fep");
1104 if ($from != $user_ID)
1105 $theError = __("Please try again!", "fep");
1106 $this->error = $theError;
1107 return $this->dispAnnounceForm();
1108 }
1109
1110 // Check if a form has been sent
1111 $postedToken = filter_input(INPUT_POST, 'token');
1112 if (empty($postedToken))
1113 {
1114 $this->error = __("Invalid Token. Please try again!", "fep");
1115 return;
1116 }
1117 if(!$this->isTokenValid($postedToken)){
1118 // Actually This is not first form submission. First Submission Pass this condition and inserted into db.
1119 $this->success = __("The announcement was successfully added!", "fep");
1120 return;
1121 }
1122 //if nothing wrong continue
1123 $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));
1124
1125 if ($adminOps['notify_ann'] == 'on') {
1126 $this->notify_users($title);
1127 $this->success = __("The announcement was successfully added and sent email to all users!", "fep");
1128 return;
1129 } else {
1130 $this->success = __("The announcement was successfully added!", "fep");
1131 return;
1132 }
1133 }
1134
1135 function deleteAnnouncement()
1136 {
1137 global $wpdb;
1138 if (isset($_GET['id'])){$delID = $_GET['id'];}
1139 if (isset($_GET['del'])){$delm = $_GET['del'];}else{ $delm = ''; }
1140 if (current_user_can('manage_options') && $delm) //Make sure only admins can delete announcements
1141 {
1142 $wpdb->query($wpdb->prepare("DELETE FROM {$this->fepTable} WHERE id = %d", $delID));
1143 return true;
1144 }
1145 return false;
1146 }
1147
1148 //Mass emails when announcement is created
1149 function notify_users($title) {
1150
1151 $domain_name = preg_replace('/^www\./','',$_SERVER['SERVER_NAME']);
1152 $usersarray = get_users("orderby=ID");
1153 $adminOps = $this->getAdminOps();
1154 $to = $adminOps['ann_to'];
1155 $from = 'noreply@'.$domain_name;
1156
1157 $bcc = array();
1158 foreach ($usersarray as $user) {
1159 $toOptions = $this->getUserOps($user->ID);
1160 $notify = $toOptions['allow_ann'];
1161 if (in_array($notify == 'true',$usersarray)){
1162 $bcc[] = $user->user_email;
1163 }
1164 }
1165
1166 $chunked_bcc = array_chunk($bcc, 25);
1167
1168 $subject = "" . get_bloginfo("name").": New Announcement";
1169 $message = "A new Announcement is Published in \r\n";
1170 $message .= get_bloginfo("name")."\r\n";
1171 $message .= "Title: ".$title. "\r\n";
1172 $message .= "Please Click the following link to view full Announcement. \r\n";
1173 $message .= $this->actionURL."viewannouncements \r\n";
1174 foreach($chunked_bcc as $bcc_chunk){
1175 $headers = array();
1176 $headers['From'] = 'From: '.get_bloginfo("name").'<'.$from.'>';
1177 $headers['Bcc'] = 'Bcc: '.implode(', ', $bcc_chunk);
1178 wp_mail($to , $subject, $message, $headers);
1179 }
1180 return;
1181 }
1182 /******************************************VIEW ANNOUNCEMENTS END******************************************/
1183
1184 /******************************************MAIN DISPLAY BEGIN******************************************/
1185 function dispHeader()
1186 {
1187 global $user_ID, $user_login;
1188
1189 $numNew = $this->getNewMsgs();
1190 $numAnn = $this->getAnnouncementsNum();
1191 $msgBoxSize = $this->getUserNumMsgs();
1192 $adminOps = $this->getAdminOps();
1193 if ($adminOps['num_messages'] == 0 || current_user_can('manage_options'))
1194 $msgBoxTotal = __("Unlimited", "fep");
1195 else
1196 $msgBoxTotal = $adminOps['num_messages'];
1197
1198 $header = "<div id='fep-wrapper'>";
1199 $header .= "<div id='fep-header'>";
1200 $header .= get_avatar($user_ID, 55)."<p><strong>".__("Welcome", "fep").": ".$this->convertToDisplay($user_login)."</strong><br/>";
1201 $header .= __("You have", "fep")." (<font color='red'>".$numNew."</font>) ".__("new messages", "fep").
1202 " ".__("and", "fep")." (<font color='red'>".$numAnn."</font>) ".__("announcement(s)", "fep")."<br/>";
1203 if ($msgBoxTotal == __("Unlimited", "fep") || $msgBoxSize < $msgBoxTotal)
1204 $header .= __("Message box size", "fep").": ".$msgBoxSize." ".__("of", "fep")." ".$msgBoxTotal."</p>";
1205 else
1206 $header .= "<font color='red'>".__("Your Message Box Is Full! Please delete some messages.", "fep")."</font></p>";
1207 $header .= "</div>";
1208 return $header;
1209 }
1210
1211 function dispMenu()
1212 {
1213
1214 $numNew = $this->getNewMsgs_btn();
1215 $numNewadm = $this->getNewMsgs_admin();
1216 $numAnn = $this->getAnnouncementsNum_btn();
1217
1218 $menu = "<div id='fep-menu'>";
1219 $menu .= "<a class='fep-button' href='".$this->pageURL."'>".__("Message Box".$numNew."", "fep")."</a>";
1220 $menu .= "<a class='fep-button' href='".$this->actionURL."viewannouncements'>".__("Announcements".$numAnn."", "fep")."</a>";
1221 $menu .= "<a class='fep-button' href='".$this->actionURL."newmessage'>".__("New Message", "fep")."</a>";
1222 if($this->adminOps['hide_directory'] != 'on' || current_user_can('manage_options'))
1223 $menu .= "<a class='fep-button' href='".$this->actionURL."directory'>".__("Directory", "fep")."</a>";
1224 $menu .= "<a class='fep-button' href='".$this->actionURL."settings'>".__("Settings", "fep")."</a>";
1225 if(current_user_can('manage_options'))
1226 $menu .= "<a class='fep-button' href='".$this->actionURL."viewallmgs'>".__("Other's Message".$numNewadm."", "fep") . "</a>";
1227 $menu .="</div>";
1228 $menu .= "<div id='fep-content'>";
1229 return $menu;
1230 }
1231
1232 function dispNotify()
1233 {
1234 if ($this->success != ""){
1235 $notify = "<div id='success'>".$this->success."</div>";
1236 } else if ($this->error != "") {
1237 $notify = "<div id='error'>".$this->error."</div>";
1238 }
1239 return $notify;
1240 }
1241
1242 function dispFooter()
1243 {
1244 $footer = "</div>"; //End content
1245 //Maybe Add Notify
1246 if ($this->error != "" || $this->success != "")
1247 $footer .= $this->dispNotify();
1248
1249 if($this->adminOps['hide_branding'] != 'on')
1250 $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>";
1251
1252 $footer .= "</div>"; //End main wrapper
1253
1254 return $footer;
1255 }
1256
1257 function dispDirectory()
1258 {
1259 if($this->adminOps['hide_directory'] == 'on' && !current_user_can('manage_options'))
1260 return;
1261 $users = $this->get_users();
1262 $result = count_users();
1263 $total = $result['total_users'];
1264 $adminOps = $this->getAdminOps();
1265 if ($total)
1266 {
1267 $directory = "<p><strong>".__("Total Users", "fep").": (".$total.")</strong></p>";
1268 $numPgs = $total / $adminOps['user_page'];
1269 if ($numPgs > 1)
1270 {
1271 $directory .= "<p><strong>".__("Page", "fep").": </strong> ";
1272 for ($i = 0; $i < $numPgs; $i++)
1273 if ($_GET['upage'] != $i)
1274 $directory .= "<a href='".$this->actionURL."directory&upage=".$i."'>".($i+1)."</a> ";
1275 else
1276 $directory .= "[<b>".($i+1)."</b>] ";
1277 $directory .= "</p>";
1278 }
1279 $directory .= "<table><tr class='head'>
1280 <th width='50%'>".__("User", "fep")."</th>
1281 <th width='50%'>".__("Send Message", "fep")."</th></tr>";
1282 $a=0;
1283
1284 foreach($users as $u)
1285 {
1286 $directory .= "<tr class='trodd".$a."'><td>".$u->display_name."</td>";
1287 $directory .= "<td><a href='".$this->actionURL."newmessage&to=".$u->user_login."'>".__("Send Message", "fep")."</a></td></tr>";
1288 if ($a) $a = 0; else $a = 1;
1289 }
1290 $directory .= "</table>";
1291
1292 return $directory;
1293 }
1294 else
1295 {
1296 $this->error = __("No User!", "fep");
1297 return;
1298 }
1299 }
1300
1301 //Display the proper contents
1302 function displayAll()
1303 {
1304 global $user_ID,$wpdb;
1305 if ($user_ID)
1306 {
1307 //Finish the setup since these wouldn't work in the constructor
1308 $this->userOps = $this->getUserOps($user_ID);
1309 $this->setPageURLs();
1310
1311 //Add header
1312 $out = $this->dispHeader();
1313
1314 //Add Menu
1315 $out .= $this->dispMenu();
1316
1317 //Start the guts of the display
1318 if (isset($_GET['fepaction'])){
1319 $switch = $_GET['fepaction'];
1320 }else{ $switch = '';}
1321 switch ($switch)
1322 {
1323 case 'newmessage':
1324 $out .= $this->dispNewMsg();
1325 break;
1326 case 'checkmessage':
1327 $out .= $this->dispCheckMsg();
1328 break;
1329 case 'viewmessage':
1330 $out .= $this->dispReadMsg();
1331 break;
1332 case 'viewmessageadmin':
1333 if (current_user_can('manage_options'))
1334 $out .= $this->dispReadMsg_admin();
1335 else
1336 $out .= $this->dispReadMsg();
1337 break;
1338 case 'deletemessage':
1339 $out .= $this->dispDelMsg();
1340 break;
1341 case 'deletemessageadmin':
1342 if (current_user_can('manage_options'))
1343 $out .= $this->dispDelMsg_admin();
1344 else
1345 $out .= $this->dispDelMsg();
1346 break;
1347 case 'directory':
1348 if($this->adminOps['hide_directory'] != 'on' || current_user_can('manage_options'))
1349 $out .= $this->dispDirectory();
1350 else
1351 $out .= $this->dispMsgBox();
1352 break;
1353 case 'settings':
1354 $out .= $this->dispUserPage();
1355 break;
1356 case 'viewannouncements':
1357 $out .= $this->dispAnnouncement();
1358 break;
1359 case 'addannouncement':
1360 $out .= $this->addAnnouncement();
1361 break;
1362 case 'viewallmgs':
1363 if (current_user_can('manage_options'))
1364 $out .= $this->dispMsgBox_admin();
1365 else
1366 $out .= $this->dispMsgBox();
1367 break;
1368 default: //Message box is shown by Default
1369 $out .= $this->dispMsgBox();
1370 break;
1371 }
1372
1373 //Add footer
1374 $out .= $this->dispFooter();
1375 }
1376 else
1377 {
1378 $out = "<p><strong>".__("You must be logged-in to view your message.", "fep")."</strong></p>";
1379 }
1380 return $out;
1381 }
1382 /******************************************MAIN DISPLAY END******************************************/
1383
1384 /******************************************MISC. FUNCTIONS BEGIN******************************************/
1385
1386 /**
1387 * Creates a token usable in a form
1388 * @return string
1389 */
1390 function session(){
1391 if(!isset($_SESSION)) {
1392 session_start();
1393 }
1394 }
1395
1396 function getToken(){
1397 $token = sha1(mt_rand());
1398 if(!isset($_SESSION['tokens'])){
1399 $_SESSION['tokens'] = array($token => 1);
1400 }else{
1401 $_SESSION['tokens'][$token] = 1;
1402 }
1403 return $token;
1404 }
1405
1406 /**
1407 * Check if a token is valid. Removes it from the valid tokens list
1408 * @param string $token The token
1409 * @return bool
1410 */
1411 function isTokenValid($token){
1412 if(!empty($_SESSION['tokens'][$token])){
1413 unset($_SESSION['tokens'][$token]);
1414 return true;
1415 }
1416 return false;
1417 }
1418
1419 //Check is user blocked by admin
1420 function have_permission(){
1421 global $current_user;
1422 $adminOps = $this->getAdminOps();
1423 $wpusers = (array) explode(',', $adminOps['have_permission']);
1424 $valid_wpusers = array();
1425 foreach($wpusers as $wpuser){
1426 $wpuser = trim($wpuser);
1427 if($wpuser!=''){
1428 $user = get_user_by('login', $wpuser);
1429 if($user){
1430 $valid_wpusers[] = $user->ID;
1431 }
1432 $valid_wpusers = array_unique($valid_wpusers);
1433 if(in_array($current_user->ID, $valid_wpusers)){
1434 return false;
1435 }
1436 } }
1437 return true;
1438 }
1439
1440 function get_users()
1441 {
1442 global $wpdb;
1443 if (isset($_GET['upage'])){
1444 $page = $_GET['upage'];
1445 }else{$page = 0;}
1446 $adminOps = $this->getAdminOps();
1447 $start = $page * $adminOps['user_page'];
1448 $end = $adminOps['user_page'];
1449 $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));
1450 return $users;
1451 }
1452
1453 function get_form_buttons()
1454 {
1455 $button = '
1456 <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>
1457 <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>
1458 <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>
1459 <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>
1460 <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>
1461 <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>
1462 <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>
1463 <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>
1464 <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>
1465 <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>
1466 <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>
1467 <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>
1468 <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>';
1469
1470 return $button;
1471 }
1472
1473 function output_filter($string)
1474 {
1475 $parser = new fepBBCParser();
1476 $html = stripslashes($parser->bbc2html($string));
1477 return ent2ncr($html);
1478 }
1479
1480 function input_filter($string)
1481 {
1482 $newStr = esc_attr($string);
1483 return wp_strip_all_tags($newStr);
1484 }
1485
1486 function getUserNumMsgs()
1487 {
1488 global $wpdb, $user_ID;
1489 $get_messages = $wpdb->get_results($wpdb->prepare("SELECT id FROM {$this->fepTable} WHERE (to_user = %d AND parent_id = 0 AND message_read <> 2 AND to_del <> 1) OR (from_user = %d AND parent_id = 0 AND message_read <> 2 AND from_del <> 1)", $user_ID, $user_ID));
1490 $num = $wpdb->num_rows;
1491 return $num;
1492 }
1493
1494 function formatDate($date)
1495 {
1496 $now = current_time('mysql');
1497 //return date('M d, h:i a', strtotime($date));
1498 return human_time_diff(strtotime($date),strtotime($now)).' ago';
1499 }
1500
1501 function TimeDelay($DeTime)
1502 {
1503 global $wpdb, $user_ID;
1504 $now = current_time('mysql');
1505 $Dtime = $DeTime * 60;
1506 $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));
1507 $diff = strtotime($now) - strtotime($Prev);
1508 $diffr = $diff/60;
1509 $next = strtotime($Prev) + $Dtime;
1510 $Ntime = human_time_diff(strtotime($now),$next);
1511 return array('diffr' => $diffr, 'time' => $Ntime);
1512 }
1513
1514 function is_positive($str) {
1515 return (is_numeric($str) && $str > 0 && $str == round($str));
1516 }
1517
1518 function getNewMsgs()
1519 {
1520 global $wpdb, $user_ID;
1521
1522 $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));
1523 return $wpdb->num_rows;
1524 }
1525 function getNewMsgs_btn(){
1526 if ($this->getNewMsgs()){
1527 $newmgs = " (<font color='red'>";
1528 $newmgs .= $this->getNewMsgs();
1529 $newmgs .="</font>)";
1530 } else {
1531 $newmgs = "";}
1532
1533 return $newmgs;
1534 }
1535
1536
1537 function getNewMsgs_admin()
1538 {
1539 global $wpdb, $user_ID;
1540
1541 $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));
1542 if ($wpdb->num_rows){
1543 $newmgs = " (<font color='red'>";
1544 $newmgs .= $wpdb->num_rows;
1545 $newmgs .="</font>)";
1546 } else {
1547 $newmgs ="";}
1548
1549 return $newmgs;
1550 }
1551
1552 function autoembed($string)
1553 {
1554 global $wp_embed;
1555 if (is_object($wp_embed))
1556 return $wp_embed->autoembed($string);
1557 else
1558 return $string;
1559 }
1560
1561 function get_version()
1562 {
1563 $plugin_data = implode('', file($this->pluginDir."front-end-pm.php"));
1564 if (preg_match("|Version:(.*)|i", $plugin_data, $version))
1565 $version = $version[1];
1566 return $version;
1567 }
1568 /******************************************MISC. FUNCTIONS END******************************************/
1569 } //END CLASS
1570 } //ENDIF
1571 ?>