setupLinks(); $this->adminOps = $this->getAdminOps(); } function fepActivate() { global $wpdb; $charset_collate = ''; if( $wpdb->has_cap('collation')) { if(!empty($wpdb->charset)) $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; if(!empty($wpdb->collate)) $charset_collate .= " COLLATE $wpdb->collate"; } $installed_ver = get_option( "fep_db_version" ); $fep_db_version = 1.1; if( $installed_ver != $fep_db_version ) { $sqlMsgs = "CREATE TABLE ".$this->fepTable."( `id` int(11) NOT NULL auto_increment, `parent_id` int(11) NOT NULL default '0', `from_user` int(11) NOT NULL default '0', `to_user` int(11) NOT NULL default '0', `last_sender` int(11) NOT NULL default '0', `date` datetime NOT NULL default '0000-00-00 00:00:00', `last_date` datetime NOT NULL default '0000-00-00 00:00:00', `message_title` varchar(65) NOT NULL, `message_contents` longtext NOT NULL, `message_read` int(11) NOT NULL default '0', `to_del` int(11) NOT NULL default '0', `from_del` int(11) NOT NULL default '0', PRIMARY KEY (`id`)) {$charset_collate};"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sqlMsgs); update_option( "fep_db_version", $fep_db_version ); } } function translation() { //SETUP TEXT DOMAIN FOR TRANSLATIONS $plugin_dir = basename(dirname(__FILE__)); load_plugin_textdomain('fep', false, $plugin_dir.'/languages/'); } function widget($args) { global $user_ID; $uData = get_userdata($user_ID); $this->setPageURLs(); echo $args['before_widget']; if (!$uData) echo __("Login to view your messages", "fep"); else { $numNew = $this->getNewMsgs_btn(); $numAnn = $this->getAnnouncementsNum_btn(); $numNewadm = $this->getNewMsgs_admin(); echo "".__("Inbox", "fep")."".$numNew." ".__("Announcement", "fep")."".$numAnn.""; if (current_user_can('manage_options')) echo "".__("Other's Message", "fep")."".$numNewadm.""; } echo $args['after_widget']; } function widget_text($args) { global $user_ID; $uData = get_userdata($user_ID); $this->setPageURLs(); echo $args['before_widget']; echo $args['before_title'].__("Messages", "fep").$args['after_title']; if (!$uData) echo __("Login to view your messages", "fep"); else { $numNew = $this->getNewMsgs(); $numAnn = $this->getAnnouncementsNum(); $numNewadm = $this->getNewMsgs_admin(); echo __("Hi", "fep")." ".$uData->display_name.",
". __("You have", "fep")." (".$numNew.") ".__("new message(s)", "fep")."
". __("There are", "fep")." (".$numAnn.") ".__("announcement(s)", "fep")."
"; if (current_user_can('manage_options')) echo "".__("Other's Message(s)", "fep")."".$numNewadm."
"; echo "".__("View Message Box", "fep")."
"; } echo $args['after_widget']; } //Setup some variables var $adminOpsName = "FEP_options"; var $adminOps = array(); var $userOpsName = "FEP_uOptions"; var $userOps = array(); var $error = ""; var $success = ""; var $pluginDir = ""; var $pluginURL = ""; var $styleDir = ""; var $styleURL = ""; var $pageURL = ""; var $actionURL = ""; var $jsURL = ""; var $fepTable = ""; function jsInit() { if (isset($_GET['fepjscript'])) if($_GET['fepjscript'] == '1') { global $wpdb, $user_ID; require_once('js/search.php'); } } function setupLinks() //And DB table name too :) { global $wpdb; $this->pluginDir = plugin_dir_path( __FILE__ )."/"; $this->pluginURL = plugins_url()."/front-end-pm/"; $this->styleDir = $this->pluginDir."style/"; $this->styleURL = $this->pluginURL."style/"; $this->jsURL = $this->pluginURL."js/"; $this->fepTable = $wpdb->prefix."fep_messages"; } function fep_enqueue_scripts() { wp_enqueue_style( 'fep-style', $this->styleURL . 'style.css' ); wp_enqueue_script( 'fep-script', $this->jsURL . 'script.js', array(), '1.0.0', true ); } function getPageID() { global $wpdb; 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"); } function setPageURLs() { global $wp_rewrite; if($wp_rewrite->using_permalinks()) $delim = "?"; else $delim = "&"; $this->pageURL = get_permalink($this->getPageID()); $this->actionURL = $this->pageURL.$delim."fepaction="; } /******************************************SETUP END******************************************/ /******************************************ADMIN SETTINGS PAGE BEGIN******************************************/ function addAdminPage() { 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' )); add_submenu_page('fep-admin-settings', 'Front End PM - ' .__('Settings','cp'), __('Settings','cp'), 'manage_options', 'fep-admin-settings', array(&$this, "dispAdminPage")); add_submenu_page('fep-admin-settings', 'Front End PM - ' .__('Instruction','cp'), __('Instruction','cp'), 'manage_options', 'fep-instruction', array(&$this, "dispInstructionPage")); } function dispAdminPage() { if ($this->pmAdminSave()) echo "

".__("Options successfully saved", "fep")."

"; $viewAdminOps = $this->getAdminOps(); //Get current options $url = 'http://www.banglardokan.com/blog/recent/project/front-end-pm-2215/'; echo "

".__("Front End PM Settings", "fep")."

".__("Setting", "fep")."".__("Value", "fep")."
".__("Max messages a user can keep in box? (0 = Unlimited)", "fep")."
".__("Admins always have Unlimited", "fep")."

".__("Default","fep").": 50
".__("Messages to show per page", "fep")."
".__("Do not set this to 0!", "fep")."

".__("Default","fep").": 15
".__("Maximum user per page in Directory", "fep")."
".__("Do not set this to 0!", "fep")."

".__("Default","fep").": 50
".__("Time delay between two messages send by a user in minutes (0 = No delay required)", "fep")."
".__("Admins have no restriction", "fep")."

".__("Default","fep").": 5
".__("Block Username", "fep")."
".__("Separated by comma", "fep")."
".__("Valid email address for \"to\" field of announcement email", "fep")."
".__("All users email will be in \"Bcc\" field", "fep")."
".__("Send email to all users when a new announcement is published?", "fep")."
".__("Hide Directory from front end?", "fep")."
".__("Always shown to Admins", "fep")."
".__("Hide Autosuggestion when typing recipient name?", "fep")."
".__("Always shown to Admins", "fep")."
".__("Disable \"send new message\" for all users except admins?", "fep")."
".__("Users can send reply", "fep")."
".__("Hide Branding Footer?", "fep")."
"; } function dispInstructionPage() { $url = 'http://www.banglardokan.com/blog/recent/project/front-end-pm-2215/'; echo "

