-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontroller.xql
246 lines (224 loc) · 9.55 KB
/
controller.xql
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
xquery version "3.0";
declare namespace json="http://www.json.org";
(:import module namespace console="http://exist-db.org/xquery/console";:)
declare variable $exist:path external;
declare variable $exist:resource external;
declare variable $exist:controller external;
declare variable $exist:prefix external;
declare variable $exist:root external;
(: Determine if the persistent login module is available :)
declare variable $login :=
let $tryImport :=
try {
util:import-module(xs:anyURI("http://exist-db.org/xquery/login"), "login", xs:anyURI("resource:org/exist/xquery/modules/persistentlogin/login.xql")),
true()
} catch * {
false()
}
return
if ($tryImport) then
function-lookup(xs:QName("login:set-user"), 3)
else
local:fallback-login#3
;
(:~
Fallback login function used when the persistent login module is not available.
Stores user/password in the HTTP session.
:)
declare function local:fallback-login($domain as xs:string, $maxAge as xs:dayTimeDuration?, $asDba as xs:boolean) {
let $durationParam := request:get-parameter("duration", ())
let $user := request:get-parameter("user", ())
let $password := request:get-parameter("password", ())
let $logout := request:get-parameter("logout", ())
return
if ($durationParam) then
error(xs:QName("login"), "Persistent login module not enabled in this version of eXist-db")
else if ($logout) then
session:invalidate()
else
if ($user) then
let $isLoggedIn := xmldb:login("/db", $user, $password, true())
return
if ($isLoggedIn and (not($asDba) or xmldb:is-admin-user($user))) then (
session:set-attribute("digitalmishnah.user", $user),
session:set-attribute("digitalmishnah.password", $password),
request:set-attribute($domain || ".user", $user),
request:set-attribute("xquery.user", $user),
request:set-attribute("xquery.password", $password)
) else
()
else
let $user := session:get-attribute("digitalmishnah.user")
let $password := session:get-attribute("digitalmishnah.password")
return (
request:set-attribute($domain || ".user", $user),
request:set-attribute("xquery.user", $user),
request:set-attribute("xquery.password", $password)
)
};
if ($exist:path eq '') then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="{request:get-uri()}/"/>
</dispatch>
else if ($exist:path eq "/" or $exist:path eq "/index.html") then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/templates/index.html"/>
<view>
<forward url="{$exist:controller}/modules/view.xql"/>
</view>
<error-handler>
<forward url="{$exist:controller}/templates/error-page.html" method="get"/>
<forward url="{$exist:controller}/modules/view.xql"/>
</error-handler>
</dispatch>
else if ($exist:resource = '$app-root') then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="{concat(request:get-context-path(), '/', request:get-attribute("$exist:prefix"), '/', request:get-attribute('$exist:controller'))}"/>
</dispatch>
else if (contains($exist:path, '$app-root')) then
(: redirect to resources actual location :)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{concat($exist:controller, '/', substring-after($exist:path, '/$app-root/'))}"/>
</dispatch>
else if ($exist:path eq "/login") then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/templates/login.html"/>
<view>
<forward url="{$exist:controller}/modules/view.xql"/>
</view>
<error-handler>
<forward url="{$exist:controller}/templates/error-page.html" method="get"/>
<forward url="{$exist:controller}/modules/view.xql"/>
</error-handler>
</dispatch>
else if ($exist:path eq "/edit") then (
let $loggedIn := $login("org.exist.login", (), false())
let $user := request:get-attribute("org.exist.login.user")
return
if ($user) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/templates/edit.html"/>
<view>
<forward url="{$exist:controller}/modules/view.xql">
<set-attribute name="resource" value="edit"/>
</forward>
</view>
</dispatch>
else
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="{concat(request:get-context-path(), '/', request:get-attribute("$exist:prefix"), '/', request:get-attribute('$exist:controller'))}/login"/>
</dispatch>
)
else if ($exist:path eq "/align") then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/templates/align.html"/>
<view>
<forward url="{$exist:controller}/modules/view.xql">
<set-attribute name="resource" value="align"/>
</forward>
</view>
</dispatch>
(:
: List browsable witnesses
:)
else if ($exist:path eq "/browse") then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/templates/browse.html"/>
<view>
<forward url="{$exist:controller}/modules/view.xql"/>
</view>
</dispatch>
(:
: Display witness
:)
else if (matches($exist:path, "/browse/[^/]+/[^/]+/[^/]+")) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/templates/read.html"/>
<view>
<forward url="{$exist:controller}/modules/view.xql">
<set-attribute name="resource" value="read"/>
<set-attribute name="path" value="{$exist:path}"/>
</forward>
</view>
</dispatch>
(:
: Digital apparatus criticus
:)
else if ($exist:path eq "/compare") then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/templates/compare.html"/>
<view>
<forward url="{$exist:controller}/modules/view.xql">
<set-attribute name="resource" value="compare"/>
<set-attribute name="path" value="{$exist:path}"/>
</forward>
</view>
</dispatch>
(:
: Digital apparatus criticus - view
:)
else if (matches($exist:path, "/compare/\d+\.\d+\.\d+(\.\d+)?/(\w+,?)+/(align|apparatus|synopsis)")) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/templates/compare.html"/>
<view>
<forward url="{$exist:controller}/modules/view.xql">
<set-attribute name="resource" value="compare"/>
<set-attribute name="path" value="{$exist:path}"/>
</forward>
</view>
</dispatch>
(:
: Digital apparatus criticus - view
:)
else if (matches($exist:path, "/edit/toTEIXML")) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/modules/jsonToTEIApp.xql"/>
</dispatch>
(:
: Login a user via AJAX. Just returns a 401 if login fails.
:)
else if ($exist:resource = 'dologin') then
let $loggedIn := $login("org.exist.login", (), false())
let $user := request:get-attribute("org.exist.login.user")
return
try {
(
util:declare-option("exist:serialize", "method=json media-type=application/json"),
if ($user) then
<status>
<user>{request:get-attribute("org.exist.login.user")}</user>
<isAdmin json:literal="true">{ xmldb:is-admin-user((request:get-attribute("org.exist.login.user"),request:get-attribute("xquery.user"), 'nobody')[1]) }</isAdmin>
</status>
else (
response:set-status-code(401),
<status>fail</status>
)
)
} catch * {
response:set-status-code(401),
<status>{$err:description}</status>
}
(:else if (ends-with($exist:resource, ".html")) then
(\: the html page is run through view.xql to expand templates :\)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/templates/{$exist:resource}"/>
<view>
<forward url="{$exist:controller}/modules/view.xql"/>
</view>
<error-handler>
<forward url="{$exist:controller}/templates/error-page.html" method="get"/>
<forward url="{$exist:controller}/modules/view.xql"/>
</error-handler>
</dispatch>:)
(: Resource paths starting with $shared are loaded from the shared-resources app :)
else if (contains($exist:path, "/$shared/")) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="/shared-resources/{substring-after($exist:path, '/$shared/')}">
<set-header name="Cache-Control" value="max-age=3600, must-revalidate"/>
</forward>
</dispatch>
else
(: everything else is passed through :)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<cache-control cache="yes"/>
</dispatch>