]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UpLoad.php
* changed stored pref representation as before.
[SourceForge/phpwiki.git] / lib / plugin / UpLoad.php
1 <?php // -*-php-*-
2 rcs_id('$Id: UpLoad.php,v 1.2 2004-01-26 09:18:00 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     var $only_authenticated = true;
46
47     function getName () {
48         return "UpLoad";
49     }
50
51     function getDescription () {
52         return _("Simple Plugin to load files up to server");
53     }
54
55     function getDefaultArguments() {
56         return array();
57     }
58
59     function run($dbi, $argstr, $request) {
60         $file_dir = defined('PHPWIKI_DIR') ? 
61             PHPWIKI_DIR . "/uploads" : "uploads";
62         $url_prefix = SERVER_NAME.DATA_PATH; 
63
64         $action = $request->getURLtoSelf();
65         $userfile = $request->getUploadedFile('userfile');
66         $form = HTML::form(array('action' => $action,
67                                  'enctype' => 'multipart/form-data',
68                                  'method' => 'post'));
69         $contents = HTML::div(array('class' => 'wikiaction'));
70         //$contents = HTML();
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',
75                                                  'type' => 'file',
76                                                  'size' => '50')));
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);
82
83         //$message = HTML::div(array('class' => 'wikiaction'));
84         $message = HTML();
85
86         if ($userfile) {
87             $userfile_name = $userfile->getName();
88             $userfile_name = basename($userfile_name);
89             $userfile_tmpname = $userfile->getTmpName();
90
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)
97                 //
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());
105
106                     $result = HTML();
107                     $result->pushContent($form);
108                     $result->pushContent($message);
109                     return $result;
110                 }
111             }
112
113             if (preg_match("/(" . join("|", $this->disallowed_extensions) . ")\$/",
114                            $userfile_name)) {
115
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());
120             }
121             elseif (file_exists($file_dir . $userfile_name)) {
122                 $message->pushContent(fmt("There is already a file with name %s uploaded",
123                                             $userfile_name));
124                 $message->pushContent(HTML::br());
125                 $message->pushContent(HTML::br());
126             }
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());
131             }
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());
137
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());
143                 }
144                 elseif (!$log_handle = fopen ($upload_log, "a")) {
145                     $message->pushContent(_("Error: can't open the upload logfile"));
146                     $message->pushContent(HTML::br());
147                 }
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 = "&lt; 0.1";
152                     }
153                     fwrite($log_handle,
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>&nbsp;&nbsp;" . date("M j, Y") . "</td>"
158                            . "<td>&nbsp;&nbsp;<em>" . $user->getId() . "</em></td></tr>");
159                     fclose($log_handle);
160                 }
161             }
162             else {
163                 $message->pushContent(HTML::br());
164                 $message->pushContent(_("Uploading failed."));
165                 $message->pushContent(HTML::br());
166             }
167         }
168         else {
169             $message->pushContent(HTML::br());
170             $message->pushContent(HTML::br());
171         }
172
173         //$result = HTML::div( array( 'class' => 'wikiaction' ) );
174         $result = HTML();
175         $result->pushContent($form);
176         $result->pushContent($message);
177         return $result;
178     }
179 }
180
181 // (c-file-style: "gnu")
182 // Local Variables:
183 // mode: php
184 // tab-width: 8
185 // c-basic-offset: 4
186 // c-hanging-comment-ender-p: nil
187 // indent-tabs-mode: nil
188 // End:
189
190 // $Log: not supported by cvs2svn $
191 // Revision 1.1  2003/11/04 18:41:41  carstenklapp
192 // New plugin which was submitted to the mailing list some time
193 // ago. (This is the best UpLoad function I have seen for PhpWiki so
194 // far. Cleaned up text formatting and typos from the version on the
195 // mailing list. Still needs a few adjustments.)
196 //
197 ?>