| 1 | <cfsetting enablecfoutputonly="yes" showdebugoutput="no"> |
|---|
| 2 | <!--- |
|---|
| 3 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net |
|---|
| 4 | * Copyright (C) 2003-2007 Frederico Caldeira Knabben |
|---|
| 5 | * |
|---|
| 6 | * == BEGIN LICENSE == |
|---|
| 7 | * |
|---|
| 8 | * Licensed under the terms of any of the following licenses at your |
|---|
| 9 | * choice: |
|---|
| 10 | * |
|---|
| 11 | * - GNU General Public License Version 2 or later (the "GPL") |
|---|
| 12 | * http://www.gnu.org/licenses/gpl.html |
|---|
| 13 | * |
|---|
| 14 | * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") |
|---|
| 15 | * http://www.gnu.org/licenses/lgpl.html |
|---|
| 16 | * |
|---|
| 17 | * - Mozilla Public License Version 1.1 or later (the "MPL") |
|---|
| 18 | * http://www.mozilla.org/MPL/MPL-1.1.html |
|---|
| 19 | * |
|---|
| 20 | * == END LICENSE == |
|---|
| 21 | * |
|---|
| 22 | * File Browser connector for ColdFusion. |
|---|
| 23 | * (based on the original CF connector by Hendrik Kramer - hk@lwd.de) |
|---|
| 24 | * |
|---|
| 25 | * Note: |
|---|
| 26 | * FCKeditor requires that the connector responds with UTF-8 encoded XML. |
|---|
| 27 | * As ColdFusion 5 does not fully support UTF-8 encoding, we force ASCII |
|---|
| 28 | * file and folder names in this connector to allow CF5 send a UTF-8 |
|---|
| 29 | * encoded response - code points under 127 in UTF-8 are stored using a |
|---|
| 30 | * single byte, using the same encoding as ASCII, which is damn handy. |
|---|
| 31 | * This is all grand for the English speakers, like meself, but I dunno |
|---|
| 32 | * how others are gonna take to it. Well, the previous version of this |
|---|
| 33 | * connector already did this with file names and nobody seemed to mind, |
|---|
| 34 | * so fingers-crossed nobody will mind their folder names being munged too. |
|---|
| 35 | * |
|---|
| 36 | ---> |
|---|
| 37 | |
|---|
| 38 | <cfparam name="url.command"> |
|---|
| 39 | <cfparam name="url.type"> |
|---|
| 40 | <cfparam name="url.currentFolder"> |
|---|
| 41 | <!--- note: no serverPath url parameter - see config.cfm if you need to set the serverPath manually ---> |
|---|
| 42 | |
|---|
| 43 | <cfinclude template="config.cfm"> |
|---|
| 44 | |
|---|
| 45 | <cfscript> |
|---|
| 46 | userFilesPath = config.userFilesPath; |
|---|
| 47 | lAllowedExtensions = config.allowedExtensions[url.type]; |
|---|
| 48 | lDeniedExtensions = config.deniedExtensions[url.type]; |
|---|
| 49 | |
|---|
| 50 | // make sure the user files path is correctly formatted |
|---|
| 51 | userFilesPath = replace(userFilesPath, "\", "/", "ALL"); |
|---|
| 52 | if ( right(userFilesPath,1) neq "/" ) { |
|---|
| 53 | userFilesPath = userFilesPath & "/"; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | // make sure the current folder is correctly formatted |
|---|
| 57 | url.currentFolder = replace(url.currentFolder, "\", "/", "ALL"); |
|---|
| 58 | url.currentFolder = replace(url.currentFolder, '//', '/', 'ALL'); |
|---|
| 59 | if ( right(url.currentFolder,1) neq "/" ) { |
|---|
| 60 | url.currentFolder = url.currentFolder & "/"; |
|---|
| 61 | } |
|---|
| 62 | if ( left(url.currentFolder,1) neq "/" ) { |
|---|
| 63 | url.currentFolder = "/" & url.currentFolder; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | if ( find("/",getBaseTemplatePath()) neq 0 ) { |
|---|
| 67 | fs = "/"; |
|---|
| 68 | } else { |
|---|
| 69 | fs = "\"; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | // Get the base physical path to the web root for this application. The code to determine the path automatically assumes that |
|---|
| 73 | // the "FCKeditor" directory in the http request path is directly off the web root for the application and that it's not a |
|---|
| 74 | // virtual directory or a symbolic link / junction. Use the serverPath config setting to force a physical path if necessary. |
|---|
| 75 | if ( len(config.serverPath) ) { |
|---|
| 76 | serverPath = config.serverPath; |
|---|
| 77 | } else { |
|---|
| 78 | serverPath = replaceNoCase(getBaseTemplatePath(),replace(cgi.script_name,"/",fs,"all"),""); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | // map the user files path to a physical directory |
|---|
| 82 | userFilesServerPath = serverPath & url.type & replace(url.currentFolder,"/",fs,"all"); |
|---|
| 83 | |
|---|
| 84 | xmlContent = ""; // append to this string to build content |
|---|
| 85 | </cfscript> |
|---|
| 86 | |
|---|
| 87 | <cfif not config.enabled> |
|---|
| 88 | |
|---|
| 89 | <cfset xmlContent = "<Error number=""1"" text=""This connector is disabled. Please check the 'editor/filemanager/browser/default/connectors/cfm/config.cfm' file"" />"> |
|---|
| 90 | |
|---|
| 91 | <cfelseif find("..",url.currentFolder)> |
|---|
| 92 | |
|---|
| 93 | <cfset xmlContent = "<Error number=""102"" />"> |
|---|
| 94 | |
|---|
| 95 | </cfif> |
|---|
| 96 | |
|---|
| 97 | <cfif not len(xmlContent)> |
|---|
| 98 | |
|---|
| 99 | <!--- create directories in physical path if they don't already exist ---> |
|---|
| 100 | <cfset currentPath = ""> |
|---|
| 101 | <cftry> |
|---|
| 102 | <cfloop list="#userFilesServerPath#" index="name" delimiters="/"> |
|---|
| 103 | |
|---|
| 104 | <cfif not directoryExists(currentPath & fs & name)> |
|---|
| 105 | <cfdirectory action="create" directory="#currentPath##fs##name#" mode="755"> |
|---|
| 106 | </cfif> |
|---|
| 107 | |
|---|
| 108 | <cfset currentPath = currentPath & fs & name> |
|---|
| 109 | |
|---|
| 110 | </cfloop> |
|---|
| 111 | |
|---|
| 112 | <!--- create sub-directory for file type if it doesn't already exist ---> |
|---|
| 113 | <!--- |
|---|
| 114 | <cfif not directoryExists(userFilesServerPath & url.type)> |
|---|
| 115 | <cfdirectory action="create" directory="#userFilesServerPath##url.type#" mode="755"> |
|---|
| 116 | </cfif> |
|---|
| 117 | ---> |
|---|
| 118 | <cfcatch> |
|---|
| 119 | |
|---|
| 120 | <!--- this should only occur as a result of a permissions problem ---> |
|---|
| 121 | <cfset xmlContent = "<Error number=""103"" />"> |
|---|
| 122 | |
|---|
| 123 | </cfcatch> |
|---|
| 124 | </cftry> |
|---|
| 125 | |
|---|
| 126 | </cfif> |
|---|
| 127 | |
|---|
| 128 | <cfif not len(xmlContent)> |
|---|
| 129 | |
|---|
| 130 | <!--- no errors thus far - run command ---> |
|---|
| 131 | |
|---|
| 132 | <!--- we need to know the physical path to the current folder for all commands ---> |
|---|
| 133 | <cfset currentFolderPath = userFilesServerPath> |
|---|
| 134 | |
|---|
| 135 | <cfswitch expression="#url.command#"> |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | <cfcase value="FileUpload"> |
|---|
| 139 | |
|---|
| 140 | <cfset fileName = ""> |
|---|
| 141 | <cfset fileExt = ""> |
|---|
| 142 | |
|---|
| 143 | <cftry> |
|---|
| 144 | |
|---|
| 145 | <!--- TODO: upload to a temp directory and move file if extension is allowed ---> |
|---|
| 146 | |
|---|
| 147 | <!--- first upload the file with an unique filename ---> |
|---|
| 148 | <cffile action="upload" |
|---|
| 149 | fileField="NewFile" |
|---|
| 150 | destination="#currentFolderPath#" |
|---|
| 151 | nameConflict="makeunique" |
|---|
| 152 | mode="644" |
|---|
| 153 | attributes="normal"> |
|---|
| 154 | |
|---|
| 155 | <cfif cffile.fileSize EQ 0> |
|---|
| 156 | <cfthrow> |
|---|
| 157 | </cfif> |
|---|
| 158 | |
|---|
| 159 | <cfif ( len(lAllowedExtensions) and not listFindNoCase(lAllowedExtensions,cffile.ServerFileExt) ) |
|---|
| 160 | or ( len(lDeniedExtensions) and listFindNoCase(lDeniedExtensions,cffile.ServerFileExt) )> |
|---|
| 161 | |
|---|
| 162 | <cfset errorNumber = "202"> |
|---|
| 163 | <cffile action="delete" file="#cffile.ServerDirectory##fs##cffile.ServerFile#"> |
|---|
| 164 | |
|---|
| 165 | <cfelse> |
|---|
| 166 | |
|---|
| 167 | <cfscript> |
|---|
| 168 | errorNumber = 0; |
|---|
| 169 | fileName = cffile.ClientFileName; |
|---|
| 170 | fileExt = cffile.ServerFileExt; |
|---|
| 171 | |
|---|
| 172 | // munge filename for html download. Only a-z, 0-9, _, - and . are allowed |
|---|
| 173 | if( reFind("[^A-Za-z0-9_\-\.]", fileName) ) { |
|---|
| 174 | fileName = reReplace(fileName, "[^A-Za-z0-9\-\.]", "_", "ALL"); |
|---|
| 175 | fileName = reReplace(fileName, "_{2,}", "_", "ALL"); |
|---|
| 176 | fileName = reReplace(fileName, "([^_]+)_+$", "\1", "ALL"); |
|---|
| 177 | fileName = reReplace(fileName, "$_([^_]+)$", "\1", "ALL"); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | // When the original filename already exists, add numbers (0), (1), (2), ... at the end of the filename. |
|---|
| 181 | if( compare( cffile.ServerFileName, fileName ) ) { |
|---|
| 182 | counter = 0; |
|---|
| 183 | tmpFileName = fileName; |
|---|
| 184 | while( fileExists("#currentFolderPath##fileName#.#fileExt#") ) { |
|---|
| 185 | counter = counter + 1; |
|---|
| 186 | fileName = tmpFileName & '(#counter#)'; |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | </cfscript> |
|---|
| 190 | |
|---|
| 191 | <!--- Rename the uploaded file, if neccessary ---> |
|---|
| 192 | <cfif compare(cffile.ServerFileName,fileName)> |
|---|
| 193 | |
|---|
| 194 | <cfset errorNumber = "201"> |
|---|
| 195 | <cffile |
|---|
| 196 | action="rename" |
|---|
| 197 | source="#currentFolderPath##cffile.ServerFileName#.#cffile.ServerFileExt#" |
|---|
| 198 | destination="#currentFolderPath##fileName#.#fileExt#" |
|---|
| 199 | mode="644" |
|---|
| 200 | attributes="normal"> |
|---|
| 201 | |
|---|
| 202 | </cfif> |
|---|
| 203 | |
|---|
| 204 | </cfif> |
|---|
| 205 | |
|---|
| 206 | <cfcatch type="Any"> |
|---|
| 207 | |
|---|
| 208 | <cfset errorNumber = "202"> |
|---|
| 209 | |
|---|
| 210 | </cfcatch> |
|---|
| 211 | |
|---|
| 212 | </cftry> |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | <cfif errorNumber eq 201> |
|---|
| 216 | |
|---|
| 217 | <!--- file was changed (201), submit the new filename ---> |
|---|
| 218 | <cfoutput> |
|---|
| 219 | <script type="text/javascript"> |
|---|
| 220 | window.parent.frames['frmUpload'].OnUploadCompleted(#errorNumber#,'#replace( fileName & "." & fileExt, "'", "\'", "ALL")#'); |
|---|
| 221 | </script> |
|---|
| 222 | </cfoutput> |
|---|
| 223 | |
|---|
| 224 | <cfelse> |
|---|
| 225 | |
|---|
| 226 | <!--- file was uploaded succesfully(0) or an error occured(202). Submit only the error code. ---> |
|---|
| 227 | <cfoutput> |
|---|
| 228 | <script type="text/javascript"> |
|---|
| 229 | window.parent.frames['frmUpload'].OnUploadCompleted(#errorNumber#); |
|---|
| 230 | </script> |
|---|
| 231 | </cfoutput> |
|---|
| 232 | |
|---|
| 233 | </cfif> |
|---|
| 234 | |
|---|
| 235 | <cfabort> |
|---|
| 236 | |
|---|
| 237 | </cfcase> |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | <cfcase value="GetFolders"> |
|---|
| 241 | |
|---|
| 242 | <!--- Sort directories first, name ascending ---> |
|---|
| 243 | <cfdirectory |
|---|
| 244 | action="list" |
|---|
| 245 | directory="#currentFolderPath#" |
|---|
| 246 | name="qDir" |
|---|
| 247 | sort="type,name"> |
|---|
| 248 | |
|---|
| 249 | <cfscript> |
|---|
| 250 | i=1; |
|---|
| 251 | folders = ""; |
|---|
| 252 | while( i lte qDir.recordCount ) { |
|---|
| 253 | if( not compareNoCase( qDir.type[i], "FILE" )) |
|---|
| 254 | break; |
|---|
| 255 | if( not listFind(".,..", qDir.name[i]) ) |
|---|
| 256 | folders = folders & '<Folder name="#HTMLEditFormat( qDir.name[i] )#" />'; |
|---|
| 257 | i=i+1; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | xmlContent = xmlContent & '<Folders>' & folders & '</Folders>'; |
|---|
| 261 | </cfscript> |
|---|
| 262 | |
|---|
| 263 | </cfcase> |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | <cfcase value="GetFoldersAndFiles"> |
|---|
| 267 | |
|---|
| 268 | <!--- Sort directories first, name ascending ---> |
|---|
| 269 | <cfdirectory |
|---|
| 270 | action="list" |
|---|
| 271 | directory="#currentFolderPath#" |
|---|
| 272 | name="qDir" |
|---|
| 273 | sort="type,name"> |
|---|
| 274 | |
|---|
| 275 | <cfscript> |
|---|
| 276 | i=1; |
|---|
| 277 | folders = ""; |
|---|
| 278 | files = ""; |
|---|
| 279 | while( i lte qDir.recordCount ) { |
|---|
| 280 | if( not compareNoCase( qDir.type[i], "DIR" ) and not listFind(".,..", qDir.name[i]) ) { |
|---|
| 281 | folders = folders & '<Folder name="#HTMLEditFormat(qDir.name[i])#" />'; |
|---|
| 282 | } else if( not compareNoCase( qDir.type[i], "FILE" ) ) { |
|---|
| 283 | fileSizeKB = round(qDir.size[i] / 1024); |
|---|
| 284 | files = files & '<File name="#HTMLEditFormat(qDir.name[i])#" size="#IIf( fileSizeKB GT 0, DE( fileSizeKB ), 1)#" />'; |
|---|
| 285 | } |
|---|
| 286 | i=i+1; |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | xmlContent = xmlContent & '<Folders>' & folders & '</Folders>'; |
|---|
| 290 | xmlContent = xmlContent & '<Files>' & files & '</Files>'; |
|---|
| 291 | </cfscript> |
|---|
| 292 | |
|---|
| 293 | </cfcase> |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | <cfcase value="CreateFolder"> |
|---|
| 297 | |
|---|
| 298 | <cfparam name="url.newFolderName" default=""> |
|---|
| 299 | |
|---|
| 300 | <cfscript> |
|---|
| 301 | newFolderName = url.newFolderName; |
|---|
| 302 | if( reFind("[^A-Za-z0-9_\-\.]", newFolderName) ) { |
|---|
| 303 | // Munge folder name same way as we do the filename |
|---|
| 304 | // This means folder names are always US-ASCII so we don't have to worry about CF5 and UTF-8 |
|---|
| 305 | newFolderName = reReplace(newFolderName, "[^A-Za-z0-9\-\.]", "_", "all"); |
|---|
| 306 | newFolderName = reReplace(newFolderName, "_{2,}", "_", "all"); |
|---|
| 307 | newFolderName = reReplace(newFolderName, "([^_]+)_+$", "\1", "all"); |
|---|
| 308 | newFolderName = reReplace(newFolderName, "$_([^_]+)$", "\1", "all"); |
|---|
| 309 | } |
|---|
| 310 | </cfscript> |
|---|
| 311 | |
|---|
| 312 | <cfif not len(newFolderName) or len(newFolderName) gt 255> |
|---|
| 313 | <cfset errorNumber = 102> |
|---|
| 314 | <cfelseif directoryExists(currentFolderPath & newFolderName)> |
|---|
| 315 | <cfset errorNumber = 101> |
|---|
| 316 | <cfelseif reFind("^\.\.",newFolderName)> |
|---|
| 317 | <cfset errorNumber = 103> |
|---|
| 318 | <cfelse> |
|---|
| 319 | <cfset errorNumber = 0> |
|---|
| 320 | |
|---|
| 321 | <cftry> |
|---|
| 322 | <cfdirectory |
|---|
| 323 | action="create" |
|---|
| 324 | directory="#currentFolderPath##newFolderName#" |
|---|
| 325 | mode="755"> |
|---|
| 326 | <cfcatch> |
|---|
| 327 | <!--- |
|---|
| 328 | un-resolvable error numbers in ColdFusion: |
|---|
| 329 | * 102 : Invalid folder name. |
|---|
| 330 | * 103 : You have no permissions to create the folder. |
|---|
| 331 | ---> |
|---|
| 332 | <cfset errorNumber = 110> |
|---|
| 333 | </cfcatch> |
|---|
| 334 | </cftry> |
|---|
| 335 | </cfif> |
|---|
| 336 | |
|---|
| 337 | <cfset xmlContent = xmlContent & '<Error number="#errorNumber#" />'> |
|---|
| 338 | |
|---|
| 339 | </cfcase> |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | <cfdefaultcase> |
|---|
| 343 | |
|---|
| 344 | <cfthrow type="fckeditor.connector" message="Illegal command: #url.command#"> |
|---|
| 345 | |
|---|
| 346 | </cfdefaultcase> |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | </cfswitch> |
|---|
| 350 | |
|---|
| 351 | </cfif> |
|---|
| 352 | |
|---|
| 353 | <cfscript> |
|---|
| 354 | xmlHeader = '<?xml version="1.0" encoding="utf-8" ?><Connector command="#url.command#" resourceType="#url.type#">'; |
|---|
| 355 | xmlHeader = xmlHeader & '<CurrentFolder path="#url.currentFolder#" url="#userFilesPath##url.type##url.currentFolder#" />'; |
|---|
| 356 | xmlFooter = '</Connector>'; |
|---|
| 357 | </cfscript> |
|---|
| 358 | |
|---|
| 359 | <cfheader name="Expires" value="#GetHttpTimeString(Now())#"> |
|---|
| 360 | <cfheader name="Pragma" value="no-cache"> |
|---|
| 361 | <cfheader name="Cache-Control" value="no-cache, no-store, must-revalidate"> |
|---|
| 362 | <cfcontent reset="true" type="text/xml; charset=UTF-8"> |
|---|
| 363 | <cfoutput>#xmlHeader##xmlContent##xmlFooter#</cfoutput> |
|---|