PYTHON   9

fckutil.py

Guest on 25th July 2021 04:39:08 PM

  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( '(?u)\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[^\u0000-\u001f\u007f-\u009f]', '_', 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 ( '(?u)/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[^\u0000-\u001f\u007f-\u009f]/', '_', 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.         return currentFolder
  96.  
  97. def 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.  
  103. def mapServerFolder(resourceTypePath, folderPath):
  104.         return combinePaths ( resourceTypePath  , folderPath )
  105.  
  106. def 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 ]

Raw Paste


Login or Register to edit or fork this paste. It's free.