forked from lesterchan/wp-downloadmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-downloadmanager.php
1548 lines (1435 loc) · 76.1 KB
/
wp-downloadmanager.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: WP-DownloadManager
Plugin URI: http://lesterchan.net/portfolio/programming/php/
Description: Adds a simple download manager to your WordPress blog.
Version: 1.61
Author: Lester 'GaMerZ' Chan
Author URI: http://lesterchan.net
Text Domain: wp-downloadmanager
*/
/*
Copyright 2013 Lester Chan (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public 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 should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
### Create text domain for translations
add_action('init', 'downloadmanager_textdomain');
function downloadmanager_textdomain() {
load_plugin_textdomain('wp-downloadmanager', false, 'wp-downloadmanager');
}
### Downloads Table Name
global $wpdb;
$wpdb->downloads = $wpdb->prefix.'downloads';
### Function: Downloads Administration Menu
add_action('admin_menu', 'downloads_menu');
function downloads_menu() {
if (function_exists('add_menu_page')) {
add_menu_page(__('Downloads', 'wp-downloadmanager'), __('Downloads', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-manager.php', '', plugins_url('wp-downloadmanager/images/drive.png'));
}
if (function_exists('add_submenu_page')) {
add_submenu_page('wp-downloadmanager/download-manager.php', __('Manage Downloads', 'wp-downloadmanager'), __('Manage Downloads', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-manager.php');
add_submenu_page('wp-downloadmanager/download-manager.php', __('Add File', 'wp-downloadmanager'), __('Add File', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-add.php');
add_submenu_page('wp-downloadmanager/download-manager.php', __('Download Options', 'wp-downloadmanager'), __('Download Options', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-options.php');
add_submenu_page('wp-downloadmanager/download-manager.php', __('Download Templates', 'wp-downloadmanager'), __('Download Templates', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-templates.php');
add_submenu_page('wp-downloadmanager/download-manager.php', __('Uninstall WP-DownloadManager', 'wp-downloadmanager'), __('Uninstall WP-DownloadManager', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-uninstall.php');
}
}
### Function: Enqueue Downloads Stylesheets
add_action('wp_print_styles', 'downloads_stylesheets');
function downloads_stylesheets() {
if(@file_exists(TEMPLATEPATH.'/download-css.css')) {
wp_enqueue_style('wp-downloadmanager', get_stylesheet_directory_uri().'/download-css.css', false, '1.50', 'all');
} else {
wp_enqueue_style('wp-downloadmanager', plugins_url('wp-downloadmanager/download-css.css'), false, '1.50', 'all');
}
}
### Function: Enqueue Downloads Stylesheets In WP-Admin
add_action('admin_enqueue_scripts', 'downloads_stylesheets_admin');
function downloads_stylesheets_admin($hook_suffix) {
$downloads_admin_pages = array('wp-downloadmanager/download-manager.php', 'wp-downloadmanager/download-add.php', 'wp-downloadmanager/download-options.php', 'wp-downloadmanager/download-templates.php', 'wp-downloadmanager/download-uninstall.php');
if(in_array($hook_suffix, $downloads_admin_pages)) {
wp_enqueue_style('wp-downloadmanager-admin', plugins_url('wp-downloadmanager/download-admin-css.css'), false, '1.50', 'all');
}
}
### Function: Displays Download Manager Footer In WP-Admin
add_action('admin_footer-post-new.php', 'downloads_footer_admin');
add_action('admin_footer-post.php', 'downloads_footer_admin');
add_action('admin_footer-page-new.php', 'downloads_footer_admin');
add_action('admin_footer-page.php', 'downloads_footer_admin');
function downloads_footer_admin() {
// Javascript Code Courtesy Of WP-AddQuicktag (http://bueltge.de/wp-addquicktags-de-plugin/120/)
echo '<script type="text/javascript">'."\n";
echo "\t".'var downloadsEdL10n = {'."\n";
echo "\t\t".'enter_download_id: "'.js_escape(__('Enter File ID (Separate Multiple IDs By A Comma)', 'wp-downloadmanager')).'",'."\n";
echo "\t\t".'download: "'.js_escape(__('Download', 'wp-downloadmanager')).'",'."\n";
echo "\t\t".'insert_download: "'.js_escape(__('Insert File Download', 'wp-downloadmanager')).'",'."\n";
echo "\t".'};'."\n";
echo "\t".'function insertDownload(where, myField) {'."\n";
echo "\t\t".'var download_id = jQuery.trim(prompt(downloadsEdL10n.enter_download_id));'."\n";
echo "\t\t".'if(download_id == null || download_id == "") {'."\n";
echo "\t\t\t".'return;'."\n";
echo "\t\t".'} else {'."\n";
echo "\t\t\t".'if(where == "code") {'."\n";
echo "\t\t\t\t".'edInsertContent(myField, "[download id=\"" + download_id + "\"]");'."\n";
echo "\t\t\t".'} else {'."\n";
echo "\t\t\t\t".'return "[download id=\"" + download_id + "\"]";'."\n";
echo "\t\t\t".'}'."\n";
echo "\t\t".'}'."\n";
echo "\t".'}'."\n";
echo "\t".'if(document.getElementById("ed_toolbar")){'."\n";
echo "\t\t".'edButtons[edButtons.length] = new edButton("ed_downloadmanager",downloadsEdL10n.download, "", "","");'."\n";
echo "\t\t".'jQuery(document).ready(function($){'."\n";
echo "\t\t\t".'$(\'#qt_content_ed_downloadmanager\').replaceWith(\'<input type="button" id="qt_content_ed_downloadmanager" accesskey="" class="ed_button" onclick="insertDownload(\\\'code\\\', edCanvas);" value="\' + downloadsEdL10n.download + \'" title="\' + downloadsEdL10n.insert_download + \'" />\');'."\n";
echo "\t\t".'});'."\n";
echo "\t".'}'."\n";
echo '</script>'."\n";
}
### Function: Add Favourite Actions >= WordPress 2.7
add_filter('favorite_actions', 'downloads_favorite_actions');
function downloads_favorite_actions($favorite_actions) {
$favorite_actions['admin.php?page=wp-downloadmanager/download-add.php'] = array(__('Add File', 'wp-downloadmanager'), 'manage_downloads');
return $favorite_actions;
}
### Function: Add Quick Tag For Poll In TinyMCE >= WordPress 2.5
add_action('init', 'download_tinymce_addbuttons');
function download_tinymce_addbuttons() {
if(!current_user_can('edit_posts') && ! current_user_can('edit_pages')) {
return;
}
if(get_user_option('rich_editing') == 'true') {
add_filter("mce_external_plugins", "download_tinymce_addplugin");
add_filter('mce_buttons', 'download_tinymce_registerbutton');
}
}
function download_tinymce_registerbutton($buttons) {
array_push($buttons, 'separator', 'downloadmanager');
return $buttons;
}
function download_tinymce_addplugin($plugin_array) {
$plugin_array['downloadmanager'] = plugins_url('wp-downloadmanager/tinymce/plugins/downloadmanager/editor_plugin.js');
return $plugin_array;
}
### Function: Add Download Query Vars
add_filter('query_vars', 'download_query_vars');
function download_query_vars($public_query_vars) {
$public_query_vars[] = "dl_id";
$public_query_vars[] = "dl_name";
return $public_query_vars;
}
### Function: Download htaccess ReWrite Rules
add_filter('generate_rewrite_rules', 'download_rewrite');
function download_rewrite($wp_rewrite) {
$wp_rewrite->rules = array_merge(array('download/([0-9]{1,})/?$' => 'index.php?dl_id=$matches[1]', 'download/(.*)$' => 'index.php?dl_name=$matches[1]'), $wp_rewrite->rules);
}
### Function: Add Download RSS Link To Download Page
add_action('wp_head', 'download_rss_link');
function download_rss_link() {
if(is_page() && strpos(get_option('download_page_url'), $_SERVER['REQUEST_URI'])) {
$download_nice_permalink = intval(get_option('download_nice_permalink'));
if($download_nice_permalink == 1) {
$download_rss_link = get_option('home').'/download/rss/';
} else {
$download_rss_link = get_option('home').'/?dl_name=rss';
}
echo '<link rel="alternate" type="application/rss+xml" title="'.get_bloginfo_rss('name').__(' Downloads RSS Feed', 'wp-downloadmanager').'" href="'.$download_rss_link.'" />'."\n";
}
}
### Function: Download File
add_action('template_redirect', 'download_file', 5);
function download_file() {
global $wpdb, $user_ID;
$dl_id = intval(get_query_var('dl_id'));
$dl_name = addslashes(get_query_var('dl_name'));
$download_options = get_option('download_options');
if($dl_name == 'rss') {
load_template(WP_PLUGIN_DIR.'/wp-downloadmanager/download-rss.php');
exit;
}
if($dl_id > 0 || !empty($dl_name)) {
if($dl_id > 0 && $download_options['use_filename'] == 0) {
$file = $wpdb->get_row("SELECT file_id, file, file_permission FROM $wpdb->downloads WHERE file_id = $dl_id AND file_permission != -2");
} elseif(!empty($dl_name) && $download_options['use_filename'] == 1) {
if(!is_remote_file($dl_name)) {
$dl_name = '/'.$dl_name;
}
$file = $wpdb->get_row("SELECT file_id, file, file_permission FROM $wpdb->downloads WHERE file = \"$dl_name\" AND file_permission != -2");
}
if(!$file) {
header('HTTP/1.0 404 Not Found');
die(__('Invalid File ID or File Name.', 'wp-downloadmanager'));
}
$file_path = stripslashes(get_option('download_path'));
$file_url = stripslashes(get_option('download_path_url'));
$download_method = intval(get_option('download_method'));
$file_id = intval($file->file_id);
$file_name = stripslashes($file->file);
$file_permission = intval($file->file_permission);
$current_user = wp_get_current_user();
if(($file_permission > 0 && intval($current_user->wp_user_level) >= $file_permission && intval($user_ID) > 0) || ($file_permission == 0 && intval($user_ID) > 0) || $file_permission == -1) {
$update_hits = $wpdb->query("UPDATE $wpdb->downloads SET file_hits = (file_hits + 1), file_last_downloaded_date = '".current_time('timestamp')."' WHERE file_id = $file_id AND file_permission != -2");
if(!is_remote_file($file_name)) {
if(!is_file($file_path.$file_name)) {
header('HTTP/1.0 404 Not Found');
die(__('File does not exist.', 'wp-downloadmanager'));
}
if($download_method == 0) {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file_name).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file_path.$file_name));
@readfile($file_path.$file_name);
} else {
header('Location: '.$file_url.$file_name);
}
exit();
} else {
if(ini_get('allow_url_fopen') && $download_method == 0) {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file_name).";");
header("Content-Transfer-Encoding: binary");
$file_size = remote_filesize($file_name);
if($file_size != __('unknown', 'wp-downloadmanager')) {
header("Content-Length: ".$file_size);
}
@readfile($file_name);
} else {
header('Location: '.$file_name);
}
exit();
}
} else {
_e('You do not have permission to download this file.', 'wp-downloadmanager');
exit();
}
}
}
### Function: Print Out File Extension Image
function file_extension_image($file_name, $file_ext_images) {
$file_ext = file_extension($file_name);
$file_ext .= '.gif';
if(in_array($file_ext, $file_ext_images)) {
return $file_ext;
} else {
return 'unknown.gif';
}
}
### Function: Get File Extension Images
function file_extension_images() {
$file_ext_images = array();
$dir = WP_PLUGIN_DIR.'/wp-downloadmanager/images/ext';
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file != '.' && $file != '..') {
$file_ext_images[] = $file;
}
}
closedir($dh);
}
}
return $file_ext_images;
}
### Function: Get File Extension
if(!function_exists('file_extension')) {
function file_extension($filename) {
$file_ext = explode('.', $filename);
$file_ext = $file_ext[sizeof($file_ext)-1];
$file_ext = strtolower($file_ext);
return $file_ext;
}
}
### Function: Get Remote File Size
if(!function_exists('remote_filesize')) {
function remote_filesize($uri) {
$header_array = @get_headers($uri, 1);
$file_size = $header_array['Content-Length'];
if(!empty($file_size)) {
return $file_size;
} else {
return __('unknown', 'wp-downloadmanager');
}
}
}
### Function: Format Bytes Into TiB/GiB/MiB/KiB/Bytes
if(!function_exists('format_filesize')) {
function format_filesize($rawSize) {
if($rawSize / 1099511627776 > 1) {
return number_format_i18n($rawSize/1099511627776, 1).' '.__('TiB', 'wp-downloadmanager');
} elseif($rawSize / 1073741824 > 1) {
return number_format_i18n($rawSize/1073741824, 1).' '.__('GiB', 'wp-downloadmanager');
} elseif($rawSize / 1048576 > 1) {
return number_format_i18n($rawSize/1048576, 1).' '.__('MiB', 'wp-downloadmanager');
} elseif($rawSize / 1024 > 1) {
return number_format_i18n($rawSize/1024, 1).' '.__('KiB', 'wp-downloadmanager');
} elseif($rawSize > 1) {
return number_format_i18n($rawSize, 0).' '.__('bytes', 'wp-downloadmanager');
} else {
return __('unknown', 'wp-downloadmanager');
}
}
}
### Function: Format Bytes Into TB/GB/MB/KB/Bytes
if(!function_exists('format_filesize_dec')) {
function format_filesize_dec($rawSize) {
if($rawSize / 1000000000000 > 1) {
return number_format_i18n($rawSize/1000000000000, 1).' '.__('TB', 'wp-downloadmanager');
} elseif($rawSize / 1000000000 > 1) {
return number_format_i18n($rawSize/1000000000, 1).' '.__('GB', 'wp-downloadmanager');
} elseif($rawSize / 1000000 > 1) {
return number_format_i18n($rawSize/1000000, 1).' '.__('MB', 'wp-downloadmanager');
} elseif($rawSize / 1000 > 1) {
return number_format_i18n($rawSize/1000, 1).' '.__('KB', 'wp-downloadmanager');
} elseif($rawSize > 1) {
return number_format_i18n($rawSize, 0).' '.__('bytes', 'wp-downloadmanager');
} else {
return __('unknown', 'wp-downloadmanager');
}
}
}
### Function: Get Max File Size That Can Be Uploaded
function get_max_upload_size() {
$maxsize = ini_get('upload_max_filesize');
if (!is_numeric($maxsize)) {
if (strpos($maxsize, 'M') !== false) {
$maxsize = intval($maxsize)*1024*1024;
} elseif (strpos($maxsize, 'K') !== false) {
$maxsize = intval($maxsize)*1024;
} elseif (strpos($maxsize, 'G') !== false) {
$maxsize = intval($maxsize)*1024*1024*1024;
}
}
return $maxsize;
}
### Function: Is Remote File
function is_remote_file($file_name) {
if(strpos($file_name, 'http://') === false && strpos($file_name, 'https://') === false && strpos($file_name, 'ftp://') === false) {
return false;
}
return true;
}
### Function: Snippet Text
if(!function_exists('snippet_text')) {
function snippet_text($text, $length = 0) {
if (defined('MB_OVERLOAD_STRING')) {
$text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
if (mb_strlen($text) > $length) {
return htmlentities(mb_substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
} else {
return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
}
} else {
$text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
if (strlen($text) > $length) {
return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
} else {
return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
}
}
}
}
### Function: Download URL
function download_file_url($file_id, $file_name) {
$file_id = intval($file_id);
$file_name = stripslashes($file_name);
if(!is_remote_file($file_name)) {
$file_name = substr($file_name, 1);
}
$download_options = get_option('download_options');
$download_use_filename = intval($download_options['use_filename']);
$download_nice_permalink = intval(get_option('download_nice_permalink'));
if($download_nice_permalink == 1) {
if($download_use_filename == 1) {
$download_file_url = get_option('home').'/download/'.$file_name;
} else {
$download_file_url = get_option('home').'/download/'.$file_id.'/';
}
} else {
if($download_use_filename == 1) {
$download_file_url = get_option('home').'/?dl_name='.$file_name;
} else {
$download_file_url = get_option('home').'/?dl_id='.$file_id;
}
}
return $download_file_url;
}
### Function: Download Category URL
function download_category_url($cat_id) {
$download_page_url = get_option('download_page_url');
if(strpos($download_page_url, '?') !== false) {
$download_page_url = "$download_page_url&dl_cat=$cat_id";
} else {
$download_page_url = "$download_page_url?dl_cat=$cat_id";
}
return $download_page_url;
}
### Function: Download Page Link
function download_page_link($page) {
$current_url = $_SERVER['REQUEST_URI'];
$curren_downloadpage = intval($_GET['dl_page']);
$download_page_link = preg_replace('/dl_page=(\d+)/i', 'dl_page='.$page, $current_url);
if($curren_downloadpage == 0) {
if(strpos($current_url, '?') !== false) {
$download_page_link = "$download_page_link&dl_page=$page";
} else {
$download_page_link = "$download_page_link?dl_page=$page";
}
}
return $download_page_link;
}
### Function Highlight Download Search
function download_search_highlight($search_word, $search_text) {
if(!empty($search_word)) {
$search_words_array = explode(' ', $search_word);
foreach($search_words_array as $search_word_array) {
$search_text = eregi_replace('('.quotemeta($search_word_array).')', '<span class="download-search-highlight">\\1</span>', $search_text);
}
}
return $search_text;
}
### Function: Short Code For Inserting Downloads Page Into Page
add_shortcode('page_download', 'download_page_shortcode');
add_shortcode('page_downloads', 'download_page_shortcode');
function download_page_shortcode($atts) {
extract(shortcode_atts(array('category' => 0), $atts));
return downloads_page($category);
}
### Function: Short Code For Inserting Files Download Into Posts
add_shortcode('download', 'download_shortcode');
function download_shortcode($atts) {
extract(shortcode_atts(array('id' => 0, 'category' => 0, 'display' => 'both', 'sort_by' => 'file_id', 'sort_order' => 'asc', 'stream_limit' => 0), $atts));
if(!is_feed()) {
$conditions = array();
if($id != 0) {
if(strpos($id, ',') !== false) {
$conditions[] = "file_id IN ($id)";
} else {
$conditions[] = "file_id = $id";
}
}
if($category != 0) {
if(strpos($category, ',') !== false) {
$conditions[] = "file_category IN ($category)";
} else {
$conditions[] = "file_category = $category";
}
}
if($conditions) {
return download_embedded(implode(' AND ', $conditions), $display, $sort_by, $sort_order, $stream_limit);
}
else {
return '';
}
} else {
return __('Note: There is a file embedded within this post, please visit this post to download the file.', 'wp-downloadmanager');
}
}
### Function: Downloads Page
function downloads_page($category_id = 0) {
global $wpdb, $user_ID;
// Variables
$category_id = intval($category_id);
$category = intval($_GET['dl_cat']);
$page = intval($_GET['dl_page']);
$search_word = strip_tags(addslashes(trim($_GET['dl_search'])));
$search_words_array = array();
$search = stripslashes($search_word);
$download_categories = get_option('download_categories');
$download_categories[0] = __('total', 'wp-downloadmanager');
$category_stats = array();
$total_stats = array('files' => 0, 'size' => 0, 'hits' => 0);
$file_sort = get_option('download_sort');
$file_extensions_images = file_extension_images();
$current_user = wp_get_current_user();
// If There Is Category Set
$category_sql = '';
if($category == 0 && $category_id > 0) {
$category = $category_id;
}
if($category > 0) {
$category_sql = "AND file_category = $category";
}
// If There Is A Search Term
$search_sql = '';
if(!empty($search)) {
$search_words_array = explode(' ', $search_word);
foreach($search_words_array as $search_word_array) {
$search_sql .= " AND ((file_name LIKE('%$search_word_array%') OR file_des LIKE ('%$search_word_array%')))";
}
}
// Calculate Categories And Total Stats
$categories = $wpdb->get_results("SELECT file_category, COUNT(file_id) as category_files, SUM(file_size) category_size, SUM(file_hits) as category_hits FROM $wpdb->downloads WHERE 1=1 $category_sql $search_sql AND file_permission != -2 GROUP BY file_category");
if($categories) {
foreach($categories as $cat) {
$cat_id = intval($cat->file_category);
$category_stats[$cat_id]['files'] = $cat->category_files;
$category_stats[$cat_id]['hits'] = $cat->category_hits;
$category_stats[$cat_id]['size'] = $cat->category_size;
$total_stats['files'] +=$cat->category_files;
$total_stats['hits'] += $cat->category_hits;
$total_stats['size'] += $cat->category_size;
}
}
// Calculate Paging
$numposts = $total_stats['files'];
$perpage = $file_sort['perpage'];
$max_page = ceil($numposts/$perpage);
if(empty($page) || $page == 0) {
$page = 1;
}
$offset = ($page-1) * $perpage;
$pages_to_show = 10;
$pages_to_show_minus_1 = $pages_to_show-1;
$half_page_start = floor($pages_to_show_minus_1/2);
$half_page_end = ceil($pages_to_show_minus_1/2);
$start_page = $page - $half_page_start;
if($start_page <= 0) {
$start_page = 1;
}
$end_page = $page + $half_page_end;
if(($end_page - $start_page) != $pages_to_show_minus_1) {
$end_page = $start_page + $pages_to_show_minus_1;
}
if($end_page > $max_page) {
$start_page = $max_page - $pages_to_show_minus_1;
$end_page = $max_page;
}
if($start_page <= 0) {
$start_page = 1;
}
if(($offset + $perpage) > $numposts) {
$max_on_page = $numposts;
} else {
$max_on_page = ($offset + $perpage);
}
if (($offset + 1) > ($numposts)) {
$display_on_page = $numposts;
} else {
$display_on_page = ($offset + 1);
}
// Get Sorting Group
$group_sql = '';
if($file_sort['group'] == 1) {
$group_sql = 'file_category ASC,';
}
// Get Files
$files = $wpdb->get_results("SELECT * FROM $wpdb->downloads WHERE 1=1 $category_sql $search_sql AND file_permission != -2 ORDER BY $group_sql {$file_sort['by']} {$file_sort['order']} LIMIT $offset, {$file_sort['perpage']}");
if($files) {
// Get Download Page Header
$template_download_header = stripslashes(get_option('download_template_header'));
if(get_option('download_nice_permalink') == '0' && preg_match('/[\?\&]page_id=(\d+)/i', get_option('download_page_url'), $matches)) {
$template_download_header = preg_replace('/(<form[^>]+>)/i', '$1<input type="hidden" name="page_id" value="'.$matches[1].'" />', $template_download_header);
}
$template_download_header = str_replace("%TOTAL_FILES_COUNT%", number_format_i18n($total_stats['files']), $template_download_header);
$template_download_header = str_replace("%TOTAL_HITS%", number_format_i18n($total_stats['hits']), $template_download_header);
$template_download_header = str_replace("%TOTAL_SIZE%", format_filesize($total_stats['size']), $template_download_header);
$template_download_header = str_replace("%TOTAL_SIZE_DEC%", format_filesize_dec($total_stats['size']), $template_download_header);
$template_download_header = str_replace("%RECORD_START%", number_format_i18n($display_on_page), $template_download_header);
$template_download_header = str_replace("%RECORD_END%", number_format_i18n($max_on_page), $template_download_header);
$template_download_header = str_replace("%CATEGORY_ID%", $category, $template_download_header);
$template_download_header = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[$category]), $template_download_header);
$template_download_header = str_replace("%FILE_SEARCH_WORD%", $search, $template_download_header);
$template_download_header = str_replace("%DOWNLOAD_PAGE_URL%", get_option('download_page_url'), $template_download_header);
$output = $template_download_header;
// Loop Through Files
$i = 1;
$k = 1;
$temp_cat_id = -1;
$need_footer = 0;
foreach($files as $file) {
$cat_id = intval($file->file_category);
// Print Out Category Footer
if($need_footer && $temp_cat_id != $cat_id && $file_sort['group'] == 1) {
// Get Download Category Footer
$template_download_category_footer = stripslashes(get_option('download_template_category_footer'));
$template_download_category_footer = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[$cat_id]), $template_download_category_footer);
$template_download_category_footer = str_replace("%CATEGORY_ID%", $cat_id, $template_download_category_footer);
$template_download_category_footer = str_replace("%CATEGORY_URL%", download_category_url($cat_id), $template_download_category_footer);
$template_download_category_footer = str_replace("%CATEGORY_FILES_COUNT%", number_format_i18n($category_stats[$cat_id]['files']), $template_download_category_footer);
$template_download_category_footer = str_replace("%CATEGORY_HITS%", number_format_i18n($category_stats[$cat_id]['hits']), $template_download_category_footer);
$template_download_category_footer = str_replace("%CATEGORY_SIZE%", format_filesize($category_stats[$cat_id]['size']), $template_download_category_footer);
$template_download_category_footer = str_replace("%CATEGORY_SIZE_DEC%", format_filesize_dec($category_stats[$cat_id]['size']), $template_download_category_footer);
$output .= $template_download_category_footer;
$need_footer = 0;
}
// Print Out Category Header
if($temp_cat_id != $cat_id && $file_sort['group'] == 1) {
// Get Download Category Header
$template_download_category_header = stripslashes(get_option('download_template_category_header'));
$template_download_category_header = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[$cat_id]), $template_download_category_header);
$template_download_category_header = str_replace("%CATEGORY_ID%", $cat_id, $template_download_category_header);
$template_download_category_header = str_replace("%CATEGORY_URL%", download_category_url($cat_id), $template_download_category_header);
$template_download_category_header = str_replace("%CATEGORY_FILES_COUNT%", number_format_i18n($category_stats[$cat_id]['files']), $template_download_category_header);
$template_download_category_header = str_replace("%CATEGORY_HITS%", number_format_i18n($category_stats[$cat_id]['hits']), $template_download_category_header);
$template_download_category_header = str_replace("%CATEGORY_SIZE%", format_filesize($category_stats[$cat_id]['size']), $template_download_category_header);
$template_download_category_header = str_replace("%CATEGORY_SIZE_DEC%", format_filesize_dec($category_stats[$cat_id]['size']), $template_download_category_header);
$output .= $template_download_category_header;
$i = 1;
$need_footer = 1;
}
// Get Download Listing
$file_permission = intval($file->file_permission);
$template_download_listing = get_option('download_template_listing');
if(($file_permission > 0 && intval($current_user->wp_user_level) >= $file_permission && intval($user_ID) > 0) || ($file_permission == 0 && intval($user_ID) > 0) || $file_permission == -1) {
$template_download_listing = stripslashes($template_download_listing[0]);
} else {
$template_download_listing = stripslashes($template_download_listing[1]);
}
$template_download_listing = str_replace("%FILE_ID%", $file->file_id, $template_download_listing);
$template_download_listing = str_replace("%FILE%", stripslashes($file->file), $template_download_listing);
$template_download_listing = str_replace("%FILE_NAME%", download_search_highlight($search, stripslashes($file->file_name)), $template_download_listing);
$template_download_listing = str_replace("%FILE_ICON%", file_extension_image(stripslashes($file->file), $file_extensions_images), $template_download_listing);
$template_download_listing = str_replace("%FILE_DESCRIPTION%", download_search_highlight($search, stripslashes($file->file_des)), $template_download_listing);
$template_download_listing = str_replace("%FILE_SIZE%", format_filesize($file->file_size), $template_download_listing);
$template_download_listing = str_replace("%FILE_SIZE_DEC%", format_filesize_dec($file->file_size), $template_download_listing);
$template_download_listing = str_replace("%FILE_CATEGORY_ID%", $cat_id, $template_download_listing);
$template_download_listing = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[$cat_id]), $template_download_listing);
$template_download_listing = str_replace("%FILE_DATE%", mysql2date(get_option('date_format'), gmdate('Y-m-d H:i:s', $file->file_date)), $template_download_listing);
$template_download_listing = str_replace("%FILE_TIME%", mysql2date(get_option('time_format'), gmdate('Y-m-d H:i:s', $file->file_date)), $template_download_listing);
$template_download_listing = str_replace("%FILE_UPDATED_DATE%", mysql2date(get_option('date_format'), gmdate('Y-m-d H:i:s', $file->file_updated_date)), $template_download_listing);
$template_download_listing = str_replace("%FILE_UPDATED_TIME%", mysql2date(get_option('time_format'), gmdate('Y-m-d H:i:s', $file->file_updated_date)), $template_download_listing);
$template_download_listing = str_replace("%FILE_HITS%", number_format_i18n($file->file_hits), $template_download_listing);
$template_download_listing = str_replace("%FILE_DOWNLOAD_URL%", download_file_url($file->file_id, $file->file), $template_download_listing);
$output .= $template_download_listing;
// Assign Cat ID To Temp Cat ID
$temp_cat_id = $cat_id;
// Count Files
$i++;
$k++;
}
// Print Out Category Footer
if($need_footer) {
// Get Download Category Footer
$template_download_category_footer = stripslashes(get_option('download_template_category_footer'));
$template_download_category_footer = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[$cat_id]), $template_download_category_footer);
$template_download_category_footer = str_replace("%CATEGORY_ID%", $cat_id, $template_download_category_footer);
$template_download_category_footer = str_replace("%CATEGORY_URL%", download_category_url($cat_id), $template_download_category_footer);
$template_download_category_footer = str_replace("%CATEGORY_FILES_COUNT%", number_format_i18n($category_stats[$cat_id]['files']), $template_download_category_footer);
$template_download_category_footer = str_replace("%CATEGORY_HITS%", number_format_i18n($category_stats[$cat_id]['hits']), $template_download_category_footer);
$template_download_category_footer = str_replace("%CATEGORY_SIZE%", format_filesize($category_stats[$cat_id]['size']), $template_download_category_footer);
$template_download_category_footer = str_replace("%CATEGORY_SIZE_DEC%", format_filesize_dec($category_stats[$cat_id]['size']), $template_download_category_footer);
$output .= $template_download_category_footer;
$need_footer = 0;
}
// Get Download Page Footer
$template_download_footer = stripslashes(get_option('download_template_footer'));
if(get_option('download_nice_permalink') == '0' && preg_match('/[\?\&]page_id=(\d+)/i', get_option('download_page_url'), $matches)) {
$template_download_footer = preg_replace('/(<form[^>]+>)/i', '$1<input type="hidden" name="page_id" value="'.$matches[1].'" />', $template_download_footer);
}
$template_download_footer = str_replace("%TOTAL_FILES_COUNT%", number_format_i18n($total_stats['files']), $template_download_footer);
$template_download_footer = str_replace("%TOTAL_HITS%", number_format_i18n($total_stats['hits']), $template_download_footer);
$template_download_footer = str_replace("%TOTAL_SIZE%", format_filesize($total_stats['size']), $template_download_footer);
$template_download_footer = str_replace("%TOTAL_SIZE_DEC%", format_filesize_dec($total_stats['size']), $template_download_footer);
$template_download_footer = str_replace("%CATEGORY_ID%", $category, $template_download_footer);
$template_download_footer = str_replace("%FILE_CATEGORY_NAME%", stripslashes($download_categories[$category]), $template_download_footer);
$template_download_footer = str_replace("%FILE_SEARCH_WORD%", $search, $template_download_footer);
$template_download_footer = str_replace("%DOWNLOAD_PAGE_URL%", get_option('download_page_url'), $template_download_footer);
$output .= $template_download_footer;
} else {
$template_download_none = stripslashes(get_option('download_template_none'));
$output .= $template_download_none;
}
// Download Paging
if($max_page > 1) {
$output .= stripslashes(get_option('download_template_pagingheader'));
if(function_exists('wp_pagenavi')) {
$output .= '<div class="wp-pagenavi">'."\n";
} else {
$output .= '<div class="wp-downloadmanager-paging">'."\n";
}
$output .= '<span class="pages"> '.sprintf(__('Page %s of %s', 'wp-downloadmanager'), number_format_i18n($page), number_format_i18n($max_page)).' </span>';
if ($start_page >= 2 && $pages_to_show < $max_page) {
$output .= '<a href="'.download_page_link(1).'" title="'.__('« First', 'wp-downloadmanager').'"> '.__('« First', 'wp-downloadmanager').' </a>';
$output .= '<span class="extend">...</span>';
}
if($page > 1) {
$output .= '<a href="'.download_page_link(($page-1)).'" title="'.__('«', 'wp-downloadmanager').'"> '.__('«', 'wp-downloadmanager').' </a>';
}
for($i = $start_page; $i <= $end_page; $i++) {
if($i == $page) {
$output .= '<span class="current"> '.number_format_i18n($i).' </span>';
} else {
$output .= '<a href="'.download_page_link($i).'" title="'.number_format_i18n($i).'"> '.number_format_i18n($i).' </a>';
}
}
if(empty($page) || ($page+1) <= $max_page) {
$output .= '<a href="'.download_page_link(($page+1)).'" title="'.__('»', 'wp-downloadmanager').'"> '.__('»', 'wp-downloadmanager').' </a>';
}
if ($end_page < $max_page) {
$output .= '<span class="extend">...</span>';
$output .= '<a href="'.download_page_link($max_page).'" title="'.__('Last »', 'wp-downloadmanager').'"> '.__('Last »', 'wp-downloadmanager').' </a>';
}
$output .= '</div>';
$output .= stripslashes(get_option('download_template_pagingfooter'));
}
return apply_filters('downloads_page', $output);
}
### Function: List Out All Files In Downloads Directory
function list_downloads_files($dir, $orginal_dir) {
global $download_files, $download_files_subfolder;
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file != '.' && $file != '..' && $file != '.htaccess') {
if(is_dir($dir.'/'.$file)) {
list_downloads_files($dir.'/'.$file, $orginal_dir);
} else {
$folder_file =str_replace($orginal_dir, '', $dir.'/'.$file);
$sub_dir = explode('/', $folder_file);
if(sizeof($sub_dir) > 2) {
$download_files_subfolder[] = $folder_file;
} else {
$download_files[] = $folder_file;
}
}
}
}
closedir($dh);
}
}
}
### Function: List Out All Files In Downloads Directory
function list_downloads_folders($dir, $orginal_dir) {
global $download_folders;
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file != '.' && $file != '..') {
if(is_dir($dir.'/'.$file)) {
$folder =str_replace($orginal_dir, '', $dir.'/'.$file);
$download_folders[] = $folder;
list_downloads_folders($dir.'/'.$file, $orginal_dir);
}
}
}
closedir($dh);
}
}
}
### Function: Print Listing Of Files In Alphabetical Order
function print_list_files($dir, $orginal_dir, $selected = '') {
global $download_files, $download_files_subfolder;
list_downloads_files($dir, $orginal_dir);
if($download_files) {
natcasesort($download_files);
}
if($download_files_subfolder) {
natcasesort($download_files_subfolder);
}
if($download_files) {
foreach($download_files as $download_file) {
if($download_file == $selected) {
echo '<option value="'.$download_file.'" selected="selected">'.$download_file.'</option>'."\n";
} else {
echo '<option value="'.$download_file.'">'.$download_file.'</option>'."\n";
}
}
}
if($download_files_subfolder) {
foreach($download_files_subfolder as $download_file_subfolder) {
if($download_file_subfolder == $selected) {
echo '<option value="'.$download_file_subfolder.'" selected="selected">'.$download_file_subfolder.'</option>'."\n";
} else {
echo '<option value="'.$download_file_subfolder.'">'.$download_file_subfolder.'</option>'."\n";
}
}
}
}
### Function: Print Listing Of Folders In Alphabetical Order
function print_list_folders($dir, $orginal_dir) {
global $download_folders;
list_downloads_folders($dir, $orginal_dir);
if($download_folders) {
natcasesort($download_folders);
echo '<option value="/">/</option>'."\n";
foreach($download_folders as $download_folder) {
echo '<option value="'.$download_folder.'">'.$download_folder.'</option>'."\n";
}
}
}
### Function: Rename File To Ensure (Credits: imvain2)
function download_rename_file($file_path, $file) {
$rename = false;
$file_old = $file;
$file = str_replace(' ', '_', $file);
$file = preg_replace('/[^A-Za-z0-9\-._\/]/', '', $file);
if($file != $file_old) {
$rename = rename($file_path.$file_old, $file_path.$file);
}
if($rename) {
return $file;
} else {
return $file_old;
}
}
### Function: Editable Timestamp
function file_timestamp($file_timestamp) {
global $month;
$day = gmdate('j', $file_timestamp);
echo '<select id="file_timestamp_day" name="file_timestamp_day" size="1">'."\n";
for($i = 1; $i <=31; $i++) {
if($day == $i) {
echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
} else {
echo "<option value=\"$i\">$i</option>\n";
}
}
echo '</select> '."\n";
$month2 = gmdate('n', $file_timestamp);
echo '<select id="file_timestamp_month" name="file_timestamp_month" size="1">'."\n";
for($i = 1; $i <= 12; $i++) {
if ($i < 10) {
$ii = '0'.$i;
} else {
$ii = $i;
}
if($month2 == $i) {
echo "<option value=\"$i\" selected=\"selected\">$month[$ii]</option>\n";
} else {
echo "<option value=\"$i\">$month[$ii]</option>\n";
}
}
echo '</select> '."\n";
$year = gmdate('Y', $file_timestamp);
echo '<select id="file_timestamp_year" name="file_timestamp_year" size="1">'."\n";
for($i = 2000; $i <= gmdate('Y'); $i++) {
if($year == $i) {
echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
} else {
echo "<option value=\"$i\">$i</option>\n";
}
}
echo '</select> @'."\n";
echo '<span dir="ltr">'."\n";
$hour = gmdate('H', $file_timestamp);
echo '<select id="file_timestamp_hour" name="file_timestamp_hour" size="1">'."\n";
for($i = 0; $i < 24; $i++) {
if($hour == $i) {
echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
} else {
echo "<option value=\"$i\">$i</option>\n";
}
}
echo '</select> :'."\n";
$minute = gmdate('i', $file_timestamp);
echo '<select id="file_timestamp_minute" name="file_timestamp_minute" size="1">'."\n";
for($i = 0; $i < 60; $i++) {
if($minute == $i) {
echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
} else {
echo "<option value=\"$i\">$i</option>\n";
}
}
echo '</select> :'."\n";
$second = gmdate('s', $file_timestamp);
echo '<select id="file_timestamp_second" name="file_timestamp_second" size="1">'."\n";
for($i = 0; $i <= 60; $i++) {
if($second == $i) {
echo "<option value=\"$i\" selected=\"selected\">$i</option>\n";
} else {
echo "<option value=\"$i\">$i</option>\n";
}
}
echo '</select>'."\n";
echo '</span>'."\n";
}
### Function: File Permission
function file_permission($file_permission) {
$file_permission_name = '';
switch(intval($file_permission)) {
case -2:
$file_permission_name = __('Hidden', 'wp-downloadmanager');
break;
case -1:
$file_permission_name = __('Everyone', 'wp-downloadmanager');
break;
case 0:
$file_permission_name = __('Registered Users Only', 'wp-downloadmanager');
break;
case 1:
$file_permission_name = __('At Least Contributor Role', 'wp-downloadmanager');
break;
case 2:
$file_permission_name = __('At Least Author Role', 'wp-downloadmanager');
break;
case 7:
$file_permission_name = __('At Least Editor Role', 'wp-downloadmanager');
break;
case 10:
$file_permission_name = __('At Least Administrator Role', 'wp-downloadmanager');
break;
}
return $file_permission_name;
}
### Function: Get Total Download Files
function get_download_files($display = true) {
global $wpdb;
$totalfiles = $wpdb->get_var("SELECT COUNT(file_id) FROM $wpdb->downloads");
if($display) {
echo number_format_i18n($totalfiles);
} else {
return number_format_i18n($totalfiles);
}
}
### Function Get Total Download Size
function get_download_size($display = true) {
global $wpdb;
$totalsize = $wpdb->get_var("SELECT SUM(file_size) FROM $wpdb->downloads");
if($display) {
echo format_filesize($totalsize);
} else {
return format_filesize($totalsize);
}
}
### Function: Get Total Download Hits
function get_download_hits($display = true) {
global $wpdb;
$totalhits = $wpdb->get_var("SELECT SUM(file_hits) FROM $wpdb->downloads");
if($display) {
echo number_format_i18n($totalhits);
} else {
return number_format_i18n($totalhits);
}
}
### Function: Download Embedded
function download_embedded($condition = '', $display = 'both', $sort_by = 'file_id', $sort_order = 'asc', $stream_limit = 0) {
global $wpdb, $user_ID;
$valid_sort_by = array('file_id', 'file', 'file_name', 'file_size', 'file_date', 'file_hits');