1[//lasso
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 * 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    /*.....................................................................
60    Calculate the path to the current folder.
61    */
62	$ServerPath == '' ? $ServerPath = $config->find('UserFilesPath');
63
64	var('currentFolderURL' = $ServerPath
65		+ $config->find('Subdirectories')->find(action_param('Type'))
66		+ action_param('CurrentFolder')
67	);
68
69
70    /*.....................................................................
71    Build the appropriate response per the 'Command' parameter. Wrap the
72    entire process in an inline for file tag permissions.
73    */
74	inline($connection);
75		select($Command);
76            /*.............................................................
77            List all subdirectories in the 'Current Folder' directory.
78            */
79			case('GetFolders');
80				$commandData += '\t<Folders>\n';
81
82				iterate(file_listdirectory($currentFolderURL), local('this'));
83					#this->endswith('/') ? $commandData += '\t\t<Folder name="' + #this->removetrailing('/')& + '" />\n';
84				/iterate;
85
86				$commandData += '\t</Folders>\n';
87
88
89            /*.............................................................
90            List both files and folders in the 'Current Folder' directory.
91            Include the file sizes in kilobytes.
92            */
93			case('GetFoldersAndFiles');
94				iterate(file_listdirectory($currentFolderURL), local('this'));
95					if(#this->endswith('/'));
96						$folders += '\t\t<Folder name="' + #this->removetrailing('/')& + '" />\n';
97					else;
98						local('size') = file_getsize($currentFolderURL + #this) / 1024;
99						$files += '\t\t<File name="' + #this + '" size="' + #size + '" />\n';
100					/if;
101				/iterate;
102
103				$folders += '\t</Folders>\n';
104				$files += '\t</Files>\n';
105
106				$commandData += $folders + $files;
107
108
109            /*.............................................................
110            Create a directory 'NewFolderName' within the 'Current Folder.'
111            */
112			case('CreateFolder');
113				var('newFolder' = $currentFolderURL + $NewFolderName + '/');
114				file_create($newFolder);
115
116
117                /*.........................................................
118                Map Lasso's file error codes to FCKEditor's error codes.
119                */
120				select(file_currenterror( -errorcode));
121					case(0);
122						$errorNumber = 0;
123					case( -9983);
124						$errorNumber = 101;
125					case( -9976);
126						$errorNumber = 102;
127					case( -9977);
128						$errorNumber = 102;
129					case( -9961);
130						$errorNumber = 103;
131					case;
132						$errorNumber = 110;
133				/select;
134
135				$commandData += '<Error number="' + $errorNumber + '" />\n';
136
137
138            /*.............................................................
139            Process an uploaded file.
140            */
141			case('FileUpload');
142                /*.........................................................
143                This is the only command that returns an HTML response.
144                */
145				$responseType = 'html';
146
147
148                /*.........................................................
149                Was a file actually uploaded?
150                */
151				file_uploads->size ? $NewFile = file_uploads->get(1) | $uploadResult = '202';
152
153				if($uploadResult == '0');
154                    /*.....................................................
155                    Split the file's extension from the filename in order
156                    to follow the API's naming convention for duplicate
157                    files. (Test.txt, Test(1).txt, Test(2).txt, etc.)
158                    */
159					$NewFileName = $NewFile->find('OrigName');
160					$OrigFilePath = $currentFolderURL + $NewFileName;
161					$NewFilePath = $OrigFilePath;
162					local('fileExtension') = '.' + $NewFile->find('OrigExtension');
163					local('shortFileName') = $NewFileName->removetrailing(#fileExtension)&;
164
165
166                    /*.....................................................
167                    Make sure the file extension is allowed.
168                    */
169					if($config->find('DeniedExtensions')->find($Type) >> $NewFile->find('OrigExtension'));
170						$uploadResult = '202';
171					else;
172                        /*.................................................
173                        Rename the target path until it is unique.
174                        */
175						while(file_exists($NewFilePath));
176							$NewFilePath = $currentFolderURL + #shortFileName + '(' + loop_count + ')' + #fileExtension;
177						/while;
178
179
180                        /*.................................................
181                        Copy the uploaded file to its final location.
182                        */
183						file_copy($NewFile->find('path'), $NewFilePath);
184
185
186                        /*.................................................
187                        Set the error code for the response. Note whether
188                        the file had to be renamed.
189                        */
190						select(file_currenterror( -errorcode));
191							case(0);
192								$OrigFilePath != $NewFilePath ? $uploadResult = 201;
193							case;
194								$uploadResult = '202';
195						/select;
196					/if;
197				/if;
198
199
200                /*.........................................................
201                Set the HTML response.
202                */
203                if($uploadResult == '0' || $uploadResult == '201');
204				$__html_reply__ = '\
205<script type="text/javascript">
206	window.parent.frames[\'frmUpload\'].OnUploadCompleted(' + $uploadResult + ',\'' + $NewFilePath + '\',\'' + $NewFilePath->split('/')->last + '\');
207</script>
208				';
209                else;
210				$__html_reply__ = '\
211<script type="text/javascript">
212	window.parent.frames[\'frmUpload\'].OnUploadCompleted(' + $uploadResult + ');
213</script>
214				';
215				/if;
216		/select;
217	/inline;
218
219
220    /*.....................................................................
221    Send a custom header for xml responses.
222    */
223	if($responseType == 'xml');
224		header;
225]
226HTTP/1.0 200 OK
227Date: [$headerDate]
228Server: Lasso Professional [lasso_version( -lassoversion)]
229Expires: Mon, 26 Jul 1997 05:00:00 GMT
230Last-Modified: [$headerDate]
231Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
232Pragma: no-cache
233Keep-Alive: timeout=15, max=98
234Connection: Keep-Alive
235Content-Type: text/xml; charset=utf-8
236[//lasso
237		/header;
238
239
240        /*.................................................................
241        Set the content type encoding for Lasso.
242        */
243		content_type('text/xml; charset=utf-8');
244
245
246        /*.................................................................
247        Wrap the response as XML and output.
248        */
249		$__html_reply__ = '\
250<?xml version="1.0" encoding="utf-8" ?>
251<Connector command="' + $Command + '" resourceType="' + $Type + '">
252	<CurrentFolder path="' + $CurrentFolder + '" url="' + $currentFolderURL + '" />
253' + $commandData + '
254</Connector>
255		';
256	/if;
257]
258