PluginProbe
Redirection / 4.0
Redirection v4.0
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / matches / user-agent.php

user-agent.php in Redirection 4.0, at matches/user-agent.php

75 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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