From 9ce1da5147b3df01812687562faca80623b27a38 Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 4 May 2016 14:27:35 +0200 Subject: Receive uploaded savegame from PHP --- inc/messages.inc | 3 +++ inc/savegame.inc | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/inc/messages.inc b/inc/messages.inc index 7b952b8..d08331a 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -12,6 +12,9 @@ $messages = array( "gamesave_not_found" => "Couldn't find the specified save file.", "gamesave_delete_fail" => "Couldn't delete the specified save file.", "gamesave_delete_success" => "Game save successfully removed from server", + + "upload_fail" => "Could not upload save file.", + "upload_success" => "Save file uploaded successfully: %s", ); function sendMessage($type, $msg, $fmt = null) { diff --git a/inc/savegame.inc b/inc/savegame.inc index 1fa95b0..8772585 100644 --- a/inc/savegame.inc +++ b/inc/savegame.inc @@ -89,4 +89,24 @@ function loadSave() { $_SESSION = array(); // drop current game parseSave($xml, $_SESSION); } + +function uploadSave() { + $fname = basename($_FILES['savefile']['name']); + $src = $_FILES['savefile']['tmp_name']; + + libxml_use_internal_errors(true); // Ignore errors when loading the received file + $xml = simplexml_load_file($src); + libxml_use_internal_errors(false); + if(!$xml) { + sendError("upload_fail"); + return; + } + $table = array(); + parseSave($xml, $table); // Parse received file + + $save = new SimpleXMLElement(""); + genXML($table, $save); // Regenerate it + if($save->asXML(SAVEDIR."/".$fname)) sendInfo("upload_success", array($fname)); + else sendError("upload_error"); +} ?> -- cgit v1.2.3-70-g09d2