|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is a part of the phpMussel\PHPMailer package. |
| 4 | + * Homepage: https://phpmussel.github.io/ |
| 5 | + * |
| 6 | + * PHPMUSSEL COPYRIGHT 2013 AND BEYOND BY THE PHPMUSSEL TEAM. |
| 7 | + * |
| 8 | + * License: GNU/GPLv2 |
| 9 | + * @see LICENSE.txt |
| 10 | + * |
| 11 | + * This file: PHPMailer-phpMussel linker (last modified: 2020.06.14). |
| 12 | + */ |
| 13 | + |
| 14 | +namespace phpMussel\PHPMailer; |
| 15 | + |
| 16 | +class PHPMailer |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var \phpMussel\Core\Loader The instantiated loader object. |
| 20 | + */ |
| 21 | + private $Loader; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var string The path to the core asset files. |
| 25 | + */ |
| 26 | + private $AssetsPath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string The path to the core L10N files. |
| 30 | + */ |
| 31 | + private $L10NPath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR; |
| 32 | + |
| 33 | + /** |
| 34 | + * Construct the linker instance. |
| 35 | + * |
| 36 | + * @param \phpMussel\Core\Loader $Loader The instantiated loader object, passed by reference. |
| 37 | + */ |
| 38 | + public function __construct(\phpMussel\Core\Loader &$Loader) |
| 39 | + { |
| 40 | + /** Link the loader to this instance. */ |
| 41 | + $this->Loader = &$Loader; |
| 42 | + |
| 43 | + /** Load configuration defaults and perform fallbacks. */ |
| 44 | + if ( |
| 45 | + is_readable($this->AssetsPath . 'config.yml') && |
| 46 | + $Configuration = $this->Loader->readFile($this->AssetsPath . 'config.yml') |
| 47 | + ) { |
| 48 | + $Defaults = []; |
| 49 | + $this->Loader->YAML->process($Configuration, $Defaults); |
| 50 | + if (isset($Defaults)) { |
| 51 | + $this->Loader->fallback($Defaults); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** Load L10N data. */ |
| 56 | + $this->Loader->loadL10N($this->Loader->L10NPath); |
| 57 | + |
| 58 | + /** |
| 59 | + * Writes to the PHPMailer event log. |
| 60 | + * |
| 61 | + * @param string $Data What to write. |
| 62 | + * @return bool True on success; False on failure. |
| 63 | + */ |
| 64 | + $this->Loader->Events->addHandler('writeToPHPMailerEventLog', function (string $Data): bool { |
| 65 | + /** Guard. */ |
| 66 | + if (!$this->Loader->Configuration['phpmailer']['event_log']) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + /** Applies formatting for dynamic log filenames. */ |
| 71 | + $EventLog = $this->Loader->timeFormat($this->Loader->Time, $this->Loader->Configuration['phpmailer']['event_log']); |
| 72 | + |
| 73 | + $WriteMode = (!file_exists($this->AssetsPath . $EventLog) || ( |
| 74 | + $this->Loader->Configuration['core']['truncate'] > 0 && |
| 75 | + filesize($this->AssetsPath . $EventLog) >= $this->readBytes($this->Loader->Configuration['core']['truncate']) |
| 76 | + )) ? 'w' : 'a'; |
| 77 | + |
| 78 | + /** Build the path to the log and write it. */ |
| 79 | + if ($phpMussel['BuildLogPath']($EventLog)) { |
| 80 | + $Handle = fopen($this->AssetsPath . $EventLog, $WriteMode); |
| 81 | + fwrite($Handle, $Data); |
| 82 | + fclose($Handle); |
| 83 | + if ($WriteMode === 'w') { |
| 84 | + $phpMussel['LogRotation']($this->Loader->Configuration['phpmailer']['event_log']); |
| 85 | + } |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + return false; |
| 90 | + }); |
| 91 | + } |
| 92 | +} |
0 commit comments