-
Notifications
You must be signed in to change notification settings - Fork 23
/
Application.cfc
348 lines (284 loc) · 11 KB
/
Application.cfc
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
This file is part of MuraFW1
Copyright 2010-2020 Stephen J. Withington, Jr.
Licensed under the Apache License, Version v2.0
http://www.apache.org/licenses/LICENSE-2.0
NOTES:
Edit the setSessionCache() method to alter the 'expires' key.
Defaults to 1 hour. The sessionCache will also expire
if the application has been reloaded.
*/
component persistent="false" accessors="true" output="false" extends="framework.one" {
include 'config.fw1.cfm'; // framework variables
this.pluginPath = GetDirectoryFromPath(GetCurrentTemplatePath());
this.muraroot = Left(this.pluginPath, Find('plugins', this.pluginPath) - 1);
this.depth = ListLen(RemoveChars(this.pluginPath,1, Len(this.muraroot)), '\/');
this.includeroot = RepeatString('../', this.depth);
if ( DirectoryExists(this.muraroot & 'core') ) {
// Using 7.1
this.muraAppConfigPath = this.includeroot & 'core/';
include this.muraAppConfigPath & 'appcfc/applicationSettings.cfm';
} else {
// Pre 7.1
this.muraAppConfigPath = this.includeroot & 'config';
include this.includeroot & 'config/applicationSettings.cfm';
try {
include this.includeroot & 'config/mappings.cfm';
include this.includeroot & 'plugins/mappings.cfm';
} catch(any e) {}
}
variables.fw1Keys = 'SERVICEEXECUTIONCOMPLETE,LAYOUTS,CONTROLLEREXECUTIONCOMPLETE,VIEW,SERVICES,CONTROLLERS,CONTROLLEREXECUTIONSTARTED';
public string function doAction(string action='') {
var p = variables.framework.package;
var fwa = variables.framework.action;
var local = {};
clearFW1Request();
local.targetPath = getPageContext().getRequest().getRequestURI();
setupFrameworkDefaults();
setupRequestDefaults();
if ( !isFrameworkInitialized() || isFrameworkReloadRequest() ) {
setupApplicationWrapper();
}
restoreFlashContext();
request.context[fwa] = StructKeyExists(form, fwa)
? form[fwa] : StructKeyExists(url, fwa)
? url[fwa] : StructKeyExists(request, fwa)
? request[fwa] : getFullyQualifiedAction(arguments.action);
request.action = getFullyQualifiedAction(request.context[fwa]);
// viewKey: package_subsystem_section_item
local.viewKey = UCase(
p
& '_' & getSubSystem(arguments.action)
& '_' & getSection(arguments.action)
& '_' & getItem(arguments.action)
);
local.response = variables.framework.siloSubsystems
? getCachedView(local.viewKey) : '';
local.newViewRequired = !Len(local.response)
? true : getSubSystem(arguments.action) == getSubSystem(request.context[fwa])
? true : false;
if ( local.newViewRequired ) {
onRequestStart(local.targetPath);
savecontent variable='local.response' {
onRequest(local.targetPath);
};
clearFW1Request();
if ( variables.framework.siloSubsystems ) {
setCachedView(local.viewKey, local.response);
}
}
return local.response;
}
// exposed for use by model.handlers.murafw1.cfc:onApplicationLoad()
public void function setupApplicationWrapper() {
lock scope='application' type='exclusive' timeout=20 {
super.setupApplicationWrapper();
};
}
public void function setupApplication() {
var local = {};
if ( !StructKeyExists(application, 'pluginManager') ) {
location(url='/', addtoken=false);
}
lock scope='application' type='exclusive' timeout=20 {
getFw1App().pluginConfig = application.pluginManager.getConfig(ID=variables.framework.applicationKey);
};
}
public void function setupRequest() {
var local = {};
param name='request.context.siteid' default='';
if ( !StructKeyExists(session, 'siteid') ) {
lock scope='session' type='exclusive' timeout='10' {
session.siteid = 'default';
};
}
secureRequest();
request.context.isAdminRequest = isAdminRequest();
request.context.isFrontEndRequest = isFrontEndRequest();
if ( StructKeyExists(url, application.configBean.getAppReloadKey()) ) {
setupApplication();
//setupApplicationWrapper();
}
if ( Len(Trim(request.context.siteid)) && ( session.siteid != request.context.siteid) ) {
local.siteCheck = application.settingsManager.getSites();
if ( StructKeyExists(local.siteCheck, request.context.siteid) ) {
lock scope='session' type='exclusive' timeout='10' {
session.siteid = request.context.siteid;
};
};
}
if ( !StructKeyExists(request.context, '$') ) {
request.context.$ = StructKeyExists(request, 'muraScope') ? request.muraScope : application.serviceFactory.getBean('muraScope').init(session.siteid);
}
if ( !StructKeyExists(request.context, 'm') ) {
request.context.m = request.context.$;
}
request.context.pc = getFw1App().pluginConfig;
request.context.pluginConfig = getFw1App().pluginConfig;
request.context.action = request.context[variables.framework.action];
}
public void function setupView() {
var httpRequestData = GetHTTPRequestData();
if (
StructKeyExists(httpRequestData.headers, 'X-#variables.framework.package#-AJAX')
&& IsBoolean(httpRequestData.headers['X-#variables.framework.package#-AJAX'])
&& httpRequestData.headers['X-#variables.framework.package#-AJAX']
) {
setupResponse();
}
}
public void function setupResponse() {
var httpRequestData = GetHTTPRequestData();
if (
StructKeyExists(httpRequestData.headers, 'X-#variables.framework.package#-AJAX')
&& IsBoolean(httpRequestData.headers['X-#variables.framework.package#-AJAX'])
&& httpRequestData.headers['X-#variables.framework.package#-AJAX']
) {
StructDelete(request.context, 'fw');
StructDelete(request.context, '$');
StructDelete(request.context, 'm');
WriteOutput(SerializeJSON(request.context));
abort;
}
}
public void function setupSession() {
include this.muraAppConfigPath & '/appcfc/onSessionStart_include.cfm';
}
public void function onSessionEnd() {
include this.muraAppConfigPath & '/appcfc/onSessionEnd_include.cfm';
}
// path=#resolvePath()#
public string function buildURL(required string action, string path='./', any queryString='') {
var regx = '&?compactDisplay=[true|false]';
arguments.action = getFullyQualifiedAction(arguments.action);
if (
StructKeyExists(request.context, 'compactDisplay')
&& IsBoolean(request.context.compactDisplay)
&& !REFindNoCase(regx, arguments.action)
&& !REFindNoCase(regx, arguments.queryString)
) {
var qs = 'compactDisplay=' & request.context.compactDisplay;
if ( !Find('?', arguments.action) ) {
if ( isSimpleValue(arguments.queryString) ) {
arguments.queryString = ListAppend(arguments.queryString, qs, '&');
} else if ( isStruct(arguments.queryString) ) {
structAppend(arguments.queryString, {"compactDisplay"=request.context.compactDisplay} );
}
} else {
arguments.action = ListAppend(arguments.action, qs, '&');
}
}
return super.buildURL(argumentCollection=arguments);
}
public string function redirect(required string action, string preserve='none', string append='none', string path='./', string queryString='', string statusCode='302') {
if ( request.muraAPIRequest ) {
request.muraJSONRedirectURL = buildURL(argumentCollection=arguments);
renderData('json','redirect complete','200');
return true;
}
return super.redirect(argumentCollection=arguments);
}
public string function resolvePath() {
return './';
}
public any function isFrameworkInitialized() {
return super.isFrameworkInitialized() && StructKeyExists(getFw1App(), 'cache');
}
// ========================== Errors & Missing Views ==========================
public any function onError() output="true" {
//var scopes = 'application,arguments,cgi,client,cookie,form,local,request,server,session,url,variables';
var scopes = 'local,request,session';
var arrScopes = ListToArray(scopes);
var i = '';
var scope = '';
WriteOutput('<h2>' & variables.framework.package & ' ERROR</h2>');
if ( IsBoolean(variables.framework.debugMode) && variables.framework.debugMode ) {
for ( i=1; i <= ArrayLen(arrScopes); i++ ) {
scope = arrScopes[i];
WriteDump(var=Evaluate(scope),label=UCase(scope));
};
}
abort;
}
// ========================== Helper Methods ==================================
public any function secureRequest() {
return !isAdminRequest() || (StructKeyExists(session, 'mura') && ListFindNoCase(session.mura.memberships,'S2')) ? true :
!StructKeyExists(session, 'mura')
|| !StructKeyExists(session, 'siteid')
|| !application.permUtility.getModulePerm(getFw1App().pluginConfig.getModuleID(), session.siteid)
? goToLogin() : true;
}
private void function goToLogin() {
location(url='#application.configBean.getContext()#/admin/index.cfm?muraAction=clogin.main&returnURL=#application.configBean.getContext()#/plugins/#variables.framework.package#/', addtoken=false);
}
public boolean function isAdminRequest() {
return StructKeyExists(request, 'context') && ListFirst(request.context[variables.framework.action], ':') == 'admin' ? true : false;
}
public boolean function isFrontEndRequest() {
return StructKeyExists(request, 'murascope');
}
// ========================== STATE ===========================================
public void function clearFW1Request() {
var arrFW1Keys = ListToArray(variables.fw1Keys);
var i = '';
if ( StructKeyExists(request, '_fw1') ) {
for ( i=1; i <= ArrayLen(arrFW1Keys); i++ ) {
StructDelete(request._fw1, arrFW1Keys[i]);
}
}
request._fw1 = {
cgiScriptName = CGI.SCRIPT_NAME
, cgiPathInfo = CGI.PATH_INFO
, cgiRequestMethod = CGI.REQUEST_METHOD
, controllers = []
, requestDefaultsInitialized = false
, services = []
, doTrace = variables.framework.trace
, trace = []
};
}
// ========================== PRIVATE =========================================
private any function getCachedView(required string viewKey) {
var view = '';
var cache = getSessionCache();
if ( StructKeyExists(cache, 'views') && StructKeyExists(cache.views, arguments.viewKey) ) {
view = cache.views[arguments.viewKey];
}
return view;
}
private void function setCachedView(required string viewKey, string viewValue='') {
lock scope='session' type='exclusive' timeout=10 {
session[variables.framework.package].views[arguments.viewKey] = arguments.viewValue;
};
}
private boolean function isCacheExpired() {
var p = variables.framework.package;
return !StructKeyExists(session, p)
|| DateCompare(now(), session[p].expires, 's') == 1
|| DateCompare(application.appInitializedTime, session[p].created, 's') == 1
? true : false;
}
private any function getSessionCache() {
var local = {};
if ( isCacheExpired() ) {
setSessionCache();
}
lock scope='session' type='readonly' timeout=10 {
local.cache = session[variables.framework.package];
};
return local.cache;
}
private void function setSessionCache() {
var p = variables.framework.package;
// Expires - s:seconds, n:minutes, h:hours, d:days
lock scope='session' type='exclusive' timeout=10 {
StructDelete(session, p);
session[p] = {
created = Now()
, expires = DateAdd('h', 1, Now())
, sessionid = Hash(CreateUUID())
, views = {}
};
};
}
}