Home directory for Malawi's wwwroot
Duncan Ewan
2021-02-19 3e758c29e0fde36fc088efcfc88f9a3014432b64
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
<cfscript>
 
/*
 * CheckCache: Checks the cache for our value, and the timeout stamp. If the value
 * exists, and has not timed out, we return true. If the value exists and is timed
 * out, we kill the old value and return false. If the value doesn't exist, we return
 * false. We also do a check to make sure server.CACHE_ROOT exist
 *
 * WARNING!! This UDF reads/writes to server scope w/o locking. I am assuming we will
 * be able to lock soon.
 *
 * @param cacheName        The name of the cache to check for
 * @return                Returns true or false
*/
function checkCache( cacheName ) {
 
     val = "";
    if(NOT CACHE_ENABLED) return false;
     if(NOT structKeyExists(server,CACHE_ROOT)) {
        server[CACHE_ROOT] = structNew();
        return false;
    }
 
    if(structKeyExists(server[CACHE_ROOT],cacheName)) {
        val = server[CACHE_ROOT][cacheName];
        if(DateCompare(Now(), val.dtTimeOut) IS 1) {
            structDelete(server[CACHE_ROOT],cacheName);
            return false;
        } else return true;
    }
    return false;
}
 
/*
 * GetCache - Returns the value for a cached item
 *
 * WARNING!! This UDF reads/writes to server scope w/o locking. I am assuming we will
 * be able to lock soon.
 *
 * @param cacheName        The name of the cache to check for
 * @return                Returns the value
*/
function getCache( cacheName ) {
 
     val = "";
    if(NOT CACHE_ENABLED) return false;
     if(NOT structKeyExists(server,CACHE_ROOT)) {
        server[CACHE_ROOT] = structNew();
    }
    if(structKeyExists(server[CACHE_ROOT],cacheName)) {
        val = server[CACHE_ROOT][cacheName];
        return duplicate(val.content);
    }
    return false;
}
 
/*
 * SetCache - Sets a value in the cache
 *
 * WARNING!! This UDF reads/writes to server scope w/o locking. I am assuming we will
 * be able to lock soon.
 *
 * @param cacheName        The name of the cache to check for
 * @param val            The value to cache
 * @return                Returns true
*/
function setCache( cacheName, val ) {
 
     cache = structNew();
    if(NOT CACHE_ENABLED) return false;
     if(NOT structKeyExists(server,CACHE_ROOT)) {
        server[CACHE_ROOT] = structNew();
    }
    cache.dtTimeout = DateAdd("n",CACHE_TIMEOUT, Now());
    cache.content = val;
    server[CACHE_ROOT][cacheName] = duplicate(cache);
    return true;
}
 
/*
 * NormalizePath - returns path with all forward slashes
 * without a trailing slash
 *
 * @param path     physical path
 * @return        physical path with all forward slashes and without trailing slash
*/
function NormalizePath( path ) {
    path = Replace( path, '\', '/', 'ALL' ) ;
    if ( Right(path,1) eq '/' )
        return RemoveChars( path, Len(path), 1 ) ;
    else
        return path ;
}
 
/*
 * browseForComponents - fills component struct in the caller with
 * component info for components found under specified dir
 *
 * @param package        package name of the current directory
 * @param dir            java file object of the current directory
 * @param cfcRoot        root where we started search - used as a key in component struct
 * @param ignoreShadowedPackages    if true, all packages that match already browsed packages
 *                        are ignored
 * @return                void
*/
function browseForComponents ( package, dir, cfcRoot, ignoreShadowedPackages ) {
 
    var i = 1;
    var file = "";
    var name ="";
    var isShadowed = false;
    var componentName = "";
    var packagePrefix = '' ;
    var fileNames = "" ;
    var dirPath = "" ;
    var isAuthorized = true;
    factory = CreateObject("java", "coldfusion.server.ServiceFactory");
    security = factory.getSecurityService();
 
    isShadowed = ignoreShadowedPackages and StructKeyExists( packages, package ) ;
 
    // is this a directory and is it not shadowed?
    if ( dir.exists() and not isShadowed ) {
 
        packagePrefix = package ;
        if ( packagePrefix neq '' )
            packagePrefix = packagePrefix & "." ;
 
        dirPath = dir.getPath() & dir.separator ;
 
        // add it to packages
        packages[package] = '' ;
 
        // get directoryContent, to optimize the lookup do it
        // thru names
        fileNames = dir.list() ;
 
        for ( i = 1; isDefined("fileNames") and i lte ArrayLen(fileNames); i=i+1 ) {
 
            name = fileNames[i] ;
 
            if ( Find( '.', name ) eq 0 ) {
                // instantiate as java File and check whether it is
                // a directory
                file = CreateObject( "java", "java.io.File" ) ;
                file.init( dirPath & name ) ;
                if(security.isSandboxSecurityEnabled())
                {
                    isAuthorized = hasAccessToFolder( dirPath & name );
                }
                if(isAuthorized)
                {
                    if ( file.isDirectory() )
                    {
                        browseForComponents ( packagePrefix & name,
                        file, cfcRoot, ignoreShadowedPackages ) ;
                    }
                }
 
            } else if ( ListLast( name, '.' ) eq 'cfc' ) {
                // instantiate as java File and check that it is
                // a file
                file = CreateObject( "java", "java.io.File" ) ;
                file.init( dirPath & name ) ;
 
                if ( file.isFile() ) {
                    componentName = StructNew() ;
                    componentName.name = packagePrefix & ListFirst( name, '.' ) ;
                    componentName.path = Replace( file.getPath(), '\', '/', 'ALL' ) ;
                    componentName.package = package ;
                    componentName.cfcroot = cfcroot ;
                    components[componentName.name] = componentName ;
                }
            }
        }
 
    }
}
 
/*
 * hasAccessToFolder -  returns true if the current logged in user has access to the specific file.
 *
 * @param filename        name of the file
 * @return                boolean
*/
function hasAccessToFolder(filename)
{
    var fileObj = CreateObject("java", "java.io.File").init(javacast("string", filename));
    var userid = getCurrentUser();
    factory = CreateObject("java", "coldfusion.server.ServiceFactory");
    security = factory.getSecurityService();
    if(security.getRootAdminUserId() eq userid)
    {
        return true;
    }
    else
    {
        authuser = security.getAuthorizedUser(userid);
        return authuser.getAuthorizedFolders().isAuthorized(fileObj);
    }
}
 
/*
 * getCurrentUser -  returns the id of the current logged in user
 *
 * @return    string
*/
 
function getCurrentUser()
{
    userutils = createObject("java", "coldfusion.security.UserUtils");
    userid = userutils.getAuthUser();
    return userid;
 
}
</cfscript>