diff options
author | Thorsten Ortlepp <post@ortlepp.eu> | 2022-02-19 18:21:46 +0100 |
---|---|---|
committer | Thorsten Ortlepp <post@ortlepp.eu> | 2022-02-19 18:21:46 +0100 |
commit | 0ae279aa6279a7862343d74a45227e6298b2669c (patch) | |
tree | 5c316a92e3f2bec42a4c80a8b5969e40b01b857e | |
parent | 879c9529a19ad446ceca9b1bc45e560093d39801 (diff) | |
download | php-stuff-0ae279aa6279a7862343d74a45227e6298b2669c.zip |
Added proxy script
-rw-r--r-- | README | 12 | ||||
-rw-r--r-- | proxy/proxy.php | 34 |
2 files changed, 46 insertions, 0 deletions
@@ -9,6 +9,18 @@ Deutschlandfunk Kalenderblatt which is only available online and as RSS feed but not as podcast. Requires PHP 8. +proxy +----- +A very simple kind of proxy. It adds an HTTP header for Basic +Authentication to all requests. On HTML websites all relative links +are replaced with proxy based links. +I use this script to embed external websites with Basic +Authentication into my personal Nextcloud instance without the need +to enter a password. +Use at your own risk - this script may break security aspects and +manipulates website content! + + rssfilter --------- A simple script to remove unwanted articles from an RSS feed, either diff --git a/proxy/proxy.php b/proxy/proxy.php new file mode 100644 index 0000000..3985308 --- /dev/null +++ b/proxy/proxy.php @@ -0,0 +1,34 @@ +<?php + // Secret key to use this script + $secret = ''; + // HTTP authorization (USERNAME:PASSWORD Base64 encoded) + $authorization = ''; + + if (isset($_GET['secret']) && $_GET['secret'] == $secret && isset($_GET['url']) && !empty($_GET['url'])) { + + $request_headers = array('Authorization: Basic '.$authorization); + $url = $_GET['url']; + + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers); + $content = curl_exec($curl); + $contenttype = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); + curl_close($curl); + + if (str_starts_with($contenttype, 'text/html')) { + $proxyurl = substr($url, 0, strrpos($url, '/') + 1); + $scripturl = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?secret='.$secret.'&'; + + $content = preg_replace('#href="(?!https)(.+)"#i', 'href="'.$scripturl.'url='.$proxyurl.'$1"', $content); + $content = preg_replace('#src="(?!https)(.+)"#i', 'src="'.$scripturl.'url='.$proxyurl.'$1"', $content); + } + + header('Content-Type: '.$contenttype); + echo $content; + + } else { + throw new Exception('Insufficient request'); + } +?> |