| 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 |
function show() { |
| 11 |
$defined = array( |
| 12 |
'feedburner|feedvalidator' => __( 'FeedBurner', 'redirection' ), |
| 13 |
'MSIE' => __( 'Internet Explorer', 'redirection' ), |
| 14 |
'Firefox' => __( 'FireFox', 'redirection' ), |
| 15 |
'Opera' => __( 'Opera', 'redirection' ), |
| 16 |
'Safari' => __( 'Safari', 'redirection' ), |
| 17 |
'iPhone' => __( 'iPhone', 'redirection' ), |
| 18 |
'iPad' => __( 'iPad', 'redirection' ), |
| 19 |
'Android' => __( 'Android', 'redirection' ), |
| 20 |
'Wii' => __( 'Nintendo Wii', 'redirection' ), |
| 21 |
); |
| 22 |
|
| 23 |
?> |
| 24 |
<tr> |
| 25 |
<th width="100"><?php _e( 'User Agent', 'redirection' ); ?>:</th> |
| 26 |
<td> |
| 27 |
<input id="user_agent_<?php echo $this->id ?>" style="width: 65%" type="text" name="user_agent" value="<?php echo esc_attr( $this->user_agent ); ?>"/> |
| 28 |
<select style="width: 30%" class="change-user-agent"> |
| 29 |
<?php foreach ( $defined as $key => $value ) : ?> |
| 30 |
<option value="<?php echo $key ?>"<?php if ( $key === $this->user_agent ) echo ' selected="selected"' ?>><?php echo esc_html( $value ) ?></option> |
| 31 |
<?php endforeach; ?> |
| 32 |
</select> |
| 33 |
</td> |
| 34 |
</tr> |
| 35 |
|
| 36 |
<?php if ( $this->action->can_change_code() ) : ?> |
| 37 |
<tr> |
| 38 |
<th><?php _e( 'HTTP Code', 'redirection' ); ?>:</th> |
| 39 |
<td> |
| 40 |
<select name="action_code"> |
| 41 |
<?php $this->action->display_actions(); ?> |
| 42 |
</select> |
| 43 |
</td> |
| 44 |
</tr> |
| 45 |
<?php endif; ?> |
| 46 |
|
| 47 |
<?php if ( $this->action->can_perform_action() ) : ?> |
| 48 |
<tr> |
| 49 |
<th></th> |
| 50 |
<td> |
| 51 |
<p style="padding: 0.5em"><?php _e( 'The visitor will be redirected from the source URL if the user agent matches. You can specify a <em>matched</em> target URL as the address to send visitors if they do match, and <em>not matched</em> if they don\'t match. Leaving a URL blank means that the visitor is not redirected. <strong>All matches are performed as regular expressions</strong>. |
| 52 |
', 'redirection' ); ?></p> |
| 53 |
</td> |
| 54 |
</tr> |
| 55 |
<tr> |
| 56 |
<th width="100" valign="top"> |
| 57 |
<?php if ( strlen( $this->url_from ) > 0 ) : ?> |
| 58 |
<a target="_blank" href="<?php echo esc_url( $this->url_from ) ?>"><?php _e( 'Matched', 'redirection' ); ?>:</a> |
| 59 |
<?php else : ?> |
| 60 |
<?php _e( 'Matched', 'redirection' ); ?>: |
| 61 |
<?php endif; ?> |
| 62 |
</th> |
| 63 |
<td valign="top"><input style="width: 95%" type="text" name="url_from" value="<?php echo esc_attr( $this->url_from ); ?>" id="new"/></td> |
| 64 |
</tr> |
| 65 |
<tr> |
| 66 |
<th width="100" valign="top"> |
| 67 |
<?php if ( strlen( $this->url_notfrom ) > 0 ) : ?> |
| 68 |
<a target="_blank" href="<?php echo esc_url( $this->url_notfrom ) ?>"><?php _e( 'Not matched', 'redirection' ); ?>:</a> |
| 69 |
<?php else : ?> |
| 70 |
<?php _e( 'Not matched', 'redirection' ); ?>: |
| 71 |
<?php endif; ?> |
| 72 |
</th> |
| 73 |
<td valign="top"> |
| 74 |
<input style="width: 95%" type="text" name="url_notfrom" value="<?php echo esc_attr( $this->url_notfrom ); ?>" id="new"/><br/> |
| 75 |
</td> |
| 76 |
</tr> |
| 77 |
<?php endif; |
| 78 |
} |
| 79 |
|
| 80 |
function save( $details ) { |
| 81 |
if ( isset( $details['target'] ) ) |
| 82 |
$details['url_from'] = $this->sanitize_url( $details['target'] ); |
| 83 |
|
| 84 |
return array( |
| 85 |
'url_from' => isset( $details['url_from'] ) ? $this->sanitize_url( $details['url_from'] ) : false, |
| 86 |
'url_notfrom' => isset( $details['url_notfrom'] ) ? $this->sanitize_url( $details['url_notfrom'] ) : false, |
| 87 |
'user_agent' => isset( $details['user_agent'] ) ? $this->sanitize_agent( $details['user_agent'] ) : false, |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
private function sanitize_agent( $agent ) { |
| 92 |
return $this->sanitize_url( $agent ); |
| 93 |
} |
| 94 |
|
| 95 |
function initialize( $url ) { |
| 96 |
$this->url = array( $url, '' ); |
| 97 |
} |
| 98 |
|
| 99 |
function wants_it() { |
| 100 |
// Match referrer |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
function get_target( $url, $matched_url, $regex ) { |
| 105 |
// Check if referrer matches |
| 106 |
if ( preg_match( '@'.str_replace( '@', '\\@', $this->user_agent ).'@i', $_SERVER['HTTP_USER_AGENT'], $matches ) > 0 ) |
| 107 |
return preg_replace( '@'.str_replace( '@', '\\@', $matched_url ).'@', $this->url_from, $url ); |
| 108 |
elseif ( $this->url_notfrom !== '' ) |
| 109 |
return $this->url_notfrom; |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
function match_name() { |
| 114 |
return sprintf( 'user agent - %s', $this->user_agent ); |
| 115 |
} |
| 116 |
} |
| 117 |
|