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

« back to all changes in this revision

Viewing changes to contrib/AnyWikiDraw/anywikidraw/mediawiki 1.7 to 1.10/extensions/AnyWikiDraw/AnyWikiDraw.php

  • 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
<?php
 
2
/*
 
3
 * @(#)AnyWikiDraw.php  
 
4
 *
 
5
 * Copyright (c) 2007-2008 by the original authors of AnyWikiDraw
 
6
 * and all its contributors.
 
7
 * All rights reserved.
 
8
 *
 
9
 * The copyright of this software is owned by the authors and  
 
10
 * contributors of the AnyWikiDraw project ("the copyright holders").
 
11
 * You may not use, copy or modify this software, except in  
 
12
 * accordance with the license agreement you entered into with  
 
13
 * the copyright holders. For details see accompanying license terms. 
 
14
 */
 
15
 
 
16
/**
 
17
 * --------
 
18
 * WARNING: This is an extension for MediaWiki 1.7 through 1.10 only.
 
19
 * Do not use it with other versions of MediaWiki without extensive testing!
 
20
 * --------
 
21
 *
 
22
 * With the AnyWikiDraw extension it is possible to define new tags of the form
 
23
 *
 
24
 * {{#drawing:image.svg|width|height}}
 
25
 *
 
26
 * - The tag needs to be put into two curly braces.
 
27
 * - The name of the tag is #drawing:
 
28
 * - The parameter image.svg specifies the name of the image file. 
 
29
 * - If you specify an image file that does not yet exist in your Wiki, the image 
 
30
 *   is created the first time you save a drawing.
 
31
 * - The parameter width specifies the width of the image.
 
32
 * - The parameter height specifies the height of the image.
 
33
 *
 
34
 * The enable drawing for the image HappyWiki.svg, which has a width of 400 and
 
35
 * a height of 300 pixels, insert the following tag into a page:
 
36
 *
 
37
 * {{#drawing:HappyDrawing.svg|300|200}}
 
38
 *
 
39
 * To activate the extension, include it from your LocalSettings.php
 
40
 * with: include("extensions/AnyWikiDraw/AnyWikiDraw.php"); 
 
41
 *
 
42
 * @author Werner Randelshofer
 
43
 * @version $Id: AnyWikiDraw.php 127 2009-07-05 12:04:33Z rawcoder $
 
44
 */
 
45
 
 
46
 
 
47
# AnyWikiDraw Version
 
48
global $wgAnywikidrawVersion;
 
49
$wgAnyWikiDrawVersion = '0.13.2';
 
50
 
 
51
# Define a setup function
 
52
$wgExtensionFunctions[] = 'wfAnyWikiDrawParserFunction_Setup';
 
53
 
 
54
# Add a hook to initialise the magic word
 
55
$wgHooks['LanguageGetMagic'][]       = 'wfAnyWikiDrawParserFunction_Magic';
 
56
 
 
57
# Define a special page
 
58
$wgAutoloadClasses['AnyWikiDraw'] = dirname(__FILE__) . '/AnyWikiDraw_body.php';
 
59
$wgSpecialPages['AnyWikiDraw'] = 'AnyWikiDraw';
 
60
$wgHooks['LoadAllMessages'][] = 'wfAnyWikiDrawLoadMessages';
 
61
 
 
62
 
 
63
function wfAnyWikiDrawParserFunction_Setup() {
 
64
        global $wgParser;
 
65
        # Set a function hook associating the "drawing" magic word with our function
 
66
        $wgParser->setFunctionHook( 'drawing', 'wfAnyWikiDrawParserFunction_Render' );
 
67
}
 
68
 
 
69
function wfAnyWikiDrawParserFunction_Magic( &$magicWords, $langCode ) {
 
70
        # Add the magic word
 
71
        # The first array element is case sensitive, in this case it is not case sensitive
 
72
        # All remaining elements are synonyms for our parser function
 
73
        $magicWords['drawing'] = array( 0, 'drawing' );
 
74
        # unless we return true, other parser functions extensions won't get loaded.
 
75
        return true;
 
76
}
 
