[BUGFIX] Cleanup Spool Mailer feature
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Mail / TransportFactory.php
index 88cb86e..9c24259 100644 (file)
@@ -1,26 +1,28 @@
 <?php
-
+declare(strict_types = 1);
 namespace TYPO3\CMS\Core\Mail;
 
-/*                                                                        *
- * This script is part of the TYPO3 project - inspiring people to share!  *
- *                                                                        *
- * TYPO3 is free software; you can redistribute it and/or modify it under *
- * the terms of the GNU General Public License version 3 as published by  *
- * the Free Software Foundation.                                          *
- *                                                                        *
- * This script is distributed in the hope that it will be useful, but     *
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
- * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
- * Public License for more details.                                       *
- *                                                                        */
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
 
+use TYPO3\CMS\Core\Exception;
+use TYPO3\CMS\Core\SingletonInterface;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * TransportFactory
  */
-class TransportFactory implements \TYPO3\CMS\Core\SingletonInterface
+class TransportFactory implements SingletonInterface
 {
     const SPOOL_MEMORY = 'memory';
     const SPOOL_FILE = 'file';
@@ -28,9 +30,9 @@ class TransportFactory implements \TYPO3\CMS\Core\SingletonInterface
     /**
      * Gets a transport from settings.
      *
-     * @param  array   $mailSettings from $GLOBALS['TYPO3_CONF_VARS']['MAIL']
+     * @param array $mailSettings from $GLOBALS['TYPO3_CONF_VARS']['MAIL']
      * @return \Swift_Transport
-     * @throws \TYPO3\CMS\Core\Exception
+     * @throws Exception
      * @throws \RuntimeException
      */
     public function get(array $mailSettings): \Swift_Transport
@@ -43,7 +45,7 @@ class TransportFactory implements \TYPO3\CMS\Core\SingletonInterface
         }
 
         $transport = null;
-        $transportType = (isset($mailSettings['transport_spool_type']) && !empty($mailSettings['transport_spool_type'])) ? 'spool': $mailSettings['transport'];
+        $transportType = isset($mailSettings['transport_spool_type']) && !empty($mailSettings['transport_spool_type']) ? 'spool' : $mailSettings['transport'];
 
         switch ($transportType) {
             case 'spool':
@@ -51,12 +53,12 @@ class TransportFactory implements \TYPO3\CMS\Core\SingletonInterface
                 break;
             case 'smtp':
                 // Get settings to be used when constructing the transport object
-                list($host, $port) = preg_split('/:/', $mailSettings['transport_smtp_server']);
+                list($host, $port) = preg_split('/:/', $mailSettings['transport_smtp_server'] ?? '');
                 if ($host === '') {
-                    throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp"', 1291068606);
+                    throw new Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_smtp_server\'] needs to be set when transport is set to "smtp".', 1291068606);
                 }
                 if ($port === null || $port === '') {
-                    $port = '25';
+                    $port = 25;
                 }
                 $useEncryption = $mailSettings['transport_smtp_encrypt'] ?: null;
                 // Create our transport
@@ -74,7 +76,7 @@ class TransportFactory implements \TYPO3\CMS\Core\SingletonInterface
             case 'sendmail':
                 $sendmailCommand = $mailSettings['transport_sendmail_command'];
                 if (empty($sendmailCommand)) {
-                    throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail"', 1291068620);
+                    throw new Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_sendmail_command\'] needs to be set when transport is set to "sendmail".', 1291068620);
                 }
                 // Create our transport
                 $transport = \Swift_SendmailTransport::newInstance($sendmailCommand);
@@ -82,10 +84,10 @@ class TransportFactory implements \TYPO3\CMS\Core\SingletonInterface
             case 'mbox':
                 $mboxFile = $mailSettings['transport_mbox_file'];
                 if ($mboxFile == '') {
-                    throw new \TYPO3\CMS\Core\Exception('$TYPO3_CONF_VARS[\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox"', 1294586645);
+                    throw new Exception('$GLOBALS[\'TYPO3_CONF_VARS\'][\'MAIL\'][\'transport_mbox_file\'] needs to be set when transport is set to "mbox".', 1294586645);
                 }
                 // Create our transport
-                $transport = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MboxTransport::class, $mboxFile);
+                $transport = GeneralUtility::makeInstance(MboxTransport::class, $mboxFile);
                 break;
             case 'mail':
                 // Create the transport, no configuration required
@@ -116,6 +118,9 @@ class TransportFactory implements \TYPO3\CMS\Core\SingletonInterface
         switch ($mailSettings['transport_spool_type']) {
             case self::SPOOL_FILE:
                 $path = GeneralUtility::getFileAbsFileName($mailSettings['transport_spool_filepath']);
+                if (empty($path) || !file_exists($path) || !is_writable($path)) {
+                    throw new \RuntimeException('The Spool Type filepath must be available and writeable for TYPO3 in order to be used. Be sure that it\'s not accessible via the web.', 1518558797);
+                }
                 $spool = GeneralUtility::makeInstance(\Swift_FileSpool::class, $path);
                 break;
             case self::SPOOL_MEMORY:
@@ -123,7 +128,7 @@ class TransportFactory implements \TYPO3\CMS\Core\SingletonInterface
                 break;
             default:
                 $spool = GeneralUtility::makeInstance($mailSettings['transport_spool_type'], $mailSettings);
-                if (!($spool instanceof \Swift_Spool)) {
+                if (!$spool instanceof \Swift_Spool) {
                     throw new \RuntimeException($mailSettings['spool'] . ' is not an implementation of \\Swift_Spool,
                             but must implement that interface to be used as a mail spool.', 1466799482);
                 }