-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.php
83 lines (71 loc) · 2.28 KB
/
server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
session_start();
$wbfile = $_SESSION['wbfile'];
if (isset($_GET['what']))
$action = $_GET['what'];
else if (isset($_POST['what']))
$action = $_POST['what'];
else
exit;
$whiteboard = unserialize(file_get_contents('whiteboards/'.$wbfile));
$shapes = $whiteboard['shapes'];
$actions = $whiteboard['last10Actions'];
switch ($action)
{
case 'getall':
foreach ($shapes as $shape)
$shapesSend .= ($shape . '|');
$shapesSend .= getTime();
echo $shapesSend;
break;
case 'update':
$lastUpdated = substr($_GET['lastupdated'], 1);
foreach ($actions as $timestamp => $action)
{
$timestamp = substr($timestamp, 1);
if ($timestamp > $lastUpdated)
$startResponse = true;
if ($startResponse)
$actionsSend .= ($action . '|');
}
$actionsSend .= getTime();
echo $actionsSend;
break;
case 'new':
$shape = $_POST['shape'];
$shapeCode = $_POST['shapecode'];
$shapes[$shapeCode] = $shape;
$actions[getTime()] = 'new#' . $shapeCode . '#' . $shape;
if (count($actions) > 10)
array_shift($actions);
$whiteboard = array('shapes' => $shapes, 'last10Actions' => $actions);
file_put_contents('whiteboards/'.$wbfile, serialize($whiteboard));
break;
case 'delete':
$shapeCode = $_POST['shapecode'];
unset($shapes[$shapeCode]);
$actions[getTime()] = 'delete#' . $shapeCode;
if (count($actions) > 10)
array_shift($actions);
$whiteboard = array('shapes' => $shapes, 'last10Actions' => $actions);
file_put_contents('whiteboards/'.$wbfile, serialize($whiteboard));
break;
case 'modify':
$shape = $_POST['shape'];
$shapeCode = $_POST['shapecode'];
if (isset($shapes[$shapeCode])) // shape still exists
{
$shapes[$shapeCode] = $shape;
$actions[getTime()] = 'modify#' . $shapeCode . '#' . $shape;
if (count($actions) > 10)
array_shift($actions);
$whiteboard = array('shapes' => $shapes, 'last10Actions' => $actions);
file_put_contents('whiteboards/'.$wbfile, serialize($whiteboard));
}
}
function getTime()
{
list($msec, $sec) = explode(" ", microtime());
return 't' . $sec . substr($msec, 2, 3);
}
?>