Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. <?php
  2. namespace App\lib;
  3. /**
  4. * Class MinecraftAuth
  5. * @description Integrate minecraft authentification in php project
  6. */
  7.  
  8. class MinecraftAuth{
  9. const AUTHSERVER = "https://authserver.mojang.com/";
  10.  
  11. /**
  12. * Function for call mojang authenfication and verify credentials
  13. *
  14. * @access public
  15. * @param string $user
  16. * @param string $pass
  17. * @return string
  18. */
  19. public function yggdrasil_authenticate($user, $pass){
  20. if (!(MinecraftAuth::str_contains($user, "@") && MinecraftAuth::str_contains($user, "."))){
  21. return "Veuillez utiliser votre adresse email pour vous connecter. Pour ce faire, vous devez avoir préalablement migré votre compte Minecraft.net auprès de Mojang.com";
  22. }
  23.  
  24. $curl = curl_init(MinecraftAuth::AUTHSERVER."authenticate");
  25. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  26. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  27. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  28.  
  29. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  30. 'Content-Type: application/json'
  31. ));
  32.  
  33. curl_setopt($curl, CURLOPT_POST, 1);
  34. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array(
  35. "agent" =>
  36. array(
  37. "name" => "Minecraft",
  38. "version" => 1),
  39. "username" => $user,
  40. "password" => $pass
  41. )));
  42.  
  43. $response = curl_exec($curl);
  44. curl_close($curl);
  45.  
  46. $responsejson = json_decode($response, true);
  47.  
  48. if (isset($responsejson["errorMessage"])){
  49. return "Vos informations d'authentification sont incorrectes. Veuillez vérifier votre adresse email ainsi que votre mot de passe.";
  50. }
  51. else if (isset($responsejson["selectedProfile"]) === false){
  52. return "Il est impératif que vous ayez une license minecraft associée à votre compte pour pouvoir vous connecter.";
  53. }
  54. else{
  55. return $responsejson["selectedProfile"];
  56. }
  57. }
  58.  
  59. /**
  60. * Function for test whether a string contains a specific character
  61. *
  62. * @access public
  63. * @param string $haystack
  64. * @param string $needle
  65. * @return bool
  66. */
  67. public function str_contains($haystack, $needle){
  68. return strpos($haystack, $needle) !== false;
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement