-
Notifications
You must be signed in to change notification settings - Fork 4
/
FileSession.hx
168 lines (118 loc) · 3.48 KB
/
FileSession.hx
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
/**
* ...
* @author Andreas Söderlund
* @author Andrea Parodi
*/
package php.ufront.web;
import ufront.web.IHttpSessionState;
import thx.sys.Lib;
import sys.FileSystem;
import sys.io.File;
import haxe.BaseCode;
import haxe.Unserializer;
import haxe.Serializer;
/** @todo compare with neko implementation */
class FileSession implements IHttpSessionState
{
private function add(name:String,value:String ,expire:Int){
var result:Bool=false;
untyped __php__("
if ($expire!=0)
$expire=time()+$expire;
$result=( 1==setcookie ( $name , $value,$expire ) );
");
if (!result)
throw "Cookie could not be set. Output already started.";
}
private function getCookie(){
var result=null;
untyped __php__("
if (array_key_exists('ufront', $_COOKIE))
$result=$_COOKIE['ufront'];
");
return result;
}
private function getId(){
return Std.string(untyped __php__("uniqid()"));
}
private var savePath : String;
private var content: Hash<Dynamic>;
private var sessionId: String;
private var sessionStoragePath: String;
//added sessionId as parameter to allow authentication not
//base on session (not sure it's the best way to do this)
public function new(savePath : String,?sessionId:String=null)
{
this.savePath=savePath;
this.content=new Hash<Dynamic>();
this.sessionId=sessionId;
setupSessionId();
if (!FileSystem.exists(savePath))
FileSystem.createDirectory(savePath);
if (!FileSystem.exists(sessionStoragePath))
FileSystem.createDirectory(sessionStoragePath);
}
private function setupSessionId(){
if (this.sessionId==null){
var cookie=getCookie();
if (cookie==null){
this.sessionId=getId();
add("ufront",this.sessionId,0);
} else {
/** @todo sanitize coockie id to avoid directory escapes */
/** @todo rebuild a new coockie id on every request and rename storage folder? */
var id=cookie;
if (!FileSystem.exists(this.savePath+"/"+id)){
//this code is to prevent session fixation. create a new cookie
//because session storage does not exists
id=getId();
add("ufront",id,0);
}
this.sessionId=id;
}
}
/** @todo check for existence of directory for provided session ids */
/** @todo MD5 the sessionId to avoid knowledge of the cookie looking at filesystem */
this.sessionStoragePath=this.savePath+"/"+this.sessionId;
// var writer=File.append("c:\\log.txt",false);
// writer.writeString("Login "+this.sessionStoragePath+"\n");
// writer.close();
}
public function setLifeTime(lifetime:Int)
{
add("ufront",this.sessionId,lifetime);
}
public function dispose() : Void
{
}
public function clear() : Void
{
for (file in FileSystem.readDirectory(sessionStoragePath))
FileSystem.deleteFile(sessionStoragePath+"/"+file);
FileSystem.deleteDirectory(sessionStoragePath);
}
public function get(name : String) : Dynamic
{
return Unserializer.run(File.getContent(getVarPath(name)));
}
/** @todo sanitize name to avoid directory escapes*/
private function getVarPath(name : String){
return sessionStoragePath+"/"+name;
}
public function set(name : String, value : Dynamic) : Void
{
File.saveContent(getVarPath(name),Serializer.run(value));
}
public function exists(name : String) : Bool
{
return FileSystem.exists(getVarPath(name));
}
public function remove(name : String) : Void
{
FileSystem.deleteFile(getVarPath(name));
}
public function id() : String
{
return this.sessionId;
}
}