forked from johngodley/redirection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirection-front.php
141 lines (118 loc) · 2.68 KB
/
redirection-front.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
require_once __DIR__ . '/modules/wordpress.php';
require_once __DIR__ . '/models/canonical.php';
require_once __DIR__ . '/database/database-status.php';
/**
* This powers all of the front-end redirecting
*/
class Redirection {
/**
* Instance variable
*
* @var Redirection|null
*/
private static $instance = null;
/**
* WordPress module
*
* @var WordPress_Module|null
*/
private $module = null;
/**
* Singleton
*
* @return Redirection
*/
public static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new Redirection();
}
return self::$instance;
}
/**
* Constructor
*/
public function __construct() {
if ( ! $this->can_start() ) {
return;
}
$this->module = Red_Module::get( WordPress_Module::MODULE_ID );
if ( $this->module ) {
$this->module->start();
}
add_action( Red_Flusher::DELETE_HOOK, array( $this, 'clean_redirection_logs' ) );
add_filter( 'redirection_url_target', [ $this, 'transform_url' ] );
$options = red_get_options();
if ( $options['ip_logging'] === 0 ) {
add_filter( 'redirection_request_ip', array( $this, 'no_ip_logging' ) );
} elseif ( $options['ip_logging'] === 2 ) {
add_filter( 'redirection_request_ip', array( $this, 'mask_ip' ) );
}
}
public function transform_url( $url ) {
$transformer = new Red_Url_Transform();
return $transformer->transform( $url );
}
/**
* Check if Redirection can run. We require the database to be installed.
*
* @return boolean
*/
public function can_start() {
$status = new Red_Database_Status();
if ( $status->needs_installing() ) {
return false;
}
return true;
}
/**
* Override the IP with an empty value
*
* @param string $ip IP.
* @return string
*/
public function no_ip_logging( $ip ) {
return '';
}
/**
* Override the IP with a masked IP
*
* @param string $ip IP.
* @return string
*/
public function mask_ip( $ip ) {
$ip = trim( $ip );
if ( strpos( $ip, ':' ) !== false ) {
// phpcs:ignore
$ip = @inet_pton( trim( $ip ) );
// phpcs:ignore
return @inet_ntop( $ip & pack( 'a16', 'ffff:ffff:ffff:ffff::ff00::0000::0000::0000' ) );
}
$parts = [];
if ( strlen( $ip ) > 0 ) {
$parts = explode( '.', $ip );
}
if ( count( $parts ) > 0 ) {
$parts[ count( $parts ) - 1 ] = 0;
}
return implode( '.', $parts );
}
/**
* Hook to flush the logs
*
* @return void
*/
public function clean_redirection_logs() {
$flusher = new Red_Flusher();
$flusher->flush();
}
/**
* Used for unit tests
*
* @return WordPress_Module|null
*/
public function get_module() {
return $this->module;
}
}
add_action( 'plugins_loaded', array( 'Redirection', 'init' ) );