| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Nginx_File extends Red_FileIO { |
| 4 |
public function force_download() { |
| 5 |
parent::force_download(); |
| 6 |
|
| 7 |
header( 'Content-Type: application/octet-stream' ); |
| 8 |
header( 'Content-Disposition: attachment; filename="' . $this->export_filename( 'nginx' ) . '"' ); |
| 9 |
} |
| 10 |
|
| 11 |
public function get_data( array $items, array $groups ) { |
| 12 |
$lines = array(); |
| 13 |
$version = red_get_plugin_data( dirname( dirname( __FILE__ ) ) . '/redirection.php' ); |
| 14 |
|
| 15 |
$lines[] = '# Created by Redirection'; |
| 16 |
$lines[] = '# ' . date( 'r' ); |
| 17 |
$lines[] = '# Redirection ' . trim( $version['Version'] ) . ' - https://redirection.me'; |
| 18 |
$lines[] = ''; |
| 19 |
$lines[] = 'server {'; |
| 20 |
|
| 21 |
$parts = array(); |
| 22 |
foreach ( $items as $item ) { |
| 23 |
if ( $item->is_enabled() ) { |
| 24 |
$parts[] = $this->get_nginx_item( $item ); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
$lines = array_merge( $lines, array_filter( $parts ) ); |
| 29 |
|
| 30 |
$lines[] = '}'; |
| 31 |
$lines[] = ''; |
| 32 |
$lines[] = '# End of Redirection'; |
| 33 |
|
| 34 |
return implode( PHP_EOL, $lines ) . PHP_EOL; |
| 35 |
} |
| 36 |
|
| 37 |
private function get_redirect_code( Red_Item $item ) { |
| 38 |
if ( $item->get_action_code() === 301 ) { |
| 39 |
return 'permanent'; |
| 40 |
} |
| 41 |
return 'redirect'; |
| 42 |
} |
| 43 |
|
| 44 |
function load( $group, $data, $filename = '' ) { |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
private function get_nginx_item( Red_Item $item ) { |
| 49 |
$target = 'add_' . $item->get_match_type(); |
| 50 |
|
| 51 |
if ( method_exists( $this, $target ) ) { |
| 52 |
return ' ' . $this->$target( $item, $item->get_match_data() ); |
| 53 |
} |
| 54 |
|
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
private function add_url( Red_Item $item, array $match_data ) { |
| 59 |
return $this->get_redirect( $item->get_url(), $item->get_action_data(), $this->get_redirect_code( $item ), $match_data['source'] ); |
| 60 |
} |
| 61 |
|
| 62 |
private function add_agent( Red_Item $item, array $match_data ) { |
| 63 |
if ( $item->match->url_from ) { |
| 64 |
$lines[] = 'if ( $http_user_agent ~* ^' . $item->match->user_agent . '$ ) {'; |
| 65 |
$lines[] = ' ' . $this->get_redirect( $item->get_url(), $item->match->url_from, $this->get_redirect_code( $item ), $match_data['source'] ); |
| 66 |
$lines[] = ' }'; |
| 67 |
} |
| 68 |
|
| 69 |
if ( $item->match->url_notfrom ) { |
| 70 |
$lines[] = 'if ( $http_user_agent !~* ^' . $item->match->user_agent . '$ ) {'; |
| 71 |
$lines[] = ' ' . $this->get_redirect( $item->get_url(), $item->match->url_notfrom, $this->get_redirect_code( $item ), $match_data['source'] ); |
| 72 |
$lines[] = ' }'; |
| 73 |
} |
| 74 |
|
| 75 |
return implode( "\n", $lines ); |
| 76 |
} |
| 77 |
|
| 78 |
private function add_referrer( Red_Item $item, array $match_data ) { |
| 79 |
if ( $item->match->url_from ) { |
| 80 |
$lines[] = 'if ( $http_referer ~* ^' . $item->match->referrer . '$ ) {'; |
| 81 |
$lines[] = ' ' . $this->get_redirect( $item->get_url(), $item->match->url_from, $this->get_redirect_code( $item ), $match_data['source'] ); |
| 82 |
$lines[] = ' }'; |
| 83 |
} |
| 84 |
|
| 85 |
if ( $item->match->url_notfrom ) { |
| 86 |
$lines[] = 'if ( $http_referer !~* ^' . $item->match->referrer . '$ ) {'; |
| 87 |
$lines[] = ' ' . $this->get_redirect( $item->get_url(), $item->match->url_notfrom, $this->get_redirect_code( $item ), $match_data['source'] ); |
| 88 |
$lines[] = ' }'; |
| 89 |
} |
| 90 |
|
| 91 |
return implode( "\n", $lines ); |
| 92 |
} |
| 93 |
|
| 94 |
private function get_redirect( $line, $target, $code, $source ) { |
| 95 |
// Remove any existing start/end from a regex |
| 96 |
$line = ltrim( $line, '^' ); |
| 97 |
$line = rtrim( $line, '$' ); |
| 98 |
|
| 99 |
if ( isset( $source['flag_case'] ) && $source['flag_case'] ) { |
| 100 |
$line = '(?i)^' . $line; |
| 101 |
} else { |
| 102 |
$line = '^' . $line; |
| 103 |
} |
| 104 |
|
| 105 |
$line = preg_replace( "/[\r\n\t].*?$/s", '', $line ); |
| 106 |
$line = preg_replace( '/[^\PC\s]/u', '', $line ); |
| 107 |
$target = preg_replace( "/[\r\n\t].*?$/s", '', $target ); |
| 108 |
$target = preg_replace( '/[^\PC\s]/u', '', $target ); |
| 109 |
|
| 110 |
return 'rewrite ' . $line . '$ ' . $target . ' ' . $code . ';'; |
| 111 |
} |
| 112 |
} |
| 113 |
|