This commit is contained in:
2026-07-02 15:54:39 -06:00
commit 9883323161
17470 changed files with 4470592 additions and 0 deletions
@@ -0,0 +1,70 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Utils;
use Gravity_Forms\Gravity_SMTP\Logging\Debug\Debug_Logger;
class Attachments_Saver {
/**
* @var Debug_Logger
*/
protected $logger;
public function __construct( $logger ) {
$this->logger = $logger;
}
public function save_attachments( $email_id, $attachments ) {
$uploads_dir = $this->get_uploads_dir_path( $email_id, $attachments );
$new_attachments = array();
if ( ! is_dir( $uploads_dir ) ) {
/* translators: %1$s: directory path */
$this->logger->log_debug( __METHOD__ . '(): ' . sprintf( __( 'Creating a new uploads directory at path: %1$s', 'gravitysmtp' ), $uploads_dir ) );
mkdir( $uploads_dir, 0755, true );
}
foreach ( $attachments as $file_path ) {
if ( ! file_exists( $file_path ) ) {
/* translators: %1$s: file path */
$this->logger->log_warning( __METHOD__ . '(): ' . sprintf( __( 'Could not locate file at path: %1$s', 'gravitysmtp' ), $file_path ) );
continue;
}
$file_name = basename( $file_path );
$contents = file_get_contents( $file_path );
$new_path = sprintf( '%s%s', trailingslashit( $uploads_dir ), $file_name );
/* translators: %1$s: file name, %2$s: new path */
$this->logger->log_debug( __METHOD__ . '(): ' . sprintf( __( '%1$s being moved to new location: %2$s', 'gravitysmtp' ), $file_name, $new_path ) );
file_put_contents( $new_path, $contents );
$new_attachments[] = $new_path;
}
return $new_attachments;
}
public function get_saved_attachment( $email_id, $og_path ) {
$file_basename = basename( $og_path );
$uploads_dir = $this->get_uploads_dir_path( $email_id, array( $og_path ) );
return sprintf( '%s%s', trailingslashit( $uploads_dir ), $file_basename );
}
private function get_uploads_dir_path( $email_id, $attachments ) {
$upload_base = wp_get_upload_dir()['basedir'];
$uploads_dir = sprintf( '%s/gravitysmtp/attachments/%s/', untrailingslashit( $upload_base ), $email_id );
/**
* Allows third-party code to modify where attachments are stored for a given upload.
*
* @param string $uploads_dir the current directory for uploading this file
* @param int $email_id the current email ID
* @param array $attachments an array of attachment paths
*/
return apply_filters( 'gravitysmtp_attachment_uploads_dir_path', $uploads_dir, $email_id, $attachments );
}
}
@@ -0,0 +1,289 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Utils;
class AWS_Signature_Handler {
const ISO8601_BASIC = 'Ymd\THis\Z';
/**
* @var string AWS Access ID
*/
protected $id;
/**
* @var string AWS Access Secret
*/
protected $secret;
/**
* @var AWS Region (defaults to us-east-1)
*/
protected $region;
/**
* Get properly-signed request data for sending an SES message.
*
* @since 1.4.0
*
* @param $body
* @param $id
* @param $secret
* @param $region
*
* @return array
*/
public function get_request_data( $body, $id, $secret, $region ) {
$this->id = $id;
$this->secret = $secret;
$this->region = $region;
$query_form = $this->build_query_from_body( $body );
$longform_date = gmdate( self::ISO8601_BASIC );
$shortform_date = substr( $longform_date, 0, 8 );
$headers = array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Host' => $this->get_host(),
'X-Amz-Date' => $longform_date,
);
$signature = $this->generate_auth_signature( $headers, $query_form, $longform_date, $shortform_date );
$headers['Authorization'] = $this->get_auth_header( $headers, $signature, $shortform_date );
$url = sprintf( 'https://%s/', $this->get_host() );
return array(
'body' => $query_form,
'headers' => $headers,
'url' => $url,
);
}
/**
* Build a query string from the body array we provide.
*
* @param $body
*
* @return string
*/
private function build_query_from_body( $body ) {
$query = array();
foreach ( $body as $key => $value ) {
$query = $this->recursively_build_key( $query, $key, $value );
}
return http_build_query( $query, '', '&', PHP_QUERY_RFC3986 );
}
/**
* AWS requires a very-specific structure for URL-encoded values; match that structure here recursively.
*
* @since 1.4.0
*
* @param $query
* @param $key
* @param $data
*
* @return mixed
*/
private function recursively_build_key( $query, $key, $data ) {
if ( ! is_array( $data ) ) {
$query[ $key ] = $data;
return $query;
}
foreach ( $data as $sub_key => $sub_value ) {
$new_key = sprintf( '%s.%s', $key, $sub_key );
$query = $this->recursively_build_key( $query, $new_key, $sub_value );
}
return $query;
}
/**
* Generate a valid auth signature.
*
* @since 1.4.0
*
* @param $headers
* @param $body
* @param $longform_date
* @param $shortform_date
*
* @return string
*/
protected function generate_auth_signature( $headers, $body = '', $longform_date = '', $shortform_date = '') {
$id = $this->id;
$secret = $this->secret;
$signing_key = $this->get_signing_key( $shortform_date );
$canonical_request = $this->create_canonical_request( $body, $headers );
$string_to_sign = $this->create_string_to_sign( $canonical_request, $longform_date, $shortform_date );
$signature = hash_hmac( 'sha256', $string_to_sign, $signing_key );
return $signature;
}
/**
* Get the formatted Auth Header.
*
* @since 1.4.0
*
* @param $headers
* @param $signature
* @param $shortform_date
*
* @return string
*/
protected function get_auth_header( $headers, $signature, $shortform_date ) {
$id = $this->id;
$region = $this->region;
$algo = 'AWS4-HMAC-SHA256';
$signed_headers = $this->get_signed_headers( $headers );
$credential = sprintf( '%s/%s/%s/%s/aws4_request', $id, $shortform_date, $region, 'ses' );
return sprintf( '%s Credential=%s, SignedHeaders=%s, Signature=%s', $algo, $credential, $signed_headers, $signature );
}
/**
* Get the proper host for this request.
*
* @since 1.4.0
*
* @return string
*/
private function get_host() {
return sprintf( 'email.%s.amazonaws.com', $this->region );
}
/**
* Create a canonical request.
*
* @since 1.4.0
*
* @param $payload
* @param $headers
*
* @return string
*/
private function create_canonical_request( $payload, $headers ) {
$method = 'POST';
$canonical_uri = '/';
$canonical_query_string = '';
$canonical_headers = $this->get_canonical_headers( $headers );
$signed_headers = $this->get_signed_headers( $headers );
$hashed_payload = $this->get_hashed_payload( $payload );
return sprintf( "%s\n%s\n%s\n%s\n%s\n%s", $method, $canonical_uri, $canonical_query_string, $canonical_headers, $signed_headers, $hashed_payload );
}
/**
* Hash the url-encoded payload.
*
* @since 1.4.0
*
* @param $payload
*
* @return string
*/
private function get_hashed_payload( $payload ) {
return hash( 'sha256', $payload );
}
/**
* Get canonical headers.
*
* @since 1.4.0
*
* @param $headers
*
* @return string
*/
private function get_canonical_headers( $headers ) {
$headers_string = '';
foreach ( $headers as $key => $value ) {
$headers_string .= sprintf( "%s:%s\n", strtolower( $key ), trim( $value ) );
}
return $headers_string;
}
/**
* Get signed headers.
*
* @since 1.4.0
*
* @param $headers
*
* @return string
*/
private function get_signed_headers( $headers ) {
$keys = array_keys( $headers );
$formatted = array();
foreach ( $keys as $key ) {
$formatted[] = strtolower( $key );
}
return join( ';', $formatted );
}
/**
* Create the string to sign for the auth header.
*
* @since 1.4.0
*
* @param $canonical_request
* @param $longform_date
* @param $shortform_date
*
* @return string
*/
private function create_string_to_sign( $canonical_request, $longform_date, $shortform_date ) {
$algo = 'AWS4-HMAC-SHA256';
$timestamp = $longform_date;
$scope = $this->generate_scope( $shortform_date );
$hashed_request = hash( 'sha256', $canonical_request );
return sprintf( "%s\n%s\n%s\n%s", $algo, $timestamp, $scope, $hashed_request );
}
/**
* Generate a scope string.
*
* @since 1.4.0
*
* @param $shortform_date
*
* @return string
*/
private function generate_scope( $shortform_date ) {
$region = $this->region;
return sprintf( '%s/%s/%s/aws4_request', $shortform_date, $region, 'ses' );
}
/**
* Get the signing key from our secret key.
*
* @since 1.4.0
*
* @param $shortform_date
*
* @return string
*/
private function get_signing_key( $shortform_date ) {
$secret = $this->secret;
$region = $this->region;
$date_key = hash_hmac( 'sha256', $shortform_date, "AWS4{$secret}", true );
$region_key = hash_hmac( 'sha256', $region, $date_key, true );
$service_key = hash_hmac( 'sha256', 'ses', $region_key, true );
return hash_hmac( 'sha256', 'aws4_request', $service_key, true );
}
}
@@ -0,0 +1,46 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Utils;
class Basic_Encrypted_Hash {
const METHOD = 'aes-256-ctr';
public function encrypt( $message ) {
$nonce_size = openssl_cipher_iv_length( self::METHOD );
$nonce = openssl_random_pseudo_bytes( $nonce_size );
$cipher = openssl_encrypt(
$message,
self::METHOD,
SECURE_AUTH_KEY,
OPENSSL_RAW_DATA,
$nonce
);
return base64_encode( $nonce . $cipher );
}
public function decrypt( $message ) {
$message = base64_decode( $message, true );
if ( $message === false ) {
return new \WP_Error( 'bad_encryption', 'Encryption failure' );
}
$nonce_size = openssl_cipher_iv_length( self::METHOD );
$nonce = mb_substr( $message, 0, $nonce_size, '8bit' );
$cipher = mb_substr( $message, $nonce_size, null, '8bit' );
$plaintext = openssl_decrypt(
$cipher,
self::METHOD,
SECURE_AUTH_KEY,
OPENSSL_RAW_DATA,
$nonce
);
return $plaintext;
}
}
@@ -0,0 +1,22 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Utils;
class Booliesh {
public static function get( $value, $default = false ) {
if ( is_string( $value ) ) {
$value = strtolower( $value );
}
if ( $value === false || $value === 0 || $value === '0' || $value === 'no' || $value === 'off' || $value === 'false' || empty( $value ) ) {
return false;
}
if ( $value === true || $value === 1 || $value === '1' || $value === 'yes' || $value === 'on' || $value === 'true' || ! empty( $value ) ) {
return true;
}
return $default;
}
}
@@ -0,0 +1,57 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Utils;
abstract class Fast_Endpoint {
public function __construct() {
$this->load_wp();
foreach( $this->extra_includes() as $file_to_include ) {
require_once( $file_to_include );
}
}
public abstract function run();
protected function extra_includes() {
return array();
}
private function load_wp() {
$found = false;
$cwd = dirname( __FILE__ );
while( ! $found ) {
$checked_path = $cwd . '/wp-load.php';
if ( file_exists( $checked_path ) ) {
require_once( $checked_path );
$found = true;
}
$cwd = dirname( $cwd );
}
require_once( ABSPATH . WPINC . '/default-constants.php' );
require_once( ABSPATH . WPINC . '/class-wp-textdomain-registry.php' );
require_once( ABSPATH . WPINC . '/capabilities.php' );
require_once( ABSPATH . WPINC . '/class-wp-session-tokens.php' );
require_once( ABSPATH . WPINC . '/class-wp-user-meta-session-tokens.php' );
require_once( ABSPATH . WPINC . '/class-wp-role.php' );
require_once( ABSPATH . WPINC . '/class-wp-roles.php' );
require_once( ABSPATH . WPINC . '/class-wp-user.php' );
require_once( ABSPATH . WPINC . '/l10n.php' );
require_once( ABSPATH . WPINC . '/user.php' );
require_once( ABSPATH . WPINC . '/pluggable.php' );
require_once( ABSPATH . WPINC . '/rest-api.php' );
require_once( ABSPATH . WPINC . '/kses.php' );
require_once( ABSPATH . WPINC . '/blocks.php' );
require_once( ABSPATH . WPINC . '/theme.php' );
wp_plugin_directory_constants();
wp_cookie_constants();
$GLOBALS['wp_textdomain_registry'] = new \WP_Textdomain_Registry();
}
}
@@ -0,0 +1,155 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Utils;
class Header_Parser {
public $standard_headers = array(
'from',
'reply-to',
'cc',
'bcc',
'content-type',
'MIME-Version',
);
public function parse( $headers ) {
if ( is_string( $headers ) ) {
$headers = preg_split( "/\r\n|\n|\r/", $headers );
}
if ( ! is_array( $headers ) ) {
$headers = array();
}
$parsed = array();
foreach ( $headers as $key => $value ) {
// Headers are numerically-indexed; get key/value from string content.
if ( is_numeric( $key ) ) {
$values = $this->get_header_from_string( $value );
if ( ! $values ) {
continue;
}
$parsed_key = $values['key'];
$parsed_value = $values['value'];
} else {
$parsed_key = in_array( strtolower( $key ), $this->standard_headers ) ? strtolower( $key ) : $key;
$parsed_value = $value;
}
if ( $parsed_key !== 'cc' && $parsed_key !== 'bcc' ) {
$parsed[ $parsed_key ] = $parsed_value;
continue;
}
if ( is_a( $parsed_value, Recipient_Collection::class ) ) {
$parsed[ $parsed_key ] = $parsed_value;
continue;
}
if ( ! isset( $parsed[ $parsed_key ] ) ) {
$parsed[ $parsed_key ] = new Recipient_Collection( array() );
}
$parsed_value = $this->get_email_from_header( $key, $parsed_value );
foreach( $parsed_value->recipients() as $recipient ) {
$parsed[ $parsed_key ]->add( $recipient );
}
}
return $parsed;
}
public function get_header_from_string( $string ) {
$parts = explode( ':', $string );
if ( count( $parts ) < 2 ) {
return false;
}
$key = trim( $parts[0] );
return array(
'key' => in_array( strtolower( $key ), $this->standard_headers ) ? strtolower( $key ) : $key,
'value' => trim( $parts[1] ),
);
}
public function get_email_from_header( $header_name, $header_string ) {
if ( is_string( $header_string ) ) {
$string = str_replace( sprintf( '%s:', $header_name ), '', $header_string );
}
if ( is_array( $header_string ) && isset( $header_string[0]['email'] ) ) {
$string = $header_string[0]['email'];
}
$recipients = new Recipient_Collection();
if ( $this->has_csv_emails( $string ) ) {
$strings = $this->get_csv_emails( $string );
foreach ( $strings as $string ) {
$email_data = $this->get_email_from_header( $header_name, $string )->recipients();
array_walk( $email_data, function( $email_item ) use ( $recipients ) {
$recipients->add_raw( $email_item->email(), $email_item->name() );
});
}
return $recipients;
}
if ( strpos( $string, '<' ) !== false ) {
preg_match( '/([^<]*)<([^>]*)>/', $string, $email_data );
$recipients->add_raw( $email_data[2], trim( $email_data[1], '" ' ) );
return $recipients;
}
$recipients->add_raw( trim( $string ), '' );
return $recipients;
}
public function has_csv_emails( $string ) {
preg_match('/.+@.+,.+@.+/', $string, $matches);
return ! empty( $matches );
}
public function get_csv_emails( $string ) {
$parts = explode( ',', $string );
$stored = array();
$emails = array();
$length = count( $parts );
foreach ( $parts as $key => $part ) {
$stored[] = $part;
if ( strpos( $part, '@' ) === false && $key < ( $length - 1 ) ) {
continue;
}
$emails[] = trim( implode( ',', $stored ) );
$stored = array();
}
return $emails;
}
public function get_formatted_cc( $values ) {
$response = array();
foreach( $values as $item ) {
$response[] = $item['email'];
}
return implode( ',', $response );
}
}
@@ -0,0 +1,43 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Utils;
class Import_Data_Checker {
public $import_plugins_to_check = array(
'gravityformspostmark/postmark.php' => 'postmark',
'gravityformssendgrid/sendgrid.php' => 'sendgrid',
'gravityformsmailgun/mailgun.php' => 'mailgun',
'wp-mail-smtp/wp_mail_smtp.php' => 'wpmailsmtp',
'wp-mail-smtp-pro/wp_mail_smtp.php' => 'wpmailsmtp',
);
/**
* Get a list of connector names that map to activated plugins we import from.
*
* @since 1.0
*
* @return array
*/
public function connectors_to_migrate() {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$active_plugins = array_filter( $this->import_plugins_to_check, function ( $key ) {
return \is_plugin_active( $key );
}, ARRAY_FILTER_USE_KEY );
return array_values( $active_plugins );
}
/**
* Check if any plugins that we import data from are activated.
* Used by setup wizard to determine if we should display the import data step.
*
* @since 1.0
*
* @return bool
*/
public function import_data_possible() {
return true;
}
}
@@ -0,0 +1,86 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Utils;
class Recipient_Collection {
public $recipients = array();
public function __construct( $recipients = array() ) {
if ( empty( $recipients ) ) {
return;
}
if ( is_string( $recipients ) ) {
$recipients = array( $recipients );
}
foreach ( $recipients as $recipient ) {
if ( is_string( $recipient ) ) {
$recipientObj = new Recipient( $recipient, '' );
} else {
$name = isset( $recipient['name'] ) ? $recipient['name'] : '';
$recipientObj = new Recipient( $recipient['email'], $name );
}
$this->add( $recipientObj );
}
}
public function add_raw( $email, $name ) {
$recipient = new Recipient( $email, $name );
$this->recipients[] = $recipient;
}
public function add( Recipient $recipient ) {
$this->recipients[] = $recipient;
}
public function first() {
if ( empty( $this->recipients ) ) {
return new Recipient( '', '' );
}
return reset( $this->recipients );
}
public function recipients() {
return $this->recipients;
}
public function count() {
return count( $this->recipients );
}
public function as_array() {
$return = array();
foreach ( $this->recipients as $recipient ) {
$return[] = $recipient->as_array();
}
return $return;
}
public function as_mailboxes() {
$return = array();
foreach ( $this->recipients as $recipient ) {
$return[] = $recipient->mailbox();
}
return $return;
}
public function as_string( $mailbox = false ) {
$items = array();
foreach( $this->recipients as $recipient ) {
$items[] = $mailbox ? $recipient->mailbox() : $recipient->email;
}
return implode( ',', $items );
}
}
@@ -0,0 +1,68 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Utils;
class Recipient_Parser {
public function parse( $to ) {
// Protect against double-parsing.
if ( is_a( $to, Recipient_Collection::class ) ) {
return $to;
}
if ( is_array( $to ) && isset( $to['email'] ) ) {
$to = array( $to );
}
if ( is_string( $to ) ) {
$to = array_filter( explode( ',', $to ) );
}
$collection = new Recipient_Collection();
if ( ! is_array( $to ) ) {
$to = array();
}
array_walk( $to, function ( $recipient ) use ( $collection ) {
if ( is_string( $recipient ) ) {
$collection->add( $this->get_recipient_from_string( $recipient ) );
}
if ( isset( $recipient['email'] ) ) {
$name = isset( $recipient['name'] ) ? $recipient['name'] : '';
$collection->add( new Recipient( $recipient['email'], $name ) );
}
} );
return $collection;
}
public function get_email_counts( $extra ) {
$values = array(
'to' => isset( $extra['to'] ) ? $extra['to'] : '',
'cc' => isset( $extra['headers']['cc'] ) ? $extra['headers']['cc'] : '',
'bcc' => isset( $extra['headers']['bcc'] ) ? $extra['headers']['bcc'] : ''
);
$count = 0;
foreach ( $values as $recipients ) {
$recipients = $this->parse( $recipients );
$count += $recipients->count();
}
return $count;
}
private function get_recipient_from_string( $string ) {
if ( strpos( $string, '<' ) === false ) {
return new Recipient( trim( $string ), '' );
}
// Email seems to be in RFC5322 Mailbox format
preg_match( '/([^<]*)<([^>]*)>/', $string, $email_data );
return new Recipient( $email_data[2], trim( $email_data[1], '" ' ) );
}
}
@@ -0,0 +1,37 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Utils;
class Recipient {
public $email;
public $name;
public function __construct( $email, $name ) {
$this->email = $email;
$this->name = $name;
}
public function name() {
return $this->name;
}
public function email() {
return $this->email;
}
public function mailbox() {
if ( empty( $this->name ) ) {
return $this->email;
}
return sprintf( '%s <%s>', $this->name, $this->email );
}
public function as_array() {
return array_filter ( array(
'email' => $this->email,
'name' => $this->name,
) );
}
}
@@ -0,0 +1,205 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Utils;
/**
* Source_Parser
*
* Takes the trace from a debug_backtrace() call and parses it to find the source
* of a wp_mail() call (if present).
*/
class Source_Parser {
/**
* Get the source of a wp_mail call from a given trace.
*
* @since 1.0
*
* @param array $trace
*
* @return string
*/
public function get_source_from_trace( $trace ) {
$relevant_trace = $this->get_relevant_trace_data( $trace );
if ( ! $relevant_trace ) {
return __( 'N/A', 'gravitysmtp' );
}
$file_path = $relevant_trace['file'];
return $this->get_source_from_file_path( $file_path );
}
/**
* Get the relevant wp_mail trace data from a given trace array.
*
* @since 1.0
*
* @param array $trace
*
* @return boolean|array
*/
protected function get_relevant_trace_data( $trace ) {
$filtered = array_filter( $trace, function ( $item ) {
return $item['function'] === 'wp_mail';
} );
if ( empty( $filtered ) ) {
return false;
}
return reset( $filtered );
}
/**
* For a given file path, determine the source of the call (WordPress core, a theme, a plugin/mu-plugin, or N/A if not found)
*
* @since 1.0
*
* @param string $path
*
* @return string
*/
protected function get_source_from_file_path( $path ) {
$core = $this->get_core_source( $path );
if ( $core ) {
return $core;
}
$theme = $this->get_theme_source( $path );
if ( $theme ) {
return $theme;
}
$plugin = $this->get_plugin_source( $path );
if ( $plugin ) {
return $plugin;
}
$mu_plugin = $this->get_mu_plugin_source( $path );
if ( $mu_plugin ) {
return $mu_plugin;
}
return __( 'N/A', 'gravitysmtp' );
}
/**
* Determine if WordPress Core was the source.
*
* @since 1.0
*
* @param string $path
*
* @return string|boolean
*/
protected function get_core_source( $path ) {
if (
strpos( $path, 'wp-admin' ) !== false ||
strpos( $path, 'wp-includes' ) !== false
) {
return __( 'WordPress', 'gravitysmtp' );
}
return false;
}
/**
* Determine if a plugin was the source.
*
* @since 1.0
*
* @param string $path
*
* @return string|boolean
*/
protected function get_plugin_source( $path ) {
if ( ! defined( 'WP_PLUGIN_DIR' ) ) {
return false;
}
$root = basename( WP_PLUGIN_DIR );
$separator = defined( 'DIRECTORY_SEPARATOR' ) ? '\\' . DIRECTORY_SEPARATOR : '\/';
preg_match( "/$separator$root$separator(.[^$separator]+)($separator|\.php)/", $path, $result );
if ( ! empty( $result[1] ) ) {
$all_plugins = \get_plugins();
$plugin_slug = $result[1];
$filtered = array_filter( $all_plugins, function ( $plugin_data, $plugin ) use ( $plugin_slug ) {
return 1 === preg_match( "/^$plugin_slug(\/|\.php)/", $plugin ) && isset( $plugin_data['Name'] );
}, ARRAY_FILTER_USE_BOTH );
if ( ! empty( $filtered ) ) {
$found = reset( $filtered );
return $found['Name'];
}
return $result[1];
}
return false;
}
/**
* Determine if an MU-plugin was the source.
*
* @since 1.0
*
* @param string $path
*
* @return string|boolean
*/
protected function get_mu_plugin_source( $path ) {
if ( ! defined( 'WPMU_PLUGIN_DIR' ) ) {
return false;
}
$root = basename( WPMU_PLUGIN_DIR );
$separator = defined( 'DIRECTORY_SEPARATOR' ) ? '\\' . DIRECTORY_SEPARATOR : '\/';
preg_match( "/$separator$root$separator(.[^$separator]+)($separator|\.php)/", $path, $result );
if ( ! empty( $result[1] ) ) {
return __( 'MU Plugin' );
}
return false;
}
/**
* Determine if a theme was the source.
*
* @since 1.0
*
* @param string $path
*
* @return string|boolean
*/
protected function get_theme_source( $path ) {
if ( ! defined( 'WP_CONTENT_DIR' ) ) {
return false;
}
$root = basename( WP_CONTENT_DIR );
$separator = defined( 'DIRECTORY_SEPARATOR' ) ? '\\' . DIRECTORY_SEPARATOR : '\/';
preg_match( "/$separator$root{$separator}themes{$separator}(.[^$separator]+)/", $path, $result );
if ( ! empty( $result[1] ) ) {
$theme = \wp_get_theme( $result[1] );
return method_exists( $theme, 'get' ) ? $theme->get( 'Name' ) : $result[1];
}
return false;
}
}
@@ -0,0 +1,80 @@
<?php
namespace Gravity_Forms\Gravity_SMTP\Utils;
class SQL_Filter_Parser {
protected $queryable = array(
'id',
'date_created',
'date_updated',
'service',
'subject',
'message',
'status',
);
protected $date_queryable = array(
'date_created',
'date_updated',
);
public function process_filters( $filters, $trailing_union = false, $union = 'AND', $passed_key = null ) {
global $wpdb;
$sql_array = array();
foreach ( $filters as $key => $value ) {
if ( in_array( $key, $this->date_queryable ) && is_array( $value ) ) {
$from = $value[0];
$to = $value[1];
if ( $from == $to ) {
$from = sprintf( '%s 00:00:00', $from );
$to = sprintf( '%s 23:59:59', $to );
}
$from = get_gmt_from_date( $from );
$to = get_gmt_from_date( $to );
$sql_array[] = $wpdb->prepare( "`" . $key . "` BETWEEN %s AND %s", $from, $to );
continue;
}
if ( is_array( $value ) ) {
$sql_array[] = $this->process_filters( $value, false, 'OR', $key );
continue;
}
if ( $passed_key !== null ) {
$key = $passed_key;
}
if ( $key === 'attachments' ) {
$inverter = $value === 'no' ? null : 'NOT';
$sql_array[] = '`extra` ' . $inverter . ' LIKE "%\"attachments\";a:0:%"';
continue;
}
if ( in_array( $key, $this->queryable ) ) {
$sql_array[] = $wpdb->prepare( "`" . $key . "` = %s", $value );
continue;
}
$like_sql = sprintf( '"%s"[^"]+"%s"', preg_quote( $key ), preg_quote( $value ) );
$sql_array[] = $wpdb->prepare( '`extra` RLIKE %s', $like_sql );
}
$sql = implode( ' ' . $union . ' ', $sql_array );
$sql = "($sql)";
if ( $trailing_union ) {
$sql .= ' ' . $union . ' ';
}
return $sql;
}
}
@@ -0,0 +1,145 @@
<?php
namespace Gravity_Forms\Gravity_Tools\Utils;
use Gravity_Forms\Gravity_SMTP\Connectors\Connector_Service_Provider;
use Gravity_Forms\Gravity_SMTP\Connectors\Endpoints\Save_Plugin_Settings_Endpoint;
use Gravity_Forms\Gravity_SMTP\Data_Store\Const_Data_Store;
use Gravity_Forms\Gravity_SMTP\Data_Store\Data_Store_Router;
use Gravity_Forms\Gravity_SMTP\Data_Store\Opts_Data_Store;
use Gravity_Forms\Gravity_SMTP\Data_Store\Plugin_Opts_Data_Store;
use Gravity_Forms\Gravity_SMTP\Logging\Debug\Null_Logger;
use Gravity_Forms\Gravity_SMTP\Logging\Debug\Null_Logging_Provider;
use Gravity_Forms\Gravity_SMTP\Models\Event_Model;
use Gravity_Forms\Gravity_SMTP\Utils\AWS_Signature_Handler;
use Gravity_Forms\Gravity_SMTP\Utils\Basic_Encrypted_Hash;
use Gravity_Forms\Gravity_SMTP\Utils\Header_Parser;
use Gravity_Forms\Gravity_SMTP\Utils\Import_Data_Checker;
use Gravity_Forms\Gravity_SMTP\Utils\Recipient_Parser;
use Gravity_Forms\Gravity_SMTP\Utils\Source_Parser;
use Gravity_Forms\Gravity_SMTP\Utils\SQL_Filter_Parser;
use Gravity_Forms\Gravity_Tools\Cache\Cache;
use Gravity_Forms\Gravity_Tools\Service_Container;
use Gravity_Forms\Gravity_Tools\Service_Provider;
class Utils_Service_Provider extends Service_Provider {
const CACHE = 'cache';
const COMMON = 'common';
const HEADER_PARSER = 'header_parser';
const IMPORT_DATA_CHECKER = 'import_data_checker';
const LOGGER = 'logger_util';
const RECIPIENT_PARSER = 'recipient_parser';
const SOURCE_PARSER = 'source_parser';
const FILTER_PARSER = 'filter_parser';
const ATTACHMENTS_SAVER = 'attachments_saver';
const AWS_SIGNATURE_HANDLER = 'aws_signature_handler';
const BASIC_ENCRYPTED_HASH = 'basic_encrypted_hash';
public function register( Service_Container $container ) {
$container->add( Connector_Service_Provider::DATA_STORE_CONST, function () {
return new Const_Data_Store();
} );
$container->add( Connector_Service_Provider::DATA_STORE_OPTS, function () {
return new Opts_Data_Store();
} );
$container->add( Connector_Service_Provider::DATA_STORE_PLUGIN_OPTS, function () {
return new Plugin_Opts_Data_Store();
} );
$container->add( Connector_Service_Provider::DATA_STORE_ROUTER, function () use ( $container ) {
return new Data_Store_Router( $container->get( Connector_Service_Provider::DATA_STORE_CONST ), $container->get( Connector_Service_Provider::DATA_STORE_OPTS ), $container->get( Connector_Service_Provider::DATA_STORE_PLUGIN_OPTS ) );
} );
$container->add( self::COMMON, function () use ( $container ) {
$data = $container->get( Connector_Service_Provider::DATA_STORE_ROUTER );
$key = $data->get_plugin_setting( Save_Plugin_Settings_Endpoint::PARAM_LICENSE_KEY, '' );
return new Common( GRAVITY_MANAGER_URL, GRAVITY_SUPPORT_URL, $key );
} );
$container->add( self::CACHE, function () use ( $container ) {
return new Cache( $container->get( self::COMMON ) );
} );
$container->add( self::HEADER_PARSER, function () {
return new Header_Parser();
} );
$container->add( self::IMPORT_DATA_CHECKER, function () {
return new Import_Data_Checker();
} );
$container->add( self::SOURCE_PARSER, function () {
return new Source_Parser();
} );
$container->add( self::LOGGER, function () {
return new Null_Logger( new Null_Logging_Provider() );
} );
$container->add( self::RECIPIENT_PARSER, function () {
return new Recipient_Parser();
} );
$container->add( self::FILTER_PARSER, function () {
return new SQL_Filter_Parser();
} );
$container->add( self::AWS_SIGNATURE_HANDLER, function () {
return new AWS_Signature_Handler();
} );
$container->add( self::BASIC_ENCRYPTED_HASH, function () {
return new Basic_Encrypted_Hash();
} );
}
public function init( \Gravity_Forms\Gravity_Tools\Service_Container $container ) {
add_action( 'init', function () {
add_filter( 'cron_schedules', function ( $schedules ) {
$schedules[ 'every-minute' ] = array(
'interval' => MINUTE_IN_SECONDS,
'display' => esc_html__( 'Every Minute', 'gravitysmtp' ),
);
return $schedules;
} );
}, 0 );
add_action( 'gravitysmtp_after_mail_updated', function ( $email_id, $email_data ) use ( $container ) {
if ( ! empty( $email_data['extra'] ) && is_string( $email_data['extra'] ) ) {
$email_data['extra'] = unserialize( $email_data['extra'] );
}
if ( empty( $email_data['extra']['attachments'] ) ) {
return;
}
if ( ! empty( $email_data['extra']['attachments_saved'] ) ) {
return;
}
/**
* @var Data_Store_Router $data
*/
$data = $container->get( Connector_Service_Provider::DATA_STORE_ROUTER );
$save_attachments = $data->get_plugin_setting( Save_Plugin_Settings_Endpoint::PARAM_SAVE_ATTACHMENTS_ENABLED, 'false' );
if ( empty( $save_attachments ) || $save_attachments === 'false' ) {
return;
}
$new_attachments = $container->get( self::ATTACHMENTS_SAVER )->save_attachments( $email_id, $email_data['extra']['attachments'] );
$email_data['extra']['attachments_saved'] = true;
$email_data['extra']['attachments'] = $new_attachments;
/**
* @var Event_Model $events
*/
$events = $container->get( Connector_Service_Provider::EVENT_MODEL );
$events->update( array( 'extra' => serialize( $email_data['extra'] ) ), $email_id );
}, 10, 2 );
}
}