PluginProbe
Redirection / 4.5
Redirection v4.5
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 4.5, at models/header.php

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