| 1 |
<?php |
| 2 |
|
| 3 |
require_once __DIR__ . '/modules/wordpress.php'; |
| 4 |
require_once __DIR__ . '/database/database-status.php'; |
| 5 |
|
| 6 |
class Redirection { |
| 7 |
private static $instance = null; |
| 8 |
private $module; |
| 9 |
|
| 10 |
public static function init() { |
| 11 |
if ( is_null( self::$instance ) ) { |
| 12 |
self::$instance = new Redirection(); |
| 13 |
} |
| 14 |
|
| 15 |
return self::$instance; |
| 16 |
} |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
if ( ! $this->can_start() ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
$this->module = Red_Module::get( WordPress_Module::MODULE_ID ); |
| 24 |
$this->module->start(); |
| 25 |
|
| 26 |
add_action( Red_Flusher::DELETE_HOOK, array( $this, 'clean_redirection_logs' ) ); |
| 27 |
add_filter( 'redirection_url_target', array( $this, 'replace_special_tags' ) ); |
| 28 |
|
| 29 |
$options = red_get_options(); |
| 30 |
if ( $options['ip_logging'] === 0 ) { |
| 31 |
add_filter( 'redirection_request_ip', array( $this, 'no_ip_logging' ) ); |
| 32 |
} elseif ( $options['ip_logging'] === 2 ) { |
| 33 |
add_filter( 'redirection_request_ip', array( $this, 'mask_ip' ) ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function can_start() { |
| 38 |
$status = new Red_Database_Status(); |
| 39 |
if ( $status->needs_installing() ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
public function no_ip_logging( $ip ) { |
| 47 |
return ''; |
| 48 |
} |
| 49 |
|
| 50 |
public function mask_ip( $ip ) { |
| 51 |
$ip = trim( $ip ); |
| 52 |
|
| 53 |
if ( strpos( $ip, ':' ) !== false ) { |
| 54 |
// phpcs:ignore |
| 55 |
$ip = @inet_pton( trim( $ip ) ); |
| 56 |
|
| 57 |
// phpcs:ignore |
| 58 |
return @inet_ntop( $ip & pack( 'a16', 'ffff:ffff:ffff:ffff::ff00::0000::0000::0000' ) ); |
| 59 |
} |
| 60 |
|
| 61 |
$parts = []; |
| 62 |
if ( strlen( $ip ) > 0 ) { |
| 63 |
$parts = explode( '.', $ip ); |
| 64 |
} |
| 65 |
|
| 66 |
if ( count( $parts ) > 0 ) { |
| 67 |
$parts[ count( $parts ) - 1 ] = 0; |
| 68 |
} |
| 69 |
|
| 70 |
return implode( '.', $parts ); |
| 71 |
} |
| 72 |
|
| 73 |
public function clean_redirection_logs() { |
| 74 |
$flusher = new Red_Flusher(); |
| 75 |
$flusher->flush(); |
| 76 |
} |
| 77 |
|
| 78 |
// From the distant Redirection past. Undecided whether to keep |
| 79 |
public function replace_special_tags( $url ) { |
| 80 |
if ( is_numeric( $url ) ) { |
| 81 |
$url = get_permalink( $url ); |
| 82 |
} else { |
| 83 |
$user = wp_get_current_user(); |
| 84 |
|
| 85 |
if ( ! empty( $user ) ) { |
| 86 |
$url = str_replace( '%userid%', $user->ID, $url ); |
| 87 |
$url = str_replace( '%userlogin%', isset( $user->user_login ) ? $user->user_login : '', $url ); |
| 88 |
$url = str_replace( '%userurl%', isset( $user->user_url ) ? $user->user_url : '', $url ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $url; |
| 93 |
} |
| 94 |
|
| 95 |
// Used for unit tests |
| 96 |
public function get_module() { |
| 97 |
return $this->module; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
add_action( 'plugins_loaded', array( 'Redirection', 'init' ) ); |
| 102 |
|