Back to index

moin  1.9.0~rc2
fckutil.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 """
00004 FCKeditor - The text editor for Internet - http://www.fckeditor.net
00005 Copyright (C) 2003-2009 Frederico Caldeira Knabben
00006 
00007 == BEGIN LICENSE ==
00008 
00009 Licensed under the terms of any of the following licenses at your
00010 choice:
00011 
00012 - GNU General Public License Version 2 or later (the "GPL")
00013 http://www.gnu.org/licenses/gpl.html
00014 
00015 - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
00016 http://www.gnu.org/licenses/lgpl.html
00017 
00018 - Mozilla Public License Version 1.1 or later (the "MPL")
00019 http://www.mozilla.org/MPL/MPL-1.1.html
00020 
00021 == END LICENSE ==
00022 
00023 Utility functions for the File Manager Connector for Python
00024 
00025 """
00026 
00027 import string, re
00028 import os
00029 import config as Config
00030 
00031 # Generic manipulation functions
00032 
00033 def removeExtension(fileName):
00034        index = fileName.rindex(".")
00035        newFileName = fileName[0:index]
00036        return newFileName
00037 
00038 def getExtension(fileName):
00039        index = fileName.rindex(".") + 1
00040        fileExtension = fileName[index:]
00041        return fileExtension
00042 
00043 def removeFromStart(string, char):
00044        return string.lstrip(char)
00045 
00046 def removeFromEnd(string, char):
00047        return string.rstrip(char)
00048 
00049 # Path functions
00050 
00051 def combinePaths( basePath, folder ):
00052        return removeFromEnd( basePath, '/' ) + '/' + removeFromStart( folder, '/' )
00053 
00054 def getFileName(filename):
00055        " Purpose: helper function to extrapolate the filename "
00056        for splitChar in ["/", "\\"]:
00057               array = filename.split(splitChar)
00058               if (len(array) > 1):
00059                      filename = array[-1]
00060        return filename
00061 
00062 def sanitizeFolderName( newFolderName ):
00063        "Do a cleanup of the folder name to avoid possible problems"
00064        # Remove . \ / | : ? * " < > and control characters
00065        return re.sub( '\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[\x00-\x1f\x7f-\x9f]', '_', newFolderName )
00066 
00067 def sanitizeFileName( newFileName ):
00068        "Do a cleanup of the file name to avoid possible problems"
00069        # Replace dots in the name with underscores (only one dot can be there... security issue).
00070        if ( Config.ForceSingleExtension ): # remove dots
00071               newFileName = re.sub ( '\\.(?![^.]*$)', '_', newFileName ) ;
00072        newFileName = newFileName.replace('\\','/')             # convert windows to unix path
00073        newFileName = os.path.basename (newFileName)     # strip directories
00074        # Remove \ / | : ? *
00075        return re.sub ( '\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[\x00-\x1f\x7f-\x9f]/', '_', newFileName )
00076 
00077 def getCurrentFolder(currentFolder):
00078        if not currentFolder:
00079               currentFolder = '/'
00080 
00081        # Check the current folder syntax (must begin and end with a slash).
00082        if (currentFolder[-1] <> "/"):
00083               currentFolder += "/"
00084        if (currentFolder[0] <> "/"):
00085               currentFolder = "/" + currentFolder
00086 
00087        # Ensure the folder path has no double-slashes
00088        while '//' in currentFolder:
00089               currentFolder = currentFolder.replace('//','/')
00090 
00091        # Check for invalid folder paths (..)
00092        if '..' in currentFolder or '\\' in currentFolder:
00093               return None
00094 
00095        # Check for invalid folder paths (..)
00096        if re.search( '(/\\.)|(//)|([\\\\:\\*\\?\\""\\<\\>\\|]|[\x00-\x1F]|[\x7f-\x9f])', currentFolder ):
00097               return None
00098 
00099        return currentFolder
00100 
00101 def mapServerPath( environ, url):
00102        " Emulate the asp Server.mapPath function. Given an url path return the physical directory that it corresponds to "
00103        # This isn't correct but for the moment there's no other solution
00104        # If this script is under a virtual directory or symlink it will detect the problem and stop
00105        return combinePaths( getRootPath(environ), url )
00106 
00107 def mapServerFolder(resourceTypePath, folderPath):
00108        return combinePaths ( resourceTypePath  , folderPath )
00109 
00110 def getRootPath(environ):
00111        "Purpose: returns the root path on the server"
00112        # WARNING: this may not be thread safe, and doesn't work w/ VirtualServer/mod_python
00113        # Use Config.UserFilesAbsolutePath instead
00114 
00115        if environ.has_key('DOCUMENT_ROOT'):
00116               return environ['DOCUMENT_ROOT']
00117        else:
00118               realPath = os.path.realpath( './' )
00119               selfPath = environ['SCRIPT_FILENAME']
00120               selfPath = selfPath [ :  selfPath.rfind( '/'  ) ]
00121               selfPath = selfPath.replace( '/', os.path.sep)
00122 
00123               position = realPath.find(selfPath)
00124 
00125               # This can check only that this script isn't run from a virtual dir
00126               # But it avoids the problems that arise if it isn't checked
00127               raise realPath
00128               if ( position < 0 or position <> len(realPath) - len(selfPath) or realPath[ : position ]==''):
00129                      raise Exception('Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/py/config.py".')
00130               return realPath[ : position ]