PluginProbe
Redirection / 5.4
Redirection v5.4
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 / models / header.php

header.php in Redirection 5.4, at models/header.php

128 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_Http_Headers {
4 private $headers = [];
5
6 public function __construct( $options = [] ) {
7 if ( is_array( $options ) ) {
8 $this->headers = array_filter( array_map( [ $this, 'normalize' ], $options ) );
9 }
10 }
11
12 private function normalize( $header ) {
13 $location = 'site';
14 if ( isset( $header['location'] ) && $header['location'] === 'redirect' ) {
15 $location = 'redirect';
16 }
17
18 $name = $this->sanitize( isset( $header['headerName'] ) ? sanitize_text_field( $header['headerName'] ) : '' );
19 $type = $this->sanitize( isset( $header['type'] ) ? sanitize_text_field( $header['type'] ) : '' );
20 $value = $this->sanitize( isset( $header['headerValue'] ) ? sanitize_text_field( $header['headerValue'] ) : '' );
21 $settings = [];
22
23 if ( isset( $header['headerSettings'] ) && is_array( $header['headerSettings'] ) ) {
24 foreach ( $header['headerSettings'] as $key => $setting_value ) {
25 if ( is_array( $setting_value ) && isset( $setting_value['value'] ) ) {
26 $settings[ $this->sanitize( sanitize_text_field( $key ) ) ] = $this->sanitize( $setting_value['value'] );
27 } else {
28 $settings[ $this->sanitize( sanitize_text_field( $key ) ) ] = $this->sanitize( $setting_value );
29 }
30 }
31 }
32
33 if ( strlen( $name ) > 0 && strlen( $type ) > 0 ) {
34 return [
35 'type' => $this->dash_case( $type ),
36 'headerName' => $this->dash_case( $name ),
37 'headerValue' => $value,
38 'location' => $location,
39 'headerSettings' => $settings,
40 ];
41 }
42
43 return null;
44 }
45
46 public function get_json() {
47 return $this->headers;
48 }
49
50 private function dash_case( $name ) {
51 $name = preg_replace( '/[^A-Za-z0-9]/', ' ', $name );
52 $name = preg_replace( '/\s{2,}/', ' ', $name );
53 $name = trim( $name, ' ' );
54 $name = ucwords( $name );
55 $name = str_replace( ' ', '-', $name );
56
57 return $name;
58 }
59
60 private function remove_dupes( $headers ) {
61 $new_headers = [];
62
63 foreach ( $headers as $header ) {
64 $new_headers[ $header['headerName'] ] = $header;
65 }
66
67 return array_values( $new_headers );
68 }
69
70 public function get_site_headers() {
71 $headers = array_values( $this->remove_dupes( array_filter( $this->headers, [ $this, 'is_site_header' ] ) ) );
72
73 return apply_filters( 'redirection_headers_site', $headers );
74 }
75
76 public function get_redirect_headers() {
77 // Site ones first, then redirect - redirect will override any site ones
78 $headers = $this->get_site_headers();
79 $headers = array_merge( $headers, array_values( array_filter( $this->headers, [ $this, 'is_redirect_header' ] ) ) );
80 $headers = array_values( $this->remove_dupes( $headers ) );
81
82 return apply_filters( 'redirection_headers_redirect', $headers );
83 }
84
85 private function is_site_header( $header ) {
86 return $header['location'] === 'site';
87 }
88
89 private function is_redirect_header( $header ) {
90 return $header['location'] === 'redirect';
91 }
92
93 public function run( $headers ) {
94 $done = [];
95
96 foreach ( $headers as $header ) {
97 if ( ! in_array( $header['headerName'], $done, true ) ) {
98 $name = $this->sanitize( $this->dash_case( $header['headerName'] ) );
99 $value = $this->sanitize( $header['headerValue'] );
100
101 // Trigger some other action
102 do_action( 'redirection_header', $name, $value );
103
104 header( sprintf( '%s: %s', $name, $value ) );
105 $done[] = $header['headerName'];
106 }
107 }
108 }
109
110 private function sanitize( $text ) {
111 // No new lines
112 $text = preg_replace( "/[\r\n\t].*?$/s", '', $text );
113
114 // Clean control codes
115 $text = preg_replace( '/[^\PC\s]/u', '', $text );
116
117 // Try and remove bad decoding
118 if ( function_exists( 'iconv' ) ) {
119 $converted = @iconv( 'UTF-8', 'UTF-8//IGNORE', $text );
120 if ( $converted !== false ) {
121 $text = $converted;
122 }
123 }
124
125 return $text;
126 }
127 }
128