PluginProbe
Redirection / 5.0
Redirection v5.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 / models / match.php

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

187 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 // No new lines
95 $url = preg_replace( "/[\r\n\t].*?$/s", '', $url );
96
97 // Clean control codes
98 $url = preg_replace( '/[^\PC\s]/u', '', $url );
99
100 return $url;
101 }
102
103 /**
104 * Apply a regular expression to the target URL, replacing any values.
105 *
106 * @param string $source_url Redirect source URL.
107 * @param string $target_url Target URL.
108 * @param string $requested_url The URL being requested (decoded).
109 * @param Red_Source_Flags $flags Source URL flags.
110 * @return string
111 */
112 protected function get_target_regex_url( $source_url, $target_url, $requested_url, Red_Source_Flags $flags ) {
113 $regex = new Red_Regex( $source_url, $flags->is_ignore_case() );
114
115 return $regex->replace( $target_url, $requested_url );
116 }
117
118 /**
119 * Create a Red_Match object, given a type
120 *
121 * @param string $name Match type.
122 * @param string $data Match data.
123 * @return Red_Match|null
124 */
125 public static function create( $name, $data = '' ) {
126 $avail = self::available();
127 if ( isset( $avail[ strtolower( $name ) ] ) ) {
128 $classname = $name . '_match';
129
130 if ( ! class_exists( strtolower( $classname ) ) ) {
131 include dirname( __FILE__ ) . '/../matches/' . $avail[ strtolower( $name ) ];
132 }
133
134 /**
135 * @var Red_Match
136 */
137 $class = new $classname( $data );
138 $class->type = $name;
139 return $class;
140 }
141
142 return null;
143 }
144
145 /**
146 * Get all Red_Match objects
147 *
148 * @return String[]
149 */
150 public static function all() {
151 $data = [];
152
153 $avail = self::available();
154 foreach ( array_keys( $avail ) as $name ) {
155 /**
156 * @var Red_Match
157 */
158 $obj = self::create( $name );
159 $data[ $name ] = $obj->name();
160 }
161
162 return $data;
163 }
164
165 /**
166 * Get list of available matches
167 *
168 * @return array
169 */
170 public static function available() {
171 return [
172 'url' => 'url.php',
173 'referrer' => 'referrer.php',
174 'agent' => 'user-agent.php',
175 'login' => 'login.php',
176 'header' => 'http-header.php',
177 'custom' => 'custom-filter.php',
178 'cookie' => 'cookie.php',
179 'role' => 'user-role.php',
180 'server' => 'server.php',
181 'ip' => 'ip.php',
182 'page' => 'page.php',
183 'language' => 'language.php',
184 ];
185 }
186 }
187