This repository was archived by the owner on Apr 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cfm
More file actions
244 lines (223 loc) · 6.5 KB
/
functions.cfm
File metadata and controls
244 lines (223 loc) · 6.5 KB
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
<cfscript>
// Place functions here that should be available globally in your application.
public function GetSignedInUserId(){
return structKeyExists(session, "userID") ? session.userID : 0
}
/**
* Convert a local datetime to UTC using server's timezone offset
* @localTime The datetime to convert to UTC
* @return The datetime in UTC
*/
public datetime function toUTC(required datetime localTime) {
var tzInfo = GetTimeZoneInfo();
var offsetSeconds = tzInfo.utcTotalOffset * 60;
return dateAdd("s", -offsetSeconds, arguments.localTime);
}
/**
* Safely convert a datetime string from a specific timezone to UTC
* @dateTimeStr The datetime string to convert (expected to be ISO format from JavaScript)
* @timeZone The timezone identifier (e.g., "America/New_York") - kept for compatibility but not used since JS sends UTC
* @return The datetime in UTC
*/
public datetime function toSafeUTC(required string dateTimeStr, string timeZone="") {
try {
// Since JavaScript sends ISO string (already in UTC), just parse it
if (len(trim(arguments.dateTimeStr))) {
return parseDateTime(arguments.dateTimeStr);
} else {
return toUTC(now());
}
} catch (any e) {
// Fallback: return current UTC time
return toUTC(now());
}
}
public function GetUserRoleId(){
return 3;
}
public function SetActive(){
return 1;
}
public function SetInactive(){
return 0;
}
public function Published(){
return 1;
}
/**
* Sort by Semvar
*/
array function sortBySemVar(required array versions){
sv=createObject("component", "app.lib.semver.models.SemanticVersion");
return arguments.versions.sort( function( a, b ) { return sv.compare( b, a ) } );
}
/**
* Get a list of available documentation versions
*/
array function getAvailableVersions(string path=getDocJSONPathExpanded()){
local.rv=[];
local.versionFiles=directoryList(arguments.path, false, "name", "*.json");
for(local.v in local.versionFiles){
arrayAppend(local.rv, getVersionFromFileName(local.v));
}
local.rv=sortBySemVar(local.rv);
return local.rv;
}
/**
* Get Path to JSON files
*/
string function getDocJSONPath(){
local.rv=get("webPath") & "json/";
return local.rv;
}
/**
* Get JSON Doc
*/
struct function getDocJSON(required string version){
local.path = getDocJSONPathExpanded() & arguments.version & ".json";
if(fileExists(local.path)){
return deserializeJSON(fileRead(path));
} else {
return {};
}
}
/**
* Get Absolute Path to JSON files
*/
string function getDocJSONPathExpanded(){
return expandPath(getDocJSONPath());
}
/**
* Gets Version from Filename
*/
string function getVersionFromFileName(required string versionString){
local.rv=right(arguments.versionString, len(arguments.versionString) -1) ;
local.rv=replaceNoCase(local.rv, ".json","", "one");
return local.rv;
}
/**
* Searches JSON array for single function
*/
struct function getFunctionFromDocs(required struct docs, required string functionSlug){
local.match=arrayFind(docs.functions, function(struct){
return (struct.slug == functionSlug);
});
if(!local.match){
// Try for non-slugged version: assume controller + model
local.match=arrayFind(docs.functions, function(struct){
return (struct.slug == "controller." & functionSlug);
});
}
if(!local.match){
// Try for non-slugged version: assume controller + model
local.match=arrayFind(docs.functions, function(struct){
return (struct.slug == "model." & functionSlug);
});
}
if(local.match){
return docs.functions[local.match];
} else {
return {};
}
}
any function getRelatedFunctionsBySection(required struct docs, required string section){
local.match=arrayFilter(docs.functions, function(struct){
return (struct.tags.section == section);
});
return local.match;
}
any function getRelatedFunctionsByCategory(required struct docs, required string category){
local.match=arrayFilter(docs.functions, function(struct){
return (struct.tags.category == category);
});
return local.match;
}
any function getRelatedFunctionsBySectionAndCategory(
required struct docs,
required string section,
required string category
){
local.match=arrayFilter(docs.functions, function(struct){
return (struct.tags.section == section && struct.tags.category == category);
});
return local.match;
}
/**
* Fetch current session user with Role, or return `false` if not valid
*/
private any function getCurrentUserWithRole() {
if (!structKeyExists(session, "userID") || session.userID == 0) {
return false;
}
var user = model("User").findByKey(key=session.userID, include="Role");
if (!isObject(user) || !structKeyExists(user, "Role")) {
return false;
}
return user;
}
/**
* Check if a user can comment based on their role
*/
boolean function canUserComment() {
var user = getCurrentUserWithRole();
if (!isObject(user)) {
return false;
}
var allowedRoles = ["admin", "editor", "commenter"];
return arrayContains(allowedRoles, lcase(user.Role.name));
}
/**
* Check if the current session user is admin
*/
boolean function isUserAdmin() {
var user = getCurrentUserWithRole();
if (!isObject(user)) {
return false;
}
return lcase(user.Role.name) == "admin";
}
/**
* Check if the current session user has editor or admin role
*/
boolean function hasEditorAccess() {
var user = getCurrentUserWithRole();
if (!isObject(user)) {
return false;
}
var roleName = lcase(user.Role.name);
return roleName == "editor" || roleName == "admin";
}
/**
* Obfuscate an ID for security (encode)
*/
string function obfuscateId(required numeric id) {
var salt = getSaltFromEnvironment();
var encoded = hash(arguments.id & salt, "SHA-256");
return left(encoded, 16); // Return first 16 characters for shorter URLs
}
/**
* Deobfuscate an ID (decode)
*/
numeric function deobfuscateId(required string obfuscatedId) {
var salt = getSaltFromEnvironment();
var users = model("User").findAll();
for (var i = 1; i <= users.recordCount; i++) {
var encoded = hash(users.id[i] & salt, "SHA-256");
var checkId = left(encoded, 16);
if (checkId == arguments.obfuscatedId) {
return users.id[i];
}
}
return 0; // Return 0 if not found
}
/**
* Get salt from environment variable with fallback
*/
private string function getSaltFromEnvironment() {
// Try to get from application.env first (from .env file)
if (structKeyExists(application, "env") && structKeyExists(application.env, "wheels_id_salt")) {
return application.env.wheels_id_salt;
}
throw(type="ConfigurationError", message="wheels_id_salt environment variable is not configured");
}
</cfscript>