PageRenderTime 20ms CodeModel.GetById 10ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/pigeoncms/Plugins/fckeditor/editor/filemanager/connectors/lasso/upload.lasso

http://pigeoncms.googlecode.com/
Unknown | 178 lines | 154 code | 24 blank | 0 comment | 0 complexity | 2ff3f586959ed730e054b349c3ef78ea MD5 | raw file
  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 Uploader" for Lasso.
 23 */
 24
 25    /*.....................................................................
 26    Include global configuration. See config.lasso for details.
 27    */
 28	include('config.lasso');
 29
 30
 31    /*.....................................................................
 32    Convert query string parameters to variables and initialize output.
 33    */
 34	var(
 35		'Type'			=	(Encode_HTML: action_param('Type')),
 36		'CurrentFolder'	=	"/",
 37		'ServerPath'	=	action_param('ServerPath'),
 38		'NewFile'		=	null,
 39		'NewFileName'	=	string,
 40		'OrigFilePath'	=	string,
 41		'NewFilePath'	=	string,
 42		'errorNumber'	=	0,
 43		'customMsg'		=	''
 44	);
 45
 46	$Type == '' ? $Type = 'File';
 47
 48
 49    /*.....................................................................
 50    Calculate the path to the current folder.
 51    */
 52	$ServerPath == '' ? $ServerPath = $config->find('UserFilesPath');
 53
 54	var('currentFolderURL' = $ServerPath
 55		+ $config->find('Subdirectories')->find(action_param('Type'))
 56		+ $CurrentFolder
 57	);
 58
 59	$currentFolderURL = string_replace($currentFolderURL, -find='//', -replace='/');
 60
 61	/*.....................................................................
 62	Custom tag sets the HTML response.
 63	*/
 64
 65	define_tag(
 66		'sendresults',
 67		-namespace='fck_',
 68		-priority='replace',
 69		-required='errorNumber',
 70		-type='integer',
 71		-optional='fileUrl',
 72		-type='string',
 73		-optional='fileName',
 74		-type='string',
 75		-optional='customMsg',
 76		-type='string',
 77		-description='Sets the HTML response for the FCKEditor Quick Upload feature.'
 78	);
 79
 80		$__html_reply__ = '<script type="text/javascript">';
 81
 82		// Minified version of the document.domain automatic fix script (#1919).
 83		// The original script can be found at _dev/domain_fix_template.js
 84		// Note: in Lasso replace \ with \\
 85		$__html_reply__ = $__html_reply__ + "(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;}}})();";
 86
 87		$__html_reply__ = $__html_reply__ + '\
 88	window.parent.OnUploadCompleted(' + #errorNumber + ',"'
 89		+ string_replace((Encode_HTML: #fileUrl), -find='"', -replace='\\"') + '","'
 90		+ string_replace((Encode_HTML: #fileUrl->split('/')->last), -find='"', -replace='\\"') + '","'
 91		+ string_replace((Encode_HTML: #customMsg), -find='"', -replace='\\"') + '");
 92</script>
 93		';
 94	/define_tag;
 95
 96	if($CurrentFolder->(Find: '..') || (String_FindRegExp: $CurrentFolder, -Find='(/\\.)|(//)|[\\\\:\\*\\?\\""\\<\\>\\|]|\\000|[\u007F]|[\u0001-\u001F]'));
 97		$errorNumber = 102;
 98	/if;
 99
100	if($config->find('Enabled'));
101		/*.................................................................
102		Process an uploaded file.
103		*/
104		inline($connection);
105			/*.............................................................
106			Was a file actually uploaded?
107			*/
108			if($errorNumber != '102');
109				file_uploads->size ? $NewFile = file_uploads->get(1) | $errorNumber = 202;
110			/if;
111
112			if($errorNumber == 0);
113				/*.........................................................
114				Split the file's extension from the filename in order
115				to follow the API's naming convention for duplicate
116				files. (Test.txt, Test(1).txt, Test(2).txt, etc.)
117				*/
118				$NewFileName = $NewFile->find('OrigName');
119				$NewFileName = (String_ReplaceRegExp: $NewFileName, -find='\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|\\000|[\u007F]|[\u0001-\u001F]', -replace='_');
120				$NewFileName = (String_ReplaceRegExp: $NewFileName, -find='\\.(?![^.]*$)', -replace='_');
121				$OrigFilePath = $currentFolderURL + $NewFileName;
122				$NewFilePath = $OrigFilePath;
123				local('fileExtension') = '.' + $NewFile->find('OrigExtension');
124				local('shortFileName') = $NewFileName->removetrailing(#fileExtension)&;
125
126
127				/*.........................................................
128				Make sure the file extension is allowed.
129				*/
130
131				local('allowedExt') = $config->find('AllowedExtensions')->find($Type);
132				local('deniedExt') = $config->find('DeniedExtensions')->find($Type);
133				if($allowedExt->Size > 0 && $allowedExt !>> $NewFile->find('OrigExtension'));
134					$errorNumber = 202;
135				else($deniedExt->Size > 0 && $deniedExt >> $NewFile->find('OrigExtension'));
136					$errorNumber = 202;
137				else;
138					/*.....................................................
139					Rename the target path until it is unique.
140					*/
141					while(file_exists($NewFilePath));
142						$NewFileName = #shortFileName + '(' + loop_count + ')' + #fileExtension;
143						$NewFilePath = $currentFolderURL + $NewFileName;
144					/while;
145
146
147					/*.....................................................
148					Copy the uploaded file to its final location.
149					*/
150					file_copy($NewFile->find('path'), $NewFilePath);
151
152
153					/*.....................................................
154					Set the error code for the response.
155					*/
156					select(file_currenterror( -errorcode));
157						case(0);
158							$OrigFilePath != $NewFilePath ? $errorNumber = 201;
159						case;
160							$errorNumber = 202;
161					/select;
162				/if;
163			/if;
164			if ($errorNumber != 0 && $errorNumber != 201);
165				$NewFilePath = "";
166			/if;
167		/inline;
168	else;
169		$errorNumber = 1;
170		$customMsg = 'This file uploader is disabled. Please check the "editor/filemanager/upload/lasso/config.lasso" file.';
171	/if;
172
173	fck_sendresults(
174		-errorNumber=$errorNumber,
175		-fileUrl=$NewFilePath,
176		-customMsg=$customMsg
177	);
178]