topical media & game development

talk show tell print

lib-ajax-example-relay.php / php



  <?php
  
  # List the allowed domain names below, between quote
  #   characters, with all but the last followed by a comma.
  #
  # This is to restrict opportunities for cross site
  #   scripting. Pages can only be retrieved from the
  #   domains listed here.
  #
  # In order to retrieve pages from both example.com and
  #   www.example.com, both domains must be listed.
  
  AllowedDomains = Array(
  "www.cs.vu.nl",
  "www.few.vu.nl"
  );
  
  # Notes:
  
  # Check for a URL in the query string, as a value in a
  #   GET variable, or as a value in a POST variable. The
  #   first to match is used. If none match, exit.
  
  if( isset(_SERVER['QUERY_STRING']) and preg_match('/^http:\/\//i',_SERVER['QUERY_STRING']) )
  { url = _SERVER['QUERY_STRING']; }
  elseif( isset(_GET['url']) and preg_match('/^http:\/\//i',_GET['url']) )
  { url = _GET['url']; }
  elseif( isset(_POST['url']) and preg_match('/^http:\/\//i',_POST['url']) )
  { url = _POST['url']; }
  else
  { exit; }
  
  # Verify the domain in the URL is allowed. If unable to
  #   verify, exit.
  
  checkdomain = preg_replace('/^http:\/\//i','',url);
  checkdomain = preg_replace('/\/.*/','',checkdomain);
  checkdomain = strtolower(checkdomain);
  domainokay = false;
  foreach(AllowedDomains as domain)
  {
          if( checkdomain == strtolower(domain) )
          {
                  domainokay = true;
                  break;
          }
  }
  if(! domainokay)
  { exit; }
  
  # An URL may be used as a filename with function readfile()
  #   when URL-aware fopen wrappers are enabled. See
  #   http://us2.php.net/manual/en/ref.filesystem.php#ini.allow-url-fopen
  
  readfile(url);
  exit;
  # When URL-aware fopen wrappers are not enabled, the
  #   following can be used.
  
  # Retrieve the page and echo it to the browser.
  
  # Break URL into host and URI parts.
  if(strpos(url,'/') == 0)
  {
          host = _SERVER['SERVER_NAME'];
          uri = url;
  }
  else
  {
          url = preg_replace('/^.+?:\/\//','',url);
          pos = strpos(url,'/');
          host = substr(url,0,pos);
          uri = substr(url,pos);
  }
  # Open socket and store remote web page in content
  content = '';
  fp = @fsockopen("host",80,errno,errstr,30);
  if(!fp)
  {
          echo("Error: errstr (errno)");
          exit;
  }
  else
  {
  
          fwrite(fp,"GET uri HTTP/1.0\r\n");
          fwrite(fp,"Host: host\r\n");
          if( isset(_SERVER['HTTP_USER_AGENT']) )
          { fwrite(fp,'User-Agent: '._SERVER['HTTP_USER_AGENT']."\r\n"); }
          fwrite(fp,"Connection: Close\r\n");
          fwrite(fp,"\r\n");
          while (! feof(fp) )
          { content .= fgets(fp,1024); }
          fclose(fp);
  }
  # Remove header lines from content and echo the rest to
  #   the browser.
  if( strpos(content,"\r") > 0 )
  { echo( preg_replace('/^.+?[\r\n]{4,}/s','',content) ); }
  else
  { echo( preg_replace('/^.+?\n{2,}/s','',content) ); }
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.