upload.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2003-2004 Patrick R. Michaud (pmichaud@pobox.com)
  3. This file is part of PmWiki; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version. See pmwiki.php for full details.
  7. This file adds upload capabilities to PmWiki. Uploads can be
  8. enabled by setting
  9. $EnableUpload = 1
  10. in config.php. In addition, an upload password must be set, and
  11. some installations may require configuration of the $UploadDir
  12. and $UploadUrlFmt variables. See the PmWiki.UploadsAdmin wiki
  13. page for full details, as it's complicated to explain here.
  14. */
  15. SDV($EnableUploadOverwrite,1);
  16. SDV($UploadExts,array(
  17. 'gif','jpg','jpeg','png','bmp','ico','wbmp', # images
  18. 'mp3','au','wav', # audio
  19. 'mpg','mpeg','wmf','mov','qt','avi', # video
  20. 'zip','gz','tgz','tar','rpm','hqx','sit', # archives
  21. 'doc','ppt','xls','exe','mdb', # MSOffice
  22. 'pdf','psd','ps','ai','eps', # Adobe
  23. 'htm','html','css','fla','swf', # web stuff
  24. 'txt','rtf','exe','tex','dvi','')); # misc
  25. $upname=@$_POST['upname'];
  26. if (@$_GET['upname']) $upname=$_GET['upname'];
  27. $upresult=@$_GET['upresult'];
  28. $upext=@$_GET['upext'];
  29. SDV($UploadMaxSize,50000);
  30. SDV($UploadPrefixQuota,0);
  31. SDV($UploadDirQuota,0);
  32. SDV($UploadPerms,0666 & ~umask());
  33. foreach($UploadExts as $ext)
  34. if (!isset($UploadExtSize[$ext]))
  35. $UploadExtSize[$ext]=$UploadMaxSize;
  36. $upextmax = @$UploadExtSize[$upext];
  37. SDV($UploadDir,'uploads');
  38. SDV($UploadUrlFmt,preg_replace("#/[^/]*\$#","/$UploadDir",$ScriptUrl,1));
  39. SDV($UploadPrefixFmt,'/$Group/');
  40. SDV($UploadNamePattern,'[A-Za-z0-9][-\w.]*[A-Za-z0-9]');
  41. SDV($UploadVerifyFunction,"UploadVerifyBasic");
  42. SDV($PageUploadFmt,array("<h1 class='wikiaction'>$[Attachments for] \$PageName</h1>
  43. <h3>\$UploadResult</h3>
  44. <form enctype='multipart/form-data' action='\$ScriptUrl' method='post'>
  45. <input type='hidden' name='pagename' value='\$PageName' />
  46. <input type='hidden' name='action' value='postupload' />
  47. <input type='hidden' name='upname' value='\$UploadName' />
  48. <table border='0'>
  49. <tr><td align='right'>$[File to upload:]</td><td><input
  50. name='uploadfile' type='file' /></td></tr>
  51. <tr><td align='right'>$[Name attachment as:]</td>
  52. <td><input type='text' name='upname'
  53. value='\$UploadName' />
  54. <input type='submit' value=' $[Upload] ' /><br />
  55. </td></tr>
  56. </table>
  57. </form>",'wiki:$[PmWiki.UploadQuickReference]'));
  58. SDV($HandleUploadFmt,array(&$PageStartFmt, &$PageUploadFmt, &$PageEndFmt));
  59. SDV($UploadFileFmt,"$UploadDir$UploadPrefixFmt");
  60. SDV($RecentUploads, array(
  61. 'Main.AllRecentUploads' =>
  62. "[[$UploadUrlFmt$UploadPrefixFmt\$UploadName \$UploadName]]",
  63. '$Group.RecentUploads' => '[[Attach:$UploadName $UploadName]]'));
  64. XLSDV('en',array(
  65. 'ULsuccess' => 'successfully uploaded',
  66. 'ULbadname' => 'invalid attachment name',
  67. 'ULbadtype' => '\'$upext\' is not an allowed file extension',
  68. 'ULtoobig' => 'file is larger than maximum allowed by webserver',
  69. 'ULtoobigext' => 'file is larger than allowed maximum of $upextmax
  70. bytes for \'$upext\' files',
  71. 'ULpartial' => 'incomplete file received',
  72. 'ULnofile' => 'no file uploaded',
  73. 'ULexists' => 'file with that name already exists',
  74. 'ULpquota' => 'group quota exceeded',
  75. 'ULtquota' => 'upload quota exceeded'));
  76. SDV($PageAttributes['passwdupload'],'$[Set new upload password]: ');
  77. SDV($DefaultPasswords['upload'],'*');
  78. SDV($LinkPatterns[120]["\\bAttach:($UploadNamePattern)"],'FmtAttachLink');
  79. SDV($InterMapUrls['Attach'],
  80. FmtPageName("$UploadUrlFmt$UploadPrefixFmt$1",$pagename));
  81. SDV($InlineReplacements['/\\[\\[\\$Attachlist\\s*(.*)\\]\\]/e'],
  82. "'<ul>'.FmtUploadList('$pagename','$1').'</ul>'");
  83. $UploadName = $upname; $UploadResult='';
  84. if ($upresult)
  85. $UploadResult = "<i>$upname</i>: ".FmtPageName("$[UL$upresult]",$pagename);
  86. if ($upresult=='success') $UploadName = "";
  87. mkgiddir($UploadDir);
  88. SDV($WikiLibDirs,array($WikiDir,"wikilib.d"));
  89. SDV($HandleActions['upload'],'HandleUpload');
  90. SDV($HandleActions['postupload'],'HandlePostUpload');
  91. function FmtAttachLink($pat,$ref,$txt) {
  92. global $UploadName,$UploadFileFmt,$pagename,$UploadFormTarget,$FmtUrlLink;
  93. preg_match("/^([^:]*):(.*)$/",$ref,$match);
  94. $rtxt=$ref; if (!is_null($txt)) $rtxt=$txt;
  95. $UploadName = $match[2];
  96. $filepath = FmtPageName($UploadFileFmt,$pagename).$UploadName;
  97. if (!file_exists($filepath)) {
  98. $target = ($UploadFormTarget) ? "target='$UploadFormTarget'" : '';
  99. return "$rtxt<a href='".FmtPageName('$PageUrl?action=upload',$pagename).
  100. "&amp;upname=".urlencode($UploadName)."' $target>?</a>";
  101. }
  102. return $FmtUrlLink($pat,$ref,$txt);
  103. }
  104. function HandleUpload($pagename) {
  105. global $UploadList,$HandleUploadFmt;
  106. $page = RetrieveAuthPage($pagename,'upload');
  107. if (!$page) { Abort("?cannot upload to $pagename"); }
  108. SetPageVars($pagename,$page,"$pagename Attachments");
  109. $UploadList = FmtUploadList($pagename);
  110. PrintFmt($pagename,$HandleUploadFmt);
  111. }
  112. function HandlePostUpload($pagename) {
  113. global $HTTP_POST_FILES,$UploadName,$UploadNamePattern,$UploadFileFmt,
  114. $UploadVerifyFunction,$UploadPerms,$RecentUploads,$TimeFmt,$Now;
  115. $page = RetrieveAuthPage($pagename,'upload');
  116. if (!$page) Abort("?cannot upload to $pagename");
  117. $uploadfile = $HTTP_POST_FILES['uploadfile'];
  118. if ($UploadName=='') { $UploadName=$uploadfile['name']; }
  119. if (!function_exists($UploadVerifyFunction))
  120. Abort("?no UploadVerifyFunction available");
  121. $filepath = FmtPageName($UploadFileFmt,$pagename).$UploadName;
  122. $result = $UploadVerifyFunction($pagename,$uploadfile,$filepath);
  123. if ($result=='') {
  124. $filedir = preg_replace('/[^\\/]*$/','',$filepath);
  125. mkgiddir($filedir);
  126. if (!move_uploaded_file($uploadfile['tmp_name'],$filepath))
  127. { Abort("?cannot move uploaded file to $filepath"); return; }
  128. chmod($filepath,$UploadPerms);
  129. foreach($RecentUploads as $rcfmt => $pgfmt) {
  130. $rcname=FmtPageName($rcfmt,$pagename); if (!$rcname) continue;
  131. $pgname=FmtPageName($pgfmt,$pagename); if (!$pgname) continue;
  132. if (@$seen[$rcname]++) continue;
  133. $rcpage = ReadPage($rcname,"");
  134. $rcpage['text'] = "* $pgname . . . . . . ".strftime($TimeFmt,$Now)."\n".
  135. preg_replace("%\\* ".preg_quote($pgname)." .*?\n%","",
  136. $rcpage['text']);
  137. WritePage($rcname,$rcpage);
  138. }
  139. $result = "upresult=success";
  140. }
  141. Redirect($pagename,
  142. '$PageUrl?action=upload&upname='.urlencode($UploadName)."&$result");
  143. }
  144. function dirsize($dir) {
  145. $size=0;
  146. $dirp = @opendir($dir);
  147. if (!$dirp) return 0;
  148. while (($file=readdir($dirp)) !== false) {
  149. if ($file[0]=='.') continue;
  150. if (is_dir("$dir/$file")) $size+=dirsize("$dir/$file");
  151. else $size+=filesize("$dir/$file");
  152. }
  153. closedir($dirp);
  154. return $size;
  155. }
  156. function UploadVerifyBasic($pagename,$uploadfile,$filepath) {
  157. global $UploadName,$UploadNamePattern,$UploadExtSize,$EnableUploadOverwrite,
  158. $UploadPrefixQuota,$UploadDirQuota,$UploadDir;
  159. if (!$EnableUploadOverwrite && file_exists($filepath))
  160. return 'upresult=exists';
  161. preg_match('/\\.([^.]+)$/',$filepath,$match); $ext=@$match[1];
  162. $maxsize = $UploadExtSize[$ext];
  163. if ($maxsize<=0) return "upresult=badtype&upext=$ext";
  164. if ($uploadfile['size']>$maxsize) return "upresult=toobigext&upext=$ext";
  165. if (!is_uploaded_file($uploadfile['tmp_name'])) return 'upresult=nofile';
  166. switch ($uploadfile['error']) {
  167. case 1: return 'upresult=toobig';
  168. case 2: return 'upresult=toobig';
  169. case 3: return 'upresult=partial';
  170. case 4: return 'upresult=nofile';
  171. }
  172. $filedir = preg_replace('/\\/[^\\/]*$/','',$filepath);
  173. if ($UploadPrefixQuota &&
  174. (@(dirsize($filedir)-filesize($filepath)+$uploadfile['size'])
  175. > $UploadPrefixQuota)) return 'upresult=pquota';
  176. if ($UploadDirQuota &&
  177. @(dirsize($UploadDir)-filesize($filepath)+$uploadfile['size'])
  178. > $UploadDirQuota) return 'upresult=tquota';
  179. return '';
  180. }
  181. function FmtUploadList($pagename,$order='N=A') {
  182. global $UploadDir,$UploadPrefixFmt,$UploadUrlFmt,$TimeFmt;
  183. $uploaddir = FmtPageName("$UploadDir$UploadPrefixFmt",$pagename);
  184. $uploadurl = FmtPageName("$UploadUrlFmt$UploadPrefixFmt",$pagename);
  185. if ($order=='') $order='N=A';
  186. $dirp = @opendir($uploaddir);
  187. $out = '';
  188. if (!$dirp) return $out;
  189. $filelist = array();
  190. while (($file=readdir($dirp)) !== false) {
  191. if ($file[0]=='.') continue;
  192. switch (strtolower($order[0])) {
  193. case 'm': $filelist[$file]=filemtime("$uploaddir$file"); break;
  194. case 's': $filelist[$file]=filesize("$uploaddir$file"); break;
  195. default: $filelist[$file]=$file; break;
  196. }
  197. }
  198. closedir($dirp);
  199. if (strtolower($order[2])=='d') arsort($filelist); else asort($filelist);
  200. foreach($filelist as $file=>$x) {
  201. $stat = stat("$uploaddir/$file");
  202. $out .= "<li> <a href='$uploadurl$file'>$file</a> ... "
  203. .$stat['size']." bytes ... ".strftime($TimeFmt,$stat['mtime'])."\n</li>";
  204. }
  205. return $out;
  206. }
  207. ?>