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