| 1 |
<?php |
| 2 |
|
| 3 |
class Header_Match extends Red_Match { |
| 4 |
public $name; |
| 5 |
public $value; |
| 6 |
public $regex; |
| 7 |
public $url_from; |
| 8 |
public $url_notfrom; |
| 9 |
|
| 10 |
function name() { |
| 11 |
return __( 'URL and HTTP header', 'redirection' ); |
| 12 |
} |
| 13 |
|
| 14 |
public function save( array $details, $no_target_url = false ) { |
| 15 |
$data = array( |
| 16 |
'regex' => isset( $details['regex'] ) && $details['regex'] ? true : false, |
| 17 |
'name' => isset( $details['name'] ) ? $this->sanitize_name( $details['name'] ): '', |
| 18 |
'value' => isset( $details['value'] ) ? $this->sanitize_value( $details['value'] ) : '', |
| 19 |
); |
| 20 |
|
| 21 |
if ( $no_target_url === false ) { |
| 22 |
$data['url_from'] = isset( $details['url_from'] ) ? $this->sanitize_url( $details['url_from'] ) : ''; |
| 23 |
$data['url_notfrom'] = isset( $details['url_notfrom'] ) ? $this->sanitize_url( $details['url_notfrom'] ) : ''; |
| 24 |
} |
| 25 |
|
| 26 |
return $data; |
| 27 |
} |
| 28 |
|
| 29 |
public function sanitize_name( $name ) { |
| 30 |
$name = $this->sanitize_url( $name ); |
| 31 |
$name = str_replace( ' ', '', $name ); |
| 32 |
$name = preg_replace( '/[^A-Za-z0-9\-_]/', '', $name ); |
| 33 |
|
| 34 |
return trim( trim( $name, ':' ) ); |
| 35 |
} |
| 36 |
|
| 37 |
public function sanitize_value( $value ) { |
| 38 |
return $this->sanitize_url( $value ); |
| 39 |
} |
| 40 |
|
| 41 |
function get_target( $url, $matched_url, $regex ) { |
| 42 |
$target = false; |
| 43 |
$matched = Redirection_Request::get_header( $this->name ) === $this->value; |
| 44 |
|
| 45 |
if ( $this->regex ) { |
| 46 |
$matched = preg_match( '@' . str_replace( '@', '\\@', $this->value ) . '@', Redirection_Request::get_header( $this->name ), $matches ) > 0; |
| 47 |
} |
| 48 |
|
| 49 |
// Check if referrer matches |
| 50 |
if ( $matched && $this->url_from !== '' ) { |
| 51 |
$target = $this->url_from; |
| 52 |
} elseif ( ! $matched && $this->url_notfrom !== '' ) { |
| 53 |
$target = $this->url_notfrom; |
| 54 |
} |
| 55 |
|
| 56 |
if ( $regex && $target ) { |
| 57 |
$target = $this->get_target_regex_url( $matched_url, $target, $url ); |
| 58 |
} |
| 59 |
|
| 60 |
return $target; |
| 61 |
} |
| 62 |
|
| 63 |
public function get_data() { |
| 64 |
return array( |
| 65 |
'url_from' => $this->url_from, |
| 66 |
'url_notfrom' => $this->url_notfrom, |
| 67 |
'regex' => $this->regex, |
| 68 |
'name' => $this->name, |
| 69 |
'value' => $this->value, |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
public function load( $values ) { |
| 74 |
$values = unserialize( $values ); |
| 75 |
|
| 76 |
if ( isset( $values['url_from'] ) ) { |
| 77 |
$this->url_from = $values['url_from']; |
| 78 |
} |
| 79 |
|
| 80 |
if ( isset( $values['url_notfrom'] ) ) { |
| 81 |
$this->url_notfrom = $values['url_notfrom']; |
| 82 |
} |
| 83 |
|
| 84 |
$this->regex = $values['regex']; |
| 85 |
$this->name = $values['name']; |
| 86 |
$this->value = $values['value']; |
| 87 |
} |
| 88 |
} |
| 89 |
|