1
|
#!/usr/bin/env python
|
2
|
|
3
|
"""
|
4
|
FCKeditor - The text editor for Internet - http://www.fckeditor.net
|
5
|
Copyright (C) 2003-2009 Frederico Caldeira Knabben
|
6
|
|
7
|
== BEGIN LICENSE ==
|
8
|
|
9
|
Licensed under the terms of any of the following licenses at your
|
10
|
choice:
|
11
|
|
12
|
- GNU General Public License Version 2 or later (the "GPL")
|
13
|
http://www.gnu.org/licenses/gpl.html
|
14
|
|
15
|
- GNU Lesser General Public License Version 2.1 or later (the "LGPL")
|
16
|
http://www.gnu.org/licenses/lgpl.html
|
17
|
|
18
|
- Mozilla Public License Version 1.1 or later (the "MPL")
|
19
|
http://www.mozilla.org/MPL/MPL-1.1.html
|
20
|
|
21
|
== END LICENSE ==
|
22
|
|
23
|
Utility functions for the File Manager Connector for Python
|
24
|
|
25
|
"""
|
26
|
|
27
|
import string, re
|
28
|
import os
|
29
|
import config as Config
|
30
|
|
31
|
# Generic manipulation functions
|
32
|
|
33
|
def removeExtension(fileName):
|
34
|
index = fileName.rindex(".")
|
35
|
newFileName = fileName[0:index]
|
36
|
return newFileName
|
37
|
|
38
|
def getExtension(fileName):
|
39
|
index = fileName.rindex(".") + 1
|
40
|
fileExtension = fileName[index:]
|
41
|
return fileExtension
|
42
|
|
43
|
def removeFromStart(string, char):
|
44
|
return string.lstrip(char)
|
45
|
|
46
|
def removeFromEnd(string, char):
|
47
|
return string.rstrip(char)
|
48
|
|
49
|
# Path functions
|
50
|
|
51
|
def combinePaths( basePath, folder ):
|
52
|
return removeFromEnd( basePath, '/' ) + '/' + removeFromStart( folder, '/' )
|
53
|
|
54
|
def getFileName(filename):
|
55
|
" Purpose: helper function to extrapolate the filename "
|
56
|
for splitChar in ["/", "\\"]:
|
57
|
array = filename.split(splitChar)
|
58
|
if (len(array) > 1):
|
59
|
filename = array[-1]
|
60
|
return filename
|
61
|
|
62
|
def sanitizeFolderName( newFolderName ):
|
63
|
"Do a cleanup of the folder name to avoid possible problems"
|
64
|
# Remove . \ / | : ? * " < > and control characters
|
65
|
return re.sub( '\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[\x00-\x1f\x7f-\x9f]', '_', newFolderName )
|
66
|
|
67
|
def sanitizeFileName( newFileName ):
|
68
|
"Do a cleanup of the file name to avoid possible problems"
|
69
|
# Replace dots in the name with underscores (only one dot can be there... security issue).
|
70
|
if ( Config.ForceSingleExtension ): # remove dots
|
71
|
newFileName = re.sub ( '\\.(?![^.]*$)', '_', newFileName ) ;
|
72
|
newFileName = newFileName.replace('\\','/') # convert windows to unix path
|
73
|
newFileName = os.path.basename (newFileName) # strip directories
|
74
|
# Remove \ / | : ? *
|
75
|
return re.sub ( '\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[\x00-\x1f\x7f-\x9f]/', '_', newFileName )
|
76
|
|
77
|
def getCurrentFolder(currentFolder):
|
78
|
if not currentFolder:
|
79
|
currentFolder = '/'
|
80
|
|
81
|
# Check the current folder syntax (must begin and end with a slash).
|
82
|
if (currentFolder[-1] <> "/"):
|
83
|
currentFolder += "/"
|
84
|
if (currentFolder[0] <> "/"):
|
85
|
currentFolder = "/" + currentFolder
|
86
|
|
87
|
# Ensure the folder path has no double-slashes
|
88
|
while '//' in currentFolder:
|
89
|
currentFolder = currentFolder.replace('//','/')
|
90
|
|
91
|
# Check for invalid folder paths (..)
|
92
|
if '..' in currentFolder or '\\' in currentFolder:
|
93
|
return None
|
94
|
|
95
|
# Check for invalid folder paths (..)
|
96
|
if re.search( '(/\\.)|(//)|([\\\\:\\*\\?\\""\\<\\>\\|]|[\x00-\x1F]|[\x7f-\x9f])', currentFolder ):
|
97
|
return None
|
98
|
|
99
|
return currentFolder
|
100
|
|
101
|
def mapServerPath( environ, url):
|
102
|
" Emulate the asp Server.mapPath function. Given an url path return the physical directory that it corresponds to "
|
103
|
# This isn't correct but for the moment there's no other solution
|
104
|
# If this script is under a virtual directory or symlink it will detect the problem and stop
|
105
|
return combinePaths( getRootPath(environ), url )
|
106
|
|
107
|
def mapServerFolder(resourceTypePath, folderPath):
|
108
|
return combinePaths ( resourceTypePath , folderPath )
|
109
|
|
110
|
def getRootPath(environ):
|
111
|
"Purpose: returns the root path on the server"
|
112
|
# WARNING: this may not be thread safe, and doesn't work w/ VirtualServer/mod_python
|
113
|
# Use Config.UserFilesAbsolutePath instead
|
114
|
|
115
|
if environ.has_key('DOCUMENT_ROOT'):
|
116
|
return environ['DOCUMENT_ROOT']
|
117
|
else:
|
118
|
realPath = os.path.realpath( './' )
|
119
|
selfPath = environ['SCRIPT_FILENAME']
|
120
|
selfPath = selfPath [ : selfPath.rfind( '/' ) ]
|
121
|
selfPath = selfPath.replace( '/', os.path.sep)
|
122
|
|
123
|
position = realPath.find(selfPath)
|
124
|
|
125
|
# This can check only that this script isn't run from a virtual dir
|
126
|
# But it avoids the problems that arise if it isn't checked
|
127
|
raise realPath
|
128
|
if ( position < 0 or position <> len(realPath) - len(selfPath) or realPath[ : position ]==''):
|
129
|
raise Exception('Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/py/config.py".')
|
130
|
return realPath[ : position ]
|