Passed
Push — master ( 6b8ca8...3384db )
by Paul
04:57
created

Router::routeAdminPostRequest()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 18
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 18
nc 6
nop 0
crap 42
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Controllers\AdminController;
7
use GeminiLabs\SiteReviews\Controllers\AjaxController;
8
use GeminiLabs\SiteReviews\Controllers\PublicController;
9
use GeminiLabs\SiteReviews\Helper;
10
11
class Router
12
{
13
	/**
14
	 * @return void
15
	 */
16
	public function routeAdminPostRequest()
17
	{
18
		$request = filter_input( INPUT_POST, Application::ID, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
19
		if( !isset( $request['action'] ))return;
20
		$this->checkNonce( $request['action'] );
21
		switch( $request['action'] ) {
22
			case 'clear-log':
23
				glsr( AdminController::class )->routerClearLog();
24
				break;
25
			case 'download-log':
26
				glsr( AdminController::class )->routerDownloadLog();
27
				break;
28
			case 'download-system-info':
29
				glsr( AdminController::class )->routerDownloadSystemInfo();
30
				break;
31
			case 'submit-review':
32
				glsr( PublicController::class )->routerSubmitReview( $request );
33
				break;
34
			default:
35
				do_action( 'site-reviews/route/admin/post/requests', $request['action'], $request );
36
		}
37
	}
38
39
	/**
40
	 * @return void
41
	 */
42
	public function routeAjaxRequest()
43
	{
44
		$request = $this->normalizeAjaxRequest();
45
		if( !wp_verify_nonce( $request['nonce'], $request['action'] )) {
46
			glsr_log()->error( 'Nonce check failed for ajax request' )->info( $request );
0 ignored issues
show
Bug introduced by
$request of type array is incompatible with the type string expected by parameter $message of GeminiLabs\SiteReviews\Modules\Logger::info(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
			glsr_log()->error( 'Nonce check failed for ajax request' )->info( /** @scrutinizer ignore-type */ $request );
Loading history...
47
			wp_die( -1, 403 );
48
		}
49
		$controller = glsr( AjaxController::class );
50
		$method = glsr( Helper::class )->buildMethodName( $request['action'] );
51
		if( is_callable( [$controller, $method] )) {
52
			call_user_func( [$controller, $method], $request );
53
		}
54
		else {
55
			do_action( 'site-reviews/route/ajax/requests', $method, $request );
56
		}
57
		wp_die();
58
	}
59
60
	/**
61
	 * @return void
62
	 */
63
	public function routePublicPostRequest()
64
	{
65
		switch( filter_input( INPUT_POST, 'action' )) {
66
			case 'submit-review':
67
				glsr( PublicController::class )->routerSubmitReview( $this->normalize( $_POST ));
68
				break;
69
		}
70
	}
71
72
	/**
73
	 * @return void
74
	 */
75
	public function routeWebhookRequest()
76
	{
77
		$request = filter_input( INPUT_GET, sprintf( '%s-hook', Application::ID ));
78
		if( !$request )return;
79
		// @todo manage webhook here
80
	}
81
82
	/**
83
	 * @param string $action
84
	 * @return void
85
	 */
86
	protected function checkNonce( $action )
87
	{
88
		$nonce = filter_input( INPUT_POST, 'option_page' ) == $action
89
			&& filter_input( INPUT_POST, 'action' ) == 'update'
90
			? $action.'-options'
91
			: $action;
92
		check_admin_referer( $nonce );
93
	}
94
95
	/**
96
	 * Undo damage done by javascript: encodeURIComponent() and sanitize values
97
	 * @return array
98
	 */
99
	protected function normalize( array $request )
100
	{
101
		array_walk_recursive( $request, function( &$value ) {
102
			$value = stripslashes( $value );
103
		});
104
		return $request;
105
	}
106
107
	/**
108
	 * All ajax requests in the plugin are triggered by a single action hook
109
	 * Each route is determined by the request["action"]
110
	 * @return array|void
111
	 */
112
	protected function normalizeAjaxRequest()
113
	{
114
		$request = filter_input( INPUT_POST, 'request', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
115
		if( isset( $request[Application::ID]['action'] )) {
116
			$request = $request[Application::ID];
117
		}
118
		if( !isset( $request['action'] )) {
119
			glsr_log()->error( 'The AJAX request must include an action' )->info( $request );
120
			wp_die();
121
		}
122
		if( !isset( $request['nonce'] )) {
123
			glsr_log()->error( 'The AJAX request must include a nonce' )->info( $request );
124
			wp_die();
125
		}
126
		$request['ajax_request'] = true;
127
		return $this->normalize( $request );
128
	}
129
}
130