2 rcs_id('$Id: UpLoad.php,v 1.15 2004-09-22 13:46:26 rurban Exp $');
4 Copyright 2003, 2004 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
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.
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.
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
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
34 class WikiPlugin_UpLoad
37 var $disallowed_extensions;
38 // TODO: use PagePerms instead
39 var $only_authenticated = true; // allow only authenticated users may upload.
45 function getDescription () {
46 return _("Upload files to the local InterWiki Upload:<filename>");
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)
54 'page' => '[pagename]',
58 function run($dbi, $argstr, &$request, $basepage) {
59 $this->disallowed_extensions = explode("\n",
96 //removed "\{[[:xdigit:]]{8}(?:-[[:xdigit:]]{4}){3}-[[:xdigit:]]{12}\}"
98 $args = $this->getArgs($argstr, $request);
101 $file_dir = getUploadFilePath();
102 //$url_prefix = SERVER_NAME . DATA_PATH;
104 $form = HTML::form(array('action' => $request->getPostURL(),
105 'enctype' => 'multipart/form-data',
106 'method' => 'post'));
107 $contents = HTML::div(array('class' => 'wikiaction'));
108 $contents->pushContent(HTML::input(array('type' => 'hidden',
109 'name' => 'MAX_FILE_SIZE',
110 'value' => MAX_UPLOAD_SIZE)));
111 $contents->pushContent(HTML::input(array('name' => 'userfile',
114 $contents->pushContent(HTML::raw(" "));
115 $contents->pushContent(HTML::input(array('value' => _("Upload"),
116 'type' => 'submit')));
117 $form->pushContent($contents);
120 $userfile = $request->getUploadedFile('userfile');
122 $userfile_name = $userfile->getName();
123 $userfile_name = basename($userfile_name);
124 $userfile_tmpname = $userfile->getTmpName();
126 if ($this->only_authenticated) {
127 // Make sure that the user is logged in.
129 $user = $request->getUser();
130 if (!$user->isAuthenticated()) {
131 $message->pushContent(_("ACCESS DENIED: You must log in to upload files."),
132 HTML::br(),HTML::br());
134 $result->pushContent($form);
135 $result->pushContent($message);
140 if (preg_match("/(\." . join("|\.", $this->disallowed_extensions) . ")\$/",
143 $message->pushContent(fmt("ERROR uploading '%s': ",$userfile_name));
144 $message->pushContent(fmt("Files with extension %s are not allowed",
145 join(", ", $this->disallowed_extensions)),HTML::br(),HTML::br());
147 elseif (file_exists($file_dir . $userfile_name)) {
148 $message->pushContent(fmt("ERROR uploading '%s': ",$userfile_name));
149 $message->pushContent(fmt("There is already a file with name %s uploaded",
150 $userfile_name),HTML::br(),HTML::br());
152 elseif ($userfile->getSize() > (MAX_UPLOAD_SIZE)) {
153 $message->pushContent(fmt("ERROR uploading '%s': ",$userfile_name));
154 $message->pushContent(_("Sorry but this file is too big"),HTML::br(),HTML::br());
156 elseif (move_uploaded_file($userfile_tmpname, $file_dir . $userfile_name) or
157 (IsWindows() and rename($userfile_tmpname, $file_dir . $userfile_name))
160 $interwiki = new PageType_interwikimap();
161 $link = $interwiki->link("Upload:$userfile_name");
162 $message->pushContent(_("File successfully uploaded."));
163 $message->pushContent(HTML::ul(HTML::li($link)));
165 // the upload was a success and we need to mark this event in the "upload log"
167 $upload_log = $file_dir . basename($logfile);
168 $this->log($userfile, $upload_log, $message);
171 require_once("lib/loadsave.php");
172 $pagehandle = $dbi->getPage($page);
173 if ($pagehandle->exists()) {// don't replace default contents
174 $current = $pagehandle->getCurrentRevision();
175 $version = $current->getVersion();
176 $text = $current->getPackedContent();
177 $newtext = $text . "\n* [Upload:$userfile_name]";
178 $meta = $current->_data;
179 $meta['summary'] = sprintf(_("uploaded %s"),$userfile_name);
180 $pagehandle->save($newtext, $version + 1, $meta);
185 $message->pushContent(fmt("ERROR uploading '%s': ",$userfile_name));
186 $message->pushContent(HTML::br(),_("Uploading failed."),HTML::br());
190 $message->pushContent(HTML::br(),HTML::br());
193 //$result = HTML::div( array( 'class' => 'wikiaction' ) );
195 $result->pushContent($form);
196 $result->pushContent($message);
200 function log ($userfile, $upload_log, &$message) {
202 $user = $GLOBALS['request']->_user;
203 if (!is_writable($upload_log)) {
204 $message->pushContent(_("Error: the upload log is not writable"));
205 $message->pushContent(HTML::br());
207 elseif (!$log_handle = fopen ($upload_log, "a")) {
208 $message->pushContent(_("Error: can't open the upload logfile"));
209 $message->pushContent(HTML::br());
211 else { // file size in KB; precision of 0.1
212 $file_size = round(($userfile->getSize())/1024, 1);
213 if ($file_size <= 0) {
214 $file_size = "< 0.1";
216 $userfile_name = $userfile->getName();
219 . "<tr><td><a href=\"$userfile_name\">$userfile_name</a></td>"
220 . "<td align=\"right\">$file_size kB</td>"
221 . "<td> " . $WikiTheme->formatDate(time()) . "</td>"
222 . "<td> <em>" . $user->getId() . "</em></td></tr>");
230 // $Log: not supported by cvs2svn $
231 // Revision 1.14 2004/06/16 10:38:59 rurban
232 // Disallow refernces in calls if the declaration is a reference
233 // ("allow_call_time_pass_reference clean").
234 // PhpWiki is now allow_call_time_pass_reference = Off clean,
235 // but several external libraries may not.
236 // In detail these libs look to be affected (not tested):
240 // Revision 1.13 2004/06/14 11:31:39 rurban
241 // renamed global $Theme to $WikiTheme (gforge nameclash)
242 // inherit PageList default options from PageList
243 // default sortby=pagename
244 // use options in PageList_Selectable (limit, sortby, ...)
245 // added action revert, with button at action=diff
246 // added option regex to WikiAdminSearchReplace
248 // Revision 1.12 2004/06/13 11:34:22 rurban
249 // fixed bug #969532 (space in uploaded filenames)
250 // improved upload error messages
252 // Revision 1.11 2004/06/11 09:07:30 rurban
253 // support theme-specific LinkIconAttr: front or after or none
255 // Revision 1.10 2004/04/12 10:19:18 rurban
256 // fixed copyright year
258 // Revision 1.9 2004/04/12 10:18:22 rurban
259 // removed the hairy regex line
261 // Revision 1.8 2004/04/12 09:12:22 rurban
264 // Revision 1.7 2004/04/09 17:49:03 rurban
265 // Added PhpWiki RssFeed to Sidebar
266 // sidebar formatting
267 // some browser dependant fixes (old-browser support)
269 // Revision 1.6 2004/02/27 01:36:51 rurban
272 // Revision 1.5 2004/02/27 01:24:43 rurban
273 // use IntwerWiki links for uploaded file.
274 // autolink to page prepared, but not yet ready
276 // Revision 1.4 2004/02/21 19:12:59 rurban
277 // patch by Sascha Carlin
279 // Revision 1.3 2004/02/17 12:11:36 rurban
280 // 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, ...)
282 // Revision 1.2 2004/01/26 09:18:00 rurban
283 // * changed stored pref representation as before.
284 // the array of objects is 1) bigger and 2)
285 // less portable. If we would import packed pref
286 // objects and the object definition was changed, PHP would fail.
287 // This doesn't happen with an simple array of non-default values.
288 // * use $prefs->retrieve and $prefs->store methods, where retrieve
289 // understands the interim format of array of objects also.
290 // * simplified $prefs->get() and fixed $prefs->set()
291 // * added $user->_userid and class '_WikiUser' portability functions
292 // * fixed $user object ->_level upgrading, mostly using sessions.
293 // this fixes yesterdays problems with loosing authorization level.
294 // * fixed WikiUserNew::checkPass to return the _level
295 // * fixed WikiUserNew::isSignedIn
296 // * added explodePageList to class PageList, support sortby arg
297 // * fixed UserPreferences for WikiUserNew
298 // * fixed WikiPlugin for empty defaults array
299 // * UnfoldSubpages: added pagename arg, renamed pages arg,
300 // removed sort arg, support sortby arg
302 // Revision 1.1 2003/11/04 18:41:41 carstenklapp
303 // New plugin which was submitted to the mailing list some time
304 // ago. (This is the best UpLoad function I have seen for PhpWiki so
305 // far. Cleaned up text formatting and typos from the version on the
306 // mailing list. Still needs a few adjustments.)
308 // (c-file-style: "gnu")
313 // c-hanging-comment-ender-p: nil
314 // indent-tabs-mode: nil