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

« back to all changes in this revision

Viewing changes to MoinMoin/action/twikidraw.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
# -*- coding: iso-8859-1 -*-
 
2
"""
 
3
    MoinMoin - twikidraw
 
4
 
 
5
    This action is used to call twikidraw
 
6
 
 
7
    @copyright: 2001 by Ken Sugino (sugino@mediaone.net),
 
8
                2001-2004 by Juergen Hermann <jh@web.de>,
 
9
                2005 MoinMoin:AlexanderSchremmer,
 
10
                2005 DiegoOngaro at ETSZONE (diego@etszone.com),
 
11
                2007-2008 MoinMoin:ThomasWaldmann,
 
12
                2005-2009 MoinMoin:ReimarBauer,
 
13
    @license: GNU GPL, see COPYING for details.
 
14
"""
 
15
import os, re
 
16
 
 
17
from MoinMoin import log
 
18
logging = log.getLogger(__name__)
 
19
 
 
20
from MoinMoin import wikiutil, config
 
21
from MoinMoin.action import AttachFile, do_show
 
22
from MoinMoin.action.AttachFile import _write_stream
 
23
from MoinMoin.security.textcha import TextCha
 
24
 
 
25
action_name = __name__.split('.')[-1]
 
26
 
 
27
 
 
28
def gedit_drawing(self, url, text, **kw):
 
29
    # This is called for displaying a drawing image by gui editor.
 
30
    _ = self.request.getText
 
31
    # TODO: this 'text' argument is kind of superfluous, replace by using alt=... kw arg
 
32
    # ToDo: make this clickable for the gui editor
 
33
    if 'alt' not in kw or not kw['alt']:
 
34
        kw['alt'] = text
 
35
    # we force the title here, needed later for html>wiki converter
 
36
    kw['title'] = "drawing:%s" % wikiutil.quoteWikinameURL(url)
 
37
    pagename, drawing = AttachFile.absoluteName(url, self.page.page_name)
 
38
    containername = wikiutil.taintfilename(drawing)
 
39
    drawing_url = AttachFile.getAttachUrl(pagename, containername, self.request)
 
40
    ci = AttachFile.ContainerItem(self.request, pagename, containername)
 
41
    if not ci.exists():
 
42
        title = _('Create new drawing "%(filename)s (opens in new window)"') % {'filename': self.text(containername)}
 
43
        img = self.icon('attachimg')  # TODO: we need a new "drawimg" in similar grey style and size
 
44
        css = 'nonexistent'
 
45
        return self.url(1, drawing_url, css=css, title=title) + img + self.url(0)
 
46
    kw['src'] = ci.member_url('drawing.png')
 
47
    return self.image(**kw)
 
48
 
 
49
 
 
50
def attachment_drawing(self, url, text, **kw):
 
51
    # This is called for displaying a clickable drawing image by text_html formatter.
 
52
    # XXX text arg is unused!
 
53
    _ = self.request.getText
 
54
    pagename, drawing = AttachFile.absoluteName(url, self.page.page_name)
 
55
    containername = wikiutil.taintfilename(drawing)
 
56
 
 
57
    drawing_url = AttachFile.getAttachUrl(pagename, containername, self.request, do='modify')
 
58
    ci = AttachFile.ContainerItem(self.request, pagename, containername)
 
59
    if not ci.exists():
 
60
        title = _('Create new drawing "%(filename)s (opens in new window)"') % {'filename': self.text(containername)}
 
61
        img = self.icon('attachimg')  # TODO: we need a new "drawimg" in similar grey style and size
 
62
        css = 'nonexistent'
 
63
        return self.url(1, drawing_url, css=css, title=title) + img + self.url(0)
 
64
 
 
65
    title = _('Edit drawing %(filename)s (opens in new window)') % {'filename': self.text(containername)}
 
66
    kw['src'] = src = ci.member_url('drawing.png')
 
67
    kw['css'] = 'drawing'
 
68
 
 
69
    try:
 
70
        mapfile = ci.get('drawing.map')
 
71
        map = mapfile.read()
 
72
        mapfile.close()
 
73
        map = map.decode(config.charset)
 
74
    except (KeyError, IOError, OSError):
 
75
        map = u''
 
76
    if map:
 
77
        # we have a image map. inline it and add a map ref to the img tag
 
78
        # we have also to set a unique ID
 
79
        mapid = u'ImageMapOf%s%s' % (self.request.uid_generator(pagename), drawing)
 
80
        map = map.replace(u'%MAPNAME%', mapid)
 
81
        # add alt and title tags to areas
 
82
        map = re.sub(ur'href\s*=\s*"((?!%TWIKIDRAW%).+?)"', ur'href="\1" alt="\1" title="\1"', map)
 
83
        map = map.replace(u'%TWIKIDRAW%"', u'%s" alt="%s" title="%s"' % (
 
84
            wikiutil.escape(drawing_url, 1), title, title))
 
85
        # unxml, because 4.01 concrete will not validate />
 
86
        map = map.replace(u'/>', u'>')
 
87
        title = _('Clickable drawing: %(filename)s') % {'filename': self.text(containername)}
 
88
        if 'title' not in kw:
 
89
            kw['title'] = title
 
90
        if 'alt' not in kw:
 
91
            kw['alt'] = kw['title']
 
92
        kw['usemap'] = '#'+mapid
 
93
        return self.url(1, drawing_url) + map + self.image(**kw) + self.url(0)
 
94
    else:
 
95
        if 'title' not in kw:
 
96
            kw['title'] = title
 
97
        if 'alt' not in kw:
 
98
            kw['alt'] = kw['title']
 
99
        return self.url(1, drawing_url) + self.image(**kw) + self.url(0)
 
