/** * (non-PHPdoc) * * @see PostmanZendModuleTransport::createZendMailTransport() */ public function createZendMailTransport($fakeHostname, $fakeConfig) { if (PostmanOptions::AUTHENTICATION_TYPE_OAUTH2 == $this->getAuthenticationType()) { $config = PostmanOAuth2ConfigurationFactory::createConfig($this); } else { $config = PostmanBasicAuthConfigurationFactory::createConfig($this); } // Google's autoloader will try and load this so we list it first require_once 'PostmanGmailApiModuleZendMailTransport.php'; // Gmail Client includes require_once 'google-api-php-client-1.1.2/src/Google/Client.php'; require_once 'google-api-php-client-1.1.2/src/Google/Service/Gmail.php'; // build the Gmail Client $authToken = PostmanOAuthToken::getInstance(); $client = new Postman_Google_Client(); $client->setClientId($this->options->getClientId()); $client->setClientSecret($this->options->getClientSecret()); $client->setRedirectUri(''); // rebuild the google access token $token = new stdClass(); $token->access_token = $authToken->getAccessToken(); $token->refresh_token = $authToken->getRefreshToken(); $token->token_type = 'Bearer'; $token->expires_in = 3600; $token->id_token = null; $token->created = 0; $client->setAccessToken(json_encode($token)); // We only need permissions to compose and send emails $client->addScope("https://www.googleapis.com/auth/gmail.compose"); $service = new Postman_Google_Service_Gmail($client); $config[PostmanGmailApiModuleZendMailTransport::SERVICE_OPTION] = $service; return new PostmanGmailApiModuleZendMailTransport(self::HOST, $config); }
/** * Important: bind() may be called multiple times * * Replace wp_mail() after making sure: * 1) the plugin has not already bound to wp_mail and * 2) wp_mail is available for use * 3) the plugin is properly configured. * 4) the plugin's prerequisites are met. */ function bind() { if (!$this->bound) { $binderOptions = PostmanOptions::getInstance(); $binderAuthorizationToken = PostmanOAuthToken::getInstance(); $ready = true; if (function_exists('wp_mail')) { // If the function exists, it's probably because another plugin has // replaced the pluggable function first, and we set an error flag. // this is an error message because it is a Bind error $this->logger->error('wp_mail is already bound, Postman can not use it'); $this->bindError = true; } if (!PostmanTransportRegistry::getInstance()->isPostmanReadyToSendEmail($binderOptions, $binderAuthorizationToken)) { // this is a debug message because bound may be called again with a NEW transport that IS configured // this is a debug message because it is not up to the Binder to report transport errors $this->logger->debug('Transport is not configured and ready'); $ready = false; } if (!PostmanPreRequisitesCheck::isReady()) { // this is a debug message because it is not up to the Binder to report transport errors $this->logger->debug('Prerequisite check failed'); $ready = false; } if ($ready && !$this->bindError) { $this->logger->debug('Binding to wp_mail()'); $this->replacePluggableFunctionWpMail(); } } }
private function createManager(PostmanZendModuleTransport $transport) { $options = PostmanOptions::getInstance(); $authorizationToken = PostmanOAuthToken::getInstance(); $authenticationType = $options->getAuthenticationType(); $hostname = $options->getHostname(); $clientId = $options->getClientId(); $clientSecret = $options->getClientSecret(); $senderEmail = $options->getMessageSenderEmail(); $scribe = $transport->getScribe(); $redirectUrl = $scribe->getCallbackUrl(); if ($transport->isOAuthUsed($options->getAuthenticationType())) { if ($transport->isServiceProviderGoogle($hostname)) { $authenticationManager = new PostmanGoogleAuthenticationManager($clientId, $clientSecret, $authorizationToken, $redirectUrl, $senderEmail); } else { if ($transport->isServiceProviderMicrosoft($hostname)) { $authenticationManager = new PostmanMicrosoftAuthenticationManager($clientId, $clientSecret, $authorizationToken, $redirectUrl); } else { if ($transport->isServiceProviderYahoo($hostname)) { $authenticationManager = new PostmanYahooAuthenticationManager($clientId, $clientSecret, $authorizationToken, $redirectUrl); } else { assert(false); } } } } else { $authenticationManager = new PostmanNonOAuthAuthenticationManager(); } $this->logger->debug('Created ' . get_class($authenticationManager)); return $authenticationManager; }
public function createConfig(PostmanTransport $transport) { // retrieve the hostname and port form the transport $hostname = $transport->getHostname(); $port = $transport->getHostPort(); // 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 = $this->createVendorString($hostname); // create the OAuth2 SMTP Authentication string $initClientRequestEncoded = $this->createAuthenticationString($senderEmail, PostmanOAuthToken::getInstance()->getAccessToken(), $vendor); // create the Configuration structure for Zend_Mail $config = $this->createConfiguration($hostname, $port, $transport->getSecurityType(), $transport->getAuthenticationType(), $initClientRequestEncoded); // return the Configuration structure return $config; }
/** * Constructor * * @param PostmanOptions $options */ function __construct() { $this->options = PostmanOptions::getInstance(); $this->authorizationToken = PostmanOAuthToken::getInstance(); $this->diagnostics = ''; PostmanUtils::registerAjaxHandler('postman_diagnostics', $this, 'getDiagnostics'); }
/** * Check for configuration errors and displays messages to the user */ public function check_for_configuration_errors() { $options = PostmanOptions::getInstance(); $authToken = PostmanOAuthToken::getInstance(); // did Postman fail binding to wp_mail()? if ($this->wpMailBinder->isUnboundDueToException()) { // this message gets printed on ANY WordPress admin page, as it's a pretty fatal error that // may occur just by activating a new plugin if (PostmanUtils::isAdmin() && is_admin()) { // I noticed the wpMandrill and SendGrid plugins have the exact same error message here // I've adopted their error message as well, for shits and giggles .... :D $this->messageHandler->addError(__('Postman: wp_mail has been declared by another plugin or theme, so you won\'t be able to use Postman until the conflict is resolved.', 'postman-smtp')); // $this->messageHandler->addError ( __ ( 'Error: Postman is properly configured, but the current theme or another plugin is preventing service.', 'postman-smtp' ) ); } } else { if (!$this->wpMailBinder->isBound()) { $transport = PostmanTransportRegistry::getInstance()->getCurrentTransport(); $scribe = PostmanConfigTextHelperFactory::createScribe($options->getHostname(), $transport); $readyToSend = PostmanTransportRegistry::getInstance()->isPostmanReadyToSendEmail($options, $authToken); $virgin = $options->isNew(); if (!$readyToSend && !$virgin) { // if the configuration is broken, and the user has started to configure the plugin // show this error message $message = PostmanTransportRegistry::getInstance()->getCurrentTransport()->getMisconfigurationMessage($scribe, $options, $authToken); if ($message) { // output the warning message $this->logger->warn('Transport has a configuration problem: ' . $message); // on pages that are Postman admin pages only, show this error message if (PostmanUtils::isAdmin() && PostmanUtils::isCurrentPagePostmanAdmin()) { $this->messageHandler->addError($message); } } } // on pages that are NOT Postman admin pages only, show this error message if (!PostmanUtils::isCurrentPagePostmanAdmin() && !$readyToSend) { // on pages that are *NOT* Postman admin pages only.... // if the configuration is broken // show this error message add_action('admin_notices', array($this, 'display_configuration_required_warning')); } } } }
/** * Check for configuration errors and displays messages to the user */ public function check_for_configuration_errors() { $options = PostmanOptions::getInstance(); $authToken = PostmanOAuthToken::getInstance(); // did Postman fail binding to wp_mail()? if ($this->wpMailBinder->isUnboundDueToException()) { // this message gets printed on ANY WordPress admin page, as it's a fatal error that // may occur just by activating a new plugin // log the fatal message $this->logger->fatal('Postman: wp_mail has been declared by another plugin or theme, so you won\'t be able to use Postman until the conflict is resolved.'); if (PostmanUtils::isAdmin() && is_admin()) { // on any admin pages, show this error message // I noticed the wpMandrill and SendGrid plugins have the exact same error message here // I've adopted their error message as well, for shits and giggles .... :D $message = __('Postman: wp_mail has been declared by another plugin or theme, so you won\'t be able to use Postman until the conflict is resolved.', Postman::TEXT_DOMAIN); $this->messageHandler->addError($message); } } else { $transport = PostmanTransportRegistry::getInstance()->getCurrentTransport(); $scribe = $transport->getScribe(); $virgin = $options->isNew(); if (!$transport->isConfiguredAndReady()) { // if the configuration is broken, and the user has started to configure the plugin // show this error message $messages = $transport->getConfigurationMessages(); foreach ($messages as $message) { if ($message) { // log the warning message $this->logger->warn(sprintf('%s Transport has a configuration problem: %s', $transport->getName(), $message)); if (PostmanUtils::isAdmin() && PostmanUtils::isCurrentPagePostmanAdmin()) { // on pages that are Postman admin pages only, show this error message $this->messageHandler->addError($message); } } } } // on pages that are NOT Postman admin pages only, show this error message if (PostmanUtils::isAdmin() && !PostmanUtils::isCurrentPagePostmanAdmin() && !$transport->isConfiguredAndReady()) { // on pages that are *NOT* Postman admin pages only.... // if the configuration is broken show this error message add_action('admin_notices', array($this, 'display_configuration_required_warning')); } } }
/** */ private function ensureAuthtokenIsUpdated(PostmanModuleTransport $transport, PostmanOptions $options, PostmanOAuthToken $authorizationToken) { assert(!empty($transport)); assert(!empty($options)); assert(!empty($authorizationToken)); // ensure the token is up-to-date $this->logger->debug('Ensuring Access Token is up-to-date'); // interact with the Authentication Manager $wpMailAuthManager = PostmanAuthenticationManagerFactory::getInstance()->createAuthenticationManager(); if ($wpMailAuthManager->isAccessTokenExpired()) { $this->logger->debug('Access Token has expired, attempting refresh'); $wpMailAuthManager->refreshToken(); $authorizationToken->save(); } }
private function isPermissionNeeded(PostmanOAuthToken $token) { $accessToken = $token->getAccessToken(); $refreshToken = $token->getRefreshToken(); $oauthVendor = $token->getVendorName(); return $oauthVendor != 'google' || empty($accessToken) || empty($refreshToken); }
/** */ public function __construct($rootPluginFilenameAndPath) { parent::__construct($rootPluginFilenameAndPath); $this->oauthToken = PostmanOAuthToken::getInstance(); }
private function isPermissionNeeded(PostmanOptions $options, PostmanOAuthToken $token) { $accessToken = $token->getAccessToken(); $refreshToken = $token->getRefreshToken(); return $options->isAuthTypeOAuth2() && (empty($accessToken) || empty($refreshToken)); }
/** * A convenient place for other code to send a PostmanMessage * * @param PostmanMessage $message * @return boolean */ public function sendMessage(PostmanMessage $message, PostmanEmailLog $log) { // start the clock $startTime = microtime(true) * 1000; // get the Options and AuthToken $options = PostmanOptions::getInstance(); $authorizationToken = PostmanOAuthToken::getInstance(); // add plugin-specific attributes to PostmanMessage $message->addHeaders($options->getAdditionalHeaders()); $message->addTo($options->getForcedToRecipients()); $message->addCc($options->getForcedCcRecipients()); $message->addBcc($options->getForcedBccRecipients()); // get the transport and create the transportConfig and engine $transport = PostmanTransportRegistry::getInstance()->getCurrentTransport(); // create the Zend Mail Transport Configuration Factory if (PostmanOptions::AUTHENTICATION_TYPE_OAUTH2 == $transport->getAuthenticationType()) { $transportConfiguration = new PostmanOAuth2ConfigurationFactory(); } else { $transportConfiguration = new PostmanBasicAuthConfigurationFactory(); } // create the Mail Engine $engine = new PostmanMailEngine($transport, $transportConfiguration); // is this a test run? $testMode = apply_filters('postman_test_email', false); $this->logger->debug('testMode=' . $testMode); try { // validate the message $message->applyFilters(); $message->validate(); // send the message if ($options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION) { if ($options->isAuthTypeOAuth2()) { PostmanUtils::lock(); // may throw an exception attempting to contact the OAuth2 provider $this->ensureAuthtokenIsUpdated($transport, $options, $authorizationToken); } $this->logger->debug('Sending mail'); // may throw an exception attempting to contact the SMTP server $engine->send($message, $options->getHostname()); // increment the success counter, unless we are just tesitng if (!$testMode) { PostmanStats::getInstance()->incrementSuccessfulDelivery(); } } if ($options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION || $options->getRunMode() == PostmanOptions::RUN_MODE_LOG_ONLY) { // log the successful delivery PostmanEmailLogService::getInstance()->writeSuccessLog($log, $message, $engine->getTranscript(), $transport); } // clean up $this->postSend($engine, $startTime, $options); // return successful return true; } catch (Exception $e) { // save the error for later $this->exception = $e; // write the error to the PHP log $this->logger->error(get_class($e) . ' code=' . $e->getCode() . ' message=' . trim($e->getMessage())); // increment the failure counter, unless we are just tesitng if (!$testMode && $options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION) { PostmanStats::getInstance()->incrementFailedDelivery(); } if ($options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION || $options->getRunMode() == PostmanOptions::RUN_MODE_LOG_ONLY) { // log the failed delivery PostmanEmailLogService::getInstance()->writeFailureLog($log, $message, $engine->getTranscript(), $transport, $e->getMessage()); } // clean up $this->postSend($engine, $startTime, $options); // return failure return false; } }