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