1#####
2#  FCKeditor - The text editor for Internet - http://www.fckeditor.net
3#  Copyright (C) 2003-2007 Frederico Caldeira Knabben
4#
5#  == BEGIN LICENSE ==
6#
7#  Licensed under the terms of any of the following licenses at your
8#  choice:
9#
10#   - GNU General Public License Version 2 or later (the "GPL")
11#     http://www.gnu.org/licenses/gpl.html
12#
13#   - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14#     http://www.gnu.org/licenses/lgpl.html
15#
16#   - Mozilla Public License Version 1.1 or later (the "MPL")
17#     http://www.mozilla.org/MPL/MPL-1.1.html
18#
19#  == END LICENSE ==
20#
21#  This is the File Manager Connector for Perl.
22#####
23
24sub GetFolders
25{
26
27	local($resourceType, $currentFolder) = @_;
28
29	# Map the virtual path to the local server path.
30	$sServerDir = &ServerMapFolder($resourceType, $currentFolder);
31	print "<Folders>";			# Open the "Folders" node.
32
33	opendir(DIR,"$sServerDir");
34	@files = grep(!/^\.\.?$/,readdir(DIR));
35	closedir(DIR);
36
37	foreach $sFile (@files) {
38		if($sFile != '.' && $sFile != '..' && (-d "$sServerDir$sFile")) {
39			$cnv_filename = &ConvertToXmlAttribute($sFile);
40			print '<Folder name="' . $cnv_filename . '" />';
41		}
42	}
43	print "</Folders>";			# Close the "Folders" node.
44}
45
46sub GetFoldersAndFiles
47{
48
49	local($resourceType, $currentFolder) = @_;
50	# Map the virtual path to the local server path.
51	$sServerDir = &ServerMapFolder($resourceType,$currentFolder);
52
53	# Initialize the output buffers for "Folders" and "Files".
54	$sFolders	= '<Folders>';
55	$sFiles		= '<Files>';
56
57	opendir(DIR,"$sServerDir");
58	@files = grep(!/^\.\.?$/,readdir(DIR));
59	closedir(DIR);
60
61	foreach $sFile (@files) {
62		if($sFile ne '.' && $sFile ne '..') {
63			if(-d "$sServerDir$sFile") {
64				$cnv_filename = &ConvertToXmlAttribute($sFile);
65				$sFolders .= '<Folder name="' . $cnv_filename . '" />' ;
66			} else {
67				($iFileSize,$refdate,$filedate,$fileperm) = (stat("$sServerDir$sFile"))[7,8,9,2];
68				if($iFileSize > 0) {
69					$iFileSize = int($iFileSize / 1024);
70					if($iFileSize < 1) {
71						$iFileSize = 1;
72					}
73				}
74				$cnv_filename = &ConvertToXmlAttribute($sFile);
75				$sFiles	.= '<File name="' . $cnv_filename . '" size="' . $iFileSize . '" />' ;
76			}
77		}
78	}
79	print $sFolders ;
80	print '</Folders>';			# Close the "Folders" node.
81	print $sFiles ;
82	print '</Files>';			# Close the "Files" node.
83}
84
85sub CreateFolder
86{
87
88	local($resourceType, $currentFolder) = @_;
89	$sErrorNumber	= '0' ;
90	$sErrorMsg		= '' ;
91
92	if($FORM{'NewFolderName'} ne "") {
93		$sNewFolderName = $FORM{'NewFolderName'};
94		# Map the virtual path to the local server path of the current folder.
95		$sServerDir = &ServerMapFolder($resourceType, $currentFolder);
96		if(-w $sServerDir) {
97			$sServerDir .= $sNewFolderName;
98			$sErrorMsg = &CreateServerFolder($sServerDir);
99			if($sErrorMsg == 0) {
100				$sErrorNumber = '0';
101			} elsif($sErrorMsg eq 'Invalid argument' || $sErrorMsg eq 'No such file or directory') {
102				$sErrorNumber = '102';		#// Path too long.
103			} else {
104				$sErrorNumber = '110';
105			}
106		} else {
107			$sErrorNumber = '103';
108		}
109	} else {
110		$sErrorNumber = '102' ;
111	}
112	# Create the "Error" node.
113	$cnv_errmsg = &ConvertToXmlAttribute($sErrorMsg);
114	print '<Error number="' . $sErrorNumber . '" originalDescription="' . $cnv_errmsg . '" />';
115}
116
117sub FileUpload
118{
119eval("use File::Copy;");
120
121	local($resourceType, $currentFolder) = @_;
122
123	$sErrorNumber = '0' ;
124	$sFileName = '' ;
125	if($new_fname) {
126		# Map the virtual path to the local server path.
127		$sServerDir = &ServerMapFolder($resourceType,$currentFolder);
128
129		# Get the uploaded file name.
130		$sFileName = $new_fname;
131		$sOriginalFileName = $sFileName;
132
133		$iCounter = 0;
134		while(1) {
135			$sFilePath = $sServerDir . $sFileName;
136			if(-e $sFilePath) {
137				$iCounter++ ;
138				($path,$BaseName,$ext) = &RemoveExtension($sOriginalFileName);
139				$sFileName = $BaseName . '(' . $iCounter . ').' . $ext;
140				$sErrorNumber = '201';
141			} else {
142				copy("$img_dir/$new_fname","$sFilePath");
143				chmod(0777,$sFilePath);
144				unlink("$img_dir/$new_fname");
145				last;
146			}
147		}
148	} else {
149		$sErrorNumber = '202' ;
150	}
151	$sFileName	=~ s/"/\\"/g;
152
153	SendUploadResults($sErrorNumber, $resourceType.$currentFolder.$sFileName, $sFileName, '');
154}
155
156sub SendUploadResults
157{
158
159	local($sErrorNumber, $sFileUrl, $sFileName, $customMsg) = @_;
160
161	print "Content-type: text/html\n\n";
162	print '<script type="text/javascript">';
163	print 'window.parent.OnUploadCompleted(' . $sErrorNumber . ',"' . JS_cnv($sFileUrl) . '","' . JS_cnv($sFileName) . '","' . JS_cnv($customMsg) . '") ;';
164	print '</script>';
165	exit ;
166}
167
1681;
169