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