connector.cfm
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:11k
源码类别:

OA系统

开发平台:

C#

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