2010-03-24 63 views
1

我试图用鱿鱼来修改网页内容的网页请求。我遵循upside-down-ternet教程,该教程显示了如何在页面上翻转图像的说明。鱿鱼代理不提供修改后的html内容

我需要更改页面的实际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; 
     } 
    } 
} 
?> 

回答

0

看看Dansguardian;它使用PCRE来即时修改内容:link(看最后2个主题)

0

不知道是否它的问题的原因,但代码有很多错误。

根据microtime分离请求 - 只有在流量相对较低时才能可靠地工作 - 请注意,如果有多个重定向器实例运行,原始(perl)代码仍可能会中断。

您已经尝试根据文件扩展名识别内容类型 - 这适用于与列表匹配的文件 - 但它不遵循与列表不匹配的内容必须为text/html - 你真的应该检查原始服务器返回的mimetype。

代码中没有错误检查/调试 - 虽然您没有可以轻松写入的错误流,但可以将错误写入文件,写入系统日志或发出电子邮件如果fopen/fread语句不起作用,或者存储的文件没有.html扩展名。

C.