-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpfib.php
3598 lines (3007 loc) · 124 KB
/
wpfib.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
/*
Plugin Name: Smallerik File Browser for EAGLE
Plugin URI: http://www.smallerik.com/index.php/wordpress-plugins/smallerik-file-browser
Description: This plugin enables (authorized) users to <strong>embed a file repository inside a standard Wordpress page or post</strong>. File repositories point to a specific area of the filesystem (inside or outside of the web root). The same page or post <strong>can optionally be made to display a different file repository for each user</strong>, thus obtaining a personal area for each user. Access level restrictions to actions such as upload, delete, rename, unzip, etc. can be set for each user or set of users of the repository.
Version: 1.1
Author: Enrico Sandoli
Author URI: http://www.smallerik.com/
License: GPLv2 or later
*/
/*
* Copyright 2012-2014 Enrico Sandoli (email: wordpress@smallerik.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Publi License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You can find copies of the GNU General Public License on
* http://www.gnu.org/licenses/ or you can write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110.1301 USA
*/
define('WPFIB_DEVELOPER_SITE', 'http://www.smallerik.com/index.php/wordpress-plugins/smallerik-file-browser');
define('DS', DIRECTORY_SEPARATOR);
define('REPOTYPE_UNSPECIFIED', 1);
define('REPOTYPE_STANDARD', 2);
define('REPOTYPE_USERBOUND', 3);
define('REPOTYPE_GROUPBOUND', 4);
define('USERBOUND_PARAMETER_ID', 1);
define('USERBOUND_PARAMETER_LOGIN', 2);
define('USERBOUND_PARAMETER_NAME', 3);
define('USERBOUND_PARAMETER_ID_LOGIN', 4);
define('USERBOUND_PARAMETER_ID_NAME', 5);
define('USERBOUND_PARAMETER_ID_LOGIN_NAME', 6);
define('CAN_DISPLAY_REPO', 1);
define('CAN_DOWNLOAD', 2);
define('CAN_UPLOAD', 3);
define('CAN_DELETE_FOLDERS', 4);
define('CAN_DELETE_FILES', 5);
define('CAN_RESTORE_FILES', 6);
define('CAN_CREATE_FOLDERS', 7);
define('CAN_RENAME_FILES', 8);
define('CAN_RENAME_FOLDERS', 9);
define('CAN_UNZIP_FILES', 10);
define('ACTION_NEEDS_RELOAD', 0);
define('ACTION_NEEDS_NOT_RELOAD', 1);
define('ACTION_TEXT_TYPE_NONE', 0);
define('ACTION_TEXT_TYPE_SUCCESS', 1);
define('ACTION_TEXT_TYPE_WARNING', 2);
define('ACTION_TEXT_TYPE_ERROR', 3);
define('SORT_BY_NAME', 1);
define('SORT_BY_SIZE', 2);
define('SORT_BY_CHANGED', 3);
define('SORT_ASCENDING', 1);
define('SORT_DESCENDING', 2);
define('LINK_THROUGH_SCRIPT', 0);
define('LINK_DIRECT_SAME_WINDOW', 1);
define('LINK_DIRECT_NEW_WINDOW', 2);
define('UPLOAD_HTML', 1);
define('UPLOAD_SWF', 2);
define('ACTIONS_BOX_IS_OPEN', 1);
define('ACTIONS_BOX_IS_CLOSED', 2);
// administrative settings
include_once plugin_dir_path(__FILE__).'includes/options.php';
register_activation_hook(__FILE__, 'wpfib_add_defaults_callback');
// global variables
$wpfib_version_number = "1.1";
// Enable output character encoding conversion only for this page ***
// http://it2.php.net/manual/en/mbstring.http.php
// Set HTTP output character encoding to SJIS
//mb_http_output('SJIS');
// Start buffering and specify "mb_output_handler" as
// callback function
//ob_start('mb_output_handler');
// *******************************************************************
if (get_magic_quotes_gpc())
{
$_POST = array_map( 'stripslashes_deep', $_POST );
$_GET = array_map( 'stripslashes_deep', $_GET );
$_COOKIE = array_map( 'stripslashes_deep', $_COOKIE );
$_REQUEST = array_map( 'stripslashes_deep', $_REQUEST );
}
// for language translations
load_plugin_textdomain('wpfib', false, 'wpfib/languages');
// this is the filter function used to remove the wpfib_art key from the URL
//function wpfib_remove_art_get_var($location, $status)
//{
// if(isset($_GET['wpfib_art']))
// {
// $location = remove_query_arg('wpfib_art', $location);
// }
// return $location;
//}
// add main shortcode
add_shortcode('wpfib', 'wpfib_shortcode_handler');
// start output buffering (needed to get cookies to work)
ob_start();
// main shortcode handler: must return a string that will replace the shortcode
function wpfib_shortcode_handler($atts, $content = null)
{
global $wpfib_options;
global $wpfib_baselink;
global $wpfib_imgdirNavigation;
$wpfib_baselink = site_url().DS."?p=".get_the_ID();
// init text
$text = "";
$success_text = "";
$warning_text = "";
$error_text = "";
// initialise all options parameters
wpfib_init_options($atts);
// load CSS and JS
// set jQuery
wp_enqueue_script('jquery');
// this cookie script must be enqueued after lightbox
wp_enqueue_script('cookie', plugin_dir_url(__FILE__).'js/jquery.cookie.js');
// write local css and js
$text .= "<style>".wpfib_do_css()."</style>";
$text .= "<script>".wpfib_do_js()."</script>";
// get the current user info
wpfib_get_userdata();
// check if the post/page is ok for displaying a repository (single post or list of them)
// if (!wpfib_page_or_post_check_passed($error_text))
// {
// return $text.$error_text;
// }
// check if the post/page was written by a trusted author
if (!wpfib_trusted_author_check_passed($error_text))
{
return $text.$error_text;
}
// set/get the repository path variables
$repo_abspath = "";
if (!wpfib_get_repository_path($repo_abspath, $error_text))
{
return $text.$error_text;
}
// once we have the repo path (it might be a userbound repo) define access rights
wpfib_get_access_rights();
// check if the current user has enough permissions to display the repository
if (!wpfib_can_display_repo($error_text))
{
return $text.$error_text;
}
// validate (and get) the current location curdir_relpath: this is the path of the current location, relative to the repo_abspath
$curdir_relpath = "";
if (!wpfib_get_current_location($curdir_relpath, $repo_abspath, $error_text))
{
return $text.$error_text;
}
// detect requests for cookie and reload accordingly if needed
if (wpfib_needs_to_reload_with_cookie($curdir_relpath))
{
wp_redirect($wpfib_baselink."&dir=".wpfib_path_encode($curdir_relpath));
}
// manage user actions will call separate functions for each individual action
// this function also manages all redirects and error/warning/success boxes display
wpfib_actions_handler($text, $repo_abspath, $curdir_relpath);
// display repository will call separate functions to render each div section of the repository
if (!wpfib_display_repository($text, $repo_abspath, $curdir_relpath, $error_text))
{
return $text.$error_text;
}
// if all goes well, return the text that will replace the shortcode
// we echo the html (it's still buffered), then get the contents from the buffer, clean it, end it and return the content
//echo $text;
//$content = ob_get_contents();
//ob_end_clean();
//return $content;
return $text;
}
function wpfib_init_options(&$atts)
{
global $wpfib_options;
global $wpfib_imgdirNavigation;
global $wpfib_imgdirExtensions;
global $wpfib_imgdirExtensionsPath;
global $wpfib_default_file_chmod;
global $wpfib_default_dir_chmod;
$wpfib_imgdirNavigation = plugin_dir_url(__FILE__).'media/smallerik/navigationIcons/';
$wpfib_imgdirExtensions = plugin_dir_url(__FILE__).'media/smallerik/extensionsIcons/';
$wpfib_imgdirExtensionsPath = plugin_dir_path(__FILE__).'media/smallerik/extensionsIcons/';
// get the backend values of the various parameters
$general_options = get_option('wpfib_general_options');
$levels_options = get_option('wpfib_levels_options');
$permissions_options = get_option('wpfib_permissions_options');
$display_options = get_option('wpfib_display_options');
$looks_options = get_option('wpfib_looks_options');
$advanced_options = get_option('wpfib_advanced_options');
// the defaults array includes all options that can be either defined only in a
// command options or backend options which may be overriden by command options
$defaults = array(
// defaults for possible command options (no backend equivalents)
'abspath' => '',
'relpath' => '',
'title' => '',
// defaults from general options
'repo' => $general_options['repo'],
// defaults from permissions options
'def_visitor_level' => $permissions_options['def_visitor_level'],
'def_visitor_level_strict' => $permissions_options['def_visitor_level_strict'],
'def_registered_level' => $permissions_options['def_registered_level'],
'def_userbound_level' => $permissions_options['def_userbound_level'],
'def_userbound_level_strict' => $permissions_options['def_userbound_level_strict'],
// defaults from display options
'type_of_link_to_files' => $display_options['type_of_link_to_files'],
'display_navigation' => $display_options['display_navigation'],
'display_file_filter' => $display_options['display_file_filter'],
'file_filter_width' => $display_options['file_filter_width'],
'display_file_ext' => $display_options['display_file_ext'],
'display_filesize' => $display_options['display_filesize'],
'filesize_separator' => $display_options['filesize_separator'],
'display_filedate' => $display_options['display_filedate'],
'date_format' => $display_options['date_format'],
'display_filetime' => $display_options['display_filetime'],
'display_seconds' => $display_options['display_seconds'],
'sort_by' => $display_options['sort_by'],
'sort_as' => $display_options['sort_as'],
'sort_nat'=> $display_options['sort_nat'],
// default from looks options (general - apply to all boxes)
'table_width' => $looks_options['table_width'],
'border_radius' => $looks_options['border_radius'],
'use_box_shadow' => $looks_options['use_box_shadow'],
'box_shadow_width' => $looks_options['box_shadow_width'],
'box_shadow_blur' => $looks_options['box_shadow_blur'],
'box_shadow_color' => $looks_options['box_shadow_color'],
'use_thumb_shadow' => $looks_options['use_thumb_shadow'],
'thumb_shadow_width' => $looks_options['thumb_shadow_width'],
'thumb_shadow_blur' => $looks_options['thumb_shadow_blur'],
'thumb_shadow_color' => $looks_options['thumb_shadow_color'],
'use_default_font_size' => $looks_options['use_default_font_size'],
'font_size' => $looks_options['font_size'],
'box_distance' => $looks_options['box_distance'],
// ---------------------------- (file/folder list params)
'header_bgcolor' => $looks_options['header_bgcolor'],
'icon_width' => $looks_options['icon_width'],
'icon_padding' => $looks_options['icon_padding'],
'thumbsize'=> $looks_options['thumbsize'],
'min_row_height' => $looks_options['min_row_height'],
'highlighted_color' => $looks_options['highlighted_color'],
'oddrows_color' => $looks_options['oddrows_color'],
'evenrows_color' => $looks_options['evenrows_color'],
'line_bgcolor' => $looks_options['line_bgcolor'],
'line_height' => $looks_options['line_height'],
// ---------------------------- (boxes colors and outlines)
'framebox_bgcolor' => $looks_options['framebox_bgcolor'],
'framebox_border' => $looks_options['framebox_border'],
'framebox_linetype' => $looks_options['framebox_linetype'],
'framebox_linecolor' => $looks_options['framebox_linecolor'],
'errorbox_bgcolor' => $looks_options['errorbox_bgcolor'],
'errorbox_border' => $looks_options['errorbox_border'],
'errorbox_linetype' => $looks_options['errorbox_linetype'],
'errorbox_linecolor' => $looks_options['errorbox_linecolor'],
'successbox_bgcolor' => $looks_options['successbox_bgcolor'],
'successbox_border' => $looks_options['successbox_border'],
'successbox_linetype' => $looks_options['successbox_linetype'],
'successbox_linecolor' => $looks_options['successbox_linecolor'],
// ---------------------------- (input styles)
'inputbox_bgcolor' => $looks_options['inputbox_bgcolor'],
'inputbox_border' => $looks_options['inputbox_border'],
'inputbox_linetype' => $looks_options['inputbox_linetype'],// echo "<br />WPFIB_DEFAULTS ----------------------------<br />";
// print_r($defaults);
// echo "<br />WPFIB_ATTS ----------------------------<br />";
// print_r($atts);
// echo "<br />WPFIB_OPTIONS ----------------------------<br />";
// print_r($wpfib_options);
'inputbox_linecolor' => $looks_options['inputbox_linecolor'],
);
// defaults from level options: we add non-empty level names to the defaults array
for ($level = 0; $level < WPFIB_LEVELS; $level++)
{
if (!empty($levels_options['level_'.($level + 1).'_name']))
$defaults[$levels_options['level_'.($level + 1).'_name']] = $permissions_options['level_'.($level + 1).'_users'];
}
// create global options with shortcode attributes (and defaults)
$wpfib_options = shortcode_atts($defaults, $atts);
if ($advanced_options['DEBUG_enabled'])
{
echo "<br />WPFIB_DEFAULTS ----------------------------<br />";
print_r($defaults);
echo "<br />WPFIB_ATTS ----------------------------<br />";
print_r($atts);
echo "<br />WPFIB_OPTIONS ----------------------------<br />";
print_r($wpfib_options);
}
// we now create the global options we don't allow front-end users to override and merge them to the global options array
$wpfib_options_protected = array(
// defaults from general options
'default_path' => $general_options['default_path'],
'is_path_relative' => $general_options['is_path_relative'],
'default_path_override_enabled' => $general_options['default_path_override_enabled'],//false,
// defaults from permissions options
'trusted_authors' => $permissions_options['trusted_authors'],
'allow_unzip' => $permissions_options['allow_unzip'],
'allow_file_archiving' => $permissions_options['allow_file_archiving'],
// defaults from display options
'hidden_files' => $display_options['hidden_files'],
'hidden_folders' => $display_options['hidden_folders'],
// defaults from advanced options
'userbound_dir_prefix' => $advanced_options['userbound_dir_prefix'],
'userbound_dir_params' => $advanced_options['userbound_dir_params'],
'userbound_dir_suffix' => $advanced_options['userbound_dir_suffix'],
'default_string_encoding' => $advanced_options['default_string_encoding'],
'default_file_chmod' => $advanced_options['default_file_chmod'],
'default_dir_chmod' => $advanced_options['default_dir_chmod'],
'DEBUG_enabled' => $advanced_options['DEBUG_enabled'],
);
$wpfib_options = array_merge($wpfib_options, $wpfib_options_protected);
// do required processing of some default values and store the results in new global variables
$wpfib_default_file_chmod = '0'.ltrim($wpfib_options['default_file_chmod'], "0");
$wpfib_default_file_chmod = octdec($wpfib_default_file_chmod); // convert octal mode to decimal
$wpfib_default_dir_chmod = '0'.ltrim($wpfib_options['default_dir_chmod'], "0");
$wpfib_default_dir_chmod = octdec($wpfib_default_dir_chmod); // convert octal mode to decimal
}
// TODO see if we need to use this
function wpfib_stripslashes()
{
// remove magic quotes if needed
if (function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc())
{
function wpfib_stripslashes_deep($value)
{
$value = is_array($value) ? array_map('wpfib_stripslashes_deep', $value) : stripslashes($value);
return $value;
}
$_POST = array_map('wpfib_stripslashes_deep', $_POST);
$_GET = array_map('wpfib_stripslashes_deep', $_GET);
$_COOKIE = array_map('wpfib_stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('wpfib_stripslashes_deep', $_REQUEST);
}
}
function wpfib_get_userdata()
{
global $wpfib_userdata;
$wpfib_userdata = array();
if (is_user_logged_in())
{
$data = get_userdata(get_current_user_id());
$wpfib_userdata['user_id'] = $data->ID;
$wpfib_userdata['user_login'] = $data->user_login;
$wpfib_userdata['user_name'] = $data->display_name;
$wpfib_userdata['remote_address'] = ($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : __('Unavailable', 'wpfib');
}
else
{
$wpfib_userdata['user_id'] = 0;
$wpfib_userdata['user_login'] = 'guest';
$wpfib_userdata['user_name'] = __('Non registered user', 'wpfib');
$wpfib_userdata['remote_address'] = ($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : __('Unavailable', 'wpfib');
}
}
function wpfib_get_access_rights()
{
global $wpfib_access_rights;
global $wpfib_options;
global $wpfib_repotype;
// if current user is a guest assign visitor's access level unless we are in a userbound repo and def_visitor_level is not strict
if (!is_user_logged_in())
{
if ($wpfib_repotype == REPOTYPE_USERBOUND && !$wpfib_options['def_visitor_level_strict'])
{
$access_level_name = $wpfib_options['def_userbound_level'];
}
else
{
$access_level_name = $wpfib_options['def_visitor_level'];
}
}
// otherwise (current user is registered) if the repo is userbound with def_userbound_level_strict force use of userbond acces level
else if ($wpfib_repotype == REPOTYPE_USERBOUND && $wpfib_options['def_userbound_level_strict'])
{
$access_level_name = $wpfib_options['def_userbound_level'];
}
// otherwise repo is not userbound or it is but userbound level is not strict, meaning it can be overridden by custom users permissions
else
{
// check if the current user ID or user name is specified in any of the user-permission lists; if it is,
// then get the relevant access level name, otherwise use the settings for registered users;
// because both user-permission lists and default registered users are either set in the backend
// or overriden by the command line, we use $wpfib_options for them
$access_level_name = $wpfib_repotype == REPOTYPE_USERBOUND ? $wpfib_options['def_userbound_level'] : $wpfib_options['def_registered_level'];
$levels_options = get_option('wpfib_levels_options');
for ($level = 0; $level < WPFIB_LEVELS; $level++)
{
if (empty($levels_options['level_'.($level + 1).'_name']))
continue;
if (wpfib_is_current_user_in_permissions_list_for_access_level($level))
{
$access_level_name = $levels_options['level_'.($level + 1).'_name'];
break;
}
}
}
// once a relevant access level has been found, get the actual rights (array with individual capabilities)
$wpfib_access_rights = wpfib_access_rights_for_level_name($access_level_name);
if ($wpfib_options['DEBUG_enabled'])
{
echo "<br />Level name <strong>$access_level_name</strong> will be used<br />";
}
}
// this function is used by wpfib_get_access_rights() to determine if the current user is in a specific user list
// (specific for an access level); the user list can be overriden
function wpfib_is_current_user_in_permissions_list_for_access_level($level)
{
global $wpfib_userdata;
global $wpfib_options;
$levels_options = get_option('wpfib_levels_options');
// we get the name from the backend options, and use this name to get the users list from $wpfib_options (which might be overriden)
$level_name = $levels_options['level_'.($level + 1).'_name'];
$users_list = preg_split("/[\s,]+/", $wpfib_options[$level_name]);
if (in_array($wpfib_userdata['user_id'], $users_list) || in_array($wpfib_userdata['user_login'], $users_list))
{
return true;
}
return false;
}
// this function is used by wpfib_get_access_rights() to determine the access rights from the access level
function wpfib_access_rights_for_level_name($level_name)
{
//global $wpfib_options;
$levels_options = get_option('wpfib_levels_options');
$access_rights = array(
CAN_DISPLAY_REPO => false,
CAN_DOWNLOAD => false,
CAN_UPLOAD => false,
CAN_DELETE_FOLDERS => false,
CAN_DELETE_FILES => false,
CAN_RESTORE_FILES => false,
CAN_CREATE_FOLDERS => false,
CAN_RENAME_FILES => false,
CAN_RENAME_FOLDERS => false,
CAN_UNZIP_FILES => false
);
for ($level = 0; $level < WPFIB_LEVELS; $level++)
{
if (empty($levels_options['level_'.($level + 1).'_name']))
continue;
if ($levels_options['level_'.($level + 1).'_name'] == $level_name)
{
$access_rights = array(
CAN_DISPLAY_REPO => $levels_options['function_1_level_'.($level + 1)],
CAN_DOWNLOAD => $levels_options['function_2_level_'.($level + 1)],
CAN_UPLOAD => $levels_options['function_3_level_'.($level + 1)],
CAN_DELETE_FOLDERS => $levels_options['function_4_level_'.($level + 1)],
CAN_DELETE_FILES => $levels_options['function_5_level_'.($level + 1)],
CAN_RESTORE_FILES => $levels_options['function_6_level_'.($level + 1)],
CAN_CREATE_FOLDERS => $levels_options['function_7_level_'.($level + 1)],
CAN_RENAME_FILES => $levels_options['function_8_level_'.($level + 1)],
CAN_RENAME_FOLDERS => $levels_options['function_9_level_'.($level + 1)],
CAN_UNZIP_FILES => $levels_options['function_10_level_'.($level + 1)]
);
break;
}
}
return $access_rights;
}
function wpfib_can_display_repo(&$error_text)
{
global $wpfib_access_rights;
global $wpfib_imgdirNavigation;
if ($wpfib_access_rights[CAN_DISPLAY_REPO])
{
return true;
}
else
{
$error_text = "<div id='JS_MAIN_DIV'><div id='JS_ERROR_DIV'><table><tr>"
."<td class='alertIcon'><img src=\"".$wpfib_imgdirNavigation."warning.png\"></td><td>"
.__('Area reserved to the Project Consortium<br /><b>Users who need to access to this Reserved Area, please contact the <a href="mailto: buono@promoter.it">Webmaster</a> to ask for login information!</b>', 'wpfib')."</td>"
."</tr></table></div><br /></div>";
return false;
}
}
function wpfib_trusted_author_check_passed(&$error_text)
{
global $wpfib_options;
global $wpfib_imgdirNavigation;
// an empty list means that every author is authorized
if (empty($wpfib_options['trusted_authors']))
{
return true;
}
// get details of lastmod user
$lastmod_author = get_the_modified_author();
$lastmod_author_data = get_user_by('login', $lastmod_author);
$lastmod_author_id = $lastmod_author_data->ID;
// echo "TMA = ".$lastmod_author."<br />";
// echo "TMA_ID = ".$lastmod_author_id."<br />";
$trusted_authors_list = preg_split("/[\s,]+/", $wpfib_options['trusted_authors']);
if (in_array($lastmod_author, $trusted_authors_list) || in_array($lastmod_author_id, $trusted_authors_list))
{
return true;
}
else
{
$msg = sprintf(__('Cannot display a file repository: this page was las modified by %s, '
.'who is not in the Trusted Authors list of the Smallerik File Browser plugin'), $lastmod_author);
$error_text = "<div id='JS_MAIN_DIV'><div id='JS_ERROR_DIV'><table><tr>"
."<td class='alertIcon'><img src=\"".$wpfib_imgdirNavigation."warning.png\"></td>"
."<td>".$msg."</td>"
."</tr></table></div><br /></div>";
return false;
}
}
function wpfib_page_or_post_check_passed(&$error_text)
{
global $wpfib_options;
global $wpfib_imgdirNavigation;
$is_singular = is_singular();
echo "IS_SINGULAR[".$is_singular."]<br />";
if ($is_singular)
{
return true;
}
else
{
$error_text = "<div id='JS_MAIN_DIV'><div id='JS_ERROR_DIV'><table><tr>"
."<td class='alertIcon'><img src=\"".$wpfib_imgdirNavigation."warning.png\"></td><td>"
.__('Cannot display a file repository: the page or post is not singular')."</td>"
."</tr></table></div><br /></div>";
return false;
}
}
function wpfib_needs_to_reload_with_cookie($curdir_relpath)
{
global $wpfib_baselink;
// check if we need to set a new selected_usergroup_index cookie (used to switch between usergroups when viewing a GROUPBOUND
// repository and the user belongs to more than one group) - we do it here as baselink is now defined
if (isset($_GET['selected_usergroup_index']))
{
setcookie('selected_usergroup_index', $_GET['selected_usergroup_index'], time() + 3600 * 24 * 365);
return true;
}
// set display filter cookie (request might come as a POST or GET variable
if (isset($_POST['current_filter_list']) && strlen($_POST['current_filter_list']))
{
setcookie('current_filter_list', $_POST['current_filter_list']);
return true;
}
else if (isset($_GET['current_filter_list']) && !strlen($_GET['current_filter_list']))
{
setcookie('current_filter_list', "");
return true;
}
return false;
}
function wpfib_get_repository_path(&$repo_abspath, &$error_text)
{
// ***********************************************************************************************************************
// MANAGE REPOSITORY INFO
// ***********************************************************************************************************************
// get the default path parameter and check if this is meant to be expressed as an absolute path or a path relative to the root folder
//
// however, before doing so, check if the default path is being overriden in this particular command - this is achieved by the command option
// abspath=<current_default_abspath> or relpath=<current_default_relative_path> - This is ONLY POSSIBLE IF the defalt path override parameter is enabled
//
// we can then check if a repository is found as repo=<repository>, which we take as the repository folder for this command; this folder
// is located within the default path; the repository, which is created if it doesn't exist, may be a keyword, either USERBOUND or GROUPBOUND, to signal
// the use of a user- or group-dependent repository (a repository whose name contains a reference to a user or a group of users)
//
// note: ABSPATH is the wordpress root directory (defined at the end of wp-config.php)
global $wpfib_options;
global $wpfib_userdata;
global $wpfib_repotype;
global $wpfib_default_dir_chmod;
global $wpfib_imgdirNavigation;
// see if overriding default path (if enabled from the backend)
$default_path_override_enabled = $wpfib_options['default_path_override_enabled'];
// define the absolute path of the repository (excluding the actual repository folder, which might not be set, or be a user- or groupbound one
if ($default_path_override_enabled && strlen($wpfib_options['relpath']))
{
$is_path_relative = true;
$default_abspath = rtrim(ABSPATH, "/\\").DS.trim($wpfib_options['abspath'], "/\\");
}
else if ($default_path_override_enabled && strlen($wpfib_options['abspath']))
{
$is_path_relative = false;
$default_abspath = rtrim($wpfib_options['abspath'], "/\\");
}
else
{
$is_path_relative = $wpfib_options['is_path_relative'];
if ($is_path_relative)
{
$default_abspath = rtrim(ABSPATH, "/\\").DS.trim($wpfib_options['default_path'], "/\\");
}
else
{
$default_abspath = rtrim($wpfib_options['default_path'], "/\\");
}
}
// set the repository name (excluding the path defined above)
$repo_option = trim($wpfib_options['repo'], "/\\");
if (!strlen($repo_option))
{
$repository = "";
$wpfib_repotype = REPOTYPE_UNSPECIFIED;
}
else if (strtoupper($repo_option) == "USERBOUND")
{
$userbound_dir_prefix = strlen($wpfib_options['userbound_dir_prefix']) ? $wpfib_options['userbound_dir_prefix'] : "";
$userbound_dir_suffix = strlen($wpfib_options['userbound_dir_suffix']) ? $wpfib_options['userbound_dir_suffix'] : "";
$userbound_dir_params = is_numeric($wpfib_options['userbound_dir_params']) ? $wpfib_options['userbound_dir_params'] : USERBOUND_PARAMETER_ID_LOGIN;
switch($userbound_dir_params)
{
case USERBOUND_PARAMETER_ID:
$repository = $userbound_dir_prefix.$wpfib_userdata['user_id'].$userbound_dir_suffix;
break;
case USERBOUND_PARAMETER_LOGIN:
$repository = $userbound_dir_prefix.$wpfib_userdata['user_login'].$userbound_dir_suffix;
break;
case USERBOUND_PARAMETER_NAME:
$repository = $userbound_dir_prefix.$wpfib_userdata['user_name'].$userbound_dir_suffix;
break;
case USERBOUND_PARAMETER_ID_LOGIN:
$repository = $userbound_dir_prefix."ID_".$wpfib_userdata['user_id'].".".$wpfib_userdata['user_login'].$userbound_dir_suffix;
break;
case USERBOUND_PARAMETER_ID_NAME:
$repository = $userbound_dir_prefix."ID_".$wpfib_userdata['user_id'].".".$wpfib_userdata['user_name'].$userbound_dir_suffix;
break;
case USERBOUND_PARAMETER_ID_LOGIN_NAME:
$repository = $userbound_dir_prefix."ID_".$wpfib_userdata['user_id'].".".$wpfib_userdata['user_login'].".".$wpfib_userdata['user_name'].$userbound_dir_suffix;
break;
}
$wpfib_repotype = REPOTYPE_USERBOUND;
}
else
{
$repository = $repo_option;
$wpfib_repotype = REPOTYPE_STANDARD;
}
// we now set the starting dir to be the actual initial folder of the repository (in absolute terms)
if ($wpfib_repotype == REPOTYPE_UNSPECIFIED)
{
$repo_abspath = $default_abspath;
}
else
{
$repo_abspath = $default_abspath.DS.$repository;
}
// some debugging lines
if ($wpfib_options['DEBUG_enabled'])
{
echo "<br />Default path (".($is_path_relative ? "RELATIVE" : "ABSOLUTE").") = [".$default_abspath."]<br />";
echo "<br />Repo = [".(strlen($repository) ? $repository : "NOT FOUND")."]<br />";
echo "<br />Repo absolute path = [".$repo_abspath."]<br /><hr />";
}
// sanity check on repo_abspath
if(preg_match("/\.\.(.*)/", $repo_abspath))
{
$error_text = "<div id='JS_MAIN_DIV'><div id='JS_ERROR_DIV'><table><tr>"
."<td class='alertIcon'><img src=\"".$wpfib_imgdirNavigation."warning.png\"></td><td>"
.__('Path to repository should not contain \'..\'')."</td>"
."</tr></table></div><br /></div>";
return false;
}
// attempt to create top level repository dir (if it does not exist)
if (!file_exists($repo_abspath))
{
if (!($rc = @mkdir ($repo_abspath, $wpfib_default_dir_chmod, true))) // we need to use recursive option TRUE
{
$error_text = "<div id='JS_MAIN_DIV'><div id='JS_ERROR_DIV'><table><tr>"
."<td class='alertIcon'><img src=\"".$wpfib_imgdirNavigation."warning.png\"></td><td>"
.sprintf(__('Failed creating repository folder %s'), wpfib_string_encode($repo_abspath))."</td>"
."</tr></table></div><br /></div>";
return false;
}
}
return true;
}
// this function will detect the current location and validate it against the repository path
function wpfib_get_current_location(&$curdir_relpath, $repo_abspath, &$error_text)
{
global $wpfib_options;
// get dir from GET
if(isset($_GET["dir"]) && strlen($_GET["dir"]))
{
$dir = wpfib_path_decode($_GET["dir"]);
}
// get dir from POST (typically from forms)
if(isset($_POST["dir"]) && strlen($_POST["dir"]))
{
// $dir = urldecode($_POST['dir']);
$dir = wpfib_path_decode($_POST['dir']);
}
// validate against illegal formats and set curdir_relpath: this is the path of the current location, relative to the repository path
$dir = trim($dir, "/\\");
if(preg_match("/\.\.(.*)/", $dir))
{
$curdir_relpath = "";
}
else
{
$curdir_relpath = $dir;
}
return true;
}
function wpfib_action_downloadfile($repo_abspath, $curdir_relpath, &$returned_text, &$text_type)
{
$dir = $repo_abspath.DS.$curdir_relpath;
$download_file = wpfib_path_decode($_GET['download_file']);
$download_file_abspath = $dir.DS.$download_file;
// check nonce
check_admin_referer('wpfib-download_file_'.md5($download_file));
// security check
if (preg_match("/\.\.(.*)/", $download_file_abspath))
{
$returned_text = __('File name contains \'..\' - Cannot download it!');
$text_type = ACTION_TEXT_TYPE_ERROR;
return ACTION_NEEDS_NOT_RELOAD;
}
if (file_exists($download_file_abspath))
{
@ob_end_clean();
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"".$download_file."\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($download_file_abspath));
@ob_flush();
@flush();
// standard PHP function readfile() has documented problems with large files; readfile_chunked() is reported on php.net
wpfib_readfile_chunked($download_file_abspath);
die(); // stop execution of further script because we are only outputting the file
// (see readfile() function comment by mark dated 17-Sep-2008 on php.net)
}
else
{
$returned_text = sprintf(__('Error downloading file %s'), wpfib_string_encode($download_file));
$text_type = ACTION_TEXT_TYPE_ERROR;
}
return ACTION_NEEDS_RELOAD;
}
function wpfib_action_delfolder($repo_abspath, $curdir_relpath, &$returned_text, &$text_type)
{
$dir = $repo_abspath.DS.$curdir_relpath;
$delfolder = wpfib_path_decode($_GET['delfolder']);
// check nonce
check_admin_referer('wpfib-delete_folder_'.md5($delfolder));
$delfolder_abspath = $dir.DS.$delfolder;
// security check
if (preg_match("/\.\.(.*)/", $delfolder_abspath))
{
$returned_text = __('Folder name contains \'..\' - Cannot delete it!');
$text_type = ACTION_TEXT_TYPE_ERROR;
return ACTION_NEEDS_NOT_RELOAD;
}
$rc = @rmdir ($delfolder_abspath);
// Check whether directory is gone
if(file_exists($delfolder_abspath))
{
$returned_text = sprintf(__('Error deleting folder %s'), $delfolder); // NOTE: Here we need urldecode as delfolder is double encoded /ErikLtz
$text_type = ACTION_TEXT_TYPE_ERROR;
}
else
{
$returned_text = __('Folder deleted successfully', 'wpfib');
$text_type = ACTION_TEXT_TYPE_SUCCESS;
}
return ACTION_NEEDS_RELOAD;
}
function wpfib_action_delfile($repo_abspath, $curdir_relpath, &$returned_text, &$text_type)
{
global $wpfib_options;
$dir = $repo_abspath.DS.$curdir_relpath;
$delfile = wpfib_path_decode($_GET['delfile']);
// check nonce
check_admin_referer('wpfib-delete_file_'.md5($delfile));
$delfile_abspath = $dir.DS.$delfile;
// security check
if (preg_match("/\.\.(.*)/", $delfile_abspath))
{
$returned_text = __('File name contains \'..\' - Cannot delete it!');
$text_type = ACTION_TEXT_TYPE_ERROR;
return ACTION_NEEDS_NOT_RELOAD;
}
$rc = @unlink ($delfile_abspath);
// try removing thumbnail and thumbs dir (will only work if a thumbnail for this file exists and if the thumbs dir is empty)
$rc_thumbs = @unlink ($dir.DS."JS_THUMBS".DS.$delfile);
$rc_thumbs = @rmdir($dir.DS."JS_THUMBS");
// Check whether directory is gone
if(file_exists($delfile_abspath))
{
$returned_text .= sprintf(__('Error deleting file %s'), $delfile); // NOTE: Here we need urldecode as delfolder is double encoded /ErikLtz
$text_type = ACTION_TEXT_TYPE_ERROR;
}
else
{
$returned_text = __('File deleted successfully', 'wpfib');
$text_type = ACTION_TEXT_TYPE_SUCCESS;
// Note : if this was the last file of an archive wpfib_display_repository() will remove the archive
}
return ACTION_NEEDS_RELOAD;
}
function wpfib_action_renfolder($repo_abspath, $curdir_relpath, &$returned_text, &$text_type)
{
global $wpfib_options;
$dir = $repo_abspath.DS.$curdir_relpath;
$old_foldername = urldecode($_POST['old_foldername']);
$new_foldername = urldecode($_POST['new_foldername']);
// check nonce
check_admin_referer('wpfib-rename_folder_'.$_POST['rowno']);
$new_foldername = preg_replace('/\.\./', '.', $new_foldername); // two dots become one
$new_foldername = preg_replace('/[\\\]+/', '', $new_foldername);
$new_foldername = preg_replace('/[\/]+/', '', $new_foldername);