| 1 |
<?php |
| 2 |
|
| 3 |
class Referrer_Match extends Red_Match { |
| 4 |
public $referrer; |
| 5 |
public $regex; |
| 6 |
|
| 7 |
function name() { |
| 8 |
return __( 'URL and referrer', 'redirection' ); |
| 9 |
} |
| 10 |
|
| 11 |
function show() { |
| 12 |
$codes = array( |
| 13 |
301 => get_status_header_desc( 301 ), |
| 14 |
302 => get_status_header_desc( 302 ), |
| 15 |
307 => get_status_header_desc( 307 ), |
| 16 |
308 => get_status_header_desc( 308 ), |
| 17 |
); |
| 18 |
|
| 19 |
?> |
| 20 |
<tr> |
| 21 |
<th width="100"><?php _e( 'Referrer', 'redirection' ); ?>:</th> |
| 22 |
<td valign="top"> |
| 23 |
<input style="width: 85%" type="text" name="referrer" value="<?php echo esc_attr( $this->referrer ); ?>"/> |
| 24 |
<label><?php _e( 'Regex', 'redirection' ); ?>: <input type="checkbox" name="regex" <?php if ( $this->regex === true ) echo ' checked="checked"' ?>/></label> |
| 25 |
</td> |
| 26 |
</tr> |
| 27 |
<tr> |
| 28 |
<th><?php _e( 'HTTP Code', 'redirection' ); ?>:</th> |
| 29 |
<td> |
| 30 |
<select name="action_code"> |
| 31 |
<?php foreach ( $codes as $key => $code ) : ?> |
| 32 |
<option value="<?php echo $key ?>"<?php if ( $key === intval( $this->action_code ) ) echo ' selected="selected"' ?>><?php printf( '%s - %s', $key, $code ) ?></option> |
| 33 |
<?php endforeach?> |
| 34 |
</select> |
| 35 |
</td> |
| 36 |
</tr> |
| 37 |
<tr> |
| 38 |
<th><?php _e( 'HTTP Code', 'redirection' ); ?>:</th> |
| 39 |
<td> |
| 40 |
<p style="padding: 0.5em"><?php _e( 'The visitor will be redirected from the source URL if the referrer 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.', 'redirection' ); ?></p> |
| 41 |
</td> |
| 42 |
</tr> |
| 43 |
<tr> |
| 44 |
<th width="100" valign="top"> |
| 45 |
<?php if ( strlen( $this->url_from ) > 0 ) : ?> |
| 46 |
<a target="_blank" href="<?php echo esc_url( $this->url_from ) ?>"><?php _e( 'Matched', 'redirection' ); ?>:</a> |
| 47 |
<?php else : ?> |
| 48 |
<?php _e( 'Matched', 'redirection' ); ?>: |
| 49 |
<?php endif; ?> |
| 50 |
</th> |
| 51 |
<td valign="top"><input style="width: 95%" type="text" name="url_from" value="<?php echo esc_attr( $this->url_from ); ?>" id="new"/></td> |
| 52 |
</tr> |
| 53 |
<tr> |
| 54 |
<th width="100" valign="top"> |
| 55 |
<?php if ( strlen( $this->url_notfrom ) > 0 ) : ?> |
| 56 |
<a target="_blank" href="<?php echo $this->url_notfrom ?>"><?php _e( 'Not matched', 'redirection' ); ?>:</a> |
| 57 |
<?php else : ?> |
| 58 |
<?php _e( 'Not matched', 'redirection' ); ?>: |
| 59 |
<?php endif; ?> |
| 60 |
</th> |
| 61 |
<td valign="top"> |
| 62 |
<input style="width: 95%" type="text" name="url_notfrom" value="<?php echo esc_attr( $this->url_notfrom ); ?>" id="new"/><br/> |
| 63 |
</td> |
| 64 |
</tr> |
| 65 |
<?php |
| 66 |
} |
| 67 |
|
| 68 |
function save( $details ) { |
| 69 |
if ( isset( $details['target'] ) ) |
| 70 |
$details['url_from'] = $this->sanitize_url( $details['target'] ); |
| 71 |
|
| 72 |
return array( |
| 73 |
'url_from' => $this->sanitize_url( $details['url_from'] ), |
| 74 |
'url_notfrom' => isset( $details['url_notfrom'] ) ? $this->sanitize_url( $details['url_notfrom'] ) : false, |
| 75 |
'regex' => isset( $details['regex'] ) ? true : false, |
| 76 |
'referrer' => isset( $details['referrer'] ) ? $this->sanitize_referrer( $details['referrer'] ) : false, |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
public function sanitize_referrer( $agent ) { |
| 81 |
return $this->sanitize_url( $agent ); |
| 82 |
} |
| 83 |
|
| 84 |
function initialize( $url ) { |
| 85 |
$this->url = array( $url, '' ); |
| 86 |
} |
| 87 |
|
| 88 |
function wants_it() { |
| 89 |
// Match referrer |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
function get_target( $url, $matched_url, $regex ) { |
| 94 |
$target = false; |
| 95 |
|
| 96 |
// Check if referrer matches |
| 97 |
if ( ( $this->regex === false && $_SERVER['HTTP_REFERER'] === $this->referrer ) || ( $this->regex === true && preg_match( '@'.str_replace( '@', '\\@', $this->referrer ).'@', $_SERVER['HTTP_REFERER'], $matches ) ) ) { |
| 98 |
$target = $this->url_from; |
| 99 |
|
| 100 |
if ( $regex ) |
| 101 |
$target = preg_replace( '@'.str_replace( '@', '\\@', $matched_url ).'@', $target, $url ); |
| 102 |
} |
| 103 |
elseif ( $this->url_notfrom !== '' ) |
| 104 |
$target = $this->url_notfrom; |
| 105 |
return $target; |
| 106 |
} |
| 107 |
|
| 108 |
function match_name() { |
| 109 |
return sprintf( 'referrer - <code>%s</code>', $this->referrer ); |
| 110 |
} |
| 111 |
} |
| 112 |
|