PluginProbe
Redirection / 4.3.2
Redirection v4.3.2
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 / models / match.php

match.php in Redirection 4.3.2, at models/match.php

192 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 abstract class Red_Match {
4 protected $type;
5
6 public function __construct( $values = '' ) {
7 if ( $values ) {
8 $this->load( $values );
9 }
10 }
11
12 public function get_type() {
13 return $this->type;
14 }
15
16 abstract public function save( array $details, $no_target_url = false );
17 abstract public function name();
18 abstract public function get_target_url( $url, $matched_url, Red_Source_Flags $flag, $is_matched );
19 abstract public function is_match( $url );
20 abstract public function get_data();
21 abstract public function load( $values );
22
23 public function sanitize_url( $url ) {
24 // No new lines
25 $url = preg_replace( "/[\r\n\t].*?$/s", '', $url );
26
27 // Clean control codes
28 $url = preg_replace( '/[^\PC\s]/u', '', $url );
29
30 return $url;
31 }
32
33 protected function get_target_regex_url( $source_url, $target_url, $requested_url, Red_Source_Flags $flags ) {
34 $regex = new Red_Regex( $source_url, $flags->is_ignore_case() );
35
36 return $regex->replace( $target_url, $requested_url );
37 }
38
39 static function create( $name, $data = '' ) {
40 $avail = self::available();
41 if ( isset( $avail[ strtolower( $name ) ] ) ) {
42 $classname = $name . '_match';
43
44 if ( ! class_exists( strtolower( $classname ) ) ) {
45 include( dirname( __FILE__ ) . '/../matches/' . $avail[ strtolower( $name ) ] );
46 }
47
48 $class = new $classname( $data );
49 $class->type = $name;
50 return $class;
51 }
52
53 return false;
54 }
55
56 static function all() {
57 $data = array();
58
59 $avail = self::available();
60 foreach ( $avail as $name => $file ) {
61 $obj = self::create( $name );
62 $data[ $name ] = $obj->name();
63 }
64
65 return $data;
66 }
67
68 static function available() {
69 return array(
70 'url' => 'url.php',
71 'referrer' => 'referrer.php',
72 'agent' => 'user-agent.php',
73 'login' => 'login.php',
74 'header' => 'http-header.php',
75 'custom' => 'custom-filter.php',
76 'cookie' => 'cookie.php',
77 'role' => 'user-role.php',
78 'server' => 'server.php',
79 'ip' => 'ip.php',
80 'page' => 'page.php',
81 );
82 }
83 }
84
85 trait FromUrl_Match {
86 public $url;
87
88 private function save_data( array $details, $no_target_url, array $data ) {
89 if ( $no_target_url === false ) {
90 return array_merge( array(
91 'url' => isset( $details['url'] ) ? $this->sanitize_url( $details['url'] ) : '',
92 ), $data );
93 }
94
95 return $data;
96 }
97
98 public function get_target_url( $requested_url, $source_url, Red_Source_Flags $flags, $matched ) {
99 $target = $this->get_matched_target( $matched );
100
101 if ( $flags->is_regex() && $target ) {
102 return $this->get_target_regex_url( $source_url, $target, $requested_url, $flags );
103 }
104
105 return $target;
106 }
107
108 private function get_matched_target( $matched ) {
109 if ( $matched ) {
110 return $this->url;
111 }
112
113 return false;
114 }
115
116 private function load_data( $values ) {
117 $values = unserialize( $values );
118
119 if ( isset( $values['url'] ) ) {
120 $this->url = $values['url'];
121 }
122
123 return $values;
124 }
125
126 private function get_from_data() {
127 return array(
128 'url' => $this->url,
129 );
130 }
131 }
132
133 trait FromNotFrom_Match {
134 public $url_from = '';
135 public $url_notfrom = '';
136
137 private function save_data( array $details, $no_target_url, array $data ) {
138 if ( $no_target_url === false ) {
139 return array_merge( array(
140 'url_from' => isset( $details['url_from'] ) ? $this->sanitize_url( $details['url_from'] ) : '',
141 'url_notfrom' => isset( $details['url_notfrom'] ) ? $this->sanitize_url( $details['url_notfrom'] ) : '',
142 ), $data );
143 }
144
145 return $data;
146 }
147
148 public function get_target_url( $requested_url, $source_url, Red_Source_Flags $flags, $matched ) {
149 // Action needs a target URL based on whether we matched or not
150 $target = $this->get_matched_target( $matched );
151
152 if ( $flags->is_regex() && $target ) {
153 return $this->get_target_regex_url( $source_url, $target, $requested_url, $flags );
154 }
155
156 return $target;
157 }
158
159 private function get_matched_target( $matched ) {
160 if ( $this->url_from !== '' && $matched ) {
161 return $this->url_from;
162 }
163
164 if ( $this->url_notfrom !== '' && ! $matched ) {
165 return $this->url_notfrom;
166 }
167
168 return false;
169 }
170
171 private function load_data( $values ) {
172 $values = unserialize( $values );
173
174 if ( isset( $values['url_from'] ) ) {
175 $this->url_from = $values['url_from'];
176 }
177
178 if ( isset( $values['url_notfrom'] ) ) {
179 $this->url_notfrom = $values['url_notfrom'];
180 }
181
182 return $values;
183 }
184
185 private function get_from_data() {
186 return array(
187 'url_from' => $this->url_from,
188 'url_notfrom' => $this->url_notfrom,
189 );
190 }
191 }
192