| 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|null |
| 22 |
*/ |
| 23 |
private $module = null; |
| 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 |
$this->module = Red_Module::get( WordPress_Module::MODULE_ID ); |
| 47 |
if ( $this->module ) { |
| 48 |
$this->module->start(); |
| 49 |
} |
| 50 |
|
| 51 |
add_action( Red_Flusher::DELETE_HOOK, array( $this, 'clean_redirection_logs' ) ); |
| 52 |
add_filter( 'redirection_url_target', [ $this, 'transform_url' ] ); |
| 53 |
|
| 54 |
$options = red_get_options(); |
| 55 |
if ( $options['ip_logging'] === 0 ) { |
| 56 |
add_filter( 'redirection_request_ip', array( $this, 'no_ip_logging' ) ); |
| 57 |
} elseif ( $options['ip_logging'] === 2 ) { |
| 58 |
add_filter( 'redirection_request_ip', array( $this, 'mask_ip' ) ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public function transform_url( $url ) { |
| 63 |
$transformer = new Red_Url_Transform(); |
| 64 |
|
| 65 |
return $transformer->transform( $url ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Check if Redirection can run. We require the database to be installed. |
| 70 |
* |
| 71 |
* @return boolean |
| 72 |
*/ |
| 73 |
public function can_start() { |
| 74 |
$status = new Red_Database_Status(); |
| 75 |
if ( $status->needs_installing() ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Override the IP with an empty value |
| 84 |
* |
| 85 |
* @param String $ip IP. |
| 86 |
* @return String |
| 87 |
*/ |
| 88 |
public function no_ip_logging( $ip ) { |
| 89 |
return ''; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Override the IP with a masked IP |
| 94 |
* |
| 95 |
* @param String $ip IP. |
| 96 |
* @return String |
| 97 |
*/ |
| 98 |
public function mask_ip( $ip ) { |
| 99 |
$ip = trim( $ip ); |
| 100 |
|
| 101 |
if ( strpos( $ip, ':' ) !== false ) { |
| 102 |
// phpcs:ignore |
| 103 |
$ip = @inet_pton( trim( $ip ) ); |
| 104 |
|
| 105 |
// phpcs:ignore |
| 106 |
return @inet_ntop( $ip & pack( 'a16', 'ffff:ffff:ffff:ffff::ff00::0000::0000::0000' ) ); |
| 107 |
} |
| 108 |
|
| 109 |
$parts = []; |
| 110 |
if ( strlen( $ip ) > 0 ) { |
| 111 |
$parts = explode( '.', $ip ); |
| 112 |
} |
| 113 |
|
| 114 |
if ( count( $parts ) > 0 ) { |
| 115 |
$parts[ count( $parts ) - 1 ] = 0; |
| 116 |
} |
| 117 |
|
| 118 |
return implode( '.', $parts ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Hook to flush the logs |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function clean_redirection_logs() { |
| 127 |
$flusher = new Red_Flusher(); |
| 128 |
$flusher->flush(); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Used for unit tests |
| 133 |
* |
| 134 |
* @return WordPress_Module|null |
| 135 |
*/ |
| 136 |
public function get_module() { |
| 137 |
return $this->module; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
add_action( 'plugins_loaded', array( 'Redirection', 'init' ) ); |
| 142 |
|