Skip to content

Commit de6fcff

Browse files
committed
Cleaned it up a bit.
1 parent 0154108 commit de6fcff

File tree

1 file changed

+40
-67
lines changed

1 file changed

+40
-67
lines changed

web/file_manager/files.php

Lines changed: 40 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,18 @@
44
//define(LISTING_TIMEOUT, 0.000001);
55
define(LISTING_TIMEOUT, 5);
66

7-
8-
9-
107
//echo 'files: ';
118
//$files = scandir(__DIR__);
129

13-
1410
//echo '<pre>';
1511
//print_r($files);
1612

17-
1813
//$_REQUEST['sort_field'] = 'size';
1914
$_REQUEST['sort_field'] = 'name';
2015
//$_REQUEST['sort_field'] = 'atime';
2116
//$_REQUEST['sort_field'] = 'mtime';
2217
$_REQUEST['sort_desc'] = 1;
2318

24-
25-
2619
/*
2720
+- copy file / dir [ recursive ]
2821
+- rename(move) file / dir
@@ -33,7 +26,7 @@
3326
+- create dir
3427
*/
3528

36-
switch($_REQUEST['action']){
29+
switch($_REQUEST['action']) {
3730
case 'copy': fm_copy($_REQUEST['source'], $_REQUEST['dest']); break;
3831
case 'rename': fm_rename($_REQUEST['source'], $_REQUEST['dest']); break;
3932
case 'delete': fm_delete($_REQUEST['source']); break;
@@ -53,24 +46,12 @@
5346
break;
5447
}
5548

56-
57-
58-
59-
60-
6149
//echo $_GET['sort_field'];
6250

6351
// if(in_array($_GET['sort_field'], $available_sort_fields)){
6452
// echo '1';
6553
// }
6654

67-
68-
69-
70-
71-
72-
73-
7455
/*
7556
upload_file
7657
@@ -87,67 +68,66 @@
8768
download file / image
8869
*/
8970

90-
91-
92-
function fm_create_file($filename){
71+
function fm_create_file($filename)
72+
{
9373
if(is_file($filename))
9474
return array('error' => 'file exists', 'code' => 1);
9575

96-
return !!fopen($filename, 'w');
76+
return (bool) fopen($filename, 'w'); // (bool) > !!, sorry
9777
}
9878

99-
100-
function fm_create_dir($dirname){
79+
function fm_create_dir($dirname)
80+
{
10181
if(is_dir($filename))
10282
return array('error' => 'directory exists', 'code' => 1);
10383

10484
// TODO set parent directory mode
10585
return mkdir($dirname);
10686
}
10787

108-
109-
function fm_chown($filename, $recursive = 0, $uid = FALSE, $gid = FALSE){
110-
if(is_dir($filename) && $recursive){
88+
function fm_chown($filename, $recursive = 0, $uid = FALSE, $gid = FALSE)
89+
{
90+
if (is_dir($filename) && $recursive) {
11191
$dir_handle = opendir($dir);
112-
while ($item = readdir($dir_handle)){
113-
if (!in_array($item, array('.','..'))){
92+
while ($item = readdir($dir_handle)) {
93+
if (!in_array($item, array('.','..'))) {
11494
$new_item = $filename.'/'.$item;
11595

116-
if($uid !== FALSE) chown($new_item, (int)$uid);
117-
if($gid !== FALSE) chgrp($new_item, (int)$gid);
96+
if ($uid !== FALSE) chown($new_item, (int)$uid);
97+
if ($gid !== FALSE) chgrp($new_item, (int)$gid);
11898

119-
if(is_dir($new_item)){
99+
if (is_dir($new_item)) {
120100
fm_chown($new_item, $recursive, $uid, $gid);
121101
}
122102
}
123103
}
124-
}else{
104+
} else {
125105
if($uid !== FALSE) chown($filename, (int)$uid);
126106
if($gid !== FALSE) chgrp($filename, (int)$gid);
127107
}
128108
}
129109

130-
131-
function fm_chmod($filename, $recursive = 0, $mode){
132-
if(is_dir($filename) && $recursive){
110+
function fm_chmod($filename, $recursive = 0, $mode)
111+
{
112+
if(is_dir($filename) && $recursive) {
133113
$dir_handle = opendir($dir);
134-
while ($item = readdir($dir_handle)){
135-
if (!in_array($item, array('.','..'))){
114+
while ($item = readdir($dir_handle)) {
115+
if (!in_array($item, array('.','..'))) {
136116
$new_item = $filename.'/'.$item;
137117
chmod($new_item, octdec($mode));
138118

139-
if(is_dir($new_item)){
119+
if (is_dir($new_item)) {
140120
fm_chmod($new_item, $recursive, $mode);
141121
}
142122
}
143123
}
144-
}else{
124+
} else {
145125
chmod($filename, octdec($mode));
146126
}
147127
}
148128

149-
150-
function fm_delete($filename){
129+
function fm_delete($filename)
130+
{
151131
if(is_dir($filename)){
152132
foreach (
153133
$iterator = new RecursiveIteratorIterator(
@@ -162,19 +142,21 @@ function fm_delete($filename){
162142
// copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
163143
}
164144
}
165-
}else{
145+
} else {
166146
return unlink($filename);
167147
}
168148
}
169149

170150

171-
function fm_rename($source, $dest){
151+
function fm_rename($source, $dest)
152+
{
172153
return rename($source, $dest);
173154
}
174155

175156

176-
function fm_copy($source, $dest){
177-
if(is_dir($source)){
157+
function fm_copy($source, $dest)
158+
{
159+
if (is_dir($source)) {
178160
foreach (
179161
$iterator = new RecursiveIteratorIterator(
180162
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
@@ -187,14 +169,13 @@ function fm_copy($source, $dest){
187169
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
188170
}
189171
}
190-
191172
}else{
192173
return copy($source, $dest);
193174
}
194175
}
195176

196-
197-
function list_dir(){
177+
function list_dir()
178+
{
198179
$dir_iterator = new RecursiveDirectoryIterator("/path");
199180
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
200181
// could use CHILD_FIRST if you so wish
@@ -214,12 +195,9 @@ function list_dir(){
214195
echo "\nTotal file size: ", $size, " bytes\n";
215196
}
216197

217-
218-
219-
220198
/// fast removing directory
221-
function rmrf($dir) {
222-
199+
function rmrf($dir)
200+
{
223201
foreach (glob($dir) as $file) {
224202
if (is_dir($file)) {
225203
rmrf("$file/*");
@@ -230,9 +208,6 @@ function rmrf($dir) {
230208
}
231209
}
232210

233-
234-
235-
236211
function dir_list($dir, $sort = 0)
237212
{
238213
$sort_order_for_filename = SORT_ASC;
@@ -252,7 +227,7 @@ function dir_list($dir, $sort = 0)
252227
if (!in_array($object, array('.','..'))){
253228
$filename = $dir . $object;
254229
$time = microtime(true) - $start;
255-
if($time <= LISTING_TIMEOUT){
230+
if ($time <= LISTING_TIMEOUT) {
256231
$stats = stat($filename);
257232
$mode = explain_mode($stats['mode']);
258233
$perms = decoct(fileperms($filename));
@@ -274,8 +249,11 @@ function dir_list($dir, $sort = 0)
274249
);
275250
}else{
276251
$listing['timeout_exeeded'] = TRUE;
277-
if(is_dir($filename)){ $type = 'd';
278-
}else{ $type = '-'; }
252+
if (is_dir($filename)) {
253+
$type = 'd';
254+
} else {
255+
$type = '-';
256+
}
279257

280258
$item = array(
281259
'name' => $object,
@@ -294,7 +272,6 @@ function dir_list($dir, $sort = 0)
294272
);
295273
}
296274

297-
298275
$listing['count']++;
299276

300277
if($item['type'] == 'd'){
@@ -311,7 +288,6 @@ function dir_list($dir, $sort = 0)
311288
}
312289
$listing['time'] = microtime(TRUE) - $start;
313290

314-
315291
if(!$listing['timeout_exeeded']){
316292
if(in_array($_REQUEST['sort_field'], $available_sort_fields)){
317293
if($_REQUEST['sort_desc']){
@@ -324,7 +300,6 @@ function dir_list($dir, $sort = 0)
324300

325301
return $listing;
326302
}
327-
328303

329304
function explain_mode($mode)
330305
{
@@ -355,5 +330,3 @@ function explain_mode($mode)
355330

356331
return $info;
357332
}
358-
359-
?>

0 commit comments

Comments
 (0)