| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Check a HTTP request header |
| 5 |
*/ |
| 6 |
class Header_Match extends Red_Match { |
| 7 |
use FromNotFrom_Match; |
| 8 |
|
| 9 |
/** |
| 10 |
* HTTP header name |
| 11 |
* |
| 12 |
* @var String |
| 13 |
*/ |
| 14 |
public $name = ''; |
| 15 |
|
| 16 |
/** |
| 17 |
* HTTP header value |
| 18 |
* |
| 19 |
* @var String |
| 20 |
*/ |
| 21 |
public $value = ''; |
| 22 |
|
| 23 |
/** |
| 24 |
* Is this a regex? |
| 25 |
* |
| 26 |
* @var boolean |
| 27 |
*/ |
| 28 |
public $regex = false; |
| 29 |
|
| 30 |
public function name() { |
| 31 |
return __( 'URL and HTTP header', 'redirection' ); |
| 32 |
} |
| 33 |
|
| 34 |
public function save( array $details, $no_target_url = false ) { |
| 35 |
$data = array( |
| 36 |
'regex' => isset( $details['regex'] ) && $details['regex'] ? true : false, |
| 37 |
'name' => isset( $details['name'] ) ? $this->sanitize_name( $details['name'] ) : '', |
| 38 |
'value' => isset( $details['value'] ) ? $this->sanitize_value( $details['value'] ) : '', |
| 39 |
); |
| 40 |
|
| 41 |
return $this->save_data( $details, $no_target_url, $data ); |
| 42 |
} |
| 43 |
|
| 44 |
public function sanitize_name( $name ) { |
| 45 |
$name = $this->sanitize_url( sanitize_text_field( $name ) ); |
| 46 |
$name = str_replace( ' ', '', $name ); |
| 47 |
$name = preg_replace( '/[^A-Za-z0-9\-_]/', '', $name ); |
| 48 |
|
| 49 |
return trim( trim( $name, ':' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
public function sanitize_value( $value ) { |
| 53 |
return $this->sanitize_url( sanitize_text_field( $value ) ); |
| 54 |
} |
| 55 |
|
| 56 |
public function is_match( $url ) { |
| 57 |
if ( $this->regex ) { |
| 58 |
$regex = new Red_Regex( $this->value, true ); |
| 59 |
return $regex->is_match( Redirection_Request::get_header( $this->name ) ); |
| 60 |
} |
| 61 |
|
| 62 |
return Redirection_Request::get_header( $this->name ) === $this->value; |
| 63 |
} |
| 64 |
|
| 65 |
public function get_data() { |
| 66 |
return array_merge( array( |
| 67 |
'regex' => $this->regex, |
| 68 |
'name' => $this->name, |
| 69 |
'value' => $this->value, |
| 70 |
), $this->get_from_data() ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Load the match data into this instance. |
| 75 |
* |
| 76 |
* @param string $values Match values, as read from the database (plain text or serialized PHP). |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function load( $values ) { |
| 80 |
$values = $this->load_data( $values ); |
| 81 |
$this->regex = isset( $values['regex'] ) ? $values['regex'] : false; |
| 82 |
$this->name = isset( $values['name'] ) ? $values['name'] : ''; |
| 83 |
$this->value = isset( $values['value'] ) ? $values['value'] : ''; |
| 84 |
} |
| 85 |
} |
| 86 |
|