Guest User

HackThisSite Remote Mission API

a guest
Mar 30th, 2012
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.40 KB | None | 0 0
  1. <?php
  2. /**
  3. Copyright (c) 2012, HackThisSite.org
  4. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8.     * Redistributions of source code must retain the above copyright
  9.       notice, this list of conditions and the following disclaimer.
  10.     * Redistributions in binary form must reproduce the above copyright
  11.       notice, this list of conditions and the following disclaimer in the
  12.       documentation and/or other materials provided with the distribution.
  13.     * Neither the name of the HackThisSite.org nor the
  14.       names of its contributors may be used to endorse or promote products
  15.       derived from this software without specific prior written permission.
  16.  
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
  18. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  21. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28.  
  29. /**
  30. * Authors:
  31. *   Bren2010 ( Brendan Mc. )
  32. **/
  33.  
  34. class HTSMission {
  35.    
  36.     // Edit these -- they will be assigned to you.
  37.     var $serverId = 0;
  38.     var $missionId = 0;
  39.    
  40.     // Do not edit these
  41.     var $remoteUrl = 'hackthissite.org';
  42.     var $remotePort = '51064';
  43.     var $userId;
  44.    
  45.     // Initiates class.  You should call this on every page load, but
  46.     // it is fine if you only call it on the index page and the
  47.     // winning page of the mission.
  48.     public function __construct() {
  49.         if (!isset($_SESSION)) session_start();
  50.         if (!empty($_SESSION['userId'])) $this->userId = $_SESSION['userId'];
  51.         if (!empty($_GET['userId'])) {
  52.             $_SESSION['userId'] = (int) $_GET['userId'];
  53.             header('Location: ' . $_SERVER['PHP_SELF']);
  54.             die;
  55.         } else if (empty($_SESSION['userId']) && empty($_GET['userId'])) {
  56.             die('Access denied.');
  57.         }
  58.     }
  59.    
  60.     // Call to mark mission as completed.
  61.     public function finish() {
  62.         $socket = @fsockopen($this->remoteUrl, $this->remotePort, $errno, $errstr, 1);
  63.         if (!$socket) die('Unable to connect to HackThisSite\'s servers!  
  64.             Please contact a HackThisSite developer.');
  65.        
  66.         $info = stream_get_meta_data($socket);
  67.         if ($info['timed_out']) die('Coult not contact HTS.');
  68.        
  69.         $serverId = $this->uncompliment($this->serverId);
  70.         $userId = $this->uncompliment($this->userId);
  71.         $missionId = $this->uncompliment($this->missionId);
  72.  
  73.         $toWrite = '';
  74.  
  75.         foreach($serverId as $byte) { $toWrite .= chr($byte); }
  76.         foreach ($userId as $byte) { $toWrite .= chr($byte); }
  77.         foreach ($missionId as $byte) { $toWrite .= chr($byte); }
  78.        
  79.         fwrite($socket, $toWrite);
  80.         fclose($socket);
  81.     }
  82.    
  83.    
  84.     private function uncompliment($integer) {
  85.         $toReturn = array();
  86.  
  87.         while (true) {
  88.                 array_push($toReturn, $integer % 256);
  89.                 $integer = floor($integer / 256);
  90.  
  91.                 if ($integer == 0) break;
  92.         }
  93.  
  94.         return array_reverse(array_pad($toReturn, 4, 0));
  95.     }
  96.    
  97. }
Add Comment
Please, Sign In to add comment