ServerController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Classes\Pterodactyl;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Server;
  6. use App\Models\User;
  7. use Exception;
  8. use Illuminate\Contracts\Foundation\Application;
  9. use Illuminate\Contracts\View\Factory;
  10. use Illuminate\Contracts\View\View;
  11. use Illuminate\Http\JsonResponse;
  12. use Illuminate\Http\RedirectResponse;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Http\Response;
  15. use Illuminate\Support\Facades\Log;
  16. class ServerController extends Controller
  17. {
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return Application|Factory|View|Response
  22. */
  23. public function index()
  24. {
  25. return view('admin.servers.index');
  26. }
  27. /**
  28. * Show the form for editing the specified resource.
  29. *
  30. * @param Server $server
  31. * @return Response
  32. */
  33. public function edit(Server $server)
  34. {
  35. // get all users from the database
  36. $users = User::all();
  37. return view('admin.servers.edit')->with([
  38. 'server' => $server,
  39. 'users' => $users,
  40. ]);
  41. }
  42. /**
  43. * Update the specified resource in storage.
  44. *
  45. * @param Request $request
  46. * @param Server $server
  47. */
  48. public function update(Request $request, Server $server)
  49. {
  50. $request->validate([
  51. 'identifier' => 'required|string',
  52. 'user_id' => 'required|integer',
  53. ]);
  54. if ($request->get('user_id') != $server->user_id) {
  55. // find the user
  56. $user = User::findOrFail($request->get('user_id'));
  57. // try to update the owner on pterodactyl
  58. try {
  59. $response = Pterodactyl::updateServerOwner($server, $user->pterodactyl_id);
  60. if ($response->getStatusCode() != 200) {
  61. return redirect()->back()->with('error', 'Failed to update server owner on pterodactyl');
  62. }
  63. // update the owner on the database
  64. $server->user_id = $user->id;
  65. } catch (Exception $e) {
  66. return redirect()->back()->with('error', 'Internal Server Error');
  67. }
  68. }
  69. // update the identifier
  70. $server->identifier = $request->get('identifier');
  71. $server->save();
  72. return redirect()->route('admin.servers.index')->with('success', 'Server updated!');
  73. }
  74. /**
  75. * Remove the specified resource from storage.
  76. *
  77. * @param Server $server
  78. * @return RedirectResponse|Response
  79. */
  80. public function destroy(Server $server)
  81. {
  82. try {
  83. $server->delete();
  84. return redirect()->route('admin.servers.index')->with('success', __('Server removed'));
  85. } catch (Exception $e) {
  86. return redirect()->route('admin.servers.index')->with('error', __('An exception has occurred while trying to remove a resource "') . $e->getMessage() . '"');
  87. }
  88. }
  89. /**
  90. * @param Server $server
  91. * @return RedirectResponse
  92. */
  93. public function toggleSuspended(Server $server)
  94. {
  95. try {
  96. $server->isSuspended() ? $server->unSuspend() : $server->suspend();
  97. } catch (Exception $exception) {
  98. return redirect()->back()->with('error', $exception->getMessage());
  99. }
  100. return redirect()->back()->with('success', __('Server has been updated!'));
  101. }
  102. public function syncServers()
  103. {
  104. $pteroServers = Pterodactyl::getServers();
  105. $CPServers = Server::get();
  106. $CPIDArray = [];
  107. $renameCount = 0;
  108. foreach ($CPServers as $CPServer) { //go thru all CP servers and make array with IDs as keys. All values are false.
  109. if ($CPServer->pterodactyl_id) {
  110. $CPIDArray[$CPServer->pterodactyl_id] = false;
  111. }
  112. }
  113. foreach ($pteroServers as $server) { //go thru all ptero servers, if server exists, change value to true in array.
  114. if (isset($CPIDArray[$server['attributes']['id']])) {
  115. $CPIDArray[$server['attributes']['id']] = true;
  116. if (isset($server['attributes']['name'])) { //failsafe
  117. //Check if a server got renamed
  118. $savedServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first();
  119. if ($savedServer->name != $server['attributes']['name']) {
  120. $savedServer->name = $server['attributes']['name'];
  121. $savedServer->save();
  122. $renameCount++;
  123. }
  124. }
  125. }
  126. }
  127. $filteredArray = array_filter($CPIDArray, function ($v, $k) {
  128. return $v == false;
  129. }, ARRAY_FILTER_USE_BOTH); //Array of servers, that dont exist on ptero (value == false)
  130. $deleteCount = 0;
  131. foreach ($filteredArray as $key => $CPID) { //delete servers that dont exist on ptero anymore
  132. if (!Pterodactyl::getServerAttributes($key, true)) {
  133. $deleteCount++;
  134. }
  135. }
  136. return redirect()->back()->with('success', __('Servers synced successfully' . (($renameCount) ? (',\n' . __('renamed') . ' ' . $renameCount . ' ' . __('servers')) : '') . ((count($filteredArray)) ? (',\n' . __('deleted') . ' ' . $deleteCount . '/' . count($filteredArray) . ' ' . __('old servers')) : ''))) . '.';
  137. }
  138. /**
  139. * @return JsonResponse|mixed
  140. *
  141. * @throws Exception
  142. */
  143. public function dataTable(Request $request)
  144. {
  145. $query = Server::with(['user', 'product']);
  146. if ($request->has('product')) {
  147. $query->where('product_id', '=', $request->input('product'));
  148. }
  149. if ($request->has('user')) {
  150. $query->where('user_id', '=', $request->input('user'));
  151. }
  152. $query->select('servers.*');
  153. Log::info($request->input('order'));
  154. return datatables($query)
  155. ->addColumn('user', function (Server $server) {
  156. return '<a href="' . route('admin.users.show', $server->user->id) . '">' . $server->user->name . '</a>';
  157. })
  158. ->addColumn('resources', function (Server $server) {
  159. return $server->product->description;
  160. })
  161. ->addColumn('actions', function (Server $server) {
  162. $suspendColor = $server->isSuspended() ? 'btn-success' : 'btn-warning';
  163. $suspendIcon = $server->isSuspended() ? 'fa-play-circle' : 'fa-pause-circle';
  164. $suspendText = $server->isSuspended() ? __('Unsuspend') : __('Suspend');
  165. return '
  166. <a data-content="' . __('Edit') . '" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.servers.edit', $server->id) . '" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
  167. <form class="d-inline" method="post" action="' . route('admin.servers.togglesuspend', $server->id) . '">
  168. ' . csrf_field() . '
  169. <button data-content="' . $suspendText . '" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm ' . $suspendColor . ' text-white mr-1"><i class="far ' . $suspendIcon . '"></i></button>
  170. </form>
  171. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.servers.destroy', $server->id) . '">
  172. ' . csrf_field() . '
  173. ' . method_field('DELETE') . '
  174. <button data-content="' . __('Delete') . '" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
  175. </form>
  176. ';
  177. })
  178. ->addColumn('status', function (Server $server) {
  179. $labelColor = $server->suspended ? 'text-danger' : 'text-success';
  180. return '<i class="fas ' . $labelColor . ' fa-circle mr-2"></i>';
  181. })
  182. ->editColumn('created_at', function (Server $server) {
  183. return $server->created_at ? $server->created_at->diffForHumans() : '';
  184. })
  185. ->editColumn('suspended', function (Server $server) {
  186. return $server->suspended ? $server->suspended->diffForHumans() : '';
  187. })
  188. ->editColumn('name', function (Server $server) {
  189. return '<a class="text-info" target="_blank" href="' . config('SETTINGS::SYSTEM:PTERODACTYL:URL') . '/admin/servers/view/' . $server->pterodactyl_id . '">' . strip_tags($server->name) . '</a>';
  190. })
  191. ->rawColumns(['user', 'actions', 'status', 'name'])
  192. ->make();
  193. }
  194. }