| 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 |
|