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")
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
23This is the "File Uploader" for Python
24
25"""
26import os
27
28from fckutil import *
29from fckcommands import * 	# default command's implementation
30from fckconnector import FCKeditorConnectorBase # import base connector
31import config as Config
32
33class FCKeditorQuickUpload(	FCKeditorConnectorBase,
34							UploadFileCommandMixin,
35							BaseHttpMixin, BaseHtmlMixin):
36	def doResponse(self):
37		"Main function. Process the request, set headers and return a string as response."
38		# Check if this connector is disabled
39		if not(Config.Enabled):
40			return self.sendUploadResults(1, "This file uploader is disabled. Please check the \"editor/filemanager/connectors/py/config.py\"")
41		command = 'QuickUpload'
42		# The file type (from the QueryString, by default 'File').
43		resourceType  = self.request.get('Type','File')
44		currentFolder = getCurrentFolder(self.request.get("CurrentFolder",""))
45		# Check for invalid paths
46		if currentFolder is None:
47			return self.sendUploadResults(102, '', '', "")
48
49		# Check if it is an allowed command
50		if ( not command in Config.ConfigAllowedCommands ):
51			return self.sendUploadResults( 1, '', '', 'The %s command isn\'t allowed' % command )
52
53		if ( not resourceType in Config.ConfigAllowedTypes  ):
54			return self.sendUploadResults( 1, '', '', 'Invalid type specified' )
55
56		# Setup paths
57		self.userFilesFolder = Config.QuickUploadAbsolutePath[resourceType]
58		self.webUserFilesFolder =  Config.QuickUploadPath[resourceType]
59		if not self.userFilesFolder: # no absolute path given (dangerous...)
60			self.userFilesFolder = mapServerPath(self.environ,
61									self.webUserFilesFolder)
62
63		# Ensure that the directory exists.
64		if not os.path.exists(self.userFilesFolder):
65			try:
66				self.createServerFoldercreateServerFolder( self.userFilesFolder )
67			except:
68				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. ")
69
70		# File upload doesn't have to return XML, so intercept here
71		return self.uploadFile(resourceType, currentFolder)
72
73# Running from command line (plain old CGI)
74if __name__ == '__main__':
75	try:
76		# Create a Connector Instance
77		conn = FCKeditorQuickUpload()
78		data = conn.doResponse()
79		for header in conn.headers:
80			if not header is None:
81				print '%s: %s' % header
82		print
83		print data
84	except:
85		print "Content-Type: text/plain"
86		print
87		import cgi
88		cgi.print_exception()
89