Back to index

moin  1.9.0~rc2
connector.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 See config.py for configuration settings
00026 
00027 """
00028 import os
00029 
00030 from fckutil import *
00031 from fckcommands import *   # default command's implementation
00032 from fckoutput import *     # base http, xml and html output mixins
00033 from fckconnector import FCKeditorConnectorBase # import base connector
00034 import config as Config
00035 
00036 class FCKeditorConnector(   FCKeditorConnectorBase,
00037                                                  GetFoldersCommandMixin,
00038                                                  GetFoldersAndFilesCommandMixin,
00039                                                  CreateFolderCommandMixin,
00040                                                  UploadFileCommandMixin,
00041                                                  BaseHttpMixin, BaseXmlMixin, BaseHtmlMixin  ):
00042        "The Standard connector class."
00043        def doResponse(self):
00044               "Main function. Process the request, set headers and return a string as response."
00045               s = ""
00046               # Check if this connector is disabled
00047               if not(Config.Enabled):
00048                      return self.sendError(1, "This connector is disabled.  Please check the connector configurations in \"editor/filemanager/connectors/py/config.py\" and try again.")
00049               # Make sure we have valid inputs
00050               for key in ("Command","Type","CurrentFolder"):
00051                      if not self.request.has_key (key):
00052                             return
00053               # Get command, resource type and current folder
00054               command = self.request.get("Command")
00055               resourceType = self.request.get("Type")
00056               currentFolder = getCurrentFolder(self.request.get("CurrentFolder"))
00057               # Check for invalid paths
00058               if currentFolder is None:
00059                      if (command == "FileUpload"):
00060                             return self.sendUploadResults( errorNo = 102, customMsg = "" )
00061                      else:
00062                             return self.sendError(102, "")
00063 
00064               # Check if it is an allowed command
00065               if ( not command in Config.ConfigAllowedCommands ):
00066                      return self.sendError( 1, 'The %s command isn\'t allowed' % command )
00067 
00068               if ( not resourceType in Config.ConfigAllowedTypes  ):
00069                      return self.sendError( 1, 'Invalid type specified' )
00070 
00071               # Setup paths
00072               if command == "QuickUpload":
00073                      self.userFilesFolder = Config.QuickUploadAbsolutePath[resourceType]
00074                      self.webUserFilesFolder =  Config.QuickUploadPath[resourceType]
00075               else:
00076                      self.userFilesFolder = Config.FileTypesAbsolutePath[resourceType]
00077                      self.webUserFilesFolder = Config.FileTypesPath[resourceType]
00078 
00079               if not self.userFilesFolder: # no absolute path given (dangerous...)
00080                      self.userFilesFolder = mapServerPath(self.environ,
00081                                                                self.webUserFilesFolder)
00082               # Ensure that the directory exists.
00083               if not os.path.exists(self.userFilesFolder):
00084                      try:
00085                             self.createServerFolder( self.userFilesFolder )
00086                      except:
00087                             return self.sendError(1, "This connector couldn\'t access to local user\'s files directories.  Please check the UserFilesAbsolutePath in \"editor/filemanager/connectors/py/config.py\" and try again. ")
00088 
00089               # File upload doesn't have to return XML, so intercept here
00090               if (command == "FileUpload"):
00091                      return self.uploadFile(resourceType, currentFolder)
00092 
00093               # Create Url
00094               url = combinePaths( self.webUserFilesFolder, currentFolder )
00095 
00096               # Begin XML
00097               s += self.createXmlHeader(command, resourceType, currentFolder, url)
00098               # Execute the command
00099               selector = {"GetFolders": self.getFolders,
00100                                    "GetFoldersAndFiles": self.getFoldersAndFiles,
00101                                    "CreateFolder": self.createFolder,
00102                                    }
00103               s += selector[command](resourceType, currentFolder)
00104               s += self.createXmlFooter()
00105               return s
00106 
00107 # Running from command line (plain old CGI)
00108 if __name__ == '__main__':
00109        try:
00110               # Create a Connector Instance
00111               conn = FCKeditorConnector()
00112               data = conn.doResponse()
00113               for header in conn.headers:
00114                      print '%s: %s' % header
00115               print
00116               print data
00117        except:
00118               print "Content-Type: text/plain"
00119               print
00120               import cgi
00121               cgi.print_exception()