".__("Front End PM Setup Instruction", "fep")."

".__("Create Page For \"Front End PM\"", "fep")."

".$this->fep_createPage()."
"; } function pmAdminSave() { if (isset($_POST['fep-admin-save'])) { if (!is_email($_POST['ann_to'])) { echo "

".__("Please enter a valid email address!", "fep")."

"; return;} if (!ctype_digit($_POST['num_messages']) || !$this->is_positive($_POST['messages_page']) || !$this->is_positive($_POST['user_page']) || !ctype_digit($_POST['time_delay'])) { echo "

".__("First four fields support only positive numbers!", "fep")."

"; return;} $saveAdminOps = array('num_messages' => $_POST['num_messages'], 'messages_page' => $_POST['messages_page'], 'user_page' => $_POST['user_page'], 'time_delay' => $_POST['time_delay'], 'hide_branding' => $_POST['hide_branding'], 'hide_directory' => $_POST['hide_directory'], 'hide_autosuggest' => $_POST['hide_autosuggest'], 'disable_new' => $_POST['disable_new'], 'ann_to' => $_POST['ann_to'], 'notify_ann' => $_POST['notify_ann'], 'have_permission' => $_POST['have_permission'] ); update_option($this->adminOpsName, $saveAdminOps); return true; } return false; } function getAdminOps() { $pmAdminOps = array('num_messages' => 50, 'messages_page' => 15, 'user_page' => 50, 'time_delay' => 5, 'hide_directory' => false, 'ann_to' => get_bloginfo("admin_email"), 'notify_ann' => false, 'hide_autosuggest' => false, 'disable_new' => false, 'hide_branding' => false, 'have_permission' => '' ); //Get old values if they exist $adminOps = get_option($this->adminOpsName); if (!empty($adminOps)) { foreach ($adminOps as $key => $option) $pmAdminOps[$key] = $option; } update_option($this->adminOpsName, $pmAdminOps); $this->adminOps = $pmAdminOps; return $pmAdminOps; } function fep_createPage(){ $token = $this->getToken(); $form = "

".__("Title of \"Front End PM\" Page", "fep").":

".__("Slug", "fep").": ".__("If blank, slug will be automatically created based on Title", "fep")."


"; return $form; } function fep_createPage_action(){ if (isset($_POST['fep-create-page'])){ $titlePre = wp_strip_all_tags($_POST['fep-create-page-title']); $title = utf8_encode($titlePre); $slugPre = wp_strip_all_tags($_POST['fep-create-page-slug']); $slug = utf8_encode($slugPre); if ($this->getPageID() !=''){ echo "

" .sprintf(__("Already created page %s for \"Front End PM\". Please use that page instead!", "fep"),get_permalink($this->getPageID()),get_the_title($this->getPageID()))."

"; return;} if (!$title){ echo "

" .__("You must enter a valid Title!", "fep")."

"; return;} // Check if a form has been sent $postedToken = filter_input(INPUT_POST, 'token'); if (empty($postedToken)) { echo "

" .__("Invalid Token. Please try again!", "fep")."

"; return; } if(!$this->isTokenValid($postedToken)){ // Actually This is not first form submission. First Submission Pass this condition and inserted into db. echo "

" .__("Page for \"Front End PM\" successfully created!", "fep")."

"; return; } $fep_page = array( 'post_title' => $title, 'post_name' => $slug, 'post_content' => '[front-end-pm]', 'post_status' => 'publish', 'post_type' => 'page' ); $pageID = wp_insert_post( $fep_page ); if($pageID == 0){ echo "

" .__("Something wrong.Please try again to create page!", "fep")."

"; return; } else { echo "

" .sprintf(__("Page %s for \"Front End PM\" successfully created!", "fep"),get_permalink($pageID),get_the_title($pageID))."

"; return;} } } /******************************************ADMIN SETTINGS PAGE END******************************************/ /******************************************USER SETTINGS PAGE BEGIN******************************************/ function dispUserPage() { global $user_ID; if ($this->pmUserSave()) $this->success = __("Your settings have been saved!", "fep"); $viewUserOps = $this->getUserOps($user_ID); //Get current options $prefs = "

