-
Notifications
You must be signed in to change notification settings - Fork 29
/
ft2.php
1904 lines (1831 loc) · 62 KB
/
ft2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* File Thingie - Andreas Haugstrup Pedersen <[email protected]>
* The newest version of File Thingie can be found at <http://www.solitude.dk/filethingie/>
*
* Copyright (c) 2003-2012 Andreas Haugstrup Pedersen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
# Version information #
define("VERSION", "2.5.7"); // Current version of File Thingie.
define("INSTALL", "EXPANDED"); // Type of File Thingie installation. EXPANDED or SIMPLE.
define("MUTEX", $_SERVER['PHP_SELF']);
$ft = array();
$ft['settings'] = array();
$ft['groups'] = array();
$ft['users'] = array();
$ft['plugins'] = array();
/**
* Check if a login cookie is valid.
*
* @param $c
* The login cookie from $_COOKIE.
* @return The username of the cookie user. FALSE if cookie is not valid.
*/
function ft_check_cookie($c) {
global $ft;
// Check primary user.
if ($c == md5(USERNAME.PASSWORD)) {
return USERNAME;
}
// Check users array.
if (is_array($ft['users']) && sizeof($ft['users']) > 0) {
// Loop through users.
foreach ($ft['users'] as $user => $a) {
if ($c == md5($user.$a['password'])) {
return $user;
}
}
}
return FALSE;
}
/**
* Check if directory is on the blacklist.
*
* @param $dir
* Directory path.
* @return TRUE if directory is not blacklisted.
*/
function ft_check_dir($dir) {
// Check against folder blacklist.
if (FOLDERBLACKLIST != "") {
$blacklist = explode(" ", FOLDERBLACKLIST);
foreach ($blacklist as $c) {
if (substr($dir, 0, strlen(ft_get_root().'/'.$c)) == ft_get_root().'/'.$c) {
return FALSE;
}
}
return TRUE;
} else {
return TRUE;
}
}
/**
* Check if file actions are allowed in the current directory.
*
* @return TRUE is file actions are allowed.
*/
function ft_check_fileactions() {
if (FILEACTIONS === TRUE) {
// Uploads are universally turned on.
return TRUE;
} else if (FILEACTIONS == TRUE && FILEACTIONS == substr(ft_get_dir(), 0, strlen(FILEACTIONS))) {
// Uploads are allowed in the current directory and subdirectories only.
return TRUE;
}
return FALSE;
}
/**
* Check if file is on the blacklist.
*
* @param $file
* File name.
* @return TRUE if file is not blacklisted.
*/
function ft_check_file($file) {
// Check against file blacklist.
if (FILEBLACKLIST != "") {
$blacklist = explode(" ", strtolower(FILEBLACKLIST));
if (in_array(strtolower($file), $blacklist)) {
return FALSE;
} else {
return TRUE;
}
} else {
return TRUE;
}
}
/**
* Check if file type is on the blacklist.
*
* @param $file
* File name.
* @return TRUE if file is not blacklisted.
*/
function ft_check_filetype($file) {
$type = strtolower(ft_get_ext($file));
// Check if we are using a whitelist.
if (FILETYPEWHITELIST != "") {
// User wants a whitelist
$whitelist = explode(" ", FILETYPEWHITELIST);
if (in_array($type, $whitelist)) {
return TRUE;
} else {
return FALSE;
}
} else {
// Check against file blacklist.
if (FILETYPEBLACKLIST != "") {
$blacklist = explode(" ", FILETYPEBLACKLIST);
if (in_array($type, $blacklist)) {
return FALSE;
} else {
return TRUE;
}
} else {
return TRUE;
}
}
}
/**
* Check if a user is authenticated to view the page or not. Must be called on all pages.
*
* @return TRUE if the user is authenticated.
*/
function ft_check_login() {
global $ft;
$valid_login = 0;
if (LOGIN == TRUE) {
if (empty($_SESSION['ft_user_'.MUTEX])) {
$cookie_mutex = str_replace('.', '_', MUTEX);
// Session variable has not been set. Check if there is a valid cookie or login form has been submitted or return false.
if (REMEMBERME == TRUE && !empty($_COOKIE['ft_user_'.$cookie_mutex])) {
// Verify cookie.
$cookie = ft_check_cookie($_COOKIE['ft_user_'.$cookie_mutex]);
if (!empty($cookie)) {
// Cookie valid. Login.
$_SESSION['ft_user_'.MUTEX] = $cookie;
ft_invoke_hook('loginsuccess', $cookie);
ft_redirect();
}
}
if (!empty($_POST['act']) && $_POST['act'] == "dologin") {
// Check username and password from login form.
if (!empty($_POST['ft_user']) && $_POST['ft_user'] == USERNAME && $_POST['ft_pass'] == PASSWORD) {
// Valid login.
$_SESSION['ft_user_'.MUTEX] = USERNAME;
$valid_login = 1;
}
// Default user was not valid, we check additional users (if any).
if (is_array($ft['users']) && sizeof($ft['users']) > 0) {
// Check username and password.
if (array_key_exists($_POST['ft_user'], $ft['users']) && $ft['users'][$_POST['ft_user']]['password'] == $_POST['ft_pass']) {
// Valid login.
$_SESSION['ft_user_'.MUTEX] = $_POST['ft_user'];
$valid_login = 1;
}
}
if ($valid_login == 1) {
// Set cookie.
if (!empty($_POST['ft_cookie']) && REMEMBERME) {
setcookie('ft_user_'.MUTEX, md5($_POST['ft_user'].$_POST['ft_pass']), time()+60*60*24*3);
} else {
// Delete cookie
setcookie('ft_user_'.MUTEX, md5($_POST['ft_user'].$_POST['ft_pass']), time()-3600);
}
ft_invoke_hook('loginsuccess', $_POST['ft_user']);
ft_redirect();
} else {
ft_invoke_hook('loginfail', $_POST['ft_user']);
ft_redirect("act=error");
}
}
return FALSE;
} else {
return TRUE;
}
} else {
return TRUE;
}
}
/**
* Check if a move action is inside the file actions area if FILEACTIONS is set to a specific director.
*
* @param $dest
* The directory to move to.
* @return TRUE if move action is allowed.
*/
function ft_check_move($dest) {
if (FILEACTIONS === TRUE) {
return TRUE;
}
// Check if destination is within the fileactions area.
$dest = substr($dest, 0, strlen($dest));
$levels = substr_count(substr(ft_get_dir(), strlen(FILEACTIONS)), '/');
if ($levels <= substr_count($dest, '../')) {
return TRUE;
} else {
return FALSE;
}
}
/**
* Check if uploads are allowed in the current directory.
*
* @return TRUE if uploads are allowed.
*/
function ft_check_upload() {
if (UPLOAD === TRUE) {
// Uploads are universally turned on.
return TRUE;
} else if (UPLOAD == TRUE && UPLOAD == substr(ft_get_dir(), 0, strlen(UPLOAD))) {
// Uploads are allowed in the current directory and subdirectories only.
return TRUE;
}
return FALSE;
}
/**
* Check if a user exists.
*
* @param $username
* Username to check.
* @return TRUE if user exists.
*/
function ft_check_user($username) {
global $ft;
if ($username == USERNAME) {
return TRUE;
} elseif (is_array($ft['users']) && sizeof($ft['users']) > 0 && array_key_exists($username, $ft['users'])) {
return TRUE;
}
return FALSE;
}
/**
* Remove unwanted characters from the settings array.
*/
function ft_clean_settings($settings) {
// TODO: Clean DIR, UPLOAD and FILEACTIONS so they can't start with ../
return $settings;
}
/**
* Run all system actions based on the value of $_REQUEST['act'].
*/
function ft_do_action() {
if (!empty($_REQUEST['act'])) {
// Only one callback action is allowed. So only the first hook that acts on an action is run.
ft_invoke_hook('action', $_REQUEST['act']);
# mkdir
if ($_REQUEST['act'] == "createdir" && CREATE === TRUE) {
$_POST['newdir'] = trim($_POST['newdir']);
if ($_POST['type'] == 'file') {
// Check file against blacklists
if (strlen($_POST['newdir']) > 0 && ft_check_filetype($_POST['newdir']) && ft_check_file($_POST['newdir'])) {
// Create file.
$newfile = ft_get_dir()."/{$_POST['newdir']}";
if (file_exists($newfile)) {
// Redirect
ft_set_message(t("File could not be created. File already exists."), 'error');
ft_redirect("dir=".$_REQUEST['dir']);
} elseif (@touch($newfile)) {
// Redirect.
ft_set_message(t("File created."));
ft_redirect("dir=".$_REQUEST['dir']);
} else {
// Redirect
ft_set_message(t("File could not be created."), 'error');
ft_redirect("dir=".$_REQUEST['dir']);
}
} else {
// Redirect
ft_set_message(t("File could not be created."), 'error');
ft_redirect("dir=".$_REQUEST['dir']);
}
} elseif ($_POST['type'] == 'url') {
// Create from URL.
$newname = trim(substr($_POST['newdir'], strrpos($_POST['newdir'], '/')+1));
if (strlen($newname) > 0 && ft_check_filetype($newname) && ft_check_file($newname)) {
// Open file handlers.
$rh = fopen($_POST['newdir'], 'rb');
if ($rh === FALSE) {
ft_set_message(t("Could not open URL. Possible reason: URL wrappers not enabled."), 'error');
ft_redirect("dir=".$_REQUEST['dir']);
}
$wh = fopen(ft_get_dir().'/'.$newname, 'wb');
if ($wh === FALSE) {
ft_set_message(t("File could not be created."), 'error');
ft_redirect("dir=".$_REQUEST['dir']);
}
// Download anf write file.
while (!feof($rh)) {
if (fwrite($wh, fread($rh, 1024)) === FALSE) {
ft_set_message(t("File could not be saved."), 'error');
}
}
fclose($rh);
fclose($wh);
ft_redirect("dir=".$_REQUEST['dir']);
} else {
// Redirect
ft_set_message(t("File could not be created."), 'error');
ft_redirect("dir=".$_REQUEST['dir']);
}
} else {
// Create directory.
// Check input.
// if (strstr($_POST['newdir'], ".")) {
// Throw error (redirect).
// ft_redirect("status=createddirfail&dir=".$_REQUEST['dir']);
// } else {
$_POST['newdir'] = ft_stripslashes($_POST['newdir']);
$newdir = ft_get_dir()."/{$_POST['newdir']}";
$oldumask = umask(0);
if (strlen($_POST['newdir']) > 0 && @mkdir($newdir, DIRPERMISSION)) {
ft_set_message(t("Directory created."));
ft_redirect("dir=".$_REQUEST['dir']);
} else {
// Redirect
ft_set_message(t("Directory could not be created."), 'error');
ft_redirect("dir=".$_REQUEST['dir']);
}
umask($oldumask);
// }
}
# Move
} elseif ($_REQUEST['act'] == "move" && ft_check_fileactions() === TRUE) {
// Check that both file and newvalue are set.
$file = trim(ft_stripslashes($_REQUEST['file']));
$dir = trim(ft_stripslashes($_REQUEST['newvalue']));
if (substr($dir, -1, 1) != "/") {
$dir .= "/";
}
// Check for level.
if (substr_count($dir, "../") <= substr_count(ft_get_dir(), "/") && ft_check_move($dir) === TRUE) {
$dir = ft_get_dir()."/".$dir;
if (!empty($file) && file_exists(ft_get_dir()."/".$file)) {
// Check that destination exists and is a directory.
if (is_dir($dir)) {
// Move file.
if (@rename(ft_get_dir()."/".$file, $dir."/".$file)) {
// Success.
ft_set_message(t("!old was moved to !new", array('!old' => $file, '!new' => $dir)));
ft_redirect("dir={$_REQUEST['dir']}");
} else {
// Error rename failed.
ft_set_message(t("!old could not be moved.", array('!old' => $file)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
} else {
// Error dest. isn't a dir or doesn't exist.
ft_set_message(t("Could not move file. !old does not exist or is not a directory.", array('!old' => $dir)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
} else {
// Error source file doesn't exist.
ft_set_message(t("!old could not be moved. It doesn't exist.", array('!old' => $file)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
} else {
// Error level
ft_set_message(t("!old could not be moved outside the base directory.", array('!old' => $file)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
# Delete
} elseif ($_REQUEST['act'] == "delete" && ft_check_fileactions() === TRUE) {
// Check that file is set.
$file = ft_stripslashes($_REQUEST['file']);
if (!empty($file) && ft_check_file($file)) {
if (is_dir(ft_get_dir()."/".$file)) {
if (DELETEFOLDERS == TRUE) {
ft_rmdir_recurse(ft_get_dir()."/".$file);
}
if (!@rmdir(ft_get_dir()."/".$file)) {
ft_set_message(t("!old could not be deleted.", array('!old' => $file)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
} else {
ft_set_message(t("!old deleted.", array('!old' => $file)));
ft_redirect("dir={$_REQUEST['dir']}");
}
} else {
if (!@unlink(ft_get_dir()."/".$file)) {
ft_set_message(t("!old could not be deleted.", array('!old' => $file)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
} else {
ft_set_message(t("!old deleted.", array('!old' => $file)));
ft_redirect("dir={$_REQUEST['dir']}");
}
}
} else {
ft_set_message(t("!old could not be deleted.", array('!old' => $file)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
# Rename && Duplicate && Symlink
} elseif ($_REQUEST['act'] == "rename" || $_REQUEST['act'] == "duplicate" || $_REQUEST['act'] == "symlink" && ft_check_fileactions() === TRUE) {
// Check that both file and newvalue are set.
$old = trim(ft_stripslashes($_REQUEST['file']));
$new = trim(ft_stripslashes($_REQUEST['newvalue']));
if ($_REQUEST['act'] == 'rename') {
$m['typefail'] = t("!old was not renamed to !new (type not allowed).", array('!old' => $old, '!new' => $new));
$m['writefail'] = t("!old could not be renamed (write failed).", array('!old' => $old));
$m['destfail'] = t("File could not be renamed to !new since it already exists.", array('!new' => $new));
$m['emptyfail'] = t("File could not be renamed since you didn't specify a new name.");
} elseif ($_REQUEST['act'] == 'duplicate') {
$m['typefail'] = t("!old was not duplicated to !new (type not allowed).", array('!old' => $old, '!new' => $new));
$m['writefail'] = t("!old could not be duplicated (write failed).", array('!old' => $old));
$m['destfail'] = t("File could not be duplicated to !new since it already exists.", array('!new' => $new));
$m['emptyfail'] = t("File could not be duplicated since you didn't specify a new name.");
} elseif ($_REQUEST['act'] == 'symlink') {
$m['typefail'] = t("Could not create symlink to !old (type not allowed).", array('!old' => $old, '!new' => $new));
$m['writefail'] = t("Could not create symlink to !old (write failed).", array('!old' => $old));
$m['destfail'] = t("Could not create symlink !new since it already exists.", array('!new' => $new));
$m['emptyfail'] = t("Symlink could not be created since you didn't specify a name.");
}
if (!empty($old) && !empty($new)) {
if (ft_check_filetype($new) && ft_check_file($new)) {
// Make sure destination file doesn't exist.
if (!file_exists(ft_get_dir()."/".$new)) {
// Check that file exists.
if (is_writeable(ft_get_dir()."/".$old)) {
if ($_REQUEST['act'] == "rename") {
if (@rename(ft_get_dir()."/".$old, ft_get_dir()."/".$new)) {
// Success.
ft_set_message(t("!old was renamed to !new", array('!old' => $old, '!new' => $new)));
ft_redirect("dir={$_REQUEST['dir']}");
} else {
// Error rename failed.
ft_set_message(t("!old could not be renamed.", array('!old' => $old)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
} elseif ($_REQUEST['act'] == 'symlink') {
if (ADVANCEDACTIONS == TRUE) {
if (@symlink(realpath(ft_get_dir()."/".$old), ft_get_dir()."/".$new)) {
@chmod(ft_get_dir()."/{$new}", PERMISSION);
// Success.
ft_set_message(t("Created symlink !new", array('!old' => $old, '!new' => $new)));
ft_redirect("dir={$_REQUEST['dir']}");
} else {
// Error symlink failed.
ft_set_message(t("Symlink to !old could not be created.", array('!old' => $old)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
}
} else {
if (@copy(ft_get_dir()."/".$old, ft_get_dir()."/".$new)) {
// Success.
ft_set_message(t("!old was duplicated to !new", array('!old' => $old, '!new' => $new)));
ft_redirect("dir={$_REQUEST['dir']}");
} else {
// Error rename failed.
ft_set_message(t("!old could not be duplicated.", array('!old' => $old)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
}
} else {
// Error old file isn't writeable.
ft_set_message($m['writefail'], 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
} else {
// Error destination exists.
ft_set_message($m['destfail'], 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
} else {
// Error file type not allowed.
ft_set_message($m['typefail'], 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
} else {
// Error. File name not set.
ft_set_message($m['emptyfail'], 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
# upload
} elseif ($_REQUEST['act'] == "upload" && ft_check_upload() === TRUE && (LIMIT <= 0 || LIMIT > ROOTDIRSIZE)) {
// If we are to upload a file we will do so.
$msglist = 0;
foreach ($_FILES as $k => $c) {
if (!empty($c['name'])) {
$c['name'] = ft_stripslashes($c['name']);
if ($c['error'] == 0) {
// Upload was successfull
if (ft_check_filetype($c['name']) && ft_check_file($c['name'])) {
if (file_exists(ft_get_dir()."/{$c['name']}")) {
$msglist++;
ft_set_message(t('!file was not uploaded.', array('!file' => ft_get_nice_filename($c['name'], 20))) . ' ' . t("File already exists"), 'error');
} else {
if (@move_uploaded_file($c['tmp_name'], ft_get_dir()."/{$c['name']}")) {
@chmod(ft_get_dir()."/{$c['name']}", PERMISSION);
// Success!
$msglist++;
ft_set_message(t('!file was uploaded.', array('!file' => ft_get_nice_filename($c['name'], 20))));
ft_invoke_hook('upload', ft_get_dir(), $c['name']);
} else {
// File couldn't be moved. Throw error.
$msglist++;
ft_set_message(t('!file was not uploaded.', array('!file' => ft_get_nice_filename($c['name'], 20))) . ' ' . t("File couldn't be moved"), 'error');
}
}
} else {
// File type is not allowed. Throw error.
$msglist++;
ft_set_message(t('!file was not uploaded.', array('!file' => ft_get_nice_filename($c['name'], 20))) . ' ' . t("File type not allowed"), 'error');
}
} else {
// An error occurred.
switch($_FILES["localfile"]["error"]) {
case 1:
$msglist++;
ft_set_message(t('!file was not uploaded.', array('!file' => ft_get_nice_filename($c['name'], 20))) . ' ' . t("The file was too large"), 'error');
break;
case 2:
$msglist++;
ft_set_message(t('!file was not uploaded.', array('!file' => ft_get_nice_filename($c['name'], 20))) . ' ' . t("The file was larger than MAXSIZE setting."), 'error');
break;
case 3:
$msglist++;
ft_set_message(t('!file was not uploaded.', array('!file' => ft_get_nice_filename($c['name'], 20))) . ' ' . t("Partial upload. Try again"), 'error');
break;
case 4:
$msglist++;
ft_set_message(t('!file was not uploaded.', array('!file' => ft_get_nice_filename($c['name'], 20))) . ' ' . t("No file was uploaded. Please try again"), 'error');
break;
default:
$msglist++;
ft_set_message(t('!file was not uploaded.', array('!file' => ft_get_nice_filename($c['name'], 20))) . ' ' . t("Unknown error"), 'error');
break;
}
}
}
}
if ($msglist > 0) {
ft_redirect("dir=".$_REQUEST['dir']);
} else {
ft_set_message(t("Upload failed."), 'error');
ft_redirect("dir=".$_REQUEST['dir']);
}
# Unzip
} elseif ($_REQUEST['act'] == "unzip" && ft_check_fileactions() === TRUE) {
// Check that file is set.
$file = ft_stripslashes($_REQUEST['file']);
if (!empty($file) && ft_check_file($file) && ft_check_filetype($file) && strtolower(ft_get_ext($file)) == 'zip' && is_file(ft_get_dir()."/".$file)) {
$escapeddir = escapeshellarg(ft_get_dir()."/");
$escapedfile = escapeshellarg(ft_get_dir()."/".$file);
if (!@exec("unzip -n ".$escapedfile." -d ".$escapeddir)) {
ft_set_message(t("!old could not be unzipped.", array('!old' => $file)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
} else {
ft_set_message(t("!old unzipped.", array('!old' => $file)));
ft_redirect("dir={$_REQUEST['dir']}");
}
} else {
ft_set_message(t("!old could not be unzipped.", array('!old' => $file)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
# chmod
} elseif ($_REQUEST['act'] == "chmod" && ft_check_fileactions() === TRUE && ADVANCEDACTIONS == TRUE) {
// Check that file is set.
$file = ft_stripslashes($_REQUEST['file']);
if (!empty($file) && ft_check_file($file) && ft_check_filetype($file)) {
// Check that chosen permission i valid
if (is_numeric($_REQUEST['newvalue'])) {
$chmod = $_REQUEST['newvalue'];
if (substr($chmod, 0, 1) == '0') {
$chmod = substr($chmod, 0, 4);
} else {
$chmod = '0'.substr($chmod, 0, 3);
}
// Chmod
if (@chmod(ft_get_dir()."/".$file, intval($chmod, 8))) {
ft_set_message(t("Permissions changed for !old.", array('!old' => $file)));
ft_redirect("dir={$_REQUEST['dir']}");
clearstatcache();
} else {
ft_set_message(t("Could not change permissions for !old.", array('!old' => $file)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
} else {
ft_set_message(t("Could not change permissions for !old.", array('!old' => $file)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
} else {
ft_set_message(t("Could not change permissions for !old.", array('!old' => $file)), 'error');
ft_redirect("dir={$_REQUEST['dir']}");
}
# logout
} elseif ($_REQUEST['act'] == "logout") {
ft_invoke_hook('logout', $_SESSION['ft_user_'.MUTEX]);
$_SESSION = array();
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
session_destroy();
// Delete persistent cookie
setcookie('ft_user_'.MUTEX, '', time()-3600);
ft_redirect();
}
}
}
/**
* Convert PHP ini shorthand notation for file size to byte size.
*
* @return Size in bytes.
*/
function ft_get_bytes($val) {
$val = trim($val);
$last = strtolower($val{strlen($val)-1});
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
/**
* Get the total disk space consumed by files available to the current user.
* Files and directories on blacklists are not counted.
*
* @param $dirname
* Name of the directory to scan.
* @return Space consumed by this directory in bytes (not counting files and directories on blacklists).
*/
function ft_get_dirsize($dirname) {
if (!is_dir($dirname) || !is_readable($dirname)) {
return false;
}
$dirname_stack[] = $dirname;
$size = 0;
do {
$dirname = array_shift($dirname_stack);
$handle = opendir($dirname);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..' && is_readable($dirname . '/' . $file)) {
if (is_dir($dirname . '/' . $file)) {
if (ft_check_dir($dirname . '/' . $file)) {
$dirname_stack[] = $dirname . '/' . $file;
}
} else {
if (ft_check_file($file) && ft_check_filetype($file)) {
$size += filesize($dirname . '/' . $file);
}
}
}
}
closedir($handle);
} while (count($dirname_stack) > 0);
return $size;
}
/**
* Get the current directory.
*
* @return The current directory.
*/
function ft_get_dir() {
if (empty($_REQUEST['dir'])) {
return ft_get_root();
} else {
return ft_get_root().$_REQUEST['dir'];
}
}
/**
* Get file extension from a file name.
*
* @param $name
* File name.
* @return The file extension without the '.'
*/
function ft_get_ext($name) {
if (strstr($name, ".")) {
$ext = str_replace(".", "", strrchr($name, "."));
} else {
$ext = "";
}
return $ext;
}
/**
* Get a list of files in a directory with metadata.
*
* @param $dir
* The directory to scan.
* @param $sort
* Sorting parameter. Possible values: name, type, size, date. Defaults to 'name'.
* @return An array of files. Each item is an array:
* array(
* 'name' => '', // File name.
* 'shortname' => '', // File name.
* 'type' => '', // 'file' or 'dir'.
* 'ext' => '', // File extension.
* 'writeable' => '', // TRUE if writeable.
* 'perms' => '', // Permissions.
* 'modified' => '', // Last modified. Unix timestamp.
* 'size' => '', // File size in bytes.
* 'extras' => '' // Array of extra classes for this file.
* )
*/
function ft_get_filelist($dir, $sort = 'name') {
$filelist = array();
$subdirs = array();
if (ft_check_dir($dir) && $dirlink = @opendir($dir)) {
// Creates an array with all file names in current directory.
while (($file = readdir($dirlink)) !== false) {
if ($file != "." && $file != ".." && ((!is_dir("{$dir}/{$file}") && ft_check_file($file) && ft_check_filetype($file)) || is_dir("{$dir}/{$file}") && ft_check_dir("{$dir}/{$file}"))) { // Hide these two special cases and files and filetypes in blacklists.
$c = array();
$c['name'] = $file;
// $c['shortname'] = ft_get_nice_filename($file, 20);
$c['shortname'] = $file;
$c['type'] = "file";
$c['ext'] = ft_get_ext($file);
$c['writeable'] = is_writeable("{$dir}/{$file}");
// Grab extra options from plugins.
$c['extras'] = array();
$c['extras'] = ft_invoke_hook('fileextras', $file, $dir);
// File permissions.
if ($c['perms'] = @fileperms("{$dir}/{$file}")) {
if (is_dir("{$dir}/{$file}")) {
$c['perms'] = substr(base_convert($c['perms'], 10, 8), 2);
} else {
$c['perms'] = substr(base_convert($c['perms'], 10, 8), 3);
}
}
$c['modified'] = @filemtime("{$dir}/{$file}");
$c['size'] = @filesize("{$dir}/{$file}");
if (ft_check_dir("{$dir}/{$file}") && is_dir("{$dir}/{$file}")) {
$c['size'] = 0;
$c['type'] = "dir";
if ($sublink = @opendir("{$dir}/{$file}")) {
while (($current = readdir($sublink)) !== false) {
if ($current != "." && $current != ".." && ft_check_file($current)) {
$c['size']++;
}
}
closedir($sublink);
}
$subdirs[] = $c;
} else {
$filelist[] = $c;
}
}
}
closedir($dirlink);
// sort($filelist);
// Obtain a list of columns
$ext = array();
$name = array();
$date = array();
$size = array();
foreach ($filelist as $key => $row) {
$ext[$key] = strtolower($row['ext']);
$name[$key] = strtolower($row['name']);
$date[$key] = $row['modified'];
$size[$key] = $row['size'];
}
if ($sort == 'type') {
// Sort by file type and then name.
array_multisort($ext, SORT_ASC, $name, SORT_ASC, $filelist);
} elseif ($sort == 'size') {
// Sort by filesize date and then name.
array_multisort($size, SORT_ASC, $name, SORT_ASC, $filelist);
} elseif ($sort == 'date') {
// Sort by last modified date and then name.
array_multisort($date, SORT_DESC, $name, SORT_ASC, $filelist);
} else {
// Sort by file name.
array_multisort($name, SORT_ASC, $filelist);
}
// Always sort dirs by name.
sort($subdirs);
return array_merge($subdirs, $filelist);
} else {
return "dirfail";
}
}
/**
* Determine the max. size for uploaded files.
*
* @return Human-readable string of upload limit.
*/
function ft_get_max_upload() {
$post_max = ft_get_bytes(ini_get('post_max_size'));
$upload = ft_get_bytes(ini_get('upload_max_filesize'));
// Compare ini settings.
$max = (($post_max > $upload) ? $upload : $post_max);
// Compare with MAXSIZE.
if ($max > MAXSIZE) {
$max = MAXSIZE;
}
return ft_get_nice_filesize($max);
}
/**
* Shorten a file name to a given length maintaining the file extension.
*
* @param $name
* File name.
* @param $limit
* The maximum length of the file name.
* @return The shortened file name.
*/
function ft_get_nice_filename($name, $limit = -1) {
if ($limit > 0) {
$noext = $name;
if (strstr($name, '.')) {
$noext = substr($name, 0, strrpos($name, '.'));
}
$ext = ft_get_ext($name);
if (strlen($noext)-3 > $limit) {
$name = substr($noext, 0, $limit).'...';
if ($ext != '') {
$name = $name. '.' .$ext;
}
}
}
return $name;
}
/**
* Convert a number of bytes to a human-readable format.
*
* @param $size
* Integer. File size in bytes.
* @return String. Human-readable file size.
*/
function ft_get_nice_filesize($size) {
if (empty($size)) {
return "—";
} elseif (strlen($size) > 6) { // Convert to megabyte
return round($size/(1024*1024), 2)." MB";
} elseif (strlen($size) > 4 || $size > 1024) { // Convert to kilobyte
return round($size/1024, 0)." Kb";
} else {
return $size." b";
}
}
/**
* Get the root directory.
*
* @return The root directory.
*/
function ft_get_root() {
return DIR;
}
/**
* Get the name of the File Thingie file. Used in <form> actions.
*
* @return File name.
*/
function ft_get_self() {
return basename($_SERVER['PHP_SELF']);
}
/**
* Retrieve the contents of a URL.
*
* @return The contents of the URL as a string.
*/
function ft_get_url($url) {
$url_parsed = parse_url($url);
$host = $url_parsed["host"];
$port = 0;
$in = '';
if (!empty($url_parsed["port"])) {
$port = $url_parsed["port"];
}
if ($port==0) {
$port = 80;
}
$path = $url_parsed["path"];
if ($url_parsed["query"] != "") {
$path .= "?".$url_parsed["query"];
}
$out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
$fp = fsockopen($host, $port, $errno, $errstr, 30);
fwrite($fp, $out);
$body = false;
while ($fp && !feof($fp)) {
$s = fgets($fp, 1024);
if ( $body ) {
$in .= $s;
}
if ( $s == "\r\n" ) {
$body = true;
}
}
fclose($fp);
return $in;
}
/**
* Get users in a group.
*
* @param $group
* Name of group.
* @return Array of usernames.
*/
function ft_get_users_by_group($group) {
global $ft;
$userlist = array();
foreach ($ft['users'] as $user => $c) {
if (!empty($c['group']) && $c['group'] == $group) {
$userlist[] = $user;
}
}
return $userlist;
}
/**
* Invoke a hook in all loaded plugins.
*
* @param $hook
* Name of the hook to invoke.
* @param ...
* Arguments to pass to the hook.
* @return Array of results from all hooks run.
*/
function ft_invoke_hook() {
global $ft;
$args = func_get_args();
$hook = $args[0];
unset($args[0]);
// Loop through loaded plugins.
$return = array();
if (isset($ft['loaded_plugins']) && is_array($ft['loaded_plugins'])) {
foreach ($ft['loaded_plugins'] as $name) {
if (function_exists('ft_'.$name.'_'.$hook)) {
$result = call_user_func_array('ft_'.$name.'_'.$hook, $args);
if (isset($result) && is_array($result)) {
$return = array_merge_recursive($return, $result);
}
else if (isset($result)) {
$return[] = $result;
}
}
}
}
return $return;
}
/**
* Create HTML for the page body. Defaults to a file list.
*/
function ft_make_body() {
$str = "";
// Make system messages.
$status = '';