1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Morphatic\AutoDeploy\Origins; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
|
7
|
|
|
abstract class AbstractOrigin implements OriginInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The name of the origin. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
public $name; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The Request object associated with this webhook. |
18
|
|
|
* |
19
|
|
|
* @var Illuminate\Http\Request |
20
|
|
|
*/ |
21
|
|
|
public $request; |
22
|
|
|
|
23
|
36 |
|
public function __construct(Request $request) |
|
|
|
|
24
|
|
|
{ |
25
|
36 |
|
$this->request = $request; |
|
|
|
|
26
|
36 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Determines whether or not the Request originated from the webhook origin. |
30
|
|
|
* |
31
|
|
|
* @return bool Returns true if the request originated from this origin. False otherwise. |
32
|
|
|
*/ |
33
|
|
|
abstract public function isOrigin(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Verifies the authenticity of a webhook request from the origin. |
37
|
|
|
* |
38
|
|
|
* @return bool Returns true if the request is authentic. False otherwise. |
39
|
|
|
*/ |
40
|
|
|
abstract public function isAuthentic(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Gets the event the triggered the webhook request. |
44
|
|
|
* |
45
|
|
|
* @return string The name of the event, e.g. push, release, create, etc. |
46
|
|
|
*/ |
47
|
|
|
abstract public function event(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets the URL to be cloned from. |
51
|
|
|
* |
52
|
|
|
* @return string The URL of the repo. |
53
|
|
|
*/ |
54
|
|
|
abstract public function getRepoUrl(); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Gets the ID of the commit that is to be cloned. |
58
|
|
|
* |
59
|
|
|
* @return string The commit ID. |
60
|
|
|
*/ |
61
|
|
|
abstract public function getCommitId(); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Determines if a target IP address is within a particular IP address range. |
65
|
|
|
* |
66
|
|
|
* @param string $targetIp The IP address to check |
67
|
|
|
* @param string $rangeIp The IP address defining the range |
68
|
|
|
* @param int $cidrMask The CIDR notation net mask defining the range |
69
|
|
|
* |
70
|
|
|
* @return bool True if the target IP falls within the specified range. Otherwise false. |
71
|
|
|
*/ |
72
|
18 |
|
protected function isIpInRange($targetIp, $rangeIp, $cidrMask) |
73
|
|
|
{ |
74
|
18 |
|
$target = sprintf('%032b', ip2long($targetIp)); |
75
|
18 |
|
$range = sprintf('%032b', ip2long($rangeIp)); |
76
|
|
|
|
77
|
18 |
|
return 0 === substr_compare($target, $range, 0, $cidrMask); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|