PluginProbe ʕ •ᴥ•ʔ
CloudSecure WP Security / 1.3.8
CloudSecure WP Security v1.3.8
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 / disable-restapi.php
cloudsecure-wp-security / modules Last commit date
admin 1 year ago lib 1 year ago captcha.php 2 years ago cloudsecure-wp.php 1 year ago common.php 1 year ago config.php 2 years ago disable-access-system-file.php 1 year ago disable-author-query.php 2 years ago disable-login.php 1 year ago disable-restapi.php 2 years ago disable-xmlrpc.php 1 year ago htaccess.php 1 year ago login-log.php 1 year ago login-notification.php 1 year ago rename-login-page.php 1 year ago restrict-admin-page.php 1 year ago server-error-notification.php 1 year ago two-factor-authentication.php 1 year ago unify-messages.php 2 years ago update-notice.php 2 years ago waf-engine.php 1 year ago waf.php 1 year ago
disable-restapi.php
166 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class CloudSecureWP_Disable_RESTAPI extends CloudSecureWP_Common {
8 private const KEY_FEATURE = 'disable_rest_api';
9 private const KEY_EXCLUDE = self::KEY_FEATURE . '_exclude';
10 private const DEFAULT_EXCLUDE = array( 'oembed', 'contact-form-7', 'akismet' );
11 private $config;
12
13 function __construct( array $info, CloudSecureWP_Config $config ) {
14 parent::__construct( $info );
15 $this->config = $config;
16 }
17
18 /**
19 * 機能毎のKEY取得
20 *
21 * @return string
22 */
23 public function get_feature_key(): string {
24 return self::KEY_FEATURE;
25 }
26
27 /**
28 * 有効無効判定
29 *
30 * @return bool
31 */
32 public function is_enabled(): bool {
33 return $this->config->get( $this->get_feature_key() ) === 't' ? true : false;
34 }
35
36 /**
37 * 初期設定値取得
38 *
39 * @return array
40 */
41 public function get_default(): array {
42 $ret = array(
43 self::KEY_FEATURE => 'f',
44 self::KEY_EXCLUDE => self::DEFAULT_EXCLUDE,
45 );
46 return $ret;
47 }
48
49 /**
50 * 設定値取得
51 */
52 public function get_settings(): array {
53 $settings = array();
54 $default = $this->get_default();
55
56 foreach ( $default as $key => $val ) {
57 $settings[ $key ] = $this->config->get( $key );
58 }
59
60 return $settings;
61 }
62
63 /**
64 * 設定値保存
65 *
66 * @param array $settings
67 * @return void
68 */
69 public function save_settings( $settings ): void {
70 $default = $this->get_default();
71
72 foreach ( $default as $key => $val ) {
73 $this->config->set( $key, $settings[ $key ] ?? '' );
74 }
75
76 $this->config->save();
77 }
78
79 /**
80 * 除外指定していない有効なプラグイン名リスト取得
81 *
82 * @return array
83 */
84 public function get_active_plugin_names(): array {
85 $plugin_names = array();
86 $plugins = get_plugins();
87 $exclude_plugins = $this->config->get( self::KEY_EXCLUDE );
88
89 if ( ! empty( $plugins ) ) {
90 foreach ( $plugins as $plugin_path => $plugin ) {
91 if ( is_plugin_active( $plugin_path ) ) {
92 if ( false === in_array( $plugin['TextDomain'], $exclude_plugins ) && $plugin['TextDomain'] !== $this->info['text_domain'] ) {
93 $plugin_names[] = $plugin['TextDomain'];
94 }
95 }
96 }
97 }
98
99 return $plugin_names;
100 }
101
102 /**
103 * テキストから�
104 �列に変換
105 *
106 * @param string $text
107 * @return array
108 */
109 public function text2Array( string $text ): array {
110 $searchs = array( "\r\n", "\r" );
111 $text = str_replace( $searchs, "\n", $text );
112 $text_array = explode( "\n", $text );
113
114 foreach ( $text_array as &$name ) {
115 $path = trim( $name );
116 }
117 unset( $path );
118
119 $text_array = array_filter( $text_array, 'strlen' );
120 $text_array = array_unique( $text_array );
121
122 return $text_array;
123 }
124
125 /**
126 * rest_pre_dispatch
127 */
128 function rest_pre_dispatch( $result, $server, $request ) {
129
130 if ( current_user_can( 'edit_pages' ) || current_user_can( 'edit_posts' ) ) {
131 return $result;
132 }
133
134 $setting = $this->get_settings();
135 $exclude_plugins = $setting[ self::KEY_EXCLUDE ];
136 $route = $request->get_route();
137
138 foreach ( $exclude_plugins as $plugin ) {
139 if ( false !== strpos( $route, "/$plugin/" ) ) {
140 return $result;
141 }
142 }
143
144 return new WP_Error( $this->get_feature_key(), 'REST API が無効化されています', array( 'status' => rest_authorization_required_code() ) );
145 }
146
147 /**
148 * 有効化
149 *
150 * @return void
151 */
152 public function activate(): void {
153 $this->save_settings( $this->get_default() );
154 }
155
156 /**
157 * 無効化
158 *
159 * @return void
160 */
161 public function deactivate(): void {
162 $this->config->set( $this->get_feature_key(), 'f' );
163 $this->config->save();
164 }
165 }
166