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 (CGI and WSGI).
24
25"""
26
27from time import gmtime, strftime
28import string
29
30def escape(text, replace=string.replace):
31	"""
32	Converts the special characters '<', '>', and '&'.
33
34	RFC 1866 specifies that these characters be represented
35	in HTML as &lt; &gt; and &amp; respectively. In Python
36	1.5 we use the new string.replace() function for speed.
37	"""
38	text = replace(text, '&', '&amp;') # must be done 1st
39	text = replace(text, '<', '&lt;')
40	text = replace(text, '>', '&gt;')
41	text = replace(text, '"', '&quot;')
42	return text
43
44def convertToXmlAttribute(value):
45	if (value is None):
46		value = ""
47	return escape(value)
48
49class BaseHttpMixin(object):
50	def setHttpHeaders(self, content_type='text/xml'):
51		"Purpose: to prepare the headers for the xml to return"
52		# Prevent the browser from caching the result.
53		# Date in the past
54		self.setHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT')
55		# always modified
56		self.setHeader('Last-Modified',strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime()))
57		# HTTP/1.1
58		self.setHeader('Cache-Control','no-store, no-cache, must-revalidate')
59		self.setHeader('Cache-Control','post-check=0, pre-check=0')
60		# HTTP/1.0
61		self.setHeader('Pragma','no-cache')
62
63		# Set the response format.
64		self.setHeader( 'Content-Type', content_type + '; charset=utf-8' )
65		return
66
67class BaseXmlMixin(object):
68	def createXmlHeader(self, command, resourceType, currentFolder, url):
69		"Purpose: returns the xml header"
70		self.setHttpHeaders()
71		# Create the XML document header
72		s =  """<?xml version="1.0" encoding="utf-8" ?>"""
73		# Create the main connector node
74		s += """<Connector command="%s" resourceType="%s">""" % (
75				command,
76				resourceType
77				)
78		# Add the current folder node
79		s += """<CurrentFolder path="%s" url="%s" />""" % (
80				convertToXmlAttribute(currentFolder),
81				convertToXmlAttribute(url),
82				)
83		return s
84
85	def createXmlFooter(self):
86		"Purpose: returns the xml footer"
87		return """</Connector>"""
88
89	def sendError(self, number, text):
90		"Purpose: in the event of an error, return an xml based error"
91		self.setHttpHeaders()
92		return ("""<?xml version="1.0" encoding="utf-8" ?>""" +
93				"""<Connector>""" +
94				self.sendErrorNode (number, text) +
95				"""</Connector>""" )
96
97	def sendErrorNode(self, number, text):
98		return """<Error number="%s" text="%s" />""" % (number, convertToXmlAttribute(text))
99
100class BaseHtmlMixin(object):
101	def sendUploadResults( self, errorNo = 0, fileUrl = '', fileName = '', customMsg = '' ):
102		self.setHttpHeaders("text/html")
103		"This is the function that sends the results of the uploading process"
104		return """<script type="text/javascript">
105			window.parent.OnUploadCompleted(%(errorNumber)s,"%(fileUrl)s","%(fileName)s","%(customMsg)s");
106			</script>""" % {
107			'errorNumber': errorNo,
108			'fileUrl': fileUrl.replace ('"', '\\"'),
109			'fileName': fileName.replace ( '"', '\\"' ) ,
110			'customMsg': customMsg.replace ( '"', '\\"' ),
111			}