]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UpLoad.php
use IntwerWiki links for uploaded file.
[SourceForge/phpwiki.git] / lib / plugin / UpLoad.php
1 <?php // -*-php-*-
2 rcs_id('$Id: UpLoad.php,v 1.5 2004-02-27 01:24:43 rurban Exp $');
3 /*
4  Copyright 2002 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
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.
12
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.
17
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
21
22  */
23
24 /**
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
32  */
33
34     /* Change these config variables to your needs. Paths must end with "/".
35      */
36
37 class WikiPlugin_UpLoad
38 extends WikiPlugin
39 {
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...
43
44     var $disallowed_extensions = array('.php', '.pl', '.sh', '.cgi', '.exe');
45     // todo: use PagePerms instead
46     var $only_authenticated = true; // allow only authenticated users upload.
47
48     function getName () {
49         return "UpLoad";
50     }
51
52     function getDescription () {
53         return _("Upload files to the local InterWiki Upload:<filename>");
54     }
55
56     function getDefaultArguments() {
57         return array('logfile'  => 'file_list.txt',
58                      // add a link of the fresh file automatically to the 
59                      // end of the page (or current page)
60                      'autolink' => true, 
61                      'page'     => '[pagename]',
62                      );
63     }
64
65     function run($dbi, $argstr, &$request, $basepage) {
66         $args = $this->getArgs($argstr, $request);
67         extract($args);
68
69         $file_dir = defined('PHPWIKI_DIR') ? 
70             PHPWIKI_DIR . "/uploads/" : "uploads/";
71         $url_prefix = SERVER_NAME.DATA_PATH; 
72
73         $form = HTML::form(array('action' => $request->getPostURL(),
74                                  'enctype' => 'multipart/form-data',
75                                  'method' => 'post'));
76         $contents = HTML::div(array('class' => 'wikiaction'));
77         $contents->pushContent(HTML::input(array('type' => 'hidden',
78                                                  'name' => 'MAX_FILE_SIZE',
79                                                  'value' => MAX_UPLOAD_SIZE)));
80         $contents->pushContent(HTML::input(array('name' => 'userfile',
81                                                  'type' => 'file',
82                                                  'size' => '50')));
83         $contents->pushContent(HTML::raw(" "));
84         $contents->pushContent(HTML::input(array('value' => _("Upload"),
85                                                  'type' => 'submit')));
86         $form->pushContent($contents);
87
88         $message = HTML();
89         $userfile = $request->getUploadedFile('userfile');
90         if ($userfile) {
91             $userfile_name = $userfile->getName();
92             $userfile_name = basename($userfile_name);
93             $userfile_tmpname = $userfile->getTmpName();
94
95             if ($this->only_authenticated) {
96                 // Make sure that the user is logged in.
97                 //
98                 $user = $request->getUser();
99                 if (!$user->isAuthenticated()) {
100                     $message->pushContent(_("ACCESS DENIED: You must log in to upload files."),
101                                           HTML::br(),HTML::br());
102                     $result = HTML();
103                     $result->pushContent($form);
104                     $result->pushContent($message);
105                     return $result;
106                 }
107             }
108
109             if (preg_match("/(" . join("|", $this->disallowed_extensions) . ")\$/",
110                            $userfile_name)) {
111
112                 $message->pushContent(fmt("Files with extension %s are not allowed",
113                                           join(", ", $this->disallowed_extensions)),HTML::br(),HTML::br());
114             }
115             elseif (file_exists($file_dir . $userfile_name)) {
116                 $message->pushContent(fmt("There is already a file with name %s uploaded",
117                                           $userfile_name),HTML::br(),HTML::br());
118             }
119             elseif ($userfile->getSize() > (MAX_UPLOAD_SIZE)) {
120                 $message->pushContent(_("Sorry but this file is too big"),HTML::br(),HTML::br());
121             }
122             elseif (move_uploaded_file($userfile_tmpname, $file_dir . $userfile_name)) {
123                 $interwiki = new PageType_interwikimap();
124                 $link = $interwiki->link("Upload:$userfile_name");
125                 $message->pushContent(_("File successfully uploaded to location: "),
126                                       HTML::br());
127
128                 // the upload was a success and we need to mark this event in the "upload log"
129                 $upload_log = $file_dir . basename($logfile);
130                 if ($logfile) { 
131                     $this->log($userfile, $upload_log, &$message);
132                 }
133                 if ($autolink) {
134                     $pagehandle = $dbi->getPage($page);
135                 }
136             }
137             elseif (IsWindows()) {
138                 rename($userfile_tmpname, $file_dir . $userfile_name);
139                 $interwiki = new PageType_interwikimap();
140                 $link = $interwiki->link("Upload:$userfile_name");
141                 $message->pushContent(_("File quirkfully uploaded to location: "),
142                                       $link,HTML::br());
143                 // the upload was a success and we need to mark this event in the "upload log"
144                 $upload_log = $file_dir . basename($logfile);
145                 if ($logfile) { 
146                     $this->log($userfile, $upload_log, &$message);
147                 }
148                 if ($autolink) {
149                     require_once("lib/loadsave.php");
150                     $pagehandle = $dbi->getPage($page);
151                     //todo... append "\n* Upload:$userfile_name"
152                 }
153             }
154             else {
155                 $message->pushContent(HTML::br(),_("Uploading failed: "),$userfile_name, HTML::br());
156             }
157         }
158         else {
159             $message->pushContent(HTML::br(),HTML::br());
160         }
161
162         //$result = HTML::div( array( 'class' => 'wikiaction' ) );
163         $result = HTML();
164         $result->pushContent($form);
165         $result->pushContent($message);
166         return $result;
167     }
168
169     function log ($userfile, $upload_log, &$message) {
170         global $Theme;
171         $user = $GLOBALS['request']->_user;
172         if (!is_writable($upload_log)) {
173             $message->pushContent(_("Error: the upload log is not writable"));
174             $message->pushContent(HTML::br());
175         }
176         elseif (!$log_handle = fopen ($upload_log, "a")) {
177             $message->pushContent(_("Error: can't open the upload logfile"));
178             $message->pushContent(HTML::br());
179         }
180         else {        // file size in KB; precision of 0.1
181             $file_size = round(($userfile->getSize())/1024, 1);
182             if ($file_size <= 0) {
183                 $file_size = "&lt; 0.1";
184             }
185             fwrite($log_handle,
186                    "\n"
187                    . "<tr><td><a href=$userfile_name>$userfile_name</a></td>"
188                    . "<td align=\"right\">$file_size kB</td>"
189                    . "<td>&nbsp;&nbsp;" . $Theme->formatDate(time()) . "</td>"
190                    . "<td>&nbsp;&nbsp;<em>" . $user->getId() . "</em></td></tr>");
191             fclose($log_handle);
192         }
193         return;
194     }
195
196 }
197
198 // (c-file-style: "gnu")
199 // Local Variables:
200 // mode: php
201 // tab-width: 8
202 // c-basic-offset: 4
203 // c-hanging-comment-ender-p: nil
204 // indent-tabs-mode: nil
205 // End:
206
207 // $Log: not supported by cvs2svn $
208 // Revision 1.4  2004/02/21 19:12:59  rurban
209 // patch by Sascha Carlin
210 //
211 // Revision 1.3  2004/02/17 12:11:36  rurban
212 // 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, ...)
213 //
214 // Revision 1.2  2004/01/26 09:18:00  rurban
215 // * changed stored pref representation as before.
216 //   the array of objects is 1) bigger and 2)
217 //   less portable. If we would import packed pref
218 //   objects and the object definition was changed, PHP would fail.
219 //   This doesn't happen with an simple array of non-default values.
220 // * use $prefs->retrieve and $prefs->store methods, where retrieve
221 //   understands the interim format of array of objects also.
222 // * simplified $prefs->get() and fixed $prefs->set()
223 // * added $user->_userid and class '_WikiUser' portability functions
224 // * fixed $user object ->_level upgrading, mostly using sessions.
225 //   this fixes yesterdays problems with loosing authorization level.
226 // * fixed WikiUserNew::checkPass to return the _level
227 // * fixed WikiUserNew::isSignedIn
228 // * added explodePageList to class PageList, support sortby arg
229 // * fixed UserPreferences for WikiUserNew
230 // * fixed WikiPlugin for empty defaults array
231 // * UnfoldSubpages: added pagename arg, renamed pages arg,
232 //   removed sort arg, support sortby arg
233 //
234 // Revision 1.1  2003/11/04 18:41:41  carstenklapp
235 // New plugin which was submitted to the mailing list some time
236 // ago. (This is the best UpLoad function I have seen for PhpWiki so
237 // far. Cleaned up text formatting and typos from the version on the
238 // mailing list. Still needs a few adjustments.)
239 //
240 ?>