source: sandbox/filemanager/tp/fckeditor/editor/filemanager/connectors/py/fckutil.py @ 1575

Revision 1575, 4.4 KB checked in by amuller, 14 years ago (diff)

Ticket #597 - Implentação, melhorias do modulo gerenciador de arquivos

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2
3"""
4FCKeditor - The text editor for Internet - http://www.fckeditor.net
5Copyright (C) 2003-2009 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 . \ / | : ? * " < > and control characters
65        return re.sub( '\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[\x00-\x1f\x7f-\x9f]', '_', 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 ( '\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[\x00-\x1f\x7f-\x9f]/', '_', 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 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
101def 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
107def mapServerFolder(resourceTypePath, folderPath):
108        return combinePaths ( resourceTypePath  , folderPath )
109
110def 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 ]
Note: See TracBrowser for help on using the repository browser.