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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
| ' Gambas module file
PUBLIC Root AS STRING PUBLIC Path AS STRING PUBLIC Exist AS BOOLEAN PUBLIC Edit AS BOOLEAN PUBLIC Image AS BOOLEAN
PUBLIC SUB GetPagePath(OPTIONAL bSuffix AS BOOLEAN) AS STRING
DIM sPath AS STRING
sPath = Root &/ "data" &/ Path &/ "~page" IF bSuffix THEN sPath &= "." & Format(Now, "yyyymmddhhnnssuu") & "." & Session["login"] RETURN sPath
END
PRIVATE SUB MakeDir(sDir AS STRING)
DIM sPath AS STRING DIM sElt AS STRING
FOR EACH sElt IN Split(sDir, "/") sPath &/= sElt 'Shell "echo MKDIR: " & Shell$(sPath) & " >> /tmp/session.log" Wait TRY MKDIR "/" & sPath NEXT
'If Not Exist(sDir) Or If Not IsDir(sDir) Then Return True
END
PRIVATE SUB InitWiki()
MKDIR Root &/ "data" COPY "passwd" TO Root &/ "passwd" COPY "page" TO GetPagePath() COPY "page" TO GetPagePath(TRUE)
END
PRIVATE SUB LockPage()
DIM sLock AS STRING = File.Dir(GetPagePath()) &/ "~lock" DIM hLock AS File DIM I AS INTEGER
FOR I = 1 TO 20 TRY hLock = LOCK sLock IF NOT ERROR THEN RETURN SLEEP 0.1 NEXT
Error.Raise(("Unable to lock the current page"))
END
PUBLIC SUB Main()
DIM iSize AS LONG DIM sMsg AS STRING DIM sIdent AS STRING DIM iPos AS INTEGER DIM sLogin AS STRING DIM sPasswd AS STRING DIM aPage AS String[] DIM sDir AS STRING
' The wiki is stored in the following root directory.
Root = User.Home &/ "wiki" TRY MKDIR Root
' All pages are stored in a "data" directory stored in the root directory. ' If the "data" directory does not exist, the wiki is initialized with a default root page.
IF NOT Exist(Root &/ "data") THEN InitWiki Response.Redirect(Application.Root) RETURN ENDIF
' The url path is directly converted to a relative directory path inside the "data" directory located in the root directory. ' The page contents is stored inside a "~page" file located in that directory. ' Consequently, no "~" character is allowed in the url path. ' Each page modification is archived in a "~page.<date>.<user>" file, allowing changes to be undone.
Path = Request.Path IF Path = "/" THEN Path = ""
' Handle resource files: images, style sheet...
IF Path AND IF Exist(".public" &/ Path) THEN Response.SendFile(".public" &/ Path) RETURN ENDIF
' Reject pages whose url includes a "~" character.
IF InStr(Path, "~") THEN Response.Status = "404 Not Found" Response.Begin PRINT ("<h1>404 Not Found</h1>") Response.End RETURN ENDIF
IF Request.Exist("logout") THEN
Session.Abandon
ELSE IF Request["login"] AND IF Request["password"] THEN
Session.Abandon
FOR EACH sIdent IN Split(File.Load(Root &/ "passwd"), "\n") iPos = InStr(sIdent, ": ") IF iPos = 0 THEN CONTINUE sLogin = Trim(Left(sIdent, iPos - 1)) sPasswd = Trim(Mid$(sIdent, iPos + 2)) IF sLogin = Request["login"] AND IF sPasswd = Request["password"] THEN Session["login"] = sLogin BREAK ENDIF NEXT
ENDIF
TRY iSize = Stat(GetPagePath()).Size Exist = iSize > 0 Image = IsImage(Path)
IF Session.Id THEN
IF Request.Exist("save") THEN
IF Request.Exist("page") THEN MakeDir(File.Dir(GetPagePath())) LockPage File.Save(GetPagePath(), Request["page"]) COPY GetPagePath() TO GetPagePath(TRUE) Response.Redirect(Application.Root &/ Request.Path) RETURN ELSE IF Request.Exist("file") AND IF Image THEN MakeDir(File.Dir(GetPagePath())) LockPage TRY KILL GetPagePath() COPY Request.Files["file"] TO GetPagePath() COPY GetPagePath() TO GetPagePath(TRUE) Response.Redirect(Application.Root &/ Request.Path) RETURN ENDIF
ELSE IF Request.Exist("delete") THEN
IF Exist THEN LockPage File.Save(GetPagePath(), "") COPY GetPagePath() TO GetPagePath(TRUE) ENDIF Response.Redirect(Application.Root &/ Request.Path) RETURN
ELSE IF Request.Exist("undo") THEN
sDir = File.Dir(GetPagePath()) aPage = Dir(sDir, "~page.*").Sort(gb.Descent) IF aPage.Count >= 2 THEN LockPage TRY KILL sDir &/ aPage[0] TRY KILL GetPagePath() TRY COPY sDir &/ aPage[1] TO GetPagePath() ENDIF Response.Redirect(Application.Root &/ Request.Path) RETURN
ELSE IF Request.Exist("create") OR IF Request.Exist("edit") THEN
Edit = TRUE
ENDIF
ENDIF
IF Image THEN IF NOT Session.Id OR IF Request.Exist("show") THEN Response.SendFile(GetPagePath(), GetContentTypeFrom(Path)) RETURN ENDIF ENDIF
Wiki.Render
CATCH
sMsg = Error.Where & ": " & Error.Text Response.Begin Response.ContentType = "text/plain;charset=utf-8" PRINT "<pre>"; sMsg; "</pre>" Response.End
END
PUBLIC SUB GetPageTitle(sPath AS STRING) AS STRING
DIM sFile AS STRING DIM iPos AS INTEGER
sPath = Root &/ "data" &/ sPath &/ "~page" IF NOT Exist(sPath) THEN RETURN
sFile = LTrim(File.Load(sPath))
IF sFile BEGINS "# " THEN iPos = InStr(sFile, "\n") IF iPos = 0 THEN iPos = Len(sFile) RETURN Trim(Mid$(sFile, 3, iPos - 3)) ENDIF
END
PUBLIC SUB IsImage(sPath AS STRING) AS BOOLEAN
DIM sExt AS STRING
sExt = File.Ext(sPath) RETURN ["png", "jpg", "jpeg", "gif"].Exist(sExt)
END
PRIVATE SUB GetContentTypeFrom(sPath AS STRING) AS STRING
SELECT CASE Lower(File.Ext(sPath)) CASE "css" RETURN "text/css" CASE "jpg", "jpeg", "jpe", "thumb" RETURN "image/jpeg" CASE "png" RETURN "image/png" CASE "gif" RETURN "image/gif" CASE "tiff", "tif" RETURN "image/tiff" CASE "odt" RETURN "application/vnd.oasis.opendocument.text" CASE "doc" RETURN "application/msword" CASE "ods" RETURN "application/vnd.oasis.opendocument.spreadsheet" CASE "xls" RETURN "application/msexcel" CASE "pdf" RETURN "application/pdf" CASE "zip" RETURN "application/zip" CASE "html", "htm" RETURN "text/html" CASE "txt" RETURN "text/plain" CASE "avi" RETURN "video/x-msvideo" CASE "mpg", "mpeg" RETURN "video/mpeg" CASE "ps" RETURN "application/postscript" CASE "dwg" RETURN "application/acad" CASE "wav" RETURN "audio/x-wav" CASE "ogg" RETURN "application/ogg" CASE "jar" RETURN "application/x-jar" 'Case "xml", "kml" ' Return "text/plain" CASE ELSE RETURN "application/octet-stream" END SELECT
END
|