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