| 1 |
<?php |
| 2 |
|
| 3 |
class Agent_Match extends Red_Match { |
| 4 |
public $user_agent; |
| 5 |
|
| 6 |
function name() { |
| 7 |
return __( 'URL and user agent', 'redirection' ); |
| 8 |
} |
| 9 |
|
| 10 |
public function save( array $details, $no_target_url = false ) { |
| 11 |
$data = array( |
| 12 |
'regex' => isset( $details['action_data_regex'] ) && $details['action_data_regex'] === 'true' ? true : false, |
| 13 |
'agent' => isset( $details['action_data_agent'] ) ? $this->sanitize_agent( $details['action_data_agent'] ) : '', |
| 14 |
); |
| 15 |
|
| 16 |
if ( $no_target_url === false ) { |
| 17 |
$data['url_from'] = isset( $details['action_data_url_from'] ) ? $this->sanitize_url( $details['action_data_url_from'] ) : ''; |
| 18 |
$data['url_notfrom'] = isset( $details['action_data_url_notfrom'] ) ? $this->sanitize_url( $details['action_data_url_notfrom'] ) : ''; |
| 19 |
} |
| 20 |
|
| 21 |
return $data; |
| 22 |
} |
| 23 |
|
| 24 |
private function sanitize_agent( $agent ) { |
| 25 |
return $this->sanitize_url( $agent ); |
| 26 |
} |
| 27 |
|
| 28 |
function initialize( $url ) { |
| 29 |
$this->url = array( $url, '' ); |
| 30 |
} |
| 31 |
|
| 32 |
function get_target( $url, $matched_url, $regex ) { |
| 33 |
// Check if referrer matches |
| 34 |
if ( preg_match( '@'.str_replace( '@', '\\@', $this->user_agent ).'@i', $_SERVER['HTTP_USER_AGENT'], $matches ) > 0 ) { |
| 35 |
return preg_replace( '@'.str_replace( '@', '\\@', $matched_url ).'@', $this->url_from, $url ); |
| 36 |
} elseif ( $this->url_notfrom !== '' ) { |
| 37 |
return $this->url_notfrom; |
| 38 |
} |
| 39 |
|
| 40 |
return false; |
| 41 |
} |
| 42 |
} |
| 43 |
|