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.8KB

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