public function save_newfile($srvid, $file, $content) { if (empty($file)) { return 'No filename given!'; } // Get network ID $result_nid = @mysql_query("SELECT netid FROM servers WHERE id = '{$srvid}'"); $row_nid = mysql_fetch_row($result_nid); $this_netid = $row_nid[0]; if (empty($this_netid)) { return 'Failed to get network ID!'; } require DOCROOT . '/includes/classes/network.php'; $Network = new Network(); $netinfo = $Network->netinfo($this_netid); #$net_game_ip = $netinfo['game_ip']; #$net_game_port = $netinfo['game_port']; #$net_gameuser = $netinfo['username']; $net_local = $netinfo['is_local']; // Get real server info require DOCROOT . '/includes/classes/servers.php'; $Servers = new Servers(); $srvinfo = $Servers->getinfo($srvid); $net_game_ip = $srvinfo[0]['ip']; $net_gameuser = $srvinfo[0]['username']; $net_game_port = $srvinfo[0]['port']; // Add full path to file if (isset($_SESSION['curdir'])) { $file = $_SESSION['curdir'] . '/' . $file; } // Get userdir if ($net_local) { $localdir = DOCROOT . '/_SERVERS/'; $game_dir = $localdir . '/accounts/' . $net_gameuser . '/' . $net_game_ip . '.' . $net_game_port . '/' . $file; // Stupid newlines, this took forever to figure out '\\\n/' - either jquery caused this or the textarea did, no idea $content = preg_replace('/\\\\n/', "\n", $content); $content = stripslashes($content); // Write to file $fh = fopen($game_dir, "w") or die('Failed to open file for writing!'); fwrite($fh, $content); fclose($fh); return 'success'; } else { #$file_path = $netinfo['ssh_homedir'] . "/accounts/$net_gameuser/$net_game_ip\.$net_game_port/$file"; // Get SSO info $sso_info = $Network->sso_info($srvid); $file_path = $sso_info['game_path'] . '/' . $file; // Save File $run_cmd = 'FileSave -f ' . $file_path . ' -c "' . $content . '"'; // Run the command, return output return $Network->runcmd($this_netid, $netinfo, $run_cmd, true, $srvid); } }
public function create($netid, $gameid, $ownerid, $tplid, $port, $description, $total_slots, $rcon_password, $is_private, $private_password) { #if(empty($netid) || empty($gameid) || empty($ownerid)) return 'Servers: Insufficient info provided'; if (empty($netid)) { return 'Servers: No Network Server provided!'; } elseif (empty($ownerid)) { return 'Servers: No username provided!'; } # elseif(empty($gameid)) return 'Servers: No game/voice server name provided!'; // Generate random token for remote server callback $Core = new Core(); $remote_token = $Core->genstring('16'); // Check for uses IP/Port combo (false if used) if (!$this->checkcombo($netid, $port)) { return 'Servers: That IP/Port combination is already in use! Please choose a different IP or Port and try again.'; } // Grab Game ID if not given (get from template) if (empty($gameid)) { if (!empty($tplid) && is_numeric($tplid)) { // Query for gameid $result_gmid = @mysql_query("SELECT cfgid FROM templates WHERE id = '{$tplid}'") or die('Failed to query for game ID'); $row_gmid = mysql_fetch_row($result_gmid); $gameid = $row_gmid[0]; if (empty($gameid)) { return 'Template ID specified, but no Game ID found from it!'; } } else { return 'No Game ID or empty/invalid Template ID specified!'; } } // Get owner username $result_name = @mysql_query("SELECT username FROM users WHERE id = '{$ownerid}' LIMIT 1") or die('Failed to query for username'); $row_name = mysql_fetch_row($result_name); $this_usrname = $row_name[0]; // Get default template if (empty($tplid)) { $result_tpl = @mysql_query("SELECT id FROM templates WHERE cfgid = '{$gameid}' AND status = 'complete' AND is_default = '1' ORDER BY id LIMIT 1") or die('Failed to get the default template'); $row_tpl = mysql_fetch_row($result_tpl); $this_tplid = $row_tpl[0]; } else { $this_tplid = $tplid; } // Setup to create on remote server require_once DOCROOT . '/includes/classes/network.php'; $Network = new Network(); $net_arr = $Network->netinfo($netid); if (!empty($net_arr['real_ip'])) { $this_ip = $net_arr['real_ip']; } else { $this_ip = $net_arr['game_ip']; } # if(empty($net_arr['game_ip'])) $this_ip = $net_arr['ssh_ip']; # else $this_ip = $net_arr['game_ip']; // Double check everything if (empty($this_usrname)) { return 'Servers: No username specified!'; } elseif (empty($this_ip)) { return 'Servers: No IP Address specified!'; } elseif (empty($port)) { return 'Servers: No port specified!'; } elseif (empty($this_tplid)) { return 'Servers: No template found for this game!'; } ############################################################################################ // Get some defaults $result_dfts = @mysql_query("SELECT maxplayers,working_dir,pid_file,update_cmd,simplecmd,map,hostname FROM default_games WHERE id = '{$gameid}' LIMIT 1") or die('Failed to query for defaults'); $row_dfts = mysql_fetch_row($result_dfts); $def_working_dir = mysql_real_escape_string($row_dfts[1]); $def_pid_file = mysql_real_escape_string($row_dfts[2]); $def_update_cmd = mysql_real_escape_string($row_dfts[3]); $def_simple_cmd = mysql_real_escape_string($row_dfts[4]); $def_map = mysql_real_escape_string($row_dfts[5]); $def_hostname = mysql_real_escape_string($row_dfts[6]); // Max player slots - use what was given, otherwise use the default if (!empty($total_slots) && is_numeric($total_slots)) { $def_maxplayers = mysql_real_escape_string($total_slots); } else { $def_maxplayers = mysql_real_escape_string($row_dfts[0]); } // Generate random rcon password if not specified if (empty($rcon_password)) { $rcon_password = $Core->genstring('8'); } ######################################################################################### // If local, ensure we can write to the _SERVERS/accounts directory $result_loc = @mysql_query("SELECT is_local FROM network WHERE id = '{$netid}' LIMIT 1"); $row_loc = mysql_fetch_row($result_loc); $net_local = $row_loc[0]; if ($net_local && !is_writable(DOCROOT . '/_SERVERS/accounts')) { die('Error: Unable to write to the "' . DOCROOT . '/_SERVERS/accounts" directory. Check that this directory is recursively owned by your webserver user, and try again.'); } ######################################################################################### // Insert into db @mysql_query("INSERT INTO servers (userid,netid,defid,port,maxplayers,status,date_created,token,working_dir,pid_file,update_cmd,description,map,rcon,hostname,sv_password) VALUES('{$ownerid}','{$netid}','{$gameid}','{$port}','{$def_maxplayers}','installing',NOW(),'{$remote_token}','{$def_working_dir}','{$def_pid_file}','{$def_update_cmd}','{$description}','{$def_map}','{$rcon_password}','{$def_hostname}','{$private_password}')") or die('Failed to insert server: ' . mysql_error()); $srv_id = mysql_insert_id(); // Insert default srv settings $result_smp = @mysql_query("SELECT * FROM default_startup WHERE defid = '{$gameid}' ORDER BY sort_order ASC"); $total_strt = mysql_num_rows($result_smp); $insert_new = 'INSERT INTO servers_startup (srvid,sort_order,single,usr_edit,cmd_item,cmd_value) VALUES '; $simplecmd = ''; while ($row_smp = mysql_fetch_array($result_smp)) { $cmd_sort = $row_smp['sort_order']; $cmd_single = $row_smp['single']; $cmd_usred = $row_smp['usr_edit']; $cmd_item = $row_smp['cmd_item']; $cmd_val = $row_smp['cmd_value']; $insert_new .= "('{$srv_id}','{$cmd_sort}','{$cmd_single}','{$cmd_usred}','{$cmd_item}','{$cmd_val}'),"; // Replace %vars% for simplecmd $cmd_val = str_replace('%IP%', $this_ip, $cmd_val); $cmd_val = str_replace('%PORT%', $port, $cmd_val); $cmd_val = str_replace('%MAP%', $def_map, $cmd_val); $cmd_val = str_replace('%MAXPLAYERS%', $def_maxplayers, $cmd_val); $cmd_val = str_replace('%HOSTNAME%', $def_hostname, $cmd_val); // Update simplecmd $simplecmd .= $cmd_item . ' '; if ($cmd_val || $cmd_val == '0') { $simplecmd .= $cmd_val . ' '; } } // Run multi-insert (only if there were default startup items) if ($total_strt) { // Remove last comma $insert_new = substr($insert_new, 0, -1); @mysql_query($insert_new) or die('Failed to insert startup items: ' . mysql_error()); } // Add simplecmd if (empty($simplecmd)) { $simplecmd = $def_simple_cmd; } @mysql_query("UPDATE servers SET simplecmd = '{$simplecmd}' WHERE id = '{$srv_id}'"); ############################################################################################ // Get callback page $this_url = $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']; $this_page = str_replace('ajax/ajax.php', '', $this_url); $this_page = str_replace('api/api.php', '', $this_page); $this_page .= '/includes/callback.php?token=' . $remote_token . '&id=' . $srv_id; $this_page = preg_replace('/\\/+/', '/', $this_page); // Remove extra slashes $this_page = 'http://' . $this_page; ############################################################################################ // // Create on Remote server // // Check via 'gpx' user if account exists, create if needed, THEN run CreateServer. Cannot add this functionality to CreateServer as CreateServer is run by the gpxblah accounts. if (!$net_local) { $sso_info = $Network->sso_info($srv_id); $sso_user = substr($sso_info['sso_user'], 3); // Lose the 'gpx' prefix $sso_pass = $sso_info['sso_pass']; // Remote: Create the system user account if needed. Okay if it already exists. $crypt_pass = crypt($sso_pass); $net_cmd = "CreateUser -u '{$sso_user}' -p '{$crypt_pass}'"; $create_result = $Network->runcmd($netid, $net_arr, $net_cmd, true); // Proceed if the user exists, or it was successfully created if ($create_result == 'success') { // Allow GPXManager to create the account sleep(4); } elseif (!preg_match('/That user already exists/', $create_result)) { // Failed, delete this server $this->delete_soft($srv_id); die('Failed to create the user account for this server (' . $create_result . ') exiting.'); } } $net_cmd = "CreateServer -u {$this_usrname} -i {$this_ip} -p {$port} -x {$this_tplid} -c \"{$this_page}\""; $result_net_create = $Network->runcmd($netid, $net_arr, $net_cmd, true, $srv_id); ################# if ($result_net_create != 'success') { // Failed on Remote Creation; delete this server #@mysql_query("DELETE FROM servers WHERE id = '$srv_id'") or die('Failed to delete the server from the database'); #@mysql_query("DELETE FROM servers_startup WHERE srvid = '$srv_id'") or die('Failed to delete the server startups from the database'); $this->delete_soft($srv_id); return 'Remote Failed: ' . $result_net_create; } else { return 'success'; } }