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