get_header() 공개 정적인 메소드

If a header includes the user's name, it will return just the email address. eg. r-a-y -> test@gmail.com
public static get_header ( array | object $headers, string $key ) : mixed
$headers array | object Email headers
$key string The key we want to check against the array.
리턴 mixed Either the email header on success or false on failure
 /**
  * Webhook parser class method for SendGrid.
  */
 public function webhook_parser()
 {
     if (empty($_SERVER['HTTP_USER_AGENT']) || !empty($_SERVER['HTTP_USER_AGENT']) && 0 !== strpos($_SERVER['HTTP_USER_AGENT'], 'SendGrid')) {
         return;
     }
     if (empty($_POST) || empty($_POST['headers'])) {
         return;
     }
     bp_rbe_log('- SendGrid webhook received -');
     // Format email headers to fit RBE spec.
     $temp = explode("\n", $_POST['headers']);
     $headers = array();
     foreach ($temp as $line) {
         $colun = strpos($line, ':');
         if (false === $colun) {
             continue;
         }
         $key = substr($line, 0, $colun);
         $headers[$key] = stripslashes(trim(substr($line, $colun + 1)));
     }
     $data = array('headers' => $headers, 'to_email' => BP_Reply_By_Email_Parser::get_header($headers, 'To'), 'from_email' => BP_Reply_By_Email_Parser::get_header($headers, 'From'), 'content' => $_POST['text'], 'subject' => $_POST['subject']);
     $parser = BP_Reply_By_Email_Parser::init($data, 1);
     if (is_wp_error($parser)) {
         do_action('bp_rbe_no_match', $parser, $data, 1, false);
     }
     bp_rbe_log('- Webhook parsing completed -');
     die;
 }
예제 #2
0
/**
 * Logs no match errors during RBE parsing.
 *
 * Also sends a failure message back to the original sender for feedback
 * purposes if enabled.
 *
 * @since 1.0-RC3
 *
 * @uses bp_rbe_log() Logs error messages in a custom log
 * @param object $parser The WP_Error object.
 * @param array $data {
 *     An array of arguments.
 *
 *     @type array $headers Email headers.
 *     @type string $to_email The 'To' email address.
 *     @type string $from_email The 'From' email address.
 *     @type string $content The email body content.
 *     @type string $subject The email subject line.
 *     @type bool $is_html Whether the email content is HTML or not.
 * }
 * @param int $i The current message number
 * @param resource|bool $imap The IMAP connection if passed. Boolean false if not.
 */
