PluginProbe ʕ •ᴥ•ʔ
CloudSecure WP Security / 1.4.5
CloudSecure WP Security v1.4.5
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 4 months ago cli 8 months ago lib 4 months ago captcha.php 3 months ago cloudsecure-wp.php 3 months ago common.php 4 months ago config.php 2 years ago disable-access-system-file.php 4 months ago disable-author-query.php 2 years ago disable-login.php 1 year ago disable-restapi.php 7 months ago disable-xmlrpc.php 1 year ago htaccess.php 5 months ago login-log.php 4 months ago login-notification.php 1 year ago rename-login-page.php 1 year ago restrict-admin-page.php 10 months ago server-error-notification.php 1 year ago two-factor-authentication.php 4 months ago unify-messages.php 2 years ago update-notice.php 7 months ago waf-engine.php 4 months ago waf.php 4 months ago
disable-restapi.php
183 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 // プラグイン名取得
92 $plugin_name = $plugin['TextDomain'];
93 if ( $plugin['Name'] === 'Hello Dolly' ) {
94 // Hello Dolly対応
95 $plugin_name = 'hello-dolly';
96 }
97
98 if ( is_plugin_active( $plugin_path ) ) {
99 if ( false === in_array( $plugin_name, $exclude_plugins, true ) && $plugin_name !== $this->info['text_domain'] ) {
100 $plugin_names[] = $plugin_name;
101 }
102 }
103 }
104 }
105
106 return $plugin_names;
107 }
108
109 /**
110 * テキストから�
111 �列に変換
112 *
113 * @param string $text
114 * @return array
115 */
116 public function text2Array( string $text ): array {
117 $searchs = array( "\r\n", "\r" );
118 $text = str_replace( $searchs, "\n", $text );
119 $text_array = explode( "\n", $text );
120
121 foreach ( $text_array as &$name ) {
122 $path = trim( $name );
123 }
124 unset( $path );
125
126 $text_array = array_filter( $text_array, 'strlen' );
127 $text_array = array_unique( $text_array );
128
129 return $text_array;
130 }
131
132 /**
133 * rest_pre_dispatch
134 */
135 function rest_pre_dispatch( $result, $server, $request ) {
136
137 if ( current_user_can( 'edit_pages' ) || current_user_can( 'edit_posts' ) ) {
138 return $result;
139 }
140
141 $setting = $this->get_settings();
142 $exclude_plugins = $setting[ self::KEY_EXCLUDE ];
143 $route = $request->get_route();
144
145 foreach ( $exclude_plugins as $plugin ) {
146 // snow-monkey対応
147 if ( $plugin === 'snow-monkey-forms' ) {
148 $plugin = 'snow-monkey-form';
149 }
150
151 // woocommerce対応
152 if ( $plugin === 'woocommerce' ) {
153 $plugin = 'wc';
154 }
155
156 if ( false !== strpos( $route, "/$plugin/" ) ) {
157 return $result;
158 }
159 }
160
161 return new WP_Error( $this->get_feature_key(), 'REST API が無効化されています', array( 'status' => rest_authorization_required_code() ) );
162 }
163
164 /**
165 * 有効化
166 *
167 * @return void
168 */
169 public function activate(): void {
170 $this->save_settings( $this->get_default() );
171 }
172
173 /**
174 * 無効化
175 *
176 * @return void
177 */
178 public function deactivate(): void {
179 $this->config->set( $this->get_feature_key(), 'f' );
180 $this->config->save();
181 }
182 }
183