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")
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
23Connector for Python (CGI and WSGI).
24
25See config.py for configuration settings
26
27"""
28import os
29
30from fckutil import *
31from fckcommands import * 	# default command's implementation
32from fckoutput import * 	# base http, xml and html output mixins
33from fckconnector import FCKeditorConnectorBase # import base connector
34import config as Config
35
36class FCKeditorConnector(	FCKeditorConnectorBase,
37							GetFoldersCommandMixin,
38							GetFoldersAndFilesCommandMixin,
39							CreateFolderCommandMixin,
40							UploadFileCommandMixin,
41							BaseHttpMixin, BaseXmlMixin, BaseHtmlMixin  ):
42	"The Standard connector class."
43	def doResponse(self):
44		"Main function. Process the request, set headers and return a string as response."
45		s = ""
46		# Check if this connector is disabled
47		if not(Config.Enabled):
48			return self.sendError(1, "This connector is disabled.  Please check the connector configurations in \"editor/filemanager/connectors/py/config.py\" and try again.")
49		# Make sure we have valid inputs
50		for key in ("Command","Type","CurrentFolder"):
51			if not self.request.has_key (key):
52				return
53		# Get command, resource type and current folder
54		command = self.request.get("Command")
55		resourceType = self.request.get("Type")
56		currentFolder = getCurrentFolder(self.request.get("CurrentFolder"))
57		# Check for invalid paths
58		if currentFolder is None:
59			return self.sendError(102, "")
60
61		# Check if it is an allowed command
62		if ( not command in Config.ConfigAllowedCommands ):
63			return self.sendError( 1, 'The %s command isn\'t allowed' % command )
64
65		if ( not resourceType in Config.ConfigAllowedTypes  ):
66			return self.sendError( 1, 'Invalid type specified' )
67
68		# Setup paths
69		if command == "QuickUpload":
70			self.userFilesFolder = Config.QuickUploadAbsolutePath[resourceType]
71			self.webUserFilesFolder =  Config.QuickUploadPath[resourceType]
72		else:
73			self.userFilesFolder = Config.FileTypesAbsolutePath[resourceType]
74			self.webUserFilesFolder = Config.FileTypesPath[resourceType]
75
76		if not self.userFilesFolder: # no absolute path given (dangerous...)
77			self.userFilesFolder = mapServerPath(self.environ,
78									self.webUserFilesFolder)
79		# Ensure that the directory exists.
80		if not os.path.exists(self.userFilesFolder):
81			try:
82				self.createServerFoldercreateServerFolder( self.userFilesFolder )
83			except:
84				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. ")
85
86		# File upload doesn't have to return XML, so intercept here
87		if (command == "FileUpload"):
88			return self.uploadFile(resourceType, currentFolder)
89
90		# Create Url
91		url = combinePaths( self.webUserFilesFolder, currentFolder )
92
93		# Begin XML
94		s += self.createXmlHeader(command, resourceType, currentFolder, url)
95		# Execute the command
96		selector = {"GetFolders": self.getFolders,
97					"GetFoldersAndFiles": self.getFoldersAndFiles,
98					"CreateFolder": self.createFolder,
99					}
100		s += selector[command](resourceType, currentFolder)
101		s += self.createXmlFooter()
102		return s
103
104# Running from command line (plain old CGI)
105if __name__ == '__main__':
106	try:
107		# Create a Connector Instance
108		conn = FCKeditorConnector()
109		data = conn.doResponse()
110		for header in conn.headers:
111			print '%s: %s' % header
112		print
113		print data
114	except:
115		print "Content-Type: text/plain"
116		print
117		import cgi
118		cgi.print_exception()
119