Project

General

Profile

1
<?php
2
/*
3
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
4
 * Copyright (C) 2003-2008 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
function CombinePaths( $sBasePath, $sFolder )
25
{
26
	return RemoveFromEnd( $sBasePath, '/' ) . '/' . RemoveFromStart( $sFolder, '/' ) ;
27
}
28
function GetResourceTypePath( $resourceType, $sCommand )
29
{
30
	global $Config ;
31

    
32
	if ( $sCommand == "QuickUpload")
33
		return $Config['QuickUploadPath'][$resourceType] ;
34
	else
35
		return $Config['FileTypesPath'][$resourceType] ;
36
}
37

    
38
function GetResourceTypeDirectory( $resourceType, $sCommand )
39
{
40
	global $Config ;
41
	if ( $sCommand == "QuickUpload")
42
	{
43
		if ( strlen( $Config['QuickUploadAbsolutePath'][$resourceType] ) > 0 )
44
			return $Config['QuickUploadAbsolutePath'][$resourceType] ;
45

    
46
		// Map the "UserFiles" path to a local directory.
47
		return Server_MapPath( $Config['QuickUploadPath'][$resourceType] ) ;
48
	}
49
	else
50
	{
51
		if ( strlen( $Config['FileTypesAbsolutePath'][$resourceType] ) > 0 )
52
			return $Config['FileTypesAbsolutePath'][$resourceType] ;
53

    
54
		// Map the "UserFiles" path to a local directory.
55
		return Server_MapPath( $Config['FileTypesPath'][$resourceType] ) ;
56
	}
57
}
58

    
59
function GetUrlFromPath( $resourceType, $folderPath, $sCommand )
60
{
61
	return CombinePaths( GetResourceTypePath( $resourceType, $sCommand ), $folderPath ) ;
62
}
63

    
64
function RemoveExtension( $fileName )
65
{
66
	return substr( $fileName, 0, strrpos( $fileName, '.' ) ) ;
67
}
68

    
69
function ServerMapFolder( $resourceType, $folderPath, $sCommand )
70
{
71
	// Get the resource type directory.
72
	$sResourceTypePath = GetResourceTypeDirectory( $resourceType, $sCommand ) ;
73

    
74
	// Ensure that the directory exists.
75
	$sErrorMsg = CreateServerFolder( $sResourceTypePath ) ;
76
	if ( $sErrorMsg != '' )
77
		SendError( 1, "Error creating folder \"{$sResourceTypePath}\" ({$sErrorMsg})" ) ;
78

    
79
	// Return the resource type directory combined with the required path.
80
	return CombinePaths( $sResourceTypePath , $folderPath ) ;
81
}
82

    
83
function GetParentFolder( $folderPath )
84
{
85
	$sPattern = "-[/\\\\][^/\\\\]+[/\\\\]?$-" ;
86
	return preg_replace( $sPattern, '', $folderPath ) ;
87
}
88

    
89
function CreateServerFolder( $folderPath, $lastFolder = null )
90
{
91
	global $Config ;
92
	$sParent = GetParentFolder( $folderPath ) ;
93

    
94
	// Ensure the folder path has no double-slashes, or mkdir may fail on certain platforms
95
	while ( strpos($folderPath, '//') !== false )
96
	{
97
		$folderPath = str_replace( '//', '/', $folderPath ) ;
98
	}
99

    
100
	// Check if the parent exists, or create it.
101
	if ( !file_exists( $sParent ) )
102
	{
103
		//prevents agains infinite loop when we can't create root folder
104
		if ( !is_null( $lastFolder ) && $lastFolder === $sParent) {
105
			return "Can't create $folderPath directory" ;
106
		}
107

    
108
		$sErrorMsg = CreateServerFolder( $sParent, $folderPath ) ;
109
		if ( $sErrorMsg != '' )
110
			return $sErrorMsg ;
111
	}
112

    
113
	if ( !file_exists( $folderPath ) )
114
	{
115
		// Turn off all error reporting.
116
		error_reporting( 0 ) ;
117

    
118
		$php_errormsg = '' ;
119
		// Enable error tracking to catch the error.
120
		ini_set( 'track_errors', '1' ) ;
121

    
122
		if ( isset( $Config['ChmodOnFolderCreate'] ) && !$Config['ChmodOnFolderCreate'] )
123
		{
124
			mkdir( $folderPath ) ;
125
		}
126
		else
127
		{
128
			$permissions = 0777 ;
129
			if ( isset( $Config['ChmodOnFolderCreate'] ) )
130
			{
131
				$permissions = $Config['ChmodOnFolderCreate'] ;
132
			}
133
			// To create the folder with 0777 permissions, we need to set umask to zero.
134
			$oldumask = umask(0) ;
135
			mkdir( $folderPath, $permissions ) ;
136
			umask( $oldumask ) ;
137
		}
138

    
139
		$sErrorMsg = $php_errormsg ;
140

    
141
		// Restore the configurations.
142
		ini_restore( 'track_errors' ) ;
143
		ini_restore( 'error_reporting' ) ;
144

    
145
		return $sErrorMsg ;
146
	}
147
	else
148
		return '' ;
149
}
150

    
151
function GetRootPath()
152
{
153
	if (!isset($_SERVER)) {
154
		global $_SERVER;
155
	}
156
	$sRealPath = realpath( './' ) ;
157

    
158
	$sSelfPath = $_SERVER['PHP_SELF'] ;
159
	$sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ;
160

    
161
	$sSelfPath = str_replace( '/', DIRECTORY_SEPARATOR, $sSelfPath ) ;
162

    
163
	$position = strpos( $sRealPath, $sSelfPath ) ;
164

    
165
	// This can check only that this script isn't run from a virtual dir
166
	// But it avoids the problems that arise if it isn't checked
167
	if ( $position === false || $position <> strlen( $sRealPath ) - strlen( $sSelfPath ) )
168
		SendError( 1, 'Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/php/config.php".' ) ;
169

    
170
	return substr( $sRealPath, 0, $position ) ;
171
}
172

    
173
// Emulate the asp Server.mapPath function.
174
// given an url path return the physical directory that it corresponds to
175
function Server_MapPath( $path )
176
{
177
	// This function is available only for Apache
178
	if ( function_exists( 'apache_lookup_uri' ) )
179
	{
180
		$info = apache_lookup_uri( $path ) ;
181
		return $info->filename . $info->path_info ;
182
	}
183

    
184
	// This isn't correct but for the moment there's no other solution
185
	// If this script is under a virtual directory or symlink it will detect the problem and stop
186
	return GetRootPath() . $path ;
187
}
188

    
189
function IsAllowedExt( $sExtension, $resourceType )
190
{
191
	global $Config ;
192
	// Get the allowed and denied extensions arrays.
193
	$arAllowed	= $Config['AllowedExtensions'][$resourceType] ;
194
	$arDenied	= $Config['DeniedExtensions'][$resourceType] ;
195

    
196
	if ( count($arAllowed) > 0 && !in_array( $sExtension, $arAllowed ) )
197
		return false ;
198

    
199
	if ( count($arDenied) > 0 && in_array( $sExtension, $arDenied ) )
200
		return false ;
201

    
202
	return true ;
203
}
204

    
205
function IsAllowedType( $resourceType )
206
{
207
	global $Config ;
208
	if ( !in_array( $resourceType, $Config['ConfigAllowedTypes'] ) )
209
		return false ;
210

    
211
	return true ;
212
}
213

    
214
function IsAllowedCommand( $sCommand )
215
{
216
	global $Config ;
217

    
218
	if ( !in_array( $sCommand, $Config['ConfigAllowedCommands'] ) )
219
		return false ;
220

    
221
	return true ;
222
}
223

    
224
function GetCurrentFolder()
225
{
226
	if (!isset($_GET)) {
227
		global $_GET;
228
	}
229
	$sCurrentFolder	= isset( $_GET['CurrentFolder'] ) ? $_GET['CurrentFolder'] : '/' ;
230

    
231
	// Check the current folder syntax (must begin and start with a slash).
232
	if ( !preg_match( '|/$|', $sCurrentFolder ) )
233
		$sCurrentFolder .= '/' ;
234
	if ( strpos( $sCurrentFolder, '/' ) !== 0 )
235
		$sCurrentFolder = '/' . $sCurrentFolder ;
236

    
237
	// Ensure the folder path has no double-slashes
238
	while ( strpos ($sCurrentFolder, '//') !== false ) {
239
		$sCurrentFolder = str_replace ('//', '/', $sCurrentFolder) ;
240
	}
241

    
242
	// Check for invalid folder paths (..)
243
	if ( strpos( $sCurrentFolder, '..' ) || strpos( $sCurrentFolder, "\\" ))
244
		SendError( 102, '' ) ;
245

    
246
	return $sCurrentFolder ;
247
}
248

    
249
// Do a cleanup of the folder name to avoid possible problems
250
function SanitizeFolderName( $sNewFolderName )
251
{
252
	$sNewFolderName = stripslashes( $sNewFolderName ) ;
253

    
254
	// Remove . \ / | : ? * " < >
255
	$sNewFolderName = preg_replace( '/\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFolderName ) ;
256

    
257
	return $sNewFolderName ;
258
}
259

    
260
// Do a cleanup of the file name to avoid possible problems
261
function SanitizeFileName( $sNewFileName )
262
{
263
	global $Config ;
264

    
265
	$sNewFileName = stripslashes( $sNewFileName ) ;
266

    
267
	// Replace dots in the name with underscores (only one dot can be there... security issue).
268
	if ( $Config['ForceSingleExtension'] )
269
		$sNewFileName = preg_replace( '/\\.(?![^.]*$)/', '_', $sNewFileName ) ;
270

    
271
	// Remove \ / | : ? * " < >
272
	$sNewFileName = preg_replace( '/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFileName ) ;
273

    
274
	return $sNewFileName ;
275
}
276

    
277
// This is the function that sends the results of the uploading process.
278
function SendUploadResults( $errorNumber, $fileUrl = '', $fileName = '', $customMsg = '' )
279
{
280
	echo <<<EOF
281
<script type="text/javascript">
282
(function()
283
{
284
	var d = document.domain ;
285

    
286
	while ( true )
287
	{
288
		// Test if we can access a parent property.
289
		try
290
		{
291
			var test = window.top.opener.document.domain ;
292
			break ;
293
		}
294
		catch( e ) {}
295

    
296
		// Remove a domain part: www.mytest.example.com => mytest.example.com => example.com ...
297
		d = d.replace( /.*?(?:\.|$)/, '' ) ;
298

    
299
		if ( d.length == 0 )
300
			break ;		// It was not able to detect the domain.
301

    
302
		try
303
		{
304
			document.domain = d ;
305
		}
306
		catch (e)
307
		{
308
			break ;
309
		}
310
	}
311
})() ;
312

    
313
EOF;
314
	$rpl = array( '\\' => '\\\\', '"' => '\\"' ) ;
315
	echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . strtr( $fileUrl, $rpl ) . '","' . strtr( $fileName, $rpl ) . '", "' . strtr( $customMsg, $rpl ) . '") ;' ;
316
	echo '</script>' ;
317
	exit ;
318
}
319

    
320
?>
(7-7/10)