1<?php
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 PHP.
23 */
24
25function GetFolders( $resourceType, $currentFolder )
26{
27	// Map the virtual path to the local server path.
28	$sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'GetFolders' ) ;
29
30	// Array that will hold the folders names.
31	$aFolders	= array() ;
32
33	$oCurrentFolder = opendir( $sServerDir ) ;
34
35	while ( $sFile = readdir( $oCurrentFolder ) )
36	{
37		if ( $sFile != '.' && $sFile != '..' && is_dir( $sServerDir . $sFile ) )
38			$aFolders[] = '<Folder name="' . ConvertToXmlAttribute( $sFile ) . '" />' ;
39	}
40
41	closedir( $oCurrentFolder ) ;
42
43	// Open the "Folders" node.
44	echo "<Folders>" ;
45
46	natcasesort( $aFolders ) ;
47	foreach ( $aFolders as $sFolder )
48		echo $sFolder ;
49
50	// Close the "Folders" node.
51	echo "</Folders>" ;
52}
53
54function GetFoldersAndFiles( $resourceType, $currentFolder )
55{
56	// Map the virtual path to the local server path.
57	$sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'GetFoldersAndFiles' ) ;
58
59	// Arrays that will hold the folders and files names.
60	$aFolders	= array() ;
61	$aFiles		= array() ;
62
63	$oCurrentFolder = opendir( $sServerDir ) ;
64
65	while ( $sFile = readdir( $oCurrentFolder ) )
66	{
67		if ( $sFile != '.' && $sFile != '..' )
68		{
69			if ( is_dir( $sServerDir . $sFile ) )
70				$aFolders[] = '<Folder name="' . ConvertToXmlAttribute( $sFile ) . '" />' ;
71			else
72			{
73				$iFileSize = @filesize( $sServerDir . $sFile ) ;
74				if ( !$iFileSize ) {
75					$iFileSize = 0 ;
76				}
77				if ( $iFileSize > 0 )
78				{
79					$iFileSize = round( $iFileSize / 1024 ) ;
80					if ( $iFileSize < 1 ) $iFileSize = 1 ;
81				}
82
83				$aFiles[] = '<File name="' . ConvertToXmlAttribute( $sFile ) . '" size="' . $iFileSize . '" />' ;
84			}
85		}
86	}
87
88	// Send the folders
89	natcasesort( $aFolders ) ;
90	echo '<Folders>' ;
91
92	foreach ( $aFolders as $sFolder )
93		echo $sFolder ;
94
95	echo '</Folders>' ;
96
97	// Send the files
98	natcasesort( $aFiles ) ;
99	echo '<Files>' ;
100
101	foreach ( $aFiles as $sFiles )
102		echo $sFiles ;
103
104	echo '</Files>' ;
105}
106
107function CreateFolder( $resourceType, $currentFolder )
108{
109	if (!isset($_GET)) {
110		global $_GET;
111	}
112	$sErrorNumber	= '0' ;
113	$sErrorMsg		= '' ;
114
115	if ( isset( $_GET['NewFolderName'] ) )
116	{
117		$sNewFolderName = $_GET['NewFolderName'] ;
118		$sNewFolderName = SanitizeFolderName( $sNewFolderName ) ;
119
120		if ( strpos( $sNewFolderName, '..' ) !== FALSE )
121			$sErrorNumber = '102' ;		// Invalid folder name.
122		else
123		{
124			// Map the virtual path to the local server path of the current folder.
125			$sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'CreateFolder' ) ;
126
127			if ( is_writable( $sServerDir ) )
128			{
129				$sServerDir .= $sNewFolderName ;
130
131				$sErrorMsg = CreateServerFolder( $sServerDir ) ;
132
133				switch ( $sErrorMsg )
134				{
135					case '' :
136						$sErrorNumber = '0' ;
137						break ;
138					case 'Invalid argument' :
139					case 'No such file or directory' :
140						$sErrorNumber = '102' ;		// Path too long.
141						break ;
142					default :
143						$sErrorNumber = '110' ;
144						break ;
145				}
146			}
147			else
148				$sErrorNumber = '103' ;
149		}
150	}
151	else
152		$sErrorNumber = '102' ;
153
154	// Create the "Error" node.
155	echo '<Error number="' . $sErrorNumber . '" originalDescription="' . ConvertToXmlAttribute( $sErrorMsg ) . '" />' ;
156}
157
158function FileUpload( $resourceType, $currentFolder, $sCommand )
159{
160	if (!isset($_FILES)) {
161		global $_FILES;
162	}
163	$sErrorNumber = '0' ;
164	$sFileName = '' ;
165
166	if ( isset( $_FILES['NewFile'] ) && !is_null( $_FILES['NewFile']['tmp_name'] ) )
167	{
168		global $Config ;
169
170		$oFile = $_FILES['NewFile'] ;
171
172		// Map the virtual path to the local server path.
173		$sServerDir = ServerMapFolder( $resourceType, $currentFolder, $sCommand ) ;
174
175		// Get the uploaded file name.
176		$sFileName = $oFile['name'] ;
177		$sFileName = SanitizeFileName( $sFileName ) ;
178
179		$sOriginalFileName = $sFileName ;
180
181		// Get the extension.
182		$sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ;
183		$sExtension = strtolower( $sExtension ) ;
184
185		if ( isset( $Config['SecureImageUploads'] ) )
186		{
187			if ( !IsImageValid( $oFile['tmp_name'], $sExtension ) )
188			{
189				$sErrorNumber = '202' ;
190			}
191		}
192
193		if ( isset( $Config['HtmlExtensions'] ) )
194		{
195			if ( !IsHtmlExtension( $sExtension, $Config['HtmlExtensions'] ) && DetectHtml( $oFile['tmp_name'] ) )
196			{
197				$sErrorNumber = '202' ;
198			}
199		}
200
201		// Check if it is an allowed extension.
202		if ( !$sErrorNumber && IsAllowedExt( $sExtension, $resourceType ) )
203		{
204			$iCounter = 0 ;
205
206			while ( true )
207			{
208				$sFilePath = $sServerDir . $sFileName ;
209
210				if ( is_file( $sFilePath ) )
211				{
212					$iCounter++ ;
213					$sFileName = RemoveExtension( $sOriginalFileName ) . '(' . $iCounter . ').' . $sExtension ;
214					$sErrorNumber = '201' ;
215				}
216				else
217				{
218					move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;
219
220					if ( is_file( $sFilePath ) )
221					{
222						$oldumask = umask(0) ;
223						chmod( $sFilePath, 0777 ) ;
224						umask( $oldumask ) ;
225					}
226
227					break ;
228				}
229			}
230		}
231		else
232			$sErrorNumber = '202' ;
233	}
234	else
235		$sErrorNumber = '202' ;
236
237
238	$sFileUrl = CombinePaths( GetResourceTypePath( $resourceType, $sCommand ) , $currentFolder ) ;
239	$sFileUrl = CombinePaths( $sFileUrl, $sFileName ) ;
240
241	SendUploadResults( $sErrorNumber, $sFileUrl, $sFileName ) ;
242
243	exit ;
244}
245?>