Easy Digital Downloads
  • Package
  • Function
  • Tree

Packages

  • EDD
    • Admin
      • Actions
      • Add-ons
      • Dashboard
      • Discounts
      • Downloads
      • Export
      • Notices
      • Pages
      • Payments
      • Reports
      • Settings
      • System
      • Upgrades
      • Upload
      • Welcome
    • Cart
    • Checkout
    • Classes
      • API
      • Fees
      • HTML
      • Roles
      • Session
    • Emails
    • Functions
      • AJAX
      • Compatibility
      • Errors
      • Formatting
      • Install
      • Login
      • Taxes
      • Templates
    • Gateways
    • Logging
    • Payments
    • Shortcodes
    • Widgets

Functions

  • edd_change_downloads_upload_dir
  • edd_create_protection_files
  • edd_scan_folders
  • edd_set_upload_dir
  1 <?php
  2 /**
  3  * Upload Functions
  4  *
  5  * @package     EDD
  6  * @subpackage  Admin/Upload
  7  * @copyright   Copyright (c) 2013, Pippin Williamson
  8  * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
  9  * @since       1.0
 10  */
 11 
 12 // Exit if accessed directly
 13 if ( ! defined( 'ABSPATH' ) ) exit;
 14 
 15 /**
 16  * Change Downloads Upload Directory
 17  *
 18  * Hooks the edd_set_upload_dir filter when appropriate. This function works by
 19  * hooking on the WordPress Media Uploader and moving the uploading files that
 20  * are used for EDD to an edd directory under wp-content/uploads/ therefore,
 21  * the new directory is wp-content/uploads/edd/{year}/{month}. This directory is
 22  * provides protection to anything uploaded to it.
 23  *
 24  * @since 1.0
 25  * @global $pagenow
 26  * @return void
 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             // We don't want users snooping in the EDD root, so let's add htacess there, first
 37             // Creating the directory if it doesn't already exist.
 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             // Now add blank index.php files to the {year}/{month} directory
 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  * Set Upload Directory
 58  *
 59  * Sets the upload dir to edd. This function is called from
 60  * edd_change_downloads_upload_dir()
 61  *
 62  * @since 1.0
 63  * @return array Upload directory information
 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  * Creates blank index.php and .htaccess files
 74  *
 75  * This function runs approximately once per month in order to ensure all folders
 76  * have their necessary protection files
 77  *
 78  * @since 1.1.5
 79  * @return void
 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         // Top level blank index.php
 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         // Top level .htaccess file
 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         // Now place index.php files in all sub folders
103         $folders = edd_scan_folders( $upload_path );
104         foreach ( $folders as $folder ) {
105             // Create index.php, if it doesn't exist
106             if ( ! file_exists( $folder . 'index.php' ) ) {
107                 @file_put_contents( $folder . 'index.php', '<?php' . PHP_EOL . '// Silence is golden.' );
108             }
109         }
110         // Only have this run the first time. This is just to create .htaccess files in existing folders
111         set_transient( 'edd_check_protection_files', true, 2678400 );
112     }
113 }
114 add_action( 'admin_init', 'edd_create_protection_files' );
115 
116 /**
117  * Scans all folders inside of /uploads/edd
118  *
119  * @since 1.1.5
120  * @return array $return List of files inside directory
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 }
Easy Digital Downloads API documentation generated by ApiGen 2.8.0