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