| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Utilities { |
| 4 |
|
| 5 |
use WPDataAccess\WPDA; |
| 6 |
|
| 7 |
class WPDA_Mail { |
| 8 |
|
| 9 |
const WPDA_MAIL_SERVER_OPTION = 'wpda_mail_server'; |
| 10 |
const CIPHER = 'AES-256-CBC'; |
| 11 |
const KEY = '886BF60F78C0EEACA0DC73D9C8E9A964CE7CFCD9DD459DE76F985030ADE0423A'; |
| 12 |
|
| 13 |
public static function send( |
| 14 |
$to, |
| 15 |
$subject, |
| 16 |
$message, |
| 17 |
$attachments = array() |
| 18 |
) { |
| 19 |
|
| 20 |
ob_start(); |
| 21 |
|
| 22 |
do_action( 'wpda_sending_mail' ); |
| 23 |
$result = wp_mail( |
| 24 |
$to, |
| 25 |
$subject, |
| 26 |
$message, |
| 27 |
array( |
| 28 |
'Content-Type: text/html; charset=UTF-8', |
| 29 |
), |
| 30 |
$attachments |
| 31 |
); |
| 32 |
|
| 33 |
$output = ob_get_clean(); |
| 34 |
|
| 35 |
if ( $result ) { |
| 36 |
return array( |
| 37 |
'code' => 'ok', |
| 38 |
'debug' => $output, |
| 39 |
'message' => 'Successfully processed request', |
| 40 |
); |
| 41 |
} else { |
| 42 |
return array( |
| 43 |
'code' => 'error', |
| 44 |
'debug' => $output, |
| 45 |
'message' => 'Failed to process request', |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
public static function mail_activated() { |
| 52 |
|
| 53 |
$mail = self::get_option(); |
| 54 |
if ( |
| 55 |
false !== $mail && |
| 56 |
( |
| 57 |
! isset( $mail['activate'] ) || |
| 58 |
'on' === $mail['activate'] |
| 59 |
) |
| 60 |
) { |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
return false; |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
public static function get_option() { |
| 69 |
|
| 70 |
$option = get_option( |
| 71 |
self::WPDA_MAIL_SERVER_OPTION |
| 72 |
); |
| 73 |
|
| 74 |
if ( false !== $option ) { |
| 75 |
return json_decode( self::decrypt( $option ), true ); // Decrypt option |
| 76 |
} else { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
public static function update_option( $option ) { |
| 83 |
|
| 84 |
return update_option( |
| 85 |
self::WPDA_MAIL_SERVER_OPTION, |
| 86 |
self::encrypt( json_encode( $option ) ) // Encrypt option |
| 87 |
); |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
public static function delete_option() { |
| 92 |
|
| 93 |
return delete_option( |
| 94 |
self::WPDA_MAIL_SERVER_OPTION |
| 95 |
); |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
private static function encrypt( $string ) { |
| 100 |
|
| 101 |
$ivlen = openssl_cipher_iv_length( self::CIPHER ); |
| 102 |
$iv = openssl_random_pseudo_bytes( $ivlen ); |
| 103 |
|
| 104 |
$ciphertext_raw = openssl_encrypt( $string, self::CIPHER, self::KEY, OPENSSL_RAW_DATA, $iv ); |
| 105 |
$hmac = hash_hmac('sha256', $ciphertext_raw, self::KEY, true ); |
| 106 |
|
| 107 |
return base64_encode($iv . $hmac . $ciphertext_raw); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
private static function decrypt( $string ) { |
| 112 |
|
| 113 |
$c = base64_decode( $string ); |
| 114 |
$ivlen = openssl_cipher_iv_length( self::CIPHER ); |
| 115 |
$iv = substr( $c, 0, $ivlen ); |
| 116 |
$hmac = substr( $c, $ivlen, 32 ); |
| 117 |
|
| 118 |
$ciphertext_raw = substr( $c, $ivlen + 32 ); |
| 119 |
$calculated_hmac = hash_hmac('sha256', $ciphertext_raw, self::KEY, true); |
| 120 |
|
| 121 |
if ( ! hash_equals($hmac, $calculated_hmac) ) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
return openssl_decrypt( $ciphertext_raw, self::CIPHER, self::KEY, OPENSSL_RAW_DATA, $iv ); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
|