PluginProbe
Redirection / 4.2
Redirection v4.2
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 / matches / ip.php

ip.php in Redirection 4.2, at matches/ip.php

61 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class IP_Match extends Red_Match {
4 use FromNotFrom_Match;
5
6 public $ip = [];
7
8 public function name() {
9 return __( 'URL and IP', 'redirection' );
10 }
11
12 public function save( array $details, $no_target_url = false ) {
13 $data = array( 'ip' => isset( $details['ip'] ) && is_array( $details['ip'] ) ? $this->sanitize_ips( $details['ip'] ) : [] );
14
15 return $this->save_data( $details, $no_target_url, $data );
16 }
17
18 private function sanitize_single_ip( $ip ) {
19 $ip = @inet_pton( trim( $ip ) );
20 if ( $ip !== false ) {
21 return @inet_ntop( $ip ); // Convert back to string
22 }
23
24 return false;
25 }
26
27 private function sanitize_ips( $ips ) {
28 if ( is_array( $ips ) ) {
29 $ips = array_map( array( $this, 'sanitize_single_ip' ), $ips );
30 return array_values( array_filter( array_unique( $ips ) ) );
31 }
32
33 return array();
34 }
35
36 private function get_matching_ips( $match_ip ) {
37 $current_ip = @inet_pton( $match_ip );
38
39 return array_filter( $this->ip, function( $ip ) use ( $current_ip ) {
40 return @inet_pton( $ip ) === $current_ip;
41 } );
42 }
43
44 public function is_match( $url ) {
45 $matched = $this->get_matching_ips( Redirection_Request::get_ip() );
46
47 return count( $matched ) > 0;
48 }
49
50 public function get_data() {
51 return array_merge( array(
52 'ip' => $this->ip,
53 ), $this->get_from_data() );
54 }
55
56 public function load( $values ) {
57 $values = $this->load_data( $values );
58 $this->ip = isset( $values['ip'] ) ? $values['ip'] : [];
59 }
60 }
61