-
Notifications
You must be signed in to change notification settings - Fork 35
/
Application.cfc
288 lines (251 loc) · 10.7 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
component extends = "framework.one" {
// ------------------------ CONSTANTS ------------------------ //
variables.HTTP_STATUS_CODES = {
NOT_FOUND = 404
};
// ------------------------ APPLICATION SETTINGS ------------------------ //
this.applicationRoot = getDirectoryFromPath(getCurrentTemplatePath());
this.name = Lcase(ReReplace(this.applicationroot, "[\W]", "", "all"));
this.sessionManagement = true;
// note: IsLocalHost on CF returns YES|NO which can't be passed to hibernate
this.development = IsLocalHost(CGI.REMOTE_ADDR) ? true : false;
// prevent bots creating lots of sessions
if (structKeyExists(cookie, "CFTOKEN")) {
this.sessionTimeout = createTimeSpan(0, 0, 20, 0);
} else {
this.sessionTimeout = createTimeSpan(0, 0, 0, 1);
}
this.mappings["/hoth"] = this.applicationRoot & "framework/hoth/";
this.mappings["/model"] = this.applicationRoot & "model/";
this.mappings["/ValidateThis"] = this.applicationRoot & "framework/ValidateThis/ValidateThis/";
this.datasource = ListLast(this.applicationRoot, "\/");
this.ormEnabled = true;
this.ormSettings = {
flushAtRequestEnd = false
, autoManageSession = false
, cfcLocation = this.mappings["/model"]
, eventHandling = true
, eventHandler = "model.aop.GlobalEventHandler"
, logSql = this.development
, secondaryCacheEnabled = server.coldfusion.productName != "Lucee" ? true : false // throws an error if enabled on Lucee
};
// create database and populate when the application starts in development environment
// you might want to comment out this code after the initial install
if (this.development && !isNull(url.rebuild)) {
this.ormSettings.dbCreate = "dropcreate";
this.ormSettings.sqlScript = "_install/setup.sql";
}
// ------------------------ FW/1 SETTINGS ------------------------ //
variables.framework = {
cacheFileExists = !this.development
, defaultSubsystem = "public"
, generateSES = true
, maxNumContextsPreserved = 1
, password = ""
, reloadApplicationOnEveryRequest = this.development
, usingSubsystems = true
, SESOmitIndex = false /* set this to true if you have url rewrite enabled */
, subsystemDelimiter = ":" // see note below
, routes = [
{"news" = "news", hint = "Redirect to news feature"}
, {"enquiry" = "enquiry", hint = "Redirect to enquiry feature"}
]
, trace = false
, diLocations = "public/controllers"
};
/*
There is a known issue with Apache on a Windows server and colons, if you have this issue, change the subsystemDelimiter setting to something like "~"
You will also need to update admin/Application.cfc
*/
// ------------------------ CALLED WHEN APPLICATION STARTS ------------------------ //
void function setupApplication() {
// add exception tracker to application scope
local.HothConfig = new hoth.config.HothConfig();
local.HothConfig.setApplicationName(getConfiguration().name);
local.HothConfig.setLogPath(this.applicationRoot & "logs/hoth");
local.HothConfig.setLogPathIsRelative(false);
local.HothConfig.setEmailNewExceptions(getConfiguration().exceptionTracker.emailNewExceptions);
local.HothConfig.setEmailNewExceptionsTo(getConfiguration().exceptionTracker.emailNewExceptionsTo);
local.HothConfig.setEmailNewExceptionsFrom(getConfiguration().exceptionTracker.emailNewExceptionsFrom);
local.HothConfig.setEmailExceptionsAsHTML(getConfiguration().exceptionTracker.emailExceptionsAsHtml);
application.exceptionTracker = new Hoth.HothTracker(local.HothConfig);
// setup bean factory
local.beanFactory = new framework.ioc("/model", {singletonPattern = "(Service|Gateway)$"});
setBeanFactory(local.beanFactory);
// add validator bean to factory
local.validatorConfig = {
definitionPath = "/model/",
JSIncludes = false,
resultPath = "model.utility.ValidatorResult"
};
local.beanFactory.addBean("Validator", new ValidateThis.ValidateThis(local.validatorConfig));
// add meta data bean to factory
local.beanFactory.addBean("MetaData", new model.content.MetaData());
// add config bean to factory
local.beanFactory.addBean("config", getConfiguration());
}
// ------------------------ CALLED WHEN PAGE REQUEST STARTS ------------------------ //
void function before(required rc) {
if (this.development && !isNull(url.rebuild)) {
ORMReload();
}
if (this.development) {
writedump(var = "*** Request Start - #TimeFormat(Now(), 'full')# ***", output = "console");
}
// define base url
rc.basehref = CGI.HTTPS eq "on" ? "https://" : "http://";
rc.basehref &= CGI.HTTP_HOST & variables.framework.base;
// define default meta data
rc.MetaData = getBeanFactory().getBean("MetaData");
// store config in request context
rc.config = getBeanFactory().getBean("Config");
}
// ------------------------ CALLED WHEN VIEW RENDERING STARTS ------------------------ //
void function setupView(required rc) {
// get data needed to build the navigation
if (getSubsystem() == "public") {
rc.navigation = getBeanFactory().getBean("ContentService").getNavigation();
}
}
// ------------------------ CALLED WHEN EXCEPTION OCCURS ------------------------ //
void function onError(Exception, event) {
if (StructKeyExists(application, "exceptionTracker")) {
application.exceptionTracker.track(arguments.Exception);
}
super.onError(arguments.Exception, arguments.event);
}
// ------------------------ CALLED WHEN VIEW IS MISSING ------------------------ //
any function onMissingView(required rc) {
// Note: IIS and Apache report the CGI.PATH_INFO differently
local.slug = LCase(ReReplace(Replace(CGI.PATH_INFO, CGI.SCRIPT_NAME, ""), "^/", "", "one"));
if (local.slug == "") {
local.slug = rc.config.page.defaultSlug; // set default
}
rc.Page = getBeanFactory().getBean("ContentService").getPageBySlug(slug = local.slug);
if (!rc.Page.isPersisted()) {
local.pageContext = getPageContext().getResponse();
local.pageContext.setStatus(variables.HTTP_STATUS_CODES.NOT_FOUND);
rc.MetaData.setMetaTitle("Page Not Found");
return view("public#variables.framework.subsystemDelimiter#main/notfound");
} else {
rc.breadcrumbs = getBeanFactory().getBean("ContentService").getNavigationPath(pageId = rc.Page.getPageId());
rc.MetaData.setMetaTitle(rc.Page.getMetaTitle());
rc.MetaData.setMetaDescription(rc.Page.getMetaDescription());
rc.MetaData.setMetaKeywords(rc.Page.getMetaKeywords());
return view("public#variables.framework.subsystemDelimiter#main/missingview");
}
}
// ------------------------ CONFIGURATION ------------------------ //
private struct function getConfiguration() {
local.config = {
development = this.development
, enquiry = {
enabled = true
, subject = "Enquiry"
, emailto = ""
}
, exceptionTracker = {
emailNewExceptions = true
, emailNewExceptionsTo = ""
, emailNewExceptionsFrom = ""
, emailExceptionsAsHtml = true
}
, fileManager = {
allowedExtensions = "txt,gif,jpg,png,wav,mpeg3,pdf,zip,mp3,jpeg"
}
, googleAnalyticsTrackingId = ""
, name = ""
, news = {
enabled = true
, recordsPerPage = 10
, rssTitle = ""
, rssDescription = ""
}
, page = {
enableAddDelete = true
, maxLevels = 2 // number of page tiers that can be added - Bootstrap dropdown supports a maximum of 2
, suppressAddPage = "" // comma delimited list of page ids for pages that cannot have child pages added
, suppressDeletePage = "1" // comma delimited list of page ids for pages that cannot be deleted
, defaultSlug = "home" // default 'slug' to use to get homepage
}
, revision = Hash(Now())
, security = {
resetPasswordEmailFrom = ""
, resetPasswordEmailSubject = ""
, whitelist = "^admin#variables.framework.subsystemDelimiter#security,^public#variables.framework.subsystemDelimiter#" // list of unsecure actions - by default all requests require authentication
}
, version = "2016.6.9"
};
// override config in development mode
if (local.config.development) {
local.config.enquiry.emailTo = "";
local.config.exceptionTracker.emailNewExceptions = false;
local.config.security.resetPasswordEmailFrom = "";
}
return local.config;
}
// ------------------------ FRAMEWORK HELPERS ------------------------ //
/**
* I override the FW/1 buildURL method to clean up the homepage URLs *
**/
public string function buildURL(string action = ".", string path = variables.magicBaseURL, any queryString = "") {
local.theUrl = super.buildURL(arguments.action, arguments.path, arguments.queryString);
return ReReplace(local.theUrl, "index\.cfm/#getBeanFactory().getBean("Config").page.defaultSlug#$", "");
}
// ------------------------ VIEW HELPERS ------------------------ //
string function snippet(required string content, numeric characterCount = 100) {
local.result = Trim(reReplace(arguments.content, "<[^>]{1,}>", " ", "all"));
if (Len(local.result) > arguments.characterCount + 10) {
return "<p>" & Trim(Left(local.result, arguments.characterCount)) & "…</p>";
} else {
return local.result;
}
}
string function getTimeInterval(required date theDate, string dateMask = "dddd dd mmmm yyyy") {
local.result = "";
if (IsDate(arguments.theDate)) {
local.timeInSeconds = DateDiff("s", arguments.theDate, Now());
// less than a minute
if (local.timeInSeconds < 60) {
local.result = " less than a minute ago";
}
// less than an hour
else if (local.timeInSeconds < 3600) {
local.interval = Int(local.timeInSeconds / 60);
// more than 1 minute
if (local.interval > 1) {
local.result = local.interval & " minutes ago";
} else {
local.result = local.interval & " minute ago";
}
}
// less than 24 hours
else if (local.timeInSeconds < (86400) && Hour(Now()) >= Hour(arguments.theDate)) {
local.interval = Int(local.timeInSeconds / 3600);
// more than 1 hour
if (local.interval > 1) {
local.result = local.interval & " hours ago";
} else {
local.result = local.interval & " hour ago";
}
}
// less than 48 hours
else if (local.timeInSeconds < 172800) {
local.result = "yesterday" & " at " & TimeFormat(arguments.theDate, "HH:MM");
}
// return the date
else {
local.result = DateFormat(arguments.theDate, arguments.dateMask) & " at " & TimeFormat(arguments.theDate, "HH:MM");
}
}
return local.result;
}
boolean function isRoute(required string slug) {
for (local.el in getRoutes()) {
if (StructKeyExists(local.el, arguments.slug)) {
return true;
}
}
return false;
}
}