|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WP_CLI\Bootstrap; |
| 4 | + |
| 5 | +use WP_CLI; |
| 6 | +use WP_CLI\Utils; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class CheckRoot. |
| 10 | + * |
| 11 | + * Check if the user is running as root and aborts with a warning if they are. |
| 12 | + * |
| 13 | + * @package WP_CLI\Bootstrap |
| 14 | + */ |
| 15 | +class CheckRoot implements BootstrapStep { |
| 16 | + |
| 17 | + /** |
| 18 | + * Process this single bootstrapping step. |
| 19 | + * |
| 20 | + * @param BootstrapState $state Contextual state to pass into the step. |
| 21 | + * |
| 22 | + * @return BootstrapState Modified state to pass to the next step. |
| 23 | + */ |
| 24 | + public function process( BootstrapState $state ) { |
| 25 | + $config = $state->getValue( 'config', [] ); |
| 26 | + if ( array_key_exists( 'allow-root', $config ) && true === $config['allow-root'] ) { |
| 27 | + // They're aware of the risks and set a flag to allow root. |
| 28 | + return $state; |
| 29 | + } |
| 30 | + |
| 31 | + if ( getenv( 'WP_CLI_ALLOW_ROOT' ) ) { |
| 32 | + // They're aware of the risks and set an environment variable to allow root. |
| 33 | + return $state; |
| 34 | + } |
| 35 | + |
| 36 | + $args = $state->getValue( 'arguments', [] ); |
| 37 | + if ( count( $args ) >= 2 && 'cli' === $args[0] && in_array( $args[1], [ 'update', 'info' ], true ) ) { |
| 38 | + // Make it easier to update root-owned copies. |
| 39 | + return $state; |
| 40 | + } |
| 41 | + |
| 42 | + if ( ! function_exists( 'posix_geteuid' ) ) { |
| 43 | + // POSIX functions not available. |
| 44 | + return $state; |
| 45 | + } |
| 46 | + |
| 47 | + if ( posix_geteuid() !== 0 ) { |
| 48 | + // Not root. |
| 49 | + return $state; |
| 50 | + } |
| 51 | + |
| 52 | + WP_CLI::error( |
| 53 | + "YIKES! It looks like you're running this as root. You probably meant to " . |
| 54 | + "run this as the user that your WordPress installation exists under.\n" . |
| 55 | + "\n" . |
| 56 | + "If you REALLY mean to run this as root, we won't stop you, but just " . |
| 57 | + 'bear in mind that any code on this site will then have full control of ' . |
| 58 | + "your server, making it quite DANGEROUS.\n" . |
| 59 | + "\n" . |
| 60 | + "If you'd like to continue as root, please run this again, adding this " . |
| 61 | + "flag: --allow-root\n" . |
| 62 | + "\n" . |
| 63 | + "If you'd like to run it as the user that this site is under, you can " . |
| 64 | + "run the following to become the respective user:\n" . |
| 65 | + "\n" . |
| 66 | + " sudo -u USER -i -- wp <command>\n" . |
| 67 | + "\n" |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
0 commit comments