ServerController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Classes\Pterodactyl;
  4. use App\Models\Egg;
  5. use App\Models\Location;
  6. use App\Models\Nest;
  7. use App\Models\Node;
  8. use App\Models\Product;
  9. use App\Models\Server;
  10. use App\Models\Settings;
  11. use App\Notifications\ServerCreationError;
  12. use Exception;
  13. use Illuminate\Database\Eloquent\Builder;
  14. use Illuminate\Http\Client\Response;
  15. use Illuminate\Http\RedirectResponse;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\Request as FacadesRequest;
  19. class ServerController extends Controller
  20. {
  21. /** Display a listing of the resource. */
  22. public function index()
  23. {
  24. $servers = Auth::user()->servers;
  25. //Get and set server infos each server
  26. foreach ($servers as $server) {
  27. //Get server infos from ptero
  28. $serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id);
  29. $serverRelationships = $serverAttributes['relationships'];
  30. $serverLocationAttributes = $serverRelationships['location']['attributes'];
  31. //Set server infos
  32. $server->location = $serverLocationAttributes['long'] ?
  33. $serverLocationAttributes['long'] :
  34. $serverLocationAttributes['short'];
  35. $server->egg = $serverRelationships['egg']['attributes']['name'];
  36. $server->nest = $serverRelationships['nest']['attributes']['name'];
  37. $server->node = $serverRelationships['node']['attributes']['name'];
  38. //get productname by product_id for server
  39. $product = Product::find($server->product_id);
  40. $server->product = $product;
  41. }
  42. return view('servers.index')->with([
  43. 'servers' => $servers
  44. ]);
  45. }
  46. /** Show the form for creating a new resource. */
  47. public function create()
  48. {
  49. if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
  50. $productCount = Product::query()->where('disabled', '=', false)->count();
  51. $locations = Location::all();
  52. $nodeCount = Node::query()
  53. ->whereHas('products', function (Builder $builder) {
  54. $builder->where('disabled', '=', false);
  55. })->count();
  56. $eggs = Egg::query()
  57. ->whereHas('products', function (Builder $builder) {
  58. $builder->where('disabled', '=', false);
  59. })->get();
  60. $nests = Nest::query()
  61. ->whereHas('eggs', function (Builder $builder) {
  62. $builder->whereHas('products', function (Builder $builder) {
  63. $builder->where('disabled', '=', false);
  64. });
  65. })->get();
  66. return view('servers.create')->with([
  67. 'productCount' => $productCount,
  68. 'nodeCount' => $nodeCount,
  69. 'nests' => $nests,
  70. 'locations' => $locations,
  71. 'eggs' => $eggs,
  72. 'user' => Auth::user(),
  73. ]);
  74. }
  75. /**
  76. * @return null|RedirectResponse
  77. */
  78. private function validateConfigurationRules()
  79. {
  80. //limit validation
  81. if (Auth::user()->servers()->count() >= Auth::user()->server_limit) {
  82. return redirect()->route('servers.index')->with('error', __('Server limit reached!'));
  83. }
  84. // minimum credits
  85. if (FacadesRequest::has("product")) {
  86. $product = Product::findOrFail(FacadesRequest::input("product"));
  87. if (
  88. Auth::user()->credits <
  89. ($product->minimum_credits == -1
  90. ? config('SETTINGS::USER:MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER', 50)
  91. : $product->minimum_credits)
  92. ) {
  93. return redirect()->route('servers.index')->with('error', "You do not have the required amount of " . CREDITS_DISPLAY_NAME . " to use this product!");
  94. }
  95. }
  96. //Required Verification for creating an server
  97. if (config('SETTINGS::USER:FORCE_EMAIL_VERIFICATION', 'false') === 'true' && !Auth::user()->hasVerifiedEmail()) {
  98. return redirect()->route('profile.index')->with('error', __("You are required to verify your email address before you can create a server."));
  99. }
  100. //Required Verification for creating an server
  101. if (config('SETTINGS::USER:FORCE_DISCORD_VERIFICATION', 'false') === 'true' && !Auth::user()->discordUser) {
  102. return redirect()->route('profile.index')->with('error', __("You are required to link your discord account before you can create a server."));
  103. }
  104. return null;
  105. }
  106. /** Store a newly created resource in storage. */
  107. public function store(Request $request)
  108. {
  109. /** @var Node $node */
  110. /** @var Egg $egg */
  111. /** @var Product $product */
  112. if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
  113. $request->validate([
  114. "name" => "required|max:191",
  115. "node" => "required|exists:nodes,id",
  116. "egg" => "required|exists:eggs,id",
  117. "product" => "required|exists:products,id"
  118. ]);
  119. //get required resources
  120. $product = Product::query()->findOrFail($request->input('product'));
  121. $egg = $product->eggs()->findOrFail($request->input('egg'));
  122. $node = $product->nodes()->findOrFail($request->input('node'));
  123. $server = $request->user()->servers()->create([
  124. 'name' => $request->input('name'),
  125. 'product_id' => $request->input('product'),
  126. ]);
  127. //get free allocation ID
  128. $allocationId = Pterodactyl::getFreeAllocationId($node);
  129. if (!$allocationId) return $this->noAllocationsError($server);
  130. //create server on pterodactyl
  131. $response = Pterodactyl::createServer($server, $egg, $allocationId);
  132. if ($response->failed()) return $this->serverCreationFailed($response, $server);
  133. $serverAttributes = $response->json()['attributes'];
  134. //update server with pterodactyl_id
  135. $server->update([
  136. 'pterodactyl_id' => $serverAttributes['id'],
  137. 'identifier' => $serverAttributes['identifier']
  138. ]);
  139. if (config('SETTINGS::SYSTEM:SERVER_CREATE_CHARGE_FIRST_HOUR', 'true') == 'true') {
  140. if ($request->user()->credits >= $server->product->getHourlyPrice()) {
  141. $request->user()->decrement('credits', $server->product->getHourlyPrice());
  142. }
  143. }
  144. return redirect()->route('servers.index')->with('success', __('Server created'));
  145. }
  146. /**
  147. * return redirect with error
  148. * @param Server $server
  149. * @return RedirectResponse
  150. */
  151. private function noAllocationsError(Server $server)
  152. {
  153. $server->delete();
  154. Auth::user()->notify(new ServerCreationError($server));
  155. return redirect()->route('servers.index')->with('error', __('No allocations satisfying the requirements for automatic deployment on this node were found.'));
  156. }
  157. /**
  158. * return redirect with error
  159. * @param Response $response
  160. * @param Server $server
  161. * @return RedirectResponse
  162. */
  163. private function serverCreationFailed(Response $response, Server $server)
  164. {
  165. $server->delete();
  166. return redirect()->route('servers.index')->with('error', json_encode($response->json()));
  167. }
  168. /** Remove the specified resource from storage. */
  169. public function delete(Server $server)
  170. {
  171. try {
  172. $server->delete();
  173. return redirect()->route('servers.index')->with('success', __('Server removed'));
  174. } catch (Exception $e) {
  175. return redirect()->route('servers.index')->with('error', __('An exception has occurred while trying to remove a resource "') . $e->getMessage() . '"');
  176. }
  177. }
  178. /** Show Server Settings */
  179. public function show(Server $server)
  180. {
  181. $serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id);
  182. $serverRelationships = $serverAttributes['relationships'];
  183. $serverLocationAttributes = $serverRelationships['location']['attributes'];
  184. //Set server infos
  185. $server->location = $serverLocationAttributes['long'] ?
  186. $serverLocationAttributes['long'] :
  187. $serverLocationAttributes['short'];
  188. $server->node = $serverRelationships['node']['attributes']['name'];
  189. $server->name = $serverAttributes['name'];
  190. $server->egg = $serverRelationships['egg']['attributes']['name'];
  191. $products = Product::orderBy("created_at")->get();
  192. // Set the each product eggs array to just contain the eggs name
  193. foreach ($products as $product) {
  194. $product->eggs = $product->eggs->pluck('name')->toArray();
  195. }
  196. return view('servers.settings')->with([
  197. 'server' => $server,
  198. 'products' => $products
  199. ]);
  200. }
  201. public function upgrade(Server $server, Request $request)
  202. {
  203. if(!isset($request->product_upgrade))
  204. {
  205. return redirect()->route('servers.show', ['server' => $server->id])->with('error', __('this product is the only one'));
  206. }
  207. $user = Auth::user();
  208. $oldProduct = Product::where('id', $server->product->id)->first();
  209. $newProduct = Product::where('id', $request->product_upgrade)->first();
  210. $serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id);
  211. $priceupgrade = $newProduct->getHourlyPrice();
  212. if ($priceupgrade < $oldProduct->getHourlyPrice()) {
  213. $priceupgrade = 0;
  214. }
  215. if ($user->credits >= $priceupgrade)
  216. {
  217. $server->product_id = $request->product_upgrade;
  218. $server->update();
  219. $server->allocation = $serverAttributes['allocation'];
  220. $response = Pterodactyl::updateServer($server, $newProduct);
  221. if ($response->failed()) return $this->serverCreationFailed($response, $server);
  222. //update user balance
  223. $user->decrement('credits', $priceupgrade);
  224. return redirect()->route('servers.show', ['server' => $server->id])->with('success', __('Server Successfully Upgraded'));
  225. }
  226. else
  227. {
  228. return redirect()->route('servers.show', ['server' => $server->id])->with('error', __('Not Enough Balance for Upgrade'));
  229. }
  230. }
  231. }