Skip to content

Commit

Permalink
[+]: update code style + fix regex
Browse files Browse the repository at this point in the history
  • Loading branch information
voku committed Jul 19, 2019
1 parent 866f28b commit 53c58a7
Show file tree
Hide file tree
Showing 12 changed files with 2,524 additions and 2,305 deletions.
180 changes: 90 additions & 90 deletions examples/callback_csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,63 @@
/**
* Callback (action) function
*
* @param int $msgnum the message number returned by Bounce Mail Handler
* @param string $bounceType the bounce type:
* 'antispam','autoreply','concurrent','content_reject','command_reject','internal_error','defer','delayed'
* =>
* array('remove'=>0,'bounce_type'=>'temporary'),'dns_loop','dns_unknown','full','inactive','latin_only','other','oversize','outofoffice','unknown','unrecognized','user_reject','warning'
* @param string $email the target email address
* @param string $subject the subject, ignore now
* @param string $xheader the XBounceHeader from the mail
* @param boolean $remove remove status, 1 means removed, 0 means not removed
* @param string|boolean $ruleNo Bounce Mail Handler detect rule no.
* @param string|boolean $ruleCat Bounce Mail Handler detect rule category.
* @param int $totalFetched total number of messages in the mailbox
* @param string $body Bounce Mail Body
* @param string $headerFull Bounce Mail Header
* @param string $bodyFull Bounce Mail Body (full)
* @param int $msgnum the message number returned by Bounce Mail Handler
* @param string $bounceType the bounce type:
* 'antispam','autoreply','concurrent','content_reject','command_reject','internal_error','defer','delayed'
* =>
* array('remove'=>0,'bounce_type'=>'temporary'),'dns_loop','dns_unknown','full','inactive','latin_only','other','oversize','outofoffice','unknown','unrecognized','user_reject','warning'
* @param string $email the target email address
* @param string $subject the subject, ignore now
* @param string $xheader the XBounceHeader from the mail
* @param bool $remove remove status, 1 means removed, 0 means not removed
* @param bool|string $ruleNo bounce Mail Handler detect rule no
* @param bool|string $ruleCat bounce Mail Handler detect rule category
* @param int $totalFetched total number of messages in the mailbox
* @param string $body Bounce Mail Body
* @param string $headerFull Bounce Mail Header
* @param string $bodyFull Bounce Mail Body (full)
*
* @return boolean
* @return bool
*/
function callbackAction($msgnum, $bounceType, $email, $subject, $xheader, $remove, $ruleNo = false, $ruleCat = false, $totalFetched = 0, $body = '', $headerFull = '', $bodyFull = '')
function callbackAction($msgnum, $bounceType, $email, $subject, $xheader, $remove, $ruleNo = false, $ruleCat = false, $totalFetched = 0, $body = '', $headerFull = '', $bodyFull = ''): bool
{
$currentTime = date('Y-m-d H:i:s', time());
$currentTime = \date('Y-m-d H:i:s', \time());

$displayData = prepData($email, $bounceType, $remove);
$bounceType = $displayData['bounce_type'];
$emailName = $displayData['emailName'];
$emailAddy = $displayData['emailAddy'];
$remove = $displayData['remove'];
$removeraw = $displayData['removestat'];
$displayData = prepData($email, $bounceType, $remove);
$bounceType = $displayData['bounce_type'];
$emailName = $displayData['emailName'];
$emailAddy = $displayData['emailAddy'];
$remove = $displayData['remove'];
$removeraw = $displayData['removestat'];

$msg = $msgnum . ',' . $currentTime . ',' . $ruleNo . ',' . $ruleCat . ',' . $bounceType . ',' . $removeraw . ',' . $email . ',' . $subject;
$msg = $msgnum . ',' . $currentTime . ',' . $ruleNo . ',' . $ruleCat . ',' . $bounceType . ',' . $removeraw . ',' . $email . ',' . $subject;

$filename = 'logs/bouncelog_' . date('m') . date('Y') . '.csv';
if (!file_exists($filename)) {
$tmsg = 'Msg#,Current Time,Rule Number,Rule Category,Bounce Type,Status,Email,Subject' . "\n" . $msg;
} else {
$fileContents = file_get_contents($filename);
$filename = 'logs/bouncelog_' . \date('m') . \date('Y') . '.csv';
if (!\file_exists($filename)) {
$tmsg = 'Msg#,Current Time,Rule Number,Rule Category,Bounce Type,Status,Email,Subject' . "\n" . $msg;
} else {
$fileContents = \file_get_contents($filename);

if (stripos($fileContents, "\n" . $msgnum . ',') !== false) {
$doPutFile = false;
if (\stripos($fileContents, "\n" . $msgnum . ',') !== false) {
$doPutFile = false;
}

$tmsg = $msg;
}

$tmsg = $msg;
}

$handle = fopen($filename, 'a');
if ($handle) {
if (fwrite($handle, $tmsg . "\n") === false) {
echo 'Cannot write message<br />';

$handle = \fopen($filename, 'ab');
if ($handle) {
if (\fwrite($handle, $tmsg . "\n") === false) {
echo 'Cannot write message<br />';
}
\fclose($handle);
} else {
echo 'Cannot open file to append<br />';
}
fclose($handle);
} else {
echo 'Cannot open file to append<br />';
}

echo $msgnum . ': ' . $ruleNo . ' | ' . $ruleCat . ' | ' . $bounceType . ' | ' . $remove . ' | ' . $email . ' | ' . $subject . "<br />\n";
echo $msgnum . ': ' . $ruleNo . ' | ' . $ruleCat . ' | ' . $bounceType . ' | ' . $remove . ' | ' . $email . ' | ' . $subject . "<br />\n";

return true;
return true;
}

/**
Expand All @@ -77,49 +77,49 @@ function callbackAction($msgnum, $bounceType, $email, $subject, $xheader, $remov
*/
function prepData($email, $bounce_type, $remove)
{
$data['bounce_type'] = trim($bounce_type);
$data['email'] = '';
$data['emailName'] = '';
$data['emailAddy'] = '';
$data['remove'] = '';
if (strpos($email, '<') !== false) {
$pos_start = strpos($email, '<');
$data['emailName'] = trim(substr($email, 0, $pos_start));
$data['emailAddy'] = substr($email, $pos_start + 1);
$pos_end = strpos($data['emailAddy'], '>');
if ($pos_end) {
$data['emailAddy'] = substr($data['emailAddy'], 0, $pos_end);
$data['bounce_type'] = \trim($bounce_type);
$data['email'] = '';
$data['emailName'] = '';
$data['emailAddy'] = '';
$data['remove'] = '';
if (\strpos($email, '<') !== false) {
$pos_start = \strpos($email, '<');
$data['emailName'] = \trim(\substr($email, 0, $pos_start));
$data['emailAddy'] = \substr($email, $pos_start + 1);
$pos_end = \strpos($data['emailAddy'], '>');
if ($pos_end) {
$data['emailAddy'] = \substr($data['emailAddy'], 0, $pos_end);
}
}

// replace the < and > able so they display on screen
// replace the < and > able so they display on screen
$email = \str_replace(['<', '>'], ['&lt;', '&gt;'], $email);

// replace the "TO:<" with nothing
$email = \str_ireplace('TO:<', '', $email);

$data['email'] = $email;

// account for legitimate emails that have no bounce type
if (\trim($bounce_type) == '') {
$data['bounce_type'] = 'none';
}
}

// replace the < and > able so they display on screen
// replace the < and > able so they display on screen
$email = str_replace(array('<', '>'), array('&lt;', '&gt;'), $email);

// replace the "TO:<" with nothing
$email = str_ireplace('TO:<', '', $email);

$data['email'] = $email;

// account for legitimate emails that have no bounce type
if (trim($bounce_type) == '') {
$data['bounce_type'] = 'none';
}

// change the remove flag from true or 1 to textual representation
if (stripos($remove, 'moved') !== false && stripos($remove, 'hard') !== false) {
$data['removestat'] = 'moved (hard)';
$data['remove'] = '<span style="color:red;">' . 'moved (hard)' . '</span>';
} elseif (stripos($remove, 'moved') !== false && stripos($remove, 'soft') !== false) {
$data['removestat'] = 'moved (soft)';
$data['remove'] = '<span style="color:gray;">' . 'moved (soft)' . '</span>';
} elseif ($remove == true || $remove == '1') {
$data['removestat'] = 'deleted';
$data['remove'] = '<span style="color:red;">' . 'deleted' . '</span>';
} else {
$data['removestat'] = 'not deleted';
$data['remove'] = '<span style="color:gray;">' . 'not deleted' . '</span>';
}

return $data;

// change the remove flag from true or 1 to textual representation
if (\stripos($remove, 'moved') !== false && \stripos($remove, 'hard') !== false) {
$data['removestat'] = 'moved (hard)';
$data['remove'] = '<span style="color:red;">' . 'moved (hard)' . '</span>';
} elseif (\stripos($remove, 'moved') !== false && \stripos($remove, 'soft') !== false) {
$data['removestat'] = 'moved (soft)';
$data['remove'] = '<span style="color:gray;">' . 'moved (soft)' . '</span>';
} elseif ($remove == true || $remove == '1') {
$data['removestat'] = 'deleted';
$data['remove'] = '<span style="color:red;">' . 'deleted' . '</span>';
} else {
$data['removestat'] = 'not deleted';
$data['remove'] = '<span style="color:gray;">' . 'not deleted' . '</span>';
}

return $data;
}
150 changes: 75 additions & 75 deletions examples/callback_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,53 @@
* This is a sample callback function for PHPMailer-BMH (Bounce Mail Handler).
* This callback function will echo the results of the BMH processing.
*
* @param int $msgnum the message number returned by Bounce Mail Handler
* @param string $bounceType the bounce type:
* 'antispam','autoreply','concurrent','content_reject','command_reject','internal_error','defer','delayed'
* =>
* array('remove'=>0,'bounce_type'=>'temporary'),'dns_loop','dns_unknown','full','inactive','latin_only','other','oversize','outofoffice','unknown','unrecognized','user_reject','warning'
* @param string $email the target email address
* @param string $subject the subject, ignore now
* @param string $xheader the XBounceHeader from the mail
* @param boolean $remove remove status, 1 means removed, 0 means not removed
* @param string|boolean $ruleNo Bounce Mail Handler detect rule no.
* @param string|boolean $ruleCat Bounce Mail Handler detect rule category.
* @param int $totalFetched total number of messages in the mailbox
* @param string $body Bounce Mail Body
* @param string $headerFull Bounce Mail Header
* @param string $bodyFull Bounce Mail Body (full)
* @param int $msgnum the message number returned by Bounce Mail Handler
* @param string $bounceType the bounce type:
* 'antispam','autoreply','concurrent','content_reject','command_reject','internal_error','defer','delayed'
* =>
* array('remove'=>0,'bounce_type'=>'temporary'),'dns_loop','dns_unknown','full','inactive','latin_only','other','oversize','outofoffice','unknown','unrecognized','user_reject','warning'
* @param string $email the target email address
* @param string $subject the subject, ignore now
* @param string $xheader the XBounceHeader from the mail
* @param bool $remove remove status, 1 means removed, 0 means not removed
* @param bool|string $ruleNo bounce Mail Handler detect rule no
* @param bool|string $ruleCat bounce Mail Handler detect rule category
* @param int $totalFetched total number of messages in the mailbox
* @param string $body Bounce Mail Body
* @param string $headerFull Bounce Mail Header
* @param string $bodyFull Bounce Mail Body (full)
*
* @return boolean
* @return bool
*/
function callbackAction($msgnum, $bounceType, $email, $subject, $xheader, $remove, $ruleNo = false, $ruleCat = false, $totalFetched = 0, $body = '', $headerFull = '', $bodyFull = '')
function callbackAction($msgnum, $bounceType, $email, $subject, $xheader, $remove, $ruleNo = false, $ruleCat = false, $totalFetched = 0, $body = '', $headerFull = '', $bodyFull = ''): bool
{

// sample mysql code
if ($remove == true || $remove == '1') {
echo "note: sample code would have set the database to allowed='false'<br />";
/*
$link = mysqli_connect("localhost","username","password");
$sql = "SELECT id FROM mailinglist WHERE email = '" . mysqli_real_escape_string($link, $email) . "'";
$result = mysqli_query($sql);
if ( $result ) {
while($row = mysqli_fetch_array($result)) {
$sql_update = "UPDATE mailinglist SET allowed='false' WHERE email = '" . $email . "'";
$result_update = mysqli_query($sql_update);
}
if ($remove == true || $remove == '1') {
echo "note: sample code would have set the database to allowed='false'<br />";
/*
$link = mysqli_connect("localhost","username","password");
$sql = "SELECT id FROM mailinglist WHERE email = '" . mysqli_real_escape_string($link, $email) . "'";
$result = mysqli_query($sql);
if ( $result ) {
while($row = mysqli_fetch_array($result)) {
$sql_update = "UPDATE mailinglist SET allowed='false' WHERE email = '" . $email . "'";
$result_update = mysqli_query($sql_update);
}
}
mysqli_close($link);
*/
}
mysqli_close($link);
*/
}

$displayData = prepData($email, $bounceType, $remove);
$bounceType = $displayData['bounce_type'];
$emailName = $displayData['emailName'];
$emailAddy = $displayData['emailAddy'];
$remove = $displayData['remove'];
$displayData = prepData($email, $bounceType, $remove);
$bounceType = $displayData['bounce_type'];
$emailName = $displayData['emailName'];
$emailAddy = $displayData['emailAddy'];
$remove = $displayData['remove'];

echo $msgnum . ': ' . $ruleNo . ' | ' . $ruleCat . ' | ' . $bounceType . ' | ' . $remove . ' | ' . $email . ' | ' . $subject . "<br />\n";
echo $msgnum . ': ' . $ruleNo . ' | ' . $ruleCat . ' | ' . $bounceType . ' | ' . $remove . ' | ' . $email . ' | ' . $subject . "<br />\n";

return true;
return true;
}

/**
Expand All @@ -66,48 +66,48 @@ function callbackAction($msgnum, $bounceType, $email, $subject, $xheader, $remov
*/
function prepData($email, $bounceType, $remove)
{
$data['bounce_type'] = trim($bounceType);
$data['email'] = '';
$data['emailName'] = '';
$data['emailAddy'] = '';
$data['remove'] = '';
if (strpos($email, '<') !== false) {
$pos_start = strpos($email, '<');
$data['emailName'] = trim(substr($email, 0, $pos_start));
$data['emailAddy'] = substr($email, $pos_start + 1);
$pos_end = strpos($data['emailAddy'], '>');
if ($pos_end) {
$data['emailAddy'] = substr($data['emailAddy'], 0, $pos_end);
$data['bounce_type'] = \trim($bounceType);
$data['email'] = '';
$data['emailName'] = '';
$data['emailAddy'] = '';
$data['remove'] = '';
if (\strpos($email, '<') !== false) {
$pos_start = \strpos($email, '<');
$data['emailName'] = \trim(\substr($email, 0, $pos_start));
$data['emailAddy'] = \substr($email, $pos_start + 1);
$pos_end = \strpos($data['emailAddy'], '>');
if ($pos_end) {
$data['emailAddy'] = \substr($data['emailAddy'], 0, $pos_end);
}
}
}

// replace the < and > able so they display on screen
$email = str_replace(array('<', '>'), array('&lt;', '&gt;'), $email);
// replace the < and > able so they display on screen
$email = \str_replace(['<', '>'], ['&lt;', '&gt;'], $email);

// replace the "TO:<" with nothing
$email = str_ireplace('TO:<', '', $email);
// replace the "TO:<" with nothing
$email = \str_ireplace('TO:<', '', $email);

$data['email'] = $email;
$data['email'] = $email;

// account for legitimate emails that have no bounce type
if (trim($bounceType) == '') {
$data['bounce_type'] = 'none';
}
// account for legitimate emails that have no bounce type
if (\trim($bounceType) == '') {
$data['bounce_type'] = 'none';
}

// change the remove flag from true or 1 to textual representation
if (stripos($remove, 'moved') !== false && stripos($remove, 'hard') !== false) {
$data['removestat'] = 'moved (hard)';
$data['remove'] = '<span style="color:red;">' . 'moved (hard)' . '</span>';
} elseif (stripos($remove, 'moved') !== false && stripos($remove, 'soft') !== false) {
$data['removestat'] = 'moved (soft)';
$data['remove'] = '<span style="color:gray;">' . 'moved (soft)' . '</span>';
} elseif ($remove == true || $remove == '1') {
$data['removestat'] = 'deleted';
$data['remove'] = '<span style="color:red;">' . 'deleted' . '</span>';
} else {
$data['removestat'] = 'not deleted';
$data['remove'] = '<span style="color:gray;">' . 'not deleted' . '</span>';
}
// change the remove flag from true or 1 to textual representation
if (\stripos($remove, 'moved') !== false && \stripos($remove, 'hard') !== false) {
$data['removestat'] = 'moved (hard)';
$data['remove'] = '<span style="color:red;">' . 'moved (hard)' . '</span>';
} elseif (\stripos($remove, 'moved') !== false && \stripos($remove, 'soft') !== false) {
$data['removestat'] = 'moved (soft)';
$data['remove'] = '<span style="color:gray;">' . 'moved (soft)' . '</span>';
} elseif ($remove == true || $remove == '1') {
$data['removestat'] = 'deleted';
$data['remove'] = '<span style="color:red;">' . 'deleted' . '</span>';
} else {
$data['removestat'] = 'not deleted';
$data['remove'] = '<span style="color:gray;">' . 'not deleted' . '</span>';
}

return $data;
return $data;
}
Loading

0 comments on commit 53c58a7

Please sign in to comment.