PluginProbe
Redirection / 5.5.2
Redirection v5.5.2
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / fileio / nginx.php

nginx.php in Redirection 5.5.2, at fileio/nginx.php

115 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 public 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'], $item->source_flags->is_regex() );
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, $regex = false ) {
95 $line = ltrim( $line, '^' );
96 $line = rtrim( $line, '$' );
97
98 $source_url = new Red_Url_Encode( $line, $regex );
99 $target_url = new Red_Url_Encode( $target );
100
101 // Remove any existing start/end from a regex
102 $from = $source_url->get_as_source();
103 $from = ltrim( $from, '^' );
104 $from = rtrim( $from, '$' );
105
106 if ( isset( $source['flag_case'] ) && $source['flag_case'] ) {
107 $from = '(?i)^' . $from;
108 } else {
109 $from = '^' . $from;
110 }
111
112 return 'rewrite ' . $from . '$ ' . $target_url->get_as_target() . ' ' . $code . ';';
113 }
114 }
115