4 * Copyright 2003,2004,2007 $ThePhpWikiProgrammingTeam
5 * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * UpLoad: Allow Administrator to upload files to a special directory,
27 * which should preferably be added to the InterWikiMap
29 * Author: NathanGass <gass@iogram.ch>
30 * Changes: ReiniUrban <rurban@x-ray.at>,
31 * qubit <rtryon@dartmouth.edu>
32 * Marc-Etienne Vargenau, Alcatel-Lucent
33 * Note: See also Jochen Kalmbach's plugin/UserFileManagement.php
36 class WikiPlugin_UpLoad
39 var $disallowed_extensions;
40 // TODO: use PagePerms instead
41 var $only_authenticated = true; // allow only authenticated users may upload.
47 function getDescription () {
48 return _("Upload files to the local InterWiki Upload:<filename>");
51 function getDefaultArguments() {
52 return array('logfile' => 'phpwiki-upload.log',
53 // add a link of the fresh file automatically to the
54 // end of the page (or current page)
56 'page' => '[pagename]',
58 'mode' => 'actionpage', // or edit
62 function run($dbi, $argstr, &$request, $basepage) {
63 $this->allowed_extensions = explode("\n",
102 $this->disallowed_extensions = explode("\n",
139 //removed "\{[[:xdigit:]]{8}(?:-[[:xdigit:]]{4}){3}-[[:xdigit:]]{12}\}"
141 $args = $this->getArgs($argstr, $request);
144 $file_dir = getUploadFilePath();
146 $form = HTML::form(array('action' => $request->getPostURL(),
147 'enctype' => 'multipart/form-data',
148 'method' => 'post'));
149 $contents = HTML::div(array('class' => 'wikiaction'));
150 $contents->pushContent(HTML::input(array('type' => 'hidden',
151 'name' => 'MAX_FILE_SIZE',
152 'value'=> MAX_UPLOAD_SIZE)));
153 $contents->pushContent(HTML::input(array('name' => 'userfile',
156 if ($mode == 'edit') {
157 $contents->pushContent(HTML::input(array('name' => 'action',
160 $contents->pushContent(HTML::raw(" "));
161 $contents->pushContent(HTML::input(array('value' => _("Upload"),
162 'name' => 'edit[upload]',
163 'type' => 'submit')));
165 $contents->pushContent(HTML::raw(" "));
166 $contents->pushContent(HTML::input(array('value' => _("Upload"),
167 'type' => 'submit')));
169 $form->pushContent($contents);
172 if ($request->isPost() and $this->only_authenticated) {
173 // Make sure that the user is logged in.
174 $user = $request->getUser();
175 if (!$user->isAuthenticated()) {
176 if (defined('FUSIONFORGE') and FUSIONFORGE) {
177 $message->pushContent(HTML::div(array('class' => 'error'),
178 HTML::p(_("You cannot upload files.")),
180 HTML::li(_("Check you are logged in.")),
181 HTML::li(_("Check you are in the right project.")),
182 HTML::li(_("Check you are a member of the current project."))
186 $message->pushContent(HTML::div(array('class' => 'error'),
187 HTML::p(_("ACCESS DENIED: You must log in to upload files."))));
190 $result->pushContent($form);
191 $result->pushContent($message);
196 $userfile = $request->getUploadedFile('userfile');
198 $userfile_name = $userfile->getName();
199 $userfile_name = trim(basename($userfile_name));
200 if (UPLOAD_USERDIR) {
201 $file_dir .= $request->_user->_userid;
202 if (!file_exists($file_dir))
203 mkdir($file_dir, 0775);
205 $u_userfile = $request->_user->_userid . "/" . $userfile_name;
207 $u_userfile = $userfile_name;
209 $u_userfile = preg_replace("/ /", "%20", $u_userfile);
210 $userfile_tmpname = $userfile->getTmpName();
211 $err_header = HTML::div(array('class' => 'error'),
212 HTML::p(fmt("ERROR uploading '%s'", $userfile_name)));
213 if (preg_match("/(\." . join("|\.", $this->disallowed_extensions) . ")(\.|\$)/i",
216 $message->pushContent($err_header);
217 $message->pushContent(HTML::p(fmt("Files with extension %s are not allowed.",
218 join(", ", $this->disallowed_extensions))));
220 elseif (! DISABLE_UPLOAD_ONLY_ALLOWED_EXTENSIONS and
221 ! preg_match("/(\." . join("|\.", $this->allowed_extensions) . ")\$/i",
224 $message->pushContent($err_header);
225 $message->pushContent(HTML::p(fmt("Only files with the extension %s are allowed.",
226 join(", ", $this->allowed_extensions))));
228 elseif (preg_match("/[^._a-zA-Z0-9- ]/", strip_accents($userfile_name)))
230 $message->pushContent($err_header);
231 $message->pushContent(HTML::p(_("Invalid filename. File names may only contain alphanumeric characters and dot, underscore, space or dash.")));
233 elseif (file_exists($file_dir . $userfile_name)) {
234 $message->pushContent($err_header);
235 $message->pushContent(HTML::p(fmt("There is already a file with name %s uploaded.",
238 elseif ($userfile->getSize() > (MAX_UPLOAD_SIZE)) {
239 $message->pushContent($err_header);
240 $message->pushContent(HTML::p(_("Sorry but this file is too big.")));
242 elseif (move_uploaded_file($userfile_tmpname, $file_dir . $userfile_name) or
243 (IsWindows() and rename($userfile_tmpname, $file_dir . $userfile_name))
246 $interwiki = new PageType_interwikimap();
247 $link = $interwiki->link("Upload:$u_userfile");
248 $message->pushContent(HTML::div(array('class' => 'feedback'),
249 HTML::p(_("File successfully uploaded.")),
252 // the upload was a success and we need to mark this event in the "upload log"
254 $upload_log = $file_dir . basename($logfile);
255 $this->log($userfile, $upload_log, $message);
258 require_once 'lib/loadsave.php';
259 $pagehandle = $dbi->getPage($page);
260 if ($pagehandle->exists()) {// don't replace default contents
261 $current = $pagehandle->getCurrentRevision();
262 $version = $current->getVersion();
263 $text = $current->getPackedContent();
264 $newtext = $text . "\n* Upload:$u_userfile"; // don't inline images
265 $meta = $current->_data;
266 $meta['summary'] = sprintf(_("uploaded %s"),$u_userfile);
267 $pagehandle->save($newtext, $version + 1, $meta);
271 $message->pushContent($err_header);
272 $message->pushContent(HTML::br(),_("Uploading failed."),HTML::br());
276 $message->pushContent(HTML::br(),_("No file selected. Please select one."),HTML::br());
279 //$result = HTML::div( array( 'class' => 'wikiaction' ) );
281 $result->pushContent($form);
282 $result->pushContent($message);
286 function log ($userfile, $upload_log, &$message) {
288 $user = $GLOBALS['request']->_user;
289 if (file_exists($upload_log) and (!is_writable($upload_log))) {
290 trigger_error(_("The upload logfile exists but is not writable."), E_USER_WARNING);
292 elseif (!$log_handle = fopen ($upload_log, "a")) {
293 trigger_error(_("Can't open the upload logfile."), E_USER_WARNING);
295 else { // file size in KB; precision of 0.1
296 $file_size = round(($userfile->getSize())/1024, 1);
297 if ($file_size <= 0) {
298 $file_size = "< 0.1";
300 $userfile_name = $userfile->getName();
303 . "<tr><td><a href=\"$userfile_name\">$userfile_name</a></td>"
304 . "<td align=\"right\">$file_size kB</td>"
305 . "<td> " . $WikiTheme->formatDate(time()) . "</td>"
306 . "<td> <em>" . $user->getId() . "</em></td></tr>");
318 // c-hanging-comment-ender-p: nil
319 // indent-tabs-mode: nil