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
23Connector for Python and Zope.
24
25This code was not tested at all.
26It just was ported from pre 2.5 release, so for further reference see
27\editor\filemanager\browser\default\connectors\py\connector.py in previous
28releases.
29
30"""
31
32from fckutil import *
33from connector import *
34import config as Config
35
36class FCKeditorConnectorZope(FCKeditorConnector):
37	"""
38	Zope versiof FCKeditorConnector
39	"""
40	# Allow access (Zope)
41	__allow_access_to_unprotected_subobjects__ = 1
42
43	def __init__(self, context=None):
44		"""
45		Constructor
46		"""
47		FCKeditorConnector.__init__(self, environ=None) # call superclass constructor
48		# Instance Attributes
49		self.context = context
50		self.request = FCKeditorRequest(context)
51
52	def getZopeRootContext(self):
53		if self.zopeRootContext is None:
54			self.zopeRootContext = self.context.getPhysicalRoot()
55		return self.zopeRootContext
56
57	def getZopeUploadContext(self):
58		if self.zopeUploadContext is None:
59			folderNames = self.userFilesFolder.split("/")
60			c = self.getZopeRootContext()
61			for folderName in folderNames:
62				if (folderName <> ""):
63					c = c[folderName]
64			self.zopeUploadContext = c
65		return self.zopeUploadContext
66
67	def setHeader(self, key, value):
68		self.context.REQUEST.RESPONSE.setHeader(key, value)
69
70	def getFolders(self, resourceType, currentFolder):
71		# Open the folders node
72		s = ""
73		s += """<Folders>"""
74		zopeFolder = self.findZopeFolder(resourceType, currentFolder)
75		for (name, o) in zopeFolder.objectItems(["Folder"]):
76			s += """<Folder name="%s" />""" % (
77					convertToXmlAttribute(name)
78					)
79		# Close the folders node
80		s += """</Folders>"""
81		return s
82
83	def getZopeFoldersAndFiles(self, resourceType, currentFolder):
84		folders = self.getZopeFolders(resourceType, currentFolder)
85		files = self.getZopeFiles(resourceType, currentFolder)
86		s = folders + files
87		return s
88
89	def getZopeFiles(self, resourceType, currentFolder):
90		# Open the files node
91		s = ""
92		s += """<Files>"""
93		zopeFolder = self.findZopeFolder(resourceType, currentFolder)
94		for (name, o) in zopeFolder.objectItems(["File","Image"]):
95			s += """<File name="%s" size="%s" />""" % (
96					convertToXmlAttribute(name),
97					((o.get_size() / 1024) + 1)
98					)
99		# Close the files node
100		s += """</Files>"""
101		return s
102
103	def findZopeFolder(self, resourceType, folderName):
104		# returns the context of the resource / folder
105		zopeFolder = self.getZopeUploadContext()
106		folderName = self.removeFromStart(folderName, "/")
107		folderName = self.removeFromEnd(folderName, "/")
108		if (resourceType <> ""):
109			try:
110				zopeFolder = zopeFolder[resourceType]
111			except:
112				zopeFolder.manage_addProduct["OFSP"].manage_addFolder(id=resourceType, title=resourceType)
113				zopeFolder = zopeFolder[resourceType]
114		if (folderName <> ""):
115			folderNames = folderName.split("/")
116			for folderName in folderNames:
117				zopeFolder = zopeFolder[folderName]
118		return zopeFolder
119
120	def createFolder(self, resourceType, currentFolder):
121		# Find out where we are
122		zopeFolder = self.findZopeFolder(resourceType, currentFolder)
123		errorNo = 0
124		errorMsg = ""
125		if self.request.has_key("NewFolderName"):
126			newFolder = self.request.get("NewFolderName", None)
127			zopeFolder.manage_addProduct["OFSP"].manage_addFolder(id=newFolder, title=newFolder)
128		else:
129			errorNo = 102
130		return self.sendErrorNode ( errorNo, errorMsg )
131
132	def uploadFile(self, resourceType, currentFolder, count=None):
133		zopeFolder = self.findZopeFolder(resourceType, currentFolder)
134		file = self.request.get("NewFile", None)
135		fileName = self.getFileName(file.filename)
136		fileNameOnly = self.removeExtension(fileName)
137		fileExtension = self.getExtension(fileName).lower()
138		if (count):
139			nid = "%s.%s.%s" % (fileNameOnly, count, fileExtension)
140		else:
141			nid = fileName
142		title = nid
143		try:
144			zopeFolder.manage_addProduct['OFSP'].manage_addFile(
145					id=nid,
146					title=title,
147					file=file.read()
148					)
149		except:
150			if (count):
151				count += 1
152			else:
153				count = 1
154			return self.zopeFileUpload(resourceType, currentFolder, count)
155		return self.sendUploadResults( 0 )
156
157class FCKeditorRequest(object):
158	"A wrapper around the request object"
159	def __init__(self, context=None):
160		r = context.REQUEST
161		self.request = r
162
163	def has_key(self, key):
164		return self.request.has_key(key)
165
166	def get(self, key, default=None):
167		return self.request.get(key, default)
168
169"""
170Running from zope, you will need to modify this connector.
171If you have uploaded the FCKeditor into Zope (like me), you need to
172move this connector out of Zope, and replace the "connector" with an
173alias as below.  The key to it is to pass the Zope context in, as
174we then have a like to the Zope context.
175
176## Script (Python) "connector.py"
177##bind container=container
178##bind context=context
179##bind namespace=
180##bind script=script
181##bind subpath=traverse_subpath
182##parameters=*args, **kws
183##title=ALIAS
184##
185
186import Products.zope as connector
187return connector.FCKeditorConnectorZope(context=context).doResponse()
188"""
189
190