~ubuntu-branches/ubuntu/hoary/mailman/hoary-security

« back to all changes in this revision

Viewing changes to Mailman/Cgi/edithtml.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2004-10-11 02:02:43 UTC
  • Revision ID: james.westby@ubuntu.com-20041011020243-ukiishnhlkmsoh21
Tags: upstream-2.1.5
ImportĀ upstreamĀ versionĀ 2.1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 1998,1999,2000,2001,2002 by the Free Software Foundation, Inc.
 
2
#
 
3
# This program is free software; you can redistribute it and/or
 
4
# modify it under the terms of the GNU General Public License
 
5
# as published by the Free Software Foundation; either version 2
 
6
# of the License, or (at your option) any later version.
 
7
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software 
 
15
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
16
 
 
17
"""Script which implements admin editing of the list's html templates."""
 
18
 
 
19
import os
 
20
import cgi
 
21
import errno
 
22
 
 
23
from Mailman import Utils
 
24
from Mailman import MailList
 
25
from Mailman.htmlformat import *
 
26
from Mailman.HTMLFormatter import HTMLFormatter
 
27
from Mailman import Errors
 
28
from Mailman.Cgi import Auth
 
29
from Mailman.Logging.Syslog import syslog
 
30
from Mailman import i18n
 
31
 
 
32
_ = i18n._
 
33
 
 
34
 
 
35
 
 
36
def main():
 
37
    # Trick out pygettext since we want to mark template_data as translatable,
 
38
    # but we don't want to actually translate it here.
 
39
    def _(s):
 
40
        return s
 
41
 
 
42
    template_data = (
 
43
        ('listinfo.html',    _('General list information page')),
 
44
        ('subscribe.html',   _('Subscribe results page')),
 
45
        ('options.html',     _('User specific options page')),
 
46
        )
 
47
 
 
48
    _ = i18n._
 
49
    doc = Document()
 
50
 
 
51
    # Set up the system default language
 
52
    i18n.set_language(mm_cfg.DEFAULT_SERVER_LANGUAGE)
 
53
    doc.set_language(mm_cfg.DEFAULT_SERVER_LANGUAGE)
 
54
 
 
55
    parts = Utils.GetPathPieces()
 
56
    if not parts:
 
57
        doc.AddItem(Header(2, _("List name is required.")))
 
58
        print doc.Format()
 
59
        return
 
60
 
 
61
    listname = parts[0].lower()
 
62
    try:
 
63
        mlist = MailList.MailList(listname, lock=0)
 
64
    except Errors.MMListError, e:
 
65
        # Avoid cross-site scripting attacks
 
66
        safelistname = Utils.websafe(listname)
 
67
        doc.AddItem(Header(2, _('No such list <em>%(safelistname)s</em>')))
 
68
        print doc.Format()
 
69
        syslog('error', 'No such list "%s": %s', listname, e)
 
70
        return
 
71
 
 
72
    # Now that we have a valid list, set the language to its default
 
73
    i18n.set_language(mlist.preferred_language)
 
74
    doc.set_language(mlist.preferred_language)
 
75
 
 
76
    # Must be authenticated to get any farther
 
77
    cgidata = cgi.FieldStorage()
 
78
 
 
79
    # Editing the html for a list is limited to the list admin and site admin.
 
80
    if not mlist.WebAuthenticate((mm_cfg.AuthListAdmin,
 
81
                                  mm_cfg.AuthSiteAdmin),
 
82
                                 cgidata.getvalue('adminpw', '')):
 
83
        if cgidata.has_key('admlogin'):
 
84
            # This is a re-authorization attempt
 
85
            msg = Bold(FontSize('+1', _('Authorization failed.'))).Format()
 
86
        else:
 
87
            msg = ''
 
88
        Auth.loginpage(mlist, 'admin', msg=msg)
 
89
        return
 
90
 
 
91
    realname = mlist.real_name
 
92
    if len(parts) > 1:
 
