1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 16 17 18 19 20 21 22 23 24 25 26 27
28 function edd_change_downloads_upload_dir() {
29 global $pagenow;
30
31 if ( ! empty( $_REQUEST['post_id'] ) && ( 'async-upload.php' == $pagenow || 'media-upload.php' == $pagenow ) ) {
32 if ( 'download' == get_post_type( $_REQUEST['post_id'] ) ) {
33 $wp_upload_dir = wp_upload_dir();
34 $upload_path = $wp_upload_dir['basedir'] . '/edd' . $wp_upload_dir['subdir'];
35
36
37
38 $rules = apply_filters( 'edd_protected_directory_htaccess_rules', 'Options -Indexes' );
39 if ( !@file_get_contents( $wp_upload_dir['basedir'] . '/edd/.htaccess' ) ) {
40 wp_mkdir_p( $wp_upload_dir['basedir'] . '/edd' );
41 }
42 @file_put_contents( $wp_upload_dir['basedir'] . '/edd/.htaccess', $rules );
43
44
45 if ( wp_mkdir_p( $upload_path ) ) {
46 if( ! file_exists( $upload_path . '/index.php' ) ) {
47 @file_put_contents( $upload_path . '/index.php', '<?php' . PHP_EOL . '// Silence is golden.' );
48 }
49 }
50 add_filter( 'upload_dir', 'edd_set_upload_dir' );
51 }
52 }
53 }
54 add_action( 'admin_init', 'edd_change_downloads_upload_dir', 999 );
55
56 57 58 59 60 61 62 63 64
65 function edd_set_upload_dir( $upload ) {
66 $upload['subdir'] = '/edd' . $upload['subdir'];
67 $upload['path'] = $upload['basedir'] . $upload['subdir'];
68 $upload['url'] = $upload['baseurl'] . $upload['subdir'];
69 return $upload;
70 }
71
72 73 74 75 76 77 78 79 80
81 function edd_create_protection_files() {
82 if ( false === get_transient( 'edd_check_protection_files' ) ) {
83 $wp_upload_dir = wp_upload_dir();
84 $upload_path = $wp_upload_dir['basedir'] . '/edd';
85
86 wp_mkdir_p( $upload_path );
87
88
89 if ( ! file_exists( $upload_path . '/index.php' ) ) {
90 @file_put_contents( $upload_path . '/index.php', '<?php' . PHP_EOL . '// Silence is golden.' );
91 }
92
93
94 $rules = apply_filters( 'edd_protected_directory_htaccess_rules', 'Options -Indexes' );
95 if ( file_exists( $upload_path . '/.htaccess' ) ) {
96 $contents = @file_get_contents( $upload_path . '/.htaccess' );
97 if ( false === strpos( $contents, 'Options -Indexes' ) || ! $contents ) {
98 @file_put_contents( $upload_path . '/.htaccess', $rules );
99 }
100 }
101
102
103 $folders = edd_scan_folders( $upload_path );
104 foreach ( $folders as $folder ) {
105
106 if ( ! file_exists( $folder . 'index.php' ) ) {
107 @file_put_contents( $folder . 'index.php', '<?php' . PHP_EOL . '// Silence is golden.' );
108 }
109 }
110
111 set_transient( 'edd_check_protection_files', true, 2678400 );
112 }
113 }
114 add_action( 'admin_init', 'edd_create_protection_files' );
115
116 117 118 119 120 121
122 function edd_scan_folders( $path = '', $return = array() ) {
123 $path = $path == ''? dirname( __FILE__ ) : $path;
124 $lists = @scandir( $path );
125
126 if ( ! empty( $lists ) ) {
127 foreach ( $lists as $f ) {
128 if ( is_dir( $path . DIRECTORY_SEPARATOR . $f ) && $f != "." && $f != ".." ) {
129 if ( ! in_array( $path . DIRECTORY_SEPARATOR . $f, $return ) )
130 $return[] = trailingslashit( $path . DIRECTORY_SEPARATOR . $f );
131
132 edd_scan_folders( $path . DIRECTORY_SEPARATOR . $f, $return);
133 }
134 }
135 }
136
137 return $return;
138 }