Kundello

Untitled

Feb 26th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. <?php
  2. declare(strict_types = 1);
  3.  
  4. namespace App\Services\Character;
  5.  
  6.  
  7. use App\Exceptions\User\Character\InvalidImageSizeException;
  8. use Illuminate\Http\UploadedFile;
  9.  
  10.  
  11. /**
  12. * Class UploadedSkin
  13. * Represents a user-loaded skin.
  14. *
  15. * @author D3lph1 <[email protected]>
  16. * @package App\Services\Character
  17. */
  18. class UploadedSkin
  19. {
  20. /**
  21. * @var UploadedFile
  22. */
  23. protected $file;
  24.  
  25. /**
  26. * @var \Intervention\Image\Image
  27. */
  28. protected $image;
  29.  
  30. /**
  31. * UploadedSkin constructor.
  32. *
  33. * @param UploadedFile $file
  34. */
  35. public function __construct(UploadedFile $file)
  36. {
  37. $this->file = $file;
  38. $this->image = \Image::make($file);
  39. }
  40.  
  41. /**
  42. * Check image size on valid.
  43. *
  44. * @param bool $canSetHdSkin
  45. *
  46. * @throws InvalidImageSizeException
  47. * @return bool
  48. */
  49. public function validate($canSetHdSkin)
  50. {
  51. $height = $this->image->getHeight();
  52. $width = $this->image->getWidth();
  53. $ratio = $width / 64;
  54.  
  55. $validHeight = $height / $ratio === 32;
  56. $validWidth = $width / $ratio === 64;
  57.  
  58. if ($canSetHdSkin) {
  59. if ($validHeight && $validWidth && $width <= 1024 && $height <= 512) {
  60. return true;
  61. } else {
  62. throw new InvalidImageSizeException();
  63. }
  64. } else {
  65. if ($validHeight && $validWidth) {
  66. $uuid = is_auth() ? \Sentinel::getUser()->getUUID() : null;
  67. $parent = "";
  68.  
  69. try {
  70. $db_settings = array(
  71. 'host' => '127.0.0.1',
  72. 'name' => 'architechperms',
  73. 'login' => '',
  74. 'password' => '',
  75. 'charset' => 'utf8'
  76. );
  77.  
  78. $db = new PDO(
  79. "mysql:host={$db_settings['host']};dbname={$db_settings['name']}",
  80. $db_settings['login'],
  81. $db_settings['password'],
  82. array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES {$db_settings['charset']}"));
  83.  
  84. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  85.  
  86. $order = $db->prepare("SELECT * FROM permissions_inheritance WHERE child=:UUID");
  87. $order->execute(array(":UUID"=>$uuid));
  88. $result = $order->fetchAll();
  89.  
  90. foreach ($result as $row) {
  91. if ($row['parent'] === "hdskin512") {
  92. $parent = "hdskin512";
  93. } else if ($row['parent'] === "hdskin256" && $parent !== "hdskin512") {
  94. $parent = "hdskin256";
  95. }
  96. }
  97. } catch(PDOException $e) {
  98. // Отвалилась база данных
  99. }
  100.  
  101. $w = 128;
  102. $h = 64;
  103.  
  104. if ($parent === "hdskin512") {
  105. $w = 512;
  106. $h = 256;
  107. }
  108.  
  109. if ($parent === "hdskin256") {
  110. $w = 256;
  111. $h = 128;
  112. }
  113.  
  114. if ($height <= $h && $width <= $w) {
  115. return true;
  116. } else {
  117. throw new InvalidImageSizeException();
  118. }
  119. } else {
  120. throw new InvalidImageSizeException();
  121. }
  122. }
  123. }
  124.  
  125. /**
  126. * Move image file in skins directory.
  127. *
  128. * @param string $username
  129. */
  130. public function move(string $username)
  131. {
  132. $this->file->move(config('l-shop.profile.skins.path'), $username . '.png');
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment