|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use \App\Skin; |
| 7 | +use \File; |
| 8 | +use \Exception; |
| 9 | +use \MinecraftSkins\MinecraftSkins; |
| 10 | + |
| 11 | +class SkinDownload extends Command { |
| 12 | + |
| 13 | + const SKIN_URL = "http://sessionserver.mojang.com/session/minecraft/profile/<uuid>?unsigned=false"; |
| 14 | + |
| 15 | + protected $signature = 'app:skin {uuid}'; |
| 16 | + protected $description = 'Download and save a skin'; |
| 17 | + |
| 18 | + public function handle() { |
| 19 | + $this->info("Downloading skins"); |
| 20 | + |
| 21 | + $uuid = $this->argument("uuid"); |
| 22 | + $this->downloadSkin($uuid); |
| 23 | + } |
| 24 | + |
| 25 | + function downloadSkin($uuid) { |
| 26 | + $url = str_replace("<uuid>", $uuid, self::SKIN_URL); |
| 27 | + |
| 28 | + $request = curl_init(); |
| 29 | + curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE); |
| 30 | + curl_setopt($request, CURLOPT_FOLLOWLOCATION, TRUE); |
| 31 | + curl_setopt($request, CURLOPT_URL, $url); |
| 32 | + try { |
| 33 | + $response = curl_exec($request); |
| 34 | + |
| 35 | + $curl_info = curl_getinfo($request); |
| 36 | + if ($curl_info['http_code'] !== 200) { |
| 37 | + $this->error("Return code: " . $curl_info['http_code']); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + $data = json_decode($response, true); |
| 42 | + $skinProperties = $data['properties'][0]; |
| 43 | + |
| 44 | + $skin = new Skin(); |
| 45 | + $skin->signature = base64_decode($skinProperties['signature']); |
| 46 | + |
| 47 | + $skinData = json_decode(base64_decode($skinProperties['value']), true); |
| 48 | + $skin->timestamp = $skinData['timestamp']; |
| 49 | + $skin->profile_id = $skinData['profileId']; |
| 50 | + $skin->profile_name = $skinData['profileName']; |
| 51 | + |
| 52 | + $textures = $skinData['textures']; |
| 53 | + if (!isset($textures['SKIN'])) { |
| 54 | + $this->error("User has no skin set"); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + $skinTextures = $textures['SKIN']; |
| 59 | + $skin->skin_url = $skinTextures['url']; |
| 60 | + $skin->slim_model = isset($skinTextures['metadata']); |
| 61 | + $this->saveRendered($uuid, $skin->skin_url); |
| 62 | + |
| 63 | + if (isset($textures['CAPE'])) { |
| 64 | + //user has a cape |
| 65 | + $skin->cape_url = $textures['CAPE']['url']; |
| 66 | + } |
| 67 | + |
| 68 | + $skin->save(); |
| 69 | + } catch (Exception $ex) { |
| 70 | + $this->error($ex); |
| 71 | + } finally { |
| 72 | + curl_close($request); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + function saveRendered($uuid, $url) { |
| 77 | + $rawSkin = imagecreatefromstring(file_get_contents($url)); |
| 78 | + $head = MinecraftSkins::head($rawSkin, 8); |
| 79 | + $skin = MinecraftSkins::skin($rawSkin, 8); |
| 80 | + |
| 81 | + $path = public_path() . "/img/head/$uuid.png"; |
| 82 | + //check if it's still the same folder |
| 83 | + if (File::dirname($path) == public_path() . "/img/head") { |
| 84 | + imagepng($head, $path); |
| 85 | + } |
| 86 | + |
| 87 | + $path = public_path() . "/img/skin/$uuid.png"; |
| 88 | + if (File::dirname($path) == public_path() . "/img/skin") { |
| 89 | + imagepng($skin, $path); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments