Serving a payment option for my minecraft server so I don't have to pay for it all alone
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MinecraftServer.class.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @Name minecraft_server
  4. * @Unique Name
  5. * @Unique Domain, Port
  6. */
  7. class MinecraftServer extends Entity {
  8. /**
  9. * @DB-Column
  10. * @Name ID
  11. * @Type INT
  12. * @Primary
  13. * @AutoIncrement
  14. * @NotNull
  15. */
  16. public $ID;
  17. /**
  18. * @DB-Column
  19. * @Name Name
  20. * @Type VARCHAR(256)
  21. * @NotNull
  22. */
  23. public $Name;
  24. /**
  25. * @DB-Column
  26. * @Name Domain
  27. * @Type VARCHAR(256)
  28. */
  29. public $Domain = null;
  30. /**
  31. * @DB-Column
  32. * @Name Port
  33. * @Type INT(4)
  34. */
  35. public $Port = null;
  36. /**
  37. * @DB-Column
  38. * @Name Password
  39. * @Type VARCHAR(256)
  40. */
  41. public $Password = null;
  42. /**
  43. * @DB-Column
  44. * @Name ServerPath
  45. * @Type VARCHAR(256)
  46. */
  47. public $ServerPath = null;
  48. /**
  49. * @DB-Column
  50. * @Name PerMonthPrice
  51. * @Type INT(4)
  52. */
  53. public $PerMonthPrice = null;
  54. /**
  55. * @DB-Column
  56. * @Name MaxPerMonthPrice
  57. * @Type INT(4)
  58. */
  59. public $MaxPerMonthPrice = null;
  60. /**
  61. * @DB-Relation
  62. * @Field ID
  63. * @FieldName SubscriptionID
  64. * @OneToMany
  65. * @OnUpdate CASCADE
  66. * @OnDelete CASCADE
  67. * @var MinecraftServer[]
  68. */
  69. public $Subscriptions;
  70. public function IsUnlocked( MinecraftUser $user ): bool {
  71. return in_array( strtolower( $user->GetUsername() ), $this->GetWhitelistedNames() );
  72. }
  73. private $RCon = null;
  74. private function GetRCon() {
  75. if ( $this->RCon == null ) $this->RCon = new Rcon( $this->GetDomain(), $this->GetPort(), $this->GetPassword(), 1500 );
  76. return $this->RCon;
  77. }
  78. public function Unlock( MinecraftUser $user, bool $keepOpen = false ): bool {
  79. $rcon = $this->GetRCon();
  80. if ( !$rcon->isConnected() ) $rcon->connect();
  81. $rcon->sendCommand( 'whitelist add ' . $user->GetUsername() );
  82. $res = $rcon->getResponse();
  83. if ( !$keepOpen ) $rcon->disconnect();
  84. return $this->IsUnlocked( $user );
  85. }
  86. public function Lock( MinecraftUser $user, bool $keepOpen = false ): bool {
  87. $rcon = $this->GetRCon();
  88. if ( !$rcon->isConnected() ) $rcon->connect();
  89. $rcon->sendCommand( 'whitelist remove ' . $user->GetUsername() );
  90. $res = $rcon->getResponse();
  91. if ( !$keepOpen ) $rcon->disconnect();
  92. return !$this->IsUnlocked( $user );
  93. }
  94. public function GetWhitelistedNames( bool $keepOpen = false ) {
  95. // There are 1 whitelisted players: DragonSkills99
  96. $rcon = $this->GetRCon();
  97. if ( !$rcon->isConnected() ) $rcon->connect();
  98. $rcon->sendCommand( 'whitelist list' );
  99. $res = $rcon->getResponse();
  100. if ( !$keepOpen ) $rcon->disconnect();
  101. if ( nocase_compare( 'There are no whitelisted players', trim( $res ) ) ) return [];
  102. preg_match( '/^There are [0-9]+ whitelisted players: (.*)/', trim( $res ), $m );
  103. if ( !isset( $m[ 1 ] ) ) return [];
  104. $players = explode( ', ', strtolower( $m[ 1 ] ) );
  105. if ( trim( $players[ 0 ] ) == '' ) return [];
  106. else return $players;
  107. }
  108. public function ClearWhitelist( int $tries = 3 ) {
  109. $rcon = $this->GetRCon();
  110. $players = $this->GetWhitelistedNames( true );
  111. if ( !count( $players ) ) { $rcon->disconnect(); return true; }
  112. foreach( $players as $player ) {
  113. $rcon->sendCommand( "whitelist remove $player" );
  114. }
  115. $nplayers = $this->GetWhitelistedNames( true );
  116. if ( count( $nplayers ) ) {
  117. if ( $tries > 0 ) $this->ClearWhitelist( $tries - 1 );
  118. else { $rcon->disconnect(); return false; }
  119. }
  120. $rcon->disconnect();
  121. return true;
  122. }
  123. }