| @@ -3,14 +3,16 @@ | ||
| 3 | 3 | class Red_Apache_File extends Red_FileIO { |
| 4 | 4 | public function force_download() { |
| 5 | 5 | parent::force_download(); |
| 6 | 6 | |
| 7 | + $filename = 'redirection-'.date_i18n( get_option( 'date_format' ) ).'.htaccess'; | |
| 8 | + | |
| 7 | 9 | header( 'Content-Type: application/octet-stream' ); |
| 8 | - header( 'Content-Disposition: attachment; filename="' . $this->export_filename( 'htaccess' ) . '"' ); | |
| 10 | + header( 'Content-Disposition: attachment; filename="'.$filename.'"' ); | |
| 9 | 11 | } |
| 10 | 12 | |
| 11 | 13 | public function get_data( array $items, array $groups ) { |
| 12 | - include_once dirname( dirname( __FILE__ ) ) . '/models/htaccess.php'; | |
| 14 | + include_once dirname( dirname( __FILE__ ) ).'/models/htaccess.php'; | |
| 13 | 15 | |
| 14 | 16 | $htaccess = new Red_Htaccess(); |
| 15 | 17 | |
| 16 | 18 | foreach ( $items as $item ) { |
| @@ -16,167 +18,142 @@ | ||
| 16 | 18 | foreach ( $items as $item ) { |
| 17 | 19 | $htaccess->add( $item ); |
| 18 | 20 | } |
| 19 | 21 | |
| 20 | - return $htaccess->get() . PHP_EOL; | |
| 22 | + return $htaccess->get().PHP_EOL; | |
| 21 | 23 | } |
| 22 | 24 | |
| 23 | 25 | public function load( $group, $filename, $data ) { |
| 24 | 26 | // Remove any comments |
| 27 | + $data = preg_replace( '@#(.*)@', '', $data ); | |
| 25 | 28 | $data = str_replace( "\n", "\r", $data ); |
| 29 | + $data = str_replace( '\\ ', '%20', $data ); | |
| 26 | 30 | |
| 27 | 31 | // Split it into lines |
| 28 | 32 | $lines = array_filter( explode( "\r", $data ) ); |
| 29 | - $count = 0; | |
| 30 | 33 | |
| 31 | - foreach ( (array) $lines as $line ) { | |
| 32 | - $item = $this->get_as_item( $line ); | |
| 33 | - | |
| 34 | - if ( $item ) { | |
| 35 | - $item['group_id'] = $group; | |
| 36 | - $redirect = Red_Item::create( $item ); | |
| 37 | - | |
| 38 | - if ( ! is_wp_error( $redirect ) ) { | |
| 39 | - $count++; | |
| 34 | + $items = array(); | |
| 35 | + if ( count( $lines ) > 0 ) { | |
| 36 | + foreach ( $lines as $line ) { | |
| 37 | + if ( preg_match( '@rewriterule\s+(.*?)\s+(.*?)\s+(\[.*\])*@i', $line, $matches ) > 0 ) { | |
| 38 | + $items[] = array( | |
| 39 | + 'source' => $this->regex_url( $matches[1] ), | |
| 40 | + 'target' => $this->decode_url( $matches[2] ), | |
| 41 | + 'code' => $this->get_code( $matches[3] ), | |
| 42 | + 'regex' => $this->is_regex( $matches[1] ), | |
| 43 | + ); | |
| 40 | 44 | } |
| 45 | + elseif ( preg_match( '@Redirect\s+(.*?)\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) { | |
| 46 | + $items[] = array( | |
| 47 | + 'source' => $this->decode_url( $matches[2] ), | |
| 48 | + 'target' => $this->decode_url( $matches[3] ), | |
| 49 | + 'code' => $this->get_code( $matches[1] ), | |
| 50 | + ); | |
| 51 | + } | |
| 52 | + elseif ( preg_match( '@Redirect\s+(.*?)\s+(.*?)@i', $line, $matches ) > 0 ) { | |
| 53 | + $items[] = array( | |
| 54 | + 'source' => $this->decode_url( $matches[1] ), | |
| 55 | + 'target' => $this->decode_url( $matches[2] ), | |
| 56 | + 'code' => 302, | |
| 57 | + ); | |
| 58 | + } | |
| 59 | + elseif ( preg_match( '@Redirectmatch\s+(.*?)\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) { | |
| 60 | + $items[] = array( | |
| 61 | + 'source' => $this->decode_url( $matches[2] ), | |
| 62 | + 'target' => $this->decode_url( $matches[3] ), | |
| 63 | + 'code' => $this->get_code( $matches[1] ), | |
| 64 | + 'regex' => true, | |
| 65 | + ); | |
| 66 | + } | |
| 67 | + elseif ( preg_match( '@Redirectmatch\s+(.*?)\s+(.*?)@i', $line, $matches ) > 0 ) { | |
| 68 | + $items[] = array( | |
| 69 | + 'source' => $this->decode_url( $matches[1] ), | |
| 70 | + 'target' => $this->decode_url( $matches[2] ), | |
| 71 | + 'code' => 302, | |
| 72 | + 'regex' => true, | |
| 73 | + ); | |
| 74 | + } | |
| 41 | 75 | } |
| 42 | - } | |
| 43 | 76 | |
| 44 | - return $count; | |
| 45 | - } | |
| 77 | + // Add items to group | |
| 78 | + if ( count( $items ) > 0 ) { | |
| 79 | + foreach ( $items as $item ) { | |
| 80 | + $item['group_id'] = $group; | |
| 81 | + $item['action_type'] = 'url'; | |
| 82 | + $item['match_type'] = 'url'; | |
| 46 | 83 | |
| 47 | - public function get_as_item( $line ) { | |
| 48 | - $item = false; | |
| 84 | + if ( $item['code'] === 0 ) { | |
| 85 | + $item['action_type'] = 'pass'; | |
| 86 | + } | |
| 49 | 87 | |
| 50 | - if ( preg_match( '@rewriterule\s+(.*?)\s+(.*?)\s+(\[.*\])*@i', $line, $matches ) > 0 ) { | |
| 51 | - $item = array( | |
| 52 | - 'url' => $this->regex_url( $matches[1] ), | |
| 53 | - 'match_type' => 'url', | |
| 54 | - 'action_type' => 'url', | |
| 55 | - 'action_data' => array( 'url' => $this->decode_url( $matches[2] ) ), | |
| 56 | - 'action_code' => $this->get_code( $matches[3] ), | |
| 57 | - 'regex' => $this->is_regex( $matches[1] ), | |
| 58 | - ); | |
| 59 | - } elseif ( preg_match( '@Redirect\s+(.*?)\s+"(.*?)"\s+(.*)@i', $line, $matches ) > 0 || preg_match( '@Redirect\s+(.*?)\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) { | |
| 60 | - $item = array( | |
| 61 | - 'url' => $this->decode_url( $matches[2] ), | |
| 62 | - 'match_type' => 'url', | |
| 63 | - 'action_type' => 'url', | |
| 64 | - 'action_data' => array( 'url' => $this->decode_url( $matches[3] ) ), | |
| 65 | - 'action_code' => $this->get_code( $matches[1] ), | |
| 66 | - ); | |
| 67 | - } elseif ( preg_match( '@Redirect\s+"(.*?)"\s+(.*)@i', $line, $matches ) > 0 || preg_match( '@Redirect\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) { | |
| 68 | - $item = array( | |
| 69 | - 'url' => $this->decode_url( $matches[1] ), | |
| 70 | - 'match_type' => 'url', | |
| 71 | - 'action_type' => 'url', | |
| 72 | - 'action_data' => array( 'url' => $this->decode_url( $matches[2] ) ), | |
| 73 | - 'action_code' => 302, | |
| 74 | - ); | |
| 75 | - } elseif ( preg_match( '@Redirectmatch\s+(.*?)\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) { | |
| 76 | - $item = array( | |
| 77 | - 'url' => $this->decode_url( $matches[2] ), | |
| 78 | - 'match_type' => 'url', | |
| 79 | - 'action_type' => 'url', | |
| 80 | - 'action_data' => array( 'url' => $this->decode_url( $matches[3] ) ), | |
| 81 | - 'action_code' => $this->get_code( $matches[1] ), | |
| 82 | - 'regex' => true, | |
| 83 | - ); | |
| 84 | - } elseif ( preg_match( '@Redirectmatch\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) { | |
| 85 | - $item = array( | |
| 86 | - 'url' => $this->decode_url( $matches[1] ), | |
| 87 | - 'match_type' => 'url', | |
| 88 | - 'action_type' => 'url', | |
| 89 | - 'action_data' => array( 'url' => $this->decode_url( $matches[2] ) ), | |
| 90 | - 'action_code' => 302, | |
| 91 | - 'regex' => true, | |
| 92 | - ); | |
| 93 | - } | |
| 88 | + Red_Item::create( $item ); | |
| 89 | + } | |
| 94 | 90 | |
| 95 | - if ( $item ) { | |
| 96 | - $item['action_type'] = 'url'; | |
| 97 | - $item['match_type'] = 'url'; | |
| 98 | - | |
| 99 | - if ( $item['action_code'] === 0 ) { | |
| 100 | - $item['action_type'] = 'pass'; | |
| 91 | + return count( $items ); | |
| 101 | 92 | } |
| 102 | - | |
| 103 | - return $item; | |
| 104 | 93 | } |
| 105 | 94 | |
| 106 | - return false; | |
| 95 | + return 0; | |
| 107 | 96 | } |
| 108 | 97 | |
| 109 | - private function decode_url( $url ) { | |
| 98 | + function decode_url( $url ) { | |
| 110 | 99 | $url = rawurldecode( $url ); |
| 111 | - | |
| 112 | - // Replace quoted slashes | |
| 113 | - $url = preg_replace( '@\\\/@', '/', $url ); | |
| 114 | - | |
| 115 | - // Ensure escaped '.' is still escaped | |
| 116 | - $url = preg_replace( '@\\\\.@', '\\\\.', $url ); | |
| 100 | + $url = str_replace( '\\.', '.', $url ); | |
| 117 | 101 | return $url; |
| 118 | 102 | } |
| 119 | 103 | |
| 120 | - private function is_str_regex( $url ) { | |
| 104 | + function is_str_regex( $url ) { | |
| 121 | 105 | $regex = '()[]$^?+.'; |
| 122 | 106 | $escape = false; |
| 123 | 107 | |
| 124 | 108 | for ( $x = 0; $x < strlen( $url ); $x++ ) { |
| 125 | - $escape = false; | |
| 126 | - | |
| 127 | - if ( $url{$x} === '\\' ) { | |
| 109 | + if ( $url{$x} === '\\' ) | |
| 128 | 110 | $escape = true; |
| 129 | - } elseif ( strpos( $regex, $url{$x} ) !== false && ! $escape ) { | |
| 111 | + elseif ( strpos( $regex, $url{$x} ) !== false && ! $escape ) | |
| 130 | 112 | return true; |
| 131 | - } | |
| 113 | + else | |
| 114 | + $escape = false; | |
| 132 | 115 | } |
| 133 | 116 | |
| 134 | 117 | return false; |
| 135 | 118 | } |
| 136 | 119 | |
| 137 | - private function is_regex( $url ) { | |
| 120 | + function is_regex( $url ) { | |
| 138 | 121 | if ( $this->is_str_regex( $url ) ) { |
| 139 | 122 | $tmp = ltrim( $url, '^' ); |
| 140 | 123 | $tmp = rtrim( $tmp, '$' ); |
| 141 | 124 | |
| 142 | - if ( $this->is_str_regex( $tmp ) ) { | |
| 125 | + if ( $this->is_str_regex( $tmp ) ) | |
| 143 | 126 | return true; |
| 144 | - } | |
| 145 | 127 | } |
| 146 | 128 | |
| 147 | 129 | return false; |
| 148 | 130 | } |
| 149 | 131 | |
| 150 | - private function regex_url( $url ) { | |
| 151 | - $url = $this->decode_url( $url ); | |
| 152 | - | |
| 132 | + function regex_url ($url) { | |
| 153 | 133 | if ( $this->is_str_regex( $url ) ) { |
| 154 | 134 | $tmp = ltrim( $url, '^' ); |
| 155 | 135 | $tmp = rtrim( $tmp, '$' ); |
| 156 | 136 | |
| 157 | - if ( $this->is_str_regex( $tmp ) ) { | |
| 158 | - return '^/' . ltrim( $tmp, '/' ); | |
| 159 | - } | |
| 137 | + if ( $this->is_str_regex( $tmp ) === false ) | |
| 138 | + return '/'.$this->decode_url( $tmp ); | |
| 160 | 139 | |
| 161 | - return '/' . ltrim( $tmp, '/' ); | |
| 140 | + return '/'.$this->decode_url( $url ); | |
| 162 | 141 | } |
| 163 | 142 | |
| 164 | 143 | return $this->decode_url( $url ); |
| 165 | 144 | } |
| 166 | 145 | |
| 167 | - private function get_code( $code ) { | |
| 168 | - if ( strpos( $code, '301' ) !== false || stripos( $code, 'permanent' ) !== false ) { | |
| 146 | + function get_code ($code) { | |
| 147 | + if ( strpos( $code, '301' ) !== false || stripos( $code, 'permanent' ) !== false ) | |
| 169 | 148 | return 301; |
| 170 | - } elseif ( strpos( $code, '302' ) !== false ) { | |
| 149 | + elseif ( strpos( $code, '302' ) !== false ) | |
| 171 | 150 | return 302; |
| 172 | - } elseif ( strpos( $code, '307' ) !== false || stripos( $code, 'seeother' ) !== false ) { | |
| 151 | + elseif ( strpos( $code, '307' ) !== false || stripos( $code, 'seeother' ) !== false ) | |
| 173 | 152 | return 307; |
| 174 | - } elseif ( strpos( $code, '404' ) !== false || stripos( $code, 'forbidden' ) !== false || strpos( $code, 'F' ) !== false ) { | |
| 153 | + elseif ( strpos( $code, '404' ) !== false || stripos( $code, 'forbidden' ) !== false || strpos( $code, 'F' ) !== false ) | |
| 175 | 154 | return 404; |
| 176 | - } elseif ( strpos( $code, '410' ) !== false || stripos( $code, 'gone' ) !== false || strpos( $code, 'G' ) !== false ) { | |
| 155 | + elseif ( strpos( $code, '410' ) !== false || stripos( $code, 'gone' ) !== false || strpos( $code, 'G' ) !== false ) | |
| 177 | 156 | return 410; |
| 178 | - } | |
| 179 | - | |
| 180 | - return 302; | |
| 157 | + return 0; | |
| 181 | 158 | } |
| 182 | 159 | } |