/Workshop3/twitter-oauth/test.php

https://github.com/oskarp/4ME102 · PHP · 126 lines · 124 code · 2 blank · 0 comment · 11 complexity · bd32a8b16656b16ad51c28dd52268023 MD5 · raw file

  1. <?
  2. class PhpReverseProxy{
  3. public $port,$host,$content,$forward_path,$content_type,$user_agent,
  4. $XFF,$request_method,$IMS,$cacheTime,$cookie;
  5. private $http_code,$lastModified,$version,$resultHeader;
  6. function __construct(){
  7. $this->version="PHP Reverse Proxy (PRP) 1.0";
  8. $this->port="8080";
  9. $this->host="127.0.0.1";
  10. $this->content="";
  11. $this->forward_path="";
  12. $this->path="";
  13. $this->content_type="";
  14. $this->user_agent="";
  15. $this->http_code="";
  16. $this->XFF="";
  17. $this->request_method="GET";
  18. $this->IMS=false; // If-Modified-Since
  19. $this->cacheTime=72000;
  20. $this->lastModified=gmdate("D, d M Y H:i:s",time()-72000)." GMT";
  21. $this->cookie="";
  22. }
  23. function translateURL($serverName) {
  24. $this->path=$this->forward_path.$_SERVER['REQUEST_URI'];
  25. if($_SERVER['QUERY_STRING']=="")
  26. return $this->translateServer($serverName).$this->path;
  27. else
  28. return $this->translateServer($ServerName).$this->path."?".$_SERVER['QUERY_STRING'];
  29. }
  30. function translateServer($serverName) {
  31. $s = empty($_SERVER["HTTPS"]) ? ''
  32. : ($_SERVER["HTTPS"] == "on") ? "s"
  33. : "";
  34. $protocol = $this->left(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
  35. if($this->port=="")
  36. return $protocol."://".$serverName;
  37. else
  38. return $protocol."://".$serverName.":".$port;
  39. }
  40. function left($s1, $s2) {
  41. return substr($s1, 0, strpos($s1, $s2));
  42. }
  43. function preConnect(){
  44. $this->user_agent=$_SERVER['HTTP_USER_AGENT'];
  45. $this->request_method=$_SERVER['REQUEST_METHOD'];
  46. $tempCookie="";
  47. foreach ($_COOKIE as $i => $value) {
  48. $tempCookie=$tempCookie." $i=$_COOKIE[$i];";
  49. }
  50. $this->cookie=$tempCookie;
  51. if(empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  52. $this->XFF=$_SERVER['REMOTE_ADDR'];
  53. } else {
  54. $this->XFF=$_SERVER['HTTP_X_FORWARDED_FOR'].", ".$_SERVER['REMOTE_ADDR'];
  55. }
  56. }
  57. function connect(){
  58. if(empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
  59. $this->preConnect();
  60. $ch=curl_init();
  61. if($this->request_method=="POST"){
  62. curl_setopt($ch, CURLOPT_POST,1);
  63. curl_setopt($ch, CURLOPT_POSTFIELDS,file_get_contents("php://input"));
  64. }
  65. curl_setopt($ch,CURLOPT_URL,$this->translateURL($this->host));
  66. curl_setopt($ch,CURLOPT_HTTPHEADER,
  67. Array(
  68. "X-Forwarded-For: ".$this->XFF,
  69. "User-Agent: ".$this->user_agent
  70. ));
  71. if($this->cookie!=""){
  72. curl_setopt($ch,CURLOPT_COOKIE,$this->cookie);
  73. }
  74. curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
  75. curl_setopt($ch,CURLOPT_AUTOREFERER,true);
  76. curl_setopt($ch,CURLOPT_HEADER,true);
  77. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  78. $output=curl_exec($ch);
  79. $info = curl_getinfo( $ch );
  80. curl_close($ch);
  81. $this->postConnect($info,$output);
  82. }else {
  83. $this->lastModified=$_SERVER['HTTP_IF_MODIFIED_SINCE'];
  84. $this->IMS=true;
  85. }
  86. }
  87. function postConnect($info,$output){
  88. $this->content_type=$info["content_type"];
  89. $this->http_code=$info['http_code'];
  90. if(!empty($info['last_modified'])){
  91. $this->lastModified=$info['last_modified'];
  92. }
  93. $this->resultHeader=substr($output,0,$info['header_size']);
  94. $content=substr($output,$info['header_size']);
  95. if($this->http_code=='200'){
  96. $this->content=$content;
  97. }
  98. }
  99. function output(){
  100. $currentTimeString=gmdate("D, d M Y H:i:s",time());
  101. $expiredTime=gmdate("D, d M Y H:i:s",(time()+$this->cacheTime));
  102. if($this->IMS){
  103. header("HTTP/1.1 304 Not Modified");
  104. header("Date: Wed, $currentTimeString GMT");
  105. header("Last-Modified: $this->lastModified");
  106. header("Server: $this->version");
  107. }else{
  108. header("HTTP/1.1 200 OK");
  109. header("Date: Wed, $currentTimeString GMT");
  110. header("Content-Type: ".$this->content_type);
  111. header("Last-Modified: $this->lastModified");
  112. header("Cache-Control: max-age=$this->cacheTime");
  113. header("Expires: $expiredTime GMT");
  114. header("Server: $this->version");
  115. preg_match("/Set-Cookie:[^\n]*/i",$this->resultHeader,$result);
  116. foreach($result as $i=>$value){
  117. header($result[$i]);
  118. }
  119. echo($this->content);
  120. }
  121. }
  122. }
  123. ?>
  124. Copyright 2011 Cloudgen