public static function get_header($headers, $key)
{
// make sure we typecast $headers to an array
// mandrill sets headers as an object, while our RBE IMAP class uses an array
$headers = (array) $headers;
if (empty($headers[$key])) {
bp_rbe_log($key . ' parser - empty key');
return false;
}
if ($key == 'To' && strpos($headers[$key], '@') === false) {
bp_rbe_log($key . ' parser - missing email address');
return false;
}
// Sender is attempting to send to multiple recipients in the "To" header
// A legit BP reply will not add multiple recipients, so let's return false
if ($key == 'To' && strpos($headers['To'], ',') !== false) {
bp_rbe_log($key . ' parser - multiple recipients - so stop!');
return false;
}
// grab email address in between triangular brackets if they exist
// strip the rest
$lbracket = strpos($headers[$key], '<');
if ($lbracket !== false) {
$rbracket = strpos($headers[$key], '>');
$headers[$key] = substr($headers[$key], ++$lbracket, $rbracket - $lbracket);
}
//bp_rbe_log( $key . ' parser - ' . $headers[$key] );
return $headers[$key];
}
/** * 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; }