Project

General

Profile

Fehler #151 » list.php

Corrected list.php for utf-8 file names - Botu Sun, 04/03/2014 05:10 PM

 
1
<?php
2
// ensure this file is being included by a parent file
3
if ( !defined('_JEXEC') && !defined('_VALID_MOS')) die('Restricted access');
4
/**
5
 * @version $Id: list.php 203 2011-07-25 06:51:11Z soeren $
6
 * @package eXtplorer
7
 * @copyright soeren 2007-2010
8
 * @author The eXtplorer project (http://extplorer.net)
9
 * @author The	The QuiX project (http://quixplorer.sourceforge.net)
10
 * 
11
 * @license
12
 * The contents of this file are subject to the Mozilla Public License
13
 * Version 1.1 (the "License"); you may not use this file except in
14
 * compliance with the License. You may obtain a copy of the License at
15
 * http://www.mozilla.org/MPL/
16
 * 
17
 * Software distributed under the License is distributed on an "AS IS"
18
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
19
 * License for the specific language governing rights and limitations
20
 * under the License.
21
 * 
22
 * Alternatively, the contents of this file may be used under the terms
23
 * of the GNU General Public License Version 2 or later (the "GPL"), in
24
 * which case the provisions of the GPL are applicable instead of
25
 * those above. If you wish to allow use of your version of this file only
26
 * under the terms of the GPL and not to allow others to use
27
 * your version of this file under the MPL, indicate your decision by
28
 * deleting  the provisions above and replace  them with the notice and
29
 * other provisions required by the GPL.  If you do not delete
30
 * the provisions above, a recipient may use your version of this file
31
 * under either the MPL or the GPL."
32
 * 
33
 * Directory-Listing Functions
34
 */
35
//------------------------------------------------------------------------------
36
// HELPER FUNCTIONS (USED BY MAIN FUNCTION 'list_dir', SEE BOTTOM)
37

    
38
// make list of files
39
function make_list(&$_list1, &$_list2) {
40
	$list = array();
41

    
42
	if ($GLOBALS["direction"]=="ASC") {
43
		$list1 = $_list1;
44
		$list2 = $_list2;
45
	} else {
46
		$list1 = $_list2;
47
		$list2 = $_list1;
48
	}
49

    
50
	if (is_array($list1)) {
51
		while (list($key, $val) = each($list1)) {
52
			$list[$key] = $val;
53
		}
54
	}
55

    
56
	if (is_array($list2)) {
57
		while (list($key, $val) = each($list2)) {
58
			$list[$key] = $val;
59
		}
60
	}
61

    
62
	return $list;
63
}
64

    
65
/**
66
 * make tables & place results in reference-variables passed to function
67
 * also 'return' total filesize & total number of items
68
 *
69
 * @param string $dir
70
 * @param array $dir_list
71
 * @param array $file_list
72
 * @param int $tot_file_size
73
 * @param int $num_items
74
 */
75

    
76
// make table of files in dir
77
function get_dircontents($dir, &$dir_list, &$file_list, &$tot_file_size, &$num_items) {
78

    
79
	$homedir = realpath($GLOBALS['home_dir']);
80
	$tot_file_size = $num_items = 0;
81
	// Open directory
82

    
83
	$handle = @$GLOBALS['ext_File']->opendir(get_abs_dir($dir));
84

    
85
	if ($handle === false && $dir == "") {
86
		$handle = @$GLOBALS['ext_File']->opendir($homedir . $GLOBALS['separator']);
87
	}
88

    
89
	if ($handle === false) {
90
		ext_Result::sendResult('list', false, $dir . ": " . $GLOBALS["error_msg"]["opendir"]);
91
	}
92

    
93
	$file_list = array();
94
	$dir_list = array();
95

    
96
	// Read directory
97
	while(($new_item = @$GLOBALS['ext_File']->readdir($handle)) !== false) {
98

    
99
		if (is_array($new_item))  {
100
			$abs_new_item = $new_item;
101
		} else {
102
			$abs_new_item = get_abs_item($dir, $new_item);
103
		}
104

    
105
		/*if (get_is_dir($abs_new_item)) {
106
			continue;
107
		}*/
108

    
109
		if ($new_item == "." || $new_item == "..") continue;
110

    
111
		if (!@$GLOBALS['ext_File']->file_exists($abs_new_item)) {
112
			//ext_Result::sendResult('list', false, $dir."/$abs_new_item: ".$GLOBALS["error_msg"]["readdir"]);
113
		}
114

    
115
		if (!get_show_item($dir, $new_item)) continue;
116

    
117
		$new_file_size = @$GLOBALS['ext_File']->filesize($abs_new_item);
118
		$tot_file_size += $new_file_size;
119
		$num_items++;
120
		$new_item_name = $new_item;
121

    
122
		if (ext_isFTPMode()) {
123
			$new_item_name = $new_item['name'];
124
		}
125

    
126
		if (get_is_dir($abs_new_item)) {
127
			if ($GLOBALS["order"] == "modified") {
128
				$dir_list[$new_item_name] = @$GLOBALS['ext_File']->filemtime($abs_new_item);
129
			} else {	// order == "size", "type" or "name"
130
				$dir_list[$new_item_name] = $new_item;
131
			}
132
		} else {
133
			if ($GLOBALS["order"] == "size") {
134
				$file_list[$new_item_name] = $new_file_size;
135
			} elseif ($GLOBALS["order"] == "modified") {
136
				$file_list[$new_item_name] = @$GLOBALS['ext_File']->filemtime($abs_new_item);
137
			} elseif ($GLOBALS["order"] == "type") {
138
				$file_list[$new_item_name] = get_mime_type($abs_new_item, "type");
139
			} else {	// order == "name"
140
				$file_list[$new_item_name] = $new_item;
141
			}
142
		}
143

    
144
	}
145

    
146
	@$GLOBALS['ext_File']->closedir($handle);
147

    
148
	// sort
149
	if (is_array($dir_list)) {
150
		if ($GLOBALS["order"] == "modified") {
151
			if ($GLOBALS["direction"] == "ASC") arsort($dir_list);
152
			else asort($dir_list);
153
		} else {	// order == "size", "type" or "name"
154
			if ($GLOBALS["direction"] == "ASC") ksort($dir_list);
155
			else krsort($dir_list);
156
		}
157
	}
158

    
159
	// sort
160
	if (is_array($file_list)) {
161
		if ($GLOBALS["order"] == "modified") {
162
			if ($GLOBALS["direction"] == "ASC") arsort($file_list);
163
			else asort($file_list);
164
		} elseif ($GLOBALS["order"] == "size" || $GLOBALS["order"] == "type") {
165
			if ($GLOBALS["direction"] == "ASC") asort($file_list);
166
			else arsort($file_list);
167
		} else {	// order == "name"
168
			if ($GLOBALS["direction"] == "ASC") ksort($file_list);
169
			else krsort($file_list);
170
		}
171
	}
172

    
173
	if ($GLOBALS['start'] > $num_items) {
174
		$GLOBALS['start'] = 0;
175
	}
176

    
177
}
178
/**
179
 * This function assembles an array (list) of files or directories in the directory specified by $dir
180
 * The result array is send using JSON
181
 *
182
 * @param string $dir
183
 * @param string $sendWhat Can be "files" or "dirs"
184
 */
