Skip to content

Commit 13d3fd4

Browse files
committed
File Manger stuff
1 parent e3f636c commit 13d3fd4

File tree

1 file changed

+67
-40
lines changed

1 file changed

+67
-40
lines changed

web/file_manager/files.php

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

7+
8+
9+
710
//echo 'files: ';
811
//$files = scandir(__DIR__);
912

13+
1014
//echo '<pre>';
1115
//print_r($files);
1216

17+
1318
//$_REQUEST['sort_field'] = 'size';
1419
$_REQUEST['sort_field'] = 'name';
1520
//$_REQUEST['sort_field'] = 'atime';
1621
//$_REQUEST['sort_field'] = 'mtime';
1722
$_REQUEST['sort_desc'] = 1;
1823

24+
25+
1926
/*
2027
+- copy file / dir [ recursive ]
2128
+- rename(move) file / dir
@@ -26,7 +33,7 @@
2633
+- create dir
2734
*/
2835

29-
switch($_REQUEST['action']) {
36+
switch($_REQUEST['action']){
3037
case 'copy': fm_copy($_REQUEST['source'], $_REQUEST['dest']); break;
3138
case 'rename': fm_rename($_REQUEST['source'], $_REQUEST['dest']); break;
3239
case 'delete': fm_delete($_REQUEST['source']); break;
@@ -46,12 +53,24 @@
4653
break;
4754
}
4855

56+
57+
58+
59+
60+
4961
//echo $_GET['sort_field'];
5062

5163
// if(in_array($_GET['sort_field'], $available_sort_fields)){
5264
// echo '1';
5365
// }
5466

67+
68+
69+
70+
71+
72+
73+
5574
/*
5675
upload_file
5776
@@ -68,66 +87,67 @@
6887
download file / image
6988
*/
7089

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

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

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

84104
// TODO set parent directory mode
85105
return mkdir($dirname);
86106
}
87107

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

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

99-
if (is_dir($new_item)) {
119+
if(is_dir($new_item)){
100120
fm_chown($new_item, $recursive, $uid, $gid);
101121
}
102122
}
103123
}
104-
} else {
124+
}else{
105125
if($uid !== FALSE) chown($filename, (int)$uid);
106126
if($gid !== FALSE) chgrp($filename, (int)$gid);
107127
}
108128
}
109129

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

119-
if (is_dir($new_item)) {
139+
if(is_dir($new_item)){
120140
fm_chmod($new_item, $recursive, $mode);
121141
}
122142
}
123143
}
124-
} else {
144+
}else{
125145
chmod($filename, octdec($mode));
126146
}
127147
}
128148

129-
function fm_delete($filename)
130-
{
149+
150+
function fm_delete($filename){
131151
if(is_dir($filename)){
132152
foreach (
133153
$iterator = new RecursiveIteratorIterator(
@@ -142,21 +162,19 @@ function fm_delete($filename)
142162
// copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
143163
}
144164
}
145-
} else {
165+
}else{
146166
return unlink($filename);
147167
}
148168
}
149169

150170

151-
function fm_rename($source, $dest)
152-
{
171+
function fm_rename($source, $dest){
153172
return rename($source, $dest);
154173
}
155174

156175

157-
function fm_copy($source, $dest)
158-
{
159-
if (is_dir($source)) {
176+
function fm_copy($source, $dest){
177+
if(is_dir($source)){
160178
foreach (
161179
$iterator = new RecursiveIteratorIterator(
162180
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
@@ -169,13 +187,14 @@ function fm_copy($source, $dest)
169187
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
170188
}
171189
}
190+
172191
}else{
173192
return copy($source, $dest);
174193
}
175194
}
176195

177-
function list_dir()
178-
{
196+
197+
function list_dir(){
179198
$dir_iterator = new RecursiveDirectoryIterator("/path");
180199
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
181200
// could use CHILD_FIRST if you so wish
@@ -195,9 +214,12 @@ function list_dir()
195214
echo "\nTotal file size: ", $size, " bytes\n";
196215
}
197216

217+
218+
219+
198220
/// fast removing directory
199-
function rmrf($dir)
200-
{
221+
function rmrf($dir) {
222+
201223
foreach (glob($dir) as $file) {
202224
if (is_dir($file)) {
203225
rmrf("$file/*");
@@ -208,6 +230,9 @@ function rmrf($dir)
208230
}
209231
}
210232

233+
234+
235+
211236
function dir_list($dir, $sort = 0)
212237
{
213238
$sort_order_for_filename = SORT_ASC;
@@ -227,7 +252,7 @@ function dir_list($dir, $sort = 0)
227252
if (!in_array($object, array('.','..'))){
228253
$filename = $dir . $object;
229254
$time = microtime(true) - $start;
230-
if ($time <= LISTING_TIMEOUT) {
255+
if($time <= LISTING_TIMEOUT){
231256
$stats = stat($filename);
232257
$mode = explain_mode($stats['mode']);
233258
$perms = decoct(fileperms($filename));
@@ -249,11 +274,8 @@ function dir_list($dir, $sort = 0)
249274
);
250275
}else{
251276
$listing['timeout_exeeded'] = TRUE;
252-
if (is_dir($filename)) {
253-
$type = 'd';
254-
} else {
255-
$type = '-';
256-
}
277+
if(is_dir($filename)){ $type = 'd';
278+
}else{ $type = '-'; }
257279

258280
$item = array(
259281
'name' => $object,
@@ -272,6 +294,7 @@ function dir_list($dir, $sort = 0)
272294
);
273295
}
274296

297+
275298
$listing['count']++;
276299

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

314+
291315
if(!$listing['timeout_exeeded']){
292316
if(in_array($_REQUEST['sort_field'], $available_sort_fields)){
293317
if($_REQUEST['sort_desc']){
@@ -300,6 +324,7 @@ function dir_list($dir, $sort = 0)
300324

301325
return $listing;
302326
}
327+
303328

304329
function explain_mode($mode)
305330
{
@@ -330,3 +355,5 @@ function explain_mode($mode)
330355

331356
return $info;
332357
}
358+
359+
?>

0 commit comments

Comments
 (0)