".__("Set your preferences below", "fep").":




"; return $prefs; } function pmUserSave() { global $user_ID; if (isset($_POST['fep-user-save'])) { $saveUserOps = array( 'allow_emails' => $_POST['allow_emails'], 'allow_messages' => $_POST['allow_messages'], 'allow_ann' => $_POST['allow_ann'] ); update_user_meta($user_ID, $this->userOpsName, $saveUserOps); return true; } return false; } function getUserOps($ID) { $pmUserOps = array( 'allow_emails' => 'true', 'allow_messages' => 'true', 'allow_ann' => 'true' ); //Get old values if they exist $userOps = get_user_meta($ID, $this->userOpsName, true); if (!empty($userOps)) { foreach ($userOps as $key => $option) $pmUserOps[$key] = $option; } update_user_meta($ID, $this->userOpsName, $pmUserOps); return $pmUserOps; } /******************************************USER SETTINGS PAGE END******************************************/ /******************************************NEW MESSAGE PAGE BEGIN******************************************/ function dispNewMsg() { global $user_ID; $token = $this->getToken(); $adminOps = $this->getAdminOps(); if (isset($_GET['to'])){ $to = $_GET['to']; }else{ $to = '';} if (!$this->have_permission()) { $this->error = __("You cannot send messages because you are blocked by administrator!", "fep"); return; } if ($this->adminOps['disable_new'] == 'on' && !current_user_can('manage_options')) { $this->error = __("Send new message is disabled for users!", "fep"); return; } if (!$this->isBoxFull($user_ID, $adminOps['num_messages'], '1')) { $message_to = ( isset( $_REQUEST['message_to'] ) ) ? $_REQUEST['message_to']: ''; $message_top = ( isset( $_REQUEST['message_top'] ) ) ? $_REQUEST['message_top']: ''; $message_title = ( isset( $_REQUEST['message_title'] ) ) ? $_REQUEST['message_title']: ''; $message_content = ( isset( $_REQUEST['message_content'] ) ) ? $_REQUEST['message_content']: ''; $newMsg = "

".__("Create New Message", "fep").":

"; $newMsg .= "
". __("To", "fep")."*: "; if($this->adminOps['hide_autosuggest'] != 'on' || current_user_can('manage_options')) { $newMsg .="
"; $newMsg .="
"; } else { $newMsg .="

";} $newMsg .= __("Subject", "fep")."*:

". __("Message", "fep")."*:
".$this->get_form_buttons()."

"; return $newMsg; } else { $this->error = __("You cannot send messages because your message box is full! Please delete some messages.", "fep"); return; } } /******************************************NEW MESSAGE PAGE END******************************************/ /******************************************READ MESSAGE PAGE BEGIN******************************************/ function dispReadMsg() { global $wpdb, $user_ID; $pID = $_GET['id']; $wholeThread = $this->getWholeThread($pID); $token = $this->getToken(); $threadOut = "

".__("Message Thread", "fep").":

"; foreach ($wholeThread as $post) { //Check for privacy errors first if ($post->to_user != $user_ID && $post->from_user != $user_ID && !current_user_can( 'manage_options' )) { $this->error = __("You do not have permission to view this message!", "fep"); return; } //setup info for the reply form if ($post->parent_id == 0) //If it is the parent message { $to = $post->from_user; if ($to == $user_ID) //Make sure user doesn't send a message to himself $to = $post->to_user; $message_title = $this->output_filter($post->message_title); if (substr_count($message_title, __("Re:", "fep")) < 1) //Prevent all the Re:'s from happening $re = __("Re:", "fep"); else $re = ""; } $uData = get_userdata($post->from_user); $threadOut .= ""; if ($post->parent_id == 0) //If it is the parent message { $threadOut .= ""; } else { $threadOut .= ""; } } $threadOut .= "
".__("Sender", "fep")."".__("Message", "fep")."
".$uData->display_name."
".$this->formatDate($post->date)."
".get_avatar($post->from_user, 60)."
".__("Subject", "fep").": ".$this->output_filter($post->message_title)."
".apply_filters("comment_text", $this->autoembed($this->output_filter($post->message_contents)))."
".apply_filters("comment_text", $this->autoembed($this->output_filter($post->message_contents)))."
"; //SHOW THE REPLY FORM if ($this->have_permission()){ $threadOut .= "

".__("Add Reply", "fep").":

". $this->get_form_buttons()."

"; } else { $this->error = __("You cannot send messages because you are blocked by administrator!", "fep"); } if ($user_ID != $post->from_user) //Update only if the reader is not the sender ??? $wpdb->query($wpdb->prepare("UPDATE {$this->fepTable} SET message_read = 1 WHERE id = %d", $pID)); return $threadOut; } function dispReadMsg_admin() { global $wpdb, $user_ID; $pID = $_GET['id']; $wholeThread = $this->getWholeThread($pID); $token = $this->getToken(); $threadOut = "

".__("Message Thread", "fep").":

