PluginProbe
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager / 0.0.3
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager v0.0.3
0.0.11 0.0.10 0.0.9 0.0.8 0.0.7 0.0.6 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5
cookiez / modules / scanner / rest / create-scan.php

create-scan.php in Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager 0.0.3, at modules/scanner/rest/create-scan.php

148 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Cookiez\Modules\Scanner\Rest;
4
5 use Cookiez\Classes\Exceptions\Quota_Exceeded_Error;
6 use Cookiez\Classes\Logger;
7 use Cookiez\Classes\Rest\{
8 Sanitizer,
9 Validator,
10 };
11 use Cookiez\Modules\Scanner\Classes\{
12 Enums\Scan_Initiator,
13 Enums\Scan_Type,
14 Route_Base,
15 Scan_Url_Collector,
16 Service\Exceptions\Scan_Service_Client_Exception,
17 Service\Exceptions\Scan_Transport_Exception,
18 };
19 use Cookiez\Modules\Scanner\Components\Scanner;
20 use Cookiez\Modules\Scanner\Database\Scan_Entry;
21 use Throwable;
22 use WP_REST_Response;
23
24 if ( ! defined( 'ABSPATH' ) ) {
25 exit;
26 }
27
28 /**
29 * Class Create_Scan
30 * REST endpoint for creating scans.
31 */
32 class Create_Scan extends Route_Base {
33 public string $path = '';
34
35 public function get_methods(): array {
36 return [ 'POST' ];
37 }
38
39 public function get_name(): string {
40 return 'create-scan';
41 }
42
43 protected function sanitize_fields(): array {
44 return [
45 'type' => Sanitizer::text(),
46 'urls' => Sanitizer::array( Sanitizer::url() ),
47 'excluded_urls' => Sanitizer::array( Sanitizer::url() ),
48 ];
49 }
50
51 protected function validate_fields(): array {
52 $url_item = ( new Validator() )
53 ->url( [ 'message' => esc_html__( 'Must be a valid URL', 'cookiez' ) ] );
54
55 return [
56 'type' => ( new Validator() )
57 ->enum( Scan_Type::class,
58 [ 'message' => esc_html__( 'Invalid scan type', 'cookiez' ) ]
59 ),
60 'urls' => ( new Validator() )
61 ->array(
62 $url_item,
63 [ 'message' => esc_html__( 'URLs must be an array', 'cookiez' ) ]
64 )
65 ->nullable(),
66 'excluded_urls' => ( new Validator() )
67 ->array(
68 $url_item,
69 [ 'message' => esc_html__( 'Excluded URLs must be an array', 'cookiez' ) ]
70 )
71 ->nullable(),
72 ];
73 }
74
75 /**
76 * Start a scan.
77 *
78 * @return WP_REST_Response
79 */
80 public function POST(): WP_REST_Response {
81 try {
82 $error = $this->verify_capability();
83
84 if ( $error ) {
85 return $error;
86 }
87
88 $validation_errors = $this->validate( $this->params );
89
90 if ( ! empty( $validation_errors ) ) {
91 return $this->respond_validation_error( $validation_errors );
92 }
93
94 if ( Scan_Entry::is_active_scan() ) {
95 return $this->respond_error_json( [
96 'message' => esc_html__( 'Scanning is already in progress', 'cookiez' ),
97 'code' => 'scan_already_active',
98 ], 409 );
99 }
100
101 $type = $this->params['type'];
102 $initiator = Scan_Initiator::MANUAL;
103
104 $urls = Scan_Url_Collector::collect(
105 $type,
106 $this->params['urls'] ?? [],
107 $this->params['excluded_urls'] ?? []
108 );
109
110 if ( empty( $urls ) ) {
111 return $this->respond_error_json( [
112 'message' => esc_html__( 'No URLs to scan. Try to exclude less URLs', 'cookiez' ),
113 'code' => 'scan_create_failed',
114 ], 409 );
115 }
116
117 $scan = ( new Scanner() )->start_scan( $type, $initiator, $urls );
118
119 return $this->respond_success_json( [
120 'message' => esc_html__( 'Scan created successfully', 'cookiez' ),
121 'scan_id' => $scan->id,
122 ], 201 );
123 } catch ( Quota_Exceeded_Error $qee ) {
124 return $this->respond_error_json( [
125 'message' => esc_html__( 'Scan quota exceeded', 'cookiez' ),
126 'code' => 'quota_exceeded',
127 ], 429 );
128 } catch ( Scan_Transport_Exception $ste ) {
129 return $this->respond_error_json( [
130 'message' => esc_html__( 'Scan service unavailable', 'cookiez' ),
131 'code' => 'scan_service_unavailable',
132 ], 502 );
133 } catch ( Scan_Service_Client_Exception $ssce ) {
134 Logger::error( $ssce->getMessage() );
135
136 return $this->respond_error_json( [
137 'message' => $ssce->getMessage(),
138 'code' => 'scan_service_error',
139 ], 400 );
140 } catch ( Throwable $t ) {
141 return $this->respond_error_json( [
142 'message' => $t->getMessage(),
143 'code' => 'internal_server_error',
144 ], 500 );
145 }
146 }
147 }
148