<?php
namespace App\Features\Habbo;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Auth;
class API
{
/**
* Summary API URL
* @var string
*/
public static $summary_url = "https://www.habbo.{country}/api/public/users?name=";
/**
* Detailed API URL
* @var string
*/
public static $detailed_url = "https://www.habbo.{country}/api/public/users/{uniqueId}/profile";
/*
* Photos
*/
public static $photos_url = "https://www.habbo.com/extradata/public/users/{uniqueId}/photos";
/*
* Public photos
*/
public static $public_photos_url = "https://www.habbo.com/extradata/public/photos";
/*
* Rooms
*/
public static $rooms_url = "https://www.habbo.com/api/public/users/{uniqueId}/rooms";
/**
* Store cookie value from habbo
* @var
*/
public static $cookie;
/**
* @var
*/
public static $days_left_until_register_allowed;
/**
* Helper function to extract the correct cookie data from Habbo
* Uses the public photos page as initial example
*/
public static function _getCookie($url)
{
if (!isset(self::$cookie)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_USERAGENT, 'github.com/gerbenjacobs/habbo-api v2.2.0');
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
preg_match("#setCookie\\('(.*)', '(.*)', (.*)\\)#", $data, $matches);
file_put_contents(storage_path(). '/test.html', $data);
if(isset($matches[2])) {
self::$cookie['key'] = $matches[1];
self::$cookie['value'] = $matches[2];
}
}
}
public static function getRecentPhotos()
{
self::_getCookie(self::$public_photos_url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::$public_photos_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHttpHeaders());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIE, self::$cookie['key'] . '=' . self::$cookie['value']);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output);
return $output;
}
/**
* Grab habbo photos
* @param $habbo_username
* @return array
*/
public static function getPhotos($habbo_username)
{
$summary = self::getSummary($habbo_username);
if(isset($summary->uniqueId))
{
$url = str_replace("{uniqueId}", $summary->uniqueId, self::$photos_url);
self::_getCookie($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'github.com/gerbenjacobs/habbo-api v2.2.0');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIE, self::$cookie['key'] . '=' . self::$cookie['value']);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output);
return $output;
}
return [];
}
public static function getRooms($habbo_username)
{
$summary = self::getSummary($habbo_username);
if(isset($summary->uniqueId))
{
$url = str_replace("{uniqueId}", $summary->uniqueId, self::$rooms_url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHttpHeaders());
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output);
if(isset($output->error)) {
return [];
}
return $output;
}
return [];
}
public static function getBadgeCount($habbo_username)
{
$summary = self::getSummary($habbo_username);
if(isset($summary->uniqueId))
{
$profile = self::getDetailed($summary->uniqueId);
return isset($profile->badges) ? count($profile->badges) : 0;
}
else {
return 0;
}
}
public static function validateOwnsBadge($badge_code, $habbo_username = null)
{
$badge_code = !is_array($badge_code) ? [$badge_code] : $badge_code;
$summary = self::getSummary($habbo_username);
$profile = self::getDetailed($summary->uniqueId);
$badges = $profile->badges;
$valid = 0;
/*
if(Auth::id() == 1248)
{
Helper::pr($badges,1);
}
*/
foreach($badges as $badge)
{
if(in_array($badge['code'], $badge_code)) {
$valid++;
}
}
return $valid;
}
public static function getBadges($habbo_username)
{
return cache()->remember('self_getBadges'.$habbo_username, 60, function() use($habbo_username) {
$summary = self::getSummary($habbo_username);
if(isset($summary->profileVisible) && $summary->profileVisible) {
$profile = self::getDetailed($summary->uniqueId);
if(isset($profile->badges))
{
return $profile->badges;
}
}
return [];
});
}
/**
* Get summary object of Habbo Profile
* @param null $habbo_username
* @return object
*/
public static function getSummary($habbo_username = null, $country = 'com')
{
$habbo_username = $habbo_username == null ? Auth::user()->habbo_username : $habbo_username;
$url = str_replace("{country}", $country, self::$summary_url.$habbo_username);
self::_getCookie($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHttpHeaders());
$output = curl_exec($ch);
curl_close($ch);
return (object)json_decode($output, true);
}
public static function profileIsVisible($habbo_username)
{
$summary = self::getSummary($habbo_username);
if(isset($summary->profileVisible) && $summary->profileVisible) {
return true;
}
return false;
}
/**
* Get detailed object of Habbo Profile
* @param null $uniqueId
* @return object
*/
public static function getDetailed($uniqueId = null, $country = 'com')
{
if($uniqueId == null) {
$uniqueId = self::getSummary(Auth::user()->habbo_username)->uniqueId;
}
$url = str_replace("{uniqueId}", $uniqueId, self::$detailed_url);
$url = str_replace("{country}", $country, $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHttpHeaders());
$output = curl_exec($ch);
curl_close($ch);
return (object)json_decode($output, true);
}
public static function getDetailedFromHabboUsername($habbo_username, $hotel = 'com')
{
return self::getDetailed(self::getSummary($habbo_username, $hotel)->uniqueId);
}
/**
* Default headers to use in CURL
* @return array
*/
public static function getHttpHeaders()
{
return array(
//'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2',
//'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',
//'User-Agent: spider',
//'User-Agent: Mozilla/1.22 (compatible; MSIE 5.01; PalmOS 3.0) EudoraWeb 2',
'User-Agent: PostmanRuntime/7.19.0',
'Connection: keep-alive',
'Content-Type: application/json',
'Accept: application/json'
);
}
/**
* Check if a Habbo account is old enough to register
* @param $habbo_username
* @param $country
* @return bool
*/
public static function oldEnough($habbo_username, $country)
{
$days = self::getHabboAccountMinimumAgeDays();
$summary = self::getSummary($habbo_username, $country);
$memberSince = strtotime($summary->memberSince);
$memberSinceAddDays = strtotime('+'.$days.' days', $memberSince);
$now = time();
self::$days_left_until_register_allowed = floor(($memberSinceAddDays - $now) / (60 * 60 * 24) + 1);
return $memberSinceAddDays < $now;
}
/**
* Get the minimum age for a Habbo account in days
* @return mixed
*/
public static function getHabboAccountMinimumAgeDays()
{
return Config::get('habbo.register_min_age_days');
}
public static function canChangeHabboUsername()
{
$user = Auth::user();
$lastTime = $user->habbo_username_last_change;
if($lastTime == null)
{
return true;
}
$waitTimeStr = Config::get('habbo.change_wait_time');
$expireTime = strtotime($waitTimeStr, strtotime($lastTime));
if(time() < $expireTime) {
return false;
}
return true;
}
public static function getChangeHabboUsernameAllowedDate()
{
$user = Auth::user();
$waitTimeStr = Config::get('habbo.change_wait_time');
$lastTime = $user->habbo_username_last_change;
return Date('Y-m-d H:i', strtotime($waitTimeStr, strtotime($lastTime)));
}
public static function verifyMotto($habbo_username, $_hotel = 'com', $motto)
{
$hotel = str_replace('.','', $_hotel);
$summary = self::getSummary($habbo_username, $hotel);
return isset($summary->motto) && $summary->motto == $motto;
}
}
Comments