Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- ______________ Token Management
- api/token/create/{panel_user}/{panel_password}
- api/token/test/{token}
- ______________ Game Server Management
- api/start (POST {token}{ip}{port}{mod_id})
- api/stop (POST {token}{ip}{port}{mod_id})
- api/restart (POST {token}{ip}{port}{mod_id})
- api/rcon (POST {token}{ip}{port}{mod_id}{command})
- ______________ Game Server Update
- api/update/steam (POST {token}{ip}{port}{mod_id})
- api/update/rsync (POST {token}{ip}{port}{mod_id})
- api/update/manual (POST {token}{ip}{port}{mod_id}{download_url})
- api/update/master (POST {token}{ip}{port}{mod_id})
- ______________ Addons Installer
- api/addon (POST {token}{ip}{port}{mod_id}{addon_id})
- ______________ Steam Workshop Mod Installer
- api/steam_workshop (POST {token}{ip}{port}{mod_id}{workshop_id}{workshop_mod_id})
- ______________ Files Management
- api/file_mgr/get (POST {token}{ip}{port}{relative_path})
- api/file_mgr/save (POST {token}{ip}{port}{relative_path}{contents})
- api/file_mgr/remove (POST {token}{ip}{port}{relative_path})
- ______________ Game servers administration
- api/servers/create (POST {token}{home_cfg_id}{mod_cfg_id}{ip}{port}{control_password}{ftp_password}{slots}{affinity}{nice})
- api/servers/clone (POST {token}{ip}{port}{mod_id}{clone_home_id})
- api/servers/assign (POST {token}{home_id}{user_id})
- api/servers/remove_assign (POST {token}{home_id}{user_id})
- api/servers/assign_expiration (POST {token}{home_id}{user_id}{timestamp})
- api/servers/home_expiration (POST {token}{home_id}{timestamp})
- api/servers/get_assigned (POST {token}{user_id})
- ______________ Users administration
- api/users/create (POST {token}{name}{password}{email})
- api/users/remove (POST {token}{email})
- api/users/set_expiration (POST {token}{email}{timestamp})
- */
- // Convert the URI request in to array // https://stackoverflow.com/questions/4213946/php-take-arguments-from-url-path
- $URL_REQ = explode('/', strtok(getenv('REQUEST_URI'), '?'));
- // Remove useless data from the request
- array_splice($URL_REQ, 0, 2);
- //Retirieve the function name
- $function = 'api_'.$URL_REQ[0];
- //Remove the main function from the request
- array_splice($URL_REQ, 0, 1);
- if(function_exists($function))
- {
- // Report only critical PHP errors
- error_reporting(E_ERROR);
- // Path definitions
- define("INCLUDES", "includes/");
- define("MODULES", "modules/");
- // require needed functions
- require_once INCLUDES.'functions.php';
- require_once INCLUDES.'helpers.php';
- require_once INCLUDES.'html_functions.php';
- require_once INCLUDES.'lib_remote.php';
- require_once INCLUDES.'config.inc.php';
- require_once MODULES.'config_games/server_config_parser.php';
- require_once MODULES.'gamemanager/home_handling_functions.php';
- // API tokens table
- define("API_TABLE", $table_prefix."api_tokens");
- // Connect to the database server and select database.
- $db = createDatabaseConnection($db_type, $db_host, $db_user, $db_pass, $db_name, $table_prefix);
- checkDbTable();
- $logged_in = false;
- if($function != 'api_token')
- {
- if(isset($_POST['token']))
- {
- $token = $_POST['token'];
- unset($_POST['token']);
- $query = "SELECT user_id FROM ".API_TABLE." WHERE `token` = '".$token."';";
- $result = $db->resultQuery($query);
- if(isset($result[0]['user_id']))
- {
- $user_info = $db->getUserById($result[0]['user_id']);
- if(isset($user_info['users_login']))
- {
- $logged_in = true;
- }
- }
- }
- else
- {
- outputJSON(array("status" => "300", "message" => "No token supplied"));
- }
- }
- if($logged_in or $function == 'api_token')
- {
- //call the function and output the returned data as json
- outputJSON($function($URL_REQ, $_POST));
- }
- else
- {
- outputJSON(array("status" => "301", "message" => "Invalid Token"));
- }
- }
- else
- {
- outputJSON(array("status" => "400", "message" => "BAD REQUEST"));
- }
- function checkDbTable()
- {
- global $db;
- if(!$db->query('SELECT 1 FROM '.API_TABLE.' LIMIT 1'))
- {
- $db->query( "CREATE TABLE IF NOT EXISTS `".API_TABLE."` (".
- "`user_id` int(11) NOT NULL,".
- "`token` varchar(64) NOT NULL,".
- "PRIMARY KEY (`user_id`),".
- "UNIQUE KEY user_id (user_id)".
- ") ENGINE=MyISAM DEFAULT CHARSET=latin1;");
- }
- }
- function outputJSON($result){
- // Send JSON output
- header('Content-Type: application/json');
- echo json_encode($result);
- exit();
- }
- function api_token($action, $args)
- {
- global $db;
- if($action[0] == "test")
- {
- $token = $action[1];
- $query = "SELECT user_id FROM ".API_TABLE." WHERE `token` = '".$token."';";
- $result = $db->resultQuery($query);
- if(isset($result[0]['user_id']))
- {
- $user_info = $db->getUserById($result[0]['user_id']);
- if(isset($user_info['users_login']))
- {
- $status = "200";
- $message = $user_info['users_role'];
- }
- else
- {
- $status = "400";
- $message = "Invalid Token";
- }
- }
- else
- {
- $status = "400";
- $message = "Invalid Token";
- }
- }
- if($action[0] == "create")
- {
- $user = urldecode($action[1]);
- $password = urldecode($action[2]);
- $userInfo = $db->getUser($user);
- if(isset($userInfo['users_passwd']) && md5($password) == $userInfo['users_passwd'])
- {
- $token = bin2hex(openssl_random_pseudo_bytes(32));
- $query ="INSERT INTO ".API_TABLE.
- " (user_id, token)".
- " VALUES".
- " ('".$userInfo['user_id']."', '".$token."')".
- " ON DUPLICATE KEY UPDATE".
- " user_id = VALUES(user_id),".
- " token = VALUES(token);";
- if($db->query($query))
- {
- $status = "200";
- $message = $token;
- }
- else
- {
- $status = "500";
- $message = "database failure";
- }
- }
- else
- {
- $status = "400";
- $message = "Invalid login information";
- }
- }
- return array("status" => $status, "message" => $message);
- }
- function api_start($action, $args)
- {
- global $db, $userInfo;
- $ip = $args['ip'];
- $port = $args['port'];
- $mod_id = isset($args['mod_id'])?$args['mod_id']:0;
- $status = "200";
- $message = "OK";
- return array("status" => $status, "message" => $message);
- }
- function api_stop($action, $args)
- {
- global $db, $userInfo;
- $ip = $args['ip'];
- $port = $args['port'];
- $mod_id = isset($args['mod_id'])?$args['mod_id']:0;
- $status = "200";
- $message = "OK";
- return array("status" => $status, "message" => $message);
- }
- function api_restart($action, $args)
- {
- global $db, $userInfo;
- $ip = $args['ip'];
- $port = $args['port'];
- $mod_id = isset($args['mod_id'])?$args['mod_id']:0;
- $status = "200";
- $message = "OK";
- return array("status" => $status, "message" => $message);
- }
- function api_rcon($action, $args)
- {
- global $db, $userInfo;
- $ip = $args['ip'];
- $port = $args['port'];
- $mod_id = isset($args['mod_id'])?$args['mod_id']:0;
- $command = $args['command'];
- $status = "200";
- $message = "OK";
- return array("status" => $status, "message" => $message);
- }
- function api_update($action, $args)
- {
- global $db, $userInfo;
- $ip = $args['ip'];
- $port = $args['port'];
- $mod_id = isset($args['mod_id'])?$args['mod_id']:0;
- if($action[0] == "steam")
- {
- $status = "200";
- $message = "OK";
- }
- if($action[0] == "rsync")
- {
- $status = "200";
- $message = "OK";
- }
- if($action[0] == "manual")
- {
- $download_url = $args['download_url'];
- $status = "200";
- $message = "OK";
- }
- if($action[0] == "master")
- {
- $status = "200";
- $message = "OK";
- }
- return array("status" => $status, "message" => $message);
- }
- function api_addon($action, $args)
- {
- global $db, $userInfo;
- $ip = $args['ip'];
- $port = $args['port'];
- $mod_id = isset($args['mod_id'])?$args['mod_id']:0;
- $addon_id = $args['addon_id'];
- $status = "200";
- $message = "OK";
- return array("status" => $status, "message" => $message);
- }
- function api_steam_workshop($action, $args)
- {
- global $db, $userInfo;
- $ip = $args['ip'];
- $port = $args['port'];
- $mod_id = isset($args['mod_id'])?$args['mod_id']:0;
- $workshop_id = $args['workshop_id'];
- $workshop_mod_id = $args['workshop_mod_id'];
- $status = "200";
- $message = "OK";
- return array("status" => $status, "message" => $message);
- }
- function api_file_mgr($action, $args)
- {
- global $db, $userInfo;
- $ip = $args['ip'];
- $port = $args['port'];
- $relative_path = $args['relative_path'];
- if($action[0] == "get")
- {
- $status = "200";
- $message = "OK";
- }
- if($action[0] == "save")
- {
- $contents = $args['contents'];
- $status = "200";
- $message = "OK";
- }
- if($action[0] == "remove")
- {
- $status = "200";
- $message = "OK";
- }
- return array("status" => $status, "message" => $message);
- }
- function api_servers($action, $args)
- {
- global $db, $userInfo;
- $status = "200";
- $message = "OK";
- return array("status" => $status, "message" => $message);
- }
- function api_users($action, $args)
- {
- global $db, $userInfo;
- $status = "200";
- $message = "OK";
- return array("status" => $status, "message" => $message);
- }
- ?>
Add Comment
Please, Sign In to add comment