| 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 |
function get_target( $url, $matched_url, $regex ) { |
| 32 |
// Check if referrer matches |
| 33 |
$matched = $this->agent === Redirection_Request::get_user_agent(); |
| 34 |
if ( $this->regex ) { |
| 35 |
$matched = preg_match( '@' . str_replace( '@', '\\@', $this->agent ) . '@i', Redirection_Request::get_user_agent() ) > 0; |
| 36 |
} |
| 37 |
|
| 38 |
$target = false; |
| 39 |
if ( $this->url_from !== '' && $matched ) { |
| 40 |
$target = $this->url_from; |
| 41 |
} elseif ( $this->url_notfrom !== '' && ! $matched ) { |
| 42 |
$target = $this->url_notfrom; |
| 43 |
} |
| 44 |
|
| 45 |
if ( $regex && $target ) { |
| 46 |
$target = $this->get_target_regex_url( $matched_url, $target, $url ); |
| 47 |
} |
| 48 |
|
| 49 |
return $target; |
| 50 |
} |
| 51 |
|
| 52 |
public function get_data() { |
| 53 |
return array( |
| 54 |
'url_from' => $this->url_from, |
| 55 |
'url_notfrom' => $this->url_notfrom, |
| 56 |
'regex' => $this->regex, |
| 57 |
'agent' => $this->agent, |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
public function load( $values ) { |
| 62 |
$values = unserialize( $values ); |
| 63 |
|
| 64 |
if ( isset( $values['url_from'] ) ) { |
| 65 |
$this->url_from = $values['url_from']; |
| 66 |
$this->url_notfrom = $values['url_notfrom']; |
| 67 |
} |
| 68 |
|
| 69 |
$this->regex = $values['regex']; |
| 70 |
$this->agent = $values['agent']; |
| 71 |
} |
| 72 |
} |
| 73 |
|