| 1 |
<?php |
| 2 |
/* |
| 3 |
+=====================================================================+ |
| 4 |
| _ _ _ _ ____ | |
| 5 |
| | \ | (_)_ __ (_) __ _/ ___| ___ __ _ _ __ _ __ ___ _ __ | |
| 6 |
| | \| | | '_ \ | |/ _` \___ \ / __/ _` | '_ \| '_ \ / _ \ '__| | |
| 7 |
| | |\ | | | | || | (_| |___) | (_| (_| | | | | | | | __/ | | |
| 8 |
| |_| \_|_|_| |_|/ |\__,_|____/ \___\__,_|_| |_|_| |_|\___|_| | |
| 9 |
| |__/ | |
| 10 |
| | |
| 11 |
| (c) NinTechNet ~ https://nintechnet.com/ | |
| 12 |
+=====================================================================+ |
| 13 |
*/ |
| 14 |
|
| 15 |
if (! defined( 'ABSPATH' ) ) { die( 'Forbidden' ); } |
| 16 |
|
| 17 |
// ===================================================================== // |
| 18 |
// Check if the user enabled WP-CLI integration. |
| 19 |
|
| 20 |
function cli_nscan_is_enabled( $exp = 0 ) { |
| 21 |
|
| 22 |
if ( empty( $exp ) ) { |
| 23 |
$res = nscan_is_valid(); |
| 24 |
if ( $res < 1 ) { |
| 25 |
exit( __('Error: Your must have a valid Premium license to perform this operation.', 'ninjascanner') ."\n" ); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
$nscan_options = get_option( 'nscan_options' ); |
| 30 |
if ( empty( $nscan_options['scan_enable_wpcli'] ) ) { |
| 31 |
exit( __('Warning: NinjaScanner integration with WP-CLI is disabled.', 'ninjascanner') ."\n" ); |
| 32 |
} |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
// ===================================================================== |
| 37 |
// WP-CLI commands: |
| 38 |
|
| 39 |
class nscan_WPCLI extends WP_CLI_Command { |
| 40 |
|
| 41 |
// ------------------------------------------------------------------ |
| 42 |
/* |
| 43 |
* Run a scan. |
| 44 |
* |
| 45 |
*/ |
| 46 |
|
| 47 |
function start() { |
| 48 |
|
| 49 |
cli_nscan_is_enabled(); |
| 50 |
|
| 51 |
// Make sure we don't have a scanning process already running: |
| 52 |
$lock_status = json_decode( nscan_is_scan_running(), true ); |
| 53 |
if ( $lock_status['status'] == 'success' ) { |
| 54 |
$message = __('A scanning process is running. Please wait or stop it.', 'ninjascanner' ); |
| 55 |
WP_CLI::error( $message ); |
| 56 |
exit; |
| 57 |
} |
| 58 |
|
| 59 |
// Generate a temporary scan key: |
| 60 |
$_POST['nscan_key'] = nscan_generate_key(); |
| 61 |
|
| 62 |
$return = array(); |
| 63 |
$_POST['first_run'] = 1; |
| 64 |
$return = json_decode( nscan_ajax_startscan(), true ); |
| 65 |
if ( $return['status'] != 'success' ) { |
| 66 |
$message = __('Cannot start the scan! More details may be available in the scanner log.', 'ninjascanner' ); |
| 67 |
WP_CLI::error( $message ); |
| 68 |
exit; |
| 69 |
|
| 70 |
} else { |
| 71 |
// All done, it's running: |
| 72 |
WP_CLI::success( __('A scanning process has started.', 'ninjascanner') ); |
| 73 |
|
| 74 |
$nscan_options = get_option( 'nscan_options' ); |
| 75 |
if (! empty( $nscan_options['admin_email'] ) ) { |
| 76 |
WP_CLI::log( sprintf( |
| 77 |
__('A report will be sent to %s as soon as the scan has finished.', 'ninjascanner' ), |
| 78 |
$nscan_options['admin_email'] |
| 79 |
)); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// ------------------------------------------------------------------ |
| 85 |
/* |
| 86 |
* Stop a running scan. |
| 87 |
* |
| 88 |
*/ |
| 89 |
|
| 90 |
function stop() { |
| 91 |
|
| 92 |
cli_nscan_is_enabled(); |
| 93 |
|
| 94 |
// Make sure we have a running scan: |
| 95 |
$lock_status = json_decode( nscan_is_scan_running(), true ); |
| 96 |
if ( $lock_status['status'] != 'success' ) { |
| 97 |
$message = __('No scan is running!', 'ninjascanner' ); |
| 98 |
WP_CLI::error( $message ); |
| 99 |
exit; |
| 100 |
} |
| 101 |
|
| 102 |
// Cancel it: |
| 103 |
nscan_log_info( __('Cancelling scanning process', 'ninjascanner'), false ); |
| 104 |
touch( NSCAN_CANCEL ); |
| 105 |
|
| 106 |
if ( file_exists( NSCAN_LOCKFILE ) ) { |
| 107 |
if ( unlink( NSCAN_LOCKFILE ) === false ) { |
| 108 |
// Cannot delete lock file: |
| 109 |
$message = sprintf( |
| 110 |
__('Cannot delete the lock file (%s).', 'ninjascanner' ) .' ' . |
| 111 |
__('Is your filesystem read-only?', 'ninjascanner'), |
| 112 |
NSCAN_LOCKFILE |
| 113 |
); |
| 114 |
WP_CLI::error( $message ); |
| 115 |
|
| 116 |
} else { |
| 117 |
|
| 118 |
// Successfully cancelled: |
| 119 |
WP_CLI::success( __('The scanning process was cancelled.', 'ninjascanner' ) ); |
| 120 |
} |
| 121 |
|
| 122 |
} else { |
| 123 |
// Missing lock file: |
| 124 |
$message = sprintf( |
| 125 |
__('Cannot find the lock file (%s).', 'ninjascanner' ) .' ' . |
| 126 |
__('Is NinjaScanner running?', 'ninjascanner'), |
| 127 |
NSCAN_LOCKFILE |
| 128 |
); |
| 129 |
WP_CLI::error( $message ); |
| 130 |
} |
| 131 |
|
| 132 |
// If there is the scanning process transient set, delete it: |
| 133 |
if ( get_transient( 'nscan_ajax_start' ) !== false ) { |
| 134 |
delete_transient( 'nscan_ajax_start' ); |
| 135 |
} |
| 136 |
|
| 137 |
exit; |
| 138 |
} |
| 139 |
|
| 140 |
// ------------------------------------------------------------------ |
| 141 |
/* |
| 142 |
* Display the scan report. |
| 143 |
* |
| 144 |
*/ |
| 145 |
|
| 146 |
function report() { |
| 147 |
|
| 148 |
cli_nscan_is_enabled(); |
| 149 |
|
| 150 |
// Make sure we don't have a scanning process already running: |
| 151 |
$lock_status = json_decode( nscan_is_scan_running(), true ); |
| 152 |
if ( $lock_status['status'] == 'success' ) { |
| 153 |
$message = __('A scanning process is running. Please wait or stop it.', 'ninjascanner' ); |
| 154 |
WP_CLI::error( $message ); |
| 155 |
exit; |
| 156 |
} |
| 157 |
|
| 158 |
// Populate plain text report: |
| 159 |
require_once __DIR__ . '/report_text.php'; |
| 160 |
$report = text_report(); |
| 161 |
|
| 162 |
// Error? |
| 163 |
if (! empty( $report['error'] ) ) { |
| 164 |
WP_CLI::error( $report['error'] ); |
| 165 |
exit; |
| 166 |
} |
| 167 |
|
| 168 |
WP_CLI::log( $report['body'] ); |
| 169 |
exit; |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
// ------------------------------------------------------------------ |
| 174 |
/* |
| 175 |
* View the debugging log. |
| 176 |
* |
| 177 |
*/ |
| 178 |
|
| 179 |
function log() { |
| 180 |
|
| 181 |
cli_nscan_is_enabled(); |
| 182 |
|
| 183 |
if (! file_exists( NSCAN_DEBUGLOG ) ) { |
| 184 |
WP_CLI::warning( __("NinjaScanner's log is empty.", 'ninjascanner') ); |
| 185 |
exit; |
| 186 |
} |
| 187 |
|
| 188 |
// Get the blog timezone: |
| 189 |
nscan_get_blogtimezone(); |
| 190 |
|
| 191 |
$lines = file( NSCAN_DEBUGLOG, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES ); |
| 192 |
$error = __('Error', 'ninjascanner'); |
| 193 |
$warning = __('Warning', 'ninjascanner'); |
| 194 |
$facility = array( |
| 195 |
1 => '', |
| 196 |
2 => WP_CLI::colorize("%B$warning%n "), |
| 197 |
4 => WP_CLI::colorize("%R$error%n "), |
| 198 |
8 => '' |
| 199 |
); |
| 200 |
WP_CLI::log( sprintf( __('Viewing %s', 'ninjascanner' ), NSCAN_DEBUGLOG ) ); |
| 201 |
foreach( $lines as $line ) { |
| 202 |
list( $date, $level, $string ) = explode( '~~', $line, 3 ); |
| 203 |
$date = date( 'd-M-y H:i:s', $date ); |
| 204 |
WP_CLI::log( "$date ". $facility[$level] ."$string" ) ; |
| 205 |
} |
| 206 |
exit; |
| 207 |
} |
| 208 |
|
| 209 |
// ------------------------------------------------------------------ |
| 210 |
/* |
| 211 |
* Check/save license. |
| 212 |
* |
| 213 |
*/ |
| 214 |
|
| 215 |
function license() { |
| 216 |
|
| 217 |
cli_nscan_is_enabled(1); |
| 218 |
|
| 219 |
_e('Enter your license:', 'ninjascanner' ); |
| 220 |
echo ' '; |
| 221 |
$input = trim( stream_get_line( STDIN, 200, PHP_EOL) ); |
| 222 |
|
| 223 |
if ( is_multisite() ) { |
| 224 |
$site_url = rtrim( strtolower( network_site_url('') ), '/' ); |
| 225 |
} else { |
| 226 |
$site_url = rtrim( strtolower(site_url('') ), '/' ); |
| 227 |
} |
| 228 |
$_SERVER['HTTP_HOST'] = parse_url( $site_url, PHP_URL_HOST); |
| 229 |
|
| 230 |
$nscan_options = get_option( 'nscan_options' ); |
| 231 |
$res = nscan_check_license( $nscan_options, $input ); |
| 232 |
|
| 233 |
if ( empty( $res['nscan_err'] ) ) { |
| 234 |
$nscan_options['key'] = $input; |
| 235 |
$nscan_options['exp'] = $res['nscan_exp']; |
| 236 |
update_option( 'nscan_options', $nscan_options ); |
| 237 |
WP_CLI::success( __('Your license has been accepted and saved.', 'ninjascanner') ); |
| 238 |
} else { |
| 239 |
WP_CLI::error( $res['nscan_err'] ); |
| 240 |
} |
| 241 |
exit; |
| 242 |
} |
| 243 |
|
| 244 |
// ------------------------------------------------------------------ |
| 245 |
/* |
| 246 |
* Display NinjaScanner status. |
| 247 |
* |
| 248 |
*/ |
| 249 |
|
| 250 |
function status() { |
| 251 |
|
| 252 |
cli_nscan_is_enabled(); |
| 253 |
|
| 254 |
$lock_status = json_decode( nscan_is_scan_running(), true ); |
| 255 |
if ( $lock_status['status'] == 'success' ) { |
| 256 |
$message = sprintf( |
| 257 |
__('NinjaScanner is currently running (step %s/%s).', 'ninjascanner'), |
| 258 |
$lock_status['current_step'], |
| 259 |
$lock_status['total_steps'] |
| 260 |
); |
| 261 |
} else { |
| 262 |
$message = __('NinjaScanner is not running.', 'ninjascanner'); |
| 263 |
} |
| 264 |
WP_CLI::log( $message ); |
| 265 |
exit; |
| 266 |
} |
| 267 |
|
| 268 |
// ------------------------------------------------------------------ |
| 269 |
/* |
| 270 |
* Display help screen and quit. |
| 271 |
* |
| 272 |
*/ |
| 273 |
|
| 274 |
function help() { |
| 275 |
|
| 276 |
WP_CLI::log( "\nNinjaScanner v". NSCAN_VERSION . |
| 277 |
" (c)". date('Y') ." NinTechNet ~ https://nintechnet.com/\n\n". |
| 278 |
__('Available commands:', 'ninjascanner') ."\n". |
| 279 |
"\twp ninjascanner help ". __('Display this help screen', 'ninjascanner') ."\n". |
| 280 |
"\twp ninjascanner start ". __('Start a scan', 'ninjascanner') ."\n". |
| 281 |
"\twp ninjascanner stop ". __('Stop the scanning process', 'ninjascanner') ."\n". |
| 282 |
"\twp ninjascanner status ". __('Show scan status', 'ninjascanner') ."\n". |
| 283 |
"\twp ninjascanner report ". __('View the last scan report', 'ninjascanner') ."\n". |
| 284 |
"\twp ninjascanner log ". __('View the debugging log', 'ninjascanner') ."\n". |
| 285 |
"\twp ninjascanner license ". __('Enter your Premium license key', 'ninjascanner') ."\n" ); |
| 286 |
exit; |
| 287 |
} |
| 288 |
// ------------------------------------------------------------------ |
| 289 |
} |
| 290 |
|
| 291 |
WP_CLI::add_command( 'ninjascanner', 'nscan_WPCLI', ['shortdesc' => 'Scan website for malware using NinjaScanner.'] ); |
| 292 |
|
| 293 |
|
| 294 |
// ===================================================================== |
| 295 |
// EOF |
| 296 |
|