attachlist', '/\\(:attachlist\\s*(.*?):\\)/ei',
"Keep('
'.attachlistsort('$pagename',PSS('$1')).'
')");
}
function attachlistsort($pagename, $args) {
global $UploadDir, $UploadPrefixFmt, $UploadUrlFmt, $EnableUploadOverwrite,
$TimeFmt, $EnableDirectDownload;
$sortby = array();
$opt = ParseArgs($args);
@$sortby = preg_split('/\\W+/', $opt['sort'], -1, PREG_SPLIT_NO_EMPTY);
if (@$opt[''][0]) $pagename = MakePageName($pagename, $opt[''][0]);
if (@$opt['ext']) $matchext = '/\\.('
. implode('|', preg_split('/\\W+/', $opt['ext'], -1, PREG_SPLIT_NO_EMPTY))
. ')$/i';
$filelist = array();
$typelist = array();
$namelist = array();
$sizelist = array();
$datelist = array();
$uploaddir = FmtPageName("$UploadDir$UploadPrefixFmt", $pagename);
$uploadurl = FmtPageName(IsEnabled($EnableDirectDownload, 1)
? "$UploadUrlFmt$UploadPrefixFmt/"
: "\$PageUrl?action=download&upname=",
$pagename);
$dirp = @opendir($uploaddir);
if (!$dirp) return '';
while (($file=readdir($dirp)) !== false) {
if ($file{0} == '.') continue;
if (@$matchext && !preg_match(@$matchext, $file)) continue;
$stat = stat("$uploaddir/$file");
$namelist[$file] = $file;
preg_match('/[^.]+$/', $file, $matches);
$typelist[$file] = $matches[0];
$sizelist[$file] = $stat['size'];
$datelist[$file] = $stat['mtime'];
}
closedir($dirp);
$A = SORT_ASC;
$D = SORT_DESC;
foreach ($sortby as $key => $s) {
switch ($s) {
case 'descendingtime':
case 'descendingdate':
$sortkey [] = 'date';
$sortlist['date'] = $datelist;
$sorthow ['date'] = $D;
break;
case 'ascendingtime':
case 'ascendingdate':
case 'time':
case 'date':
$sortkey [] = 'date';
$sortlist['date'] = $datelist;
$sorthow ['date'] = $A;
break;
case 'descendingsize':
$sortkey [] = 'size';
$sortlist['size'] = $sizelist;
$sorthow ['size'] = $D;
break;
case 'ascendingsize':
case 'size':
$sortkey [] = 'size';
$sortlist['size'] = $sizelist;
$sorthow ['size'] = $A;
break;
case 'descendingtype':
case 'descendingext':
$sortkey [] = 'type';
$sortlist['type'] = $typelist;
$sorthow ['type'] = $D;
break;
case 'ascendingtype':
case 'type':
case 'ascendingext':
case 'ext':
$sortkey [] = 'type';
$sortlist['type'] = $typelist;
$sorthow ['type'] = $A;
break;
case 'descendingname':
$sortkey [] = 'name';
$sortlist['name'] = $namelist;
$sorthow ['name'] = $D;
break;
case 'ascendingname':
case 'name':
default:
$sortkey [] = 'name';
$sortlist['name'] = $namelist;
$sorthow ['name'] = $A;
break;
}
}
if (!isset($sortlist['name'])) { $sortkey[] = 'name'; $sortlist['name'] = $namelist; $sorthow['name'] = $A; }
if (!isset($sortlist['type'])) { $sortkey[] = 'type'; $sortlist['type'] = $typelist; $sorthow['type'] = $A; }
if (!isset($sortlist['size'])) { $sortkey[] = 'size'; $sortlist['size'] = $sizelist; $sorthow['size'] = $A; }
if (!isset($sortlist['date'])) { $sortkey[] = 'date'; $sortlist['date'] = $datelist; $sorthow['date'] = $A; }
#echo "Sorted by:"; foreach ($sortkey as $key => $v ) { echo " $key='$v'"; }# echo " TimeFormat='$TimeFmt'";
array_multisort($sortlist[$sortkey[0]], $sorthow[$sortkey[0]],
$sortlist[$sortkey[1]], $sorthow[$sortkey[1]],
$sortlist[$sortkey[2]], $sorthow[$sortkey[2]],
$sortlist[$sortkey[3]], $sorthow[$sortkey[3]]);
$filelist = $sortlist[$sortkey[0]];
$overwrite = '';
foreach ($filelist as $key => $s) {
$file = $namelist[$key];
$name = PUE("$uploadurl$file");
if ($EnableUploadOverwrite)
$overwrite = FmtPageName(" Δ",
$pagename);
$t = '%B %d, %Y, at %I:%M:%S %p';
$out[] = " $file$overwrite ... ".
number_format($sizelist[$key]) . ' bytes ... ' .
strftime($t, $datelist[$key]) . '';
}
if (count($namelist) == 0) { return null; }
return implode("\n",$out);
}