annotate save.php @ 815:3b0770e1e616

Test Create: Delete Buttons included on final checks
author Nicholas Jillings <nicholas.jillings@eecs.qmul.ac.uk>
date Wed, 25 Nov 2015 09:26:10 +0000
parents
children 1b6fa37d46a4
rev   line source
nicholas@815 1 <?php
nicholas@815 2 header('Access-Control-Allow-Origin: *');
nicholas@815 3 header("Content-type: text/xml");
nicholas@815 4 $postText = file_get_contents('php://input');
nicholas@815 5 $datetime = date('ymdHis');
nicholas@815 6 $xmlfile = "save".$datetime."-".generateRandomString(6).".xml";
nicholas@815 7 $fileHandle = fopen("saves/".$xmlfile, 'w');
nicholas@815 8 if ($fileHandle == FALSE)
nicholas@815 9 {
nicholas@815 10 // Filehandle failed
nicholas@815 11 $xml = '<response state="error"><message>Could not open file</message></response>';
nicholas@815 12 echo $xml;
nicholas@815 13 return;
nicholas@815 14 }
nicholas@815 15 $wbytes = fwrite($fileHandle, $postText);
nicholas@815 16 if ($wbytes == FALSE)
nicholas@815 17 {
nicholas@815 18 // FileWrite failed
nicholas@815 19 $xml = '<response state="error"><message>Could not write file "saves/'.$xmlfile.'"</message></response>';
nicholas@815 20 echo $xml;
nicholas@815 21 return;
nicholas@815 22 }
nicholas@815 23 fclose($fileHandle);
nicholas@815 24
nicholas@815 25 // Return JSON confirmation data
nicholas@815 26 $xml = '<response state="OK"><message>OK</message><file bytes="'.$wbytes.'">"saves/'.$xmlfile.'"</file></response>';
nicholas@815 27 echo $xml;
nicholas@815 28
nicholas@815 29 // Random String generator from http://stackoverflow.com/questions/4356289/php-random-string-generator
nicholas@815 30 function generateRandomString($length = 10) {
nicholas@815 31 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
nicholas@815 32 $charactersLength = strlen($characters);
nicholas@815 33 $randomString = '';
nicholas@815 34 for ($i = 0; $i < $length; $i++) {
nicholas@815 35 $randomString .= $characters[rand(0, $charactersLength - 1)];
nicholas@815 36 }
nicholas@815 37 return $randomString;
nicholas@815 38 }
nicholas@815 39 ?>