PluginProbe
Redirection / 5.6.0
Redirection v5.6.0
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / redirection-front.php

redirection-front.php in Redirection 5.6.0, at redirection-front.php

158 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require_once __DIR__ . '/modules/wordpress.php';
4 require_once __DIR__ . '/models/canonical.php';
5 require_once __DIR__ . '/database/database-status.php';
6
7 /**
8 * This powers all of the front-end redirecting
9 */
10 class Redirection {
11 /**
12 * Instance variable
13 *
14 * @var Redirection|null
15 */
16 private static $instance = null;
17
18 /**
19 * WordPress module
20 *
21 * @var WordPress_Module|false
22 */
23 private $module = false;
24
25 /**
26 * Singleton
27 *
28 * @return Redirection
29 */
30 public static function init() {
31 if ( is_null( self::$instance ) ) {
32 self::$instance = new Redirection();
33 }
34
35 return self::$instance;
36 }
37
38 /**
39 * Constructor
40 */
41 public function __construct() {
42 if ( ! $this->can_start() ) {
43 return;
44 }
45
46 /** @var WordPress_Module|false */
47 $module = Red_Module::get( WordPress_Module::MODULE_ID );
48 $this->module = $module;
49 if ( $this->module !== false ) {
50 $this->module->start();
51 }
52
53 add_action( Red_Flusher::DELETE_HOOK, array( $this, 'clean_redirection_logs' ) );
54 add_filter( 'redirection_url_target', [ $this, 'transform_url' ] );
55
56 $options = Red_Options::get();
57 if ( $options['ip_logging'] === 0 ) {
58 add_filter( 'redirection_request_ip', array( $this, 'no_ip_logging' ) );
59 } elseif ( $options['ip_logging'] === 2 ) {
60 add_filter( 'redirection_request_ip', array( $this, 'mask_ip' ) );
61 }
62 }
63
64 /**
65 * @param string $url
66 * @return string
67 */
68 public function transform_url( $url ) {
69 $transformer = new Red_Url_Transform();
70
71 return $transformer->transform( $url );
72 }
73
74 /**
75 * Check if Redirection can run. We require the database to be installed.
76 *
77 * @return boolean
78 */
79 public function can_start() {
80 $status = new Red_Database_Status();
81 if ( $status->needs_installing() ) {
82 return false;
83 }
84
85 return true;
86 }
87
88 /**
89 * Override the IP with an empty value
90 *
91 * @param string $ip IP.
92 * @return string
93 */
94 public function no_ip_logging( $ip ) {
95 return '';
96 }
97
98 /**
99 * Override the IP with a masked IP
100 *
101 * @param string $ip IP.
102 * @return string
103 */
104 public function mask_ip( $ip ) {
105 $ip = trim( $ip );
106
107 if ( strpos( $ip, ':' ) !== false ) {
108 $packed = inet_pton( trim( $ip ) );
109 if ( $packed === false ) {
110 return '';
111 }
112
113 // Mask lower 64 bits of the IPv6 address (keep the upper 64 bits).
114 $mask = str_repeat( "\xff", 8 ) . str_repeat( "\x00", 8 );
115 $masked = $packed & $mask;
116 $converted = inet_ntop( $masked );
117 if ( $converted === false ) {
118 return '';
119 }
120
121 return $converted;
122 }
123
124 $parts = [];
125 if ( strlen( $ip ) > 0 ) {
126 $parts = explode( '.', $ip );
127 }
128
129 if ( count( $parts ) > 0 ) {
130 $parts[ count( $parts ) - 1 ] = 0;
131 }
132
133 return implode( '.', $parts );
134 }
135
136 /**
137 * Hook to flush the logs
138 *
139 * @return void
140 */
141 public function clean_redirection_logs() {
142 $flusher = new Red_Flusher();
143 $flusher->flush();
144 }
145
146 /**
147 * Used for unit tests
148 *
149 * @return WordPress_Module|false
150 */
151 public function get_module() {
152 return $this->module;
153 }
154 }
155
156 // @phpstan-ignore return.void
157 add_action( 'plugins_loaded', array( 'Redirection', 'init' ) );
158