* * Adds actions to delete & restore deleted attachments, as * well as an attachlist replacement to use those actions, * show file types and list attachment references. * * Developed and tested using the PmWiki 2.2 series. * * To install, add the following line to your configuration file : include_once("$FarmD/cookbook/attachtable/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 */ $RecipeInfo['Attachtable']['Version'] = '2009-04-29'; if ( !IsEnabled($EnableUpload,0) ) return; ## set a few values that we depend on in case upload.php hasn't been included yet SDVA( $HandleAuth, array( 'upload' => 'upload', 'download' => 'read') ); SDV( $HandleAuth['postupload'], $HandleAuth['upload'] ); ## set the default Attachtable authorization levels -- order is important for proper inheritance SDVA( $HandleAuth, array( 'delattach' => $HandleAuth['upload'], # deleting a file from PmWiki while keeping it on disk 'deldelattach' => $HandleAuth['attr'], # deleting a file from disk ) ); SDVA( $HandleAuth, array( 'undelattach' => $HandleAuth['delattach'], # restoring a file 'renameattach' => $HandleAuth['delattach'] # 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( 'attachtable', 'directives', '/\\(:attachtable\\s*(.*?):\\)/ei', "Keep(FmtAttachtable(\$pagename,PSS('$1')))"); SDV( $AttachtablePubDirUrl, "$PubDirUrl/attachtable" ); SDV( $AttachtableCSS, "$AttachtablePubDirUrl/attachtable.css" ); if (!empty( $AttachtableCSS )) $HTMLHeaderFmt['attachtable-css'] = "\n"; include_once('attachtable-util.php'); function AttachtableCountUploadLinks( $pagename, $imap, $path, $alt='', $txt='', $fmt=NULL ) { global $UploadFileFmt, $AttachtableReferences; if (preg_match( '!^(.*)/([^/]+)$!', $path, $match )) { $pn_upload = MakePageName( $pagename, $match[1] ); $path = $match[2]; } else $pn_upload = $pagename; $upname = MakeUploadName( $pn_upload, $path ); $filepath = FmtPageName( "$UploadFileFmt/$upname", $pn_upload ); $AttachtableReferences[$filepath][] = $pagename; return ''; } ## show attachments ## based on FmtUploadList in upload.php function FmtAttachtable($pagename, $args) { global $PubDirUrl, $UploadDir, $UploadPrefixFmt, $UploadUrlFmt, $TimeFmt, $HandleAuth, $EnableUploadOverwrite, $EnableUploadVersions, $EnableDirectDownload, $EnableAttachtableSortable, $EnableAttachtableFileInfo, $AttachtableDataFields, $AttachtableShowRows, $AttachtableDefaultFilter, $AttachtableReferences, $AttachtablePubDirUrl; SDV( $AttachtableDataFields, array( 'fileinfo', 'name', 'size', 'references', 'modtime', 'mimetype' ) ); SDV( $AttachtableShowRows, array( 'header', 'normal', 'deleted', 'linkonly', 'footer' ) ); $opt = ParseArgs($args); ## page to list attachments for if (@$opt[''][0]) $pagename = MakePageName($pagename, $opt[''][0]); ## filter to inc/exclude files based on their name $filter = empty($opt['filter']) ? @$AttachtableDefaultFilter : str_replace( '$', '(?:,\d+)?$', $opt['filter'] ); ## filter to include files based on their extension if (!empty( $opt['ext'] )) { $filter = (array)$filter; $filter[] = '/\\.(' . implode( '|', preg_split('/\\W+/', $opt['ext'], -1, PREG_SPLIT_NO_EMPTY) ) . ')(?:,[0-9]+)?$/i'; } ## (dis)allow re-sorting table $sortable = ( IsEnabled( $EnableAttachtableSortable, 0 ) && !in_array( 'sortable', (array)$opt['-'] ) ); ## data columns to show if ( array_key_exists('data',$opt) ) switch( $opt['data'] ) { case 'all': $data = array( 'fileinfo', 'name', 'size', 'references', 'modtime', 'mimetype', 'filetype' ); break; case 'none': $data = array('name'); 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); if ( !IsEnabled( $EnableAttachtableFileInfo, 0 ) && ( ( $key = array_search('fileinfo',$data) ) !== FALSE ) ) unset( $data[$key] ); if ( count($data) && !in_array('name',$data) ) { if ( ( $k=array_search('fileinfo',$data) ) !== FALSE ) array_splice( $data, $k+1, 0, 'name' ); else array_unshift( $data, 'name' ); } ## data rows to show if ( array_key_exists('show',$opt) ) switch( $opt['show'] ) { case 'all': $show = array( 'header', 'normal', 'deleted', 'linkonly', 'footer' ); break; case 'default': $show = array_unique($AttachtableShowRows); break; default: $show = array_unique( preg_split( '/\\W+/', $opt['show'], -1, PREG_SPLIT_NO_EMPTY ) ); } else $show = array_unique($AttachtableShowRows); ## lookup attachment references, if necessary ## FIXME: these really ought to be cached $AttachtableReferences = array(); if ( in_array('references',$data) ) { global $LinkFunctions, $SearchPatterns, $UrlExcludeChars, $AttachtableProperReferenceLookupMaxPages, $AttachtableReferenceListPatterns; SDV( $AttachtableProperReferenceLookupMaxPages, 8 ); SDVA( $AttachtableReferenceListPatterns, array( 'limit' => '/^'.substr( $pagename, 0, strcspn($pagename,'./') ).'\./', 'recent' => $SearchPatterns['normal']['recent'] ) ); $ls = ListPages( $AttachtableReferenceListPatterns ); if ( count($ls) <= $AttachtableProperReferenceLookupMaxPages ) { $prev_LinkFunctions_Attach = $LinkFunctions['Attach:']; $LinkFunctions['Attach:'] = 'AttachtableCountUploadLinks'; foreach( $ls as $pn ) { $pg = RetrieveAuthPage( $pn, 'read', FALSE, READPAGE_CURRENT ); $html = MarkupToHTML( $pn, $pg['text'] ); } $LinkFunctions['Attach:'] = $prev_LinkFunctions_Attach; } else { foreach( $ls as $pn ) { $pg = RetrieveAuthPage( $pn, 'read', FALSE, READPAGE_CURRENT ); if ($pg) $txt = preg_replace( array( "/(\n[^\\S\n]*)?\\[([=@])(.*?)\\2\\]/s", ## preserved text '/\[\[[^\]]*?\bAttach:([^"\]\|]*)/e', ## links to attachments "/\\bAttach:([^\\s$UrlExcludeChars]*[^\\s.,?!$UrlExcludeChars])/e" ), ## raw attachments array( ' ', "'[['.AttachtableCountUploadLinks(\$pn,'','$1')", "AttachtableCountUploadLinks(\$pn,'','$1')" ), htmlspecialchars( $pg['text'], ENT_NOQUOTES ) ); ## assumes $MarkupFrame[0]['escape'] == 1 } } } ## check authorization $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 $filelist = array(); if ( $dirp = @opendir($uploaddir) ) { while ( ( $file = readdir($dirp) ) !== FALSE ) { if ( $file[0] == '.' ) continue; $filelist[$file] = $file; } closedir($dirp); } if ($filter) $filelist = MatchPageNames( $filelist, $filter ); ## output $restore = $deldel = $change = $rename = $delete = ''; $totalsize = $lastmod = $delcount = $dircount = 0; $out = array(); if (count( $filelist )) { usort($filelist,'AttachtableSortNameCmp'); $showfiles = in_array('normal',$show) || in_array('deleted',$show); foreach( $filelist as $file ) { $name = PUE("$uploadurl$file"); $filepath = "$uploaddir/$file"; if (!$showfiles) { $totalsize += filesize($filepath); continue; } $stat = stat($filepath); if ( is_dir($filepath) ) { ++$dircount; if ($EnableDirectDownload) { $s = "