-
Notifications
You must be signed in to change notification settings - Fork 2
/
Application.cfc
314 lines (258 loc) · 12.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
/*
This file is part of eManager
Copyright 2010-2013 @CFMitrah from @MitrahSoft.
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.fw1" {
include 'includes/fw1config.cfm'; // framework variables
include '../../config/applicationSettings.cfm';
include '../../config/mappings.cfm';
include '../mappings.cfm';
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 = {};
local.targetPath = getPageContext().getRequest().getRequestURI();
onApplicationStart();
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 = 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();
setCachedView(local.viewKey, local.response);
};
return local.response;
}
public any function setupApplication() {
var local = {};
if ( !StructKeyExists(application, 'pluginManager') ) {
location(url='/', addtoken=false);
};
lock scope='application' type='exclusive' timeout=50 {
application[variables.framework.applicationKey].pluginConfig = application.pluginManager.getConfig(ID=variables.framework.applicationKey);
};
// Bean Factory Options
// 1) Use DI/1
// just be sure to pass in your comma-separated list of folders to scan for CFCs
//local.beanFactory = new includes.factory.ioc('/#variables.framework.package#/app2/services,/#variables.framework.package#/app3/model');
//setBeanFactory( local.beanFactory );
// OR
// 2) Use Mura's
local.pc = application[variables.framework.applicationKey].pluginConfig;
setBeanFactory(local.pc.getApplication(purge=false));
siteid = application.serviceFactory.getBean('contentserver').bindToDomain();
if ( !StructKeyExists(request.context, '$') ) {
request.context.$ = StructKeyExists(request, 'muraScope') ? request.muraScope : application.serviceFactory.getBean('muraScope').init(siteid);
};
muradsn = request.context.$.globalConfig().getValue( "datasource" );
muradbType = request.context.$.globalConfig().getValue( "dbtype" );
Application.locationsDAO = createobject("component","model.DAO.locationsDAO").init(muradsn, muradbType);
Application.settingsDAO = createobject("component","model.DAO.settingsDAO").init(muradsn, muradbType);
Application.termDAO = createobject("component","model.DAO.termDAO").init(muradsn, muradbType);
Application.organizersDAO = createobject("component","model.DAO.organizersDAO").init(muradsn, muradbType);
Application.eventsDAO = createobject("component","model.DAO.eventsDAO").init(muradsn, muradbType);
}
public void function setupRequest() {
var local = {};
param name='request.context.siteid' default='';
Application.COUNTRIES = ["Afghanistan","Albania","Algeria","Andorra","Angola","Antigua & Barbuda","Argentina","Armenia","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bhutan","Bolivia","Bosnia & Herzegovina","Botswana","Brazil","Brunei","Bulgaria","Burkina Faso","Burundi","Cambodia","Cameroon","Canada","Cape Verde","Central African Republic","Chad","Chile","China","Colombia","Comoros","Congo","Congo Democratic Republic of","Costa Rica","Cote d'Ivoire","Croatia","Cuba","Cyprus","Czech Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","East Timor","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Ethiopia","Fiji","Finland","France","Gabon","Gambia","Georgia","Germany","Ghana","Greece","Grenada","Guatemala","Guinea","Guinea-Bissau","Guyana","Haiti","Honduras","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Israel","Italy","Jamaica","Japan","Jordan","Kazakhstan","Kenya","Kiribati","Korea North","Korea South","Kosovo","Kuwait","Kyrgyzstan","Laos","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Mauritania","Mauritius","Mexico","Micronesia","Moldova","Monaco","Mongolia","Montenegro","Morocco","Mozambique","Myanmar (Burma)","Namibia","Nauru","Nepal","The Netherlands","New Zealand","Nicaragua","Niger","Nigeria","Norway","Oman","Pakistan","Palau","Palestinian State","Panama","Papua New Guinea","Paraguay","Peru","The Philippines","Poland","Portugal","Qatar","Romania","Russia","Rwanda","St. Kitts & Nevis","St. Lucia","St. Vincent & The Grenadines","Samoa","San Marino","Sao Tome & Principe","Saudi Arabia","Senegal","Serbia","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Sudan","Spain","Sri Lanka","Sudan","Suriname","Swaziland","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Togo","Tonga","Trinidad & Tobago","Tunisia","Turkey","Turkmenistan","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","United States of America","Uruguay","Uzbekistan","Vanuatu","Vatican City (Holy See)","Venezuela","Vietnam","Yemen","Zambia","Zimbabwe"];
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();
};
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 = application[variables.framework.applicationKey].pluginConfig;
request.context.pluginConfig = application[variables.framework.applicationKey].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 string function buildURL(required string action, string path='#variables.framework.baseURL#', string 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) ) {
arguments.queryString = ListAppend(arguments.queryString, qs, '&');
} else {
arguments.action = ListAppend(arguments.action, qs, '&');
};
};
return super.buildURL(argumentCollection=arguments);
}
// ========================== 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;
}
public any function onMissingView(any rc) {
rc.errors = [];
rc.isMissingView = true;
// forward to appropriate error screen
if ( isFrontEndRequest() ) {
ArrayAppend(rc.errors, "The page you're looking for doesn't exist.");
redirect(action='public:main.error', preserve='errors,isMissingView');
} else {
ArrayAppend(rc.errors, "The page you're looking for <strong>#rc.action#</strong> doesn't exist.");
redirect(action='admin:main', preserve='errors,isMissingView');
};
}
// ========================== 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(application[variables.framework.applicationKey].pluginConfig.getModuleID(), session.siteid)
? goToLogin() : true;
}
private void function goToLogin() {
location(url='#application.configBean.getContext()#/admin/index.cfm?muraAction=clogin.main&returnURL=/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
, cgiRequestMethod = CGI.REQUEST_METHOD
, controllers = []
, requestDefaultsInitialized = false
, services = []
, 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 = {}
};
};
}
}