Back to index

moin  1.9.0~rc2
fckcommands.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 Connector for Python (CGI and WSGI).
00024 
00025 """
00026 
00027 import os
00028 try: # Windows needs stdio set for binary mode for file upload to work.
00029        import msvcrt
00030        msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
00031        msvcrt.setmode (1, os.O_BINARY) # stdout = 1
00032 except ImportError:
00033        pass
00034 
00035 from fckutil import *
00036 from fckoutput import *
00037 import config as Config
00038 
00039 class GetFoldersCommandMixin (object):
00040        def getFolders(self, resourceType, currentFolder):
00041               """
00042               Purpose: command to recieve a list of folders
00043               """
00044               # Map the virtual path to our local server
00045               serverPath = mapServerFolder(self.userFilesFolder,currentFolder)
00046               s = """<Folders>"""   # Open the folders node
00047               for someObject in os.listdir(serverPath):
00048                      someObjectPath = mapServerFolder(serverPath, someObject)
00049                      if os.path.isdir(someObjectPath):
00050                             s += """<Folder name="%s" />""" % (
00051                                           convertToXmlAttribute(someObject)
00052                                           )
00053               s += """</Folders>""" # Close the folders node
00054               return s
00055 
00056 class GetFoldersAndFilesCommandMixin (object):
00057        def getFoldersAndFiles(self, resourceType, currentFolder):
00058               """
00059               Purpose: command to recieve a list of folders and files
00060               """
00061               # Map the virtual path to our local server
00062               serverPath = mapServerFolder(self.userFilesFolder,currentFolder)
00063               # Open the folders / files node
00064               folders = """<Folders>"""
00065               files = """<Files>"""
00066               for someObject in os.listdir(serverPath):
00067                      someObjectPath = mapServerFolder(serverPath, someObject)
00068                      if os.path.isdir(someObjectPath):
00069                             folders += """<Folder name="%s" />""" % (
00070                                           convertToXmlAttribute(someObject)
00071                                           )
00072                      elif os.path.isfile(someObjectPath):
00073                             size = os.path.getsize(someObjectPath)
00074                             if size > 0:
00075                                    size = round(size/1024)
00076                                    if size < 1:
00077                                           size = 1
00078                             files += """<File name="%s" size="%d" />""" % (
00079                                           convertToXmlAttribute(someObject),
00080                                           size
00081                                           )
00082               # Close the folders / files node
00083               folders += """</Folders>"""
00084               files += """</Files>"""
00085               return folders + files
00086 
00087 class CreateFolderCommandMixin (object):
00088        def createFolder(self, resourceType, currentFolder):
00089               """
00090               Purpose: command to create a new folder
00091               """
00092               errorNo = 0; errorMsg ='';
00093               if self.request.has_key("NewFolderName"):
00094                      newFolder = self.request.get("NewFolderName", None)
00095                      newFolder = sanitizeFolderName (newFolder)
00096                      try:
00097                             newFolderPath = mapServerFolder(self.userFilesFolder, combinePaths(currentFolder, newFolder))
00098                             self.createServerFolder(newFolderPath)
00099                      except Exception, e:
00100                             errorMsg = str(e).decode('iso-8859-1').encode('utf-8') # warning with encodigns!!!
00101                             if hasattr(e,'errno'):
00102                                    if e.errno==17: #file already exists
00103                                           errorNo=0
00104                                    elif e.errno==13: # permission denied
00105                                           errorNo = 103
00106                                    elif e.errno==36 or e.errno==2 or e.errno==22: # filename too long / no such file / invalid name
00107                                           errorNo = 102
00108                             else:
00109                                    errorNo = 110
00110               else:
00111                      errorNo = 102
00112               return self.sendErrorNode ( errorNo, errorMsg )
00113 
00114        def createServerFolder(self, folderPath):
00115               "Purpose: physically creates a folder on the server"
00116               # No need to check if the parent exists, just create all hierachy
00117 
00118               try:
00119                      permissions = Config.ChmodOnFolderCreate
00120                      if not permissions:
00121                             os.makedirs(folderPath)
00122               except AttributeError: #ChmodOnFolderCreate undefined
00123                      permissions = 0755
00124 
00125               if permissions:
00126                      oldumask = os.umask(0)
00127                      os.makedirs(folderPath,mode=0755)
00128                      os.umask( oldumask )
00129 
00130 class UploadFileCommandMixin (object):
00131        def uploadFile(self, resourceType, currentFolder):
00132               """
00133               Purpose: command to upload files to server (same as FileUpload)
00134               """
00135               errorNo = 0
00136               if self.request.has_key("NewFile"):
00137                      # newFile has all the contents we need
00138                      newFile = self.request.get("NewFile", "")
00139                      # Get the file name
00140                      newFileName = newFile.filename
00141                      newFileName = sanitizeFileName( newFileName )
00142                      newFileNameOnly = removeExtension(newFileName)
00143                      newFileExtension = getExtension(newFileName).lower()
00144                      allowedExtensions = Config.AllowedExtensions[resourceType]
00145                      deniedExtensions = Config.DeniedExtensions[resourceType]
00146 
00147                      if (allowedExtensions):
00148                             # Check for allowed
00149                             isAllowed = False
00150                             if (newFileExtension in allowedExtensions):
00151                                    isAllowed = True
00152                      elif (deniedExtensions):
00153                             # Check for denied
00154                             isAllowed = True
00155                             if (newFileExtension in deniedExtensions):
00156                                    isAllowed = False
00157                      else:
00158                             # No extension limitations
00159                             isAllowed = True
00160 
00161                      if (isAllowed):
00162                             # Upload to operating system
00163                             # Map the virtual path to the local server path
00164                             currentFolderPath = mapServerFolder(self.userFilesFolder, currentFolder)
00165                             i = 0
00166                             while (True):
00167                                    newFilePath = os.path.join (currentFolderPath,newFileName)
00168                                    if os.path.exists(newFilePath):
00169                                           i += 1
00170                                           newFileName = "%s(%d).%s" % (
00171                                                         newFileNameOnly, i, newFileExtension
00172                                                         )
00173                                           errorNo= 201 # file renamed
00174                                    else:
00175                                           # Read file contents and write to the desired path (similar to php's move_uploaded_file)
00176                                           fout = file(newFilePath, 'wb')
00177                                           while (True):
00178                                                  chunk = newFile.file.read(100000)
00179                                                  if not chunk: break
00180                                                  fout.write (chunk)
00181                                           fout.close()
00182 
00183                                           if os.path.exists ( newFilePath ):
00184                                                  doChmod = False
00185                                                  try:
00186                                                         doChmod = Config.ChmodOnUpload
00187                                                         permissions = Config.ChmodOnUpload
00188                                                  except AttributeError: #ChmodOnUpload undefined
00189                                                         doChmod = True
00190                                                         permissions = 0755
00191                                                  if ( doChmod ):
00192                                                         oldumask = os.umask(0)
00193                                                         os.chmod( newFilePath, permissions )
00194                                                         os.umask( oldumask )
00195 
00196                                           newFileUrl = combinePaths(self.webUserFilesFolder, currentFolder) + newFileName
00197 
00198                                           return self.sendUploadResults( errorNo , newFileUrl, newFileName )
00199                      else:
00200                             return self.sendUploadResults( errorNo = 202, customMsg = "" )
00201               else:
00202                      return self.sendUploadResults( errorNo = 202, customMsg = "No File" )