├── index.php ├── .gitignore ├── readme.txt ├── README.md └── wc-conditionally-allow-download-access.php /index.php: -------------------------------------------------------------------------------- 1 | cookie_name = preg_replace('/[^_a-z]/i', '', $this->cookie_name); 34 | $this->cookie_value = preg_replace('/[^a-z0-9]/i', '', $this->cookie_value); 35 | if (empty($this->cookie_name) || empty($this->cookie_value)) { 36 | // abort remainder of __construct 37 | // this does not prevent the opbject from being created but prevents the actions from being added 38 | return; 39 | } 40 | // maybe set or unset cookie on login/logout 41 | add_action('wp_login', array($this, 'maybe_set_login_cookie'), 20, 2); 42 | add_action('wp_logout', array($this, 'maybe_clear_login_cookie'), 20, 2); 43 | 44 | // I believe that the following are the only times that WC writes the .htaccess file, but not 100% sure 45 | // to ensure our .htaccess file is in place it will also be checked during login 46 | // this hook fires after WC updates settings and writes its .htaccess file to downloads folder 47 | add_action('woocommerce_settings_saved', array($this, 'maybe_replace_htaccess'), 100); 48 | // this hook is called when WC is installed or updated 49 | add_action('woocommerce_installed', array($this, 'maybe_replace_htaccess'), 100); 50 | } // end public function __construct 51 | 52 | public function maybe_set_login_cookie($login, $user) { 53 | if (!class_exists('WooCommerce')) { 54 | // do nothing if WC is not active 55 | return; 56 | } 57 | // set cookit to allow direct access to WC downloads folder if user has an allowed role 58 | $set_cookie = false; 59 | foreach ($this->roles as $role) { 60 | //echo $role.' '; var_dump(current_user_can($role)); echo '
'; 61 | if (current_user_can($role)) { 62 | $set_cookie = true;; 63 | } 64 | } 65 | if (!$set_cookie) { 66 | //echo 'no cookie'; 67 | return; 68 | } 69 | //echo 'set cookie'; 70 | $options = array( 71 | 'expires' => 0, // session only 72 | 'path' => '/', 73 | 'secure' => true, 74 | 'httponly' => true 75 | ); 76 | setcookie($this->cookie_name, $this->cookie_value, $options); 77 | // htaccess file may have been changed, check it 78 | // rechecking during login because I cannot be sure I have caught all the places 79 | // that WC writes the .htaccess file 80 | $this->maybe_replace_htaccess(); 81 | } // end public function maybe_set_login_cookie 82 | 83 | public function maybe_clear_login_cookie($user_id) { 84 | if (!class_exists('WooCommerce')) { 85 | // do nothing if WC is not active 86 | return; 87 | } 88 | // only clear the cookie if it was previously set 89 | // so that we don't send any cookie information at all to the browser 90 | // unsetting a cookie that is not set would still send this information to the browswer 91 | // and potentially reveal our cookie name 92 | if (!isset($_COOKIE[$this->cookie_name])) { 93 | return; 94 | } 95 | $options = array( 96 | 'expires' => time()-86400, // time in 1 day ago 97 | 'path' => '/', 98 | 'secure' => true, 99 | 'httponly' => true 100 | ); 101 | setcookie($this->cookie_name, NULL, $options); 102 | } // end public function maybe_clear_login_cookie 103 | 104 | public function maybe_replace_htaccess() { 105 | // maybe overwite the .htaccess file in downloads folder 106 | // this is basically pulled directly from WC_Admin_Settings::check_download_folder_protection 107 | // and then modified for use 108 | $upload_dir = wp_get_upload_dir(); 109 | $downloads_path = $upload_dir['basedir'].'/woocommerce_uploads'; 110 | $file_path = $downloads_path . '/.htaccess'; 111 | $download_method = get_option('woocommerce_file_download_method'); 112 | 113 | if ($download_method == 'redirect') { 114 | $file_content = 'Options -Indexes'; 115 | } elseif ($download_method == 'force') { 116 | $file_content = 'RewriteEngine On'.PHP_EOL.'RewriteCond %{HTTP_COOKIE} !^.*'. 117 | $this->cookie_name.'='.$this->cookie_value. 118 | '.*$ [NC]'.PHP_EOL.'RewriteRule .* - [L,R=404]'; 119 | } 120 | $create = false; 121 | if (wp_mkdir_p($downloads_path ) && !file_exists($file_path)) { 122 | $create = true; 123 | } else { 124 | $current_content = @file_get_contents($file_path); 125 | if ($current_content !== $file_content) { 126 | unlink($file_path); 127 | $create = true; 128 | } 129 | } 130 | if ($create) { 131 | $file_handle = @fopen($file_path, 'wb'); 132 | if ($file_handle) { 133 | fwrite($file_handle, $file_content); 134 | fclose($file_handle); 135 | } 136 | } 137 | } // end public function maybe_replace_htaccess 138 | 139 | } // end class wc_condtional_download_access --------------------------------------------------------------------------------