~ubuntu-branches/ubuntu/oneiric/moin/oneiric-security

« back to all changes in this revision

Viewing changes to MoinMoin/web/static/htdocs/applets/FCKeditor/fckeditor.py

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2010-03-30 12:55:34 UTC
  • mfrom: (0.1.17 sid)
  • Revision ID: james.westby@ubuntu.com-20100330125534-4c2ufc1rok24447l
Tags: 1.9.2-2ubuntu1
* Merge from Debian testing (LP: #521834). Based on work by Stefan Ebner.
  Remaining changes:
 - Remove python-xml from Suggests field, the package isn't anymore in
   sys.path.
 - Demote fckeditor from Recommends to Suggests; the code was previously
   embedded in moin, but it was also disabled, so there's no reason for us
   to pull this in by default currently. Note: This isn't necessary anymore
   but needs a MIR for fckeditor, so postpone dropping this change until
   lucid+1
* debian/rules:
  - Replace hardcoded python2.5 with python* and hardcore python2.6 for ln
* debian/control.in: drop versioned depends on cdbs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
FCKeditor - The text editor for Internet - http://www.fckeditor.net
 
3
Copyright (C) 2003-2009 Frederico Caldeira Knabben
 
4
 
 
5
== BEGIN LICENSE ==
 
6
 
 
7
Licensed under the terms of any of the following licenses at your
 
8
choice:
 
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
 
 
21
This is the integration file for Python.
 
22
"""
 
23
 
 
24
import cgi
 
25
import os
 
26
import re
 
27
import string
 
28
 
 
29
def 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
 
44
class 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 = ""
 
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
                return Html
 
109
 
 
110
        def IsCompatible(self):
 
111
                if (os.environ.has_key("HTTP_USER_AGENT")):
 
112
                        sAgent = os.environ.get("HTTP_USER_AGENT", "")
 
113
                else:
 
114
                        sAgent = ""
 
115
                if (sAgent.find("MSIE") >= 0) and (sAgent.find("mac") < 0) and (sAgent.find("Opera") < 0):
 
116
                        i = sAgent.find("MSIE")
 
117
                        iVersion = float(sAgent[i+5:i+5+3])
 
118
                        if (iVersion >= 5.5):
 
119
                                return True
 
120
                        return False
 
121
                elif (sAgent.find("Gecko/") >= 0):
 
122
                        i = sAgent.find("Gecko/")
 
123
                        iVersion = int(sAgent[i+6:i+6+8])
 
124
                        if (iVersion >= 20030210):
 
125
                                return True
 
126
                        return False
 
127
                elif (sAgent.find("Opera/") >= 0):
 
128
                        i = sAgent.find("Opera/")
 
129
                        iVersion = float(sAgent[i+6:i+6+4])
 
130
                        if (iVersion >= 9.5):
 
131
                                return True
 
132
                        return False
 
133
                elif (sAgent.find("AppleWebKit/") >= 0):
 
134
                        p = re.compile('AppleWebKit\/(\d+)', re.IGNORECASE)
 
135
                        m = p.search(sAgent)
 
136
                        if (m.group(1) >= 522):
 
137
                                return True
 
138
                        return False
 
139
                else:
 
140
                        return False
 
141
 
 
142
        def GetConfigFieldString(self):
 
143
                sParams = ""
 
144
                bFirst = True
 
145
                for sKey in self.Config.keys():
 
146
                        sValue = self.Config[sKey]
 
147
                        if (not bFirst):
 
148
                                sParams += "&amp;"
 
149
                        else:
 
150
                                bFirst = False
 
151
                        if (sValue):
 
152
                                k = escape(sKey)
 
153
                                v = escape(sValue)
 
154
                                if (sValue == "true"):
 
155
                                        sParams += "%s=true" % k
 
156
                                elif (sValue == "false"):
 
157
                                        sParams += "%s=false" % k
 
158
                                else:
 
159
                                        sParams += "%s=%s" % (k, v)
 
160
                return sParams