[PHP] Restock Alerter

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

  1. Archon

    Archon Newbie

    Joined:
    Jan 20, 2009
    Messages:
    2
    Likes Received:
    2
    Code (Text):
    1.    <?php
    2.  
    3.     /*****************************
    4.      * Neopets Restock Alerter   *
    5.      * Author: Archon            *
    6.      * Start Date: 05/01/2009    *
    7.      * Last Update: 05/02/2009   *
    8.      *****************************/
    9.     // Suggestions
    10.     // Better GUI
    11.      
    12.      set_time_limit(0);
    13.      
    14.      // START PHP WRAPPER
    15.      
    16.      class httpwrapper {
    17.         private $cookies;
    18.         private $lastpage;
    19.         private $html;
    20.         private $headers;
    21.  
    22.         function __construct() {
    23.             $this->useproxy = false;
    24.             $this->cookies = array();
    25.             $this->lastpage = NULL;
    26.         }
    27.  
    28.         public function getLastPage() {
    29.             return $this->lastpage;
    30.         }
    31.  
    32.         public function clearCookies() {
    33.             $this->cookies = array();
    34.         }
    35.  
    36.         public function req($type, $url, $postdata = NULL, $referer = NULL, $follow_location = true, $return_headers = false) {
    37.             if($referer == NULL) {
    38.                 if($this->lastpage != NULL)
    39.                     $r = "Referer: " . $this->lastpage . "\r\n";
    40.             } else
    41.                 $r = "Referer: $referer\r\n";
    42.  
    43.             $url = str_replace("http://", "", $url);
    44.             $host = $this->getHost($url);
    45.             $path = $this->getPath($url);
    46.  
    47.             $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8";
    48.  
    49.             $type = strtoupper($type);
    50.             if(!in_array($type,array("GET","POST"))) $type="GET";
    51.  
    52.             $f = fsockopen($host . ":80");
    53.             if($f) {
    54.                 $h = "$type $path HTTP/1.1\r\n";
    55.                 $h .= "HOST: $host\r\n";
    56.                 $h .= "User-Agent: $agent\r\n";
    57.                 $h .= $this->cookieString();
    58.                 $h .= $r;
    59.                 if($type == "POST") {
    60.                     $length = strlen($postdata);
    61.                     $h .= "Content-Type: application/x-www-form-urlencoded\r\n";
    62.                     $h .= "Content-Length: $length\r\n";
    63.                 }
    64.                 $h .= "Connection: close\r\n\r\n";
    65.  
    66.                 if($type == "POST")
    67.                     $h .= $postdata;
    68.  
    69.                 fwrite($f,$h);
    70.                 while(!feof($f))
    71.                     $data.=fgets($f,256);
    72.                 list($headers,$html) = explode("\r\n\r\n", $data, 2);
    73.                 //list($rand,$html) = explode("\r\n", $html, 2);
    74.                 $this->html = $html;
    75.                 $this->lastpage = "http://".$url;
    76.                 $this->process_headers($headers,$url,$follow_location,$return_headers);
    77.                 return ($return_headers) ? $this->headers : $this->html;
    78.             }
    79.         }
    80.  
    81.         private function getHost($url) {
    82.             if(strpos($url,"/") === FALSE)
    83.                 return $url;
    84.             else
    85.                 return $this->gsb("<".$url,"<","/");
    86.         }
    87.  
    88.         private function getPath($url) {
    89.             if(strpos($url,"/") === FALSE)
    90.                 return "/";
    91.             else
    92.                 return "/".$this->gsb($url.">","/",">");
    93.         }
    94.  
    95.         private function process_headers($headers,$url,$follow,$return) {
    96.             $this->headers = $headers;
    97.             $lines = explode("\n", $headers);
    98.             foreach($lines AS $line) {
    99.                 if(substr($line,0,10) == "Set-Cookie") {
    100.                     $cookie = $this->gsb($line, "Set-Cookie: ", ";");
    101.                     list($key,$val) = explode("=", $cookie, 2);
    102.                     $this->cookies[$key] = $val;
    103.                 } else if(substr($line,0,8) == "Location" && $follow == true) {
    104.                     $location = $this->gsb($line.">>", "Location: ", ">>");
    105.                     $this->req("GET", "http://".$this->getHost($url).$location, null, null, true, $return);
    106.                 }
    107.             }
    108.         }
    109.  
    110.         private function cookieString() {
    111.             if(count($this->cookies) > 0) {
    112.                 $string = "Cookie:";
    113.                 foreach($this->cookies AS $k=>$v)
    114.                     $string .= " $k=$v;";
    115.                 return $string."\r\n";
    116.             }
    117.         }
    118.  
    119.         private function gsb($search, $start, $end) {
    120.             list($a,$b) = explode($start, $search, 2);
    121.             list($a,$b) = explode($end, $b, 2);
    122.             return $a;
    123.         }
    124.     }
    125.    
    126.     // END PHP WRAPPER
    127.    
    128.     $form = <<<FRM
    129.     <html>
    130.     <h2>Neopets Restock Alerter</h2>
    131.     <form method="POST">
    132.     Neopets Username: <input type="text" name="user" /><br />
    133.     Neopets Password: <input type="password" name="pass" /><br /><br /><br />
    134.     <h4>Restock Alerter Settings</h4>
    135.     Refresh Time: <input type="text" name="min" size="2" /> - <input type="text" name="max" size="2" /><br /><br />
    136.     Shop ID: <input type="text" maxlength="3" name="shopid" size="2" /><br /><br />
    137.     Check for Restock Ban?: <input type="checkbox" name="restockban" /> <br />
    138.     ------ Restock Ban Refresher (In Minutes)
    139.     <input type="text" name="rmin" size="2" /> - <input type="text" name="rmax" size="2" /><br /><br />
    140.     <input type="checkbox" name="pausetime" /> Pause every <input type="text" name="pminevery" size="2" /> - <input type="text" name="pmaxevery" size="2" /> minutes for <input type="text" name="pminfor" size="2" /> - <input type="text" name="pmaxfor" size="2" /> minutes.<br /><br />
    141.     List Input
    142.     <textarea name="itemlist"></textarea><br />
    143.     <input type="submit" name="Submit" value="Submit" />
    144.     </form>
    145.     </html>
    146. FRM;
    147.  
    148.     $banned = <<<FRM
    149.     <script>alert('You have been restock banned! :(');</script>
    150. FRM;
    151.  
    152.     if(!$_POST["user"] || !$_POST["pass"] || !$_POST['min'] || !$_POST['max'] || !$_POST['shopid'] || !$_POST['itemlist'])
    153.         die($form);
    154.    
    155.  
    156.     $user = $_POST['user'];
    157.     $pass = $_POST['pass'];
    158.     $refreshTimeMin = $_POST['min'];
    159.     $refreshTimeMax = $_POST['max'];
    160.     $shop = $_POST['shopid'];
    161.     $restockBanCheck = $_POST['restockban'];
    162.     $restockBanMin = $_POST['rmin'] * 60;
    163.     $restockBanMax = $_POST['rmax'] * 60;
    164.     $pauseTimeCheck = $_POST['pausetime'];
    165.     $pauseEveryMin = $_POST['pminevery'] * 60;
    166.     $pauseEveryMax = $_POST['pmaxevery'] * 60;
    167.     $pauseForMin = $_POST['pminfor'] * 60;
    168.     $pauseForMax = $_POST['pmaxfor'] * 60;
    169.     $itemList = $_POST['itemlist'];
    170.     $items = explode("\r\n", $itemList);
    171.     $soldOut = "Sorry, we are sold out of everything! We get restocked every eight minutes or so, so please come back soon.";
    172.     $restock = <<<FRM
    173.     <script type="text/javascript">
    174.     var win = window.open().document;
    175.     win.location = "http://www.neopets.com/objects.phtml?type=shop&obj_type=$shop";
    176.     </script>
    177.     Shop has restocked with one of the items that was on the list! Now restart this Refresher to continue!
    178. FRM;
    179.  
    180.     $w = new httpwrapper();
    181.     $login = $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);
    182.  
    183.     if(strpos($login, "badpassword.phtml") !== FALSE)
    184.         die("Bad Password!");
    185.     elseif(strpos($login, "failed_bday") !== FALSE)
    186.         die("Birthday Needed!");
    187.     elseif(strpos($w->req("GET", "http://www.neopets.com/"), "Welcome, <a") === FALSE)
    188.         die("Reason for error is unknown. You could possibly be frozen! ;-;");
    189.        
    190.     $time = time(); $time2 = $time;
    191.     while(true) {
    192.         $rsWait = rand($restockBanMin, $restockBanMax);
    193.         $pauseWait = rand($pauseEveryMin, $pauseEveryMax);
    194.         $pBetween = (time() - $time) / 60;
    195.         $rBetween = (time() - $time2) / 60;
    196.  
    197.         if($pBetween >= $pauseWait) {
    198.             sleep(rand($pauseForMin, $pauseForMax));
    199.             $time = time();
    200.         }
    201.  
    202.         $shophtml = $w->req("GET", "http://www.neopets.com/objects.phtml?type=shop&obj_type=$shop");
    203.  
    204.         if(strpos($shophtml, $soldOut) !== FALSE && $rBetween >= $rsWait) {
    205.             $restockBanHTML = $w->req("GET", "http://www.neopets.com/objects.phtml?type=shop&obj_type=21");
    206.             if(strpos($restockBanHTML, $soldOut) !== FALSE);
    207.                 die($banned);
    208.             $time2 = time();
    209.         }
    210.         foreach($items AS $item) {
    211.             if(strpos($shophtml, $item) !== FALSE) {
    212.                 die($restock);
    213.             }
    214.         }
    215.      }
    216. ?>
     
    Heya_old likes this.
  2. Heya_old

    Heya_old Level IV

    Joined:
    Mar 31, 2008
    Messages:
    928
    Likes Received:
    46
    Nice code :) keep up the good work :)
     
  3. dreamlorde

    dreamlorde Level III

    Joined:
    Feb 17, 2007
    Messages:
    470
    Likes Received:
    32
    Location:
    Tijuana
    What does this restock alerter do?

    Forgive me, I'mnot a programmer so I can't just read the code and figure it out. But the title makes it sound very interesting.
     
  4. krilas

    krilas Level II

    Joined:
    Jan 1, 2009
    Messages:
    324
    Likes Received:
    2
    Location:
    Singapore
    Seems like it just refreshes the page continuously and checks against a list to see if the shop has restocked, when it does it will alert you and ask you to reload the page in order for it to check again. Basically if you are a "legit" player this might come in handy since you don't have to refresh the page continuously, instead it alerts you when the shop does restock.
     
  5. ricothegreat221

    ricothegreat221 Level II

    Joined:
    May 8, 2009
    Messages:
    302
    Likes Received:
    3
    Well I hate to sounds like I'm trying to devalue your hard work, but isn't this just a weak autobuyer?
     
  6. dreamlorde

    dreamlorde Level III

    Joined:
    Feb 17, 2007
    Messages:
    470
    Likes Received:
    32
    Location:
    Tijuana
    Thanks for the reply... sounds very cool. So, uh, how would someone use it?
     
  7. Freya

    Freya Level III

    Joined:
    May 18, 2009
    Messages:
    437
    Likes Received:
    12
    I put it on my php server... looks like the script checks for a restock but I can't really see the use of it... Whenever the script alerts you it should be too late for you to get any of the items.
    Correct me if thats wrong...
     
  8. boredhacker2

    boredhacker2 Newbie

    Joined:
    Jun 28, 2010
    Messages:
    30
    Likes Received:
    0
    How do we convert this into a program?
     
  9. Josh21227

    Josh21227 Level I

    Joined:
    May 23, 2009
    Messages:
    57
    Likes Received:
    10
    ...It already is a program. Someone already explained how to run it. Try reading. "boredhacker2" isn't a great name for you if you don't even know what PHP is.
     
  10. Fantersam

    Fantersam Level I

    Joined:
    Aug 16, 2009
    Messages:
    88
    Likes Received:
    2
    Okay.
    This is really what i looked for.
    can somebody tell me how to put this into a program or how to run it ...
    sorry i'm a noob in such things.
     
  11. Shawn

    Shawn Level IV

    Joined:
    Jul 15, 2009
    Messages:
    1,989
    Likes Received:
    76
    Location:
    Somewhere, lah.
    What exactly are you looking for?
     
  12. Fantersam

    Fantersam Level I

    Joined:
    Aug 16, 2009
    Messages:
    88
    Likes Received:
    2
    For a restock alerter. But i dont know how to use it.
     
  13. Shawn

    Shawn Level IV

    Joined:
    Jul 15, 2009
    Messages:
    1,989
    Likes Received:
    76
    Location:
    Somewhere, lah.
    You have to upload the php script onto a webhost and then go to the php page you uploaded
     
  14. Fantersam

    Fantersam Level I

    Joined:
    Aug 16, 2009
    Messages:
    88
    Likes Received:
    2
    And how i can use it then?
    :S
    im just looking for a program that shows me if a shop restocks
     
  15. Lightning

    Lightning Administrator
    Staff Member

    Joined:
    Nov 8, 2008
    Messages:
    3,021
    Likes Received:
    195
    Gender:
    Male
    Location:
    Florida, USA
    This isn't really a 'program' that you are talking about. You're probably thinking about a Windows Application...this script has to be uploaded on your own website.