我正在尝试使用 squid 来修改网页请求的页面内容。我遵循了颠倒的互联网教程,该教程显示了如何在页面上翻转图像的说明。
我需要更改页面的实际 html。我一直在尝试做与教程中相同的事情,但我没有编辑图像,而是尝试编辑 html 页面。下面是我用来尝试执行此操作的 php 脚本。
所有 jpg 图像都会被翻转,但页面上的内容不会被编辑。编写的已编辑 index.html 文件包含已编辑的内容,但用户收到的页面不包含已编辑的内容。
#!/usr/bin/php
<?php
$temp = array();
while ( $input = fgets(STDIN) ) {
$micro_time = microtime();
// Split the output (space delimited) from squid into an array.
$temp = split(' ', $input);
//Flip jpg images, this works correctly
if (preg_match("/.*\.jpg/i", $temp[0])) {
system("/usr/bin/wget -q -O /var/www/cache/$micro_time.jpg ". $temp[0]);
system("/usr/bin/mogrify -flip /var/www/cache/$micro_time.jpg");
echo "http://127.0.0.1/cache/$micro_time.jpg\n";
}
//Don't edit files that are obviously not html. $temp[0] contains url of file to get
elseif (preg_match("/(jpg|png|gif|css|js|\(|\))/i", $temp[0], $matches)) {
echo $input;
}
//Otherwise, could be html (e.g. `wget http://www.google.com` downloads index.html)
else{
$time = time() . microtime(); //For unique directory names
$time = preg_replace("/ /", "", $time); //Simplify things by removing the spaces
mkdir("/var/www/cache/". $time); //Create unique folder
system("/usr/bin/wget -q --directory-prefix=\"/var/www/cache/$time/\" ". $temp[0]);
$filename = system("ls /var/www/cache/$time/"); //Get filename of downloaded file
//File is html, edit the content (this does not work)
if(preg_match("/.*\.html/", $filename)){
//Get the html file contents
$contentfh = fopen("/var/www/cache/$time/". $filename, 'r');
$content = fread($contentfh, filesize("/var/www/cache/$time/". $filename));
fclose($contentfh);
//Edit the html file contents
$content = preg_replace("/<\/body>/i", "<!-- content served by proxy --></body>", $content);
//Write the edited file
$contentfh = fopen("/var/www/cache/$time/". $filename, 'w');
fwrite($contentfh, $content);
fclose($contentfh);
//Return the edited page
echo "http://127.0.0.1/cache/$time/$filename\n";
}
//Otherwise file is not html, don't edit
else{
echo $input;
}
}
}
?>