diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 2a77175..08cbe4b 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -8,7 +8,8 @@ "vscode": { "extensions": [ "recca0120.vscode-phpunit", - "github.vscode-github-actions" + "github.vscode-github-actions", + "slevesque.vscode-zipexplorer" ] } }, diff --git a/Infrastructure/Database/FindingsQuery.php b/Infrastructure/Database/FindingsQuery.php index c0bf084..1959ee7 100644 --- a/Infrastructure/Database/FindingsQuery.php +++ b/Infrastructure/Database/FindingsQuery.php @@ -6,23 +6,24 @@ use wpdb; class FindingsQuery implements IFindingsQuery { - private wpdb $wpdb; private LoggerInterface $logger; public function __construct( LoggerInterface $logger, ) { - global $wpdb; - $this->wpdb = $wpdb; $this->logger = $logger; } private function get_table_name(): string { - return $this->wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FINDINGS_TABLE_NAME; + global $wpdb; + + return $wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FINDINGS_TABLE_NAME; } public function create(): void { - $charset_collate = $this->wpdb->get_charset_collate(); + global $wpdb; + + $charset_collate = $wpdb->get_charset_collate(); $sql = 'CREATE TABLE ' . $this->get_table_name() . ' ( file_path VARCHAR(512) NOT NULL, UNIQUE KEY file_path (file_path) @@ -34,21 +35,25 @@ public function create(): void { } public function remove(): void { + global $wpdb; + if (! $this->table_exists()) { return; } - $this->wpdb->query( - $this->wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name()) + $wpdb->query( + $wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name()) ); \wp_cache_set($this->get_table_name(), 'false', 'GdataAntivirus'); } public function table_exists(): bool { + global $wpdb; + $tables_exists = \wp_cache_get($this->get_table_name(), 'GdataAntivirus'); $this->logger->debug('Exists in cache: ' . ($tables_exists ? 'true' : 'false')); if (false === $tables_exists) { - $exists = $this->wpdb->get_var( - $this->wpdb->prepare('SHOW TABLES LIKE %s', $this->get_table_name()) + $exists = $wpdb->get_var( + $wpdb->prepare('SHOW TABLES LIKE %s', $this->get_table_name()) ) === $this->get_table_name(); $this->logger->debug('Exists in database: ' . ($exists ? 'true' : 'false')); \wp_cache_set($this->get_table_name(), \wp_json_encode($exists), 'GdataAntivirus'); @@ -61,12 +66,14 @@ public function table_exists(): bool { } public function add( string $file ): void { + global $wpdb; + if (! $this->table_exists()) { return; } try { - $this->wpdb->insert( + $wpdb->insert( $this->get_table_name(), array( 'file_path' => $file ) ); @@ -76,32 +83,38 @@ public function add( string $file ): void { } public function delete( string $file ): void { + global $wpdb; + if (! $this->table_exists()) { return; } - $this->wpdb->delete( + $wpdb->delete( $this->get_table_name(), array( 'file_path' => $file ) ); } public function get_all(): array { + global $wpdb; + if (! $this->table_exists()) { return array(); } - return $this->wpdb->get_results( - $this->wpdb->prepare('SELECT file_path FROM %i', $this->get_table_name()), + return $wpdb->get_results( + $wpdb->prepare('SELECT file_path FROM %i', $this->get_table_name()), ARRAY_A ); } public function count(): int { + global $wpdb; + $this->logger->debug('FindingsMenuPage::get_findings_count'); if (! $this->table_exists()) { return 0; } - return (int) $this->wpdb->get_var( - $this->wpdb->prepare('SELECT COUNT(*) FROM %i', $this->get_table_name()) + return (int) $wpdb->get_var( + $wpdb->prepare('SELECT COUNT(*) FROM %i', $this->get_table_name()) ); } diff --git a/Infrastructure/Database/ScansQuery.php b/Infrastructure/Database/ScansQuery.php index db96cc8..29efbe9 100644 --- a/Infrastructure/Database/ScansQuery.php +++ b/Infrastructure/Database/ScansQuery.php @@ -2,27 +2,20 @@ namespace Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database; -use Psr\Log\LoggerInterface; -use wpdb; - class ScansQuery implements IScansQuery { - private wpdb $wpdb; - private LoggerInterface $logger; - - public function __construct( - LoggerInterface $logger, - ) { - global $wpdb; - $this->wpdb = $wpdb; - $this->logger = $logger; + public function __construct() { } private function get_table_name(): string { - return $this->wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_OPERATIONS_TABLE_NAME; + global $wpdb; + + return $wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_OPERATIONS_TABLE_NAME; } public function create(): void { - $charset_collate = $this->wpdb->get_charset_collate(); + global $wpdb; + + $charset_collate = $wpdb->get_charset_collate(); $sql = 'CREATE TABLE ' . $this->get_table_name() . ' ( scheduled_scans TINYINT NOT NULL DEFAULT 0, finished_scans TINYINT NOT NULL DEFAULT 0 @@ -31,56 +24,72 @@ public function create(): void { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta($sql); - $this->wpdb->query( - $this->wpdb->prepare('INSERT INTO %i (scheduled_scans, finished_scans) VALUES (0, 0)', $this->get_table_name()) + $wpdb->query( + $wpdb->prepare('INSERT INTO %i (scheduled_scans, finished_scans) VALUES (0, 0)', $this->get_table_name()) ); } public function remove(): void { - $this->wpdb->query( - $this->wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name()) + global $wpdb; + + $wpdb->query( + $wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name()) ); } public function write_lock(): void { - $this->wpdb->query( - $this->wpdb->prepare('LOCK TABLES %i WRITE', $this->get_table_name()) + global $wpdb; + + $wpdb->query( + $wpdb->prepare('LOCK TABLES %i WRITE', $this->get_table_name()) ); } public function write_unlock(): void { - $this->wpdb->query( - $this->wpdb->prepare('UNLOCK TABLES %i WRITE', $this->get_table_name()) + global $wpdb; + + $wpdb->query( + $wpdb->prepare('UNLOCK TABLES %i WRITE', $this->get_table_name()) ); } public function scheduled_count(): int { - return $this->wpdb->get_var( - $this->wpdb->prepare('SELECT scheduled_scans FROM %i', $this->get_table_name()) + global $wpdb; + + return $wpdb->get_var( + $wpdb->prepare('SELECT scheduled_scans FROM %i', $this->get_table_name()) ); } public function increase_scheduled(): void { - $this->wpdb->query( - $this->wpdb->prepare('UPDATE %i SET scheduled_scans = scheduled_scans + 1', $this->get_table_name()) + global $wpdb; + + $wpdb->query( + $wpdb->prepare('UPDATE %i SET scheduled_scans = scheduled_scans + 1', $this->get_table_name()) ); } public function finished_count(): int { - return $this->wpdb->get_var( - $this->wpdb->prepare('SELECT finished_scans FROM %i', $this->get_table_name()) + global $wpdb; + + return $wpdb->get_var( + $wpdb->prepare('SELECT finished_scans FROM %i', $this->get_table_name()) ); } public function increase_finished(): void { - $this->wpdb->query( - $this->wpdb->prepare('UPDATE %i SET finished_scans = finished_scans + 1', $this->get_table_name()) + global $wpdb; + + $wpdb->query( + $wpdb->prepare('UPDATE %i SET finished_scans = finished_scans + 1', $this->get_table_name()) ); } public function reset(): void { - $this->wpdb->query( - $this->wpdb->prepare('UPDATE %i SET scheduled_scans = 0, finished_scans = 0', $this->get_table_name()) + global $wpdb; + + $wpdb->query( + $wpdb->prepare('UPDATE %i SET scheduled_scans = 0, finished_scans = 0', $this->get_table_name()) ); } } diff --git a/Vaas/ScanClient.php b/Vaas/ScanClient.php index db3ac05..358d5db 100644 --- a/Vaas/ScanClient.php +++ b/Vaas/ScanClient.php @@ -160,7 +160,11 @@ public function scan_single_upload( $file ) { $is_plugin_uplad = false; $action = \sanitize_key($_GET['action'] ?? $_POST['action'] ?? ''); - $nonce = \sanitize_key($_POST['nonce'] ?? $_POST['_wpnonce']); + if (isset($_POST['_wpnonce'])) { + $nonce = \sanitize_key($_POST['nonce'] ?? $_POST['_wpnonce']); + } else { + $nonce = \sanitize_key($_GET['nonce'] ?? ''); + } if ($action === 'upload-plugin') { if (wp_verify_nonce($nonce, $action) === false) { return $file; diff --git a/composer.lock b/composer.lock index dad2ef0..2b807c4 100644 --- a/composer.lock +++ b/composer.lock @@ -4656,4 +4656,4 @@ "php": "8.2" }, "plugin-api-version": "2.6.0" -} +} \ No newline at end of file