1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RedirectionIO\Client\Wordpress; |
4
|
|
|
|
5
|
|
|
use RedirectionIO\Client\Sdk\Client; |
6
|
|
|
use RedirectionIO\Client\Sdk\HttpMessage\Request; |
7
|
|
|
use RedirectionIO\Client\Sdk\HttpMessage\Response; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Main plugin file. |
11
|
|
|
* |
12
|
|
|
* This class is the core logic of the plugin. |
13
|
|
|
*/ |
14
|
|
|
class RedirectionIO |
15
|
|
|
{ |
16
|
|
|
private $client; |
17
|
|
|
|
18
|
|
|
private $lastRuleId = null; |
19
|
|
|
|
20
|
4 |
|
public function __construct() |
21
|
|
|
{ |
22
|
4 |
|
add_action('plugins_loaded', [$this, 'findRedirect']); |
|
|
|
|
23
|
4 |
|
add_action('template_redirect', [$this, 'log']); |
24
|
4 |
|
} |
25
|
|
|
|
26
|
|
|
public function setUp() |
27
|
|
|
{ |
28
|
|
|
add_option('redirectionio', [ |
29
|
|
|
'connections' => [ |
30
|
|
|
[ |
31
|
|
|
'name' => '', |
32
|
|
|
'remote_socket' => '', |
33
|
|
|
], |
34
|
|
|
], |
35
|
|
|
'doNotRedirectAdmin' => true, |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
public function findRedirect() |
40
|
|
|
{ |
41
|
4 |
|
$options = get_option('redirectionio'); |
|
|
|
|
42
|
4 |
|
$connections = []; |
43
|
|
|
|
44
|
4 |
|
if (false === $options || !isset($options['connections'])) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
foreach ($options['connections'] as $connection) { |
49
|
4 |
|
$connections[$connection['name']] = $connection['remote_socket']; |
50
|
4 |
|
} |
51
|
|
|
|
52
|
4 |
|
$this->client = new Client($connections); |
53
|
4 |
|
$scheme = 'http'; |
54
|
|
|
|
55
|
4 |
|
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { |
56
|
|
|
$scheme = $_SERVER['HTTP_X_FORWARDED_PROTO']; |
57
|
4 |
|
} elseif (!empty($_SERVER['HTTPS'])) { |
58
|
|
|
$scheme = 'https'; |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
$request = new Request( |
62
|
4 |
|
$_SERVER['HTTP_HOST'], |
63
|
4 |
|
$_SERVER['REQUEST_URI'], |
64
|
4 |
|
$_SERVER['HTTP_USER_AGENT'], |
65
|
4 |
|
$_SERVER['HTTP_REFERER'], |
66
|
|
|
$scheme |
67
|
4 |
|
); |
68
|
|
|
|
69
|
4 |
|
if ($this->isAdminPage($request) && $options['doNotRedirectAdmin']) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
$response = $this->client->findRedirect($request); |
74
|
|
|
|
75
|
4 |
|
if (null === $response) { |
76
|
2 |
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
if (method_exists($response, 'getRuleId')) { |
80
|
|
|
$this->lastRuleId = $response->getRuleId(); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
if ($response->getStatusCode() === 410) { |
84
|
1 |
|
define('DONOTCACHEPAGE', true); // WP Super Cache and W3 Total Cache recognise this |
85
|
1 |
|
status_header(410); |
86
|
1 |
|
} else { |
87
|
1 |
|
wp_redirect($response->getLocation(), $response->getStatusCode()); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
$this->exitCode(); |
91
|
2 |
|
} |
92
|
|
|
|
93
|
|
|
public function log() |
94
|
|
|
{ |
95
|
|
|
$scheme = 'http'; |
96
|
|
|
|
97
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { |
98
|
|
|
$scheme = $_SERVER['HTTP_X_FORWARDED_PROTO']; |
99
|
|
|
} elseif (!empty($_SERVER['HTTPS'])) { |
100
|
|
|
$scheme = 'https'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$location = null; |
104
|
|
|
$headers = headers_list(); |
105
|
|
|
|
106
|
|
|
foreach ($headers as $header) { |
107
|
|
|
$locationPos = stripos($header, 'Location: '); |
108
|
|
|
|
109
|
|
|
if ($locationPos !== false) { |
110
|
|
|
$location = substr($header, $locationPos); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$request = new Request( |
115
|
|
|
$_SERVER['HTTP_HOST'], |
116
|
|
|
$_SERVER['REQUEST_URI'], |
117
|
|
|
$_SERVER['HTTP_USER_AGENT'], |
118
|
|
|
$_SERVER['HTTP_REFERER'], |
119
|
|
|
$scheme |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$response = new Response(http_response_code(), $this->lastRuleId, $location); |
|
|
|
|
123
|
|
|
|
124
|
|
|
$this->client->log($request, $response); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function exitCode() |
128
|
|
|
{ |
129
|
|
|
exit; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Check if the requested page belongs to admin area. |
134
|
|
|
* |
135
|
|
|
* @param Request $request |
136
|
|
|
*/ |
137
|
4 |
|
private function isAdminPage(Request $request) |
138
|
|
|
{ |
139
|
4 |
|
$adminRoot = str_replace(get_site_url(), '', get_admin_url()); |
|
|
|
|
140
|
4 |
|
$requestPath = substr($request->getPath(), 0, strlen($adminRoot)); |
141
|
|
|
|
142
|
4 |
|
if ($adminRoot === $requestPath) { |
143
|
4 |
|
return true; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.