[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/PHPMailer/ -> DSNConfigurator.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPMailer - PHP email creation and transport class.
   5   * PHP Version 5.5.
   6   *
   7   * @see https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
   8   *
   9   * @author    Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
  10   * @author    Jim Jagielski (jimjag) <jimjag@gmail.com>
  11   * @author    Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  12   * @author    Brent R. Matzelle (original founder)
  13   * @copyright 2012 - 2023 Marcus Bointon
  14   * @copyright 2010 - 2012 Jim Jagielski
  15   * @copyright 2004 - 2009 Andy Prevost
  16   * @license   https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html GNU Lesser General Public License
  17   * @note      This program is distributed in the hope that it will be useful - WITHOUT
  18   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19   * FITNESS FOR A PARTICULAR PURPOSE.
  20   */
  21  
  22  namespace PHPMailer\PHPMailer;
  23  
  24  /**
  25   * Configure PHPMailer with a DSN string.
  26   * Be aware that a DSN string can configure *all* PHPMailer properties,
  27   * including those that have security consequences, so do not use
  28   * user-provided DSN strings without strict validation of all parameters.
  29   *
  30   * @see https://en.wikipedia.org/wiki/Data_source_name
  31   *
  32   * @author Oleg Voronkovich <oleg-voronkovich@yandex.ru>
  33   */
  34  class DSNConfigurator
  35  {
  36      /**
  37       * Create new PHPMailer instance configured by DSN.
  38       *
  39       * @param string $dsn        DSN
  40       * @param bool   $exceptions Should we throw external exceptions?
  41       *
  42       * @return PHPMailer
  43       */
  44      public static function mailer($dsn, $exceptions = null)
  45      {
  46          static $configurator = null;
  47  
  48          if (null === $configurator) {
  49              $configurator = new DSNConfigurator();
  50          }
  51  
  52          return $configurator->configure(new PHPMailer($exceptions), $dsn);
  53      }
  54  
  55      /**
  56       * Configure PHPMailer instance with DSN string.
  57       *
  58       * @param PHPMailer $mailer PHPMailer instance
  59       * @param string    $dsn    DSN
  60       *
  61       * @return PHPMailer
  62       */
  63      public function configure(PHPMailer $mailer, $dsn)
  64      {
  65          $config = $this->parseDSN($dsn);
  66  
  67          $this->applyConfig($mailer, $config);
  68  
  69          return $mailer;
  70      }
  71  
  72      /**
  73       * Parse DSN string.
  74       *
  75       * @param string $dsn DSN
  76       *
  77       * @throws Exception If DSN is malformed
  78       *
  79       * @return array Configuration
  80       */
  81      private function parseDSN($dsn)
  82      {
  83          $config = $this->parseUrl($dsn);
  84  
  85          if (false === $config || !isset($config['scheme']) || !isset($config['host'])) {
  86              throw new Exception('Malformed DSN');
  87          }
  88  
  89          if (isset($config['query'])) {
  90              parse_str($config['query'], $config['query']);
  91          }
  92  
  93          return $config;
  94      }
  95  
  96      /**
  97       * Apply configuration to mailer.
  98       *
  99       * @param PHPMailer $mailer PHPMailer instance
 100       * @param array     $config Configuration
 101       *
 102       * @throws Exception If scheme is invalid
 103       */
 104      private function applyConfig(PHPMailer $mailer, $config)
 105      {
 106          switch ($config['scheme']) {
 107              case 'mail':
 108                  $mailer->isMail();
 109                  break;
 110              case 'sendmail':
 111                  $mailer->isSendmail();
 112                  break;
 113              case 'qmail':
 114                  $mailer->isQmail();
 115                  break;
 116              case 'smtp':
 117              case 'smtps':
 118                  $mailer->isSMTP();
 119                  $this->configureSMTP($mailer, $config);
 120                  break;
 121              default:
 122                  throw new Exception(
 123                      sprintf(
 124                          'Invalid scheme: "%s". Allowed values: "mail", "sendmail", "qmail", "smtp", "smtps".',
 125                          $config['scheme']
 126                      )
 127                  );
 128          }
 129  
 130          if (isset($config['query'])) {
 131              $this->configureOptions($mailer, $config['query']);
 132          }
 133      }
 134  
 135      /**
 136       * Configure SMTP.
 137       *
 138       * @param PHPMailer $mailer PHPMailer instance
 139       * @param array     $config Configuration
 140       */
 141      private function configureSMTP($mailer, $config)
 142      {
 143          $isSMTPS = 'smtps' === $config['scheme'];
 144  
 145          if ($isSMTPS) {
 146              $mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
 147          }
 148  
 149          $mailer->Host = $config['host'];
 150  
 151          if (isset($config['port'])) {
 152              $mailer->Port = $config['port'];
 153          } elseif ($isSMTPS) {
 154              $mailer->Port = SMTP::DEFAULT_SECURE_PORT;
 155          }
 156  
 157          $mailer->SMTPAuth = isset($config['user']) || isset($config['pass']);
 158  
 159          if (isset($config['user'])) {
 160              $mailer->Username = $config['user'];
 161          }
 162  
 163          if (isset($config['pass'])) {
 164              $mailer->Password = $config['pass'];
 165          }
 166      }
 167  
 168      /**
 169       * Configure options.
 170       *
 171       * @param PHPMailer $mailer  PHPMailer instance
 172       * @param array     $options Options
 173       *
 174       * @throws Exception If option is unknown
 175       */
 176      private function configureOptions(PHPMailer $mailer, $options)
 177      {
 178          $allowedOptions = get_object_vars($mailer);
 179  
 180          unset($allowedOptions['Mailer']);
 181          unset($allowedOptions['SMTPAuth']);
 182          unset($allowedOptions['Username']);
 183          unset($allowedOptions['Password']);
 184          unset($allowedOptions['Hostname']);
 185          unset($allowedOptions['Port']);
 186          unset($allowedOptions['ErrorInfo']);
 187  
 188          $allowedOptions = \array_keys($allowedOptions);
 189  
 190          foreach ($options as $key => $value) {
 191              if (!in_array($key, $allowedOptions)) {
 192                  throw new Exception(
 193                      sprintf(
 194                          'Unknown option: "%s". Allowed values: "%s"',
 195                          $key,
 196                          implode('", "', $allowedOptions)
 197                      )
 198                  );
 199              }
 200  
 201              switch ($key) {
 202                  case 'AllowEmpty':
 203                  case 'SMTPAutoTLS':
 204                  case 'SMTPKeepAlive':
 205                  case 'SingleTo':
 206                  case 'UseSendmailOptions':
 207                  case 'do_verp':
 208                  case 'DKIM_copyHeaderFields':
 209                      $mailer->$key = (bool) $value;
 210                      break;
 211                  case 'Priority':
 212                  case 'SMTPDebug':
 213                  case 'WordWrap':
 214                      $mailer->$key = (int) $value;
 215                      break;
 216                  default:
 217                      $mailer->$key = $value;
 218                      break;
 219              }
 220          }
 221      }
 222  
 223      /**
 224       * Parse a URL.
 225       * Wrapper for the built-in parse_url function to work around a bug in PHP 5.5.
 226       *
 227       * @param string $url URL
 228       *
 229       * @return array|false
 230       */
 231      protected function parseUrl($url)
 232      {
 233          if (\PHP_VERSION_ID >= 50600 || false === strpos($url, '?')) {
 234              return parse_url($url);
 235          }
 236  
 237          $chunks = explode('?', $url);
 238          if (is_array($chunks)) {
 239              $result = parse_url($chunks[0]);
 240              if (is_array($result)) {
 241                  $result['query'] = $chunks[1];
 242              }
 243              return $result;
 244          }
 245  
 246          return false;
 247      }
 248  }


Generated : Tue Nov 24 08:20:01 2026 Cross-referenced by PHPXref