PluginProbe
Redirection / 5.8.0
Redirection v5.8.0
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 / actions / pass.php

pass.php in Redirection 5.8.0, at actions/pass.php

78 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require_once __DIR__ . '/url.php';
4
5 /**
6 * A 'pass through' action. Matches a rewrite rather than a redirect, and uses PHP to fetch data from a remote URL.
7 */
8 class Pass_Action extends Url_Action {
9 /**
10 * Process an external passthrough - a URL that lives external to this server.
11 *
12 * @param string $url Target URL.
13 * @return void
14 */
15 public function process_external( $url ) {
16 // This is entirely at the user's risk. The $url is set by the user
17 // phpcs:ignore
18 echo wp_remote_fopen( $url );
19 }
20
21 /**
22 * Process an internal passthrough - a URL that lives on the same server. Here we change the request URI and continue without making a remote request.
23 *
24 * @param string $target Target URL.
25 * @return void
26 */
27 public function process_internal( $target ) {
28 // Another URL on the server
29 $pos = strpos( $target, '?' );
30 $_SERVER['REQUEST_URI'] = $target;
31 $_SERVER['PATH_INFO'] = $target;
32
33 if ( $pos ) {
34 $_SERVER['QUERY_STRING'] = substr( $target, $pos + 1 );
35 $_SERVER['PATH_INFO'] = $target;
36
37 // Take the query params in the target and make them the params for this request
38 parse_str( $_SERVER['QUERY_STRING'], $_GET );
39 }
40 }
41
42 /**
43 * Is a URL external?
44 *
45 * @param string $target URL to test.
46 * @return boolean
47 */
48 public function is_external( $target ) {
49 return substr( $target, 0, 7 ) === 'http://' || substr( $target, 0, 8 ) === 'https://';
50 }
51
52 /**
53 * Pass the data from the target
54 *
55 * @return void
56 */
57 public function run() {
58 // External target
59 $target = $this->get_target();
60 if ( $target === null ) {
61 return;
62 }
63
64 if ( $this->is_external( $target ) ) {
65 // Pass on to an external request, echo the results, and then stop
66 $this->process_external( $target );
67 exit();
68 }
69
70 // Change the request and carry on
71 $this->process_internal( $target );
72 }
73
74 public function name() {
75 return __( 'Pass-through', 'redirection' );
76 }
77 }
78