"; foreach ($wholeThread as $post) { //Check for privacy errors first if (!current_user_can( 'manage_options' )) { $this->error = __("You do not have permission to view this message!", "fep"); return; } //setup info for the reply form if ($post->parent_id == 0) //If it is the parent message { $to = $post->from_user; if ($to == $user_ID) //Make sure user doesn't send a message to himself $to = $post->to_user; $message_title = $this->output_filter($post->message_title); if (substr_count($message_title, __("Re:", "fep")) < 1) //Prevent all the Re:'s from happening $re = __("Re:", "fep"); else $re = ""; } $uData = get_userdata($post->from_user); $threadOut .= ""; if ($post->parent_id == 0) //If it is the parent message { $threadOut .= ""; } else { $threadOut .= ""; } } //SHOW THE REPLY FORM $threadOut .= "
".__("Sender", "fep")."".__("Message", "fep")."
".$uData->display_name."
".$this->formatDate($post->date)."
".get_avatar($post->from_user, 60)."
".__("Subject", "fep").": ".$this->output_filter($post->message_title)."
".apply_filters("comment_text", $this->autoembed($this->output_filter($post->message_contents)))."
".apply_filters("comment_text", $this->autoembed($this->output_filter($post->message_contents)))."

".__("Add Reply", "fep").":

". $this->get_form_buttons()."