93
        template_name = parts[1]
 
94
        for (template, info) in template_data:
 
95
            if template == template_name:
 
96
                template_info = _(info)
 
97
                doc.SetTitle(_(
 
98
                    '%(realname)s -- Edit html for %(template_info)s'))
 
99
                break
 
100
        else:
 
101
            # Avoid cross-site scripting attacks
 
102
            safetemplatename = Utils.websafe(template_name)
 
103
            doc.SetTitle(_('Edit HTML : Error'))
 
104
            doc.AddItem(Header(2, _("%(safetemplatename)s: Invalid template")))
 
105
            doc.AddItem(mlist.GetMailmanFooter())
 
106
            print doc.Format()
 
107
            return
 
108
    else:
 
109
        doc.SetTitle(_('%(realname)s -- HTML Page Editing'))
 
110
        doc.AddItem(Header(1, _('%(realname)s -- HTML Page Editing')))
 
111
        doc.AddItem(Header(2, _('Select page to edit:')))
 
112
        template_list = UnorderedList()
 
113
        for (template, info) in template_data:
 
114
            l = Link(mlist.GetScriptURL('edithtml') + '/' + template, _(info))
 
115
            template_list.AddItem(l)
 
116
        doc.AddItem(FontSize("+2", template_list))
 
117
        doc.AddItem(mlist.GetMailmanFooter())
 
118
        print doc.Format()
 
119
        return
 
120
 
 
121
    try:
 
122
        if cgidata.keys():
 
123
            ChangeHTML(mlist, cgidata, template_name, doc)
 
124
        FormatHTML(mlist, doc, template_name, template_info)
 
125
    finally:
 
126
        doc.AddItem(mlist.GetMailmanFooter())
 
127
        print doc.Format()
 
128
 
 
129
 
 
130
 
 
131
def FormatHTML(mlist, doc, template_name, template_info):
 
132
    doc.AddItem(Header(1,'%s:' % mlist.real_name))
 
133
    doc.AddItem(Header(1, template_info))
 
134
    doc.AddItem('<hr>')
 
135
 
 
136
    link = Link(mlist.GetScriptURL('admin'),
 
137
                _('View or edit the list configuration information.'))
 
138
 
 
139
    doc.AddItem(FontSize("+1", link))
 
140
    doc.AddItem('<p>')
 
141
    doc.AddItem('<hr>')
 
142
    form = Form(mlist.GetScriptURL('edithtml') + '/' + template_name)
 
143
    text = Utils.websafe(Utils.maketext(template_name, raw=1, mlist=mlist))
 
144
    form.AddItem(TextArea('html_code', text, rows=40, cols=75))
 
145
    form.AddItem('<p>' + _('When you are done making changes...'))
 
146
    form.AddItem(SubmitButton('submit', _('Submit Changes')))
 
147
    doc.AddItem(form)
 
148
 
 
149
 
 
150
 
 
151
def ChangeHTML(mlist, cgi_info, template_name, doc):
 
152
    if not cgi_info.has_key('html_code'):
 
153
        doc.AddItem(Header(3,_("Can't have empty html page.")))
 
154
        doc.AddItem(Header(3,_("HTML Unchanged.")))
 
155
        doc.AddItem('<hr>')
 
156
        return
 
157
    code = cgi_info['html_code'].value
 
158
    langdir = os.path.join(mlist.fullpath(), mlist.preferred_language)
 
159
    # Make sure the directory exists
 
160
    try:
 
161
        os.mkdir(langdir, 02775)
 
162
    except OSError, e:
 
163
        if e.errno <> errno.EEXIST: raise
 
164
    fp = open(os.path.join(langdir, template_name), 'w')
 
165
    try:
 
166
        fp.write(code)
 
167
    finally:
 
168
        fp.close()
 
169
    doc.AddItem(Header(3, _('HTML successfully updated.')))
 
170
    doc.AddItem('<hr>')