forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.php
More file actions
230 lines (198 loc) · 5.39 KB
/
session.php
File metadata and controls
230 lines (198 loc) · 5.39 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* $Id: session.php,v 1.1 2012/11/15 13:26:41 gaudenz Exp $
* Copyright (c) 2006, Gaudenz Alder
*/
include_once("../src/mxServer.php");
// enables logging to a file
mxLog::addLogfile("session.log");
session_start();
mxLog::enter("main (session ".session_id().")");
if (!is_dir("test"))
{
mkdir("test");
chmod("test", 0777);
}
$document = $_SESSION['document'];
$sid = session_id();
$filename = $document."/".$sid;
$delta = $document."/delta.xml";
if (!isset($document) || isset($_GET["init"]))
{
$document = "test";
$filename = $document."/".session_id();
$delta = $document."/delta.xml";
$_SESSION["document"] = $document;
session_commit();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/xhtml+xml");
// Gives the client a unique namespace that
// is used as a prefix for new cell ids.
$ns = md5(uniqid(rand(), true));
mxLog::debug("session $sid initialized: ns=$ns");
echo "<message namespace=\"$ns\">";
echo "<state>";
$fp = fopen("diagrams/graphmodel.xml", "r");
fpassthru($fp);
fclose($fp);
echo "</state>";
echo "<delta>";
if (is_file($delta))
{
$fp = fopen($delta, "r+");
fpassthru($fp);
fclose($fp);
}
echo "</delta>";
echo "</message>";
// Deletes existing buffer
if (is_file($filename))
{
unlink($filename);
}
touch($filename);
chmod($filename, 0777);
}
else
{
// Gets the XML parameter from the POST request and converts all linefeeds
// into a HTML entity. This is required for correct handling of the XML on
// the client side.
if (isset($_POST["xml"]))
{
$xml = str_replace("\n", "
", stripslashes($_POST["xml"]));
// TODO: Take only the edits from the XML
$edits = "";
$doc = mxUtils::parseXml($xml);
$child = $doc->documentElement;
if ($child->nodeName == "message")
{
$child = $child->firstChild;
while ($child != null)
{
if ($child->nodeName == "delta")
{
$edit = $child->firstChild;
while ($edit != null)
{
if ($edit->nodeName == "edit")
{
$edits .= $doc->saveXML($edit);
}
$edit = $edit->nextSibling;
}
}
$child = $child->nextSibling;
}
}
// Appends the change to all connected sessions except the incoming
// session and the global delta file
if (strlen($edits) > 0)
{
mxLog::debug("received changes from ".session_id().
": ".strlen($edits)." bytes");
// Makes sure the global delta file exists so that the change is
// appended below
if (!is_file($delta))
{
touch($delta);
chmod($delta, 0777);
}
// Clears out the delta file if this change contains a mxRootChange
// in which case the previous changes will no longer be visible and
// just waste bandwidth.
if (strpos($edits, "mxRootChange") > 0)
{
$fp = fopen($delta, "r+");
fpassthru($fp);
ftruncate($fp, 0);
fflush($fp);
fclose($fp);
}
// Dispatches the XML to all sessions except the incoming session
// TODO: Remove dead sessions
$fp = opendir($document);
while($filename = readdir($fp))
{
if ($filename!= "." &&
$filename != ".." &&
!is_dir("$document/$filename") &&
$filename != session_id())
{
mxLog::debug("dispatch changes to $filename");
$tmp = fopen("$document/$filename", "a");
fwrite($tmp, $edits);
fflush($tmp);
fclose($tmp);
}
}
flush();
}
}
else
{
// Makes sure to cancel existing pending requests before they consume the
// change data after a refresh, where the request must be served instead.
$requestid = md5(uniqid(rand(), true));
$_SESSION['requestid'] = $requestid;
session_commit();
mxLog::debug("request $requestid enters");
if (!is_file($filename))
{
touch($filename);
chmod($filename, 0777);
}
else
{
// Keeps the request for 10 secs and asks for changes each second
$timeout = 10;
$count = 0;
while (is_file($filename) &&
filesize($filename) == 0 &&
$count < $timeout &&
$_SESSION['requestid'] == $requestid)
{
sleep(1);
clearstatcache();
$count++;
// Sync the session state
session_start();
session_commit();
}
// Sync the session state
session_start();
session_commit();
if ($_SESSION['requestid'] != $requestid)
{
mxLog::debug("request $requestid has died");
}
else if (filesize($filename) > 0)
{
mxLog::debug("request $requestid leaves: ".filesize($filename)." bytes");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/xhtml+xml");
// Sends the changes to the client
echo "<message>";
echo "<delta>";
$fp = fopen($filename, "r+");
fpassthru($fp);
ftruncate($fp, 0);
fflush($fp);
fclose($fp);
echo "</delta>";
echo "</message>";
}
else
{
touch($filename);
}
}
}
}
mxLog::leave();
mxLog::close();
?>