| @@ -1,25 +1,26 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | abstract class Red_Match { |
| 4 | - protected $type; | |
| 4 | + public $url; | |
| 5 | 5 | |
| 6 | 6 | public function __construct( $values = '' ) { |
| 7 | 7 | if ( $values ) { |
| 8 | - $this->load( $values ); | |
| 8 | + $this->url = $values; | |
| 9 | + | |
| 10 | + $obj = maybe_unserialize( $values ); | |
| 11 | + | |
| 12 | + if ( is_array( $obj ) ) { | |
| 13 | + foreach ( $obj as $key => $value ) { | |
| 14 | + $this->$key = $value; | |
| 15 | + } | |
| 16 | + } | |
| 9 | 17 | } |
| 10 | 18 | } |
| 11 | 19 | |
| 12 | - public function get_type() { | |
| 13 | - return $this->type; | |
| 14 | - } | |
| 15 | - | |
| 16 | 20 | abstract public function save( array $details, $no_target_url = false ); |
| 17 | 21 | 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 | + abstract public function get_target( $url, $matched_url, $regex ); | |
| 22 | 23 | |
| 23 | 24 | public function sanitize_url( $url ) { |
| 24 | 25 | // No new lines |
| 25 | 26 | $url = preg_replace( "/[\r\n\t].*?$/s", '', $url ); |
| @@ -29,26 +30,16 @@ | ||
| 29 | 30 | |
| 30 | 31 | return $url; |
| 31 | 32 | } |
| 32 | 33 | |
| 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 | 34 | static function create( $name, $data = '' ) { |
| 40 | 35 | $avail = self::available(); |
| 41 | 36 | if ( isset( $avail[ strtolower( $name ) ] ) ) { |
| 42 | - $classname = $name . '_match'; | |
| 37 | + $classname = $name.'_match'; | |
| 43 | 38 | |
| 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; | |
| 39 | + if ( ! class_exists( strtolower( $classname ) ) ) | |
| 40 | + include( dirname( __FILE__ ).'/../matches/'.$avail[ strtolower( $name ) ] ); | |
| 41 | + return new $classname( $data ); | |
| 51 | 42 | } |
| 52 | 43 | |
| 53 | 44 | return false; |
| 54 | 45 | } |
| @@ -65,127 +56,12 @@ | ||
| 65 | 56 | return $data; |
| 66 | 57 | } |
| 67 | 58 | |
| 68 | 59 | static function available() { |
| 69 | - return array( | |
| 60 | + return array( | |
| 70 | 61 | 'url' => 'url.php', |
| 71 | 62 | 'referrer' => 'referrer.php', |
| 72 | 63 | 'agent' => 'user-agent.php', |
| 73 | 64 | '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 | - ); | |
| 65 | + ); | |
| 190 | 66 | } |
| 191 | 67 | } |