1 | [//lasso |
---|
2 | /* |
---|
3 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net |
---|
4 | * Copyright (C) 2003-2009 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 | * This is the File Manager Connector for Lasso. |
---|
23 | */ |
---|
24 | |
---|
25 | /*..................................................................... |
---|
26 | Include global configuration. See config.lasso for details. |
---|
27 | */ |
---|
28 | include('config.lasso'); |
---|
29 | |
---|
30 | |
---|
31 | /*..................................................................... |
---|
32 | Translate current date/time to GMT for custom header. |
---|
33 | */ |
---|
34 | var('headerDate') = date_localtogmt(date)->format('%a, %d %b %Y %T GMT'); |
---|
35 | |
---|
36 | |
---|
37 | /*..................................................................... |
---|
38 | Convert query string parameters to variables and initialize output. |
---|
39 | */ |
---|
40 | var( |
---|
41 | 'Command' = action_param('Command'), |
---|
42 | 'Type' = action_param('Type'), |
---|
43 | 'CurrentFolder' = action_param('CurrentFolder'), |
---|
44 | 'ServerPath' = action_param('ServerPath'), |
---|
45 | 'NewFolderName' = action_param('NewFolderName'), |
---|
46 | 'NewFile' = null, |
---|
47 | 'NewFileName' = string, |
---|
48 | 'OrigFilePath' = string, |
---|
49 | 'NewFilePath' = string, |
---|
50 | 'commandData' = string, |
---|
51 | 'folders' = '\t<Folders>\n', |
---|
52 | 'files' = '\t<Files>\n', |
---|
53 | 'errorNumber' = integer, |
---|
54 | 'responseType' = 'xml', |
---|
55 | 'uploadResult' = '0' |
---|
56 | ); |
---|
57 | |
---|
58 | /*..................................................................... |
---|
59 | Custom tag sets the HTML response. |
---|
60 | */ |
---|
61 | |
---|
62 | define_tag( |
---|
63 | 'htmlreply', |
---|
64 | -namespace='fck_', |
---|
65 | -priority='replace', |
---|
66 | -required='uploadResult', |
---|
67 | -optional='NewFilePath', |
---|
68 | -type='string', |
---|
69 | -description='Sets the HTML response for the FCKEditor File Upload feature.' |
---|
70 | ); |
---|
71 | $__html_reply__ = '\ |
---|
72 | <script type="text/javascript"> |
---|
73 | (function() |
---|
74 | { |
---|
75 | var d = document.domain ; |
---|
76 | |
---|
77 | while ( true ) |
---|
78 | { |
---|
79 | // Test if we can access a parent property. |
---|
80 | try |
---|
81 | { |
---|
82 | var test = window.top.opener.document.domain ; |
---|
83 | break ; |
---|
84 | } |
---|
85 | catch( e ) {} |
---|
86 | |
---|
87 | // Remove a domain part: www.mytest.example.com => mytest.example.com => example.com ... |
---|
88 | d = d.replace( /.*?(?:\\.|$)/, "" ) ; |
---|
89 | |
---|
90 | if ( d.length == 0 ) |
---|
91 | break ; // It was not able to detect the domain. |
---|
92 | |
---|
93 | try |
---|
94 | { |
---|
95 | document.domain = d ; |
---|
96 | } |
---|
97 | catch (e) |
---|
98 | { |
---|
99 | break ; |
---|
100 | } |
---|
101 | } |
---|
102 | })() ; |
---|
103 | '; |
---|
104 | if($uploadResult == '0' || $uploadResult == '201'); |
---|
105 | $__html_reply__ = $__html_reply__ + '\ |
---|
106 | window.parent.OnUploadCompleted(' + $uploadResult + ',\'' + $NewFilePath + '\',\'' + $NewFilePath->split('/')->last + '\'); |
---|
107 | </script> |
---|
108 | '; |
---|
109 | else; |
---|
110 | $__html_reply__ = $__html_reply__ + '\ |
---|
111 | window.parent.OnUploadCompleted(' + $uploadResult + '); |
---|
112 | </script> |
---|
113 | '; |
---|
114 | /if; |
---|
115 | /define_tag; |
---|
116 | |
---|
117 | |
---|
118 | /*..................................................................... |
---|
119 | Calculate the path to the current folder. |
---|
120 | */ |
---|
121 | $ServerPath == '' ? $ServerPath = $config->find('UserFilesPath'); |
---|
122 | |
---|
123 | var('currentFolderURL' = $ServerPath |
---|
124 | + $config->find('Subdirectories')->find(action_param('Type')) |
---|
125 | + $CurrentFolder |
---|
126 | ); |
---|
127 | |
---|
128 | if($CurrentFolder->(Find: '..') || $CurrentFolder->(Find: '\\')); |
---|
129 | if($Command == 'FileUpload'); |
---|
130 | $responseType = 'html'; |
---|
131 | $uploadResult = '102'; |
---|
132 | fck_htmlreply( |
---|
133 | -uploadResult=$uploadResult |
---|
134 | ); |
---|
135 | else; |
---|
136 | $errorNumber = 102; |
---|
137 | $commandData += '<Error number="' + $errorNumber + '" />\n'; |
---|
138 | /if; |
---|
139 | else; |
---|
140 | |
---|
141 | /*..................................................................... |
---|
142 | Build the appropriate response per the 'Command' parameter. Wrap the |
---|
143 | entire process in an inline for file tag permissions. |
---|
144 | */ |
---|
145 | inline($connection); |
---|
146 | select($Command); |
---|
147 | /*............................................................. |
---|
148 | List all subdirectories in the 'Current Folder' directory. |
---|
149 | */ |
---|
150 | case('GetFolders'); |
---|
151 | $commandData += '\t<Folders>\n'; |
---|
152 | |
---|
153 | iterate(file_listdirectory($currentFolderURL), local('this')); |
---|
154 | #this->endswith('/') ? $commandData += '\t\t<Folder name="' + #this->removetrailing('/')& + '" />\n'; |
---|
155 | /iterate; |
---|
156 | |
---|
157 | $commandData += '\t</Folders>\n'; |
---|
158 | |
---|
159 | |
---|
160 | /*............................................................. |
---|
161 | List both files and folders in the 'Current Folder' directory. |
---|
162 | Include the file sizes in kilobytes. |
---|
163 | */ |
---|
164 | case('GetFoldersAndFiles'); |
---|
165 | iterate(file_listdirectory($currentFolderURL), local('this')); |
---|
166 | if(#this->endswith('/')); |
---|
167 | $folders += '\t\t<Folder name="' + #this->removetrailing('/')& + '" />\n'; |
---|
168 | else; |
---|
169 | local('size') = file_getsize($currentFolderURL + #this) / 1024; |
---|
170 | $files += '\t\t<File name="' + #this + '" size="' + #size + '" />\n'; |
---|
171 | /if; |
---|
172 | /iterate; |
---|
173 | |
---|
174 | $folders += '\t</Folders>\n'; |
---|
175 | $files += '\t</Files>\n'; |
---|
176 | |
---|
177 | $commandData += $folders + $files; |
---|
178 | |
---|
179 | |
---|
180 | /*............................................................. |
---|
181 | Create a directory 'NewFolderName' within the 'Current Folder.' |
---|
182 | */ |
---|
183 | case('CreateFolder'); |
---|
184 | $NewFolderName = (String_ReplaceRegExp: $NewFolderName, -find='\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>', -replace='_'); |
---|
185 | var('newFolder' = $currentFolderURL + $NewFolderName + '/'); |
---|
186 | file_create($newFolder); |
---|
187 | |
---|
188 | |
---|
189 | /*......................................................... |
---|
190 | Map Lasso's file error codes to FCKEditor's error codes. |
---|
191 | */ |
---|
192 | select(file_currenterror( -errorcode)); |
---|
193 | case(0); |
---|
194 | $errorNumber = 0; |
---|
195 | case( -9983); |
---|
196 | $errorNumber = 101; |
---|
197 | case( -9976); |
---|
198 | $errorNumber = 102; |
---|
199 | case( -9977); |
---|
200 | $errorNumber = 102; |
---|
201 | case( -9961); |
---|
202 | $errorNumber = 103; |
---|
203 | case; |
---|
204 | $errorNumber = 110; |
---|
205 | /select; |
---|
206 | |
---|
207 | $commandData += '<Error number="' + $errorNumber + '" />\n'; |
---|
208 | |
---|
209 | |
---|
210 | /*............................................................. |
---|
211 | Process an uploaded file. |
---|
212 | */ |
---|
213 | case('FileUpload'); |
---|
214 | /*......................................................... |
---|
215 | This is the only command that returns an HTML response. |
---|
216 | */ |
---|
217 | $responseType = 'html'; |
---|
218 | |
---|
219 | |
---|
220 | /*......................................................... |
---|
221 | Was a file actually uploaded? |
---|
222 | */ |
---|
223 | if(file_uploads->size); |
---|
224 | $NewFile = file_uploads->get(1); |
---|
225 | else; |
---|
226 | $uploadResult = '202'; |
---|
227 | /if; |
---|
228 | |
---|
229 | if($uploadResult == '0'); |
---|
230 | /*..................................................... |
---|
231 | Split the file's extension from the filename in order |
---|
232 | to follow the API's naming convention for duplicate |
---|
233 | files. (Test.txt, Test(1).txt, Test(2).txt, etc.) |
---|
234 | */ |
---|
235 | $NewFileName = $NewFile->find('OrigName'); |
---|
236 | $NewFileName = (String_ReplaceRegExp: $NewFileName, -find='\\\\|\\/|\\||\\:|\\?|\\*|"|<|>', -replace='_'); |
---|
237 | $OrigFilePath = $currentFolderURL + $NewFileName; |
---|
238 | $NewFilePath = $OrigFilePath; |
---|
239 | local('fileExtension') = '.' + $NewFile->find('OrigExtension'); |
---|
240 | #fileExtension = (String_ReplaceRegExp: #fileExtension, -find='\\\\|\\/|\\||\\:|\\?|\\*|"|<|>', -replace='_'); |
---|
241 | local('shortFileName') = $NewFileName->removetrailing(#fileExtension)&; |
---|
242 | |
---|
243 | |
---|
244 | /*..................................................... |
---|
245 | Make sure the file extension is allowed. |
---|
246 | */ |
---|
247 | if($config->find('DeniedExtensions')->find($Type) >> $NewFile->find('OrigExtension')); |
---|
248 | $uploadResult = '202'; |
---|
249 | else; |
---|
250 | /*................................................. |
---|
251 | Rename the target path until it is unique. |
---|
252 | */ |
---|
253 | while(file_exists($NewFilePath)); |
---|
254 | $NewFilePath = $currentFolderURL + #shortFileName + '(' + loop_count + ')' + #fileExtension; |
---|
255 | /while; |
---|
256 | |
---|
257 | |
---|
258 | /*................................................. |
---|
259 | Copy the uploaded file to its final location. |
---|
260 | */ |
---|
261 | file_copy($NewFile->find('path'), $NewFilePath); |
---|
262 | |
---|
263 | |
---|
264 | /*................................................. |
---|
265 | Set the error code for the response. Note whether |
---|
266 | the file had to be renamed. |
---|
267 | */ |
---|
268 | select(file_currenterror( -errorcode)); |
---|
269 | case(0); |
---|
270 | $OrigFilePath != $NewFilePath ? $uploadResult = 201; |
---|
271 | case; |
---|
272 | $uploadResult = file_currenterror( -errorcode); |
---|
273 | /select; |
---|
274 | /if; |
---|
275 | /if; |
---|
276 | fck_htmlreply( |
---|
277 | -uploadResult=$uploadResult, |
---|
278 | -NewFilePath=$NewFilePath |
---|
279 | ); |
---|
280 | /select; |
---|
281 | /inline; |
---|
282 | /if; |
---|
283 | |
---|
284 | /*..................................................................... |
---|
285 | Send a custom header for xml responses. |
---|
286 | */ |
---|
287 | if($responseType == 'xml'); |
---|
288 | header; |
---|
289 | ] |
---|
290 | HTTP/1.0 200 OK |
---|
291 | Date: [$headerDate] |
---|
292 | Server: Lasso Professional [lasso_version( -lassoversion)] |
---|
293 | Expires: Mon, 26 Jul 1997 05:00:00 GMT |
---|
294 | Last-Modified: [$headerDate] |
---|
295 | Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 |
---|
296 | Pragma: no-cache |
---|
297 | Keep-Alive: timeout=15, max=98 |
---|
298 | Connection: Keep-Alive |
---|
299 | Content-Type: text/xml; charset=utf-8 |
---|
300 | [//lasso |
---|
301 | /header; |
---|
302 | |
---|
303 | /* |
---|
304 | Set the content type encoding for Lasso. |
---|
305 | */ |
---|
306 | content_type('text/xml; charset=utf-8'); |
---|
307 | |
---|
308 | /* |
---|
309 | Wrap the response as XML and output. |
---|
310 | */ |
---|
311 | $__html_reply__ = '\ |
---|
312 | <?xml version="1.0" encoding="utf-8" ?> |
---|
313 | <Connector command="' + $Command + '" resourceType="' + $Type + '">'; |
---|
314 | |
---|
315 | if($errorNumber != '102'); |
---|
316 | $__html_reply__ += '<CurrentFolder path="' + $CurrentFolder + '" url="' + $currentFolderURL + '" />'; |
---|
317 | /if; |
---|
318 | |
---|
319 | $__html_reply__ += $commandData + ' |
---|
320 | </Connector>'; |
---|
321 | /if; |
---|
322 | ] |
---|