PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 110926
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v110926
260917 260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 All 189 releases
s2member / includes / classes / files.inc.php

files.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 110926, at includes/classes/files.inc.php

221 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @also-called-by: API Function {@link s2Member\API_Functions\s2member_file_download_url()}, w/ ``$create_file_download_url`` param.
38 *
39 * @param array $create_file_download_url Optional. If this function is called directly, we can pass arguments through this array.
40 * Possible array elements: `file_download` *(required)*, `file_download_key`, `file_stream`, `file_inline`, `file_storage`, `file_remote`, `file_ssl`, `file_rewrite`, `file_rewrite_base`, `skip_confirmation`, `url_to_storage_source`, `count_against_user`, `ignore_user`.
41 * @return null|str If called directly with ``$create_file_download_url``, returns a string with the URL, based on configuration.
42 * Else, this function may exit script execution after serving a File Download.
43 */
44 public static function check_file_download_access ($create_file_download_url = FALSE) /* Calls inner routine. */
45 {
46 if (is_array ($create_file_download_url) || !empty ($_GET["s2member_file_download"])) /* Call inner routine? */
47 {
48 return c_ws_plugin__s2member_files_in::check_file_download_access ($create_file_download_url);
49 }
50 }
51 /**
52 * Generates a File Download URL for access to a file protected by s2Member.
53 *
54 * @package s2Member\Files
55 * @since 110926
56 *
57 * @param array $config Required. This is an array of configuration options associated with permissions being checked against the current User/Member; and also the actual URL generated by this routine.
58 * Possible ``$config`` array elements: `file_download` *(required)*, `file_download_key`, `file_stream`, `file_inline`, `file_storage`, `file_remote`, `file_ssl`, `file_rewrite`, `file_rewrite_base`, `skip_confirmation`, `url_to_storage_source`, `count_against_user`, `ignore_user`.
59 * @return str A File Download URL string on success; or an array on success, with elements `streamer`, `file`, `url` when/if ``$get_streamer_array`` is true; else false on any type of failure.
60 * Note: by default, this function DOES consider the current User/Member. If `ignore_user` is NOT set, or is set to false, this function MAY very well return false, when/if the current User/Member is NOT allowed access to the `file_download`, based on configuration.
61 */
62 public static function create_file_download_url ($config = FALSE, $get_streamer_array = FALSE) /* Calls inner routine. */
63 {
64 return c_ws_plugin__s2member_files_in::create_file_download_url ($config, $get_streamer_array);
65 }
66 /**
67 * Auto-configures an Amazon® S3 Bucket's ACLs.
68 *
69 * @package s2Member\Files
70 * @since 110926
71 *
72 * @return bool|array True on success, else array on failure.
73 * Failure array will contain a failure `code`, and a failure `message`.
74 */
75 public static function amazon_s3_auto_configure_acls () /* Calls inner routine. */
76 {
77 return c_ws_plugin__s2member_files_in::amazon_s3_auto_configure_acls ();
78 }
79 /**
80 * Auto-configures Amazon® S3/CloudFront distros.
81 *
82 * @package s2Member\Files
83 * @since 110926
84 *
85 * @return bool|array True on success, else array on failure.
86 * Failure array will contain a failure `code`, and a failure `message`.
87 */
88 public static function amazon_s3_cf_auto_configure_distros () /* Calls inner routine. */
89 {
90 return c_ws_plugin__s2member_files_in::amazon_s3_cf_auto_configure_distros ();
91 }
92 /**
93 * Determines the max period ( in days ), for Download Access.
94 *
95 * @package s2Member\Files
96 * @since 3.5
97 *
98 * @return int Number of days, where 0 means no access to files is allowed.
99 * Will not return a value > `365`, because this routine also controls the age of download logs to archives.
100 *
101 * @todo Remove the limitation of `365` days somehow.
102 */
103 public static function max_download_period ()
104 {
105 do_action ("ws_plugin__s2member_before_max_download_period", get_defined_vars ());
106 /**/
107 for ($n = 0, $max = 0; $n <= $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["levels"]; $n++)
108 if (!empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed"]))
109 if (!empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed_days"]))
110 if (($days = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed_days"]))
111 $max = ($max < $days) ? $days : $max;
112 /**/
113 return apply_filters ("ws_plugin__s2member_max_download_period", (($max > 365) ? 365 : $max), get_defined_vars ());
114 }
115 /**
116 * Determines the minimum Level required for File Download Access.
117 *
118 * @package s2Member\Files
119 * @since 3.5
120 *
121 * @return bool|int False if no access is allowed, else Level number (int) 0+.
122 */
123 public static function min_level_4_downloads ()
124 {
125 do_action ("ws_plugin__s2member_before_min_level_4_downloads", get_defined_vars ());
126 /**/
127 for ($n = 0, $min = false; $n <= $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["levels"]; $n++)
128 if (!empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed"]))
129 if (!empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed_days"]))
130 if (($min = $n) >= 0)
131 break;
132 /**/
133 return apply_filters ("ws_plugin__s2member_min_level_4_downloads", ((is_int ($min)) ? $min : false), get_defined_vars ());
134 }
135 /**
136 * Determines how many File Downloads are allowed, also provides some extended details.
137 *
138 * @package s2Member\Files
139 * @since 3.5
140 *
141 * @param obj $user Optional. Defaults to the current User's object.
142 * @param str $not_counting_this_particular_file Optional. If you want to exclude a particular file.
143 * @param array $log Optional. Prevents another database connection *( i.e. the log does not need to be pulled again )*.
144 * @return array An array with three elements: `allowed`, `allowed_days`, `currently`.
145 */
146 public static function user_downloads ($user = FALSE, $not_counting_this_particular_file = FALSE, $log = NULL)
147 {
148 eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
149 do_action ("ws_plugin__s2member_before_user_downloads", get_defined_vars ());
150 unset ($__refs, $__v); /* Unset defined __refs, __v. */
151 /**/
152 $allowed = $allowed_days = $currently = 0; /* Initialize all of these to zero. */
153 /**/
154 if ((is_object ($user) || is_object ($user = (is_user_logged_in ()) ? wp_get_current_user () : false)) && !empty ($user->ID) && ($user_id = $user->ID))
155 {
156 for ($n = 0; $n <= $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["levels"]; $n++)
157 {
158 if ($user->has_cap ("access_s2member_level" . $n)) /* Do they have access? */
159 {
160 if (!empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed"]))
161 if (!empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed_days"]))
162 {
163 $allowed = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed"];
164 $allowed_days = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $n . "_file_downloads_allowed_days"];
165 }
166 if ($user->has_cap ("s2member_level" . $n)) /* We can stop now, if this is their Role. */
167 break; /* Break now. */
168 }
169 }
170 /**/
171 $file_download_access_log = (isset ($log)) ? (array)$log : (array)get_user_option ("s2member_file_download_access_log", $user_id);
172 foreach ($file_download_access_log as $file_download_access_log_entry_key => $file_download_access_log_entry)
173 if (strtotime ($file_download_access_log_entry["date"]) >= strtotime ("-" . $allowed_days . " days"))
174 if ($file_download_access_log_entry["file"] !== $not_counting_this_particular_file)
175 $currently = $currently + 1;
176 }
177 /**/
178 return apply_filters ("ws_plugin__s2member_user_downloads", array ("allowed" => $allowed, "allowed_days" => $allowed_days, "currently" => $currently), get_defined_vars ());
179 }
180 /**
181 * Creates a File Download Key.
182 *
183 * Builds a hash of: ``date("Y-m-d") . $_SERVER["REMOTE_ADDR"] . $_SERVER["HTTP_USER_AGENT"] . $file``.
184 *
185 * @package s2Member\Files
186 * @since 3.5
187 *
188 * @param str $file Location of your protected file, relative to the `/s2member-files/` directory.
189 * In other words, just the name of the file *(i.e. `file.zip` )*.
190 * @param str $directive Optional. One of `ip-forever|universal|cache-compatible`.
191 * `ip-forever` = a Download Key that never expires, tied only to a specific file and IP address.
192 * `universal` and/or `cache-compatible` = a Download Key which never expires, and is NOT tied to any specific User. Use at your own risk.
193 * @return str A Download Key. MD5 hash, 32 characters, URL-safe.
194 */
195 public static function file_download_key ($file = FALSE, $directive = FALSE)
196 {
197 eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
198 do_action ("ws_plugin__s2member_before_file_download_key", get_defined_vars ());
199 unset ($__refs, $__v); /* Unset defined __refs, __v. */
200 /**/
201 $file = ($file && is_string ($file) && ($file = trim ($file, "/"))) ? $file : "";
202 /**/
203 if ($directive === "ip-forever") /* Allows the current IP forever. */
204 eval ('$allow_caching = false; $salt = $file . $_SERVER["REMOTE_ADDR"];');
205 /**/
206 else if ($directive === "universal" || $directive === "cache-compatible" || $directive)
207 eval ('$allow_caching = true; $salt = $file;');
208 /**/
209 else /* Otherwise, we use the default ``$salt``, which is VERY restrictive; even to a specific browser. */
210 eval ('$allow_caching = false; $salt = date ("Y-m-d") . $_SERVER["REMOTE_ADDR"] . $_SERVER["HTTP_USER_AGENT"] . $file;');
211 /**/
212 $key = md5 (c_ws_plugin__s2member_utils_encryption::xencrypt ($salt));
213 /**/
214 if ($allow_caching === false) /* Disallow caching? */
215 c_ws_plugin__s2member_no_cache::no_cache_constants (true);
216 /**/
217 return apply_filters ("ws_plugin__s2member_file_download_key", $key, get_defined_vars ());
218 }
219 }
220 }
221 ?>