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