1#!/usr/bin/env python 2 3""" 4FCKeditor - The text editor for Internet - http://www.fckeditor.net 5Copyright (C) 2003-2007 Frederico Caldeira Knabben 6 7== BEGIN LICENSE == 8 9Licensed under the terms of any of the following licenses at your 10choice: 11 12- GNU General Public License Version 2 or later (the "GPL") 13http://www.gnu.org/licenses/gpl.html 14 15- GNU Lesser General Public License Version 2.1 or later (the "LGPL") 16http://www.gnu.org/licenses/lgpl.html 17 18- Mozilla Public License Version 1.1 or later (the "MPL") 19http://www.mozilla.org/MPL/MPL-1.1.html 20 21== END LICENSE == 22 23Utility functions for the File Manager Connector for Python 24 25""" 26 27import string, re 28import os 29import config as Config 30 31# Generic manipulation functions 32 33def removeExtension(fileName): 34 index = fileName.rindex(".") 35 newFileName = fileName[0:index] 36 return newFileName 37 38def getExtension(fileName): 39 index = fileName.rindex(".") + 1 40 fileExtension = fileName[index:] 41 return fileExtension 42 43def removeFromStart(string, char): 44 return string.lstrip(char) 45 46def removeFromEnd(string, char): 47 return string.rstrip(char) 48 49# Path functions 50 51def combinePaths( basePath, folder ): 52 return removeFromEnd( basePath, '/' ) + '/' + removeFromStart( folder, '/' ) 53 54def 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 62def sanitizeFolderName( newFolderName ): 63 "Do a cleanup of the folder name to avoid possible problems" 64 # Remove . \ / | : ? * 65 return re.sub( '\\.|\\\\|\\/|\\||\\:|\\?|\\*', '_', newFolderName ) 66 67def 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 ( '/\\\\|\\/|\\||\\:|\\?|\\*/', '_', newFileName ) 76 77def 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: 93 return None 94 95 return currentFolder 96 97def mapServerPath( environ, url): 98 " Emulate the asp Server.mapPath function. Given an url path return the physical directory that it corresponds to " 99 # This isn't correct but for the moment there's no other solution 100 # If this script is under a virtual directory or symlink it will detect the problem and stop 101 return combinePaths( getRootPath(environ), url ) 102 103def mapServerFolder(resourceTypePath, folderPath): 104 return combinePaths ( resourceTypePath , folderPath ) 105 106def getRootPath(environ): 107 "Purpose: returns the root path on the server" 108 # WARNING: this may not be thread safe, and doesn't work w/ VirtualServer/mod_python 109 # Use Config.UserFilesAbsolutePath instead 110 111 if environ.has_key('DOCUMENT_ROOT'): 112 return environ['DOCUMENT_ROOT'] 113 else: 114 realPath = os.path.realpath( './' ) 115 selfPath = environ['SCRIPT_FILENAME'] 116 selfPath = selfPath [ : selfPath.rfind( '/' ) ] 117 selfPath = selfPath.replace( '/', os.path.sep) 118 119 position = realPath.find(selfPath) 120 121 # This can check only that this script isn't run from a virtual dir 122 # But it avoids the problems that arise if it isn't checked 123 raise realPath 124 if ( position < 0 or position <> len(realPath) - len(selfPath) or realPath[ : position ]==''): 125 raise Exception('Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/py/config.py".') 126 return realPath[ : position ] 127 128