説明なし

easy-wp-smtp.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. <?php
  2. use PHPMailer\PHPMailer\PHPMailer;
  3. use PHPMailer\PHPMailer\Exception;
  4. /*
  5. Plugin Name: Easy WP SMTP
  6. Version: 1.4.7
  7. Plugin URI: https://wp-ecommerce.net/easy-wordpress-smtp-send-emails-from-your-wordpress-site-using-a-smtp-server-2197
  8. Author: wpecommerce, alexanderfoxc
  9. Author URI: https://wp-ecommerce.net/
  10. Description: Send email via SMTP from your WordPress Blog
  11. Text Domain: easy-wp-smtp
  12. Domain Path: /languages
  13. */
  14. //Prefix/Slug - swpsmtp
  15. class EasyWPSMTP {
  16. public $opts;
  17. public $plugin_file;
  18. protected static $instance = null;
  19. public static $reset_log_str = "Easy WP SMTP debug log file\r\n\r\n";
  20. public function __construct() {
  21. $this->opts = get_option( 'swpsmtp_options' );
  22. $this->opts = ! is_array( $this->opts ) ? array() : $this->opts;
  23. $this->plugin_file = __FILE__;
  24. require_once 'class-easywpsmtp-utils.php';
  25. add_action( 'plugins_loaded', array( $this, 'plugins_loaded_handler' ) );
  26. add_filter( 'wp_mail', array( $this, 'wp_mail' ), 2147483647 );
  27. add_action( 'phpmailer_init', array( $this, 'init_smtp' ), 999 );
  28. add_action( 'admin_init', array( $this, 'admin_init' ) );
  29. if ( ! empty( $this->opts['smtp_settings']['enable_debug'] ) ) {
  30. add_action( 'wp_mail_failed', array( $this, 'wp_mail_failed' ) );
  31. }
  32. if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
  33. require_once 'class-easywpsmtp-admin.php';
  34. register_activation_hook( __FILE__, array( $this, 'activate' ) );
  35. register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
  36. register_uninstall_hook( __FILE__, 'swpsmtp_uninstall' );
  37. add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2 );
  38. add_filter( 'plugin_row_meta', array( $this, 'register_plugin_links' ), 10, 2 );
  39. add_action( 'admin_notices', array( $this, 'admin_notices' ) );
  40. }
  41. }
  42. public static function get_instance() {
  43. if ( null === self::$instance ) {
  44. self::$instance = new self();
  45. }
  46. return self::$instance;
  47. }
  48. public function wp_mail( $args ) {
  49. $domain = $this->is_domain_blocked();
  50. if ( false !== $domain && ( isset( $this->opts['block_all_emails'] ) && 1 === $this->opts['block_all_emails'] ) ) {
  51. $this->log(
  52. "\r\n------------------------------------------------------------------------------------------------------\r\n" .
  53. 'Domain check failed: website domain (' . $domain . ") is not in allowed domains list.\r\n" .
  54. "Following email not sent (block all emails option is enabled):\r\n" .
  55. 'To: ' . $args['to'] . '; Subject: ' . $args['subject'] . "\r\n" .
  56. "------------------------------------------------------------------------------------------------------\r\n\r\n"
  57. );
  58. return $args;
  59. }
  60. if ( ! empty( $this->opts['smtp_settings']['enable_debug'] ) ) {
  61. $line = sprintf(
  62. 'Headers: %s, To: %s, Subject: %s',
  63. ! empty( $args['headers'] && is_array( $args['headers'] ) ) ? implode( ' | ', $args['headers'] ) : '',
  64. ! empty( $args['to'] ) ? $args['to'] : '',
  65. ! empty( $args['subject'] ) ? $args['subject'] : ''
  66. );
  67. $this->log( $line . "\r\n" );
  68. }
  69. return $args;
  70. }
  71. public function wp_mail_failed( $wp_error ) {
  72. if ( ! empty( $wp_error->errors ) && ! empty( $wp_error->errors['wp_mail_failed'] ) && is_array( $wp_error->errors['wp_mail_failed'] ) ) {
  73. $this->log( '*** ' . implode( ' | ', $wp_error->errors['wp_mail_failed'] ) . " ***\r\n" );
  74. }
  75. }
  76. public function init_smtp( &$phpmailer ) {
  77. //check if SMTP credentials have been configured.
  78. if ( ! $this->credentials_configured() ) {
  79. return;
  80. }
  81. //check if Domain Check enabled
  82. $domain = $this->is_domain_blocked();
  83. if ( false !== $domain ) {
  84. //domain check failed
  85. //let's check if we have block all emails option enabled
  86. if ( isset( $this->opts['block_all_emails'] ) && 1 === $this->opts['block_all_emails'] ) {
  87. // it's enabled. Let's use gag mailer class that would prevent emails from being sent out.
  88. require_once 'class-easywpsmtp-gag-mailer.php';
  89. $phpmailer = new EasyWPSMTP_Gag_Mailer();
  90. } else {
  91. // it's disabled. Let's write some info to the log
  92. $this->log(
  93. "\r\n------------------------------------------------------------------------------------------------------\r\n" .
  94. 'Domain check failed: website domain (' . $domain . ") is not in allowed domains list.\r\n" .
  95. "SMTP settings won't be used.\r\n" .
  96. "------------------------------------------------------------------------------------------------------\r\n\r\n"
  97. );
  98. }
  99. return;
  100. }
  101. /* Set the mailer type as per config above, this overrides the already called isMail method */
  102. $phpmailer->IsSMTP();
  103. if ( isset( $this->opts['force_from_name_replace'] ) && 1 === $this->opts['force_from_name_replace'] ) {
  104. $from_name = $this->opts['from_name_field'];
  105. } else {
  106. $from_name = ! empty( $phpmailer->FromName ) ? $phpmailer->FromName : $this->opts['from_name_field'];
  107. }
  108. $from_email = $this->opts['from_email_field'];
  109. //set ReplyTo option if needed
  110. //this should be set before SetFrom, otherwise might be ignored
  111. if ( ! empty( $this->opts['reply_to_email'] ) ) {
  112. if ( isset( $this->opts['sub_mode'] ) && 1 === $this->opts['sub_mode'] ) {
  113. if ( count( $phpmailer->getReplyToAddresses() ) >= 1 ) {
  114. // Substitute from_email_field with reply_to_email
  115. if ( array_key_exists( $this->opts['from_email_field'], $phpmailer->getReplyToAddresses() ) ) {
  116. $reply_to_emails = $phpmailer->getReplyToAddresses();
  117. unset( $reply_to_emails[ $this->opts['from_email_field'] ] );
  118. $phpmailer->clearReplyTos();
  119. foreach ( $reply_to_emails as $reply_to_email => $reply_to_name ) {
  120. $phpmailer->AddReplyTo( $reply_to_email, $reply_to_name );
  121. }
  122. $phpmailer->AddReplyTo( $this->opts['reply_to_email'], $from_name );
  123. }
  124. } else { // Reply-to array is empty so add reply_to_email
  125. $phpmailer->AddReplyTo( $this->opts['reply_to_email'], $from_name );
  126. }
  127. } else { // Default behaviour
  128. $phpmailer->AddReplyTo( $this->opts['reply_to_email'], $from_name );
  129. }
  130. }
  131. if ( ! empty( $this->opts['bcc_email'] ) ) {
  132. $bcc_emails = explode( ',', $this->opts['bcc_email'] );
  133. foreach ( $bcc_emails as $bcc_email ) {
  134. $bcc_email = trim( $bcc_email );
  135. $phpmailer->AddBcc( $bcc_email );
  136. }
  137. }
  138. // let's see if we have email ignore list populated
  139. if ( isset( $this->opts['email_ignore_list'] ) && ! empty( $this->opts['email_ignore_list'] ) ) {
  140. $emails_arr = explode( ',', $this->opts['email_ignore_list'] );
  141. $from = $phpmailer->From;
  142. $match_found = false;
  143. foreach ( $emails_arr as $email ) {
  144. if ( strtolower( trim( $email ) ) === strtolower( trim( $from ) ) ) {
  145. $match_found = true;
  146. break;
  147. }
  148. }
  149. if ( $match_found ) {
  150. //we should not override From and Fromname
  151. $from_email = $phpmailer->From;
  152. $from_name = $phpmailer->FromName;
  153. }
  154. }
  155. $phpmailer->From = $from_email;
  156. $phpmailer->FromName = $from_name;
  157. $phpmailer->SetFrom( $phpmailer->From, $phpmailer->FromName );
  158. //This should set Return-Path header for servers that are not properly handling it, but needs testing first
  159. //$phpmailer->Sender = $phpmailer->From;
  160. /* Set the SMTPSecure value */
  161. if ( 'none' !== $this->opts['smtp_settings']['type_encryption'] ) {
  162. $phpmailer->SMTPSecure = $this->opts['smtp_settings']['type_encryption'];
  163. }
  164. /* Set the other options */
  165. $phpmailer->Host = $this->opts['smtp_settings']['host'];
  166. $phpmailer->Port = $this->opts['smtp_settings']['port'];
  167. /* If we're using smtp auth, set the username & password */
  168. if ( 'yes' === $this->opts['smtp_settings']['autentication'] ) {
  169. $phpmailer->SMTPAuth = true;
  170. $phpmailer->Username = $this->opts['smtp_settings']['username'];
  171. $phpmailer->Password = $this->get_password();
  172. }
  173. //PHPMailer 5.2.10 introduced this option. However, this might cause issues if the server is advertising TLS with an invalid certificate.
  174. $phpmailer->SMTPAutoTLS = false;
  175. if ( isset( $this->opts['smtp_settings']['insecure_ssl'] ) && false !== $this->opts['smtp_settings']['insecure_ssl'] ) {
  176. // Insecure SSL option enabled
  177. $phpmailer->SMTPOptions = array(
  178. 'ssl' => array(
  179. 'verify_peer' => false,
  180. 'verify_peer_name' => false,
  181. 'allow_self_signed' => true,
  182. ),
  183. );
  184. }
  185. //set reasonable timeout
  186. $phpmailer->Timeout = 10;
  187. }
  188. public function test_mail( $to_email, $subject, $message ) {
  189. $ret = array();
  190. if ( ! $this->credentials_configured() ) {
  191. return false;
  192. }
  193. global $wp_version;
  194. if ( version_compare( $wp_version, '5.4.99' ) > 0 ) {
  195. require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
  196. require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
  197. require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
  198. $mail = new PHPMailer( true );
  199. } else {
  200. require_once ABSPATH . WPINC . '/class-phpmailer.php';
  201. $mail = new \PHPMailer( true );
  202. }
  203. try {
  204. $charset = get_bloginfo( 'charset' );
  205. $mail->CharSet = $charset;
  206. $from_name = $this->opts['from_name_field'];
  207. $from_email = $this->opts['from_email_field'];
  208. $mail->IsSMTP();
  209. // send plain text test email
  210. $mail->ContentType = 'text/plain';
  211. $mail->IsHTML( false );
  212. /* If using smtp auth, set the username & password */
  213. if ( 'yes' === $this->opts['smtp_settings']['autentication'] ) {
  214. $mail->SMTPAuth = true;
  215. $mail->Username = $this->opts['smtp_settings']['username'];
  216. $mail->Password = $this->get_password();
  217. }
  218. /* Set the SMTPSecure value, if set to none, leave this blank */
  219. if ( 'none' !== $this->opts['smtp_settings']['type_encryption'] ) {
  220. $mail->SMTPSecure = $this->opts['smtp_settings']['type_encryption'];
  221. }
  222. /* PHPMailer 5.2.10 introduced this option. However, this might cause issues if the server is advertising TLS with an invalid certificate. */
  223. $mail->SMTPAutoTLS = false;
  224. if ( isset( $this->opts['smtp_settings']['insecure_ssl'] ) && false !== $this->opts['smtp_settings']['insecure_ssl'] ) {
  225. // Insecure SSL option enabled
  226. $mail->SMTPOptions = array(
  227. 'ssl' => array(
  228. 'verify_peer' => false,
  229. 'verify_peer_name' => false,
  230. 'allow_self_signed' => true,
  231. ),
  232. );
  233. }
  234. /* Set the other options */
  235. $mail->Host = $this->opts['smtp_settings']['host'];
  236. $mail->Port = $this->opts['smtp_settings']['port'];
  237. //Add reply-to if set in settings.
  238. if ( ! empty( $this->opts['reply_to_email'] ) ) {
  239. $mail->AddReplyTo( $this->opts['reply_to_email'], $from_name );
  240. }
  241. //Add BCC if set in settings.
  242. if ( ! empty( $this->opts['bcc_email'] ) ) {
  243. $bcc_emails = explode( ',', $this->opts['bcc_email'] );
  244. foreach ( $bcc_emails as $bcc_email ) {
  245. $bcc_email = trim( $bcc_email );
  246. $mail->AddBcc( $bcc_email );
  247. }
  248. }
  249. $mail->SetFrom( $from_email, $from_name );
  250. //This should set Return-Path header for servers that are not properly handling it, but needs testing first
  251. //$mail->Sender = $mail->From;
  252. $mail->Subject = $subject;
  253. $mail->Body = $message;
  254. $mail->AddAddress( $to_email );
  255. global $debug_msg;
  256. $debug_msg = '';
  257. $mail->Debugoutput = function ( $str, $level ) {
  258. global $debug_msg;
  259. $debug_msg .= $str;
  260. };
  261. $mail->SMTPDebug = 1;
  262. //set reasonable timeout
  263. $mail->Timeout = 10;
  264. /* Send mail and return result */
  265. $mail->Send();
  266. $mail->ClearAddresses();
  267. $mail->ClearAllRecipients();
  268. } catch ( \Exception $e ) {
  269. $ret['error'] = $mail->ErrorInfo;
  270. } catch ( \Throwable $e ) {
  271. $ret['error'] = $mail->ErrorInfo;
  272. }
  273. $ret['debug_log'] = $debug_msg;
  274. return $ret;
  275. }
  276. public function admin_init() {
  277. if ( current_user_can( 'manage_options' ) ) {
  278. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  279. add_action( 'wp_ajax_swpsmtp_clear_log', array( $this, 'clear_log' ) );
  280. add_action( 'wp_ajax_swpsmtp_self_destruct', array( $this, 'self_destruct_handler' ) );
  281. }
  282. //view log file
  283. if ( isset( $_GET['swpsmtp_action'] ) ) {
  284. if ( 'view_log' === $_GET['swpsmtp_action'] ) {
  285. $log_file_name = isset( $this->opts['smtp_settings']['log_file_name'] ) ? $this->opts['smtp_settings']['log_file_name'] : '';
  286. if ( empty( $log_file_name ) ) {
  287. //Nothing in the log file yet so nothing to show.
  288. wp_die( 'Nothing in the log file yet.' );
  289. }
  290. if ( ! file_exists( plugin_dir_path( __FILE__ ) . $log_file_name ) ) {
  291. if ( $this->log( self::$reset_log_str ) === false ) {
  292. wp_die( esc_html( sprintf( 'Can\'t write to log file. Check if plugin directory (%s) is writeable.', plugin_dir_path( __FILE__ ) ) ) );
  293. };
  294. }
  295. $logfile = fopen( plugin_dir_path( __FILE__ ) . $log_file_name, 'rb' ); //phpcs:ignore
  296. if ( ! $logfile ) {
  297. wp_die( 'Can\'t open log file.' );
  298. }
  299. header( 'Content-Type: text/plain' );
  300. fpassthru( $logfile );
  301. die;
  302. }
  303. }
  304. //check if this is export settings request
  305. $is_export_settings = filter_input( INPUT_POST, 'swpsmtp_export_settings', FILTER_SANITIZE_NUMBER_INT );
  306. if ( $is_export_settings ) {
  307. check_admin_referer( 'easy_wp_smtp_export_settings', 'easy_wp_smtp_export_settings_nonce' );
  308. $data = array();
  309. $opts = get_option( 'swpsmtp_options', array() );
  310. $data['swpsmtp_options'] = $opts;
  311. $swpsmtp_pass_encrypted = get_option( 'swpsmtp_pass_encrypted', false );
  312. $data['swpsmtp_pass_encrypted'] = $swpsmtp_pass_encrypted;
  313. if ( $swpsmtp_pass_encrypted ) {
  314. $swpsmtp_enc_key = get_option( 'swpsmtp_enc_key', false );
  315. $data['swpsmtp_enc_key'] = $swpsmtp_enc_key;
  316. }
  317. $smtp_test_mail = get_option( 'smtp_test_mail', array() );
  318. $data['smtp_test_mail'] = $smtp_test_mail;
  319. $out = array();
  320. $out['data'] = wp_json_encode( $data );
  321. $out['ver'] = 2;
  322. $out['checksum'] = md5( $out['data'] );
  323. $filename = 'easy_wp_smtp_settings.json';
  324. header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
  325. header( 'Content-Type: application/json' );
  326. echo wp_json_encode( $out );
  327. exit;
  328. }
  329. $is_import_settings = filter_input( INPUT_POST, 'swpsmtp_import_settings', FILTER_SANITIZE_NUMBER_INT );
  330. if ( $is_import_settings ) {
  331. check_admin_referer( 'easy_wp_smtp_import_settings', 'easy_wp_smtp_import_settings_nonce' );
  332. $err_msg = __( 'Error occurred during settings import', 'easy-wp-smtp' );
  333. if ( empty( $_FILES['swpsmtp_import_settings_file'] ) ) {
  334. echo esc_html( $err_msg );
  335. wp_die();
  336. }
  337. $in_raw = file_get_contents( $_FILES['swpsmtp_import_settings_file']['tmp_name'] ); //phpcs:ignore
  338. try {
  339. $in = json_decode( $in_raw, true );
  340. if ( json_last_error() !== 0 ) {
  341. $in = unserialize( $in_raw ); //phpcs:ignore
  342. }
  343. if ( empty( $in['data'] ) ) {
  344. echo esc_html( $err_msg );
  345. wp_die();
  346. }
  347. if ( empty( $in['checksum'] ) ) {
  348. echo esc_html( $err_msg );
  349. wp_die();
  350. }
  351. if ( md5( $in['data'] ) !== $in['checksum'] ) {
  352. echo esc_html( $err_msg );
  353. wp_die();
  354. }
  355. $data = json_decode( $in['data'], true );
  356. if ( json_last_error() !== 0 ) {
  357. $data = unserialize( $in['data'] ); //phpcs:ignore
  358. }
  359. update_option( 'swpsmtp_options', $data['swpsmtp_options'] );
  360. update_option( 'swpsmtp_pass_encrypted', $data['swpsmtp_pass_encrypted'] );
  361. if ( $data['swpsmtp_pass_encrypted'] ) {
  362. update_option( 'swpsmtp_enc_key', $data['swpsmtp_enc_key'] );
  363. }
  364. update_option( 'smtp_test_mail', $data['smtp_test_mail'] );
  365. set_transient( 'easy_wp_smtp_settings_import_success', true, 60 * 60 );
  366. $url = admin_url() . 'options-general.php?page=swpsmtp_settings';
  367. wp_safe_redirect( $url );
  368. exit;
  369. } catch ( Exception $ex ) {
  370. echo esc_html( $err_msg );
  371. wp_die();
  372. }
  373. }
  374. }
  375. }
  376. public function admin_notices() {
  377. if ( ! $this->credentials_configured() ) {
  378. $settings_url = admin_url() . 'options-general.php?page=swpsmtp_settings';
  379. ?>
  380. <div class="error">
  381. <p>
  382. <?php
  383. printf( __( 'Please configure your SMTP credentials in the <a href="%s">settings menu</a> in order to send email using Easy WP SMTP plugin.', 'easy-wp-smtp' ), esc_url( $settings_url ) );
  384. ?>
  385. </p>
  386. </div>
  387. <?php
  388. }
  389. $settings_import_notice = get_transient( 'easy_wp_smtp_settings_import_success' );
  390. if ( $settings_import_notice ) {
  391. delete_transient( 'easy_wp_smtp_settings_import_success' );
  392. ?>
  393. <div class="updated">
  394. <p><?php echo esc_html( __( 'Settings have been imported successfully.', 'easy-wp-smtp' ) ); ?></p>
  395. </div>
  396. <?php
  397. }
  398. }
  399. public function get_log_file_path() {
  400. $log_file_name = 'logs' . DIRECTORY_SEPARATOR . '.' . uniqid( '', true ) . '.txt';
  401. $log_file_name = apply_filters( 'swpsmtp_log_file_path_override', $log_file_name );
  402. return $log_file_name;
  403. }
  404. public function clear_log() {
  405. if ( ! check_ajax_referer( 'easy-wp-smtp-clear-log', 'nonce', false ) ) {
  406. echo esc_html( __( 'Nonce check failed.', 'easy-wp-smtp' ) );
  407. exit;
  408. };
  409. if ( $this->log( self::$reset_log_str, true ) !== false ) {
  410. echo '1';
  411. } else {
  412. echo esc_html( __( "Can't clear log - file is not writeable.", 'easy-wp-smtp' ) );
  413. }
  414. die;
  415. }
  416. public function log( $str, $overwrite = false ) {
  417. try {
  418. $log_file_name = '';
  419. if ( isset( $this->opts['smtp_settings']['log_file_name'] ) ) {
  420. $log_file_name = $this->opts['smtp_settings']['log_file_name'];
  421. }
  422. if ( empty( $log_file_name ) || $overwrite ) {
  423. if ( ! empty( $log_file_name ) && file_exists( plugin_dir_path( __FILE__ ) . $log_file_name ) ) {
  424. unlink( plugin_dir_path( __FILE__ ) . $log_file_name );
  425. }
  426. $log_file_name = $this->get_log_file_path();
  427. $this->opts['smtp_settings']['log_file_name'] = $log_file_name;
  428. update_option( 'swpsmtp_options', $this->opts );
  429. file_put_contents( plugin_dir_path( __FILE__ ) . $log_file_name, self::$reset_log_str ); //phpcs:ignore
  430. }
  431. //Timestamp the log output
  432. $str = '[' . date( 'm/d/Y g:i:s A' ) . '] - ' . $str;
  433. //Write to the log file
  434. return ( file_put_contents( plugin_dir_path( __FILE__ ) . $log_file_name, $str, ( ! $overwrite ? FILE_APPEND : 0 ) ) ); //phpcs:ignore
  435. } catch ( \Exception $e ) {
  436. return false;
  437. }
  438. }
  439. public function plugin_action_links( $links, $file ) {
  440. if ( plugin_basename( $this->plugin_file ) === $file ) {
  441. $settings_link = '<a href="options-general.php?page=swpsmtp_settings">' . __( 'Settings', 'easy-wp-smtp' ) . '</a>';
  442. array_unshift( $links, $settings_link );
  443. }
  444. return $links;
  445. }
  446. public function register_plugin_links( $links, $file ) {
  447. if ( plugin_basename( $this->plugin_file ) === $file ) {
  448. $links[] = '<a href="options-general.php?page=swpsmtp_settings">' . __( 'Settings', 'easy-wp-smtp' ) . '</a>';
  449. }
  450. return $links;
  451. }
  452. public function plugins_loaded_handler() {
  453. load_plugin_textdomain( 'easy-wp-smtp', false, dirname( plugin_basename( $this->plugin_file ) ) . '/languages/' );
  454. }
  455. public function is_domain_blocked() {
  456. //check if Domain Check enabled
  457. if ( isset( $this->opts['enable_domain_check'] ) && $this->opts['enable_domain_check'] ) {
  458. //check if allowed domains list is not blank
  459. if ( isset( $this->opts['allowed_domains'] ) && ! empty( $this->opts['allowed_domains'] ) ) {
  460. $this->opts['allowed_domains'] = EasyWPSMTP_Utils::base64_decode_maybe( $this->opts['allowed_domains'] );
  461. //let's see if we have one domain or coma-separated domains
  462. $domains_arr = explode( ',', $this->opts['allowed_domains'] );
  463. //TODO: Change parse_url() to wp_parse_url() and bump required WP version to 4.4.0
  464. $site_domain = parse_url( get_site_url(), PHP_URL_HOST ); //phpcs:ignore
  465. $match_found = false;
  466. foreach ( $domains_arr as $domain ) {
  467. if ( strtolower( trim( $domain ) ) === strtolower( trim( $site_domain ) ) ) {
  468. $match_found = true;
  469. break;
  470. }
  471. }
  472. if ( ! $match_found ) {
  473. return $site_domain;
  474. }
  475. }
  476. }
  477. return false;
  478. }
  479. public function get_password() {
  480. $temp_password = isset( $this->opts['smtp_settings']['password'] ) ? $this->opts['smtp_settings']['password'] : '';
  481. if ( '' === $temp_password ) {
  482. return '';
  483. }
  484. try {
  485. if ( get_option( 'swpsmtp_pass_encrypted' ) ) {
  486. //this is encrypted password
  487. $cryptor = EasyWPSMTP_Utils::get_instance();
  488. $decrypted = $cryptor->decrypt_password( $temp_password );
  489. //check if encryption option is disabled
  490. if ( empty( $this->opts['smtp_settings']['encrypt_pass'] ) ) {
  491. //it is. let's save decrypted password
  492. $this->opts['smtp_settings']['password'] = $this->encrypt_password( addslashes( $decrypted ) );
  493. update_option( 'swpsmtp_options', $this->opts );
  494. }
  495. return $decrypted;
  496. }
  497. } catch ( Exception $e ) {
  498. $this->log( $e->getMessage() );
  499. return '';
  500. }
  501. $password = '';
  502. $decoded_pass = base64_decode( $temp_password ); //phpcs:ignore
  503. /* no additional checks for servers that aren't configured with mbstring enabled */
  504. if ( ! function_exists( 'mb_detect_encoding' ) ) {
  505. return $decoded_pass;
  506. }
  507. /* end of mbstring check */
  508. if ( base64_encode( $decoded_pass ) === $temp_password ) { //phpcs:ignore
  509. //it might be encoded
  510. if ( false === mb_detect_encoding( $decoded_pass ) ) { //could not find character encoding.
  511. $password = $temp_password;
  512. } else {
  513. $password = base64_decode( $temp_password ); //phpcs:ignore
  514. }
  515. } else { //not encoded
  516. $password = $temp_password;
  517. }
  518. return stripslashes( $password );
  519. }
  520. public function encrypt_password( $pass ) {
  521. if ( '' === $pass ) {
  522. return '';
  523. }
  524. if ( empty( $this->opts['smtp_settings']['encrypt_pass'] ) || ! extension_loaded( 'openssl' ) ) {
  525. // no openssl extension loaded - we can't encrypt the password
  526. $password = base64_encode( $pass ); //phpcs:ignore
  527. update_option( 'swpsmtp_pass_encrypted', false );
  528. } else {
  529. // let's encrypt password
  530. $cryptor = EasyWPSMTP_Utils::get_instance();
  531. $password = $cryptor->encrypt_password( $pass );
  532. update_option( 'swpsmtp_pass_encrypted', true );
  533. }
  534. return $password;
  535. }
  536. public function credentials_configured() {
  537. $credentials_configured = true;
  538. if ( ! isset( $this->opts['from_email_field'] ) || empty( $this->opts['from_email_field'] ) ) {
  539. $credentials_configured = false;
  540. }
  541. if ( ! isset( $this->opts['from_name_field'] ) || empty( $this->opts['from_name_field'] ) ) {
  542. $credentials_configured = false;
  543. }
  544. return $credentials_configured;
  545. }
  546. public function activate() {
  547. $swpsmtp_options_default = array(
  548. 'from_email_field' => '',
  549. 'from_name_field' => '',
  550. 'force_from_name_replace' => 0,
  551. 'sub_mode' => 0,
  552. 'smtp_settings' => array(
  553. 'host' => 'smtp.example.com',
  554. 'type_encryption' => 'none',
  555. 'port' => 25,
  556. 'autentication' => 'yes',
  557. 'username' => '',
  558. 'password' => '',
  559. ),
  560. );
  561. /* install the default plugin options if needed */
  562. if ( empty( $this->opts ) ) {
  563. $this->opts = $swpsmtp_options_default;
  564. }
  565. $this->opts = array_merge( $swpsmtp_options_default, $this->opts );
  566. // reset log file
  567. $this->log( self::$reset_log_str, true );
  568. update_option( 'swpsmtp_options', $this->opts, 'yes' );
  569. //add current domain to allowed domains list
  570. if ( ! isset( $this->opts['allowed_domains'] ) ) {
  571. //TODO: Change parse_url() to wp_parse_url() and bump required WP version to 4.4.0
  572. $domain = parse_url( get_site_url(), PHP_URL_HOST ); //phpcs:ignore
  573. if ( $domain ) {
  574. $this->opts['allowed_domains'] = base64_encode( $domain ); //phpcs:ignore
  575. update_option( 'swpsmtp_options', $this->opts );
  576. }
  577. } else { // let's check if existing value should be base64 encoded
  578. if ( ! empty( $this->opts['allowed_domains'] ) ) {
  579. if ( EasyWPSMTP_Utils::base64_decode_maybe( $this->opts['allowed_domains'] ) === $this->opts['allowed_domains'] ) {
  580. $this->opts['allowed_domains'] = base64_encode( $this->opts['allowed_domains'] ); //phpcs:ignore
  581. update_option( 'swpsmtp_options', $this->opts );
  582. }
  583. }
  584. }
  585. // Encrypt password if needed
  586. if ( ! get_option( 'swpsmtp_pass_encrypted' ) ) {
  587. if ( extension_loaded( 'openssl' ) ) {
  588. if ( '' !== $this->opts['smtp_settings']['password'] ) {
  589. $this->opts['smtp_settings']['password'] = $this->encrypt_password( $this->get_password() );
  590. update_option( 'swpsmtp_options', $this->opts );
  591. }
  592. }
  593. }
  594. }
  595. public function deactivate() {
  596. // reset log file
  597. $this->log( self::$reset_log_str, true );
  598. }
  599. public function self_destruct_handler() {
  600. $err_msg = __( 'Please refresh the page and try again.', 'easy-wp-smtp' );
  601. $trans = get_transient( 'easy_wp_smtp_sd_code' );
  602. if ( empty( $trans ) ) {
  603. echo esc_html( $err_msg );
  604. exit;
  605. }
  606. $sd_code = filter_input( INPUT_POST, 'sd_code', FILTER_SANITIZE_STRING );
  607. if ( $trans !== $sd_code ) {
  608. echo esc_html( $err_msg );
  609. exit;
  610. }
  611. $this->log( self::$reset_log_str, true );
  612. delete_site_option( 'swpsmtp_options' );
  613. delete_option( 'swpsmtp_options' );
  614. delete_site_option( 'smtp_test_mail' );
  615. delete_option( 'smtp_test_mail' );
  616. delete_site_option( 'swpsmtp_pass_encrypted' );
  617. delete_option( 'swpsmtp_pass_encrypted' );
  618. delete_option( 'swpsmtp_enc_key' );
  619. echo 1;
  620. deactivate_plugins( __FILE__ );
  621. exit;
  622. }
  623. }
  624. EasyWPSMTP::get_instance();
  625. function swpsmtp_uninstall() {
  626. // Don't delete plugin options. It is better to retain the options so if someone accidentally deactivates, the configuration is not lost.
  627. //delete_site_option('swpsmtp_options');
  628. //delete_option('swpsmtp_options');
  629. }