1
0
mirror of https://github.com/e107inc/e107.git synced 2025-04-20 12:41:51 +02:00

fix(#5131): Accurate resource open check when using log file handle

Fixes: https://github.com/e107inc/e107/issues/5131
This commit is contained in:
Nick Liu 2023-12-03 21:20:43 -06:00
parent 1637707a03
commit 688ebdfea8
No known key found for this signature in database
GPG Key ID: 1167C5F9C9897637
2 changed files with 54 additions and 11 deletions

View File

@ -130,14 +130,10 @@ if (!defined('e107_INIT')) { exit; }
//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\PHPMailer;
// use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\POP3;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\POP3;
require_once(e_HANDLER.'vendor/autoload.php');
@ -438,7 +434,7 @@ class e107Email extends PHPMailer
*/
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
@ -487,7 +483,7 @@ class e107Email extends PHPMailer
*/
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");
}
@ -496,11 +492,12 @@ class e107Email extends PHPMailer
}
/**
* 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);
}

View File

@ -276,4 +276,50 @@ Admin<br />
}
/**
* @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;
}
}