admin
1 year ago
lib
2 years ago
captcha.php
2 years ago
cloudsecure-wp.php
1 year ago
common.php
2 years ago
config.php
2 years ago
disable-author-query.php
2 years ago
disable-login.php
2 years ago
disable-restapi.php
2 years ago
disable-xmlrpc.php
2 years ago
htaccess.php
2 years ago
login-log.php
2 years ago
login-notification.php
2 years ago
rename-login-page.php
2 years ago
restrict-admin-page.php
2 years ago
server-error-notification.php
1 year ago
two-factor-authentication.php
2 years ago
unify-messages.php
2 years ago
update-notice.php
2 years ago
login-notification.php
176 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class CloudSecureWP_Login_Notification extends CloudSecureWP_Common { |
| 8 | private const KEY_FEATURE = 'login_notification'; |
| 9 | private const KEY_SUBJECT = self::KEY_FEATURE . '_subject'; |
| 10 | private const KEY_BODY = self::KEY_FEATURE . '_body'; |
| 11 | private const KEY_ADMIN_ONLY = self::KEY_FEATURE . '_admin_only'; |
| 12 | private $config; |
| 13 | |
| 14 | function __construct( array $info, CloudSecureWP_Config $config ) { |
| 15 | parent::__construct( $info ); |
| 16 | $this->config = $config; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * 機能毎のKEY取得 |
| 21 | * |
| 22 | * @return string |
| 23 | */ |
| 24 | public function get_feature_key(): string { |
| 25 | return self::KEY_FEATURE; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * 有効無効判定 |
| 30 | * |
| 31 | * @return bool |
| 32 | */ |
| 33 | public function is_enabled(): bool { |
| 34 | return $this->config->get( $this->get_feature_key() ) === 't' ? true : false; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * 初期設定値取得 |
| 39 | * |
| 40 | * @return array |
| 41 | */ |
| 42 | public function get_default(): array { |
| 43 | $ret = array( |
| 44 | self::KEY_FEATURE => $this->check_environment() ? 't' : 'f', |
| 45 | self::KEY_SUBJECT => $this->get_default_subject(), |
| 46 | self::KEY_BODY => $this->get_default_body(), |
| 47 | self::KEY_ADMIN_ONLY => 't', |
| 48 | ); |
| 49 | return $ret; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * 設定値取得 |
| 54 | */ |
| 55 | public function get_settings(): array { |
| 56 | $settings = array(); |
| 57 | $default = $this->get_default(); |
| 58 | |
| 59 | foreach ( $default as $key => $val ) { |
| 60 | $settings[ $key ] = $this->config->get( $key ); |
| 61 | } |
| 62 | |
| 63 | return $settings; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * 設定値保存 |
| 68 | * |
| 69 | * @param array $settings |
| 70 | * @return void |
| 71 | */ |
| 72 | public function save_settings( $settings ): void { |
| 73 | $default = $this->get_default(); |
| 74 | |
| 75 | foreach ( $default as $key => $val ) { |
| 76 | $this->config->set( $key, $settings[ $key ] ?? '' ); |
| 77 | } |
| 78 | |
| 79 | $this->config->save(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * メール件名デフォルト値取得 |
| 84 | * |
| 85 | * @return string |
| 86 | */ |
| 87 | private function get_default_subject(): string { |
| 88 | return 'ログイン通知'; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * メール本文デフォルト値取得 |
| 93 | * |
| 94 | * @return string |
| 95 | */ |
| 96 | private function get_default_body(): string { |
| 97 | |
| 98 | $text = "{\$date} {\$time} に {\$user_name} がログインしました。" . "\n"; |
| 99 | $text .= "" . "\n"; |
| 100 | $text .= "【ログイン� |
| 101 | 報】" . "\n"; |
| 102 | $text .= "" . "\n"; |
| 103 | $text .= "・日時:{\$date} {\$time}" . "\n"; |
| 104 | $text .= "" . "\n"; |
| 105 | $text .= "・ユーザー名:{\$user_name}" . "\n"; |
| 106 | $text .= "" . "\n"; |
| 107 | $text .= "・IPアドレス:{\$ip}" . "\n"; |
| 108 | $text .= "" . "\n"; |
| 109 | $text .= "・リファラー:{\$http_referer}" . "\n"; |
| 110 | $text .= "" . "\n"; |
| 111 | $text .= "・ユーザーエージェント:{\$http_user_agent}" . "\n"; |
| 112 | $text .= "" . "\n"; |
| 113 | $text .= "--" . "\n"; |
| 114 | $text .= "CloudSecure WP Security" . "\n"; |
| 115 | |
| 116 | return $text; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * 値割当て |
| 121 | * |
| 122 | * @param string $text |
| 123 | * @param array $values |
| 124 | * @return string |
| 125 | */ |
| 126 | private function bind_values( string $text, array $values ): string { |
| 127 | foreach ( $values as $key => $val ) { |
| 128 | $text = str_replace( '{$' . $key . '}', $val, $text ); |
| 129 | } |
| 130 | return $text; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * 通知 |
| 135 | */ |
| 136 | public function notification( $user_name, $user ) { |
| 137 | if ( 't' === $this->config->get( self::KEY_ADMIN_ONLY ) && ! $user->has_cap( 'administrator' ) ) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | $values = array( |
| 142 | 'user_name' => $user_name, |
| 143 | 'date' => $this->get_date(), |
| 144 | 'time' => $this->get_time(), |
| 145 | 'ip' => '' !== $this->get_client_ip() ? $this->get_client_ip() : '--', |
| 146 | 'http_referer' => '' !== $this->get_http_referer() ? $this->get_http_referer() : '--', |
| 147 | 'http_user_agent' => '' !== $this->get_http_user_agent() ? $this->get_http_user_agent() : '--', |
| 148 | ); |
| 149 | |
| 150 | $mail = $user->get( 'user_email' ); |
| 151 | $subject = $this->bind_values( $this->config->get( self::KEY_SUBJECT ), $values ); |
| 152 | $body = $this->bind_values( $this->config->get( self::KEY_BODY ), $values ); |
| 153 | |
| 154 | $this->wp_send_mail( $mail, esc_html( $subject ), esc_html( $body ) ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * 有効化 |
| 159 | * |
| 160 | * @return void |
| 161 | */ |
| 162 | public function activate(): void { |
| 163 | $this->save_settings( $this->get_default() ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * 無効化 |
| 168 | * |
| 169 | * @return void |
| 170 | */ |
| 171 | public function deactivate(): void { |
| 172 | $this->config->set( $this->get_feature_key(), 'f' ); |
| 173 | $this->config->save(); |
| 174 | } |
| 175 | } |
| 176 |