| 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, $matched_url, $regex ); |
| 19 |
abstract public function get_data(); |
| 20 |
abstract public function load( $values ); |
| 21 |
|
| 22 |
public function sanitize_url( $url ) { |
| 23 |
// No new lines |
| 24 |
$url = preg_replace( "/[\r\n\t].*?$/s", '', $url ); |
| 25 |
|
| 26 |
// Clean control codes |
| 27 |
$url = preg_replace( '/[^\PC\s]/u', '', $url ); |
| 28 |
|
| 29 |
return $url; |
| 30 |
} |
| 31 |
|
| 32 |
protected function get_target_regex_url( $matched_url, $target, $url ) { |
| 33 |
return preg_replace( '@' . str_replace( '@', '\\@', $matched_url ) . '@', $target, $url ); |
| 34 |
} |
| 35 |
|
| 36 |
static function create( $name, $data = '' ) { |
| 37 |
$avail = self::available(); |
| 38 |
if ( isset( $avail[ strtolower( $name ) ] ) ) { |
| 39 |
$classname = $name . '_match'; |
| 40 |
|
| 41 |
if ( ! class_exists( strtolower( $classname ) ) ) { |
| 42 |
include( dirname( __FILE__ ) . '/../matches/' . $avail[ strtolower( $name ) ] ); |
| 43 |
} |
| 44 |
|
| 45 |
$class = new $classname( $data ); |
| 46 |
$class->type = $name; |
| 47 |
return $class; |
| 48 |
} |
| 49 |
|
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
static function all() { |
| 54 |
$data = array(); |
| 55 |
|
| 56 |
$avail = self::available(); |
| 57 |
foreach ( $avail as $name => $file ) { |
| 58 |
$obj = self::create( $name ); |
| 59 |
$data[ $name ] = $obj->name(); |
| 60 |
} |
| 61 |
|
| 62 |
return $data; |
| 63 |
} |
| 64 |
|
| 65 |
static function available() { |
| 66 |
return array( |
| 67 |
'url' => 'url.php', |
| 68 |
'referrer' => 'referrer.php', |
| 69 |
'agent' => 'user-agent.php', |
| 70 |
'login' => 'login.php', |
| 71 |
'header' => 'http-header.php', |
| 72 |
'custom' => 'custom-filter.php', |
| 73 |
'cookie' => 'cookie.php', |
| 74 |
'role' => 'user-role.php', |
| 75 |
'server' => 'server.php', |
| 76 |
'ip' => 'ip.php', |
| 77 |
'page' => 'page.php', |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
trait FromUrl_Match { |
| 83 |
public $url; |
| 84 |
|
| 85 |
private function save_data( array $details, $no_target_url, array $data ) { |
| 86 |
if ( $no_target_url === false ) { |
| 87 |
return array_merge( array( |
| 88 |
'url' => isset( $details['url'] ) ? $this->sanitize_url( $details['url'] ) : '', |
| 89 |
), $data ); |
| 90 |
} |
| 91 |
|
| 92 |
return $data; |
| 93 |
} |
| 94 |
|
| 95 |
private function get_matched_target( $matched ) { |
| 96 |
if ( $matched ) { |
| 97 |
return $this->url; |
| 98 |
} |
| 99 |
|
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
private function load_data( $values ) { |
| 104 |
$values = unserialize( $values ); |
| 105 |
|
| 106 |
if ( isset( $values['url'] ) ) { |
| 107 |
$this->url = $values['url']; |
| 108 |
} |
| 109 |
|
| 110 |
return $values; |
| 111 |
} |
| 112 |
|
| 113 |
private function get_from_data() { |
| 114 |
return array( |
| 115 |
'url' => $this->url, |
| 116 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
trait FromNotFrom_Match { |
| 121 |
public $url_from; |
| 122 |
public $url_notfrom; |
| 123 |
|
| 124 |
private function save_data( array $details, $no_target_url, array $data ) { |
| 125 |
if ( $no_target_url === false ) { |
| 126 |
return array_merge( array( |
| 127 |
'url_from' => isset( $details['url_from'] ) ? $this->sanitize_url( $details['url_from'] ) : '', |
| 128 |
'url_notfrom' => isset( $details['url_notfrom'] ) ? $this->sanitize_url( $details['url_notfrom'] ) : '', |
| 129 |
), $data ); |
| 130 |
} |
| 131 |
|
| 132 |
return $data; |
| 133 |
} |
| 134 |
|
| 135 |
private function get_matched_target( $matched ) { |
| 136 |
if ( $this->url_from !== '' && $matched ) { |
| 137 |
return $this->url_from; |
| 138 |
} |
| 139 |
|
| 140 |
if ( $this->url_notfrom !== '' && ! $matched ) { |
| 141 |
return $this->url_notfrom; |
| 142 |
} |
| 143 |
|
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
private function load_data( $values ) { |
| 148 |
$values = unserialize( $values ); |
| 149 |
|
| 150 |
if ( isset( $values['url_from'] ) ) { |
| 151 |
$this->url_from = $values['url_from']; |
| 152 |
} |
| 153 |
|
| 154 |
if ( isset( $values['url_notfrom'] ) ) { |
| 155 |
$this->url_notfrom = $values['url_notfrom']; |
| 156 |
} |
| 157 |
|
| 158 |
return $values; |
| 159 |
} |
| 160 |
|
| 161 |
private function get_from_data() { |
| 162 |
return array( |
| 163 |
'url_from' => $this->url_from, |
| 164 |
'url_notfrom' => $this->url_notfrom, |
| 165 |
); |
| 166 |
} |
| 167 |
} |
| 168 |
|