PluginProbe ʕ •ᴥ•ʔ
CloudSecure WP Security / 1.1.1
CloudSecure WP Security v1.1.1
1.4.10 1.4.9 trunk 0.9.0 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.19 1.3.2 1.3.20 1.3.21 1.3.22 1.3.23 1.3.24 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8
cloudsecure-wp-security / modules / server-error-notification.php
cloudsecure-wp-security / modules Last commit date
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
server-error-notification.php
233 lines
1 <?php
2
3 class CloudSecureWP_Server_Error_Notification extends CloudSecureWP_Common {
4 private const KEY_FEATURE = 'server_error_notification';
5 private const TABLE_NAME = 'cloudsecurewp_server_error';
6 private const MAX_ITEMS = 10000;
7
8 private $config;
9
10 function __construct( array $info, CloudSecureWP_Config $config ) {
11 parent::__construct( $info );
12 $this->config = $config;
13 }
14
15 /**
16 * 機能毎のKEY取得
17 *
18 * @return string
19 */
20 public function get_feature_key(): string {
21 return self::KEY_FEATURE;
22 }
23
24 /**
25 * テーブル名取得
26 *
27 * @return string
28 */
29 public function get_table_name(): string {
30 global $wpdb;
31
32 return $wpdb->prefix . self::TABLE_NAME;
33 }
34
35 /**
36 * サーバーエラーを追加
37 *
38 * @param array $error
39 *
40 * @return void
41 */
42 private function insert_error( array $error ): void {
43 global $wpdb;
44 $table_name = $wpdb->prefix . self::TABLE_NAME;
45 $max_items = self::MAX_ITEMS;
46 $error['created_at'] = current_time( 'mysql' );
47 $wpdb->query( 'START TRANSACTION' );
48 $wpdb->insert( $table_name, $error );
49 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}cloudsecurewp_server_error WHERE id <= (SELECT id FROM (SELECT id FROM {$wpdb->prefix}cloudsecurewp_server_error ORDER BY id DESC LIMIT 1 OFFSET %d) tmp)", $max_items ) );
50 $wpdb->query( 'COMMIT' );
51 }
52
53 /**
54 * サーバーエラーを取得
55 *
56 * @param string $orderby
57 * @param string $order
58 * @param int $per_page
59 * @param int $offset
60 *
61 * @return array
62 */
63 public function get_server_errors( string $orderby, string $order, int $per_page, int $offset ): array {
64 global $wpdb;
65 $table_name = $wpdb->prefix . self::TABLE_NAME;
66 $sanitized_orderby = sanitize_sql_orderby( "$orderby $order" );
67 $sql = "SELECT * FROM $table_name ORDER BY $sanitized_orderby LIMIT %d OFFSET %d";
68
69 return array(
70 $wpdb->get_results( $wpdb->prepare( $sql, $per_page, $offset ), ARRAY_A ),
71 $wpdb->get_var( "SELECT count(*) FROM {$wpdb->prefix}cloudsecurewp_server_error" ),
72 );
73 }
74
75 /**
76 * 有効無効判定
77 *
78 * @return bool
79 */
80 public function is_enabled(): bool {
81 return $this->config->get( $this->get_feature_key() ) === 't';
82 }
83
84 /**
85 * 初期設定値取得
86 *
87 * @return array
88 */
89 public function get_default(): array {
90 return array( self::KEY_FEATURE => $this->check_environment() ? 't' : 'f' );
91 }
92
93 /**
94 * 設定値取得
95 */
96 public function get_settings(): array {
97 $settings = array();
98 $default = $this->get_default();
99
100 foreach ( $default as $key => $val ) {
101 $settings[ $key ] = $this->config->get( $key );
102 }
103
104 return $settings;
105 }
106
107 /**
108 * 設定値保存
109 *
110 * @param array $settings
111 *
112 * @return void
113 */
114 public function save_settings( array $settings ): void {
115 $default = $this->get_default();
116
117 foreach ( $default as $key => $val ) {
118 $this->config->set( $key, $settings[ $key ] ?? '' );
119 }
120
121 $this->config->save();
122 }
123
124 /**
125 * エラータイプ名称を取得
126 *
127 * @param int $error_type
128 *
129 * @return string
130 */
131 private function get_error_type_name( int $error_type ): string {
132 // 下記のコードを参考にさせていただきました。
133 // https://github.com/WordPress/WordPress/blob/aac1b7c487c9a5fd206f6845e4eba4164b02f02b/wp-includes/error-protection.php#L48
134 $constants = get_defined_constants( true );
135 $constants = $constants['Core'] ?? $constants['internal'];
136
137 foreach ( $constants as $constant => $value ) {
138 if ( str_starts_with( $constant, 'E_' ) && $error_type === $value ) {
139 return $constant;
140 }
141 }
142
143 return '';
144 }
145
146 /**
147 * メール本文取得
148 *
149 * @param array{ type: int , message: string, file: string, line: int } $error
150 *
151 * @return string
152 */
153 private function get_body( array $error ): string {
154 $date = $this->get_date();
155 $time = $this->get_time();
156
157 $text = "$date $time にサーバーエラーが発生しました。" . PHP_EOL;
158 $text .= PHP_EOL;
159 $text .= '【サーバーエラー�
160 報】' . PHP_EOL;
161 $text .= PHP_EOL;
162 $text .= "・エラータイプ:{$error['type']}" . PHP_EOL;
163 $text .= PHP_EOL;
164 $text .= "・エラーメッセージ:{$error['message']}" . PHP_EOL;
165 $text .= PHP_EOL;
166 $text .= "・エラー箇所:{$error['file']}:{$error['line']}" . PHP_EOL;
167 $text .= PHP_EOL;
168 $text .= '--' . PHP_EOL;
169 $text .= 'CloudSecure WP Security' . PHP_EOL;
170
171 return $text;
172 }
173
174 /**
175 * 通知
176 */
177 public function notification( $args, $error ) {
178
179 $error['type'] = $this->get_error_type_name( $error['type'] );
180
181 $this->insert_error( $error );
182
183 $enable_email_server_error_notification = get_option( 'cloudsecurewp_enable_email_server_error_notification', 't' );
184 if ( 't' === $enable_email_server_error_notification ) {
185 $option_name = "cloudsecurewp_error_type_{$error['type']}_email_last_sent";
186 $last_sent = get_option( $option_name );
187 if ( ! $last_sent || time() > $last_sent + HOUR_IN_SECONDS ) {
188 update_option( $option_name, time() );
189 $to = get_option( 'admin_email' );
190 $this->wp_send_mail( $to, esc_html( 'サーバーエラー通知' ), $this->get_body( $error ) );
191 }
192 }
193
194 return $args;
195 }
196
197 /**
198 * 有効化
199 *
200 * @return void
201 */
202 public function activate(): void {
203 $this->save_settings( $this->get_default() );
204
205 global $wpdb;
206 $charset_collate = $wpdb->get_charset_collate();
207 $table_name = $this->get_table_name();
208
209 $sql = "CREATE TABLE {$table_name} ( ";
210 $sql .= 'id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT, ';
211 $sql .= 'type VARCHAR( 31 ) NOT NULL DEFAULT 0, ';
212 $sql .= "message VARCHAR( 65535 ) NOT NULL DEFAULT '', ";
213 $sql .= "file VARCHAR( 65535 ) NOT NULL DEFAULT '', ";
214 $sql .= 'line INT NOT NULL DEFAULT 0, ';
215 $sql .= 'created_at DATETIME, ';
216 $sql .= 'UNIQUE KEY id ( id ) ';
217 $sql .= " ) {$charset_collate};";
218
219 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
220 dbDelta( $sql );
221 }
222
223 /**
224 * 無効化
225 *
226 * @return void
227 */
228 public function deactivate(): void {
229 $this->config->set( $this->get_feature_key(), 'f' );
230 $this->config->save();
231 }
232 }
233