]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UpLoad.php
fixed bug #969532 (space in uploaded filenames)
[SourceForge/phpwiki.git] / lib / plugin / UpLoad.php
1 <?php // -*-php-*-
2 rcs_id('$Id: UpLoad.php,v 1.12 2004-06-13 11:34:22 rurban Exp $');
3 /*
4  Copyright 2003, 2004 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22  */
23
24 /**
25  * UpLoad:  Allow Administrator to upload files to a special directory,
26  *          which should preferably be added to the InterWikiMap
27  * Usage:   <?plugin UpLoad ?>
28  * Author:  NathanGass <gass@iogram.ch>
29  * Changes: ReiniUrban <rurban@x-ray.at>,
30  *          qubit <rtryon@dartmouth.edu>
31  * Note:    See also Jochen Kalmbach's plugin/UserFileManagement.php
32  */
33
34 class WikiPlugin_UpLoad
35 extends WikiPlugin
36 {
37     var $disallowed_extensions;
38     // TODO: use PagePerms instead
39     var $only_authenticated = true; // allow only authenticated users may upload.
40
41     function getName () {
42         return "UpLoad";
43     }
44
45     function getDescription () {
46         return _("Upload files to the local InterWiki Upload:<filename>");
47     }
48
49     function getDefaultArguments() {
50         return array('logfile'  => 'phpwiki-upload.log',
51                      // add a link of the fresh file automatically to the 
52                      // end of the page (or current page)
53                      'autolink' => true, 
54                      'page'     => '[pagename]',
55                      );
56     }
57
58     function run($dbi, $argstr, &$request, $basepage) {
59         $this->disallowed_extensions = explode("\n",
60 "ad[ep]
61 asd
62 ba[st]
63 chm
64 cmd
65 com
66 cgi
67 cpl
68 crt
69 dll
70 eml
71 exe
72 hlp
73 hta
74 in[fs]
75 isp
76 jse?
77 lnk
78 md[betw]
79 ms[cipt]
80 nws
81 ocx
82 ops
83 pcd
84 p[ir]f
85 php
86 pl
87 py
88 reg
89 sc[frt]
90 sh[bsm]?
91 swf
92 url
93 vb[esx]?
94 vxd
95 ws[cfh]");
96         //removed "\{[[:xdigit:]]{8}(?:-[[:xdigit:]]{4}){3}-[[:xdigit:]]{12}\}"
97
98         $args = $this->getArgs($argstr, $request);
99         extract($args);
100
101         $file_dir = defined('PHPWIKI_DIR') ? 
102             PHPWIKI_DIR . "/uploads/" : "uploads/";
103         $url_prefix = SERVER_NAME . DATA_PATH; 
104
105         $form = HTML::form(array('action' => $request->getPostURL(),
106                                  'enctype' => 'multipart/form-data',
107                                  'method' => 'post'));
108         $contents = HTML::div(array('class' => 'wikiaction'));
109         $contents->pushContent(HTML::input(array('type' => 'hidden',
110                                                  'name' => 'MAX_FILE_SIZE',
111                                                  'value' => MAX_UPLOAD_SIZE)));
112         $contents->pushContent(HTML::input(array('name' => 'userfile',
113                                                  'type' => 'file',
114                                                  'size' => '50')));
115         $contents->pushContent(HTML::raw(" "));
116         $contents->pushContent(HTML::input(array('value' => _("Upload"),
117                                                  'type' => 'submit')));
118         $form->pushContent($contents);
119
120         $message = HTML();
121         $userfile = $request->getUploadedFile('userfile');
122         if ($userfile) {
123             $userfile_name = $userfile->getName();
124             $userfile_name = basename($userfile_name);
125             $userfile_tmpname = $userfile->getTmpName();
126
127             if ($this->only_authenticated) {
128                 // Make sure that the user is logged in.
129                 //
130                 $user = $request->getUser();
131                 if (!$user->isAuthenticated()) {
132                     $message->pushContent(_("ACCESS DENIED: You must log in to upload files."),
133                                           HTML::br(),HTML::br());
134                     $result = HTML();
135                     $result->pushContent($form);
136                     $result->pushContent($message);
137                     return $result;
138                 }
139             }
140
141             if (preg_match("/(\." . join("|\.", $this->disallowed_extensions) . ")\$/",
142                            $userfile_name))
143             {
144                 $message->pushContent(fmt("ERROR uploading '%s': ",$userfile_name));
145                 $message->pushContent(fmt("Files with extension %s are not allowed",
146                                           join(", ", $this->disallowed_extensions)),HTML::br(),HTML::br());
147             }
148             elseif (file_exists($file_dir . $userfile_name)) {
149                 $message->pushContent(fmt("ERROR uploading '%s': ",$userfile_name));
150                 $message->pushContent(fmt("There is already a file with name %s uploaded",
151                                           $userfile_name),HTML::br(),HTML::br());
152             }
153             elseif ($userfile->getSize() > (MAX_UPLOAD_SIZE)) {
154                 $message->pushContent(fmt("ERROR uploading '%s': ",$userfile_name));
155                 $message->pushContent(_("Sorry but this file is too big"),HTML::br(),HTML::br());
156             }
157             elseif (move_uploaded_file($userfile_tmpname, $file_dir . $userfile_name) or
158                     (IsWindows() and rename($userfile_tmpname, $file_dir . $userfile_name))
159                     )
160             {
161                 $interwiki = new PageType_interwikimap();
162                 $link = $interwiki->link("Upload:$userfile_name");
163                 $message->pushContent(_("File successfully uploaded."));
164                 $message->pushContent(HTML::ul(HTML::li($link)));
165
166                 // the upload was a success and we need to mark this event in the "upload log"
167                 if ($logfile) { 
168                     $upload_log = $file_dir . basename($logfile);
169                     $this->log($userfile, $upload_log, &$message);
170                 }
171                 if ($autolink) {
172                     require_once("lib/loadsave.php");
173                     $pagehandle = $dbi->getPage($page);
174                     if ($pagehandle->exists()) {// don't replace default contents
175                         $current = $pagehandle->getCurrentRevision();
176                         $version = $current->getVersion();
177                         $text = $current->getPackedContent();
178                         $newtext = $text . "\n* [Upload:$userfile_name]";
179                         $meta = $current->_data;
180                         $meta['summary'] = sprintf(_("uploaded %s"),$userfile_name);
181                         $pagehandle->save($newtext, $version + 1, $meta);
182                     }
183                 }
184             }
185             else {
186                 $message->pushContent(fmt("ERROR uploading '%s': ",$userfile_name));
187                 $message->pushContent(HTML::br(),_("Uploading failed."),HTML::br());
188             }
189         }
190         else {
191             $message->pushContent(HTML::br(),HTML::br());
192         }
193
194         //$result = HTML::div( array( 'class' => 'wikiaction' ) );
195         $result = HTML();
196         $result->pushContent($form);
197         $result->pushContent($message);
198         return $result;
199     }
200
201     function log ($userfile, $upload_log, &$message) {
202         global $Theme;
203         $user = $GLOBALS['request']->_user;
204         if (!is_writable($upload_log)) {
205             $message->pushContent(_("Error: the upload log is not writable"));
206             $message->pushContent(HTML::br());
207         }
208         elseif (!$log_handle = fopen ($upload_log, "a")) {
209             $message->pushContent(_("Error: can't open the upload logfile"));
210             $message->pushContent(HTML::br());
211         }
212         else {        // file size in KB; precision of 0.1
213             $file_size = round(($userfile->getSize())/1024, 1);
214             if ($file_size <= 0) {
215                 $file_size = "&lt; 0.1";
216             }
217             $userfile_name = $userfile->getName();
218             fwrite($log_handle,
219                    "\n"
220                    . "<tr><td><a href=\"$userfile_name\">$userfile_name</a></td>"
221                    . "<td align=\"right\">$file_size kB</td>"
222                    . "<td>&nbsp;&nbsp;" . $Theme->formatDate(time()) . "</td>"
223                    . "<td>&nbsp;&nbsp;<em>" . $user->getId() . "</em></td></tr>");
224             fclose($log_handle);
225         }
226         return;
227     }
228
229 }
230
231 // $Log: not supported by cvs2svn $
232 // Revision 1.11  2004/06/11 09:07:30  rurban
233 // support theme-specific LinkIconAttr: front or after or none
234 //
235 // Revision 1.10  2004/04/12 10:19:18  rurban
236 // fixed copyright year
237 //
238 // Revision 1.9  2004/04/12 10:18:22  rurban
239 // removed the hairy regex line
240 //
241 // Revision 1.8  2004/04/12 09:12:22  rurban
242 // fix syntax errors
243 //
244 // Revision 1.7  2004/04/09 17:49:03  rurban
245 // Added PhpWiki RssFeed to Sidebar
246 // sidebar formatting
247 // some browser dependant fixes (old-browser support)
248 //
249 // Revision 1.6  2004/02/27 01:36:51  rurban
250 // autolink enabled
251 //
252 // Revision 1.5  2004/02/27 01:24:43  rurban
253 // use IntwerWiki links for uploaded file.
254 // autolink to page prepared, but not yet ready
255 //
256 // Revision 1.4  2004/02/21 19:12:59  rurban
257 // patch by Sascha Carlin
258 //
259 // Revision 1.3  2004/02/17 12:11:36  rurban
260 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
261 //
262 // Revision 1.2  2004/01/26 09:18:00  rurban
263 // * changed stored pref representation as before.
264 //   the array of objects is 1) bigger and 2)
265 //   less portable. If we would import packed pref
266 //   objects and the object definition was changed, PHP would fail.
267 //   This doesn't happen with an simple array of non-default values.
268 // * use $prefs->retrieve and $prefs->store methods, where retrieve
269 //   understands the interim format of array of objects also.
270 // * simplified $prefs->get() and fixed $prefs->set()
271 // * added $user->_userid and class '_WikiUser' portability functions
272 // * fixed $user object ->_level upgrading, mostly using sessions.
273 //   this fixes yesterdays problems with loosing authorization level.
274 // * fixed WikiUserNew::checkPass to return the _level
275 // * fixed WikiUserNew::isSignedIn
276 // * added explodePageList to class PageList, support sortby arg
277 // * fixed UserPreferences for WikiUserNew
278 // * fixed WikiPlugin for empty defaults array
279 // * UnfoldSubpages: added pagename arg, renamed pages arg,
280 //   removed sort arg, support sortby arg
281 //
282 // Revision 1.1  2003/11/04 18:41:41  carstenklapp
283 // New plugin which was submitted to the mailing list some time
284 // ago. (This is the best UpLoad function I have seen for PhpWiki so
285 // far. Cleaned up text formatting and typos from the version on the
286 // mailing list. Still needs a few adjustments.)
287
288 // (c-file-style: "gnu")
289 // Local Variables:
290 // mode: php
291 // tab-width: 8
292 // c-basic-offset: 4
293 // c-hanging-comment-ender-p: nil
294 // indent-tabs-mode: nil
295 // End:
296 ?>