1"""
2FCKeditor - The text editor for Internet - http://www.fckeditor.net
3Copyright (C) 2003-2007 Frederico Caldeira Knabben
4
5== BEGIN LICENSE ==
6
7Licensed under the terms of any of the following licenses at your
8choice:
9
10 - GNU General Public License Version 2 or later (the "GPL")
11   http://www.gnu.org/licenses/gpl.html
12
13 - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   http://www.gnu.org/licenses/lgpl.html
15
16 - Mozilla Public License Version 1.1 or later (the "MPL")
17   http://www.mozilla.org/MPL/MPL-1.1.html
18
19== END LICENSE ==
20
21This is the integration file for Python.
22"""
23
24import cgi
25import os
26import re
27import string
28
29def escape(text, replace=string.replace):
30    """Converts the special characters '<', '>', and '&'.
31
32    RFC 1866 specifies that these characters be represented
33    in HTML as &lt; &gt; and &amp; respectively. In Python
34    1.5 we use the new string.replace() function for speed.
35    """
36    text = replace(text, '&', '&amp;') # must be done 1st
37    text = replace(text, '<', '&lt;')
38    text = replace(text, '>', '&gt;')
39    text = replace(text, '"', '&quot;')
40    text = replace(text, "'", '&#39;')
41    return text
42
43# The FCKeditor class
44class FCKeditor(object):
45	def __init__(self, instanceName):
46		self.InstanceName = instanceName
47		self.BasePath = '/fckeditor/'
48		self.Width = '100%'
49		self.Height = '200'
50		self.ToolbarSet = 'Default'
51		self.Value = '';
52
53		self.Config = {}
54
55	def Create(self):
56		return self.CreateHtml()
57
58	def CreateHtml(self):
59		HtmlValue = escape(self.Value)
60		Html = "<div>"
61
62		if (self.IsCompatible()):
63			File = "fckeditor.html"
64			Link = "%seditor/%s?InstanceName=%s" % (
65					self.BasePath,
66					File,
67					self.InstanceName
68					)
69			if (self.ToolbarSet is not None):
70				Link += "&amp;ToolBar=%s" % self.ToolbarSet
71
72			# Render the linked hidden field
73			Html += "<input type=\"hidden\" id=\"%s\" name=\"%s\" value=\"%s\" style=\"display:none\" />" % (
74					self.InstanceName,
75					self.InstanceName,
76					HtmlValue
77					)
78
79			# Render the configurations hidden field
80			Html += "<input type=\"hidden\" id=\"%s___Config\" value=\"%s\" style=\"display:none\" />" % (
81					self.InstanceName,
82					self.GetConfigFieldString()
83					)
84
85			# Render the editor iframe
86			Html += "<iframe id=\"%s\__Frame\" src=\"%s\" width=\"%s\" height=\"%s\" frameborder=\"0\" scrolling=\"no\"></iframe>" % (
87					self.InstanceName,
88					Link,
89					self.Width,
90					self.Height
91					)
92		else:
93			if (self.Width.find("%%") < 0):
94				WidthCSS = "%spx" % self.Width
95			else:
96				WidthCSS = self.Width
97			if (self.Height.find("%%") < 0):
98				HeightCSS = "%spx" % self.Height
99			else:
100				HeightCSS = self.Height
101
102			Html += "<textarea name=\"%s\" rows=\"4\" cols=\"40\" style=\"width: %s; height: %s;\" wrap=\"virtual\">%s</textarea>" % (
103					self.InstanceName,
104					WidthCSS,
105					HeightCSS,
106					HtmlValue
107					)
108		Html += "</div>"
109		return Html
110
111	def IsCompatible(self):
112		if (os.environ.has_key("HTTP_USER_AGENT")):
113			sAgent = os.environ.get("HTTP_USER_AGENT", "")
114		else:
115			sAgent = ""
116		if (sAgent.find("MSIE") >= 0) and (sAgent.find("mac") < 0) and (sAgent.find("Opera") < 0):
117			i = sAgent.find("MSIE")
118			iVersion = float(sAgent[i+5:i+5+3])
119			if (iVersion >= 5.5):
120				return True
121			return False
122		elif (sAgent.find("Gecko/") >= 0):
123			i = sAgent.find("Gecko/")
124			iVersion = int(sAgent[i+6:i+6+8])
125			if (iVersion >= 20030210):
126				return True
127			return False
128		elif (sAgent.find("Opera/") >= 0):
129			i = sAgent.find("Opera/")
130			iVersion = float(sAgent[i+6:i+6+4])
131			if (iVersion >= 9.5):
132				return True
133			return False
134		elif (sAgent.find("AppleWebKit/") >= 0):
135			p = re.compile('AppleWebKit\/(\d+)', re.IGNORECASE)
136			m = p.search(sAgent)
137			if (m.group(1) >= 522):
138				return True
139			return False
140		else:
141			return False
142
143	def GetConfigFieldString(self):
144		sParams = ""
145		bFirst = True
146		for sKey in self.Config.keys():
147			sValue = self.Config[sKey]
148			if (not bFirst):
149				sParams += "&amp;"
150			else:
151				bFirst = False
152			if (sValue):
153				k = escape(sKey)
154				v = escape(sValue)
155				if (sValue == "true"):
156					sParams += "%s=true" % k
157				elif (sValue == "false"):
158					sParams += "%s=false" % k
159				else:
160					sParams += "%s=%s" % (k, v)
161		return sParams
162
163