| 1 |
<?php |
| 2 |
|
| 3 |
class Agent_Match extends Red_Match { |
| 4 |
public $agent; |
| 5 |
public $regex; |
| 6 |
public $url_from; |
| 7 |
public $url_notfrom; |
| 8 |
|
| 9 |
function name() { |
| 10 |
return __( 'URL and user agent', 'redirection' ); |
| 11 |
} |
| 12 |
|
| 13 |
public function save( array $details, $no_target_url = false ) { |
| 14 |
$data = array( |
| 15 |
'regex' => isset( $details['regex'] ) && $details['regex'] ? true : false, |
| 16 |
'agent' => isset( $details['agent'] ) ? $this->sanitize_agent( $details['agent'] ) : '', |
| 17 |
); |
| 18 |
|
| 19 |
if ( $no_target_url === false ) { |
| 20 |
$data['url_from'] = isset( $details['url_from'] ) ? $this->sanitize_url( $details['url_from'] ) : ''; |
| 21 |
$data['url_notfrom'] = isset( $details['url_notfrom'] ) ? $this->sanitize_url( $details['url_notfrom'] ) : ''; |
| 22 |
} |
| 23 |
|
| 24 |
return $data; |
| 25 |
} |
| 26 |
|
| 27 |
private function sanitize_agent( $agent ) { |
| 28 |
return $this->sanitize_url( $agent ); |
| 29 |
} |
| 30 |
|
| 31 |
public function get_target( $requested_url, $source_url, Red_Source_Flags $flags ) { |
| 32 |
// Check if referrer matches |
| 33 |
$matched = $this->agent === Redirection_Request::get_user_agent(); |
| 34 |
|
| 35 |
if ( $this->regex ) { |
| 36 |
$regex = new Red_Regex( $this->agent, true ); |
| 37 |
$matched = $regex->is_match( Redirection_Request::get_user_agent() ); |
| 38 |
} |
| 39 |
|
| 40 |
$target = false; |
| 41 |
if ( $this->url_from !== '' && $matched ) { |
| 42 |
$target = $this->url_from; |
| 43 |
} elseif ( $this->url_notfrom !== '' && ! $matched ) { |
| 44 |
$target = $this->url_notfrom; |
| 45 |
} |
| 46 |
|
| 47 |
if ( $flags->is_regex() && $target ) { |
| 48 |
$target = $this->get_target_regex_url( $source_url, $target, $requested_url, $flags ); |
| 49 |
} |
| 50 |
|
| 51 |
return $target; |
| 52 |
} |
| 53 |
|
| 54 |
public function get_data() { |
| 55 |
return array( |
| 56 |
'url_from' => $this->url_from, |
| 57 |
'url_notfrom' => $this->url_notfrom, |
| 58 |
'regex' => $this->regex, |
| 59 |
'agent' => $this->agent, |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
public function load( $values ) { |
| 64 |
$values = unserialize( $values ); |
| 65 |
|
| 66 |
if ( isset( $values['url_from'] ) ) { |
| 67 |
$this->url_from = $values['url_from']; |
| 68 |
$this->url_notfrom = $values['url_notfrom']; |
| 69 |
} |
| 70 |
|
| 71 |
$this->regex = $values['regex']; |
| 72 |
$this->agent = $values['agent']; |
| 73 |
} |
| 74 |
} |
| 75 |
|