Skip to content

Instantly share code, notes, and snippets.

@KevinDoughty
Last active January 17, 2017 20:49
Show Gist options
  • Save KevinDoughty/5f1cadc2eba4c5e63b3c5a90d434cf43 to your computer and use it in GitHub Desktop.
Save KevinDoughty/5f1cadc2eba4c5e63b3c5a90d434cf43 to your computer and use it in GitHub Desktop.
#!/opt/local/bin/php
<?php
// logs can be viewed with:
// sudo tail -f /opt/local/var/squid/logs/cache.log
$enabled = true;
$verboseLogging = true;
$upsideDownTernet = false;
$negativeLand = true;
$searchEngines = true;
$youTube = true;
$nonYouTube = true;
$faceBook = true;
$wiki = true;
$youtubeVideo = 'yzC4hFK5P3g'; // pon pon pon
$youtubeMobile = 'cb30b88452b93f78';
$searchRequest = 'きゃりーぱみゅぱみゅ';
$wikipediaPage = 'Kyary_Pamyu_Pamyu';
//$localVideoLink = 'http://pon.dev/ponponpon'; // leave off the video file extension, you must have a duplicate for every possible kind.
$localVideoLink = 'http://pon.pon.pon/ponponpon'; // leave off the video file extension, you must have a duplicate for every possible kind.
$localSite = 'http://pon.pon.pon/'; // local site serving videos to block the above video replacement by file type.
//$localFacebookPic = 'http://pon.dev/jelly'; // leave off the image file extension, you must have a duplicate for every possible kind.
$localFacebookPic = 'http://pon.pon.pon/jelly'; // leave off the image file extension, you must have a duplicate for every possible kind.
$facebookId = '123456789'; // target facebook id, the middle number
$debugFacebookId = '123456789'; // so you can test using your own facebook page.
$videoFileTypes = array("flv","f4v","mp4","mov","mpg","mpeg");
mb_internal_encoding('UTF-8');
$timeout = 3600 * 24 * 365; // in seconds
stream_set_timeout(STDIN, $timeout);
if ($verboseLogging) @error_log('SQUID SQUID SQUID', 0);
function videoFileTypeReplacement($theString) {
global $videoFileTypes;
global $verboseLogging;
global $localVideoLink;
foreach ($videoFileTypes as $videoFileType) {
$theMatchRegex = '/^.+\.' . $videoFileType . '$/';
if (preg_match($theMatchRegex,$theString)) {
$theReplaceRegex = '/^.+(?=(\.' . $videoFileType . '$))/';
if ($verboseLogging) @error_log('VIDEO ' . $videoFileType . ' PRE:' . $theString,0);
$theString = preg_replace($theReplaceRegex,$localVideoLink,$theString);
if ($verboseLogging) @error_log('VIDEO ' . $videoFileType . ' POST:' . $theString,0);
return $theString;
}
}
return $theString;
}
function searchReplacement($matches) { // this is used by preg_replace_callback
global $searchRequest;
return urlencode(mb_substr($searchRequest,0,mb_strlen($matches[0]))); // there is no error if strlen is too long.
}
while (FALSE !== ($theInput = fgets(STDIN))) {
$temp = preg_split('/\s/', $theInput);
$theString = $temp[0] . "\n";
$urlLineTerminator = "\n";
if ($upsideDownTernet === true || $negativeLand === true) {
if (preg_match('/^.+\.jpe?g$/i',$theString) || preg_match('/^.+\.gif$/i',$theString) || preg_match('/^.+\.png$/i',$theString)) {
$negateString = '0';
if ($negativeLand === true) $negateString = '1';
$flipString = '0';
if ($upsideDownTernet === true) $flipString = '1';
$finalString = 'http://pon.pon.pon/onTheFlyImage.php?flip=' .$flipString. '&negate='.$negateString.'&url='.$theString; // warning!
if ($verboseLogging) @error_log('IMAGE MAGICK:' . $finalString,0);
echo($finalString);
continue;
}
}
if (strpos($theString,$localSite) === 0 || strpos($theString,'proxy:'.$localSite) === 0) { // safety net
if ($verboseLogging) @error_log('LOCAL SITE:' . $theString,0);
echo($theString);
continue;
}
if (strpos($theString,$localVideoLink) === 0 || strpos($theString,'proxy:'.$localVideoLink) === 0) { // safety net
if ($verboseLogging) @error_log('LOCAL VIDEO LINK:' . $theString,0);
echo($theString);
continue;
}
if (strpos($theString,$localFacebookPic) === 0 || strpos($theString,'proxy:'.$localFacebookPic) === 0) { // safety net
if ($verboseLogging) @error_log('LOCAL FACEBOOK PIC:' . $theString,0);
echo($theString);
continue;
}
if ($nonYouTube === true) { // REPLACE NON YOUTUBE VIDEOS
if (strpos($theString,'http://www.youtube.com/') !== 0 && strpos($theString,'proxy:http://www.youtube.com/') !== 0) { // if NOT youtube video replacement
$theVideoString = videoFileTypeReplaceMent($theString);
if (strpos($theString,$theVideoString) === false) { // if theString was altered to local video link
if ($enabled) $theString = $theVideoString;
echo($theString);
continue;
}
}
}
if (preg_match('/\?/',$theString)) { // QUERY
if ($youTube === true) {
/*
if (strpos($theString,'youtube.com/videoplayback') !== false) {
if ($enabled) $theString = preg_replace('/(?<=\?id=|&id=).+?(?=(&|$))/',$youtubeMobile,$theString);
if ($verboseLogging) @error_log('YOUTUBE MOBILE:' . $theString,0);
echo($theString);
continue;
}
*/
if (strpos($theString,'http://www.youtube.com/watch') === 0 || strpos($theString,'proxy:http://www.youtube.com/watch') === 0) { // youtube video redirect
if ($enabled) $theString = preg_replace('/(?<=\?v=|&v=).+?(?=(&|$))/',$youtubeVideo,$theString);
if ($verboseLogging) @error_log('YOUTUBE VIDEO:' . $theString,0);
echo($theString);
continue;
}
if (strpos($theString,'youtube.com/results?') !== false) { // youtube video redirect
if ($enabled) $theString = preg_replace_callback('/(?<=\?search_query=|&oq=).+?(?=(&|$))/','searchReplacement',$theString);
if ($verboseLogging) @error_log('YOUTUBE VIDEO:' . $theString,0);
echo($theString);
continue;
}
}
if ($searchEngines === true) {
if (strpos($theString,'google.com/search?') !== false || strpos($theString,'google.com/complete/search?') !== false) { // google and youtube
//if ($enabled) $theString = preg_replace_callback('/(?<=\?q=|&q=|&oq=).+?(?=(&|$))/','searchReplacement',$theString);
if ($enabled) $theString = preg_replace_callback('/(?<=\?q=|&q=|&oq=|&pq=).+?(?=(&|$))/','searchReplacement',$theString);
if ($verboseLogging) @error_log('GOOGLE SEARCH:' . $theString,0);
echo($theString);
continue;
}
if (strpos($theString,'google.com/') !== false) { // google and youtube
//if ($enabled) $theString = preg_replace_callback('/(?<=\?q=|&q=|&oq=).+?(?=(&|$))/','searchReplacement',$theString);
if ($enabled) $theString = preg_replace_callback('/(?<=\?q=|&q=|&oq=|&pq=).+?(?=(&|$))/','searchReplacement',$theString);
if ($verboseLogging) @error_log('GOOGLE ALTERNATE:' . $theString,0);
echo($theString);
continue;
}
if (strpos($theString,'search.yahoo.com/') !== false || strpos($theString,'geo.yahoo.com/p?') !== false) { // yahoo
if ($enabled) $theString = preg_replace_callback('/(?<=command=|\?p=|&ssug=|&pqstr).+?(?=(&|$))/','searchReplacement',$theString);
if ($verboseLogging) @error_log('YAHOO SEARCH:' . $theString,0);
echo($theString);
continue;
}
if (strpos($theString,'ask.com/web?') !== false) {
if ($enabled) $theString = preg_replace_callback('/(?<=\?q=).+?(?=(&|$))/','searchReplacement',$theString);
if ($verboseLogging) @error_log('ASK SEARCH:' . $theString,0);
echo($theString);
continue;
}
if (strpos($theString,'bing.com/search?') !== false || strpos($theString,'api.bing.com/qsonhs') !== false) {
if ($enabled) $theString = preg_replace_callback('/(?<=\?q=|&q=).*?(?=(&|$))/','searchReplacement',$theString);
if ($verboseLogging) @error_log('BING SEARCH:' . $theString,0);
echo($theString);
continue;
}
if (strpos($theString,'wikipedia.org/') !== false) {
if ($enabled) $theString = preg_replace_callback('/(?<=\?search=).+?(?=(&|$))/','searchReplacement',$theString);
if ($verboseLogging) @error_log('ASK SEARCH:' . $theString,0);
echo($theString);
continue;
}
}
if ($verboseLogging) @error_log('QUERY:' . $theString,0);
echo($theString);
continue;
} else { // NOT QUERY
if ($youTube === true) {
if (strpos($theString,'http://www.youtube.com/user/') === 0 || strpos($theString,'proxy:http://www.youtube.com/user/') === 0) { // youtube user redirect
if ($verboseLogging) @error_log('YOUTUBE USER:' . $theString,0);
echo($theString);
continue;
}
if (strpos($theString,'.ytimg.com/vi/') !== false) { // youtube thumbnail
if (preg_match('/^.+\.jpg$/i',$theString)) { // youtube jpg thumbnail
if ($verboseLogging) @error_log('JPG THUMB:' . $theString,0);
echo($theString);
continue;
}
if (preg_match('/^.+\.jpeg$/i',$theString)) { // youtube jpeg thumbnail
if ($verboseLogging) @error_log('JPEG THUMB:' . $theString,0);
echo($theString);
continue;
}
if (preg_match('/^.+\.gif$/i',$theString)) { // youtube gif thumbnail
if ($verboseLogging) @error_log('GIF THUMB:' . $theString,0);
echo($theString);
continue;
}
if (preg_match('/^.+\.png$/i',$theString)) { // youtube png thumbnail
if ($verboseLogging) @error_log('PNG THUMB:' . $theString,0);
echo($theString);
continue;
}
if ($verboseLogging) @error_log('UNHANDLED THUMB:' . $theString,0);
echo($theString);
continue;
}
}
if ($faceBook === true) {
//if (strpos($theString,'.fbcdn.net/hprofile') !== false) { // facebook
//if (strpos($theString,'fbcdn') !== false && (strpos($theString,'hprofile') !== false || strpos($theString,'hphotos') !== false) && ( strpos($theString, $facebookId) !== false || strpos($theString, $debugFacebookId) !== false)) { // facebook
if (strpos($theString,'fbcdn') !== false && strpos($theString,'hprofile') !== false && ( strpos($theString, $facebookId) !== false || strpos($theString, $debugFacebookId) !== false)) { // facebook
if (preg_match('/^.+\.(jpg|jpeg|gif|png|bmp|tif)$/i',$theString)) { // facebook pic
if ($verboseLogging) @error_log('FACEBOOK PIC OLD:' . $theString,0);
//if ($enabled) $theString = preg_replace('/^.+(?=(\.jpg$|\.jpeg$|\.gif$|\.png$|\.bmp$|\.tif$))/i','proxy:'. $localFacebookPic,$theString);
if ($enabled) $theString = preg_replace('/^.+(?=(\.jpg$|\.jpeg$|\.gif$|\.png$|\.bmp$|\.tif$))/i', $localFacebookPic,$theString);
if ($verboseLogging) @error_log('FACEBOOK PIC NEW:' . $theString,0);
echo($theString);
continue;
}
}
}
//http://en.wikipedia.org/wiki/Kyary_Pamyu_Pamyu
if ($wiki === true) {
if (strpos($theString,'wikipedia.org/wiki/') !== false) {
if ($enabled) $theString = preg_replace('/(?<=wikipedia.org\/wiki\/).*$/',$wikipediaPage,$theString);
if ($verboseLogging) @error_log('WIKIPEDIA:' . $theString,0);
echo($theString);
continue;
}
}
if ($verboseLogging) @error_log('URL:' . $theString,0);
echo($theString);
continue;
}
}
fclose(STDIN);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment