1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Request management object. |
4
|
|
|
* @package WordPress_GitHub_Sync |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class WordPress_GitHub_Sync_Request |
9
|
|
|
*/ |
10
|
|
|
class WordPress_GitHub_Sync_Request { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Application container. |
14
|
|
|
* |
15
|
|
|
* @var WordPress_GitHub_Sync |
16
|
|
|
*/ |
17
|
|
|
protected $app; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Raw request data. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $raw_data; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* WordPress_GitHub_Sync_Request constructor. |
28
|
|
|
* |
29
|
|
|
* @param WordPress_GitHub_Sync $app Application container. |
30
|
|
|
*/ |
31
|
2 |
|
public function __construct( WordPress_GitHub_Sync $app ) { |
32
|
2 |
|
$this->app = $app; |
33
|
2 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Validates the header's secret. |
37
|
|
|
* |
38
|
|
|
* @return true|WP_Error |
39
|
|
|
*/ |
40
|
2 |
|
public function is_secret_valid() { |
41
|
2 |
|
$headers = $this->headers(); |
42
|
|
|
|
43
|
2 |
|
$this->raw_data = $this->read_raw_data(); |
44
|
|
|
|
45
|
|
|
// Validate request secret. |
46
|
2 |
|
$hash = hash_hmac( 'sha1', $this->raw_data, $this->secret() ); |
47
|
2 |
|
if ( 'sha1=' . $hash !== $headers['X-Hub-Signature'] ) { |
48
|
1 |
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns a payload object for the given request. |
56
|
|
|
* |
57
|
|
|
* @return WordPress_GitHub_Sync_Payload |
58
|
|
|
*/ |
59
|
1 |
|
public function payload() { |
60
|
1 |
|
return new WordPress_GitHub_Sync_Payload( $this->app, $this->raw_data ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Cross-server header support. |
65
|
|
|
* |
66
|
|
|
* Returns an array of the request's headers. |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
2 |
|
protected function headers() { |
71
|
2 |
|
if ( function_exists( 'getallheaders' ) ) { |
72
|
|
|
return getallheaders(); |
73
|
|
|
} |
74
|
|
|
/** |
75
|
|
|
* Nginx and pre 5.4 workaround. |
76
|
|
|
* @see http://www.php.net/manual/en/function.getallheaders.php |
77
|
|
|
*/ |
78
|
2 |
|
$headers = array(); |
79
|
2 |
|
foreach ( $_SERVER as $name => $value ) { |
80
|
2 |
|
if ( 'HTTP_' === substr( $name, 0, 5 ) ) { |
81
|
2 |
|
$headers[ str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) ) ] = $value; |
82
|
2 |
|
} |
83
|
2 |
|
} |
84
|
|
|
|
85
|
2 |
|
return $headers; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Reads the raw data from STDIN. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
protected function read_raw_data() { |
94
|
|
|
return file_get_contents( 'php://input' ); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the Webhook secret |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
2 |
|
protected function secret() { |
103
|
2 |
|
return get_option( 'wpghs_secret' ); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|