comparison save.php @ 829:75e1c1ce6604

Bug #1464 #1456, PHP server hashes file contents to differentiate file names. Confirmed working on multiple connections with same file contents.
author Nicholas Jillings <nicholas.jillings@eecs.qmul.ac.uk>
date Fri, 27 Nov 2015 09:27:33 +0000
parents 1b6fa37d46a4
children 2647dd909229
comparison
equal deleted inserted replaced
828:16ee07203740 829:75e1c1ce6604
1 <?php 1 <?php
2 header('Access-Control-Allow-Origin: *'); 2 header('Access-Control-Allow-Origin: *');
3 header("Content-type: text/xml"); 3 header("Content-type: text/xml");
4 $postText = file_get_contents('php://input'); 4 $postText = file_get_contents('php://input');
5 $sha1_hash = sha1($postText);
5 $datetime = date('ymdHis'); 6 $datetime = date('ymdHis');
6 $xmlfile = "save".$datetime."-".generateRandomString(6).".xml"; 7 $xmlfile = "save".$datetime."-".$sha1_hash.".xml";
7 $fileHandle = fopen("saves/".$xmlfile, 'w'); 8 $fileHandle = fopen("saves/".$xmlfile, 'w');
8 if ($fileHandle == FALSE) 9 if ($fileHandle == FALSE)
9 { 10 {
10 // Filehandle failed 11 // Filehandle failed
11 $xml = '<response state="error"><message>Could not open file</message></response>'; 12 $xml = '<response state="error"><message>Could not open file</message></response>';
23 fclose($fileHandle); 24 fclose($fileHandle);
24 25
25 // Return JSON confirmation data 26 // Return JSON confirmation data
26 $xml = '<response state="OK"><message>OK</message><file bytes="'.$wbytes.'">"saves/'.$xmlfile.'"</file></response>'; 27 $xml = '<response state="OK"><message>OK</message><file bytes="'.$wbytes.'">"saves/'.$xmlfile.'"</file></response>';
27 echo $xml; 28 echo $xml;
28
29 // Random String generator from http://stackoverflow.com/questions/4356289/php-random-string-generator
30 function generateRandomString($length = 10) {
31 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
32 $charactersLength = strlen($characters);
33 $randomString = '';
34 for ($i = 0; $i < $length; $i++) {
35 $randomString .= $characters[rand(0, $charactersLength - 1)];
36 }
37 return $randomString;
38 }
39 ?> 29 ?>