"; return $threadOut; } function getWholeThread($id) { global $wpdb; $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$this->fepTable} WHERE id = %d OR parent_id = %d ORDER BY id ASC", $id, $id)); return $results; } function getInfo($id) { global $wpdb; $to = $wpdb->get_var($wpdb->prepare("SELECT to_user FROM {$this->fepTable} WHERE id = %d", $id)); $from = $wpdb->get_var($wpdb->prepare("SELECT from_user FROM {$this->fepTable} WHERE id = %d", $id)); return array ( 'to' => $to , 'from' => $from ); } function convertToUser($to) { $user = get_user_by( 'login' , $to ); $result = $user->user_login; return $result; } function convertToDisplay($to) { $user = get_user_by( 'login' , $to ); $result = $user->display_name; return $result; } /******************************************READ MESSAGE PAGE END******************************************/ /******************************************CHECK MESSAGE PAGE BEGIN******************************************/ function dispCheckMsg() { global $wpdb, $user_ID; $from = $_POST['message_from']; if ($_POST['message_to']) { $preTo = $_POST['message_to']; } else { $preTo = $_POST['message_top']; } $to = $this->convertToID($preTo); $title = $this->input_filter($_POST['message_title']); $content = $this->input_filter($_POST['message_content']); $parentID = $_POST['parent_id']; $date = current_time('mysql'); $adminOps = $this->getAdminOps(); if ($to) $toUserOps = $this->getUserOps($to); //Check for errors first if (!$to || !$title || !$content || ($from != $user_ID)) { if (!$to) $theError = __("You must enter a valid recipient!", "fep"); if (!$title) $theError = __("You must enter a valid subject!", "fep"); if (!$content) $theError = __("You must enter some message content!", "fep"); if ($from != $user_ID) $theError = __("You do not have permission to send this message!", "fep"); $this->error = $theError; return $this->dispNewMsg(); } if ($toUserOps['allow_messages'] != 'true') { $this->error = __("This user does not want to receive messages!", "fep"); return; } if ($this->isBoxFull($to, $adminOps['num_messages'], $parentID)) { $this->error = __("Your or Recipients Message Box Is Full!", "fep"); return; } if (!$this->have_permission()) { $this->error = __("You cannot send messages because you are blocked by administrator!", "fep"); return; } $timeDelay = $this->TimeDelay($adminOps['time_delay']); if ($timeDelay['diffr'] < $adminOps['time_delay'] && !current_user_can('manage_options')) { $this->error = sprintf(__("Please wait at least more %s to send another message!", "fep"),$timeDelay['time']); return; } if ($parentID != 0) { $mgsInfo = $this->getInfo($parentID); if ($mgsInfo['to'] != $user_ID && $mgsInfo['from'] != $user_ID && !current_user_can( 'manage_options' )) { $this->error = __("You do not have permission to send this message!", "fep"); return; } } // Check if a form has been sent $postedToken = filter_input(INPUT_POST, 'token'); if (empty($postedToken)) { $this->error = __("Invalid Token. Please try again!", "fep"); return; } if(!$this->isTokenValid($postedToken)){ // Actually This is not first form submission. First Submission Pass this condition and inserted into db. $this->success = __("Your message was successfully sent!", "fep"); return; } //If no errors then continue on if ($parentID == 0) $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)); else { $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)); $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)); } $this->success = __("Your message was successfully sent!", "fep"); $this->sendEmail($to, $from, $title); return; } function isBoxFull($to, $boxSize, $parentID) { global $wpdb; $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)); $num = $wpdb->num_rows; if ($boxSize == 0 || $num < $boxSize || $parentID != 0 || current_user_can('manage_options') || user_can( $to, 'manage_options' )) return false; else return true; } function sendEmail($to, $from, $title) { $toOptions = $this->getUserOps($to); $notify = $toOptions['allow_emails']; if ($notify == 'true') { $sendername = get_bloginfo("name"); $sendermail = get_bloginfo("admin_email"); $uData = get_userdata($from); $sendfrom = $uData->display_name; $headers = "MIME-Version: 1.0\r\n" . "From: ".$sendername." "."<".$sendermail.">\r\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\r\n"; $subject = "" . get_bloginfo("name").": New Message"; $message = "You have received a new message in \r\n"; $message .= get_bloginfo("name")."\r\n"; $message .= "From: ".$sendfrom. "\r\n"; $message .= "Subject: ".$title. "\r\n"; $message .= "Please Click the following link to view full Message. \r\n"; $message .= $this->pageURL."\r\n"; $mUser = get_userdata($to); $mailTo = $mUser->user_email; wp_mail($mailTo, $subject, $message); } } function convertToID($preTo) { global $user_ID; $user = get_user_by( 'login' , $preTo ); $result = $user->ID; if ($result != $user_ID && $result) return $result; else return 0; } /******************************************CHECK MESSAGE PAGE END******************************************/ /******************************************MESSAGE-BOX PAGE BEGIN******************************************/ function dispMsgBox() { global $wpdb, $user_ID; $adminOps = $this->getAdminOps(); $numMsgs = $this->getUserNumMsgs(); if ($numMsgs) { $msgsOut = "

".__("Your Messages", "fep").":

"; $numPgs = $numMsgs / $adminOps['messages_page']; if ($numPgs > 1) { $msgsOut .= "

".__("Page", "fep").": "; for ($i = 0; $i < $numPgs; $i++) if ($_GET['pmpage'] != $i) $msgsOut .= "".($i+1)." "; else $msgsOut .= "[".($i+1)."] "; $msgsOut .= "

"; } $msgsOut .= ""; $msgs = $this->getMsgs(); $a = 0; foreach ($msgs as $msg) { if ($msg->message_read == 0 && $msg->last_sender != $user_ID) $read = "".__("Unread", "fep").""; else $read = __("Read", "fep"); $uSend = get_userdata($msg->from_user); $uLast = get_userdata($msg->last_sender); $toUser = get_userdata($msg->to_user); $msgsOut .= ""; if ($uSend->ID != $user_ID){ $msgsOut .= ""; } else { $msgsOut .= ""; } if ($toUser->ID != $user_ID){ $msgsOut .= "";} else { $msgsOut .= "";} $msgsOut .= ""; $msgsOut .= ""; $msgsOut .= ""; //Alternate table colors if ($a) $a = 0; else $a = 1; } $msgsOut .= "
".__("Started By", "fep")." ".__("To", "fep")." ".__("Subject", "fep")." ".__("Last Reply By", "fep")." ".__("Delete", "fep")."
" .$uSend->display_name. "
".$this->formatDate($msg->date)."
" .$uSend->display_name. "
".$this->formatDate($msg->date)."
" .$toUser->display_name. "" .$toUser->display_name. "".$this->output_filter($msg->message_title)."
".$read."
" .$uLast->display_name. "
".$this->formatDate($msg->last_date)."
".__("Delete", "fep")."
"; return $msgsOut; } else { $this->error = __("Your message box is empty!", "fep"); return; } } function getUserNumMsgs_admin() { global $wpdb, $user_ID; $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)); $num = $wpdb->num_rows; return $num; } function dispMsgBox_admin() { global $wpdb, $user_ID; $adminOps = $this->getAdminOps(); $numMsgs = $this->getUserNumMsgs_admin(); if ($numMsgs) { $msgsOut = "

".__("All Messages", "fep").":

"; $numPgs = $numMsgs / $adminOps['messages_page']; if ($numPgs > 1) { $msgsOut .= "

".__("Page", "fep").": "; for ($i = 0; $i < $numPgs; $i++) if ($_GET['apmpage'] != $i) $msgsOut .= "".($i+1)." "; else $msgsOut .= "[".($i+1)."] "; $msgsOut .= "

"; } $msgsOut .= ""; $msgs = $this->getMsgs_admin(); $a = 0; foreach ($msgs as $msg) { if ($msg->message_read == 0 && $msg->last_sender != $user_ID) $read = "".__("Unread", "fep").""; else $read = __("Read", "fep"); $uSend = get_userdata($msg->from_user); $uLast = get_userdata($msg->last_sender); $toUser = get_userdata($msg->to_user); $msgsOut .= ""; $msgsOut .= ""; $msgsOut .= ""; $msgsOut .= ""; $msgsOut .= ""; $msgsOut .= ""; //Alternate table colors if ($a) $a = 0; else $a = 1; } $msgsOut .= "
".__("Started By", "fep")." ".__("To", "fep")." ".__("Subject", "fep")." ".__("Last Reply By", "fep")." ".__("Delete", "fep")."
" .$uSend->display_name. "
".$this->formatDate($msg->date)."
" .$toUser->display_name. "".$this->output_filter($msg->message_title)."
".$read."
" .$uLast->display_name. "
".$this->formatDate($msg->last_date)."
".__("Delete", "fep")."
"; return $msgsOut; } else { $this->error = __("Message box is empty!", "fep"); return; } } function getMsgs() { global $wpdb, $user_ID; if (isset($_GET['pmpage'])){ $page = $_GET['pmpage']; }else{$page = 0;} $adminOps = $this->getAdminOps(); $start = $page * $adminOps['messages_page']; $end = $adminOps['messages_page']; $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)); return $get_messages; } function getMsgs_admin() { global $wpdb, $user_ID; if (isset($_GET['apmpage'])){ $page = $_GET['apmpage']; }else{$page = 0;} $adminOps = $this->getAdminOps(); $start = $page * $adminOps['messages_page']; $end = $adminOps['messages_page']; $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)); return $get_messages; } /******************************************MESSAGE-BOX PAGE END******************************************/ /******************************************DELETE PAGE BEGIN******************************************/ function dispDelMsg() { global $wpdb, $user_ID; $delID = $_GET['id']; $toDuser = $wpdb->get_var($wpdb->prepare("SELECT to_user FROM {$this->fepTable} WHERE id = %d", $delID)); $toDel = $wpdb->get_var($wpdb->prepare("SELECT to_del FROM {$this->fepTable} WHERE id = %d", $delID)); $fromDel = $wpdb->get_var($wpdb->prepare("SELECT from_del FROM {$this->fepTable} WHERE id = %d", $delID)); if ($toDuser == $user_ID) { if ($fromDel == 0) $wpdb->query($wpdb->prepare("UPDATE {$this->fepTable} SET to_del = 1 WHERE id = %d", $delID)); else $wpdb->query($wpdb->prepare("DELETE FROM {$this->fepTable} WHERE id = %d OR parent_id = %d", $delID, $delID)); } else { if ($toDel == 0) $wpdb->query($wpdb->prepare("UPDATE {$this->fepTable} SET from_del = 1 WHERE id = %d", $delID)); else $wpdb->query($wpdb->prepare("DELETE FROM {$this->fepTable} WHERE id = %d OR parent_id = %d", $delID, $delID)); } $this->success = __("Your message was successfully deleted!", "fep"); return; } function dispDelMsg_admin() { global $wpdb, $user_ID; $delID = $_GET['id']; if (current_user_can('manage_options')) { $wpdb->query($wpdb->prepare("DELETE FROM {$this->fepTable} WHERE id = %d OR parent_id = %d", $delID, $delID)); } $this->success = __("Message was successfully deleted!", "fep"); return; } /******************************************DELETE PAGE END******************************************/ /******************************************VIEW ANNOUNCEMENTS BEGIN******************************************/ function dispAnnouncement() { global $wpdb, $user_ID; $announcements = $this->getAnnouncements(); $num = $wpdb->num_rows; if ($this->deleteAnnouncement()) //Deleting an announcement? { $this->success = __("The announcement was successfully deleted!", "fep"); return; } if (!$num) //Just viewing announcements { $announce = "

".__("Announcements", "fep").":

"; if (current_user_can('manage_options')) { $announce .= $this->dispAnnounceForm(); } $this->error = __("There are no announcements!", "fep"); } else { $announce = "

".__("Announcements", "fep").":

"; if (current_user_can('manage_options')) { $announce .= $this->dispAnnounceForm(); } $announce .= ""; $a = 0; foreach ($announcements as $announcement) { $announce .= ""; if ($a) $a = 0; else $a = 1; //Alternate table colors } $announce .= "
".__("Subject", "fep").": ".$this->output_filter($announcement->message_title). "
".__("Date", "fep").": ".$this->formatDate($announcement->date); if (current_user_can('manage_options')) { $announce .= "
".__("Added by", "fep").": ".get_userdata($announcement->from_user)->display_name; $announce .= "
".__("Delete", "fep").""; } $announce .= "
"; $announce .= "".__("Message", "fep").":
".apply_filters("comment_text", $this->output_filter($announcement->message_contents))."
"; } return $announce; } function dispAnnounceForm() { global $user_ID; $token = $this->getToken(); $message_title = ( isset( $_REQUEST['message_title'] ) ) ? $_REQUEST['message_title']: ''; $message_content = ( isset( $_REQUEST['message_content'] ) ) ? $_REQUEST['message_content']: ''; $form = "

".__("Add a new announcement below", "fep")."

".__("Subject", "fep").":

". $this->get_form_buttons()."

"; return $form; } function getAnnouncements() { global $wpdb; //message_read = 2 indicates that the msg is an announcement :) $results = $wpdb->get_results("SELECT * FROM {$this->fepTable} WHERE message_read = 2 ORDER BY id DESC"); return $results; } function getAnnouncementsNum() { global $wpdb; //message_read = 2 indicates that the msg is an announcement :) $results = $wpdb->get_results("SELECT id FROM {$this->fepTable} WHERE message_read = 2 ORDER BY id DESC"); return $wpdb->num_rows; } function getAnnouncementsNum_btn(){ if ($this->getAnnouncementsNum()){ $newmgs = " ("; $newmgs .= $this->getAnnouncementsNum(); $newmgs .=")"; } else { $newmgs ="";} return $newmgs; } function addAnnouncement() { global $wpdb,$user_ID; $adminOps = $this->getAdminOps(); $title = $this->input_filter($_POST['message_title']); $contents = $this->input_filter($_POST['message_content']); $from = $_POST['message_from']; $date = current_time('mysql'); $read = '2'; if (!$title || !$contents || $from != $user_ID) { if (!$title) $theError = __("You must enter a valid subject!", "fep"); if (!$contents) $theError = __("You must enter some content!", "fep"); if ($from != $user_ID) $theError = __("Please try again!", "fep"); $this->error = $theError; return $this->dispAnnounceForm(); } // Check if a form has been sent $postedToken = filter_input(INPUT_POST, 'token'); if (empty($postedToken)) { $this->error = __("Invalid Token. Please try again!", "fep"); return; } if(!$this->isTokenValid($postedToken)){ // Actually This is not first form submission. First Submission Pass this condition and inserted into db. $this->success = __("The announcement was successfully added!", "fep"); return; } //if nothing wrong continue $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)); if ($adminOps['notify_ann'] == 'on') { $this->notify_users($title); $this->success = __("The announcement was successfully added and sent email to all users!", "fep"); return; } else { $this->success = __("The announcement was successfully added!", "fep"); return; } } function deleteAnnouncement() { global $wpdb; if (isset($_GET['id'])){$delID = $_GET['id'];} if (isset($_GET['del'])){$delm = $_GET['del'];}else{ $delm = ''; } if (current_user_can('manage_options') && $delm) //Make sure only admins can delete announcements { $wpdb->query($wpdb->prepare("DELETE FROM {$this->fepTable} WHERE id = %d", $delID)); return true; } return false; } //Mass emails when announcement is created function notify_users($title) { $domain_name = preg_replace('/^www\./','',$_SERVER['SERVER_NAME']); $usersarray = get_users("orderby=ID"); $adminOps = $this->getAdminOps(); $to = $adminOps['ann_to']; $from = 'noreply@'.$domain_name; $bcc = array(); foreach ($usersarray as $user) { $toOptions = $this->getUserOps($user->ID); $notify = $toOptions['allow_ann']; if (in_array($notify == 'true',$usersarray)){ $bcc[] = $user->user_email; } } $chunked_bcc = array_chunk($bcc, 25); $subject = "" . get_bloginfo("name").": New Announcement"; $message = "A new Announcement is Published in \r\n"; $message .= get_bloginfo("name")."\r\n"; $message .= "Title: ".$title. "\r\n"; $message .= "Please Click the following link to view full Announcement. \r\n"; $message .= $this->actionURL."viewannouncements \r\n"; foreach($chunked_bcc as $bcc_chunk){ $headers = array(); $headers['From'] = 'From: '.get_bloginfo("name").'<'.$from.'>'; $headers['Bcc'] = 'Bcc: '.implode(', ', $bcc_chunk); wp_mail($to , $subject, $message, $headers); } return; } /******************************************VIEW ANNOUNCEMENTS END******************************************/ /******************************************MAIN DISPLAY BEGIN******************************************/ function dispHeader() { global $user_ID, $user_login; $numNew = $this->getNewMsgs(); $numAnn = $this->getAnnouncementsNum(); $msgBoxSize = $this->getUserNumMsgs(); $adminOps = $this->getAdminOps(); if ($adminOps['num_messages'] == 0 || current_user_can('manage_options')) $msgBoxTotal = __("Unlimited", "fep"); else $msgBoxTotal = $adminOps['num_messages']; $header = "
"; $header .= "
"; $header .= get_avatar($user_ID, 55)."

".__("Welcome", "fep").": ".$this->convertToDisplay($user_login)."
"; $header .= __("You have", "fep")." (".$numNew.") ".__("new messages", "fep"). " ".__("and", "fep")." (".$numAnn.") ".__("announcement(s)", "fep")."
"; if ($msgBoxTotal == __("Unlimited", "fep") || $msgBoxSize < $msgBoxTotal) $header .= __("Message box size", "fep").": ".$msgBoxSize." ".__("of", "fep")." ".$msgBoxTotal."

"; else $header .= "".__("Your Message Box Is Full! Please delete some messages.", "fep")."

"; $header .= "
"; return $header; } function dispMenu() { $numNew = $this->getNewMsgs_btn(); $numNewadm = $this->getNewMsgs_admin(); $numAnn = $this->getAnnouncementsNum_btn(); $menu = "
"; $menu .= "".__("Message Box".$numNew."", "fep").""; $menu .= "".__("Announcements".$numAnn."", "fep").""; $menu .= "".__("New Message", "fep").""; if($this->adminOps['hide_directory'] != 'on' || current_user_can('manage_options')) $menu .= "".__("Directory", "fep").""; $menu .= "".__("Settings", "fep").""; if(current_user_can('manage_options')) $menu .= "".__("Other's Message".$numNewadm."", "fep") . ""; $menu .="
"; $menu .= "
"; return $menu; } function dispNotify() { if ($this->success != ""){ $notify = "
".$this->success."
"; } else if ($this->error != "") { $notify = "
".$this->error."
"; } return $notify; } function dispFooter() { $footer = "
"; //End content //Maybe Add Notify if ($this->error != "" || $this->success != "") $footer .= $this->dispNotify(); if($this->adminOps['hide_branding'] != 'on') $footer .= ""; $footer .= "
"; //End main wrapper return $footer; } function dispDirectory() { if($this->adminOps['hide_directory'] == 'on' && !current_user_can('manage_options')) return; $users = $this->get_users(); $result = count_users(); $total = $result['total_users']; $adminOps = $this->getAdminOps(); if ($total) { $directory = "

".__("Total Users", "fep").": (".$total.")

"; $numPgs = $total / $adminOps['user_page']; if ($numPgs > 1) { $directory .= "

".__("Page", "fep").": "; for ($i = 0; $i < $numPgs; $i++) if ($_GET['upage'] != $i) $directory .= "".($i+1)." "; else $directory .= "[".($i+1)."] "; $directory .= "

"; } $directory .= ""; $a=0; foreach($users as $u) { $directory .= ""; $directory .= ""; if ($a) $a = 0; else $a = 1; } $directory .= "
".__("User", "fep")." ".__("Send Message", "fep")."
".$u->display_name."".__("Send Message", "fep")."
"; return $directory; } else { $this->error = __("No User!", "fep"); return; } } //Display the proper contents function displayAll() { global $user_ID,$wpdb; if ($user_ID) { //Finish the setup since these wouldn't work in the constructor $this->userOps = $this->getUserOps($user_ID); $this->setPageURLs(); //Add header $out = $this->dispHeader(); //Add Menu $out .= $this->dispMenu(); //Start the guts of the display if (isset($_GET['fepaction'])){ $switch = $_GET['fepaction']; }else{ $switch = '';} switch ($switch) { case 'newmessage': $out .= $this->dispNewMsg(); break; case 'checkmessage': $out .= $this->dispCheckMsg(); break; case 'viewmessage': $out .= $this->dispReadMsg(); break; case 'viewmessageadmin': if (current_user_can('manage_options')) $out .= $this->dispReadMsg_admin(); else $out .= $this->dispReadMsg(); break; case 'deletemessage': $out .= $this->dispDelMsg(); break; case 'deletemessageadmin': if (current_user_can('manage_options')) $out .= $this->dispDelMsg_admin(); else $out .= $this->dispDelMsg(); break; case 'directory': if($this->adminOps['hide_directory'] != 'on' || current_user_can('manage_options')) $out .= $this->dispDirectory(); else $out .= $this->dispMsgBox(); break; case 'settings': $out .= $this->dispUserPage(); break; case 'viewannouncements': $out .= $this->dispAnnouncement(); break; case 'addannouncement': $out .= $this->addAnnouncement(); break; case 'viewallmgs': if (current_user_can('manage_options')) $out .= $this->dispMsgBox_admin(); else $out .= $this->dispMsgBox(); break; default: //Message box is shown by Default $out .= $this->dispMsgBox(); break; } //Add footer $out .= $this->dispFooter(); } else { $out = "

".__("You must be logged-in to view your message.", "fep")."

"; } return $out; } /******************************************MAIN DISPLAY END******************************************/ /******************************************MISC. FUNCTIONS BEGIN******************************************/ /** * Creates a token usable in a form * @return string */ function session(){ if(!isset($_SESSION)) { session_start(); } } function getToken(){ $token = sha1(mt_rand()); if(!isset($_SESSION['tokens'])){ $_SESSION['tokens'] = array($token => 1); }else{ $_SESSION['tokens'][$token] = 1; } return $token; } /** * Check if a token is valid. Removes it from the valid tokens list * @param string $token The token * @return bool */ function isTokenValid($token){ if(!empty($_SESSION['tokens'][$token])){ unset($_SESSION['tokens'][$token]); return true; } return false; } //Check is user blocked by admin function have_permission(){ global $current_user; $adminOps = $this->getAdminOps(); $wpusers = (array) explode(',', $adminOps['have_permission']); $valid_wpusers = array(); foreach($wpusers as $wpuser){ $wpuser = trim($wpuser); if($wpuser!=''){ $user = get_user_by('login', $wpuser); if($user){ $valid_wpusers[] = $user->ID; } $valid_wpusers = array_unique($valid_wpusers); if(in_array($current_user->ID, $valid_wpusers)){ return false; } } } return true; } function get_users() { global $wpdb; if (isset($_GET['upage'])){ $page = $_GET['upage']; }else{$page = 0;} $adminOps = $this->getAdminOps(); $start = $page * $adminOps['user_page']; $end = $adminOps['user_page']; $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)); return $users; } function get_form_buttons() { $button = ' '; return $button; } function output_filter($string) { $parser = new fepBBCParser(); $html = stripslashes($parser->bbc2html($string)); return ent2ncr($html); } function input_filter($string) { $newStr = esc_attr($string); return wp_strip_all_tags($newStr); } function getUserNumMsgs() { global $wpdb, $user_ID; $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)); $num = $wpdb->num_rows; return $num; } function formatDate($date) { $now = current_time('mysql'); //return date('M d, h:i a', strtotime($date)); return human_time_diff(strtotime($date),strtotime($now)).' ago'; } function TimeDelay($DeTime) { global $wpdb, $user_ID; $now = current_time('mysql'); $Dtime = $DeTime * 60; $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)); $diff = strtotime($now) - strtotime($Prev); $diffr = $diff/60; $next = strtotime($Prev) + $Dtime; $Ntime = human_time_diff(strtotime($now),$next); return array('diffr' => $diffr, 'time' => $Ntime); } function is_positive($str) { return (is_numeric($str) && $str > 0 && $str == round($str)); } function getNewMsgs() { global $wpdb, $user_ID; $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)); return $wpdb->num_rows; } function getNewMsgs_btn(){ if ($this->getNewMsgs()){ $newmgs = " ("; $newmgs .= $this->getNewMsgs(); $newmgs .=")"; } else { $newmgs = "";} return $newmgs; } function getNewMsgs_admin() { global $wpdb, $user_ID; $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)); if ($wpdb->num_rows){ $newmgs = " ("; $newmgs .= $wpdb->num_rows; $newmgs .=")"; } else { $newmgs ="";} return $newmgs; } function autoembed($string) { global $wp_embed; if (is_object($wp_embed)) return $wp_embed->autoembed($string); else return $string; } function get_version() { $plugin_data = implode('', file($this->pluginDir."front-end-pm.php")); if (preg_match("|Version:(.*)|i", $plugin_data, $version)) $version = $version[1]; return $version; } /******************************************MISC. FUNCTIONS END******************************************/ } //END CLASS } //ENDIF ?>