185
function send_dircontents($dir, $sendWhat = 'files') {	// print table of files
186
	global $dir_up, $mainframe;
187

    
188
	// make file & dir tables, & get total filesize & number of items
189
	get_dircontents($dir, $dir_list, $file_list, $tot_file_size, $num_items);
190

    
191
	if ($sendWhat == 'files') {
192
		$list = $file_list;
193
	} elseif ($sendWhat == 'dirs') {
194
		$list = $dir_list;
195
	} else {
196
		$list = make_list($dir_list, $file_list);
197
	}
198

    
199
	$i = 0;
200
	$items['totalCount'] = count($list);
201
	$items['items'] = array();
202
	$dirlist = array();
203

    
204
	if ($sendWhat != 'dirs') {
205
		// Replaced array_splice, because it resets numeric indexes (like files or dirs with a numeric name)
206
		// Here we reduce the list to the range of $limit beginning at $start 
207
		$a = 0;
208
		$output_array = array();
209
		foreach ($list as $key => $value) {
210
			if ($a >= $GLOBALS['start'] && ($a - $GLOBALS['start'] < $GLOBALS['limit'])) {
211
				$output_array[$key] = $value;
212
			}
213
			$a++;
214
		}
215
		$list = $output_array;
216
	}
217

    
218
	while(list($item,$info) = each($list)) {
219
		// link to dir / file
220
		if (is_array($info)) {
221

    
222
			$abs_item = $info;
223
			if (extension_loaded('posix')) {
224
				$user_info = posix_getpwnam($info['user']);
225
				$file_info['uid'] = $user_info['uid'];
226
				$file_info['gid'] = $user_info['gid'];
227
			}
228
		} else {
229
			$abs_item = get_abs_item(utf8_decode($dir), $item);
230
			$file_info = @stat($abs_item);
231
		}
232

    
233
		$is_dir = get_is_dir($abs_item);
234

    
235
		if ($GLOBALS['use_mb']) {
236
			if (ext_isFTPMode()) {
237
				$items['items'][$i]['name'] = $item;
238
			} else if (mb_detect_encoding($item) == 'ASCII') {
239
				$items['items'][$i]['name'] = $item;
240
			} else {
241
				$items['items'][$i]['name'] = $item;
242
			}
243
		} else {
244
			$items['items'][$i]['name'] = ext_isFTPMode() ? $item : $item;
245
		}
246

    
247
		$items['items'][$i]['is_file']		= get_is_file($abs_item);
248
		$items['items'][$i]['is_archive']	= ext_isArchive($item) && !ext_isFTPMode();
249
		$items['items'][$i]['is_writable']	= $is_writable	= @$GLOBALS['ext_File']->is_writable($abs_item);
250
		$items['items'][$i]['is_chmodable'] = $is_chmodable = @$GLOBALS['ext_File']->is_chmodable($abs_item);
251
		$items['items'][$i]['is_readable']	= $is_readable	= @$GLOBALS['ext_File']->is_readable($abs_item);
252
		$items['items'][$i]['is_deletable'] = $is_deletable = @$GLOBALS['ext_File']->is_deletable($abs_item);
253
		$items['items'][$i]['is_editable']	= get_is_editable($abs_item);
254

    
255
		$items['items'][$i]['icon'] = _EXT_URL."/images/".get_mime_type($abs_item, "img");
256
		$items['items'][$i]['size'] = parse_file_size(get_file_size($abs_item)); // type
257
		$items['items'][$i]['type'] = get_mime_type($abs_item, "type"); // modified
258
		$items['items'][$i]['modified'] = parse_file_date(get_file_date($abs_item)); // permissions
259

    
260
		$perms = get_file_perms($abs_item);
261

    
262
		if ($perms) {
263
			if (strlen($perms)>3) {
264
				$perms = substr($perms, 2);
265
			}
266
			$items['items'][$i]['perms'] = $perms. ' (' . parse_file_perms($perms) . ')';
267
		} else {
268
			$items['items'][$i]['perms'] = ' (unknown) ';
269
		}
270

    
271
		$items['items'][$i]['perms'] = $perms. ' (' . parse_file_perms($perms) . ')';
272

    
273
		if (extension_loaded("posix")) {
274
			if ($file_info["uid"]) {
275
				$user_info = posix_getpwuid($file_info["uid"]);
276
				//$group_info = posix_getgrgid($file_info["gid"]);
277
				$items['items'][$i]['owner'] = $user_info["name"]. " (".$file_info["uid"].")";
278
			} else {
279
				$items['items'][$i]['owner'] = " (unknown) ";
280
			}
281
		} else {
282
			$items['items'][$i]['owner'] = 'n/a';
283
		}
284

    
285
		if ($is_dir && $sendWhat != 'files') {
286

    
287
			$id = str_replace('/', $GLOBALS['separator'], $dir). $GLOBALS['separator'].$item;
288
			$id = str_replace($GLOBALS['separator'], '_RRR_', $id);
289

    
290
			$qtip = "<strong>".ext_Lang::mime('dir',true)."</strong><br /><strong>".ext_Lang::msg('miscperms',true).":</strong> ".$perms."<br />";
291
			$qtip .= '<strong>'.ext_Lang::msg('miscowner',true).':</strong> '.$items['items'][$i]['owner'];
292
			if ($GLOBALS['use_mb']) {
293
				if (ext_isFTPMode()) {
294
					$dirlist[] = array('text' => htmlspecialchars($item),
295
									'id'		=> $id,
296
									'qtip'		=> $qtip,
297
									'is_writable'  => $is_writable,
298
									'is_chmodable' => $is_chmodable,
299
									'is_readable'  => $is_readable,
300
									'is_deletable' => $is_deletable,
301
									'cls'		=> 'folder');
302
				} else if (mb_detect_encoding($item) == 'ASCII') {
303
					$dirlist[] = array('text' => htmlspecialchars($item),
304
									'id'		=> utf8_encode($id),
305
									'qtip'		=> $qtip,
306
									'is_writable'  => $is_writable,
307
									'is_chmodable' => $is_chmodable,
308
									'is_readable'  => $is_readable,
309
									'is_deletable' => $is_deletable,
310
									'cls'		=> 'folder');
311
				} else {
312
					$dirlist[] = array('text' => htmlspecialchars($item),
313
									'id'		=> $id,
314
									'qtip'		=> $qtip,
315
									'is_writable'  => $is_writable,
316
									'is_chmodable' => $is_chmodable,
317
									'is_readable'  => $is_readable,
318
									'is_deletable' => $is_deletable,
319
									'cls'		=> 'folder');
320
				}
321
			} else {
322
				$dirlist[] = array('text' => htmlspecialchars(ext_isFTPMode() ? $item : $item),
323
									'id'		=> ext_isFTPMode() ? $id : utf8_encode($id),
324
									'qtip'		=> $qtip,
325
									'is_writable'  => $is_writable,
326
									'is_chmodable' => $is_chmodable,
327
									'is_readable'  => $is_readable,
328
									'is_deletable' => $is_deletable,
329
									'cls'		=> 'folder');
330
			}
331
		}
332
		if (!$is_dir && $sendWhat == 'files' || $sendWhat == 'both') {
333
			$i++;
334
		}
335
	}
336

    
337
	while(@ob_end_clean());
338

    
339
	if ($sendWhat == 'dirs') {
340
		$result = $dirlist;
341
	} else {
342
		$result = $items;
343
	}
344
	$classname = class_exists('ext_Json') ? 'ext_Json' : 'Services_JSON';
345
	$json = new $classname();
346
	echo $json->encode($result);
347

    
348
	ext_exit();
349

    
350
}
351
class ext_List extends ext_Action {
352

    
353
	function execAction($dir) {			// list directory contents
354
		global $dir_up, $mosConfig_live_site, $_VERSION;
355

    
356
		$allow = ($GLOBALS["permissions"]&01) == 01;
357
		$admin = ((($GLOBALS["permissions"]&04) == 04) || (($GLOBALS["permissions"]&02) == 02));
358

    
359
		$dir_up = dirname($dir);
360
		if ($dir_up == ".") $dir_up = "";
361

    
362
		if (!get_show_item($dir_up,basename($dir))) {
363
			ext_Result::sendResult('list', false, $dir." : ".$GLOBALS["error_msg"]["accessdir"]);
364
		}
365

    
366
		// Sorting of items
367
		if ($GLOBALS["direction"] == "ASC") {
368
			$_srt = "no";
369
		} else {
370
			$_srt = "yes";
371
		}
372

    
373
		show_header();
374
		extHTML::loadExtJS();
375
		
376
		?>
377
		
378
	<div id="dirtree-panel"></div>
379
	<div id="locationbar-panel"></div>
380
	<div id="item-grid"></div>
381
	<div id="ext_statusbar" class="ext_statusbar"></div>
382

    
383
	<?php
384
		// That's the main javascript file to build the Layout & App Logic
385
		include(_EXT_PATH.'/scripts/application.js.php');
386
	}
387
}
388

    
(2-2/2)