2 rcs_id('$Id: UpLoad.php,v 1.19 2005-04-11 19:40:15 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 if ($request->isPost() and $this->only_authenticated) {
121 // Make sure that the user is logged in.
122 $user = $request->getUser();
123 if (!$user->isAuthenticated()) {
124 $message->pushContent(HTML::h2(_("ACCESS DENIED: You must log in to upload files.")),
125 HTML::br(),HTML::br());
127 $result->pushContent($form);
128 $result->pushContent($message);
133 $userfile = $request->getUploadedFile('userfile');
135 $userfile_name = $userfile->getName();
136 $userfile_name = trim(basename($userfile_name));
137 $userfile_tmpname = $userfile->getTmpName();
138 $err_header = HTML::h2(fmt("ERROR uploading '%s': ", $userfile_name));
139 if (preg_match("/(\." . join("|\.", $this->disallowed_extensions) . ")\$/",
142 $message->pushContent($err_header);
143 $message->pushContent(fmt("Files with extension %s are not allowed.",
144 join(", ", $this->disallowed_extensions)),HTML::br(),HTML::br());
146 elseif (preg_match("/[^._a-zA-Z0-9-]/", $userfile_name))
148 $message->pushContent($err_header);
149 $message->pushContent(_("File names may only contain alphanumeric characters and dot, underscore or dash."),
150 HTML::br(),HTML::br());
152 elseif (file_exists($file_dir . $userfile_name)) {
153 $message->pushContent($err_header);
154 $message->pushContent(fmt("There is already a file with name %s uploaded.",
155 $userfile_name),HTML::br(),HTML::br());
157 elseif ($userfile->getSize() > (MAX_UPLOAD_SIZE)) {
158 $message->pushContent($err_header);
159 $message->pushContent(_("Sorry but this file is too big."),HTML::br(),HTML::br());
161 elseif (move_uploaded_file($userfile_tmpname, $file_dir . $userfile_name) or
162 (IsWindows() and rename($userfile_tmpname, $file_dir . $userfile_name))
165 $interwiki = new PageType_interwikimap();
166 $link = $interwiki->link("Upload:$userfile_name");
167 $message->pushContent(HTML::h2(_("File successfully uploaded.")));
168 $message->pushContent(HTML::ul(HTML::li($link)));
170 // the upload was a success and we need to mark this event in the "upload log"
172 $upload_log = $file_dir . basename($logfile);
173 $this->log($userfile, $upload_log, $message);
176 require_once("lib/loadsave.php");
177 $pagehandle = $dbi->getPage($page);
178 if ($pagehandle->exists()) {// don't replace default contents
179 $current = $pagehandle->getCurrentRevision();
180 $version = $current->getVersion();
181 $text = $current->getPackedContent();
182 $newtext = $text . "\n* [Upload:$userfile_name]";
183 $meta = $current->_data;
184 $meta['summary'] = sprintf(_("uploaded %s"),$userfile_name);
185 $pagehandle->save($newtext, $version + 1, $meta);
190 $message->pushContent($err_header);
191 $message->pushContent(HTML::br(),_("Uploading failed."),HTML::br());
195 $message->pushContent(HTML::br(),HTML::br());
198 //$result = HTML::div( array( 'class' => 'wikiaction' ) );
200 $result->pushContent($form);
201 $result->pushContent($message);
205 function log ($userfile, $upload_log, &$message) {
207 $user = $GLOBALS['request']->_user;
208 if (!is_writable($upload_log)) {
209 trigger_error(_("The upload logfile is not writable."), E_USER_WARNING);
211 elseif (!$log_handle = fopen ($upload_log, "a")) {
212 trigger_error(_("Can't open the upload logfile."), E_USER_WARNING);
214 else { // file size in KB; precision of 0.1
215 $file_size = round(($userfile->getSize())/1024, 1);
216 if ($file_size <= 0) {
217 $file_size = "< 0.1";
219 $userfile_name = $userfile->getName();
222 . "<tr><td><a href=\"$userfile_name\">$userfile_name</a></td>"
223 . "<td align=\"right\">$file_size kB</td>"
224 . "<td> " . $WikiTheme->formatDate(time()) . "</td>"
225 . "<td> <em>" . $user->getId() . "</em></td></tr>");
233 // $Log: not supported by cvs2svn $
234 // Revision 1.18 2005/02/12 17:24:24 rurban
235 // locale update: missing . : fixed. unified strings
238 // Revision 1.17 2004/11/09 08:15:50 rurban
241 // Revision 1.16 2004/10/21 19:03:37 rurban
242 // Be more stricter with uploads: Filenames may only contain alphanumeric
243 // characters. Patch #1037825
245 // Revision 1.15 2004/09/22 13:46:26 rurban
246 // centralize upload paths.
247 // major WikiPluginCached feature enhancement:
248 // support _STATIC pages in uploads/ instead of dynamic getimg.php? subrequests.
249 // mainly for debugging, cache problems and action=pdf
251 // Revision 1.14 2004/06/16 10:38:59 rurban
252 // Disallow refernces in calls if the declaration is a reference
253 // ("allow_call_time_pass_reference clean").
254 // PhpWiki is now allow_call_time_pass_reference = Off clean,
255 // but several external libraries may not.
256 // In detail these libs look to be affected (not tested):
260 // Revision 1.13 2004/06/14 11:31:39 rurban
261 // renamed global $Theme to $WikiTheme (gforge nameclash)
262 // inherit PageList default options from PageList
263 // default sortby=pagename
264 // use options in PageList_Selectable (limit, sortby, ...)
265 // added action revert, with button at action=diff
266 // added option regex to WikiAdminSearchReplace
268 // Revision 1.12 2004/06/13 11:34:22 rurban
269 // fixed bug #969532 (space in uploaded filenames)
270 // improved upload error messages
272 // Revision 1.11 2004/06/11 09:07:30 rurban
273 // support theme-specific LinkIconAttr: front or after or none
275 // Revision 1.10 2004/04/12 10:19:18 rurban
276 // fixed copyright year
278 // Revision 1.9 2004/04/12 10:18:22 rurban
279 // removed the hairy regex line
281 // Revision 1.8 2004/04/12 09:12:22 rurban
284 // Revision 1.7 2004/04/09 17:49:03 rurban
285 // Added PhpWiki RssFeed to Sidebar
286 // sidebar formatting
287 // some browser dependant fixes (old-browser support)
289 // Revision 1.6 2004/02/27 01:36:51 rurban
292 // Revision 1.5 2004/02/27 01:24:43 rurban
293 // use IntwerWiki links for uploaded file.
294 // autolink to page prepared, but not yet ready
296 // Revision 1.4 2004/02/21 19:12:59 rurban
297 // patch by Sascha Carlin
299 // Revision 1.3 2004/02/17 12:11:36 rurban
300 // 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, ...)
302 // Revision 1.2 2004/01/26 09:18:00 rurban
303 // * changed stored pref representation as before.
304 // the array of objects is 1) bigger and 2)
305 // less portable. If we would import packed pref
306 // objects and the object definition was changed, PHP would fail.
307 // This doesn't happen with an simple array of non-default values.
308 // * use $prefs->retrieve and $prefs->store methods, where retrieve
309 // understands the interim format of array of objects also.
310 // * simplified $prefs->get() and fixed $prefs->set()
311 // * added $user->_userid and class '_WikiUser' portability functions
312 // * fixed $user object ->_level upgrading, mostly using sessions.
313 // this fixes yesterdays problems with loosing authorization level.
314 // * fixed WikiUserNew::checkPass to return the _level
315 // * fixed WikiUserNew::isSignedIn
316 // * added explodePageList to class PageList, support sortby arg
317 // * fixed UserPreferences for WikiUserNew
318 // * fixed WikiPlugin for empty defaults array
319 // * UnfoldSubpages: added pagename arg, renamed pages arg,
320 // removed sort arg, support sortby arg
322 // Revision 1.1 2003/11/04 18:41:41 carstenklapp
323 // New plugin which was submitted to the mailing list some time
324 // ago. (This is the best UpLoad function I have seen for PhpWiki so
325 // far. Cleaned up text formatting and typos from the version on the
326 // mailing list. Still needs a few adjustments.)
328 // (c-file-style: "gnu")
333 // c-hanging-comment-ender-p: nil
334 // indent-tabs-mode: nil