Skip to content

Commit

Permalink
Merge pull request #5134 from Deltik/fix/5131
Browse files Browse the repository at this point in the history
Fixes: #5131 Accurate resource open check when using log file handle
  • Loading branch information
CaMer0n committed Dec 4, 2023
2 parents 1637707 + 688ebdf commit 1ad7bc3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
17 changes: 7 additions & 10 deletions e107_handlers/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,10 @@
//define('MAIL_DEBUG',true);
//define('LOG_CALLER', true);

//require_once(e_HANDLER.'phpmailer/class.phpmailer.php');
//require_once(e_HANDLER.'phpmailer/class.smtp.php');
//require_once(e_HANDLER.'phpmailer/PHPMailerAutoload.php');

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
// use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\POP3;
use PHPMailer\PHPMailer\Exception;

require_once(e_HANDLER.'vendor/autoload.php');


Expand Down Expand Up @@ -438,7 +434,7 @@ public function makePrintableAddress($email,$to)
*/
protected function openLog($logInfo = true)
{
if ($this->logEnable && ($this->logHandle === false))
if ($this->logEnable && !is_resource($this->logHandle))
{
$logFileName = MAIL_LOG_PATH.'mailoutlog.log';
$this->logHandle = fopen($logFileName, 'a'); // Always append to file
Expand Down Expand Up @@ -487,7 +483,7 @@ protected function openLog($logInfo = true)
*/
protected function logLine($text)
{
if ($this->logEnable && ($this->logHandle > 0))
if ($this->logEnable && is_resource($this->logHandle))
{
fwrite($this->logHandle,date('H:i:s y.m.d').' - '.$text."\r\n");
}
Expand All @@ -496,11 +492,12 @@ protected function logLine($text)
}

/**
* Close log
* Close log
* @return void
*/
protected function closeLog()
{
if ($this->logEnable && ($this->logHandle > 0))
if ($this->logEnable && is_resource($this->logHandle))
{
fclose($this->logHandle);
}
Expand Down
46 changes: 46 additions & 0 deletions e107_tests/tests/unit/e107EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,50 @@ function testSentMimeMessage()

}

/**
* @see https://github.com/e107inc/e107/issues/5131
* @throws Exception if the {@link e107Email} object cannot be created.
*/
function testLogFileHandle()
{
$logFilePath = e_ROOT . MAIL_LOG_PATH . 'mailoutlog.log';

$randomString1 = uniqid();
$randomString2 = uniqid();

$this->assertFalse($this->fileContainsString($logFilePath, $randomString1));
$this->assertFalse($this->fileContainsString($logFilePath, $randomString2));

$eml = $this->make('e107Email', ['send' => function() { return true; }]);
$eml->logEnable(2);
$eml->sendEmail(
'nobody@example.com',
"$randomString1 Example",
['body' => 'Message body'],
);
$this->assertTrue($this->fileContainsString($logFilePath, $randomString1));
$eml->sendEmail(
'nobody2@example.com',
"$randomString2 Example",
['body' => 'Message body'],
);
$this->assertTrue($this->fileContainsString($logFilePath, $randomString2));
}

/**
* @param $filePath
* @param $string
* @return bool
*/
private function fileContainsString($filePath, $string)
{
if (!file_exists($filePath)) return false;
$handle = fopen($filePath, 'r');
while (($buffer = fgets($handle)) !== false) {
if (strpos($buffer, $string) !== false) {
return true;
}
}
return false;
}
}

0 comments on commit 1ad7bc3

Please sign in to comment.