function bp_rbe_log_no_matches($parser, $data, $i, $imap)
{
    $log = $message = false;
    $type = is_wp_error($parser) ? $parser->get_error_code() : false;
    // log messages based on the type
    switch ($type) {
        /** RBE **********************************************************/
        case 'no_address_tag':
            $log = __('error - no address tag could be found', 'bp-rbe');
            break;
        case 'no_user_id':
            $log = __('error - no user ID could be found', 'bp-rbe');
            $sitename = wp_specialchars_decode(get_blog_option(bp_get_root_blog_id(), 'blogname'), ENT_QUOTES);
            $message = sprintf(__('Hi there,

You tried to use the email address - %s - to reply by email.  Unfortunately, we could not find this email address in our system.

This can happen in a couple of different ways:
* You have configured your email client to reply with a custom "From:" email address.
* You read email addressed to more than one account inside of a single Inbox.

Make sure that, when replying by email, your "From:" email address is the same as the address you\'ve registered at %s.

If you have any questions, please let us know.', 'bp-rbe'), BP_Reply_By_Email_Parser::get_header($data['headers'], 'From'), $sitename);
            break;
        case 'user_is_spammer':
            $log = __('notice - user is marked as a spammer.  reply not posted!', 'bp-rbe');
            break;
        case 'no_params':
            $log = __('error - no parameters were found', 'bp-rbe');
            break;
        case 'no_reply_body':
            $log = __('error - body message for reply was empty', 'bp-rbe');
            $message = sprintf(__('Hi there,

Your reply could not be posted because we could not find the "%s" marker in the body of your email.

In the future, please make sure you reply *above* this line for your comment to be posted on the site.

For reference, your entire reply was:

"%s".

If you have any questions, please let us know.', 'bp-rbe'), bp_rbe_get_marker(), $data['content']);
            break;
            /** ACTIVITY *****************************************************/
        /** ACTIVITY *****************************************************/
        case 'root_activity_deleted':
            $log = __('error - root activity update was deleted before this could be posted', 'bp-rbe');
            $message = sprintf(__('Hi there,

Your reply:

"%s"

Could not be posted because the activity entry you were replying to no longer exists.

We apologize for any inconvenience this may have caused.', 'bp-rbe'), BP_Reply_By_Email_Parser::get_body($data['content'], $data['is_html'], true, $i));
            break;
        case 'root_or_parent_activity_deleted':
            $log = __('error - root or parent activity update was deleted before this could be posted', 'bp-rbe');
            $message = sprintf(__('Hi there,

Your reply:

"%s"

Could not be posted because the activity entry you were replying to no longer exists.

We apologize for any inconvenience this may have caused.', 'bp-rbe'), BP_Reply_By_Email_Parser::get_body($data['content'], $data['is_html'], true, $i));
            break;
            /** GROUP FORUMS *************************************************/
        /** GROUP FORUMS *************************************************/
        case 'user_not_group_member':
            $log = __('error - user is not a member of the group. forum reply not posted.', 'bp-rbe');
            $message = sprintf(__('Hi there,

Your forum reply:

"%s"

Could not be posted because you are no longer a member of this group.  To comment on the forum thread, please rejoin the group.

We apologize for any inconvenience this may have caused.', 'bp-rbe'), BP_Reply_By_Email_Parser::get_body($data['content'], $data['is_html'], true, $i));
            break;
        case 'user_banned_from_group':
            $log = __('notice - user is banned from group. forum reply not posted.', 'bp-rbe');
            break;
        case 'new_forum_topic_empty':
            $log = __('error - body message for new forum topic was empty', 'bp-rbe');
            $message = __('Hi there,

We could not post your new forum topic by email because we could not find any text in the body of the email.

In the future, please make sure to type something in your email! :)

If you have any questions, please let us know.', 'bp-rbe');
            break;
        case 'forum_reply_exists':
            $log = __('error - forum reply already exists in topic', 'bp-rbe');
            $message = sprintf(__('Hi there,

Your forum reply:

"%s"

Could not be posted because you have already posted the same message in the forum topic you were attempting to reply to.

We apologize for any inconvenience this may have caused.', 'bp-rbe'), BP_Reply_By_Email_Parser::get_body($data['content'], $data['is_html'], true, $i));
            break;
        case 'forum_reply_fail':
            $log = __('error - forum topic was deleted before reply could be posted', 'bp-rbe');
            $message = sprintf(__('Hi there,

Your forum reply:

"%s"

Could not be posted because the forum topic you were replying to no longer exists.

We apologize for any inconvenience this may have caused.', 'bp-rbe'), BP_Reply_By_Email_Parser::get_body($data['content'], $data['is_html'], true, $i));
            break;
        case 'forum_topic_fail':
            $log = __('error - forum topic failed to be created', 'bp-rbe');
            // this is a pretty generic message...
            $message = sprintf(__('Hi there,

Your forum topic titled "%s" could not be posted due to an error.

We apologize for any inconvenience this may have caused.', 'bp-rbe'), $data['subject']);
            break;
            /** PRIVATE MESSAGES *********************************************/
            // most likely a spammer trying to infiltrate an existing PM thread
        /** PRIVATE MESSAGES *********************************************/
        // most likely a spammer trying to infiltrate an existing PM thread
        case 'private_message_not_in_thread':
            $log = __('error - user is not a part of the existing PM conversation', 'bp-rbe');
            break;
        case 'private_message_thread_deleted':
            $log = __('error - private message thread was deleted by all parties before this could be posted', 'bp-rbe');
            $message = sprintf(__('Hi there,

Your private message reply:

"%s"

Could not be posted because the private message thread you were replying to no longer exists.

We apologize for any inconvenience this may have caused.', 'bp-rbe'), BP_Reply_By_Email_Parser::get_body($data['content'], $data['is_html'], true, $i));
            break;
        case 'private_message_fail':
            $log = __('error - private message failed to post', 'bp-rbe');
            $message = sprintf(__('Hi there,

Your reply:

"%s"

Could not be posted due to an error.

We apologize for any inconvenience this may have caused.', 'bp-rbe'), BP_Reply_By_Email_Parser::get_body($data['content'], $data['is_html'], true, $i));
            break;
            // 3rd-party plugins can filter the two variables below to add their own logs and email messages.
        // 3rd-party plugins can filter the two variables below to add their own logs and email messages.
        default:
            $log = apply_filters('bp_rbe_extend_log_no_match', $log, $type, $data, $i, $imap);
            $message = apply_filters('bp_rbe_extend_log_no_match_email_message', $message, $type, $data, $i, $imap);
            break;
    }
    // internal logging
    if ($log) {
        bp_rbe_log(sprintf(__('Message #%d: %s', 'bp-rbe'), $i, $log));
    }
    // failure message to author
    // if you want to turn off failure messages, use the filter below
    if (apply_filters('bp_rbe_enable_failure_message', true) && $message) {
        $to = BP_Reply_By_Email_Parser::get_header($data['headers'], 'From');
        if (!empty($to)) {
            $sitename = wp_specialchars_decode(get_blog_option(bp_get_root_blog_id(), 'blogname'), ENT_QUOTES);
            $subject = sprintf(__('[%s] Your Reply By Email message could not be posted', 'bp-rbe'), $sitename);
            // temporarily remove RBE mail filter by wiping out email querystring
            add_filter('bp_rbe_querystring', '__return_false');
            // send email
            wp_mail($to, $subject, $message);
            // add it back
            remove_filter('bp_rbe_querystring', '__return_false');
        }
    }
}
 /**
  * Post by email handler.
  *
  * Validate data and post on success.
  *
  * @param bool $retval True by default.
  * @param array $data {
  *     An array of arguments.
  *
  *     @type array $headers Email headers.
  *     @type string $content The email body content.
  *     @type string $subject The email subject line.
  *     @type int $user_id The user ID who sent the email.
  *     @type bool $is_html Whether the email content is HTML or not.
  *     @type int $i The email message number.
  * }
  * @param array $params Parsed paramaters from the email address querystring.
  *   See {@link BP_Reply_By_Email_Parser::get_parameters()}.
  * @return array|object Array of the parsed item on success. WP_Error object
  *  on failure.
  */
 public function post($retval, $data, $params)
 {
     global $bp;
     $comment_id = !empty($params[$this->secondary_item_id_param]) ? $params[$this->secondary_item_id_param] : false;
     $i = $data['i'];
     // this means that the current email is a BP Doc reply
     // let's proceed!
     if (!empty($comment_id)) {
         // it's important to let RBE know what's happening during the process
         // for debugging purposes
         //
         // use bp_rbe_log() to log anything you want
         // in this case, we're letting RBE know that we're in the process of
         // rendering a comment reply
         bp_rbe_log('Message #' . $i . ': this is a BP Doc comment reply');
         // get parent comment data
         $comment = get_comment($comment_id);
         // parent comment doesn't exist or was deleted
         if (empty($comment)) {
             // when a condition for posting isn't met, return a WP_Error object.
             // next, log it under the internal_rbe_log()_method
             // and optionally, prep a failure message under the failure_message_to_sender() method
             //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bp_doc_parent_comment_deleted' );
             return new WP_Error('bp_doc_parent_comment_deleted', '', $data);
         }
         // parent comment status checks
         switch ($comment->comment_approved) {
             case 'spam':
                 //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bp_doc_parent_comment_spam' );
                 return new WP_Error('bp_doc_parent_comment_spam', '', $data);
                 break;
             case 'trash':
                 //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bp_doc_parent_comment_deleted' );
                 return new WP_Error('bp_doc_parent_comment_deleted', '', $data);
                 break;
             case '0':
                 //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bp_doc_parent_comment_unapproved' );
                 return new WP_Error('bp_doc_parent_comment_unapproved', '', $data);
                 break;
         }
         // get doc settings
         $doc_settings = get_post_meta($comment->comment_post_ID, 'bp_docs_settings', true);
         // set temporary variable
         $bp->rbe = $bp->rbe->temp = new stdClass();
         // get group ID
         // $bp->rbe->temp->group_id gets passed to BP_Reply_By_Email::set_group_id()
         $group_id = $bp->rbe->temp->group_id = $params[$this->item_id_param];
         // get user ID
         $user_id = $data['user_id'];
         // check to see if the user can post comments for the group doc in question
         //
         // bp_docs_user_can( 'post_comments', $user_id, $group_id ) doesn't work the way I want it to
         // using the doc's comment settings as a guideline
         // check the comment settings for the doc
         switch ($doc_settings['post_comments']) {
             // this means that the comment settings for the doc recently switched to 'no-one'
             case 'no-one':
                 //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bp_doc_comment_change_to_noone' );
                 return new WP_Error('bp_doc_comment_change_to_noone', '', $data);
                 break;
                 // if the doc only allows group admins and mods to comment, return false for regular group members
             // if the doc only allows group admins and mods to comment, return false for regular group members
             case 'admins-mods':
                 // get the email address of the replier
                 $user_email = BP_Reply_By_Email_Parser::get_header($data['headers'], 'From');
                 // get an array of group admin / mod email addresses
                 // note: email addresses are set as key, not value
                 $admin_mod_emails = $this->get_admin_mod_user_emails($group_id);
                 // if the replier's email address does not match a group admin or mod, stop now!
                 if (!isset($admin_mod_emails[$user_email])) {
                     //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bp_doc_user_not_admin_mod' );
                     return new WP_Error('bp_doc_user_not_admin_mod', '', $data);
                 }
                 break;
                 // if the doc allows any group member to comment, check if member is still part of
                 // the group and not banned
             // if the doc allows any group member to comment, check if member is still part of
             // the group and not banned
             case 'group-members':
                 if (!groups_is_user_member($user_id, $group_id)) {
                     //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bp_doc_user_not_member' );
                     return new WP_Error('bp_doc_user_not_member', '', $data);
                 }
                 if (groups_is_user_banned($user_id, $group_id)) {
                     //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bp_doc_user_banned' );
                     return new WP_Error('bp_doc_user_banned', '', $data);
                 }
                 break;
         }
         /* okay! we should be good to post now! */
         // get the userdata
         $userdata = get_userdata($user_id);
         // we're using wp_insert_comment() instead of wp_new_comment()
         // why? because wp_insert_comment() bypasses all the WP comment hooks, which is good for us!
         $new_comment_id = wp_insert_comment(array('user_id' => $user_id, 'comment_post_ID' => $comment->comment_post_ID, 'comment_content' => $data['content'], 'comment_parent' => $comment_id, 'comment_author' => $userdata->user_nicename, 'comment_author_url' => '', 'comment_author_email' => $userdata->user_email, 'comment_author_IP' => '', 'comment_agent' => '', 'comment_type' => ''));
         // comment successfully posted!
         if (!empty($new_comment_id)) {
             // more internal logging
             bp_rbe_log('Message #' . $i . ': BP Doc comment reply successfully posted!');
             /* now let's record the activity item for this comment */
             // override BP Docs' default comment activity action
             add_filter('bp_docs_comment_activity_action', array($this, 'comment_activity_action'));
             // now post the activity item with BP Docs' special class method
             if (class_exists('BP_Docs_BP_Integration')) {
                 // BP Docs v1.1.x support
                 $activity_id = BP_Docs_BP_Integration::post_comment_activity($new_comment_id);
             } else {
                 // BP Docs v1.2.x support
                 $activity_id = BP_Docs_Component::post_comment_activity($new_comment_id);
             }
             // special hook for RBE activity items
             // if you're adding an activity entry in this method, remember to add this hook after posting
             // your activity item in this method!
             do_action('bp_rbe_new_activity', array('activity_id' => $activity_id, 'type' => $this->activity_type, 'user_id' => $user_id, 'item_id' => $group_id, 'secondary_item_id' => $comment_id, 'content' => $data['content']));
             // remove the filter after posting
             remove_filter('bp_docs_comment_activity_action', array($this, 'comment_activity_action'));
             return array('bp_doc_comment_id' => $new_comment_id);
         } else {
             //do_action( 'bp_rbe_imap_no_match', $connection, $i, $headers, 'bp_doc_new_comment_fail' );
             return new WP_Error('bp_doc_new_comment_fail', '', $data);
         }
     }
 }
 /**
  * The main method we use to parse an IMAP inbox.
  */
 public function run()
 {
     // $instance must be initialized before we go on!
     if (self::$instance === false) {
         return false;
     }
     // If safe mode isn't on, then let's set the execution time to unlimited
     if (!ini_get('safe_mode')) {
         set_time_limit(0);
     }
     // Try to connect
     $connect = $this->connect();
     if (!$connect) {
         return false;
     }
     // Total duration we should keep the IMAP stream alive for in seconds
     $duration = bp_rbe_get_execution_time();
     bp_rbe_log('--- Keep alive for ' . $duration / 60 . ' minutes ---');
     bp_rbe_remove_imap_lock();
     // Mark the current timestamp, mark the future time when we should close the IMAP connection;
     // Do our parsing until $future > $now; re-mark the timestamp at end of loop... rinse and repeat!
     for ($now = time(), $future = time() + $duration; $future > $now; $now = time()) {
         // Get number of messages
         $message_count = imap_num_msg($this->connection);
         // If there are messages in the inbox, let's start parsing!
         if ($message_count != 0) {
             // According to this:
             // http://www.php.net/manual/pl/function.imap-headerinfo.php#95012
             // This speeds up rendering the email headers... could be wrong
             imap_headers($this->connection);
             bp_rbe_log('- Checking inbox -');
             // Loop through each email message
             for ($i = 1; $i <= $message_count; ++$i) {
                 // flush object cache if necessary
                 //
                 // we only flush the cache if the default object cache is in use
                 // why? b/c the default object cache is only meant for single page loads and
                 // since RBE runs in the background, we need to flush the object cache so WP
                 // will do a direct DB query for the next data fetch
                 if (!wp_using_ext_object_cache()) {
                     wp_cache_flush();
                 }
                 self::$html = false;
                 $content = self::body_parser($this->connection, $i);
                 $headers = $this->get_mail_headers($this->connection, $i);
                 $data = array('headers' => $headers, 'to_email' => BP_Reply_By_Email_Parser::get_header($headers, 'To'), 'from_email' => BP_Reply_By_Email_Parser::get_header($headers, 'From'), 'content' => $content, 'is_html' => self::$html, 'subject' => imap_utf8(BP_Reply_By_Email_Parser::get_header($headers, 'Subject')));
                 $parser = BP_Reply_By_Email_Parser::init($data, $i);
                 if (is_wp_error($parser)) {
                     //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, $parser->get_error_code() );
                     do_action('bp_rbe_no_match', $parser, $data, $i, $this->connection);
                 }
                 // do something during the loop
                 // we mark the message for deletion here via this hook
                 do_action('bp_rbe_imap_loop', $this->connection, $i);
                 // unset some variables at the end of the loop
                 unset($content, $headers, $data, $parser);
             }
             // do something after the loop
             do_action('bp_rbe_imap_after_loop', $this->connection);
         }
         // stop the loop if necessary
         if (bp_rbe_should_stop()) {
             if ($this->close()) {
                 bp_rbe_log('--- Manual termination of connection confirmed! Kaching! ---');
             } else {
                 bp_rbe_log('--- Error - invalid connection during manual termination ---');
             }
             remove_action('shutdown', 'bp_rbe_spawn_inbox_check');
             exit;
         }
         // Give IMAP server a break
         sleep(10);
         // If the IMAP connection is down, reconnect
         if (!imap_ping($this->connection)) {
             bp_rbe_log('-- IMAP connection is down, attempting to reconnect... --');
             if (bp_rbe_is_connecting(array('clearcache' => true))) {
                 bp_rbe_log('--- RBE is already attempting to connect - stopping connection attempt ---');
                 continue;
             }
             // add lock marker before connecting
             bp_rbe_add_imap_lock();
             // attempt to reconnect
             $reopen = BP_Reply_By_Email_Connect::init(array('reconnect' => true), $this->connection);
             if ($reopen) {
                 bp_rbe_log('-- Reconnection successful! --');
             } else {
                 bp_rbe_log('-- Reconnection failed! :( --');
                 bp_rbe_log('Cannot connect: ' . imap_last_error());
                 // cleanup RBE after failure
                 bp_rbe_cleanup();
                 remove_action('shutdown', 'bp_rbe_spawn_inbox_check');
                 exit;
             }
         }
         // Unset some variables to clear some memory
         unset($message_count);
     }
     if ($this->close()) {
         bp_rbe_log('--- Closing current connection automatically ---');
     } else {
         bp_rbe_log('--- Invalid connection during close time ---');
     }
     // autoconnect is off
     if (1 !== (int) bp_rbe_get_setting('keepaliveauto', array('refetch' => true))) {
         remove_action('shutdown', 'bp_rbe_spawn_inbox_check');
         // sleep a bit before autoconnecting again
     } else {
         sleep(5);
     }
     exit;
 }