PageRenderTime 76ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/gforge/plugins/wiki/www/lib/plugin/UpLoad.php

https://github.com/neymanna/fusionforge
PHP | 344 lines | 265 code | 15 blank | 64 comment | 10 complexity | 6851b9d0dda79d5f0d2a069551ecba4b MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception
  1. <?php // -*-php-*-
  2. rcs_id('$Id: UpLoad.php,v 1.19 2005/04/11 19:40:15 rurban Exp $');
  3. /*
  4. Copyright 2003, 2004 $ThePhpWikiProgrammingTeam
  5. This file is part of PhpWiki.
  6. PhpWiki is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. PhpWiki is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with PhpWiki; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /**
  19. * UpLoad: Allow Administrator to upload files to a special directory,
  20. * which should preferably be added to the InterWikiMap
  21. * Usage: <?plugin UpLoad ?>
  22. * Author: NathanGass <gass@iogram.ch>
  23. * Changes: ReiniUrban <rurban@x-ray.at>,
  24. * qubit <rtryon@dartmouth.edu>
  25. * Note: See also Jochen Kalmbach's plugin/UserFileManagement.php
  26. */
  27. class WikiPlugin_UpLoad
  28. extends WikiPlugin
  29. {
  30. var $disallowed_extensions;
  31. // TODO: use PagePerms instead
  32. var $only_authenticated = true; // allow only authenticated users may upload.
  33. function getName () {
  34. return "UpLoad";
  35. }
  36. function getDescription () {
  37. return _("Upload files to the local InterWiki Upload:<filename>");
  38. }
  39. function getDefaultArguments() {
  40. return array('logfile' => 'phpwiki-upload.log',
  41. // add a link of the fresh file automatically to the
  42. // end of the page (or current page)
  43. 'autolink' => true,
  44. 'page' => '[pagename]',
  45. );
  46. }
  47. function run($dbi, $argstr, &$request, $basepage) {
  48. $disablemsg = HTML();
  49. $disablemsg->pushContent(HTML::h2("Upload is temporarily disabled."),HTML::br());
  50. return $disablemsg;
  51. $this->disallowed_extensions = explode("\n",
  52. "ad[ep]
  53. asd
  54. ba[st]
  55. chm
  56. cmd
  57. com
  58. cgi
  59. cpl
  60. crt
  61. dll
  62. eml
  63. exe
  64. hlp
  65. hta
  66. in[fs]
  67. isp
  68. jse?
  69. lnk
  70. md[betw]
  71. ms[cipt]
  72. nws
  73. ocx
  74. ops
  75. pcd
  76. p[ir]f
  77. php
  78. pl
  79. py
  80. reg
  81. sc[frt]
  82. sh[bsm]?
  83. swf
  84. url
  85. vb[esx]?
  86. vxd
  87. ws[cfh]");
  88. //removed "\{[[:xdigit:]]{8}(?:-[[:xdigit:]]{4}){3}-[[:xdigit:]]{12}\}"
  89. $args = $this->getArgs($argstr, $request);
  90. extract($args);
  91. $file_dir = getUploadFilePath();
  92. //$url_prefix = SERVER_NAME . DATA_PATH;
  93. $form = HTML::form(array('action' => $request->getPostURL(),
  94. 'enctype' => 'multipart/form-data',
  95. 'method' => 'post'));
  96. $contents = HTML::div(array('class' => 'wikiaction'));
  97. $contents->pushContent(HTML::input(array('type' => 'hidden',
  98. 'name' => 'MAX_FILE_SIZE',
  99. 'value' => MAX_UPLOAD_SIZE)));
  100. $contents->pushContent(HTML::input(array('name' => 'userfile',
  101. 'type' => 'file',
  102. 'size' => '50')));
  103. $contents->pushContent(HTML::raw(" "));
  104. $contents->pushContent(HTML::input(array('value' => _("Upload"),
  105. 'type' => 'submit')));
  106. $form->pushContent($contents);
  107. $message = HTML();
  108. if ($request->isPost() and $this->only_authenticated) {
  109. // Make sure that the user is logged in.
  110. $user = $request->getUser();
  111. if (!$user->isAuthenticated()) {
  112. $message->pushContent(HTML::h2(_("ACCESS DENIED: You must log in to upload files.")),
  113. HTML::br(),HTML::br());
  114. $result = HTML();
  115. $result->pushContent($form);
  116. $result->pushContent($message);
  117. return $result;
  118. }
  119. }
  120. $userfile = $request->getUploadedFile('userfile');
  121. if ($userfile) {
  122. $userfile_name = $userfile->getName();
  123. $userfile_name = trim(basename($userfile_name));
  124. $userfile_tmpname = $userfile->getTmpName();
  125. $err_header = HTML::h2(fmt("ERROR uploading '%s': ", $userfile_name));
  126. if (preg_match("/(\." . join("|\.", $this->disallowed_extensions) . ")\$/",
  127. $userfile_name))
  128. {
  129. $message->pushContent($err_header);
  130. $message->pushContent(fmt("Files with extension %s are not allowed.",
  131. join(", ", $this->disallowed_extensions)),HTML::br(),HTML::br());
  132. }
  133. elseif (preg_match("/[^._a-zA-Z0-9-]/", $userfile_name))
  134. {
  135. $message->pushContent($err_header);
  136. $message->pushContent(_("File names may only contain alphanumeric characters and dot, underscore or dash."),
  137. HTML::br(),HTML::br());
  138. }
  139. elseif (file_exists($file_dir . $userfile_name)) {
  140. $message->pushContent($err_header);
  141. $message->pushContent(fmt("There is already a file with name %s uploaded.",
  142. $userfile_name),HTML::br(),HTML::br());
  143. }
  144. elseif ($userfile->getSize() > (MAX_UPLOAD_SIZE)) {
  145. $message->pushContent($err_header);
  146. $message->pushContent(_("Sorry but this file is too big."),HTML::br(),HTML::br());
  147. }
  148. elseif (move_uploaded_file($userfile_tmpname, $file_dir . $userfile_name) or
  149. (IsWindows() and rename($userfile_tmpname, $file_dir . $userfile_name))
  150. )
  151. {
  152. $interwiki = new PageType_interwikimap();
  153. $link = $interwiki->link("Upload:$userfile_name");
  154. $message->pushContent(HTML::h2(_("File successfully uploaded.")));
  155. $message->pushContent(HTML::ul(HTML::li($link)));
  156. // the upload was a success and we need to mark this event in the "upload log"
  157. if ($logfile) {
  158. $upload_log = $file_dir . basename($logfile);
  159. $this->log($userfile, $upload_log, $message);
  160. }
  161. if ($autolink) {
  162. require_once("lib/loadsave.php");
  163. $pagehandle = $dbi->getPage($page);
  164. if ($pagehandle->exists()) {// don't replace default contents
  165. $current = $pagehandle->getCurrentRevision();
  166. $version = $current->getVersion();
  167. $text = $current->getPackedContent();
  168. $newtext = $text . "\n* [Upload:$userfile_name]";
  169. $meta = $current->_data;
  170. $meta['summary'] = sprintf(_("uploaded %s"),$userfile_name);
  171. $pagehandle->save($newtext, $version + 1, $meta);
  172. }
  173. }
  174. }
  175. else {
  176. $message->pushContent($err_header);
  177. $message->pushContent(HTML::br(),_("Uploading failed."),HTML::br());
  178. }
  179. }
  180. else {
  181. $message->pushContent(HTML::br(),HTML::br());
  182. }
  183. //$result = HTML::div( array( 'class' => 'wikiaction' ) );
  184. $result = HTML();
  185. $result->pushContent($form);
  186. $result->pushContent($message);
  187. return $result;
  188. }
  189. function log ($userfile, $upload_log, &$message) {
  190. global $WikiTheme;
  191. $user = $GLOBALS['request']->_user;
  192. if (!is_writable($upload_log)) {
  193. trigger_error(_("The upload logfile is not writable."), E_USER_WARNING);
  194. }
  195. elseif (!$log_handle = fopen ($upload_log, "a")) {
  196. trigger_error(_("Can't open the upload logfile."), E_USER_WARNING);
  197. }
  198. else { // file size in KB; precision of 0.1
  199. $file_size = round(($userfile->getSize())/1024, 1);
  200. if ($file_size <= 0) {
  201. $file_size = "&lt; 0.1";
  202. }
  203. $userfile_name = $userfile->getName();
  204. fwrite($log_handle,
  205. "\n"
  206. . "<tr><td><a href=\"$userfile_name\">$userfile_name</a></td>"
  207. . "<td align=\"right\">$file_size kB</td>"
  208. . "<td>&nbsp;&nbsp;" . $WikiTheme->formatDate(time()) . "</td>"
  209. . "<td>&nbsp;&nbsp;<em>" . $user->getId() . "</em></td></tr>");
  210. fclose($log_handle);
  211. }
  212. return;
  213. }
  214. }
  215. // $Log: UpLoad.php,v $
  216. // Revision 1.19 2005/04/11 19:40:15 rurban
  217. // Simplify upload. See https://sourceforge.net/forum/message.php?msg_id=3093651
  218. // Improve UpLoad warnings.
  219. // Move auth check before upload.
  220. //
  221. // Revision 1.18 2005/02/12 17:24:24 rurban
  222. // locale update: missing . : fixed. unified strings
  223. // proper linebreaks
  224. //
  225. // Revision 1.17 2004/11/09 08:15:50 rurban
  226. // trim filename
  227. //
  228. // Revision 1.16 2004/10/21 19:03:37 rurban
  229. // Be more stricter with uploads: Filenames may only contain alphanumeric
  230. // characters. Patch #1037825
  231. //
  232. // Revision 1.15 2004/09/22 13:46:26 rurban
  233. // centralize upload paths.
  234. // major WikiPluginCached feature enhancement:
  235. // support _STATIC pages in uploads/ instead of dynamic getimg.php? subrequests.
  236. // mainly for debugging, cache problems and action=pdf
  237. //
  238. // Revision 1.14 2004/06/16 10:38:59 rurban
  239. // Disallow refernces in calls if the declaration is a reference
  240. // ("allow_call_time_pass_reference clean").
  241. // PhpWiki is now allow_call_time_pass_reference = Off clean,
  242. // but several external libraries may not.
  243. // In detail these libs look to be affected (not tested):
  244. // * Pear_DB odbc
  245. // * adodb oracle
  246. //
  247. // Revision 1.13 2004/06/14 11:31:39 rurban
  248. // renamed global $Theme to $WikiTheme (gforge nameclash)
  249. // inherit PageList default options from PageList
  250. // default sortby=pagename
  251. // use options in PageList_Selectable (limit, sortby, ...)
  252. // added action revert, with button at action=diff
  253. // added option regex to WikiAdminSearchReplace
  254. //
  255. // Revision 1.12 2004/06/13 11:34:22 rurban
  256. // fixed bug #969532 (space in uploaded filenames)
  257. // improved upload error messages
  258. //
  259. // Revision 1.11 2004/06/11 09:07:30 rurban
  260. // support theme-specific LinkIconAttr: front or after or none
  261. //
  262. // Revision 1.10 2004/04/12 10:19:18 rurban
  263. // fixed copyright year
  264. //
  265. // Revision 1.9 2004/04/12 10:18:22 rurban
  266. // removed the hairy regex line
  267. //
  268. // Revision 1.8 2004/04/12 09:12:22 rurban
  269. // fix syntax errors
  270. //
  271. // Revision 1.7 2004/04/09 17:49:03 rurban
  272. // Added PhpWiki RssFeed to Sidebar
  273. // sidebar formatting
  274. // some browser dependant fixes (old-browser support)
  275. //
  276. // Revision 1.6 2004/02/27 01:36:51 rurban
  277. // autolink enabled
  278. //
  279. // Revision 1.5 2004/02/27 01:24:43 rurban
  280. // use IntwerWiki links for uploaded file.
  281. // autolink to page prepared, but not yet ready
  282. //
  283. // Revision 1.4 2004/02/21 19:12:59 rurban
  284. // patch by Sascha Carlin
  285. //
  286. // Revision 1.3 2004/02/17 12:11:36 rurban
  287. // 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, ...)
  288. //
  289. // Revision 1.2 2004/01/26 09:18:00 rurban
  290. // * changed stored pref representation as before.
  291. // the array of objects is 1) bigger and 2)
  292. // less portable. If we would import packed pref
  293. // objects and the object definition was changed, PHP would fail.
  294. // This doesn't happen with an simple array of non-default values.
  295. // * use $prefs->retrieve and $prefs->store methods, where retrieve
  296. // understands the interim format of array of objects also.
  297. // * simplified $prefs->get() and fixed $prefs->set()
  298. // * added $user->_userid and class '_WikiUser' portability functions
  299. // * fixed $user object ->_level upgrading, mostly using sessions.
  300. // this fixes yesterdays problems with loosing authorization level.
  301. // * fixed WikiUserNew::checkPass to return the _level
  302. // * fixed WikiUserNew::isSignedIn
  303. // * added explodePageList to class PageList, support sortby arg
  304. // * fixed UserPreferences for WikiUserNew
  305. // * fixed WikiPlugin for empty defaults array
  306. // * UnfoldSubpages: added pagename arg, renamed pages arg,
  307. // removed sort arg, support sortby arg
  308. //
  309. // Revision 1.1 2003/11/04 18:41:41 carstenklapp
  310. // New plugin which was submitted to the mailing list some time
  311. // ago. (This is the best UpLoad function I have seen for PhpWiki so
  312. // far. Cleaned up text formatting and typos from the version on the
  313. // mailing list. Still needs a few adjustments.)
  314. // (c-file-style: "gnu")
  315. // Local Variables:
  316. // mode: php
  317. // tab-width: 8
  318. // c-basic-offset: 4
  319. // c-hanging-comment-ender-p: nil
  320. // indent-tabs-mode: nil
  321. // End:
  322. ?>