1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8function wikiplugin_lsdir_info()
9{
10	return [
11		'name' => tra('List Directory'),
12		'documentation' => 'PluginLsDir',
13		'description' => tra('List files in a directory'),
14		'prefs' => [ 'wikiplugin_lsdir' ],
15		'validate' => 'all',
16		'iconname' => 'file-archive',
17		'introduced' => 1,
18		'params' => [
19			'dir' => [
20				'required' => true,
21				'name' => tra('Directory'),
22				'description' => tra('Full path to the server-local directory. Default is the document root.'),
23				'since' => '1',
24				'default' => '',
25			],
26			'urlprefix' => [
27				'required' => false,
28				'name' => tra('URL Prefix'),
29				'description' => tra('Make the file name a link to the file by adding the URL path preceding the file
30					name. Example:') . ' <code>http://yoursite.com/tiki/</code>',
31				'since' => '1',
32				'default' => null,
33				'filter' => 'url',
34			],
35			'sort' => [
36				'required' => false,
37				'name' => tra('Sort order'),
38				'description' => tra('Set the sort order of the file list'),
39				'since' => '1',
40				'default' => 'name',
41				'filter' => 'word',
42				'options' => [
43					['text' => '', 'value' => ''],
44					['text' => tra('File Name'), 'value' => 'name'],
45					['text' => tra('File Size'), 'value' => 'size'],
46					['text' => tra('Last Access'), 'value' => 'atime'],
47					['text' => tra('Last Metadata Change'), 'value' => 'ctime'],
48					['text' => tra('Last modified'), 'value' => 'mtime'],
49				]
50			],
51			'filter' => [
52				'required' => false,
53				'name' => tra('Filter'),
54				'description' => tra('Only list files with file names that contain this filter. Example:')
55					. ' <code>.jpg</code>',
56				'since' => '1',
57				'default' => null
58			],
59			'limit' => [
60				'required' => false,
61				'name' => tra('Limit'),
62				'description' => tra('Maximum amount of files to display. Default is no limit.'),
63				'since' => '1',
64				'default' => 0,
65				'filter' => 'digits',
66			],
67		],
68	];
69}
70
71function wikiplugin_lsdir($data, $params)
72{
73	global $tikilib;
74//	$dir = '';
75	$dir = $params['dir'];
76//	$urlprefix = NULL;
77	$urlprefix = $params['urlprefix'];
78//	$sort = 'name';
79	$sort = 'size';
80	$sortmode = 'asc';
81	$filter = null;
82	$limit = 0;
83	$tmp_array = [];
84	$ret = '';
85
86	extract($params, EXTR_SKIP);
87
88	// make sure document_root has no trailing slash
89	if (! empty($_SERVER['DOCUMENT_ROOT'])) {
90		$tail = strlen($_SERVER['DOCUMENT_ROOT']) - 1;
91		if (substr($_SERVER['DOCUMENT_ROOT'], $tail) == '/') {
92			$pathprefix = substr($_SERVER['DOCUMENT_ROOT'], 0, $tail);
93		} else {
94			$pathprefix = $_SERVER['DOCUMENT_ROOT'];
95		}
96	}
97
98	// make sure dir has starting slash
99	if (! empty($dir)) {
100		if (substr($dir, 0, 1) != '/') {
101			$dir = '/' . $dir;
102		}
103	}
104
105	$dir = $pathprefix . $dir;
106
107	// make sure urlprefix has a trailing slash
108	if (! empty($urlprefix)) {
109		$tail = strlen($urlprefix) - 1;
110		if (substr($urlprefix, $tail) != '/') {
111			$urlprefix .= '/';
112		}
113	}
114
115	if ($limit > 0) {
116		$count = 0;
117	} else {
118		$count = -1;
119	}
120
121	// fileatime, filectime, filemtime, filesize are PHP functions
122	if ($sort == 'atime') {
123		$getkey = 'fileatime';
124	} elseif ($sort == 'ctime') {
125		$getkey = 'filectime';
126	} elseif ($sort == 'mtime') {
127		$getkey = 'filemtime';
128	} elseif ($sort == 'size') {
129		$getkey = 'filesize';
130	}
131
132	// supress the PHP error because that causes Tiki to crash
133	$dh = @opendir($dir);
134
135	if (! $dh) {
136		$error = "<span class='attention'><b>$dir</b> " . tra("could not be opened because it doesn't exist or permission was denied") . "</span>";
137		return $error;
138	}
139
140	while ($file = readdir($dh)) {
141		if (empty($filter) || stristr($file, $filter)) {
142			//Don't list subdirectories
143			if (! is_dir("$dir/$file")) {
144				if ($sort == 'name') {
145					$key = "$file";
146				} else {
147					$key = $getkey("$dir/$file");
148				}
149				$tmp_array["$key"] = "$file";
150			}
151		}
152	}
153	closedir($dh);
154
155	if ($sortmode == 'asc') {
156		ksort($tmp_array);
157	} elseif ($sortmode == 'desc') {
158		krsort($tmp_array);
159	}
160
161	foreach ($tmp_array as $filename) {
162		if ($count >= $limit) {
163			break 1;
164		}
165		if (! empty($urlprefix)) {
166			$ret .= "<a href='$urlprefix$filename' class='wiki'>$filename</a><br />";
167		} else {
168			$ret .= "$filename<br />";
169		}
170		if ($limit > 0) {
171			$count++;
172		}
173	}
174
175	return $ret;
176}
177