-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLaravelRickRoll.php
141 lines (120 loc) · 3 KB
/
LaravelRickRoll.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace Felix\RickRoll;
class LaravelRickRoll
{
/**
* The URL where the request is redirected.
*/
protected string $redirectsTo = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
/**
* List of urls that redirects to Never Gonna Give You Up.
*
* @var array<int, URL|string>
*/
public array $urls = [
'.env',
'.git',
'wp-admin',
'wp-login.php',
'composer.lock',
'yarn.lock',
'package-lock.json',
'xmlrpc.php',
'typo3',
'.aws',
];
/**
* The front-end of the library. There is a few options you can specify here
* redirects_to, changes the url where request are rick-rolled
* urls, a list of url to append
* If used in combination of `use_defaults => false`, only these urls will be registered.
*/
public function routes(array $options = []): self
{
if (isset($options['redirects_to'])) {
$this->redirectsTo = $options['redirects_to'];
}
if (isset($options['use_defaults']) && $options['use_defaults'] === false) {
$this->clear();
}
if (isset($options['urls'])) {
$this->withUrls($options['urls']);
}
return $this;
}
/**
* Clears all urls.
*
* @return $this
*/
public function clear(): self
{
$this->urls = [];
return $this;
}
/**
* Register an url.
*
* @return $this
*/
public function push(string $url, array $constraints = []): self
{
$this->urls[] = new URL($url, $constraints);
return $this;
}
/**
* Remove one or more urls from the list
* If a callable is the provided then we filter the array using this callable.
*
* @param string|URL|callable ...$urls
*/
public function remove(...$urls): bool
{
foreach ($urls as $url) {
$this->urls = array_filter(
$this->urls,
is_callable($url) ? $url : fn ($given) => $url !== $given
);
}
return false;
}
/**
* Sets the redirection url.
*
* @return $this
*/
public function redirectsTo(string $url): self
{
$this->redirectsTo = $url;
return $this;
}
/**
* Once everything is setup and the object is not used anymore,
* we can register our routes.
*/
public function __destruct()
{
foreach ($this->urls as $url) {
if (is_string($url)) {
$url = URL::createFromURL($url);
}
$url->register($this->redirectsTo);
}
}
/**
* Returns the redirect url.
*/
public function getRedirectURL(): string
{
return $this->redirectsTo;
}
public function getUrls(): array
{
return $this->urls;
}
public function withUrls(array $urls): self
{
$this->urls = [...$this->urls, ...$urls];
return $this;
}
}