Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ERROR | E_PARSE);
- //$host = "ccs.shop.wii.com";
- $host = $_SERVER["SERVER_NAME"];
- $page = $_SERVER["REQUEST_URI"];
- $cachedisabled = 0; //Set to 1 if not storing any contents locally.
- $logdisabled = 0;
- $path = "logs";
- if(!file_exists($path))
- mkdir($path,"0777",1);
- if($logdisabled == 0)
- {
- $logfh = fopen("$path/log.txt", "a");
- //$save_string = implode("`|`", $_SERVER);
- //fwrite($logfh,$save_string);
- //$save_string = "\r\n";
- //fwrite($logfh,$save_string);
- fwrite($logfh,$_SERVER["REMOTE_ADDR"]);
- fwrite($logfh,", ");
- fwrite($logfh,$_SERVER["REQUEST_TIME"]);
- fwrite($logfh,", \"");
- fwrite($logfh,$_SERVER["HTTP_USER_AGENT"]);
- fwrite($logfh,"\", \"");
- fwrite($logfh,$_SERVER["HTTP_ACCEPT"]);
- fwrite($logfh,",");
- fwrite($logfh,$_SERVER["HTTP_ACCEPT_LANGUAGE"]);
- fwrite($logfh,",");
- fwrite($logfh,$_SERVER["HTTP_ACCEPT_ENCODING"]);
- fwrite($logfh,",");
- fwrite($logfh,$_SERVER["HTTP_ACCEPT_CHARSET"]);
- fwrite($logfh,"\", \"");
- fwrite($logfh,$_SERVER["REQUEST_METHOD"]);
- fwrite($logfh," http://");
- fwrite($logfh,$host);
- fwrite($logfh,$_SERVER["REQUEST_URI"]);
- fwrite($logfh,"\", ");
- fwrite($logfh,$_SERVER["SERVER_PROTOCOL"]);
- }
- $host = "ccs.cdn.c.shop.nintendowifi.net";
- list($blank,$ccs,$download,$title,$filename) = split("/",$page);
- $hexchars = array("0","1","2","3","4","5","6","7","8","9","A","a","B","b","C","c","D","d","E","e","F","f");
- $emptytitle = str_replace($hexchars, "", $title);
- $uppertitle = str_replace("a","A",$title);
- $uppertitle = str_replace("b","B",$uppertitle);
- $uppertitle = str_replace("c","C",$uppertitle);
- $uppertitle = str_replace("d","D",$uppertitle);
- $uppertitle = str_replace("e","E",$uppertitle);
- $uppertitle = str_replace("f","F",$uppertitle); //Convert ALL HEX characters to UPPERCASE. (Greatly simplifies comparison)
- if ( strcmp ( $ccs,"ccs" ) or strcmp ( $download,"download" ) or empty ( $filename ) or (strlen($title) != 16) or (!empty ($emptytitle)))
- {
- header("HTTP/1.1 404 Not Found");
- echo "404: File $page not found on this server<br>";
- if($logdisabled == 0)
- {
- fwrite($logfh,", 404 -\r\n");
- fclose($logfh);
- }
- die;
- } //Request was not for Nintendo's Content server, this means the File definitely does NOT exist.
- $path = "$ccs/$download/$title";
- if(!file_exists($path))
- mkdir($path,"0777",1);
- if(!file_exists("a/$path"))
- mkdir("a/$path","0777",1);
- $myFile = $page;
- $fh = fopen("a/$path/$filename", "wb");
- if(! $fh)
- {
- header("HTTP/1.1 500 Internal Server Error");
- echo "could not open local file for writing";
- if($logdisabled == 0)
- {
- fwrite($logfh,", 500 -\r\n");
- fclose($logfh);
- }
- $cachedisabled = 2;
- } //We were unable to create the local file.
- $fp = fsockopen( "$host", 80, &$errno, &$errdesc);
- if ( ! $fp )
- {
- header("HTTP/1.1 503 Service Unavailable");
- if($logdisabled == 0)
- {
- fwrite($logfh,", 503 -\r\n");
- fclose($logfh);
- }
- die ( "Couldn't connect to $host:\nError: $errno\nDesc: $errdesc\n" );
- } //Nintendo's File content server is down.
- $request = "GET $page HTTP/1.0\r\n";
- $request .= "Host: $host\r\n";
- $request .= "User-Agent: PHP test client\r\n\r\n";
- $page = array();
- fputs ( $fp, $request );
- $result = fgets( $fp, 1024 );
- $contentsize = 0;
- if(((strcmp($result,"HTTP/1.0 200 OK\r\n"))&&(strcmp($result,"HTTP/1.1 200 OK\r\n")))||($cachedisabled != 0))
- {
- if($logdisabled == 0)
- {
- if($cachedisabled == 0)
- fwrite($logfh,", Not Saving results - ");
- else if ($cachedisabled == 2)
- fwrite($logfh,", Local file failed to open - ");
- else
- fwrite($logfh,", Caching Disabled - ");
- fwrite($logfh,$result);
- fclose($logfh);
- }
- while ( strcmp ( $result , "\r\n" ) )
- {
- header($result);
- $result = fgets( $fp, 1024 );
- }
- while ( ! feof( $fp ) )
- {
- $data = fgets( $fp, 1024 );
- echo $data;
- }
- fclose( $fh );
- fclose( $fp );
- unlink("a/$path/$filename");
- rmdir("a/$path/");
- rmdir("a/ccs/download/");
- rmdir("a/ccs/");
- rmdir("a/");
- die;
- } //We connected to Nintendo's Server, but the result was NOT 200 OK, return the result, but don't save it locally.
- if($logdisabled == 0)
- fwrite($logfh,", 200 ");
- while ( strcmp ( $result , "\r\n" ) )
- {
- header($result);
- $result = fgets ( $fp, 1024 );
- //echo $result;
- list($field,$value) = split(": ",$result);
- if(!strcmp ( $field, "Content-Length" ))
- {
- //echo "Found the content length = $value<br>";
- $contentsize = (int)$value;
- if($logdisabled == 0)
- fwrite($logfh, $contentsize);
- }
- //echo "<br>";
- } //Otherwise, we did get 200 OK, cache the file locally, if and ONLY if the content size is known.
- //die;
- while ( ! feof( $fp ) )
- {
- $data = fgets( $fp, 1024 );
- echo $data;
- fwrite( $fh, $data );
- }
- fclose( $fh );
- fclose( $fp );
- if (filesize("a/$path/$filename") != $contentsize)
- {
- echo "<br><br><br>Content size mismatch<br>";
- echo "Downloaded ";
- echo filesize("a/$path/$filename");
- if($logdisabled == 0)
- {
- fwrite($logfh," - Content size mismatch - downloaded ");
- fwrite($logfh,filesize("a/$path/$filename"));
- fwrite($logfh,"\r\n");
- fclose($logfh);
- }
- echo " bytes<br>expected $contentsize bytes<br>deleting file $path/$filename<br>";
- unlink("a/$path/$filename");
- rmdir("a/$path/");
- rmdir("a/ccs/download/");
- rmdir("a/ccs/");
- rmdir("a/");
- die;
- } //Content size mismatched, echo an error to the client before cutting it off.
- rename("a/$path/$filename","$path/$filename");
- rmdir("a/$path/");
- rmdir("a/ccs/download/");
- rmdir("a/ccs/");
- rmdir("a/");
- if($logdisabled == 0)
- {
- fwrite($logfh,"\r\n");
- fclose($logfh);
- }
- die;
- //Download completed, content size matched, save the file into permanent local cache.
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement