| 1 |
<?php |
| 2 |
|
| 3 |
class Header_Match extends Red_Match { |
| 4 |
use FromNotFrom_Match; |
| 5 |
|
| 6 |
public $name; |
| 7 |
public $value; |
| 8 |
public $regex; |
| 9 |
|
| 10 |
public 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 |
return $this->save_data( $details, $no_target_url, $data ); |
| 22 |
} |
| 23 |
|
| 24 |
public function sanitize_name( $name ) { |
| 25 |
$name = $this->sanitize_url( $name ); |
| 26 |
$name = str_replace( ' ', '', $name ); |
| 27 |
$name = preg_replace( '/[^A-Za-z0-9\-_]/', '', $name ); |
| 28 |
|
| 29 |
return trim( trim( $name, ':' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
public function sanitize_value( $value ) { |
| 33 |
return $this->sanitize_url( $value ); |
| 34 |
} |
| 35 |
|
| 36 |
public function is_match( $url ) { |
| 37 |
if ( $this->regex ) { |
| 38 |
$regex = new Red_Regex( $this->value, true ); |
| 39 |
return $regex->is_match( Redirection_Request::get_header( $this->name ) ); |
| 40 |
} |
| 41 |
|
| 42 |
return Redirection_Request::get_header( $this->name ) === $this->value; |
| 43 |
} |
| 44 |
|
| 45 |
public function get_data() { |
| 46 |
return array_merge( array( |
| 47 |
'regex' => $this->regex, |
| 48 |
'name' => $this->name, |
| 49 |
'value' => $this->value, |
| 50 |
), $this->get_from_data() ); |
| 51 |
} |
| 52 |
|
| 53 |
public function load( $values ) { |
| 54 |
$values = $this->load_data( $values ); |
| 55 |
$this->regex = isset( $values['regex'] ) ? $values['regex'] : false; |
| 56 |
$this->name = isset( $values['name'] ) ? $values['name'] : ''; |
| 57 |
$this->value = isset( $values['value'] ) ? $values['value'] : ''; |
| 58 |
} |
| 59 |
} |
| 60 |
|