| 1 |
<?php |
| 2 |
/** |
| 3 |
* File Download routines for s2Member. |
| 4 |
* |
| 5 |
* Copyright: © 2009-2011 |
| 6 |
* {@link http://www.websharks-inc.com/ WebSharks, Inc.} |
| 7 |
* ( coded in the USA ) |
| 8 |
* |
| 9 |
* Released under the terms of the GNU General Public License. |
| 10 |
* You should have received a copy of the GNU General Public License, |
| 11 |
* along with this software. In the main directory, see: /licensing/ |
| 12 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 13 |
* |
| 14 |
* @package s2Member\Files |
| 15 |
* @since 3.5 |
| 16 |
*/ |
| 17 |
if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"])) |
| 18 |
exit ("Do not access this file directly."); |
| 19 |
/**/ |
| 20 |
if (!class_exists ("c_ws_plugin__s2member_files")) |
| 21 |
{ |
| 22 |
/** |
| 23 |
* File Download routines for s2Member. |
| 24 |
* |
| 25 |
* @package s2Member\Files |
| 26 |
* @since 3.5 |
| 27 |
*/ |
| 28 |
class c_ws_plugin__s2member_files |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Handles Download Access permissions. |
| 32 |
* |
| 33 |
* @package s2Member\Files |
| 34 |
* @since 110524RC |
| 35 |
* |
| 36 |
* @attaches-to: ``add_action("init");`` |
| 37 |
* |
| 38 |
* @return null|inner Return-value of inner routine. |
| 39 |
*/ |
| 40 |
public static function check_file_download_access () |
| 41 |
{ |
| 42 |
if (!empty ($_GET["s2member_file_download"])) /* Call inner routine? */ |
| 43 |
{ |
| 44 |
return c_ws_plugin__s2member_files_in::check_file_download_access (); |
| 45 |
} |
| 46 |
} |
| 47 |
/** |
| 48 |
* Determines the max period ( in days ), for Download Access. |
| 49 |
* |
| 50 |
* @package s2Member\Files |
| 51 |
* @since 3.5 |
| 52 |
* |
| 53 |
* @return int Number of days, where 0 means no access to files is allowed. |
| 54 |
* Will not return a value > `365`, because this routine also controls the age of download logs to archives. |
| 55 |
* |
| 56 |
* @todo Remove the limitation of `365` days somehow. |
| 57 |
*/ |
| 58 |
public static function max_download_period () |
| 59 |
{ |
| 60 |
do_action ("ws_plugin__s2member_before_max_download_period", get_defined_vars ()); |
| 61 |
/**/ |
| 62 |
for ($n = 0, $max = 0; $n <= $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["levels"]; $n++) |
| 63 |
if (!empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed"])) |
| 64 |
if (!empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed_days"])) |
| 65 |
if (($days = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed_days"])) |
| 66 |
$max = ($max < $days) ? $days : $max; |
| 67 |
/**/ |
| 68 |
return apply_filters ("ws_plugin__s2member_max_download_period", (($max > 365) ? 365 : $max), get_defined_vars ()); |
| 69 |
} |
| 70 |
/** |
| 71 |
* Determines the minimum Level required for File Download Access. |
| 72 |
* |
| 73 |
* @package s2Member\Files |
| 74 |
* @since 3.5 |
| 75 |
* |
| 76 |
* @return bool|int False if no access is allowed, else Level number (int) 0+. |
| 77 |
*/ |
| 78 |
public static function min_level_4_downloads () |
| 79 |
{ |
| 80 |
do_action ("ws_plugin__s2member_before_min_level_4_downloads", get_defined_vars ()); |
| 81 |
/**/ |
| 82 |
for ($n = 0, $min = false; $n <= $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["levels"]; $n++) |
| 83 |
if (!empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed"])) |
| 84 |
if (!empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed_days"])) |
| 85 |
if (($min = $n) >= 0) |
| 86 |
break; |
| 87 |
/**/ |
| 88 |
return apply_filters ("ws_plugin__s2member_min_level_4_downloads", ((is_int ($min)) ? $min : false), get_defined_vars ()); |
| 89 |
} |
| 90 |
/** |
| 91 |
* Determines how many File Downloads are allowed, also provides some extended details. |
| 92 |
* |
| 93 |
* @package s2Member\Files |
| 94 |
* @since 3.5 |
| 95 |
* |
| 96 |
* @param obj $user Optional. Defaults to the current User's object. |
| 97 |
* @param str $not_counting_this_particular_file Optional. If you want to exclude a particular file. |
| 98 |
* @param array $log Optional. Prevents another database connection *( i.e. the log does not need to be pulled again )*. |
| 99 |
* @return array An array with three elements: `allowed`, `allowed_days`, `currently`. |
| 100 |
*/ |
| 101 |
public static function user_downloads ($user = FALSE, $not_counting_this_particular_file = FALSE, $log = NULL) |
| 102 |
{ |
| 103 |
eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 104 |
do_action ("ws_plugin__s2member_before_user_downloads", get_defined_vars ()); |
| 105 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 106 |
/**/ |
| 107 |
$allowed = $allowed_days = $currently = 0; /* Initialize all of these to zero. */ |
| 108 |
/**/ |
| 109 |
if ((is_object ($user) || is_object ($user = (is_user_logged_in ()) ? wp_get_current_user () : false)) && ($user_id = $user->ID)) |
| 110 |
{ |
| 111 |
for ($n = 0; $n <= $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["levels"]; $n++) |
| 112 |
{ |
| 113 |
if ($user->has_cap ("access_s2member_level" . $n)) /* Do they have access? */ |
| 114 |
{ |
| 115 |
if (!empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed"])) |
| 116 |
if (!empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed_days"])) |
| 117 |
{ |
| 118 |
$allowed = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed"]; |
| 119 |
$allowed_days = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed_days"]; |
| 120 |
} |
| 121 |
if ($user->has_cap ("s2member_level" . $n)) /* We can stop now, if this is their Role. */ |
| 122 |
break; /* Break now. */ |
| 123 |
} |
| 124 |
} |
| 125 |
/**/ |
| 126 |
$file_download_access_log = (isset ($log)) ? (array)$log : (array)get_user_option ("s2member_file_download_access_log", $user_id); |
| 127 |
foreach ($file_download_access_log as $file_download_access_log_entry_key => $file_download_access_log_entry) |
| 128 |
if (strtotime ($file_download_access_log_entry["date"]) >= strtotime ("-" . $allowed_days . " days")) |
| 129 |
if ($file_download_access_log_entry["file"] !== $not_counting_this_particular_file) |
| 130 |
$currently = $currently + 1; |
| 131 |
} |
| 132 |
/**/ |
| 133 |
return apply_filters ("ws_plugin__s2member_user_downloads", array ("allowed" => $allowed, "allowed_days" => $allowed_days, "currently" => $currently), get_defined_vars ()); |
| 134 |
} |
| 135 |
/** |
| 136 |
* Creates a File Download Key. |
| 137 |
* |
| 138 |
* Builds a hash of: ``date("Y-m-d") . $_SERVER["REMOTE_ADDR"] . $_SERVER["HTTP_USER_AGENT"] . $file``. |
| 139 |
* |
| 140 |
* @package s2Member\Files |
| 141 |
* @since 3.5 |
| 142 |
* |
| 143 |
* @param str $file Location of your protected file, relative to the `/s2member-files/` directory. |
| 144 |
* In other words, just the name of the file *(i.e. `file.zip` )*. |
| 145 |
* @param str $directive Optional. One of `ip-forever|universal|cache-compatible`. |
| 146 |
* `ip-forever` = a Download Key that never expires, tied only to a specific file and IP address. |
| 147 |
* `universal` and/or `cache-compatible` = a Download Key which never expires, and is NOT tied to any specific User. Use at your own risk. |
| 148 |
* @return str A Download Key. MD5 hash, 32 characters, URL-safe. |
| 149 |
*/ |
| 150 |
public static function file_download_key ($file = FALSE, $directive = FALSE) |
| 151 |
{ |
| 152 |
eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 153 |
do_action ("ws_plugin__s2member_before_file_download_key", get_defined_vars ()); |
| 154 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 155 |
/**/ |
| 156 |
if ($directive === "ip-forever") |
| 157 |
{ |
| 158 |
$allow_caching = false; |
| 159 |
$salt = $file . $_SERVER["REMOTE_ADDR"]; |
| 160 |
} |
| 161 |
else if ($directive === "universal" || $directive === "cache-compatible" || !empty ($directive)) |
| 162 |
{ |
| 163 |
$allow_caching = true; |
| 164 |
$salt = $file; |
| 165 |
} |
| 166 |
else /* Otherwise, we use the default ``$salt``. */ |
| 167 |
{ |
| 168 |
$allow_caching = false; |
| 169 |
$salt = date ("Y-m-d") . $_SERVER["REMOTE_ADDR"] . $_SERVER["HTTP_USER_AGENT"] . $file; |
| 170 |
} |
| 171 |
/**/ |
| 172 |
$key = md5 (c_ws_plugin__s2member_utils_encryption::xencrypt ($salt)); |
| 173 |
/**/ |
| 174 |
if ($allow_caching === false) /* Disallow caching? */ |
| 175 |
c_ws_plugin__s2member_no_cache::no_cache_constants (true); |
| 176 |
/**/ |
| 177 |
return apply_filters ("ws_plugin__s2member_file_download_key", $key, get_defined_vars ()); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
?> |