| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Nginx_File extends Red_FileIO { |
| 4 |
public function force_download() { |
| 5 |
parent::force_download(); |
| 6 |
|
| 7 |
$filename = 'redirection-'.date_i18n( get_option( 'date_format' ) ).'.nginx'; |
| 8 |
|
| 9 |
header( 'Content-Type: application/octet-stream' ); |
| 10 |
header( 'Content-Disposition: attachment; filename="'.$filename.'"' ); |
| 11 |
} |
| 12 |
|
| 13 |
public function get_data( array $items, array $groups ) { |
| 14 |
$lines = array(); |
| 15 |
$version = get_plugin_data( dirname( dirname( __FILE__ ) ).'/redirection.php' ); |
| 16 |
|
| 17 |
$lines[] = '# Created by Redirection'; |
| 18 |
$lines[] = '# '.date( 'r' ); |
| 19 |
$lines[] = '# Redirection '.trim( $version['Version'] ).' - http://urbangiraffe.com/plugins/redirection/'; |
| 20 |
$lines[] = ''; |
| 21 |
$lines[] = 'server {'; |
| 22 |
|
| 23 |
foreach ( $items as $item ) { |
| 24 |
$lines[] = $this->get_nginx_item( $item ); |
| 25 |
} |
| 26 |
|
| 27 |
$lines[] = '}'; |
| 28 |
$lines[] = ''; |
| 29 |
$lines[] = '# End of Redirection'; |
| 30 |
|
| 31 |
return implode( PHP_EOL, $lines ).PHP_EOL; |
| 32 |
} |
| 33 |
|
| 34 |
private function get_redirect_code( Red_Item $item ) { |
| 35 |
if ( $item->get_action_code() === 301 ) |
| 36 |
return 'permanent'; |
| 37 |
return 'redirect'; |
| 38 |
} |
| 39 |
|
| 40 |
function load( $group, $data, $filename = '' ) { |
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
private function get_nginx_item( Red_Item $item ) { |
| 45 |
$target = 'add_'.$item->get_match_type(); |
| 46 |
|
| 47 |
if ( method_exists( $this, $target ) ) |
| 48 |
return ' '.$this->$target( $item, $item->match ); |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
private function add_url( Red_Item $item ) { |
| 53 |
return $this->add_redirect( $item->get_url(), $item->get_action_data(), $this->get_redirect_code( $item ) ); |
| 54 |
} |
| 55 |
|
| 56 |
private function add_agent( Red_Item $item ) { |
| 57 |
if ( $item->match->url_from ) { |
| 58 |
$lines[] = 'if ( $http_user_agent ~* ^'.$item->match->user_agent.'$ ) {'; |
| 59 |
$lines[] = ' '.$this->add_redirect( $item->get_url(), $item->match->url_from, $this->get_redirect_code( $item ) ); |
| 60 |
$lines[] = ' }'; |
| 61 |
} |
| 62 |
|
| 63 |
if ( $item->match->url_notfrom ) { |
| 64 |
$lines[] = 'if ( $http_user_agent !~* ^'.$item->match->user_agent.'$ ) {'; |
| 65 |
$lines[] = ' '.$this->add_redirect( $item->get_url(), $item->match->url_notfrom, $this->get_redirect_code( $item ) ); |
| 66 |
$lines[] = ' }'; |
| 67 |
} |
| 68 |
|
| 69 |
return implode( "\n", $lines ); |
| 70 |
} |
| 71 |
|
| 72 |
private function add_referrer( Red_Item $item ) { |
| 73 |
if ( $item->match->url_from ) { |
| 74 |
$lines[] = 'if ( $http_referer ~* ^'.$item->match->referrer.'$ ) {'; |
| 75 |
$lines[] = ' '.$this->add_redirect( $item->get_url(), $item->match->url_from, $this->get_redirect_code( $item ) ); |
| 76 |
$lines[] = ' }'; |
| 77 |
} |
| 78 |
|
| 79 |
if ( $item->match->url_notfrom ) { |
| 80 |
$lines[] = 'if ( $http_referer !~* ^'.$item->match->referrer.'$ ) {'; |
| 81 |
$lines[] = ' '.$this->add_redirect( $item->get_url(), $item->match->url_notfrom, $this->get_redirect_code( $item ) ); |
| 82 |
$lines[] = ' }'; |
| 83 |
} |
| 84 |
|
| 85 |
return implode( "\n", $lines ); |
| 86 |
} |
| 87 |
|
| 88 |
private function add_redirect( $source, $target, $code ) { |
| 89 |
$source = preg_replace( "/[\r\n\t].*?$/s", '', $source ); |
| 90 |
$source = preg_replace( '/[^\PC\s]/u', '', $source ); |
| 91 |
$target = preg_replace( "/[\r\n\t].*?$/s", '', $target ); |
| 92 |
$target = preg_replace( '/[^\PC\s]/u', '', $target ); |
| 93 |
|
| 94 |
return 'rewrite ^'.$source.'$ '.$target.' '.$code.';'; |
| 95 |
} |
| 96 |
} |
| 97 |
|