File indexing completed on 2024-12-22 05:36:28

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Application
0017  * @subpackage Resource
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Application_Resource_ResourceAbstract
0025  */
0026 // require_once 'Zend/Application/Resource/ResourceAbstract.php';
0027 
0028 /**
0029  * Resource for setting up Mail Transport and default From & ReplyTo addresses
0030  *
0031  * @uses       Zend_Application_Resource_ResourceAbstract
0032  * @category   Zend
0033  * @package    Zend_Application
0034  * @subpackage Resource
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Application_Resource_Mail extends Zend_Application_Resource_ResourceAbstract
0039 {
0040 
0041     /**
0042      * @var Zend_Mail_Transport_Abstract
0043      */
0044     protected $_transport;
0045 
0046     public function init()
0047     {
0048         return $this->getMail();
0049     }
0050 
0051     /**
0052      *
0053      * @return Zend_Mail_Transport_Abstract|null
0054      */
0055     public function getMail()
0056     {
0057         if (null === $this->_transport) {
0058             $options = $this->getOptions();
0059             foreach ($options as $key => $option) {
0060                 $options[strtolower($key)] = $option;
0061             }
0062             $this->setOptions($options);
0063 
0064             if (isset($options['transport'])
0065                 && !is_numeric($options['transport'])
0066             ) {
0067                 $this->_transport = $this->_setupTransport($options['transport']);
0068                 if (!isset($options['transport']['register'])
0069                     || $options['transport']['register'] == '1'
0070                     || (isset($options['transport']['register'])
0071                         && !is_numeric($options['transport']['register'])
0072                         && (bool)$options['transport']['register'] == true)
0073                 ) {
0074                     Zend_Mail::setDefaultTransport($this->_transport);
0075                 }
0076             }
0077 
0078             $this->_setDefaults('from');
0079             $this->_setDefaults('replyTo');
0080         }
0081 
0082         return $this->_transport;
0083     }
0084 
0085     protected function _setDefaults($type)
0086     {
0087         $key = strtolower('default' . $type);
0088         $options = $this->getOptions();
0089 
0090         if (isset($options[$key]['email'])
0091             && !is_numeric($options[$key]['email'])
0092         ) {
0093             $method = array('Zend_Mail', 'setDefault' . ucfirst($type));
0094             if (isset($options[$key]['name'])
0095                 && !is_numeric(
0096                     $options[$key]['name']
0097                 )
0098             ) {
0099                 call_user_func(
0100                     $method, $options[$key]['email'], $options[$key]['name']
0101                 );
0102             } else {
0103                 call_user_func($method, $options[$key]['email']);
0104             }
0105         }
0106     }
0107 
0108     protected function _setupTransport($options)
0109     {
0110         if (!isset($options['type'])) {
0111             $options['type'] = 'sendmail';
0112         }
0113 
0114         $transportName = $options['type'];
0115         if (!Zend_Loader_Autoloader::autoload($transportName)) {
0116             $transportName = ucfirst(strtolower($transportName));
0117 
0118             if (!Zend_Loader_Autoloader::autoload($transportName)) {
0119                 $transportName = 'Zend_Mail_Transport_' . $transportName;
0120                 if (!Zend_Loader_Autoloader::autoload($transportName)) {
0121                     throw new Zend_Application_Resource_Exception(
0122                         "Specified Mail Transport '{$transportName}'"
0123                         . 'could not be found'
0124                     );
0125                 }
0126             }
0127         }
0128 
0129         unset($options['type']);
0130         unset($options['register']); //@see ZF-11022
0131 
0132         switch($transportName) {
0133             case 'Zend_Mail_Transport_Smtp':
0134                 if (!isset($options['host'])) {
0135                     throw new Zend_Application_Resource_Exception(
0136                         'A host is necessary for smtp transport,'
0137                         . ' but none was given'
0138                     );
0139                 }
0140 
0141                 $transport = new $transportName($options['host'], $options);
0142                 break;
0143             case 'Zend_Mail_Transport_Sendmail':
0144             default:
0145                 $transport = new $transportName($options);
0146                 break;
0147         }
0148 
0149         return $transport;
0150     }
0151 }