summaryrefslogtreecommitdiff
path: root/Postman/Postman-Mail/PostmanZendMailTransportConfigurationFactory.php
blob: 08e18108ed3cdad8050911200adc3464612fa327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
if (! interface_exists ( 'PostmanZendMailTransportConfigurationFactory' )) {
	interface PostmanZendMailTransportConfigurationFactory {
		static function createConfig(PostmanTransport $transport);
	}
}

if (! class_exists ( 'PostmanBasicAuthConfigurationFactory' )) {
	class PostmanBasicAuthConfigurationFactory implements PostmanZendMailTransportConfigurationFactory {
		public static function createConfig(PostmanTransport $transport) {
			
			// create Logger
			$logger = new PostmanLogger ( "PostmanBasicAuthConfigurationFactory" );
			
			// retrieve the hostname and port form the transport
			$hostname = $transport->getHostname ();
			$port = $transport->getPort ();
			$securityType = $transport->getSecurityType ();
			$authType = $transport->getAuthenticationType ();
			$username = $transport->getCredentialsId ();
			$password = $transport->getCredentialsSecret ();
			
			// create the Configuration structure for Zend_Mail
			$config = array (
					'port' => $port 
			);
			$logger->debug ( sprintf ( 'Using %s:%s ', $hostname, $port ) );
			if ($securityType != PostmanOptions::SECURITY_TYPE_NONE) {
				$config ['ssl'] = $securityType;
				$logger->debug ( 'Using encryption ' . $securityType );
			} else {
				$logger->debug ( 'Using no encryption' );
			}
			if ($authType != PostmanOptions::AUTHENTICATION_TYPE_NONE) {
				$config ['auth'] = $authType;
				$config ['username'] = $username;
				$config ['password'] = $password;
				$logger->debug ( sprintf ( 'Using auth %s with username %s and password %s', $authType, $username, PostmanUtils::obfuscatePassword ( $password ) ) );
			} else {
				$logger->debug ( 'Using no authentication' );
			}
			
			// return the Configuration structure
			return $config;
		}
	}
}

if (! class_exists ( 'PostmanOAuth2ConfigurationFactory' )) {
	class PostmanOAuth2ConfigurationFactory implements PostmanZendMailTransportConfigurationFactory {
		public static function createConfig(PostmanTransport $transport) {
			
			// create Logger
			$logger = new PostmanLogger ( 'PostmanOAuth2ConfigurationFactory' );
			
			// retrieve the hostname and port form the transport
			$hostname = $transport->getHostname ();
			$port = $transport->getPort ();
			
			// the sender email is needed for the OAuth2 Bearer token
			$senderEmail = PostmanOptions::getInstance ()->getEnvelopeSender ();
			assert ( ! empty ( $senderEmail ) );
			
			// the vendor is required for Yahoo's OAuth2 implementation
			$vendor = self::createVendorString ( $hostname );
			
			// create the OAuth2 SMTP Authentication string
			$initClientRequestEncoded = self::createAuthenticationString ( $senderEmail, PostmanOAuthToken::getInstance ()->getAccessToken (), $vendor );
			
			// create the Configuration structure for Zend_Mail
			$config = self::createConfiguration ( $logger, $hostname, $port, $transport->getSecurityType (), $transport->getAuthenticationType (), $initClientRequestEncoded );
			
			// return the Configuration structure
			return $config;
		}
		
		/**
		 *
		 * Create the Configuration structure for Zend_Mail
		 *
		 * @param mixed $hostname        	
		 * @param mixed $port        	
		 * @param mixed $securityType        	
		 * @param mixed $authenticationType        	
		 * @param mixed $initClientRequestEncoded        	
		 * @return multitype:unknown NULL
		 */
		private static function createConfiguration($logger, $hostname, $port, $securityType, $authenticationType, $initClientRequestEncoded) {
			$config = array (
					'ssl' => $securityType,
					'port' => $port,
					'auth' => $authenticationType,
					'xoauth2_request' => $initClientRequestEncoded 
			);
			$logger->debug ( sprintf ( 'Using auth %s with encryption %s to %s:%s ', $config ['auth'], $config ['ssl'], $hostname, $config ['port'] ) );
			return $config;
		}
		
		/**
		 * Create the vendor string (for Yahoo servers only)
		 *
		 * @param mixed $hostname        	
		 * @return string
		 */
		private static function createVendorString($hostname) {
			// the vendor is required for Yahoo's OAuth2 implementation
			$vendor = '';
			if (PostmanUtils::endsWith ( $hostname, 'yahoo.com' )) {
				// Yahoo Mail requires a Vendor - see http://imapclient.freshfoo.com/changeset/535%3A80ae438f4e4a/
				$pluginData = apply_filters ( 'postman_get_plugin_metadata', null );
				$vendor = sprintf ( "vendor=Postman SMTP %s\1", $pluginData ['version'] );
			}
			return $vendor;
		}
		
		/**
		 * Create the standard OAuth2 SMTP Authentication string
		 *
		 * @param mixed $senderEmail        	
		 * @param mixed $oauth2AccessToken        	
		 * @param mixed $vendor        	
		 * @return string
		 */
		private static function createAuthenticationString($senderEmail, $oauth2AccessToken, $vendor) {
			$initClientRequestEncoded = base64_encode ( sprintf ( "user=%s\1auth=Bearer %s\1%s\1", $senderEmail, $oauth2AccessToken, $vendor ) );
			return $initClientRequestEncoded;
		}
	}
}