* * Adds actions to delete & restore deleted attachments, as * well as an attachlist replacement to use those actions. * * Developed and tested using the PmWiki 2.2.0-beta series. * * To install, add the following line to your configuration file : include_once("$FarmD/cookbook/attachtable.php"); * * For more information, please see the online documentation at * http://www.pmwiki.org/wiki/Cookbook/Attachtable * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License, Version 2, as * published by the Free Software Foundation. * http://www.gnu.org/copyleft/gpl.html File modified by Petko Yotov www.pmwiki.org/petko */ $RecipeInfo['Attachtable']['Version'] = '20170809'; SDVA( $HandleActions, array( 'delattach' => 'HandleDeleteAttachment', 'renameattach' => 'HandleRenameAttachment', 'downloaddeleted' => 'HandleDownloadDeleted' ) ); SDVA( $HandleAuth, array( 'delattach' => @$HandleAuth['upload'], # deleting a file from PmWiki while keeping it on disk 'deldelattach' => @$HandleAuth['postattr'], # deleting a file from disk ) ); SDVA( $HandleAuth, array( 'undelattach' => @$HandleAuth['delattach'], # restoring a file 'renameattach' => @$HandleAuth['deldelattach'] # renaming a file ) ); SDVA( $HandleAuth, array( 'downloaddeleted' => @$HandleAuth['undelattach'] # download a deleted file ) ); XLSDV( 'en', array( 'ULdelsuccess' => 'successfully deleted', 'ULundelsuccess' => 'successfully restored', 'ULrenamesuccess' => 'successfully renamed', 'ULisdir' => 'can\'t act on directory', 'ULdelfail' => 'file delete error', 'ULrenamefail' => 'file rename error' ) ); Markup_e('attachtable', 'directives', '/\\(:attachtable\\s*(.*?):\\)/i', "Keep(FmtAttachtable(\$pagename,\$m[1]))"); SDVA($AttachListPatterns['all'], array()); SDVA($AttachListPatterns['default'], array('deleted'=>'!,\\d+$!')); SDVA($AttachListPatterns['deleted'], array('deleted'=>'/,\\d+$/')); if(!function_exists('MatchNames')){function MatchNames($list,$pat){return MatchPageNames($list,$pat);}} ## action to delete attachment ## usage: ?action=delattach&upname=filename function HandleDeleteAttachment($pagename, $auth = 'upload') { global $UploadFileFmt, $LastModFile, $EnableUploadVersions, $Now, $HandleAuth, $AttachtableRedirectFunction; if(@$GLOBALS['EnableReadOnly']) Abort('Cannot modify site -- $EnableReadOnly is set', 'readonly'); $upname = $_REQUEST['upname']; if ($upname=='') Abort("?no attachment name"); $deleted = preg_match( '/^(.*?)(,[0-9]+)$/', $upname, $delmatch ); $delfromdisk = $deleted || !IsEnabled($EnableUploadVersions, 0); if ( $delfromdisk ) $auth = $HandleAuth['deldelattach']; $page = RetrieveAuthPage( $pagename, $auth, true, READPAGE_CURRENT ); if (!$page) Abort("?cannot delete attachment from $pagename"); $upname = $deleted ? MakeUploadName( $pagename, $delmatch[1] ) . $delmatch[2] : MakeUploadName( $pagename, $upname ); $filepath = FmtPageName("$UploadFileFmt/$upname",$pagename); if ( file_exists($filepath) ) { if ( is_dir($filepath) ) $result = 'isdir'; else { $r = $delfromdisk ? unlink($filepath) : rename($filepath, "$filepath,$Now"); if ($LastModFile) { touch($LastModFile); fixperms($LastModFile); } $result = $r ? 'delsuccess' : ( $delfromdisk ? 'delfail' : 'renamefail' ); } } else $result = 'badname'; $f = IsEnabled($AttachtableRedirectFunction['delattach'], 'Redirect'); $f($pagename,"{\$PageUrl}?action=upload&uprname=$upname&upresult=$result"); } ## action to rename attachment ## usage: ?action=renameattach&upname=filename&newname=filename function HandleRenameAttachment($pagename, $auth = 'upload') { global $HandleAuth, $UploadFileFmt, $LastModFile; if(@$GLOBALS['EnableReadOnly']) Abort('Cannot modify site -- $EnableReadOnly is set', 'readonly'); ## check file $upname = $_REQUEST['upname']; if ($upname=='') Abort("?no attachment name"); $deleted = preg_match( '/^(.*?)(,[0-9]+)$/', $upname, $delmatch ); if ($deleted) { $dname = MakeUploadName( $pagename, $delmatch[1] ); $upname = $dname . $delmatch[2]; } else { $upname = MakeUploadName( $pagename, $upname ); } $oldfilepath = FmtPageName( "$UploadFileFmt/$upname", $pagename ); if ( !file_exists($oldfilepath) ) Abort("?no such attachment: $upname"); if ( is_dir($oldfilepath) ) { Redirect( $pagename, "{\$PageUrl}?action=upload&uprname=$upname&upresult=isdir" ); return; } ## new name $upname = $_REQUEST['newname']; if ($upname=='') Abort("?no new attachment name"); $upname = MakeUploadName( $pagename, $upname ); ## check authorization if ($deleted) { if ( !RetrieveAuthPage( $pagename, $HandleAuth['undelattach'], TRUE, READPAGE_CURRENT ) ) Abort("?cannot restore attachment from $pagename"); if ( ( $dname != $upname ) && !RetrieveAuthPage( $pagename, $HandleAuth['renameattach'], TRUE, READPAGE_CURRENT ) ) Abort("?cannot restore attachment from $pagename with a different name"); } else { if ( !RetrieveAuthPage( $pagename, $HandleAuth['renameattach'], TRUE, READPAGE_CURRENT ) ) Abort("?cannot rename attachment from $pagename"); } ## verify & rename if ($upname=='') { $upname = $_REQUEST['newname']; $result = 'upresult=badname'; } else { $newfilepath = FmtPageName( "$UploadFileFmt/$upname", $pagename ); $result = UploadVerifyRename( $oldfilepath, $newfilepath ); if ( $result == '' ) { $r = rename( $oldfilepath, $newfilepath ); if ($LastModFile) { touch($LastModFile); fixperms($LastModFile); } $result = $r ? ( $deleted ? 'upresult=undelsuccess' : 'upresult=renamesuccess' ) : 'upresult=renamefail'; } } Redirect( $pagename, "{\$PageUrl}?action=upload&uprname=$upname&$result" ); } function UploadVerifyRename( $oldfilepath, $newfilepath ) { global $UploadExtSize; if ( file_exists( $newfilepath ) ) return 'upresult=exists'; preg_match( '/\\.([^.\\/]+)$/', $newfilepath, $match ); $ext = @$match[1]; $maxsize = $UploadExtSize[$ext]; if ( $maxsize <= 0 ) return "upresult=badtype&upext=$ext"; $size = filesize($oldfilepath); if ( $size > $maxsize ) return "upresult=toobigext&upext=$ext&upmax=$maxsize"; /* for now, can't rename across directories so no point in checking $UploadPrefixQuota or $UploadDirQuota global $UploadPrefixQuota; $filedirs = preg_replace( '#/[^/]*$#', '', array( $oldfilepath, $newfilepath ) ); if ( $UploadPrefixQuota && ( $filedirs[0] != $filedirs[1] ) && ( ( dirsize($filedirs[1]) + $size ) > $UploadPrefixQuota ) ) return 'upresult=pquota'; */ return ''; } ## action to download deleted (ie. renamed) attachment ## usage: ?action=downloaddeleted&upname=filename,timestamp function HandleDownloadDeleted( $pagename, $auth = 'upload' ) { global $UploadFileFmt, $UploadExts, $DownloadDisposition; SDV($DownloadDisposition, "inline"); $upname = $_REQUEST['upname']; if ( !preg_match( '/^(.*?)(,[0-9]+)$/', $upname, $delmatch ) ) Redirect($pagename,"{\$PageUrl}?action=download&upname=$upname"); $page = RetrieveAuthPage( $pagename, $auth, true, READPAGE_CURRENT ); if (!$page) Abort("?cannot read $pagename"); $upname = MakeUploadName( $pagename, $delmatch[1] ) . $delmatch[2]; $filepath = FmtPageName("$UploadFileFmt/$upname", $pagename); if ( !$upname || !file_exists($filepath) ) { header("HTTP/1.0 404 Not Found"); Abort("?requested file not found"); exit(); } preg_match( '/\\.([^.]+)$/', $delmatch[1], $match ); if ( $UploadExts[@$match[1]] ) header( "Content-Type: {$UploadExts[@$match[1]]}" ); header( "Content-Length: ".filesize($filepath) ); header( "Content-disposition: $DownloadDisposition; filename={$delmatch[1]}" ); $fp = fopen( $filepath, "r" ); if ($fp) { while (!feof($fp)) echo fread($fp, 4096); fclose($fp); } exit(); } ## show attachments ## based on FmtUploadList in upload.php function FmtAttachtable($pagename, $args) { global $UploadDir, $UploadPrefixFmt, $UploadUrlFmt, $TimeFmt, $HandleAuth, $EnableUploadOverwrite, $EnableUploadVersions, $EnableDirectDownload, $DefaultGroup, $DefaultName, $AttachtableDataFields, $AttachtableFileCheck, $AttachListPatterns; SDV( $AttachtableDataFields, array( 'size', 'modtime' ) ); ## options $opt = ParseArgs($args); if (@$opt[''][0]) $pagename = MakePageName($pagename, $opt[''][0]); else $pagename = MakePageName("$DefaultGroup.$DefaultName",$pagename); if (@$opt['ext']) $matchext = '/\\.(' . implode('|', preg_split('/\\W+/', $opt['ext'], -1, PREG_SPLIT_NO_EMPTY)) . ')(?:,[0-9]+)?$/i'; if ( array_key_exists( 'data', $opt ) ) switch( $opt['data'] ) { case 'all': $data = array( 'size', 'modtime' ); break; case 'none': $data = array(); break; case 'default': $data = array_unique($AttachtableDataFields); break; default: $data = array_unique( preg_split( '/\\W+/', $opt['data'], -1, PREG_SPLIT_NO_EMPTY ) ); } else $data = array_unique($AttachtableDataFields); // xmp($data); $cols = array(); foreach( $data as $x ) { if ( in_array( $x, $AttachtableDataFields ) ) $cols[$x] = 1; } $hidedeleted = ( array_key_exists('-',$opt) && in_array( 'deleted', $opt['-'] ) ); ## authorization check $auth = array( 'upload' => 0, 'delattach' => 0, 'undelattach' => 0, 'renameattach' => 0, 'deldelattach' => 0, 'downloaddeleted' => 0 ); switch( @$opt['actions'] ) { case 'all': foreach( $auth as $n => $a ) $auth[$n] = 1; case 'none': break; default: if ( @$opt[''][0] != '*' ) { $ac = array(); foreach( $auth as $n => $a ) { $h = $HandleAuth[$n]; if ( !array_key_exists( $h, $ac ) ) $ac[$h] = CondAuth( $pagename, $h ); $auth[$n] = $a || $ac[$h]; } } } if ( !$EnableUploadOverwrite ) $auth['upload'] = 0; $confirmdel = !IsEnabled( $EnableUploadVersions, 0 ); if ( $confirmdel ) $auth['delattach'] = $auth['deldelattach']; ## locations $pageurl = FmtPageName( '$PageUrl', $pagename ); $uploaddir = FmtPageName( "$UploadDir$UploadPrefixFmt", $pagename ); $uploadurl = FmtPageName( IsEnabled($EnableDirectDownload, 1) ? "$UploadUrlFmt$UploadPrefixFmt/" : "\$PageUrl?action=download&upname=", $pagename ); ## read files $dirp = @opendir($uploaddir); if (!$dirp) return XL('(no attached files)'); $filelist = array(); while ( ( $file = readdir($dirp) ) !== false ) { if ( $file{0} == '.' ) continue; if ( @$matchext && !preg_match(@$matchext, $file) ) continue; $filelist[$file] = $file; } closedir($dirp); natcasesort($filelist); if(!@$opt['list']>'')$opt['list'] = 'default'; $pat = array_merge((array)@$AttachListPatterns[$opt['list']]); $filelist = MatchNames($filelist, $pat); if(@$opt['name']) $filelist = MatchNames($filelist, $opt['name']); if(@$opt['order']=='-name') arsort($filelist); $target = (@$opt['target']) ? " target='{$opt['target']}'" : ''; ## output $restore = $deldel = $change = $rename = $delete = ''; $out = array(); foreach( $filelist as $file ) { $name = PUE("$uploadurl$file"); $filepath = "$uploaddir/$file"; $stat = stat($filepath); if ( is_dir($filepath) ) { if ($EnableDirectDownload) { $s = "$file/"; foreach( $data as $d ) switch($d) { case 'size': $s .= ''; break; case 'modtime': $s .= ''.strftime($TimeFmt, $stat['mtime']).''; break; default: $fn = "at_$d"; if(function_exists($fn)) $s .= ''.$fn($file, $stat, $filepath, $name).''; } $out[] = $s; } continue; } $confirm = "onclick=\"return confirm('$[Really delete] $file?')\""; $aopt = "rel='nofollow' class='createlink' href='$pageurl?upname=$file&action"; if ( preg_match( '/^(.*?),([0-9]+)$/', $file, $delmatch ) ) { if ( $hidedeleted ) continue; $dname = $delmatch[1]; $dtime = strftime( $TimeFmt, $delmatch[2] ); if ($auth['undelattach']) $restore = " R '; if ($auth['deldelattach']) $deldel = " X "; if ($auth['downloaddeleted']) $dname = "$dname"; $s = "$dname $restore $deldel"; } else { if ($auth['upload']) $change = " Δ "; if ($auth['renameattach']) $rename = " R "; if ($auth['delattach']) $delete = ( $confirmdel ? " X " : " X " ); $s = "$file $change $rename $delete"; } foreach( $data as $d ) switch($d) { case 'size': $s .= ''.number_format($stat['size']).''; break; case 'modtime': $s .= ''.strftime($TimeFmt, $stat['mtime']).''; break; default: $fn = "at_$d"; if(function_exists($fn)) $s .= ''.$fn($pagename, $file, $stat, $filepath, $name).''; } $out[] = $s; if(intval(@$opt['max'])>0 && intval(@$opt['max'])>= count($out)) break; } return FmtPageName(''.implode("\n",$out).'
', $pagename); }