説明なし

class-easywpsmtp-utils.php 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. use ioncube\phpOpensslCryptor\Cryptor;
  3. class EasyWPSMTP_Utils {
  4. public $enc_key;
  5. protected static $instance = null;
  6. public function __construct() {
  7. require_once 'inc/Cryptor.php';
  8. $key = get_option( 'swpsmtp_enc_key', false );
  9. if ( empty( $key ) ) {
  10. $key = wp_salt();
  11. update_option( 'swpsmtp_enc_key', $key );
  12. }
  13. $this->enc_key = $key;
  14. }
  15. public static function get_instance() {
  16. // If the single instance hasn't been set, set it now.
  17. if ( null === self::$instance ) {
  18. self::$instance = new self();
  19. }
  20. return self::$instance;
  21. }
  22. public static function base64_decode_maybe( $str ) {
  23. if ( ! function_exists( 'mb_detect_encoding' ) ) {
  24. return base64_decode( $str ); //phpcs:ignore
  25. }
  26. if ( mb_detect_encoding( $str ) === mb_detect_encoding( base64_decode( base64_encode( base64_decode( $str ) ) ) ) ) { //phpcs:ignore
  27. $str = base64_decode( $str ); //phpcs:ignore
  28. }
  29. return $str;
  30. }
  31. public function encrypt_password( $pass ) {
  32. if ( '' === $pass ) {
  33. return '';
  34. }
  35. $password = Cryptor::Encrypt( $pass, $this->enc_key );
  36. return $password;
  37. }
  38. public function decrypt_password( $pass ) {
  39. $password = Cryptor::Decrypt( $pass, $this->enc_key );
  40. return $password;
  41. }
  42. /**
  43. * Sanitizes textarea. Tries to use wp sanitize_textarea_field() function. If that's not available, uses its own methods
  44. * @return string
  45. */
  46. public static function sanitize_textarea( $str ) {
  47. if ( function_exists( 'sanitize_textarea_field' ) ) {
  48. return sanitize_textarea_field( $str );
  49. }
  50. $filtered = wp_check_invalid_utf8( $str );
  51. if ( strpos( $filtered, '<' ) !== false ) {
  52. $filtered = wp_pre_kses_less_than( $filtered );
  53. // This will strip extra whitespace for us.
  54. $filtered = wp_strip_all_tags( $filtered, false );
  55. // Use html entities in a special case to make sure no later
  56. // newline stripping stage could lead to a functional tag
  57. $filtered = str_replace( "<\n", "&lt;\n", $filtered );
  58. }
  59. $filtered = trim( $filtered );
  60. $found = false;
  61. while ( preg_match( '/%[a-f0-9]{2}/i', $filtered, $match ) ) {
  62. $filtered = str_replace( $match[0], '', $filtered );
  63. $found = true;
  64. }
  65. if ( $found ) {
  66. // Strip out the whitespace that may now exist after removing the octets.
  67. $filtered = trim( preg_replace( '/ +/', ' ', $filtered ) );
  68. }
  69. return $filtered;
  70. }
  71. }