100
 
 
101
 
 
102
class TwikiDraw(object):
 
103
    """ twikidraw action """
 
104
    def __init__(self, request, pagename, target):
 
105
        self.request = request
 
106
        self.pagename = pagename
 
107
        self.target = target
 
108
 
 
109
    def save(self):
 
110
        request = self.request
 
111
        _ = request.getText
 
112
 
 
113
        if not wikiutil.checkTicket(request, request.args.get('ticket', '')):
 
114
            return _('Please use the interactive user interface to use action %(actionname)s!') % {'actionname': 'twikidraw.save' }
 
115
 
 
116
        pagename = self.pagename
 
117
        target = self.target
 
118
        if not request.user.may.write(pagename):
 
119
            return _('You are not allowed to save a drawing on this page.')
 
120
        if not target:
 
121
            return _("Empty target name given.")
 
122
 
 
123
        file_upload = request.files.get('filepath')
 
124
        if not file_upload:
 
125
            # This might happen when trying to upload file names
 
126
            # with non-ascii characters on Safari.
 
127
            return _("No file content. Delete non ASCII characters from the file name and try again.")
 
128
 
 
129
        filename = request.form['filename']
 
130
        basepath, basename = os.path.split(filename)
 
131
        basename, ext = os.path.splitext(basename)
 
132
        ci = AttachFile.ContainerItem(request, pagename, target)
 
133
        filecontent = file_upload.stream
 
134
        content_length = None
 
135
        if ext == '.draw': # TWikiDraw POSTs this first
 
136
            AttachFile._addLogEntry(request, 'ATTDRW', pagename, target)
 
137
            ci.truncate()
 
138
            filecontent = filecontent.read() # read file completely into memory
 
139
            filecontent = filecontent.replace("\r", "")
 
140
        elif ext == '.map':
 
141
            # touch attachment directory to invalidate cache if new map is saved
 
142
            attach_dir = AttachFile.getAttachDir(request, pagename)
 
143
            os.utime(attach_dir, None)
 
144
            filecontent = filecontent.read() # read file completely into memory
 
145
            filecontent = filecontent.strip()
 
146
        else:
 
147
            #content_length = file_upload.content_length
 
148
            # XXX gives -1 for wsgiref :( If this is fixed, we could use the file obj,
 
149
            # without reading it into memory completely:
 
150
            filecontent = filecontent.read()
 
151
 
 
152
        ci.put('drawing' + ext, filecontent, content_length)
 
153
 
 
154
 
 
155
    def render(self):
 
156
        request = self.request
 
157
        _ = request.getText
 
158
        pagename = self.pagename
 
159
        target = self.target
 
160
        if not request.user.may.read(pagename):
 
161
            return _('You are not allowed to view attachments of this page.')
 
162
        if not target:
 
163
            return _("Empty target name given.")
 
164
 
 
165
        ci = AttachFile.ContainerItem(request, pagename, target)
 
166
        if ci.exists():
 
167
            drawurl = ci.member_url('drawing.draw')
 
168
            pngurl = ci.member_url('drawing.png')
 
169
        else:
 
170
            drawurl = 'drawing.draw'
 
171
            pngurl = 'drawing.png'
 
172
        pageurl = request.href(pagename)
 
173
        saveurl = request.href(pagename, action=action_name, do='save', target=target,
 
174
                               ticket=wikiutil.createTicket(request))
 
175
        helpurl = request.href("HelpOnActions/AttachFile")
 
176
 
 
177
        html = """
 
178
<p>
 
179
<applet code="CH.ifa.draw.twiki.TWikiDraw.class"
 
180
        archive="%(htdocs)s/applets/TWikiDrawPlugin/twikidraw.jar" width="640" height="480">
 
181
    <param name="drawpath" value="%(drawurl)s">
 
182
    <param name="pngpath"  value="%(pngurl)s">
 
183
    <param name="savepath" value="%(saveurl)s">
 
184
    <param name="basename" value="%(basename)s">
 
185
    <param name="viewpath" value="%(pageurl)s">
 
186
    <param name="helppath" value="%(helpurl)s">
 
187
    <strong>NOTE:</strong> You need a Java enabled browser to edit the drawing.
 
188
</applet>
 
189
</p>
 
190
""" % dict(
 
191
    htdocs=request.cfg.url_prefix_static,
 
192
    basename=wikiutil.escape(target, 1),
 
193
    drawurl=wikiutil.escape(drawurl, 1),
 
194
    pngurl=wikiutil.escape(pngurl, 1),
 
195
    pageurl=wikiutil.escape(pageurl, 1),
 
196
    saveurl=wikiutil.escape(saveurl, 1),
 
197
    helpurl=wikiutil.escape(helpurl, 1),
 
198
)
 
199
 
 
200
        title = "%s %s:%s" % (_("Edit drawing"), pagename, target)
 
201
        request.theme.send_title(title, page=request.page, pagename=pagename)
 
202
        request.write(request.formatter.startContent("content"))
 
203
        request.write(request.formatter.rawHTML(html))
 
204
        request.write(request.formatter.endContent())
 
205
        request.theme.send_footer(pagename)
 
206
        request.theme.send_closing_html()
 
207
 
 
208
 
 
209
def execute(pagename, request):
 
210
    target = request.values.get('target')
 
211
    twd = TwikiDraw(request, pagename, target)
 
212
 
 
213
    do = request.values.get('do')
 
214
    if do == 'save':
 
215
        msg = twd.save()
 
216
    else:
 
217
        msg = twd.render()
 
218
    if msg:
 
219
        request.theme.add_msg(msg, 'error')
 
220
        do_show(pagename, request)
 
221
 
 
222