77
 
 
78
function wfAnyWikiDrawParserFunction_Render( &$parser, $name = null, $width = null, $height = null ) {
 
79
        global $wgUser, $wgLang, $wgTitle, $wgRightsText, $wgOut, $wgArticlePath, $wgScriptPath, $wgEnableUploads;
 
80
        $skin = $wgUser->getSkin();
 
81
        wfAnyWikiDrawLoadMessages();
 
82
 
 
83
        // Don't cache pages with drawings on it
 
84
        $parser->disableCache();
 
85
        
 
86
        # Validate parameters
 
87
        $error = '';
 
88
        if ($name == null || strlen($name) == 0) {
 
89
                $error .= '<br>Please specify a name for your drawing.';
 
90
        }
 
91
        if ($width != null && 
 
92
                        (! is_numeric($width) || $width < 1 || $width > 2000)) {
 
93
                $error .= '<br>Please specify the width as a number between 1 and 2000 or leave it away.';
 
94
        }
 
95
        if ($height != null && 
 
96
                        (! is_numeric($height) || $height < 1 || $height > 2000)) {
 
97
                $error .= '<br>Please specify the height as a number between 1 and 2000 or leave it away.';
 
98
        }
 
99
        if (strlen($error) > 0) {
 
100
                $error = '<b>Sorry.</b>'.$error.'<br>'.
 
101
                                'Usage: <code>{{#drawing:<i>image.svg</i>|<i>width</i>||<i>height</i>}}</code><br>'.
 
102
                                'Example: <code>{{#drawing:HappyDrawing.svg|400|300}}</code><br>';
 
103
                return array($error, 'isHTML'=>true, 'noparse'=>true);
 
104
        }
 
105
 
 
106
        # The parser function itself
 
107
        # The input parameters are wikitext with templates expanded
 
108
        # The output should be wikitext too, but in this case, it is HTML
 
109
        #return array("param1 is $param1 and param2 is $param2", 'isHTML');
 
110
        
 
111
        # Generate the image HTML as if viewed by a web request
 
112
        $image = Image::newFromName($name);
 
113
        
 
114
        
 
115
        $isProtected = $parser->getTitle()->isProtected();
 
116
        
 
117
        if ($width == null && $image->getWidth() != -1) {
 
118
                        $width = $image->getWidth();
 
119
        }
 
120
        if ($height == null && $image->getHeight() != -1) {
 
121
                $height = $image->getHeight();
 
122
        }
 
123
                
 
124
        // render a header
 
125
        $output = '<table><tr><td>';
 
126
        if ($wgEnableUploads && ! $isProtected &&
 
127
                        key_exists('drawingtitle', $_POST) && 
 
128
                        $_POST['drawingtitle'] == $name) {
 
129
                
 
130
                // edit the drawing using the applet
 
131
        global $wgUsePathInfo;
 
132
        if ($wgUsePathInfo) {
 
133
            $downloadURL = str_replace('$1', 'Special:AnyWikiDraw?image='.htmlspecialchars($image->getName()), $wgArticlePath);
 
134
        } else {
 
135
            $downloadURL = str_replace('$1', 'Special:AnyWikiDraw&image='.htmlspecialchars($image->getName()), $wgArticlePath);
 
136
        }
 
137
        $uploadURL = str_replace('$1', 'Special:AnyWikiDraw', $wgArticlePath);
 
138
                $output .= 
 
139
                                '<a name="anywikidraw" id="anywikidraw">'.
 
140
                                '<applet codebase="/" archive="'.$wgScriptPath.'/extensions/AnyWikiDraw/AnyWikiDrawForMediaWiki.jar"'. 
 
141
                                ' code="org.anywikidraw.mediawiki.MediaWikiDrawingApplet.class"'.
 
142
                                ' width="'.htmlspecialchars(min(max($width+4, 600), 800)).'" '.
 
143
                ' height="'.htmlspecialchars(min(max($height+140, 480), 600)).'">'.
 
144
 
 
145
           // The following parameters are used to tell AnyWikiDraw how to communicate with MediaWiki:
 
146
 
 
147
                                '<param name="DrawingName" value="'.htmlspecialchars($name).'">'.
 
148
                                '<param name="DrawingWidth" value="'.htmlspecialchars($width).'">'.
 
149
                                '<param name="DrawingHeight" value="'.htmlspecialchars($height).'">'.
 
150
                                (($image->getURL() !== '') ? '<param name="DrawingURL" value="'.$downloadURL.'">' : '').
 
151
                                '<param name="PageURL" value="'.htmlspecialchars($wgTitle->getLocalURL()).'">'.
 
152
                                '<param name="UploadURL" value="'.$uploadURL.'">'.
 
153
 
 
154
           // The following parameters are used to configure the drawing applet: 
 
155
 
 
156
                '<param name="Locale" value="'.$wgUser->getOption('language','en').'"/>'.
 
157
           
 
158
           // The following parameters are used to configure Sun's Java Plug-In: 
 
159
 
 
160
                '<param name="codebase_lookup" value="false"/>'.
 
161
                '<param name="classloader_cache" value="false"/>'.
 
162
                '<param name="java_arguments" value="-Djnlp.packEnabled=true"/>'.
 
163
                '<param name="image" value="lib/Splash.gif"/>'.
 
164
                '<param name="boxborder" value="false"/>'.
 
165
                '<param name="centerimage" value="true"/>'.
 
166
 
 
167
                                '</applet>'.
 
168
                                '</a>';
 
169
                        
 
170
                $output .= '<div style="'.
 
171
                                        'background-color: #C9CDD6; border: 1px solid #ccc; padding: 5px 10px 5px 10px; text-align: left; '.
 
172
                                        'font-size: 12px; line-height: 16px; '.
 
173
                                        '">'.
 
174
                                        wfMsg(($image->getURL() == '') ? 'anywikidraw_license_terms_new_work' : 'anywikidraw_license_terms_derived_work',
 
175
                                        '<a href='.wfMsgForContent('copyrightpage').'>'.wfMsg('copyrightpage').'</a>', '<a href="./Image:'.$name.'">Image:'.$name.'</a>').
 
176
                                        '</div>';
 
177
        } else {
 
178
                // Retrieve the page object of the image to determine, whether the user may edit it
 
179
                $filtered = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $name );
 
180
                $nt = Title::newFromText( $filtered );
 
181
                if(! is_null( $nt ) ) {
 
182
                        $nt =& Title::makeTitle( NS_IMAGE, $nt->getDBkey() );
 
183
                }
 
184
 
 
185
        // Determine if the user has permission to edit the image
 
186
                $userCanEdit = $wgEnableUploads && 
 
187
                    !$isProtected && 
 
188
                    (is_null($nt) || $nt->userCanEdit()) &&
 
189
                    ($image->getURL() == null || $wgUser->isAllowed( 'reupload' ));
 
190
 
 
191
        // Determine if the user may edit images using the specified 
 
192
        // filename extension.
 
193
        if ($userCanEdit) {
 
194
            $extension = array_pop(explode( '.', $filtered ));
 
195
            global $wgFileExtensions;
 
196
            $userCanEdit = in_array($extension, $wgFileExtensions);
 
197
        }
 
198
                
 
199
                // If the user can edit the image, display an edit link.
 
200
        // We do not display the edit link, if the user is already
 
201
        // editing a drawing.
 
202
                if ($userCanEdit && ! key_exists('drawingtitle', $_POST)) {
 
203
                        $formId = 'Form'.rand();
 
204
            global $wgUsePathInfo;
 
205
            if ($wgUsePathInfo) {
 
206
                $action = $wgTitle->getLocalURL().'#anywikidraw';
 
207
            } else {
 
208
                //$action = str_replace('?','#anywikidraw?',$wgTitle->getLocalURL());
 
209
                $action = $wgTitle->getLocalURL();
 
210
            }
 
211
                        $output .= '<form name="'.$formId.'" method="post" action="'.$action.'">'.
 
212
                                        '<input type="hidden" name="drawingtitle" value="'.htmlspecialchars($name).'">'.
 
213
                                        '<p align="right">'.
 
214
                                        '[<a href="javascript:document.'.$formId.'.submit();">'.wfMsg('edit').'</a>]'.
 
215
                                        '<noscript><input type="submit" name="submit" value="'.wfMsg('edit').'"></input></noscript>'.
 
216
                                        '</p>'
 
217
                                        ;
 
218
                }
 
219
                // render the drawing
 
220
                $image->width = $width; // FIXME This is an ugly hack to get a view URL
 
221
                $viewURL = $image->getViewURL();
 
222
                
 
223
                if ($image->getURL() == '') {
 
224
                        // the drawing does not exist yet, render an empty rectangle
 
225
                        $output .= '<div style="border:1px solid #000;text-align:center;'.
 
226
                                (($width != null) ? 'width:'.$width.'px;' : '').
 
227
                                (($height != null) ? 'height:'.$height.'px;' : '').'"'.
 
228
                                '>'.htmlspecialchars($name).'</div>';
 
229
                } else if (substr($name,-4) == '.svg' && $viewURL == null) {
 
230
                        // SVG images need an object tag
 
231
                        $output .= '<object '.
 
232
                                'type="image/svg+xml" '.
 
233
                                'data="'.(($viewURL != null) ? $viewURL : $image->getURL()).'" '.
 
234
                                (($width != null) ? 'width="'.$width.'" ' : '').
 
235
                                (($height != null) ? 'height="'.$height.'" ' : '').
 
236
                                '></object>';
 
237
                } else {
 
238
                        // All other image types need an img tag
 
239
                        // Render the image map, if it exists
 
240
                        $thumbnail = $image->getThumbnail($width);
 
241
                        $isImageMap = $thumbnail != null && file_exists($thumbnail->path.'.map');
 
242
                        $mapId = 'Map'.rand();
 
243
                        if ($isImageMap) {
 
244
                                $output .= '<map name="'.$mapId.'">'.
 
245
                                        file_get_contents($thumbnail->path.'.map').
 
246
                                        '</map>';
 
247
                        }
 
248
                        // Render the image
 
249
                        if (! $isImageMap) {
 
250
                                $output .= '<a href="./Image:'.$name.'">';
 
251
                        }
 
252
            // Note: We append the timestamp of the image to the
 
253
            //       view URL as a query string. This way, we ensure,
 
254
            //       that the browser always displays the last edited version
 
255
            //       of the image
 
256
                        $output .= '<img '.
 
257
                                'src="'.(($viewURL != null) ? $viewURL : $image->getURL()).
 
258
                    '?version='.$image->nextHistoryLine()->img_timestamp.'" '.
 
259
                                (($width != null) ? 'width="'.$width.'" ' : '').
 
260
                                (($height != null) ? 'height="'.$height.'" ' : '').
 
261
                                'alt="Image:'.$name.'" '.
 
262
                                'title="Image:'.$name.'" '.
 
263
                                (($isImageMap) ? 'usemap="#'.$mapId.'" ' : '').
 
264
                                '></img>';
 
265
                        if (! $isImageMap) {
 
266
                                $output .= '</a>';
 
267
                        }
 
268
                }
 
269
                // If the user can edit the image, display an edit link.
 
270
        // We do not display the edit link, if the user is already
 
271
        // editing a drawing.
 
272
                if ($userCanEdit && ! key_exists('drawingtitle', $_POST)) {
 
273
                        $output .= '</form>';
 
274
                }
 
275
        }
 
276
        
 
277
        // render a footer
 
278
        $output .= '</tr></td></table>';
 
279
        
 
280
        return array($output, 'isHTML'=>true, 'noparse'=>true);
 
281
}
 
282
 
 
283
function wfAnyWikiDrawLoadMessages() {
 
284
                global $wgAnyWikiDrawMessagesLoaded;
 
285
                global $wgMessageCache;
 
286
                if ( $wgAnyWikiDrawMessagesLoaded ) return;
 
287
                $wgAnyWikiDrawMessagesLoaded = true;
 
288
 
 
289
                require( dirname( __FILE__ ) . '/AnyWikiDraw.i18n.php' );
 
290
                foreach ( $messages as $lang => $msgs ) {
 
291
                                $wgMessageCache->addMessages( $msgs, $lang );
 
292
                }
 
293
}
 
294
?>
 
 
b'\\ No newline at end of file'