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.
48 function getDescription()
50 return _("Upload files to the local InterWiki Upload:<filename>");
53 function getDefaultArguments()
55 return array('logfile' => 'phpwiki-upload.log',
56 // add a link of the fresh file automatically to the
57 // end of the page (or current page)
59 'page' => '[pagename]',
61 'mode' => 'actionpage', // or edit
65 function run($dbi, $argstr, &$request, $basepage)
67 $this->allowed_extensions = explode("\n",
106 $this->disallowed_extensions = explode("\n",
143 //removed "\{[[:xdigit:]]{8}(?:-[[:xdigit:]]{4}){3}-[[:xdigit:]]{12}\}"
145 $args = $this->getArgs($argstr, $request);
148 $file_dir = getUploadFilePath();
150 $form = HTML::form(array('action' => $request->getPostURL(),
151 'enctype' => 'multipart/form-data',
152 'method' => 'post'));
153 $contents = HTML::div(array('class' => 'wikiaction'));
154 $contents->pushContent(HTML::input(array('type' => 'hidden',
155 'name' => 'MAX_FILE_SIZE',
156 'value' => MAX_UPLOAD_SIZE)));
157 $contents->pushContent(HTML::input(array('name' => 'userfile',
160 if ($mode == 'edit') {
161 $contents->pushContent(HTML::input(array('name' => 'action',
163 'value' => 'edit')));
164 $contents->pushContent(HTML::raw(" "));
165 $contents->pushContent(HTML::input(array('value' => _("Upload"),
166 'name' => 'edit[upload]',
167 'type' => 'submit')));
169 $contents->pushContent(HTML::raw(" "));
170 $contents->pushContent(HTML::input(array('value' => _("Upload"),
171 'type' => 'submit')));
173 $form->pushContent($contents);
176 if ($request->isPost() and $this->only_authenticated) {
177 // Make sure that the user is logged in.
178 $user = $request->getUser();
179 if (!$user->isAuthenticated()) {
180 if (defined('FUSIONFORGE') and FUSIONFORGE) {
181 $message->pushContent(HTML::div(array('class' => 'error'),
182 HTML::p(_("You cannot upload files.")),
184 HTML::li(_("Check you are logged in.")),
185 HTML::li(_("Check you are in the right project.")),
186 HTML::li(_("Check you are a member of the current project."))
190 $message->pushContent(HTML::div(array('class' => 'error'),
191 HTML::p(_("ACCESS DENIED: You must log in to upload files."))));
194 $result->pushContent($form);
195 $result->pushContent($message);
200 $userfile = $request->getUploadedFile('userfile');
202 $userfile_name = $userfile->getName();
203 $userfile_name = trim(basename($userfile_name));
204 if (UPLOAD_USERDIR) {
205 $file_dir .= $request->_user->_userid;
206 if (!file_exists($file_dir))
207 mkdir($file_dir, 0775);
209 $u_userfile = $request->_user->_userid . "/" . $userfile_name;
211 $u_userfile = $userfile_name;
213 $u_userfile = preg_replace("/ /", "%20", $u_userfile);
214 $userfile_tmpname = $userfile->getTmpName();
215 $err_header = HTML::div(array('class' => 'error'),
216 HTML::p(fmt("ERROR uploading '%s'", $userfile_name)));
217 if (preg_match("/(\." . join("|\.", $this->disallowed_extensions) . ")(\.|\$)/i",
220 $message->pushContent($err_header);
221 $message->pushContent(HTML::p(fmt("Files with extension %s are not allowed.",
222 join(", ", $this->disallowed_extensions))));
223 } elseif (!DISABLE_UPLOAD_ONLY_ALLOWED_EXTENSIONS and
224 !preg_match("/(\." . join("|\.", $this->allowed_extensions) . ")\$/i",
227 $message->pushContent($err_header);
228 $message->pushContent(HTML::p(fmt("Only files with the extension %s are allowed.",
229 join(", ", $this->allowed_extensions))));
230 } elseif (preg_match("/[^._a-zA-Z0-9- ]/", strip_accents($userfile_name))) {
231 $message->pushContent($err_header);
232 $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.",
237 } elseif ($userfile->getSize() > (MAX_UPLOAD_SIZE)) {
238 $message->pushContent($err_header);
239 $message->pushContent(HTML::p(_("Sorry but this file is too big.")));
240 } elseif (move_uploaded_file($userfile_tmpname, $file_dir . $userfile_name) or
241 (IsWindows() and rename($userfile_tmpname, $file_dir . $userfile_name))
243 $interwiki = new PageType_interwikimap();
244 $link = $interwiki->link("Upload:$u_userfile");
245 $message->pushContent(HTML::div(array('class' => 'feedback'),
246 HTML::p(_("File successfully uploaded.")),
249 // the upload was a success and we need to mark this event in the "upload log"
251 $upload_log = $file_dir . basename($logfile);
252 $this->log($userfile, $upload_log, $message);
255 require_once 'lib/loadsave.php';
256 $pagehandle = $dbi->getPage($page);
257 if ($pagehandle->exists()) { // don't replace default contents
258 $current = $pagehandle->getCurrentRevision();
259 $version = $current->getVersion();
260 $text = $current->getPackedContent();
261 $newtext = $text . "\n* Upload:$u_userfile"; // don't inline images
262 $meta = $current->_data;
263 $meta['summary'] = sprintf(_("uploaded %s"), $u_userfile);
264 $pagehandle->save($newtext, $version + 1, $meta);
268 $message->pushContent($err_header);
269 $message->pushContent(HTML::br(), _("Uploading failed."), HTML::br());
272 $message->pushContent(HTML::br(), _("No file selected. Please select one."), HTML::br());
275 //$result = HTML::div( array( 'class' => 'wikiaction' ) );
277 $result->pushContent($form);
278 $result->pushContent($message);
282 function log($userfile, $upload_log, &$message)
285 $user = $GLOBALS['request']->_user;
286 if (file_exists($upload_log) and (!is_writable($upload_log))) {
287 trigger_error(_("The upload logfile exists but is not writable."), E_USER_WARNING);
288 } elseif (!$log_handle = fopen($upload_log, "a")) {
289 trigger_error(_("Can't open the upload logfile."), E_USER_WARNING);
290 } else { // file size in KB; precision of 0.1
291 $file_size = round(($userfile->getSize()) / 1024, 1);
292 if ($file_size <= 0) {
293 $file_size = "< 0.1";
295 $userfile_name = $userfile->getName();
298 . "<tr><td><a href=\"$userfile_name\">$userfile_name</a></td>"
299 . "<td align=\"right\">$file_size kB</td>"
300 . "<td> " . $WikiTheme->formatDate(time()) . "</td>"
301 . "<td> <em>" . $user->getId() . "</em></td></tr>");
313 // c-hanging-comment-ender-p: nil
314 // indent-tabs-mode: nil