| 1 |
<?php |
| 2 |
/** |
| 3 |
* Specific Post/Page Access routines. |
| 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\SP_Access |
| 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_sp_access")) |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Specific Post/Page Access routines. |
| 24 |
* |
| 25 |
* @package s2Member\SP_Access |
| 26 |
* @since 3.5 |
| 27 |
*/ |
| 28 |
class c_ws_plugin__s2member_sp_access |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Generates Specific Post/Page Access links. |
| 32 |
* |
| 33 |
* @package s2Member\SP_Access |
| 34 |
* @since 3.5 |
| 35 |
* |
| 36 |
* @param str $sp_ids Comma-delimited list of Specific Post/Page IDs *( numerical )*. |
| 37 |
* @param int $hours Optional. An expiration time for this link, in hours. Defaults to `72`. |
| 38 |
* @param bool $shrink Optional. Defaults to true. If false, the raw link will NOT be processed by the tinyURL API. |
| 39 |
* @return str|bool A Specific Post/Page Access Link, or false on failure. |
| 40 |
*/ |
| 41 |
public static function sp_access_link_gen ($sp_ids = FALSE, $hours = 72, $shrink = TRUE) |
| 42 |
{ |
| 43 |
eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 44 |
do_action ("ws_plugin__s2member_before_sp_access_link_gen", get_defined_vars ()); |
| 45 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 46 |
/**/ |
| 47 |
if (($sp_ids = preg_replace ("/[^0-9;,]/", "", $sp_ids)) && ($leading_id = preg_replace ("/^([0-9]+)(.*?)$/", "$1", $sp_ids)) && $hours) |
| 48 |
{ |
| 49 |
$sp_access = c_ws_plugin__s2member_utils_encryption::encrypt ("sp_time_hours:.:|:.:" . $sp_ids . ":.:|:.:" . strtotime ("now") . ":.:|:.:" . $hours); |
| 50 |
$sp_access_link = add_query_arg ("s2member_sp_access", urlencode ($sp_access), get_permalink ($leading_id)); |
| 51 |
/**/ |
| 52 |
if ($shrink && ($_alternative = apply_filters ("ws_plugin__s2member_sp_access_link_gen_alternative", $sp_access_link, get_defined_vars ())) && strlen ($_alternative) < strlen ($sp_access_link)) |
| 53 |
return apply_filters ("ws_plugin__s2member_sp_access_link_gen", $_alternative, get_defined_vars ()); |
| 54 |
/**/ |
| 55 |
else if ($shrink && ($tinyurl = c_ws_plugin__s2member_utils_urls::remote ("http://tinyurl.com/api-create.php?url=" . rawurlencode ($sp_access_link)))) |
| 56 |
return apply_filters ("ws_plugin__s2member_sp_access_link_gen", $tinyurl . "#" . $_SERVER["HTTP_HOST"], get_defined_vars ()); |
| 57 |
/**/ |
| 58 |
else /* Else use the long one; tinyURL will fail when/if their server is down periodically. */ |
| 59 |
return apply_filters ("ws_plugin__s2member_sp_access_link_gen", $sp_access_link, get_defined_vars ()); |
| 60 |
} |
| 61 |
/**/ |
| 62 |
return false; |
| 63 |
} |
| 64 |
/** |
| 65 |
* Generates Specific Post/Page Access links via AJAX. |
| 66 |
* |
| 67 |
* @package s2Member\SP_Access |
| 68 |
* @since 3.5 |
| 69 |
* |
| 70 |
* @attaches-to: ``add_action("wp_ajax_ws_plugin__s2member_sp_access_link_via_ajax");`` |
| 71 |
* |
| 72 |
* @return null Exits script execution after returning data for AJAX caller. |
| 73 |
*/ |
| 74 |
public static function sp_access_link_via_ajax () |
| 75 |
{ |
| 76 |
do_action ("ws_plugin__s2member_before_sp_access_link_via_ajax", get_defined_vars ()); |
| 77 |
/**/ |
| 78 |
if (current_user_can ("create_users")) /* Check priveledges as well. */ |
| 79 |
if (!empty ($_POST["ws_plugin__s2member_sp_access_link_via_ajax"]) && ($nonce = $_POST["ws_plugin__s2member_sp_access_link_via_ajax"]) && wp_verify_nonce ($nonce, "ws-plugin--s2member-sp-access-link-via-ajax") && ($_p = c_ws_plugin__s2member_utils_strings::trim_deep (stripslashes_deep ($_POST))) && isset ($_p["s2member_sp_access_link_ids"], $_p["s2member_sp_access_link_hours"])) |
| 80 |
echo apply_filters ("ws_plugin__s2member_sp_access_link_via_ajax", c_ws_plugin__s2member_sp_access::sp_access_link_gen ($_p["s2member_sp_access_link_ids"], $_p["s2member_sp_access_link_hours"]), get_defined_vars ()); |
| 81 |
/**/ |
| 82 |
exit (); /* Clean exit. */ |
| 83 |
} |
| 84 |
/** |
| 85 |
* Handles Specific Post/Page Access authentication. |
| 86 |
* |
| 87 |
* @package s2Member\SP_Access |
| 88 |
* @since 3.5 |
| 89 |
* |
| 90 |
* @param int|str $sp_id Numeric Post/Page ID in WordPress®. |
| 91 |
* @param bool $read_only Optional. Defaults to false. If ``$read_only = true``, |
| 92 |
* no session cookies are set, no IP Restrictions are checked, and script execution is not exited on Link failure. |
| 93 |
* In other words, with ``$read_only = true``, this function will simply return true or false. |
| 94 |
* @return null|bool Always returns true if access is indeed allowed in one way or another. |
| 95 |
* If access is denied with ``$read_only = true`` simply return false. |
| 96 |
* If access is denied with ``$read_only = false``, return false; |
| 97 |
* but if a Specific Post/Page Access Link is currently being used, |
| 98 |
* we exit with a warning about Access Link expiration instead. |
| 99 |
*/ |
| 100 |
public static function sp_access ($sp_id = FALSE, $read_only = FALSE) |
| 101 |
{ |
| 102 |
do_action ("ws_plugin__s2member_before_sp_access", get_defined_vars ()); |
| 103 |
/**/ |
| 104 |
$excluded = apply_filters ("ws_plugin__s2member_sp_access_excluded", false, get_defined_vars ()); |
| 105 |
/**/ |
| 106 |
if ($excluded || current_user_can (apply_filters ("ws_plugin__s2member_sp_access_excluded_cap", "edit_posts", get_defined_vars ()))) |
| 107 |
return apply_filters ("ws_plugin__s2member_sp_access", true, get_defined_vars ()); |
| 108 |
/**/ |
| 109 |
else if ($sp_id && ((!empty ($_GET["s2member_sp_access"]) && is_array ($sp_access_values = (array)trim (stripslashes ($_GET["s2member_sp_access"])))) || (is_array ($sp_access_values = c_ws_plugin__s2member_sp_access::sp_access_session ()) && !empty ($sp_access_values)))) |
| 110 |
{ |
| 111 |
foreach ($sp_access_values as $sp_access_value) /* Supports multiple access values in a session. */ |
| 112 |
{ |
| 113 |
if (is_array ($sp_access = preg_split ("/\:\.\:\|\:\.\:/", c_ws_plugin__s2member_utils_encryption::decrypt ($sp_access_value)))) |
| 114 |
{ |
| 115 |
if (count ($sp_access) === 4 && $sp_access[0] === "sp_time_hours" && in_array ($sp_id, preg_split ("/[\r\n\t\s;,]+/", $sp_access[1]))) |
| 116 |
{ |
| 117 |
if ($sp_access[2] <= strtotime ("now") && ($sp_access[2] + ($sp_access[3] * 3600)) >= strtotime ("now")) |
| 118 |
{ |
| 119 |
if (!$read_only && !empty ($_GET["s2member_sp_access"])) /* Cookie session. */ |
| 120 |
c_ws_plugin__s2member_sp_access::sp_access_session ($_GET["s2member_sp_access"]); |
| 121 |
/**/ |
| 122 |
if ($read_only || c_ws_plugin__s2member_ip_restrictions::ip_restrictions_ok ($_SERVER["REMOTE_ADDR"], $sp_access_value)) |
| 123 |
return apply_filters ("ws_plugin__s2member_sp_access", true, get_defined_vars ()); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
/**/ |
| 129 |
if (!$read_only && !empty ($_GET["s2member_sp_access"])) /* If this is a Specific Post/Page Link? */ |
| 130 |
{ |
| 131 |
echo '<strong>Your Link Expired:</strong><br />Please contact Support if you need assistance.'; |
| 132 |
/**/ |
| 133 |
exit (); /* $_GET["s2member_sp_access"] has expired. Or it is simply invalid. */ |
| 134 |
} |
| 135 |
/**/ |
| 136 |
return apply_filters ("ws_plugin__s2member_sp_access", false, get_defined_vars ()); |
| 137 |
} |
| 138 |
/**/ |
| 139 |
else |
| 140 |
return apply_filters ("ws_plugin__s2member_sp_access", false, get_defined_vars ()); |
| 141 |
} |
| 142 |
/** |
| 143 |
* Handles Specific Post/Page sessions, by writing access values into a cookie. |
| 144 |
* |
| 145 |
* Can be used to add a new value to the session, and/or to return the current set of values in the session. |
| 146 |
* |
| 147 |
* @package s2Member\SP_Access |
| 148 |
* @since 3.5 |
| 149 |
* |
| 150 |
* @param str $add_sp_access_value Encrypted Specific Post/Page Access value. |
| 151 |
* @return array Array of Specific Post/Page Access values. |
| 152 |
*/ |
| 153 |
public static function sp_access_session ($add_sp_access_value = FALSE) |
| 154 |
{ |
| 155 |
eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 156 |
do_action ("ws_plugin__s2member_before_sp_access_session", get_defined_vars ()); |
| 157 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 158 |
/**/ |
| 159 |
$sp_access_values = (!empty ($_COOKIE["s2member_sp_access"])) ? preg_split ("/\:\.\:\|\:\.\:/", $_COOKIE["s2member_sp_access"]) : array (); |
| 160 |
/**/ |
| 161 |
if ($add_sp_access_value && !in_array ($add_sp_access_value, $sp_access_values)) /* If it's not in the session already. */ |
| 162 |
{ |
| 163 |
$sp_access_values[] = $add_sp_access_value; /* Add an access value, and update the delimited session cookie. */ |
| 164 |
$sp_access_values = array_unique ($sp_access_values); /* Keep this array unique; disallow double-stacking. */ |
| 165 |
/**/ |
| 166 |
$cookie = implode (":.:|:.:", $sp_access_values); /* Implode the access values into a delimited string. */ |
| 167 |
$cookie = (strlen ($cookie) >= 4096) ? $add_sp_access_value : $cookie; /* Max cookie size is 4kbs. */ |
| 168 |
/**/ |
| 169 |
setcookie ("s2member_sp_access", $cookie, time () + 31556926, COOKIEPATH, COOKIE_DOMAIN) . setcookie ("s2member_sp_access", $cookie, time () + 31556926, SITECOOKIEPATH, COOKIE_DOMAIN) . ($_COOKIE["s2member_sp_access"] = $cookie); |
| 170 |
/**/ |
| 171 |
eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 172 |
do_action ("ws_plugin__s2member_during_sp_access_session", get_defined_vars ()); |
| 173 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 174 |
} |
| 175 |
/**/ |
| 176 |
return apply_filters ("ws_plugin__s2member_sp_access_session", $sp_access_values, get_defined_vars ()); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
?> |