| 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 |
public 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 |
public 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 |
public 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 |
'language' => 'language.php', |
| 82 |
); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
trait FromUrl_Match { |
| 87 |
public $url; |
| 88 |
|
| 89 |
private function save_data( array $details, $no_target_url, array $data ) { |
| 90 |
if ( $no_target_url === false ) { |
| 91 |
return array_merge( array( |
| 92 |
'url' => isset( $details['url'] ) ? $this->sanitize_url( $details['url'] ) : '', |
| 93 |
), $data ); |
| 94 |
} |
| 95 |
|
| 96 |
return $data; |
| 97 |
} |
| 98 |
|
| 99 |
public function get_target_url( $requested_url, $source_url, Red_Source_Flags $flags, $matched ) { |
| 100 |
$target = $this->get_matched_target( $matched ); |
| 101 |
|
| 102 |
if ( $flags->is_regex() && $target ) { |
| 103 |
return $this->get_target_regex_url( $source_url, $target, $requested_url, $flags ); |
| 104 |
} |
| 105 |
|
| 106 |
return $target; |
| 107 |
} |
| 108 |
|
| 109 |
private function get_matched_target( $matched ) { |
| 110 |
if ( $matched ) { |
| 111 |
return $this->url; |
| 112 |
} |
| 113 |
|
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
private function load_data( $values ) { |
| 118 |
$values = unserialize( $values ); |
| 119 |
|
| 120 |
if ( isset( $values['url'] ) ) { |
| 121 |
$this->url = $values['url']; |
| 122 |
} |
| 123 |
|
| 124 |
return $values; |
| 125 |
} |
| 126 |
|
| 127 |
private function get_from_data() { |
| 128 |
return array( |
| 129 |
'url' => $this->url, |
| 130 |
); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
trait FromNotFrom_Match { |
| 135 |
public $url_from = ''; |
| 136 |
public $url_notfrom = ''; |
| 137 |
|
| 138 |
private function save_data( array $details, $no_target_url, array $data ) { |
| 139 |
if ( $no_target_url === false ) { |
| 140 |
return array_merge( array( |
| 141 |
'url_from' => isset( $details['url_from'] ) ? $this->sanitize_url( $details['url_from'] ) : '', |
| 142 |
'url_notfrom' => isset( $details['url_notfrom'] ) ? $this->sanitize_url( $details['url_notfrom'] ) : '', |
| 143 |
), $data ); |
| 144 |
} |
| 145 |
|
| 146 |
return $data; |
| 147 |
} |
| 148 |
|
| 149 |
public function get_target_url( $requested_url, $source_url, Red_Source_Flags $flags, $matched ) { |
| 150 |
// Action needs a target URL based on whether we matched or not |
| 151 |
$target = $this->get_matched_target( $matched ); |
| 152 |
|
| 153 |
if ( $flags->is_regex() && $target ) { |
| 154 |
return $this->get_target_regex_url( $source_url, $target, $requested_url, $flags ); |
| 155 |
} |
| 156 |
|
| 157 |
return $target; |
| 158 |
} |
| 159 |
|
| 160 |
private function get_matched_target( $matched ) { |
| 161 |
if ( $this->url_from !== '' && $matched ) { |
| 162 |
return $this->url_from; |
| 163 |
} |
| 164 |
|
| 165 |
if ( $this->url_notfrom !== '' && ! $matched ) { |
| 166 |
return $this->url_notfrom; |
| 167 |
} |
| 168 |
|
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
private function load_data( $values ) { |
| 173 |
$values = @unserialize( $values ); |
| 174 |
|
| 175 |
if ( isset( $values['url_from'] ) ) { |
| 176 |
$this->url_from = $values['url_from']; |
| 177 |
} |
| 178 |
|
| 179 |
if ( isset( $values['url_notfrom'] ) ) { |
| 180 |
$this->url_notfrom = $values['url_notfrom']; |
| 181 |
} |
| 182 |
|
| 183 |
return $values; |
| 184 |
} |
| 185 |
|
| 186 |
private function get_from_data() { |
| 187 |
return array( |
| 188 |
'url_from' => $this->url_from, |
| 189 |
'url_notfrom' => $this->url_notfrom, |
| 190 |
); |
| 191 |
} |
| 192 |
} |
| 193 |
|