admin
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
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 |