| 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( $requested_url, $source_url, Red_Source_Flags $flags ) { |
| 42 |
$target = false; |
| 43 |
$matched = Redirection_Request::get_header( $this->name ) === $this->value; |
| 44 |
|
| 45 |
if ( $this->regex ) { |
| 46 |
$regex = new Red_Regex( $this->value, true ); |
| 47 |
$matched = $regex->is_match( Redirection_Request::get_header( $this->name ) ); |
| 48 |
} |
| 49 |
|
| 50 |
// Check if referrer matches |
| 51 |
if ( $matched && $this->url_from !== '' ) { |
| 52 |
$target = $this->url_from; |
| 53 |
} elseif ( ! $matched && $this->url_notfrom !== '' ) { |
| 54 |
$target = $this->url_notfrom; |
| 55 |
} |
| 56 |
|
| 57 |
if ( $flags->is_regex() && $target ) { |
| 58 |
$target = $this->get_target_regex_url( $source_url, $target, $requested_url, $flags ); |
| 59 |
} |
| 60 |
|
| 61 |
return $target; |
| 62 |
} |
| 63 |
|
| 64 |
public function get_data() { |
| 65 |
return array( |
| 66 |
'url_from' => $this->url_from, |
| 67 |
'url_notfrom' => $this->url_notfrom, |
| 68 |
'regex' => $this->regex, |
| 69 |
'name' => $this->name, |
| 70 |
'value' => $this->value, |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
public function load( $values ) { |
| 75 |
$values = unserialize( $values ); |
| 76 |
|
| 77 |
if ( isset( $values['url_from'] ) ) { |
| 78 |
$this->url_from = $values['url_from']; |
| 79 |
} |
| 80 |
|
| 81 |
if ( isset( $values['url_notfrom'] ) ) { |
| 82 |
$this->url_notfrom = $values['url_notfrom']; |
| 83 |
} |
| 84 |
|
| 85 |
$this->regex = $values['regex']; |
| 86 |
$this->name = $values['name']; |
| 87 |
$this->value = $values['value']; |
| 88 |
} |
| 89 |
} |
| 90 |
|