Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 64 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| CLI | |
0.00% |
0 / 62 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
| mode | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
30 | |||
| setup | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| teardown | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| generate_rules | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * CLI handler for Jetpack Waf. |
| 4 | * |
| 5 | * @package automattic/jetpack-waf |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Waf; |
| 9 | |
| 10 | use WP_CLI; |
| 11 | use WP_CLI_Command; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit( 0 ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Set up the WAF, change its mode, or generate its rules. |
| 19 | */ |
| 20 | class CLI extends WP_CLI_Command { |
| 21 | /** |
| 22 | * View or set the current mode of the WAF. |
| 23 | * ## OPTIONS |
| 24 | * |
| 25 | * [<mode>] |
| 26 | * : The new mode to be set. |
| 27 | * --- |
| 28 | * options: |
| 29 | * - silent |
| 30 | * - normal |
| 31 | * --- |
| 32 | * |
| 33 | * @param array $args Arguments passed to CLI. |
| 34 | * @return void|null |
| 35 | * @throws WP_CLI\ExitException If there is an error switching the mode. |
| 36 | */ |
| 37 | public function mode( $args ) { |
| 38 | if ( count( $args ) > 1 ) { |
| 39 | |
| 40 | return WP_CLI::error( __( 'Only one mode may be specified.', 'jetpack-waf' ) ); |
| 41 | } |
| 42 | if ( count( $args ) === 1 ) { |
| 43 | if ( ! Waf_Runner::is_allowed_mode( $args[0] ) ) { |
| 44 | |
| 45 | return WP_CLI::error( |
| 46 | sprintf( |
| 47 | /* translators: %1$s is the mode that was actually found. Also note that the expected "silent" and "normal" are hard-coded strings and must therefore stay the same in any translation. */ |
| 48 | __( 'Invalid mode: %1$s. Expected "silent" or "normal".', 'jetpack-waf' ), |
| 49 | $args[0] |
| 50 | ) |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | update_option( Waf_Runner::MODE_OPTION_NAME, $args[0] ); |
| 55 | |
| 56 | try { |
| 57 | ( new Waf_Standalone_Bootstrap() )->generate(); |
| 58 | } catch ( \Exception $e ) { |
| 59 | WP_CLI::warning( |
| 60 | sprintf( |
| 61 | /* translators: %1$s is the unexpected error message. */ |
| 62 | __( 'Unable to generate waf bootstrap - standalone mode may not work properly: %1$s', 'jetpack-waf' ), |
| 63 | $e->getMessage() |
| 64 | ) |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | return WP_CLI::success( |
| 69 | sprintf( |
| 70 | /* translators: %1$s is the name of the mode that was just switched to. */ |
| 71 | __( 'Jetpack WAF mode switched to "%1$s".', 'jetpack-waf' ), |
| 72 | get_option( Waf_Runner::MODE_OPTION_NAME ) |
| 73 | ) |
| 74 | ); |
| 75 | } |
| 76 | WP_CLI::line( |
| 77 | sprintf( |
| 78 | /* translators: %1$s is the name of the mode that the waf is currently running in. */ |
| 79 | __( 'Jetpack WAF is running in "%1$s" mode.', 'jetpack-waf' ), |
| 80 | get_option( Waf_Runner::MODE_OPTION_NAME ) |
| 81 | ) |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Setup the WAF to run. |
| 87 | * ## OPTIONS |
| 88 | * |
| 89 | * [<mode>] |
| 90 | * : The new mode to be set. |
| 91 | * --- |
| 92 | * options: |
| 93 | * - silent |
| 94 | * - normal |
| 95 | * --- |
| 96 | * |
| 97 | * @param array $args Arguments passed to CLI. |
| 98 | * @return void|null |
| 99 | * @throws WP_CLI\ExitException If there is an error switching the mode. |
| 100 | */ |
| 101 | public function setup( $args ) { |
| 102 | // Let is_allowed_mode know we are running from the CLI |
| 103 | define( 'WAF_CLI_MODE', $args[0] ); |
| 104 | |
| 105 | // Set the mode and generate the bootstrap |
| 106 | $this->mode( array( $args[0] ) ); |
| 107 | |
| 108 | try { |
| 109 | // Add relevant options and generate the rules.php file |
| 110 | Waf_Runner::activate(); |
| 111 | } catch ( \Exception $e ) { |
| 112 | |
| 113 | return WP_CLI::error( |
| 114 | sprintf( |
| 115 | /* translators: %1$s is the unexpected error message. */ |
| 116 | __( 'Jetpack WAF rules file failed to generate: %1$s', 'jetpack-waf' ), |
| 117 | $e->getMessage() |
| 118 | ) |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | return WP_CLI::success( __( 'Jetpack WAF has successfully been set up.', 'jetpack-waf' ) ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Delete the WAF options. |
| 127 | * |
| 128 | * @return void|null |
| 129 | * @throws WP_CLI\ExitException If deactivating has failures. |
| 130 | */ |
| 131 | public function teardown() { |
| 132 | try { |
| 133 | Waf_Runner::deactivate(); |
| 134 | } catch ( \Exception $e ) { |
| 135 | WP_CLI::error( __( 'Jetpack WAF failed to fully deactivate.', 'jetpack-waf' ) ); |
| 136 | } |
| 137 | |
| 138 | return WP_CLI::success( __( 'Jetpack WAF has been deactivated.', 'jetpack-waf' ) ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Generate the rules.php file with latest rules for the WAF. |
| 143 | * |
| 144 | * @return void|null |
| 145 | * @throws WP_CLI\ExitException If there is an error switching the mode. |
| 146 | */ |
| 147 | public function generate_rules() { |
| 148 | try { |
| 149 | Waf_Constants::define_entrypoint(); |
| 150 | Waf_Rules_Manager::generate_automatic_rules(); |
| 151 | Waf_Rules_Manager::generate_rules(); |
| 152 | } catch ( \Exception $e ) { |
| 153 | |
| 154 | return WP_CLI::error( |
| 155 | sprintf( |
| 156 | /* translators: %1$s is the unexpected error message. */ |
| 157 | __( 'Jetpack WAF rules file failed to generate: %1$s', 'jetpack-waf' ), |
| 158 | $e->getMessage() |
| 159 | ) |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | return WP_CLI::success( |
| 164 | sprintf( |
| 165 | /* translators: %1$s is the name of the mode that was just switched to. */ |
| 166 | __( 'Jetpack WAF rules successfully created to: "%1$s".', 'jetpack-waf' ), |
| 167 | Waf_Runner::get_waf_file_path( JETPACK_WAF_ENTRYPOINT ) |
| 168 | ) |
| 169 | ); |
| 170 | } |
| 171 | } |