Advertisement
benshepherd

HabboCreate's old habbo

Nov 4th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.91 KB | None | 0 0
  1. <?php
  2. namespace App\Features\Habbo;
  3. use Illuminate\Support\Facades\Config;
  4. use Illuminate\Support\Facades\Auth;
  5.  
  6. class API
  7. {
  8.     /**
  9.      * Summary API URL
  10.      * @var string
  11.      */
  12.     public static $summary_url = "https://www.habbo.{country}/api/public/users?name=";
  13.     /**
  14.      * Detailed API URL
  15.      * @var string
  16.      */
  17.     public static $detailed_url = "https://www.habbo.{country}/api/public/users/{uniqueId}/profile";
  18.  
  19.     /*
  20.      * Photos
  21.      */
  22.     public static $photos_url = "https://www.habbo.com/extradata/public/users/{uniqueId}/photos";
  23.  
  24.     /*
  25.     * Public photos
  26.     */
  27.     public static $public_photos_url = "https://www.habbo.com/extradata/public/photos";
  28.  
  29.     /*
  30.     * Rooms
  31.     */
  32.     public static $rooms_url = "https://www.habbo.com/api/public/users/{uniqueId}/rooms";
  33.  
  34.     /**
  35.      * Store cookie value from habbo
  36.      * @var
  37.      */
  38.     public static $cookie;
  39.  
  40.     /**
  41.      * @var
  42.      */
  43.     public static $days_left_until_register_allowed;
  44.  
  45.     /**
  46.      * Helper function to extract the correct cookie data from Habbo
  47.      * Uses the public photos page as initial example
  48.      */
  49.     public static function _getCookie($url)
  50.     {
  51.         if (!isset(self::$cookie)) {
  52.             $ch = curl_init();
  53.             curl_setopt($ch, CURLOPT_URL, $url);
  54. //            curl_setopt($ch, CURLOPT_USERAGENT, 'github.com/gerbenjacobs/habbo-api v2.2.0');
  55. //            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  56. //            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  57.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  58.             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  59.             $data = curl_exec($ch);
  60.             curl_close($ch);
  61.  
  62.             preg_match("#setCookie\\('(.*)', '(.*)', (.*)\\)#", $data, $matches);
  63.             file_put_contents(storage_path(). '/test.html', $data);
  64.  
  65.             if(isset($matches[2])) {
  66.                 self::$cookie['key'] = $matches[1];
  67.                 self::$cookie['value'] = $matches[2];
  68.             }
  69.         }
  70.     }
  71.  
  72.     public static function getRecentPhotos()
  73.     {
  74.         self::_getCookie(self::$public_photos_url);
  75.  
  76.         $ch = curl_init();
  77.         curl_setopt($ch, CURLOPT_URL, self::$public_photos_url);
  78.         curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHttpHeaders());
  79.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  80.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  81.         curl_setopt($ch, CURLOPT_COOKIE, self::$cookie['key'] . '=' . self::$cookie['value']);
  82.         $output = curl_exec($ch);
  83.         curl_close($ch);
  84.         $output = json_decode($output);
  85.  
  86.         return $output;
  87.     }
  88.  
  89.     /**
  90.      * Grab habbo photos
  91.      * @param $habbo_username
  92.      * @return array
  93.      */
  94.     public static function getPhotos($habbo_username)
  95.     {
  96.         $summary = self::getSummary($habbo_username);
  97.  
  98.  
  99.         if(isset($summary->uniqueId))
  100.         {
  101.             $url = str_replace("{uniqueId}", $summary->uniqueId, self::$photos_url);
  102.             self::_getCookie($url);
  103.  
  104.  
  105.             $ch = curl_init();
  106.             curl_setopt($ch, CURLOPT_URL, $url);
  107.             curl_setopt($ch, CURLOPT_USERAGENT, 'github.com/gerbenjacobs/habbo-api v2.2.0');
  108.             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  109.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  110.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  111.             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  112.             curl_setopt($ch, CURLOPT_COOKIE, self::$cookie['key'] . '=' . self::$cookie['value']);
  113.             $output = curl_exec($ch);
  114.             curl_close($ch);
  115.             $output = json_decode($output);
  116.  
  117.             return $output;
  118.         }
  119.  
  120.         return [];
  121.     }
  122.  
  123.     public static function getRooms($habbo_username)
  124.     {
  125.  
  126.         $summary = self::getSummary($habbo_username);
  127.  
  128.         if(isset($summary->uniqueId))
  129.         {
  130.             $url = str_replace("{uniqueId}", $summary->uniqueId, self::$rooms_url);
  131.  
  132.             $ch = curl_init();
  133.             curl_setopt($ch, CURLOPT_URL, $url);
  134.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  135.             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  136.             curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHttpHeaders());
  137.             $output = curl_exec($ch);
  138.             curl_close($ch);
  139.             $output = json_decode($output);
  140.  
  141.             if(isset($output->error)) {
  142.                 return [];
  143.             }
  144.  
  145.             return $output;
  146.         }
  147.  
  148.         return [];
  149.     }
  150.  
  151.  
  152.     public static function getBadgeCount($habbo_username)
  153.     {
  154.         $summary = self::getSummary($habbo_username);
  155.  
  156.         if(isset($summary->uniqueId))
  157.         {
  158.             $profile = self::getDetailed($summary->uniqueId);
  159.             return isset($profile->badges) ? count($profile->badges) : 0;
  160.         }
  161.         else {
  162.             return 0;
  163.         }
  164.     }
  165.  
  166.     public static function validateOwnsBadge($badge_code, $habbo_username = null)
  167.     {
  168.         $badge_code = !is_array($badge_code) ? [$badge_code] : $badge_code;
  169.         $summary = self::getSummary($habbo_username);
  170.         $profile = self::getDetailed($summary->uniqueId);
  171.         $badges = $profile->badges;
  172.         $valid = 0;
  173.  
  174.         /*
  175.                 if(Auth::id() == 1248)
  176.                 {
  177.                     Helper::pr($badges,1);
  178.                 }
  179.         */
  180.  
  181.         foreach($badges as $badge)
  182.         {
  183.             if(in_array($badge['code'], $badge_code)) {
  184.                 $valid++;
  185.             }
  186.         }
  187.  
  188.         return $valid;
  189.     }
  190.  
  191.     public static function getBadges($habbo_username)
  192.     {
  193.         return cache()->remember('self_getBadges'.$habbo_username, 60, function() use($habbo_username) {
  194.             $summary = self::getSummary($habbo_username);
  195.  
  196.             if(isset($summary->profileVisible) && $summary->profileVisible) {
  197.                 $profile = self::getDetailed($summary->uniqueId);
  198.  
  199.                 if(isset($profile->badges))
  200.                 {
  201.                     return $profile->badges;
  202.                 }
  203.             }
  204.  
  205.             return [];
  206.         });
  207.     }
  208.  
  209.     /**
  210.      * Get summary object of Habbo Profile
  211.      * @param null $habbo_username
  212.      * @return object
  213.      */
  214.     public static function getSummary($habbo_username = null, $country = 'com')
  215.     {
  216.         $habbo_username = $habbo_username == null ? Auth::user()->habbo_username : $habbo_username;
  217.         $url = str_replace("{country}", $country, self::$summary_url.$habbo_username);
  218.  
  219.         self::_getCookie($url);
  220.  
  221.         $ch = curl_init();
  222.  
  223.         curl_setopt($ch, CURLOPT_URL, $url);
  224.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  225.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  226.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  227.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  228.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  229.         curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHttpHeaders());
  230.         $output = curl_exec($ch);
  231.  
  232.         curl_close($ch);
  233.  
  234.         return (object)json_decode($output, true);
  235.     }
  236.  
  237.     public static function profileIsVisible($habbo_username)
  238.     {
  239.         $summary = self::getSummary($habbo_username);
  240.  
  241.         if(isset($summary->profileVisible) && $summary->profileVisible) {
  242.             return true;
  243.         }
  244.  
  245.         return false;
  246.     }
  247.  
  248.     /**
  249.      * Get detailed object of Habbo Profile
  250.      * @param null $uniqueId
  251.      * @return object
  252.      */
  253.     public static function getDetailed($uniqueId = null, $country = 'com')
  254.     {
  255.         if($uniqueId == null) {
  256.             $uniqueId = self::getSummary(Auth::user()->habbo_username)->uniqueId;
  257.         }
  258.  
  259.         $url = str_replace("{uniqueId}", $uniqueId, self::$detailed_url);
  260.         $url = str_replace("{country}", $country, $url);
  261.         $ch = curl_init();
  262.  
  263.         curl_setopt($ch, CURLOPT_URL, $url);
  264.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  265.         curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHttpHeaders());
  266.         $output = curl_exec($ch);
  267.  
  268.         curl_close($ch);
  269.  
  270.         return (object)json_decode($output, true);
  271.     }
  272.  
  273.     public static function getDetailedFromHabboUsername($habbo_username, $hotel = 'com')
  274.     {
  275.         return self::getDetailed(self::getSummary($habbo_username, $hotel)->uniqueId);
  276.     }
  277.  
  278.     /**
  279.      * Default headers to use in CURL
  280.      * @return array
  281.      */
  282.     public static function getHttpHeaders()
  283.     {
  284.         return array(
  285.             //'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2',
  286.             //'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13',
  287.             //'User-Agent: spider',
  288.             //'User-Agent: Mozilla/1.22 (compatible; MSIE 5.01; PalmOS 3.0) EudoraWeb 2',
  289.             'User-Agent: PostmanRuntime/7.19.0',
  290.             'Connection: keep-alive',
  291.             'Content-Type: application/json',
  292.             'Accept: application/json'
  293.         );
  294.     }
  295.     /**
  296.      * Check if a Habbo account is old enough to register
  297.      * @param $habbo_username
  298.      * @param $country
  299.      * @return bool
  300.      */
  301.     public static function oldEnough($habbo_username, $country)
  302.     {
  303.         $days = self::getHabboAccountMinimumAgeDays();
  304.         $summary = self::getSummary($habbo_username, $country);
  305.         $memberSince = strtotime($summary->memberSince);
  306.         $memberSinceAddDays = strtotime('+'.$days.' days', $memberSince);
  307.         $now = time();
  308.  
  309.         self::$days_left_until_register_allowed = floor(($memberSinceAddDays - $now) / (60 * 60 * 24) + 1);
  310.  
  311.         return $memberSinceAddDays < $now;
  312.     }
  313.  
  314.     /**
  315.      * Get the minimum age for a Habbo account in days
  316.      * @return mixed
  317.      */
  318.     public static function getHabboAccountMinimumAgeDays()
  319.     {
  320.         return Config::get('habbo.register_min_age_days');
  321.     }
  322.  
  323.     public static function canChangeHabboUsername()
  324.     {
  325.         $user = Auth::user();
  326.         $lastTime = $user->habbo_username_last_change;
  327.  
  328.         if($lastTime == null)
  329.         {
  330.             return true;
  331.         }
  332.  
  333.         $waitTimeStr = Config::get('habbo.change_wait_time');
  334.         $expireTime = strtotime($waitTimeStr, strtotime($lastTime));
  335.  
  336.         if(time() < $expireTime) {
  337.             return false;
  338.         }
  339.  
  340.         return true;
  341.     }
  342.  
  343.     public static function getChangeHabboUsernameAllowedDate()
  344.     {
  345.         $user = Auth::user();
  346.         $waitTimeStr = Config::get('habbo.change_wait_time');
  347.         $lastTime = $user->habbo_username_last_change;
  348.  
  349.         return Date('Y-m-d H:i', strtotime($waitTimeStr, strtotime($lastTime)));
  350.     }
  351.  
  352.     public static function verifyMotto($habbo_username, $_hotel = 'com', $motto)
  353.     {
  354.         $hotel = str_replace('.','', $_hotel);
  355.         $summary = self::getSummary($habbo_username, $hotel);
  356.  
  357.         return isset($summary->motto) && $summary->motto == $motto;
  358.     }
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement