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 / models / header.php

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

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