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
20 * along with PhpWiki; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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",
140 //removed "\{[[:xdigit:]]{8}(?:-[[:xdigit:]]{4}){3}-[[:xdigit:]]{12}\}"
142 $args = $this->getArgs($argstr, $request);
145 $file_dir = getUploadFilePath();
147 $form = HTML::form(array('action' => $request->getPostURL(),
148 'enctype' => 'multipart/form-data',
149 'method' => 'post'));
150 $contents = HTML::div(array('class' => 'wikiaction'));
151 $contents->pushContent(HTML::input(array('type' => 'hidden',
152 'name' => 'MAX_FILE_SIZE',
153 'value'=> MAX_UPLOAD_SIZE)));
154 $contents->pushContent(HTML::input(array('name' => 'userfile',
157 if ($mode == 'edit') {
158 $contents->pushContent(HTML::input(array('name' => 'action',
161 $contents->pushContent(HTML::raw(" "));
162 $contents->pushContent(HTML::input(array('value' => _("Upload"),
163 'name' => 'edit[upload]',
164 'type' => 'submit')));
166 $contents->pushContent(HTML::raw(" "));
167 $contents->pushContent(HTML::input(array('value' => _("Upload"),
168 'type' => 'submit')));
170 $form->pushContent($contents);
173 if ($request->isPost() and $this->only_authenticated) {
174 // Make sure that the user is logged in.
175 $user = $request->getUser();
176 if (!$user->isAuthenticated()) {
177 if (defined('GFORGE') and GFORGE) {
178 $message->pushContent(HTML::div(array('class' => 'error'),
179 HTML::p(_("You cannot upload files.")),
181 HTML::li(_("Check you are logged in.")),
182 HTML::li(_("Check you are in the right project.")),
183 HTML::li(_("Check you are a member of the current project."))
187 $message->pushContent(HTML::div(array('class' => 'error'),
188 HTML::p(_("ACCESS DENIED: You must log in to upload files."))));
191 $result->pushContent($form);
192 $result->pushContent($message);
197 $userfile = $request->getUploadedFile('userfile');
199 $userfile_name = $userfile->getName();
200 $userfile_name = trim(basename($userfile_name));
201 if (UPLOAD_USERDIR) {
202 $file_dir .= $request->_user->_userid;
203 if (!file_exists($file_dir))
204 mkdir($file_dir, 0775);
206 $u_userfile = $request->_user->_userid . "/" . $userfile_name;
208 $u_userfile = $userfile_name;
210 $u_userfile = preg_replace("/ /", "%20", $u_userfile);
211 $userfile_tmpname = $userfile->getTmpName();
212 $err_header = HTML::div(array('class' => 'error'),
213 HTML::p(fmt("ERROR uploading '%s'", $userfile_name)));
214 if (preg_match("/(\." . join("|\.", $this->disallowed_extensions) . ")(\.|\$)/i",
217 $message->pushContent($err_header);
218 $message->pushContent(HTML::p(fmt("Files with extension %s are not allowed.",
219 join(", ", $this->disallowed_extensions))));
221 elseif (! DISABLE_UPLOAD_ONLY_ALLOWED_EXTENSIONS and
222 ! preg_match("/(\." . join("|\.", $this->allowed_extensions) . ")\$/i",
225 $message->pushContent($err_header);
226 $message->pushContent(HTML::p(fmt("Only files with the extension %s are allowed.",
227 join(", ", $this->allowed_extensions))));
229 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.")));
234 elseif (file_exists($file_dir . $userfile_name)) {
235 $message->pushContent($err_header);
236 $message->pushContent(HTML::p(fmt("There is already a file with name %s uploaded.",
239 elseif ($userfile->getSize() > (MAX_UPLOAD_SIZE)) {
240 $message->pushContent($err_header);
241 $message->pushContent(HTML::p(_("Sorry but this file is too big.")));
243 elseif (move_uploaded_file($userfile_tmpname, $file_dir . $userfile_name) or
244 (IsWindows() and rename($userfile_tmpname, $file_dir . $userfile_name))
247 $interwiki = new PageType_interwikimap();
248 $link = $interwiki->link("Upload:$u_userfile");
249 $message->pushContent(HTML::div(array('class' => 'feedback'),
250 HTML::p(_("File successfully uploaded.")),
253 // the upload was a success and we need to mark this event in the "upload log"
255 $upload_log = $file_dir . basename($logfile);
256 $this->log($userfile, $upload_log, $message);
259 require_once("lib/loadsave.php");
260 $pagehandle = $dbi->getPage($page);
261 if ($pagehandle->exists()) {// don't replace default contents
262 $current = $pagehandle->getCurrentRevision();
263 $version = $current->getVersion();
264 $text = $current->getPackedContent();
265 $newtext = $text . "\n* Upload:$u_userfile"; // don't inline images
266 $meta = $current->_data;
267 $meta['summary'] = sprintf(_("uploaded %s"),$u_userfile);
268 $pagehandle->save($newtext, $version + 1, $meta);
272 $message->pushContent($err_header);
273 $message->pushContent(HTML::br(),_("Uploading failed."),HTML::br());
277 $message->pushContent(HTML::br(),_("No file selected. Please select one."),HTML::br());
280 //$result = HTML::div( array( 'class' => 'wikiaction' ) );
282 $result->pushContent($form);
283 $result->pushContent($message);
287 function log ($userfile, $upload_log, &$message) {
289 $user = $GLOBALS['request']->_user;
290 if (file_exists($upload_log) and (!is_writable($upload_log))) {
291 trigger_error(_("The upload logfile exists but is not writable."), E_USER_WARNING);
293 elseif (!$log_handle = fopen ($upload_log, "a")) {
294 trigger_error(_("Can't open the upload logfile."), E_USER_WARNING);
296 else { // file size in KB; precision of 0.1
297 $file_size = round(($userfile->getSize())/1024, 1);
298 if ($file_size <= 0) {
299 $file_size = "< 0.1";
301 $userfile_name = $userfile->getName();
304 . "<tr><td><a href=\"$userfile_name\">$userfile_name</a></td>"
305 . "<td align=\"right\">$file_size kB</td>"
306 . "<td> " . $WikiTheme->formatDate(time()) . "</td>"
307 . "<td> <em>" . $user->getId() . "</em></td></tr>");
315 // (c-file-style: "gnu")
320 // c-hanging-comment-ender-p: nil
321 // indent-tabs-mode: nil