[Pmwiki-users] AllRecentUploads

Patrick R. Michaud pmichaud at pobox.com
Tue May 27 09:34:13 CDT 2003


--huq684BweRXVnRxX
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Thanks for noticing this--I've fixed the problem in upload.php
and put it in the 0.4.28 release (just released).  I'm also
attaching an updated upload.php script to this email for those
who simply want to replace the one file that was changed.

Pm




On Tue, May 27, 2003 at 08:56:35AM -0400, Joe Crumpton wrote:
> I noticed that the group name is not being substituted
> correctly in the URLs for Main.AllRecentUploads. You can
> see an example of this at 
> http://www.pmichaud.com/wiki/Main/AllRecentUploads
> 
> I tried to come up with a fix but I could not. I am
> very much the PHP beginner. 
> 
> Joe
> 
> _______________________________________________
> Pmwiki-users mailing list
> Pmwiki-users at pmichaud.com
> http://pmichaud.com/mailman/listinfo/pmwiki-users_pmichaud.com
> 

--huq684BweRXVnRxX
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="upload.php"

<?php
/*  Copyright 2003 Patrick R. Michaud (pmichaud at pobox.com)
    This file is part of PmWiki; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License
    (at your option) any later version.  See pmwiki.php for full details.

    This file adds upload capabilities to PmWiki.  Uploads can be
    enabled by setting
	$EnableUpload = 1
    in local.php.  In addition, an upload password must be set, and
    some installations may require configuration of the $UploadDir
    and $UploadUrlFmt variables.  See the PmWiki.UploadsAdmin wiki
    page for full details, as it's complicated to explain here.
*/

SDV($UploadExts,array(
  'gif','jpg','jpeg','png','bmp','ico','wbmp',		# images
  'mp3','au','wav',					# audio
  'mpg','mpeg','wmf','mov','qt','avi',			# video
  'zip','gz','tgz','tar','rpm','hqx',			# archives
  'doc','ppt','xls','exe','mdb',			# MSOffice
  'pdf','psd','ps','ai','eps',				# Adobe
  'htm','html','fla','swf',				# web stuff
  'txt','rtf','exe','tex','dvi',''));			# misc

$upname=$HTTP_POST_VARS['upname'];
if ($HTTP_GET_VARS['upname']) $upname=$HTTP_GET_VARS['upname'];
$upresult=$HTTP_GET_VARS['upresult'];
$upext=$HTTP_GET_VARS['upext'];

SDV($UploadMaxSize,50000);
foreach($UploadExts as $ext) 
  if (!isset($UploadExtSize[$ext])) 
    $UploadExtSize[$ext]=$UploadMaxSize;

