2 rcs_id('$Id: UpLoad.php,v 1.3 2004-02-17 12:11:36 rurban Exp $');
4 Copyright 2002 $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 /* Change these config variables to your needs. Paths must end with "/".
37 class WikiPlugin_UpLoad
40 //var $file_dir = PHPWIKI_DIR . "/img/";
41 //var $url_prefix = DATA_PATH . "/img/";
42 //what if the above are not set in index.php? seems to fail...
44 var $disallowed_extensions = array('.php', '.pl', '.sh', '.cgi', '.exe');
45 var $only_authenticated = true;
51 function getDescription () {
52 return _("Simple Plugin to load files up to server");
55 function getDefaultArguments() {
59 function run($dbi, $argstr, &$request, $basepage) {
60 $file_dir = defined('PHPWIKI_DIR') ?
61 PHPWIKI_DIR . "/uploads" : "uploads";
62 $url_prefix = SERVER_NAME.DATA_PATH;
64 $action = $request->getURLtoSelf();
65 $userfile = $request->getUploadedFile('userfile');
66 $form = HTML::form(array('action' => $action,
67 'enctype' => 'multipart/form-data',
69 $contents = HTML::div(array('class' => 'wikiaction'));
71 $contents->pushContent(HTML::input(array('type' => 'hidden',
72 'name' => 'MAX_FILE_SIZE',
73 'value' => MAX_UPLOAD_SIZE)));
74 $contents->pushContent(HTML::input(array('name' => 'userfile',
77 //$contents->pushContent(HTML::br());
78 $contents->pushContent(HTML::raw(" "));
79 $contents->pushContent(HTML::input(array('value' => _("Upload"),
80 'type' => 'submit')));
81 $form->pushContent($contents);
83 //$message = HTML::div(array('class' => 'wikiaction'));
87 $userfile_name = $userfile->getName();
88 $userfile_name = basename($userfile_name);
89 $userfile_tmpname = $userfile->getTmpName();
91 if ($this->only_authenticated) {
92 // Make sure that the user is logged in.
93 // (NOTE: It's probably overkill to make sure that
94 // they're both signed in AND authenticated, and
95 // I'm not exactly sure of the difference between
96 // the two, but I'm using both of them)
98 $user = $request->getUser();
99 $signed_in = $user->isSignedIn();
100 $authenticated = $user->isAuthenticated();
101 if (!$signed_in || !$authenticated) {
102 $message->pushContent(_("ACCESS DENIED: Please log in to upload files"));
103 $message->pushContent(HTML::br());
104 $message->pushContent(HTML::br());
107 $result->pushContent($form);
108 $result->pushContent($message);
113 if (preg_match("/(" . join("|", $this->disallowed_extensions) . ")\$/",
116 $message->pushContent(fmt("Files with extension %s are not allowed",
117 join(", ", $this->disallowed_extensions)));
118 $message->pushContent(HTML::br());
119 $message->pushContent(HTML::br());
121 elseif (file_exists($file_dir . $userfile_name)) {
122 $message->pushContent(fmt("There is already a file with name %s uploaded",
124 $message->pushContent(HTML::br());
125 $message->pushContent(HTML::br());
127 elseif ($userfile->getSize() > (MAX_UPLOAD_SIZE)) {
128 $message->pushContent(_("Sorry but this file is too big"));
129 $message->pushContent(HTML::br());
130 $message->pushContent(HTML::br());
132 elseif (move_uploaded_file($userfile_tmpname, $file_dir . $userfile_name)) {
133 $message->pushContent(_("File successfully uploaded to location:"));
134 $message->pushContent(HTML::br());
135 $message->pushContent("$url_prefix$userfile_name");
136 $message->pushContent(HTML::br());
138 // the upload was a success and we need to mark this event in the "upload log"
139 $upload_log = $file_dir . "file_list.txt";
140 if (!is_writable($upload_log)) {
141 $message->pushContent(_("Error: the upload log is not writable"));
142 $message->pushContent(HTML::br());
144 elseif (!$log_handle = fopen ($upload_log, "a")) {
145 $message->pushContent(_("Error: can't open the upload logfile"));
146 $message->pushContent(HTML::br());
148 else { // file size in KB; precision of 0.1
149 $file_size = round(($userfile->getSize())/1024, 1);
150 if ($file_size <= 0) {
151 $file_size = "< 0.1";
154 "\n" // the newline makes it easier to read the log file
155 . "<tr><td><a href=$userfile_name>$userfile_name</a></td>"
156 . "<td align=right>$file_size</td>"
157 . "<td> " . date("M j, Y") . "</td>"
158 . "<td> <em>" . $user->getId() . "</em></td></tr>");
163 $message->pushContent(HTML::br());
164 $message->pushContent(_("Uploading failed."));
165 $message->pushContent(HTML::br());
169 $message->pushContent(HTML::br());
170 $message->pushContent(HTML::br());
173 //$result = HTML::div( array( 'class' => 'wikiaction' ) );
175 $result->pushContent($form);
176 $result->pushContent($message);
181 // (c-file-style: "gnu")
186 // c-hanging-comment-ender-p: nil
187 // indent-tabs-mode: nil
190 // $Log: not supported by cvs2svn $
191 // Revision 1.2 2004/01/26 09:18:00 rurban
192 // * changed stored pref representation as before.
193 // the array of objects is 1) bigger and 2)
194 // less portable. If we would import packed pref
195 // objects and the object definition was changed, PHP would fail.
196 // This doesn't happen with an simple array of non-default values.
197 // * use $prefs->retrieve and $prefs->store methods, where retrieve
198 // understands the interim format of array of objects also.
199 // * simplified $prefs->get() and fixed $prefs->set()
200 // * added $user->_userid and class '_WikiUser' portability functions
201 // * fixed $user object ->_level upgrading, mostly using sessions.
202 // this fixes yesterdays problems with loosing authorization level.
203 // * fixed WikiUserNew::checkPass to return the _level
204 // * fixed WikiUserNew::isSignedIn
205 // * added explodePageList to class PageList, support sortby arg
206 // * fixed UserPreferences for WikiUserNew
207 // * fixed WikiPlugin for empty defaults array
208 // * UnfoldSubpages: added pagename arg, renamed pages arg,
209 // removed sort arg, support sortby arg
211 // Revision 1.1 2003/11/04 18:41:41 carstenklapp
212 // New plugin which was submitted to the mailing list some time
213 // ago. (This is the best UpLoad function I have seen for PhpWiki so
214 // far. Cleaned up text formatting and typos from the version on the
215 // mailing list. Still needs a few adjustments.)