PluginProbe
Redirection / 5.3.7
Redirection v5.3.7
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 5.3.7, at models/match.php

189 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require_once dirname( __DIR__ ) . '/matches/from-notfrom.php';
4 require_once dirname( __DIR__ ) . '/matches/from-url.php';
5
6 /**
7 * Matches a URL and some other condition
8 */
9 abstract class Red_Match {
10 /**
11 * Match type
12 *
13 * @var string
14 */
15 protected $type = '';
16
17 /**
18 * Constructor
19 *
20 * @param string $values Initial values.
21 */
22 public function __construct( $values = '' ) {
23 if ( $values ) {
24 $this->load( $values );
25 }
26 }
27
28 /**
29 * Get match type
30 *
31 * @return string
32 */
33 public function get_type() {
34 return $this->type;
35 }
36
37 /**
38 * Save the match
39 *
40 * @param array $details Details to save.
41 * @param boolean $no_target_url The URL when no target.
42 * @return array|null
43 */
44 abstract public function save( array $details, $no_target_url = false );
45
46 /**
47 * Get the match name
48 *
49 * @return String
50 */
51 abstract public function name();
52
53 /**
54 * Match the URL against the specific matcher conditions
55 *
56 * @param String $url Requested URL.
57 * @return boolean
58 */
59 abstract public function is_match( $url );
60
61 /**
62 * Get the target URL for this match. Some matches may have a matched/unmatched target.
63 *
64 * @param String $original_url The client URL (not decoded).
65 * @param String $matched_url The URL in the redirect.
66 * @param Red_Source_Flags $flag Source flags.
67 * @param boolean $is_matched Was the match successful.
68 * @return String|false
69 */
70 abstract public function get_target_url( $original_url, $matched_url, Red_Source_Flags $flag, $is_matched );
71
72 /**
73 * Get the match data
74 *
75 * @return array|null
76 */
77 abstract public function get_data();
78
79 /**
80 * Load the match data into this instance.
81 *
82 * @param String $values Match values, as read from the database (plain text or serialized PHP).
83 * @return void
84 */
85 abstract public function load( $values );
86
87 /**
88 * Sanitize a match URL
89 *
90 * @param String $url URL.
91 * @return String
92 */
93 public function sanitize_url( $url ) {
94 $url = sanitize_text_field( $url );
95
96 // No new lines
97 $url = preg_replace( "/[\r\n\t].*?$/s", '', $url );
98
99 // Clean control codes
100 $url = preg_replace( '/[^\PC\s]/u', '', $url );
101
102 return $url;
103 }
104
105 /**
106 * Apply a regular expression to the target URL, replacing any values.
107 *
108 * @param string $source_url Redirect source URL.
109 * @param string $target_url Target URL.
110 * @param string $requested_url The URL being requested (decoded).
111 * @param Red_Source_Flags $flags Source URL flags.
112 * @return string
113 */
114 protected function get_target_regex_url( $source_url, $target_url, $requested_url, Red_Source_Flags $flags ) {
115 $regex = new Red_Regex( $source_url, $flags->is_ignore_case() );
116
117 return $regex->replace( $target_url, $requested_url );
118 }
119
120 /**
121 * Create a Red_Match object, given a type
122 *
123 * @param string $name Match type.
124 * @param string $data Match data.
125 * @return Red_Match|null
126 */
127 public static function create( $name, $data = '' ) {
128 $avail = self::available();
129 if ( isset( $avail[ strtolower( $name ) ] ) ) {
130 $classname = $name . '_match';
131
132 if ( ! class_exists( strtolower( $classname ) ) ) {
133 include dirname( __FILE__ ) . '/../matches/' . $avail[ strtolower( $name ) ];
134 }
135
136 /**
137 * @var Red_Match
138 */
139 $class = new $classname( $data );
140 $class->type = $name;
141 return $class;
142 }
143
144 return null;
145 }
146
147 /**
148 * Get all Red_Match objects
149 *
150 * @return String[]
151 */
152 public static function all() {
153 $data = [];
154
155 $avail = self::available();
156 foreach ( array_keys( $avail ) as $name ) {
157 /**
158 * @var Red_Match
159 */
160 $obj = self::create( $name );
161 $data[ $name ] = $obj->name();
162 }
163
164 return $data;
165 }
166
167 /**
168 * Get list of available matches
169 *
170 * @return array
171 */
172 public static function available() {
173 return [
174 'url' => 'url.php',
175 'referrer' => 'referrer.php',
176 'agent' => 'user-agent.php',
177 'login' => 'login.php',
178 'header' => 'http-header.php',
179 'custom' => 'custom-filter.php',
180 'cookie' => 'cookie.php',
181 'role' => 'user-role.php',
182 'server' => 'server.php',
183 'ip' => 'ip.php',
184 'page' => 'page.php',
185 'language' => 'language.php',
186 ];
187 }
188 }
189