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