forked from blueriver/MuraGoogleSitemaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.cfc
370 lines (300 loc) · 11.9 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
This file is part of the BlueRiver Inc. Google Sitemaps Plugin
Copyright 2017 BlueRiver Inc.
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.
See /includes/displayObjects.cfc && /includes/eventHandler.cfc
on how to access these methods.
*/
component persistent="false" accessors="true" output="false" extends="includes.framework.one" {
include 'includes/fw1config.cfm'; // framework variables
local.pluginPath = GetDirectoryFromPath(GetCurrentTemplatePath());
local.muraroot = Left(local.pluginPath, Find('plugins', local.pluginPath) - 1);
local.depth = ListLen(RemoveChars(local.pluginPath,1, Len(local.muraroot)), '\/');
local.includeroot = RepeatString('../', local.depth);
if ( DirectoryExists(local.muraroot & 'core') ) {
// Using 7.1
this.muraAppConfigPath = local.includeroot & 'core/';
include this.muraAppConfigPath & 'appcfc/applicationSettings.cfm';
} else {
// Pre 7.1
this.muraAppConfigPath = local.includeroot & 'config';
include local.includeroot & 'config/applicationSettings.cfm';
try {
include local.includeroot & 'config/mappings.cfm';
include local.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 eventHandler.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);
};
// Bean Factory (uses DI/1)
// Be sure to pass in your comma-separated list of folders to scan for CFCs
local.beanFactory = new includes.framework.ioc('/#variables.framework.package#/myapp/model');
local.beanFactory.addBean('fw', this);
// optionally set Mura to be the parent beanFactory
local.parentBeanFactory = application.serviceFactory;
local.beanFactory.setParent(local.parentBeanFactory);
setBeanFactory(local.beanFactory);
}
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);
}
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, '$');
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';
}
public string function buildURL(required string action, string path='#resolvePath()#', 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='#resolvePath()#', string queryString='', string statusCode='302') {
return super.redirect(argumentCollection=arguments);
}
public any function resolvePath(string path='#variables.framework.baseURL#') {
// don't modify a submitted path
if ( arguments.path != variables.framework.baseURL ) {
return arguments.path;
}
var uri = getPageContext().getRequest().getRequestURI();
var arrURI = ListToArray(uri, '/');
var indexPos = ArrayFind(arrURI, 'index.cfm');
var useIndex = YesNoFormat(application.configBean.getValue('indexfileinurls'));
var useSiteID = YesNoFormat(application.configBean.getValue('siteidinurls'));
if ( !useIndex && indexPos ) {
ArrayDeleteAt(arrURI, indexPos);
uri = ArrayLen(arrURI)
? '/' & ArrayToList(arrURI, '/') & '/'
: '/';
}
return uri;
}
public any function isFrameworkInitialized() {
return super.isFrameworkInitialized() && StructKeyExists(getFw1App(), 'cache');
}
// ========================== Errors & Missing Views ==========================
public any function onError( e ) 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>');
WriteDump(e);
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 = {}
};
};
}
}