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
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
<cfsetting enablecfoutputonly="Yes">
<!---
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2009 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * This file include IO specific functions used by the ColdFusion Connector (MX 6.0 and above).
 *
--->
 
<cffunction name="CombinePaths" returntype="String" output="true">
    <cfargument name="sBasePath" required="true">
    <cfargument name="sFolder" required="true">
    <cfset sBasePath = RemoveFromEnd( sBasePath, "/" )>
    <cfset sBasePath = RemoveFromEnd( sBasePath, "\" )>
    <cfreturn sBasePath & "/" & RemoveFromStart( ARGUMENTS.sFolder, '/' )>
</cffunction>
 
<cffunction name="GetResourceTypePath" returntype="String" output="false">
    <cfargument name="resourceType" required="true">
    <cfargument name="sCommand" required="true">
 
    <cfif ARGUMENTS.sCommand eq "QuickUpload">
        <cfreturn REQUEST.Config['QuickUploadPath'][ARGUMENTS.resourceType]>
    <cfelse>
        <cfreturn REQUEST.Config['FileTypesPath'][ARGUMENTS.resourceType]>
    </cfif>
</cffunction>
 
<cffunction name="GetResourceTypeDirectory" returntype="String" output="false">
    <cfargument name="resourceType" required="true">
    <cfargument name="sCommand" required="true">
 
    <cfif ARGUMENTS.sCommand eq "QuickUpload">
        <cfif isDefined( "REQUEST.Config.QuickUploadAbsolutePath" )
            and structkeyexists( REQUEST.Config.QuickUploadAbsolutePath, ARGUMENTS.resourceType )
            and Len( REQUEST.Config.QuickUploadAbsolutePath[ARGUMENTS.resourceType] )>
                <cfreturn REQUEST.Config.QuickUploadAbsolutePath[ARGUMENTS.resourceType]>
        </cfif>
 
        <cfreturn expandpath( REQUEST.Config.QuickUploadPath[ARGUMENTS.resourceType] )>
    <cfelse>
        <cfif isDefined( "REQUEST.Config.FileTypesAbsolutePath" )
            and structkeyexists( REQUEST.Config.FileTypesAbsolutePath, ARGUMENTS.resourceType )
            and Len( REQUEST.Config.FileTypesAbsolutePath[ARGUMENTS.resourceType] )>
                <cfreturn REQUEST.Config.FileTypesAbsolutePath[ARGUMENTS.resourceType]>
        </cfif>
 
        <cfreturn expandpath( REQUEST.Config.FileTypesPath[ARGUMENTS.resourceType] )>
    </cfif>
</cffunction>
 
<cffunction name="GetUrlFromPath" returntype="String" output="false">
    <cfargument name="resourceType" required="true">
    <cfargument name="folderPath" required="true">
    <cfargument name="sCommand" required="true">
 
    <cfreturn CombinePaths( GetResourceTypePath( ARGUMENTS.resourceType, ARGUMENTS.sCommand ), ARGUMENTS.folderPath )>
</cffunction>
 
<cffunction name="RemoveExtension" output="false" returntype="String">
    <cfargument name="fileName" required="true">
    <cfset var pos = find( ".", reverse ( ARGUMENTS.fileName ) )>
 
    <cfreturn mid( ARGUMENTS.fileName, 1, Len( ARGUMENTS.fileName ) - pos ) >
</cffunction>
 
<cffunction name="GetExtension" output="false" returntype="String">
    <cfargument name="fileName" required="true">
    <cfset var pos = find( ".", reverse ( ARGUMENTS.fileName ) )>
 
    <cfif not pos>
        <cfreturn "">
    </cfif>
 
    <cfreturn mid( ARGUMENTS.fileName, pos, Len( ARGUMENTS.fileName ) - pos ) >
</cffunction>
 
<cffunction name="ServerMapFolder" returntype="String" output="false">
    <cfargument name="resourceType" required="true">
    <cfargument name="folderPath" required="true">
    <cfargument name="sCommand" required="true">
 
    <!--- Get the resource type directory. --->
    <cfset var sResourceTypePath = GetResourceTypeDirectory( ARGUMENTS.resourceType, ARGUMENTS.sCommand ) >
    <!--- Ensure that the directory exists. --->
    <cfset var sErrorMsg = CreateServerFolder( sResourceTypePath ) >
 
    <cfif sErrorMsg neq ''>
        <cfset SendError( 1, 'Error creating folder "' & sResourceTypePath & '" (' & sErrorMsg & ')' )>
    </cfif>
 
    <!--- Return the resource type directory combined with the required path. --->
    <cfreturn CombinePaths( sResourceTypePath , ARGUMENTS.folderPath )>
</cffunction>
 
<cffunction name="GetParentFolder" returntype="string" output="false">
    <cfargument name="folderPath" required="true">
 
    <cfreturn rereplace(ARGUMENTS.folderPath, "[/\\\\][^/\\\\]+[/\\\\]?$", "")>
</cffunction>
 
<cffunction name="CreateServerFolder" returntype="String" output="false">
    <cfargument name="folderPath">
 
    <!--- Ensure the folder path has no double-slashes, or mkdir may fail on certain platforms --->
    <cfset folderPath = rereplace(ARGUMENTS.folderPath, "//+", "/", "all")>
 
    <cfif directoryexists(ARGUMENTS.folderPath) or fileexists(ARGUMENTS.folderPath)>
        <cfreturn "">
    <cfelse>
        <cftry>
            <cfdirectory action="create" mode="0755" directory="#ARGUMENTS.folderPath#">
        <cfcatch type="any">
            <cfreturn CFCATCH.Message>
        </cfcatch>
        </cftry>
    </cfif>
 
    <cfreturn "">
</cffunction>
 
<cffunction name="IsAllowedExt" returntype="boolean" output="false">
    <cfargument name="sExtension" required="true">
    <cfargument name="resourceType" required="true">
 
    <cfif isDefined( "REQUEST.Config.AllowedExtensions." & ARGUMENTS.resourceType )
            and listLen( REQUEST.Config.AllowedExtensions[ARGUMENTS.resourceType] )
            and not listFindNoCase( REQUEST.Config.AllowedExtensions[ARGUMENTS.resourceType], ARGUMENTS.sExtension )>
            <cfreturn false>
    </cfif>
 
    <cfif isDefined( "REQUEST.Config.DeniedExtensions." & ARGUMENTS.resourceType )
            and listLen( REQUEST.Config.DeniedExtensions[ARGUMENTS.resourceType] )
            and listFindNoCase( REQUEST.Config.DeniedExtensions[ARGUMENTS.resourceType], ARGUMENTS.sExtension )>
            <cfreturn false>
    </cfif>
 
    <cfreturn true>
</cffunction>
 
<cffunction name="IsAllowedType" returntype="boolean" output="false">
    <cfargument name="resourceType">
 
    <cfif not listFindNoCase( REQUEST.Config.ConfigAllowedTypes, ARGUMENTS.resourceType )>
        <cfreturn false>
    </cfif>
 
    <cfreturn true>
</cffunction>
 
<cffunction name="IsAllowedCommand" returntype="boolean" output="true">
    <cfargument name="sCommand" required="true" type="String">
 
    <cfif not listFindNoCase( REQUEST.Config.ConfigAllowedCommands, ARGUMENTS.sCommand )>
        <cfreturn false>
    </cfif>
 
    <cfreturn true>
</cffunction>
 
<cffunction name="GetCurrentFolder" returntype="String" output="true">
    <cfset var sCurrentFolder = "/">
 
    <cfif isDefined( "URL.CurrentFolder" )>
        <cfset sCurrentFolder = URL.CurrentFolder>
    </cfif>
 
    <!--- Check the current folder syntax (must begin and start with a slash). --->
    <cfif not refind( "/$", sCurrentFolder)>
        <cfset sCurrentFolder = sCurrentFolder & "/">
    </cfif>
 
    <cfif not refind( "^/", sCurrentFolder )>
        <cfset sCurrentFolder = "/" & sCurrentFolder>
    </cfif>
 
    <!--- Ensure the folder path has no double-slashes, or mkdir may fail on certain platforms --->
    <cfset sCurrentFolder = rereplace( sCurrentFolder, "//+", "/", "all" )>
 
    <cfif find( "..", sCurrentFolder) or find( "\", sCurrentFolder) or REFind('(/\.)|(//)|[[:cntrl:]]|([\\:\*\?\"<>])', sCurrentFolder)>
        <cfif URL.Command eq "FileUpload" or URL.Command eq "QuickUpload">
            <cfset SendUploadResults( 102, "", "", "") >
        <cfelse>
            <cfset SendError( 102, "" )>
        </cfif>
    </cfif>
 
    <cfreturn sCurrentFolder>
</cffunction>
 
<cffunction name="SanitizeFolderName" returntype="String" output="false">
    <cfargument name="sNewFolderName" required="true">
 
    <!--- Do a cleanup of the folder name to avoid possible problems --->
    <!--- Remove . \ / | : ? * " < > and control characters --->
    <cfset sNewFolderName = rereplace( sNewFolderName, '\.+|\\+|\/+|\|+|\:+|\?+|\*+|"+|<+|>+|[[:cntrl:]]+', "_", "all" )>
 
    <cfreturn sNewFolderName>
</cffunction>
 
<cffunction name="BinaryFileRead" returntype="String" output="true">
    <cfargument name="fileName" required="true" type="string">
    <cfargument name="bytes" required="true" type="Numeric">
 
    <cfscript>
    var chunk = "";
    var fileReaderClass = "";
    var fileReader = "";
    var file = "";
    var done = false;
    var counter = 0;
    var byteArray = "";
 
    if( not fileExists( ARGUMENTS.fileName ) )
    {
        return "" ;
    }
 
    if (REQUEST.CFVersion gte 8)
    {
         file  = FileOpen( ARGUMENTS.fileName, "readbinary" ) ;
         byteArray = FileRead( file, 1024 ) ;
         chunk = toString( toBinary( toBase64( byteArray ) ) ) ;
         FileClose( file ) ;
    }
    else
    {
        fileReaderClass = createObject("java", "java.io.FileInputStream");
        fileReader = fileReaderClass.init(fileName);
 
        while(not done)
        {
            char = fileReader.read();
            counter = counter + 1;
            if ( char eq -1 or counter eq ARGUMENTS.bytes)
            {
                done = true;
            }
            else
            {
                chunk = chunk & chr(char) ;
            }
        }
    }
    </cfscript>
 
    <cfreturn chunk>
</cffunction>
 
<cffunction name="SendUploadResults" returntype="String" output="true">
    <cfargument name="errorNumber" required="true" type="Numeric">
    <cfargument name="fileUrl" required="false" type="String" default="">
    <cfargument name="fileName" required="false" type="String" default="">
    <cfargument name="customMsg" required="false" type="String" default="">
 
    <cfif errorNumber and errorNumber neq 201>
        <cfset fileUrl = "">
        <cfset fileName = "">
    </cfif>
    <!--- Minified version of the document.domain automatic fix script (#1919).
    The original script can be found at _dev/domain_fix_template.js --->
    <cfoutput>
<script type="text/javascript">
(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();
window.parent.OnUploadCompleted( #errorNumber#, "#JSStringFormat(fileUrl)#", "#JSStringFormat(fileName)#", "#JSStringFormat(customMsg)#" );
</script>
    </cfoutput>
    <cfabort>
</cffunction>
 
<cffunction name="SanitizeFileName" returntype="String" output="false">
    <cfargument name="sNewFileName" required="true">
 
    <cfif isDefined("REQUEST.Config.ForceSingleExtension") and REQUEST.Config.ForceSingleExtension>
        <cfset sNewFileName = rereplace( sNewFileName, '\.(?![^.]*$)', "_", "all" )>
    </cfif>
 
    <!--- Do a cleanup of the file name to avoid possible problems --->
    <!--- Remove \ / | : ? * " < > and control characters --->
    <cfset sNewFileName = rereplace( sNewFileName, '\\[.]+|\\+|\/+|\|+|\:+|\?+|\*+|"+|<+|>+|[[:cntrl:]]+', "_", "all" )>
 
    <cfreturn sNewFileName>
</cffunction>