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 / http-header.php

http-header.php in Redirection 4.0, at matches/http-header.php

90 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Header_Match extends Red_Match {
4 public $name;
5 public $value;
6 public $regex;
7 public $url_from;
8 public $url_notfrom;
9
10 function name() {
11 return __( 'URL and HTTP header', 'redirection' );
12 }
13
14 public function save( array $details, $no_target_url = false ) {
15 $data = array(
16 'regex' => isset( $details['regex'] ) && $details['regex'] ? true : false,
17 'name' => isset( $details['name'] ) ? $this->sanitize_name( $details['name'] ) : '',
18 'value' => isset( $details['value'] ) ? $this->sanitize_value( $details['value'] ) : '',
19 );
20
21 if ( $no_target_url === false ) {
22 $data['url_from'] = isset( $details['url_from'] ) ? $this->sanitize_url( $details['url_from'] ) : '';
23 $data['url_notfrom'] = isset( $details['url_notfrom'] ) ? $this->sanitize_url( $details['url_notfrom'] ) : '';
24 }
25
26 return $data;
27 }
28
29 public function sanitize_name( $name ) {
30 $name = $this->sanitize_url( $name );
31 $name = str_replace( ' ', '', $name );
32 $name = preg_replace( '/[^A-Za-z0-9\-_]/', '', $name );
33
34 return trim( trim( $name, ':' ) );
35 }
36
37 public function sanitize_value( $value ) {
38 return $this->sanitize_url( $value );
39 }
40
41 function get_target( $requested_url, $source_url, Red_Source_Flags $flags ) {
42 $target = false;
43 $matched = Redirection_Request::get_header( $this->name ) === $this->value;
44
45 if ( $this->regex ) {
46 $regex = new Red_Regex( $this->value, true );
47 $matched = $regex->is_match( Redirection_Request::get_header( $this->name ) );
48 }
49
50 // Check if referrer matches
51 if ( $matched && $this->url_from !== '' ) {
52 $target = $this->url_from;
53 } elseif ( ! $matched && $this->url_notfrom !== '' ) {
54 $target = $this->url_notfrom;
55 }
56
57 if ( $flags->is_regex() && $target ) {
58 $target = $this->get_target_regex_url( $source_url, $target, $requested_url, $flags );
59 }
60
61 return $target;
62 }
63
64 public function get_data() {
65 return array(
66 'url_from' => $this->url_from,
67 'url_notfrom' => $this->url_notfrom,
68 'regex' => $this->regex,
69 'name' => $this->name,
70 'value' => $this->value,
71 );
72 }
73
74 public function load( $values ) {
75 $values = unserialize( $values );
76
77 if ( isset( $values['url_from'] ) ) {
78 $this->url_from = $values['url_from'];
79 }
80
81 if ( isset( $values['url_notfrom'] ) ) {
82 $this->url_notfrom = $values['url_notfrom'];
83 }
84
85 $this->regex = $values['regex'];
86 $this->name = $values['name'];
87 $this->value = $values['value'];
88 }
89 }
90