admin
2 months ago
cli
2 months ago
lib
3 months ago
captcha.php
3 months ago
cloudsecure-wp.php
2 months ago
common.php
2 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
2 months ago
disable-restapi.php
2 months ago
disable-xmlrpc.php
1 year ago
htaccess.php
3 months ago
login-log.php
2 months ago
login-notification.php
1 year ago
rename-login-page.php
3 months ago
restrict-admin-page.php
3 months ago
server-error-notification.php
2 months ago
two-factor-authentication.php
2 months ago
unify-messages.php
2 years ago
update-notice.php
7 months ago
waf-engine.php
3 months ago
waf.php
2 months ago
disable-restapi.php
187 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 | if ( empty( $plugin ) ) { |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | // snow-monkey対応 |
| 151 | if ( $plugin === 'snow-monkey-forms' ) { |
| 152 | $plugin = 'snow-monkey-form'; |
| 153 | } |
| 154 | |
| 155 | // woocommerce対応 |
| 156 | if ( $plugin === 'woocommerce' ) { |
| 157 | $plugin = 'wc'; |
| 158 | } |
| 159 | |
| 160 | if ( strpos( $route, "/$plugin/" ) === 0 ) { |
| 161 | return $result; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return new WP_Error( $this->get_feature_key(), 'REST API が無効化されています', array( 'status' => rest_authorization_required_code() ) ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * 有効化 |
| 170 | * |
| 171 | * @return void |
| 172 | */ |
| 173 | public function activate(): void { |
| 174 | $this->save_settings( $this->get_default() ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * 無効化 |
| 179 | * |
| 180 | * @return void |
| 181 | */ |
| 182 | public function deactivate(): void { |
| 183 | $this->config->set( $this->get_feature_key(), 'f' ); |
| 184 | $this->config->save(); |
| 185 | } |
| 186 | } |
| 187 |