tra('List Directory'),
'documentation' => 'PluginLsDir',
'description' => tra('List files in a directory'),
'prefs' => [ 'wikiplugin_lsdir' ],
'validate' => 'all',
'iconname' => 'file-archive',
'introduced' => 1,
'params' => [
'dir' => [
'required' => true,
'name' => tra('Directory'),
'description' => tra('Full path to the server-local directory. Default is the document root.'),
'since' => '1',
'default' => '',
],
'urlprefix' => [
'required' => false,
'name' => tra('URL Prefix'),
'description' => tra('Make the file name a link to the file by adding the URL path preceding the file
name. Example:') . ' http://yoursite.com/tiki/',
'since' => '1',
'default' => null,
'filter' => 'url',
],
'sort' => [
'required' => false,
'name' => tra('Sort order'),
'description' => tra('Set the sort order of the file list'),
'since' => '1',
'default' => 'name',
'filter' => 'word',
'options' => [
['text' => '', 'value' => ''],
['text' => tra('File Name'), 'value' => 'name'],
['text' => tra('File Size'), 'value' => 'size'],
['text' => tra('Last Access'), 'value' => 'atime'],
['text' => tra('Last Metadata Change'), 'value' => 'ctime'],
['text' => tra('Last modified'), 'value' => 'mtime'],
]
],
'filter' => [
'required' => false,
'name' => tra('Filter'),
'description' => tra('Only list files with file names that contain this filter. Example:')
. ' .jpg',
'since' => '1',
'default' => null
],
'limit' => [
'required' => false,
'name' => tra('Limit'),
'description' => tra('Maximum amount of files to display. Default is no limit.'),
'since' => '1',
'default' => 0,
'filter' => 'digits',
],
],
];
}
function wikiplugin_lsdir($data, $params)
{
global $tikilib;
// $dir = '';
$dir = $params['dir'];
// $urlprefix = NULL;
$urlprefix = $params['urlprefix'];
// $sort = 'name';
$sort = 'size';
$sortmode = 'asc';
$filter = null;
$limit = 0;
$tmp_array = [];
$ret = '';
extract($params, EXTR_SKIP);
// make sure document_root has no trailing slash
if (! empty($_SERVER['DOCUMENT_ROOT'])) {
$tail = strlen($_SERVER['DOCUMENT_ROOT']) - 1;
if (substr($_SERVER['DOCUMENT_ROOT'], $tail) == '/') {
$pathprefix = substr($_SERVER['DOCUMENT_ROOT'], 0, $tail);
} else {
$pathprefix = $_SERVER['DOCUMENT_ROOT'];
}
}
// make sure dir has starting slash
if (! empty($dir)) {
if (substr($dir, 0, 1) != '/') {
$dir = '/' . $dir;
}
}
$dir = $pathprefix . $dir;
// make sure urlprefix has a trailing slash
if (! empty($urlprefix)) {
$tail = strlen($urlprefix) - 1;
if (substr($urlprefix, $tail) != '/') {
$urlprefix .= '/';
}
}
if ($limit > 0) {
$count = 0;
} else {
$count = -1;
}
// fileatime, filectime, filemtime, filesize are PHP functions
if ($sort == 'atime') {
$getkey = 'fileatime';
} elseif ($sort == 'ctime') {
$getkey = 'filectime';
} elseif ($sort == 'mtime') {
$getkey = 'filemtime';
} elseif ($sort == 'size') {
$getkey = 'filesize';
}
// supress the PHP error because that causes Tiki to crash
$dh = @opendir($dir);
if (! $dh) {
$error = "$dir " . tra("could not be opened because it doesn't exist or permission was denied") . "";
return $error;
}
while ($file = readdir($dh)) {
if (empty($filter) || stristr($file, $filter)) {
//Don't list subdirectories
if (! is_dir("$dir/$file")) {
if ($sort == 'name') {
$key = "$file";
} else {
$key = $getkey("$dir/$file");
}
$tmp_array["$key"] = "$file";
}
}
}
closedir($dh);
if ($sortmode == 'asc') {
ksort($tmp_array);
} elseif ($sortmode == 'desc') {
krsort($tmp_array);
}
foreach ($tmp_array as $filename) {
if ($count >= $limit) {
break 1;
}
if (! empty($urlprefix)) {
$ret .= "$filename
";
} else {
$ret .= "$filename
";
}
if ($limit > 0) {
$count++;
}
}
return $ret;
}