33namespace Pterodactyl \Models ;
44
55use Auth ;
6- use DB ;
7- use Debugbar ;
8- use Validator ;
9-
10- use Pterodactyl \Exceptions \DisplayException ;
11- use Pterodactyl \Exceptions \AccountNotFoundException ;
12- use Pterodactyl \Exceptions \DisplayValidationException ;
13-
14- use Pterodactyl \Models ;
156
7+ use Pterodactyl \Models \Subuser ;
168use Illuminate \Database \Eloquent \Model ;
179
1810class Server extends Model
@@ -50,18 +42,6 @@ public function __construct()
5042 self ::$ user = Auth::user ();
5143 }
5244
53- protected static function generateSFTPUsername ($ name )
54- {
55-
56- $ name = preg_replace ('/\s+/ ' , '' , $ name );
57- if (strlen ($ name ) > 6 ) {
58- return strtolower ('ptdl- ' . substr ($ name , 0 , 6 ) . '_ ' . str_random (5 ));
59- }
60-
61- return strtolower ('ptdl- ' . $ name . '_ ' . str_random ((11 - strlen ($ name ))));
62-
63- }
64-
6545 /**
6646 * Determine if we need to change the server's daemonSecret value to
6747 * match that of the user if they are a subuser.
@@ -76,7 +56,7 @@ protected static function getUserDaemonSecret(Server $server)
7656 return $ server ->daemonSecret ;
7757 }
7858
79- $ subuser = Models \ Subuser::where ('server_id ' , $ server ->id )->where ('user_id ' , self ::$ user ->id )->first ();
59+ $ subuser = Subuser::where ('server_id ' , $ server ->id )->where ('user_id ' , self ::$ user ->id )->first ();
8060
8161 if (is_null ($ subuser )) {
8262 return null ;
@@ -101,7 +81,7 @@ public static function getUserServers()
10181 ->where ('active ' , 1 );
10282
10383 if (self ::$ user ->root_admin !== 1 ) {
104- $ query ->whereIn ('servers.id ' , Models \ Subuser::accessServers ());
84+ $ query ->whereIn ('servers.id ' , Subuser::accessServers ());
10585 }
10686
10787 return $ query ->get ();
@@ -124,7 +104,7 @@ public static function getByUUID($uuid)
124104 $ query = self ::where ('uuidShort ' , $ uuid )->where ('active ' , 1 );
125105
126106 if (self ::$ user ->root_admin !== 1 ) {
127- $ query ->whereIn ('servers.id ' , Models \ Subuser::accessServers ());
107+ $ query ->whereIn ('servers.id ' , Subuser::accessServers ());
128108 }
129109
130110 $ result = $ query ->first ();
@@ -158,84 +138,4 @@ public static function getGuzzleHeaders($uuid)
158138
159139 }
160140
161- /**
162- * Adds a new server to the system.
163- * @param array $data An array of data descriptors for creating the server. These should align to the columns in the database.
164- */
165- public static function addServer (array $ data )
166- {
167-
168- // Validate Fields
169- $ validator = Validator::make ($ data , [
170- 'owner ' => 'required|email|exists:users,email ' ,
171- 'node ' => 'required|numeric|min:1|exists:nodes,id ' ,
172- 'name ' => 'required|regex:([\w -]{4,35}) ' ,
173- 'memory ' => 'required|numeric|min:1 ' ,
174- 'disk ' => 'required|numeric|min:1 ' ,
175- 'cpu ' => 'required|numeric|min:0 ' ,
176- 'io ' => 'required|numeric|min:10|max:1000 ' ,
177- 'ip ' => 'required|ip ' ,
178- 'port ' => 'required|numeric|min:1|max:65535 ' ,
179- 'service ' => 'required|numeric|min:1|exists:services,id ' ,
180- 'option ' => 'required|numeric|min:1|exists:service_options,id ' ,
181- 'custom_image_name ' => 'required_if:use_custom_image,on ' ,
182- ]);
183-
184- // Run validator, throw catchable and displayable exception if it fails.
185- // Exception includes a JSON result of failed validation rules.
186- if ($ validator ->fails ()) {
187- throw new DisplayValidationException (json_encode ($ validator ->errors ()->all ()));
188- }
189-
190- // Get the User ID; user exists since we passed the 'exists:users,email' part of the validation
191- $ user = Models \User::select ('id ' )->where ('email ' , $ data ['owner ' ])->first ();
192-
193- // Verify IP & Port are a.) free and b.) assigned to the node.
194- // We know the node exists because of 'exists:nodes,id' in the validation
195- $ node = Models \Node::find ($ data ['node ' ]);
196- $ allocation = Models \Allocation::where ('ip ' , $ data ['ip ' ])->where ('port ' , $ data ['port ' ])->where ('node ' , $ data ['node ' ])->whereNull ('assigned_to ' )->first ();
197-
198- // Something failed in the query, either that combo doesn't exist, or it is in use.
199- if (!$ allocation ) {
200- throw new DisplayException ('The selected IP/Port combination ( ' . $ data ['ip ' ] . ': ' . $ data ['port ' ] . ') is either already in use, or unavaliable for this node. ' );
201- }
202-
203- // Validate those Service Option Variables
204- // We know the service and option exists because of the validation.
205- // We need to verify that the option exists for the service, and then check for
206- // any required variable fields. (fields are labeled env_<env_variable>)
207- $ option = Models \ServiceOptions::where ('id ' , $ data ['option ' ])->where ('parent_service ' , $ data ['service ' ])->first ();
208- if (!$ option ) {
209- throw new DisplayException ('The requested service option does not exist for the specified service. ' );
210- }
211-
212- // Check those Variables
213- $ variables = Models \ServiceVariables::where ('option_id ' , $ data ['option ' ])->get ();
214- if ($ variables ) {
215- foreach ($ variables as $ variable ) {
216-
217- // Is the variable required?
218- if (!$ data ['env_ ' . $ variable ->env_variable ]) {
219- if ($ variable ->required === 1 ) {
220- throw new DisplayException ('A required service option variable field (env_ ' . $ variable ->env_variable . ') was missing from the request. ' );
221- }
222-
223- $ data ['env_ ' . $ variable ->env_variable ] = $ variable ->default_value ;
224- continue ;
225- }
226-
227- // Check aganist Regex Pattern
228- if (!is_null ($ variable ->regex ) && !preg_match ($ variable ->regex , $ data ['env_ ' . $ variable ->env_variable ])) {
229- throw new DisplayException ('Failed to validate service option variable field (env_ ' . $ variable ->env_variable . ') aganist regex ( ' . $ variable ->regex . '). ' );
230- }
231-
232- continue ;
233-
234- }
235- }
236-
237- return self ::generateSFTPUsername ($ data ['name ' ]);
238-
239- }
240-
241141}
0 commit comments