[PHP] Neopets Avatar Refresher

Discussion in 'Code Snippets and Tutorials' started by Archon, May 1, 2009.

  1. Archon

    Archon Newbie

    Joined:
    Jan 20, 2009
    Messages:
    2
    Likes Received:
    2
    Code (Text):
    1. <?php
    2.  
    3. /**
    4.  ******************
    5.  * Neopets Avatar *
    6.  *     Getter     *
    7.  ******************
    8.  * @author Archon *
    9.  * @copyright 2009*
    10.  ******************
    11.  */
    12.  
    13.     set_time_limit(0);
    14.    
    15.     class httpwrapper {
    16.         private $cookies;
    17.         private $lastpage;
    18.         private $html;
    19.         private $headers;
    20.  
    21.         function __construct() {
    22.             $this->useproxy = false;
    23.             $this->cookies = array();
    24.             $this->lastpage = NULL;
    25.         }
    26.  
    27.         public function getLastPage() {
    28.             return $this->lastpage;
    29.         }
    30.  
    31.         public function clearCookies() {
    32.             $this->cookies = array();
    33.         }
    34.  
    35.         public function req($type, $url, $postdata = NULL, $referer = NULL, $follow_location = true, $return_headers = false) {
    36.             if($referer == NULL) {
    37.                 if($this->lastpage != NULL)
    38.                     $r = "Referer: " . $this->lastpage . "\r\n";
    39.             } else
    40.                 $r = "Referer: $referer\r\n";
    41.  
    42.             $url = str_replace("http://", "", $url);
    43.             $host = $this->getHost($url);
    44.             $path = $this->getPath($url);
    45.  
    46.             $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8";
    47.  
    48.             $type = strtoupper($type);
    49.             if(!in_array($type,array("GET","POST"))) $type="GET";
    50.  
    51.             $f = fsockopen($host . ":80");
    52.             if($f) {
    53.                 $h = "$type $path HTTP/1.1\r\n";
    54.                 $h .= "HOST: $host\r\n";
    55.                 $h .= "User-Agent: $agent\r\n";
    56.                 $h .= $this->cookieString();
    57.                 $h .= $r;
    58.                 if($type == "POST") {
    59.                     $length = strlen($postdata);
    60.                     $h .= "Content-Type: application/x-www-form-urlencoded\r\n";
    61.                     $h .= "Content-Length: $length\r\n";
    62.                 }
    63.                 $h .= "Connection: close\r\n\r\n";
    64.  
    65.                 if($type == "POST")
    66.                     $h .= $postdata;
    67.  
    68.                 fwrite($f,$h);
    69.                 while(!feof($f))
    70.                     $data.=fgets($f,256);
    71.                 list($headers,$html) = explode("\r\n\r\n", $data, 2);
    72.                 //list($rand,$html) = explode("\r\n", $html, 2);
    73.                 $this->html = $html;
    74.                 $this->lastpage = "http://".$url;
    75.                 $this->process_headers($headers,$url,$follow_location,$return_headers);
    76.                 return ($return_headers) ? $this->headers : $this->html;
    77.             }
    78.         }
    79.  
    80.         private function getHost($url) {
    81.             if(strpos($url,"/") === FALSE)
    82.                 return $url;
    83.             else
    84.                 return $this->gsb("<".$url,"<","/");
    85.         }
    86.  
    87.         private function getPath($url) {
    88.             if(strpos($url,"/") === FALSE)
    89.                 return "/";
    90.             else
    91.                 return "/".$this->gsb($url.">","/",">");
    92.         }
    93.  
    94.         private function process_headers($headers,$url,$follow,$return) {
    95.             $this->headers = $headers;
    96.             $lines = explode("\n", $headers);
    97.             foreach($lines AS $line) {
    98.                 if(substr($line,0,10) == "Set-Cookie") {
    99.                     $cookie = $this->gsb($line, "Set-Cookie: ", ";");
    100.                     list($key,$val) = explode("=", $cookie, 2);
    101.                     $this->cookies[$key] = $val;
    102.                 } else if(substr($line,0,8) == "Location" && $follow == true) {
    103.                     $location = $this->gsb($line.">>", "Location: ", ">>");
    104.                     $this->req("GET", "http://".$this->getHost($url).$location, null, null, true, $return);
    105.                 }
    106.             }
    107.         }
    108.  
    109.         private function cookieString() {
    110.             if(count($this->cookies) > 0) {
    111.                 $string = "Cookie:";
    112.                 foreach($this->cookies AS $k=>$v)
    113.                     $string .= " $k=$v;";
    114.                 return $string."\r\n";
    115.             }
    116.         }
    117.  
    118.         private function gsb($search, $start, $end) {
    119.             list($a,$b) = explode($start, $search, 2);
    120.             list($a,$b) = explode($end, $b, 2);
    121.             return $a;
    122.         }
    123.     }
    124.  
    125.     $form = <<<EOF
    126. <html>
    127. <h3>Neopets Avatar Getter</h3>
    128. <form method="POST">
    129. Neopets Username: <input type="text" name="user" /><br />
    130. Neopets Password: <input type="text" name="pass" /><br /><br /><br />
    131. <h4>Avatars Needed</h4>
    132. <img src="http://images.neopets.com/neoboards/avatars/pooralbert.gif"> <input type="checkbox" name="mutant" /><br /><br />
    133. <img src="http://images.neopets.com/neoboards/avatars/haiku.gif"> <input type="checkbox" name="rorru" /><br /><br /><br />
    134. Refresh Time: <input type="text" name="min" size="2" /> - <input type="text" name="max" size="2" /><br /><br />
    135. <input type="submit" name="Submit" />
    136. </form>
    137. </html>
    138. EOF;
    139.  
    140.     if(!$_POST["user"] || !$_POST["pass"])
    141.         die($form);
    142.  
    143.     $user = $_POST['user'];
    144.     $pass = $_POST['pass'];
    145.  
    146.     $w = new httpwrapper();
    147.     $html = $w->req("POST", "http://www.neopets.com/login.phtml", "username=$user&password=$pass&destination=%2Findex.phtml&x=".rand(0,100)."&y=".rand(0,30), "http://www.neopets.com/hi.phtml", false, true);
    148.  
    149.     if(strpos($html, "badpassword.phtml") !== FALSE)
    150.         die("Bad Password!");
    151.     elseif(strpos($html, "failed_bday") !== FALSE)
    152.         die("Birthday Needed!");
    153.     elseif(strpos($w->req("GET", "http://www.neopets.com/"), "Welcome, <a") === FALSE)
    154.         die("Reason for error is unknown. You could possibly be frozen! ;-;");
    155.    
    156.     $mutant = $_POST['mutant'];
    157.     $rorru = $_POST['rorru'];
    158.     $min = (int)$_POST["min"];
    159.     $max = (int)$_POST["max"];
    160.    
    161.      if($mutant == "on") {
    162.          $av = $w->req("GET", "http://www.neopets.com/neoboards/preferences.phtml");
    163.          if(strpos($av, "value='pooralbert'") !== FALSE)
    164.              echo "You already have the Mutant Graveyard of Doom avatar!";
    165.          else {
    166.              while(strpos($html, "http://images.neopets.com/neoboards/avatars/pooralbert.gif") === FALSE){
    167.                  $html = $w->req("GET", "http://www.neopets.com/halloween/gamegraveyard.phtml");
    168.                  sleep(rand($min, $max));
    169.              }
    170.              echo "Mutant Graveyard of Doom avatar obtained!";
    171.         }
    172.      }
    173.        
    174.      if($rorru == "on") {
    175.          $av = $w->req("GET", "http://www.neopets.com/neoboards/preferences.phtml");
    176.          if(strpos($av, "value='haiku'") !== FALSE)
    177.              echo "You already have the Rorru avatar!";
    178.          else {
    179.              while(strpos($html, "http://images.neopets.com/neoboards/avatars/haiku.gif") === FALSE){
    180.                  $html = $w->req("GET", "http://www.neopets.com/island/haiku/haiku.phtml");
    181.                  sleep(rand($min, $max));
    182.              }
    183.             echo "Rorru avatar obtained!";
    184.         }
    185.      }
    186.    
    187.  
    188. ?>
    Made by me (Wrapper was Charlie's doing)

    This gets Haiku and the Mutant Graveyard of Doom Avatar! And with random wait time! Enjoy.

    If you know PHP you can edit the avatars and change it to which one you want to get. ;o

    Oh and I uploaded this because the programming section needs to be more active. ):
     
    Cacklenub likes this.
  2. ninopino1

    ninopino1 Newbie

    Joined:
    Oct 17, 2008
    Messages:
    4
    Likes Received:
    0
    Re: Neopets Avatar Refresher

    I think there is a script that is way easier than this one?
     
  3. Anfan

    Anfan Level IV

    Joined:
    Feb 12, 2009
    Messages:
    1,327
    Likes Received:
    105
    Location:
    USA
    Re: Neopets Avatar Refresher

    The "Helpful Zafara" avatar (do a search on "Avatar" in the help section of Neo) also takes random refreshing, although not nearly as much as Mutant Graveyard of DOOM does.

    And ninopino1, if you want to on your own main or whatever, you can always just hold down F5 for a bit. Or, if you're on Firefox, download/enable ReloadEvery, use custom for 1 second, and just leave that running for a bit. (Refreshing a page exactly every 1 second would have a much higher freeze rate than random waiting times, but it's only for a few avatars...I've done that on several accounts and have yet to be frozen.) Then just check "Preferences" on the NeoBoards to see if/when you've gotten the avatars.

    And, of course, kudos to you, Archon. *thumbs up*