ServerController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. class ServerController extends Controller
  16. {
  17. /**
  18. * Display a listing of the resource.
  19. *
  20. * @return Application|Factory|View|Response
  21. */
  22. public function index()
  23. {
  24. return view('admin.servers.index');
  25. }
  26. /**
  27. * Show the form for creating a new resource.
  28. *
  29. * @return Response
  30. */
  31. public function create()
  32. {
  33. //
  34. }
  35. /**
  36. * Store a newly created resource in storage.
  37. *
  38. * @param Request $request
  39. * @return Response
  40. */
  41. public function store(Request $request)
  42. {
  43. //
  44. }
  45. /**
  46. * Display the specified resource.
  47. *
  48. * @param Server $server
  49. * @return Response
  50. */
  51. public function show(Server $server)
  52. {
  53. //
  54. }
  55. /**
  56. * Show the form for editing the specified resource.
  57. *
  58. * @param Server $server
  59. * @return Response
  60. */
  61. public function edit(Server $server)
  62. {
  63. // get all users from the database
  64. $users = User::all();
  65. return view('admin.servers.edit')->with([
  66. 'server' => $server,
  67. 'users' => $users,
  68. ]);
  69. }
  70. /**
  71. * Update the specified resource in storage.
  72. *
  73. * @param Request $request
  74. * @param Server $server
  75. */
  76. public function update(Request $request, Server $server)
  77. {
  78. $request->validate([
  79. 'identifier' => 'required|string',
  80. 'user_id' => 'required|integer',
  81. ]);
  82. if ($request->get('user_id') != $server->user_id) {
  83. // find the user
  84. $user = User::findOrFail($request->get('user_id'));
  85. // try to update the owner on pterodactyl
  86. try {
  87. $response = Pterodactyl::updateServerOwner($server, $user->pterodactyl_id);
  88. if ($response->getStatusCode() != 200) {
  89. return redirect()->back()->with('error', 'Failed to update server owner on pterodactyl');
  90. }
  91. // update the owner on the database
  92. $server->user_id = $user->id;
  93. } catch (Exception $e) {
  94. return redirect()->back()->with('error', 'Internal Server Error');
  95. }
  96. }
  97. // update the identifier
  98. $server->identifier = $request->get('identifier');
  99. $server->save();
  100. return redirect()->route('admin.servers.index')->with('success', 'Server updated!');
  101. }
  102. /**
  103. * Remove the specified resource from storage.
  104. *
  105. * @param Server $server
  106. * @return RedirectResponse|Response
  107. */
  108. public function destroy(Server $server)
  109. {
  110. try {
  111. $server->delete();
  112. return redirect()->route('admin.servers.index')->with('success', __('Server removed'));
  113. } catch (Exception $e) {
  114. return redirect()->route('admin.servers.index')->with('error', __('An exception has occurred while trying to remove a resource "') . $e->getMessage() . '"');
  115. }
  116. }
  117. /**
  118. * Cancel the Server billing cycle.
  119. *
  120. * @param Server $server
  121. * @return RedirectResponse|Response
  122. */
  123. public function cancel(Server $server)
  124. {
  125. try {
  126. error_log($server->update([
  127. 'cancelled' => now(),
  128. ]));
  129. return redirect()->route('servers.index')->with('success', __('Server cancelled'));
  130. } catch (Exception $e) {
  131. return redirect()->route('servers.index')->with('error', __('An exception has occurred while trying to cancel the server"') . $e->getMessage() . '"');
  132. }
  133. }
  134. /**
  135. * @param Server $server
  136. * @return RedirectResponse
  137. */
  138. public function toggleSuspended(Server $server)
  139. {
  140. try {
  141. $server->isSuspended() ? $server->unSuspend() : $server->suspend();
  142. } catch (Exception $exception) {
  143. return redirect()->back()->with('error', $exception->getMessage());
  144. }
  145. return redirect()->back()->with('success', __('Server has been updated!'));
  146. }
  147. public function syncServers()
  148. {
  149. $pteroServers = Pterodactyl::getServers();
  150. $CPServers = Server::get();
  151. $CPIDArray = [];
  152. $renameCount = 0;
  153. foreach ($CPServers as $CPServer) { //go thru all CP servers and make array with IDs as keys. All values are false.
  154. if ($CPServer->pterodactyl_id) {
  155. $CPIDArray[$CPServer->pterodactyl_id] = false;
  156. }
  157. }
  158. foreach ($pteroServers as $server) { //go thru all ptero servers, if server exists, change value to true in array.
  159. if (isset($CPIDArray[$server['attributes']['id']])) {
  160. $CPIDArray[$server['attributes']['id']] = true;
  161. if (isset($server['attributes']['name'])) { //failsafe
  162. //Check if a server got renamed
  163. $savedServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first();
  164. if ($savedServer->name != $server['attributes']['name']) {
  165. $savedServer->name = $server['attributes']['name'];
  166. $savedServer->save();
  167. $renameCount++;
  168. }
  169. }
  170. }
  171. }
  172. $filteredArray = array_filter($CPIDArray, function ($v, $k) {
  173. return $v == false;
  174. }, ARRAY_FILTER_USE_BOTH); //Array of servers, that dont exist on ptero (value == false)
  175. $deleteCount = 0;
  176. foreach ($filteredArray as $key => $CPID) { //delete servers that dont exist on ptero anymore
  177. if (!Pterodactyl::getServerAttributes($key, true)) {
  178. $deleteCount++;
  179. }
  180. }
  181. return redirect()->back()->with('success', __('Servers synced successfully' . (($renameCount) ? (',\n' . __('renamed') . ' ' . $renameCount . ' ' . __('servers')) : '') . ((count($filteredArray)) ? (',\n' . __('deleted') . ' ' . $deleteCount . '/' . count($filteredArray) . ' ' . __('old servers')) : ''))) . '.';
  182. }
  183. /**
  184. * @return JsonResponse|mixed
  185. *
  186. * @throws Exception
  187. */
  188. public function dataTable(Request $request)
  189. {
  190. $query = Server::with(['user', 'product']);
  191. if ($request->has('product')) {
  192. $query->where('product_id', '=', $request->input('product'));
  193. }
  194. if ($request->has('user')) {
  195. $query->where('user_id', '=', $request->input('user'));
  196. }
  197. $query->select('servers.*');
  198. return datatables($query)
  199. ->addColumn('user', function (Server $server) {
  200. return '<a href="' . route('admin.users.show', $server->user->id) . '">' . $server->user->name . '</a>';
  201. })
  202. ->addColumn('resources', function (Server $server) {
  203. return $server->product->description;
  204. })
  205. ->addColumn('actions', function (Server $server) {
  206. $suspendColor = $server->isSuspended() ? 'btn-success' : 'btn-warning';
  207. $suspendIcon = $server->isSuspended() ? 'fa-play-circle' : 'fa-pause-circle';
  208. $suspendText = $server->isSuspended() ? __('Unsuspend') : __('Suspend');
  209. return '
  210. <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>
  211. <form class="d-inline" method="post" action="' . route('admin.servers.togglesuspend', $server->id) . '">
  212. ' . csrf_field() . '
  213. <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>
  214. </form>
  215. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.servers.destroy', $server->id) . '">
  216. ' . csrf_field() . '
  217. ' . method_field('DELETE') . '
  218. <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>
  219. </form>
  220. ';
  221. })
  222. ->addColumn('status', function (Server $server) {
  223. $labelColor = $server->isSuspended() ? 'text-danger' : 'text-success';
  224. return '<i class="fas ' . $labelColor . ' fa-circle mr-2"></i>';
  225. })
  226. ->editColumn('created_at', function (Server $server) {
  227. return $server->created_at ? $server->created_at->diffForHumans() : '';
  228. })
  229. ->editColumn('suspended', function (Server $server) {
  230. return $server->suspended ? $server->suspended->diffForHumans() : '';
  231. })
  232. ->editColumn('name', function (Server $server) {
  233. return '<a class="text-info" target="_blank" href="' . config('SETTINGS::SYSTEM:PTERODACTYL:URL') . '/admin/servers/view/' . $server->pterodactyl_id . '">' . strip_tags($server->name) . '</a>';
  234. })
  235. ->rawColumns(['user', 'actions', 'status', 'name'])
  236. ->make();
  237. }
  238. }