SDV($UploadDir,'uploads');
SDV($UploadUrlFmt,"$ScriptDir/$UploadDir");
SDV($UploadPrefixFmt,'/$Group/');
SDV($UploadNamePattern,'[A-Za-z0-9][-\w.]*[A-Za-z0-9]');
SDV($UploadVerifyFunction,"UploadVerifyBasic");
SDV($PageUploadFmt,"<h1>Attachments for <a href='\$PageUrl'>\$PageName</a></h1>
  <h3>\$UploadResult</h3>
  <form enctype='multipart/form-data' action='\$ScriptUrl' method='post'>
  <input type='hidden' name='pagename' value='\$PageName'>
  <input type='hidden' name='action' value='postupload'>
  <input type='hidden' name='upname' value='\$UploadName'>
  <table border='0'>
    <tr><td align='right'>File to upload:</td><td><input 
      name='uploadfile' type='file'></td></tr>
    <tr><td align='right'top'>Name attachment as:
      </td>
      <td><input type='text' name='upname' 
        value='\$UploadName'>
        <input type='submit' value=' Upload '><br>
      </td></tr>
  </table>
  </form>"); 
SDV($UploadQuickReferencePage,"PmWiki.UploadQuickReference");
SDV($UploadFileFmt,"$UploadDir$UploadPrefixFmt");
SDV($RecentUploads, array(
  'Main.AllRecentUploads' => 
    "[[$UploadUrlFmt$UploadPrefixFmt\$UploadName \$UploadName]]",
  '$Group.RecentUploads' => '[[Attach:$UploadName $UploadName]]'));
SDV($UploadResultMsgs['success'],': successfully uploaded');
SDV($UploadResultMsgs['badname'],': invalid attachment name');
SDV($UploadResultMsgs['badtype'],": '$upext' is not an allowed file extension");
SDV($UploadResultMsgs['toobig'],
  ": file is larger than maximum allowed by webserver");
SDV($UploadResultMsgs['toobigext'],
  ": file is larger than allowed maximum of {$UploadExtSize[$upext]}
    bytes for '$upext' files");
SDV($UploadResultMsgs['partial'],': incomplete file received');
SDV($UploadResultMsgs['nofile'],': no file uploaded');
SDV($PageAttributes['passwdupload'],'Set new upload password: ');
SDV($DefaultPasswords['upload'],'*');

SDV($LinkPatterns[120]["\\bAttach:($UploadNamePattern)"],'FmtAttachLink');
SDV($InterMapUrls['Attach'],
  FmtPageName("$UploadUrlFmt$UploadPrefixFmt",$pagename));
SDV($InlineReplacements['/\\[\\[\\$Attachlist\\s*(.*)\\]\\]/e'],
  "'<ul>'.FmtUploadList('$pagename','$1').'</ul>'");

$UploadName = $upname; $UploadResult='';
if ($upresult) 
  $UploadResult = "<i>$upname</i>".$UploadResultMsgs[$upresult];
if ($upresult=='success') $UploadName = "";

mkgiddir($UploadDir);
SDV($WikiLibDirs,array($WikiDir,"wikilib.d"));
SDV($HandleActions['upload'],'HandleUpload');
SDV($HandleActions['postupload'],'HandlePostUpload');

function FmtAttachLink($pat,$ref,$txt) {
  global $UploadName,$UploadFileFmt,$pagename,$UploadFormTarget,$FmtUrlLink;
  preg_match("/^([^:]*):(.*)$/",$ref,$match);
  $rtxt=$ref;  if (!is_null($txt)) $rtxt=$txt;
  $UploadName = $match[2];
  $filepath = FmtPageName($UploadFileFmt,$pagename).$UploadName;
  if (!file_exists($filepath)) {
    if ($UploadFormTarget) $target = "target='$UploadFormTarget'";
    return "$rtxt<a href='".FmtPageName('$PageUrl',$pagename).
      "?action=upload&upname=".urlencode($UploadName)."' $target>?</a>";
  }
  return $FmtUrlLink($pat,$ref,$txt);
}

function HandleUpload($pagename) {
  global $PageUploadFmt,$UploadList,$UploadQuickReferencePage;
  $page = RetrieveAuthPage($pagename,'upload');
  if (!$page) { Abort("?cannot upload to $pagename"); }
  $UploadList = FmtUploadList($pagename);
  StartHTML("Upload file for $pagename",$pagename);
  echo FmtPageName($PageUploadFmt,$pagename);
  if ($UploadQuickReferencePage) {
    $qrpage = ReadPage($UploadQuickReferencePage);
    PrintText($UploadQuickReferencePage,$qrpage['text']);
  }
  EndHTML();
}

function HandlePostUpload($pagename) {
  global $HTTP_POST_FILES,$UploadName,$UploadNamePattern,$UploadFileFmt,
    $UploadVerifyFunction,$RecentUploads,$TimeFmt,$Newline,$Now;
  $page = RetrieveAuthPage($pagename,'upload');
  if (!$page) Abort("?cannot upload to $pagename");
  $uploadfile = $HTTP_POST_FILES['uploadfile'];
  if ($UploadName=='') { $UploadName=$uploadfile['name']; }
  if (!function_exists($UploadVerifyFunction)) 
    Abort("?no UploadVerifyFunction available");
  $filepath = FmtPageName($UploadFileFmt,$pagename).$UploadName;
  $result = $UploadVerifyFunction($pagename,$uploadfile,$filepath);
  if ($result=='') {
    $filedir = preg_replace('/[^\\/]*$/','',$filepath);
    mkgiddir($filedir);
    if (!move_uploaded_file($uploadfile['tmp_name'],$filepath))
      { Abort("?cannot move uploaded file to $filepath"); return; }
    foreach($RecentUploads as $rcfmt => $pgfmt) {
      $rcname=FmtPageName($rcfmt,$pagename); if (!$rcname) continue;
      $pgname=FmtPageName($pgfmt,$pagename); if (!$pgname) continue;
      if (@$seen[$rcname]++) continue;
      $rcpage = ReadPage($rcname,"");
      $rcpage['text'] = "* $pgname . . . . . . ".strftime($TimeFmt,$Now)."\n".
        preg_replace("%\\* ".preg_quote($pgname)." .*?$Newline%","",
          $rcpage['text']);
      WritePage($rcname,$rcpage);
    }
    $result = "upresult=success";
  }
  header(FmtPageName('Location: $PageUrl',$pagename)
    .'?action=upload&upname='.urlencode($UploadName)."&$result");
}  

function UploadVerifyBasic($pagename,$uploadfile,$filepath) {
  global $UploadName,$UploadNamePattern,$UploadExtSize;
  preg_match('/\\.([^.]+)$/',$filepath,$match); $ext=$match[1];
  $maxsize = $UploadExtSize[$ext];
  if ($maxsize<=0) return "upresult=badtype&upext=$ext";
  if ($uploadfile['size']>$maxsize) return "upresult=toobigext&upext=$ext";
  if (!is_uploaded_file($uploadfile['tmp_name'])) return 'upresult=nofile';
  switch ($uploadfile['error']) {
    case 1: return 'upresult=toobig';
    case 2: return 'upresult=toobig';
    case 3: return 'upresult=partial';
    case 4: return 'upresult=nofile';
  }
  return '';
}

function FmtUploadList($pagename,$order='N=A') {
  global $UploadDir,$UploadPrefixFmt,$UploadUrlFmt,$TimeFmt;
  $uploaddir = FmtPageName("$UploadDir$UploadPrefixFmt",$pagename);
  $uploadurl = FmtPageName("$UploadUrlFmt$UploadPrefixFmt",$pagename);
  $dirp = @opendir($uploaddir);
  if (!$dirp) return $out;
  $filelist = array();
  while (($file=readdir($dirp)) !== false) {
    if ($file[0]=='.') continue;
    switch (strtolower($order[0])) {
      case 'm': $filelist[$file]=filemtime("$uploaddir$file"); break;
      case 's': $filelist[$file]=filesize("$uploaddir$file"); break;
      default: $filelist[$file]=$file; break;
    }
  }
  closedir($dirp);
  if (strtolower($order[2])=='d') arsort($filelist); else asort($filelist);
  foreach($filelist as $file=>$x) {
    $stat = stat("$uploaddir/$file");
    $out .= "<li> <a href='$uploadurl$file'>$file</a> ... "
      .$stat['size']." bytes ... ".strftime($TimeFmt,$stat['mtime'])."\n";
  }
  return $out;
}

?>

--huq684BweRXVnRxX--




More information about the pmwiki-users mailing list