PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 261011
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v261011
261011 261001 260927 260917 260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 All 192 releases
s2member / src / includes / classes / register-access.inc.php

register-access.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 261011, at src/includes/classes/register-access.inc.php

177 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // @codingStandardsIgnoreFile
3 /**
4 * Registration Access Links.
5 *
6 * Copyright: © 2009-2011
7 * {@link http://websharks-inc.com/ WebSharks, Inc.}
8 * (coded in the USA)
9 *
10 * Released under the terms of the GNU General Public License.
11 * You should have received a copy of the GNU General Public License,
12 * along with this software. In the main directory, see: /licensing/
13 * If not, see: {@link http://www.gnu.org/licenses/}.
14 *
15 * @package s2Member\Registrations
16 * @since 3.5
17 */
18 if(!defined('WPINC')) // MUST have WordPress.
19 exit('Do not access this file directly.');
20
21 if(!class_exists('c_ws_plugin__s2member_register_access'))
22 {
23 /**
24 * Registration Access Links.
25 *
26 * @package s2Member\Registrations
27 * @since 3.5
28 */
29 class c_ws_plugin__s2member_register_access
30 {
31 /**
32 * Encrypts and authenticates a registration link or cookie value.
33 *
34 * @package s2Member\Registrations
35 * @since 261006
36 *
37 * @param string $string Value to encrypt.
38 * @param string $purpose Registration context: `link` or the cookie field name.
39 * @return string Authenticated encrypted token, or an empty string for an empty value.
40 */
41 public static function encrypt_token($string, $purpose)
42 {
43 $string = is_scalar($string) ? (string)$string : '';
44 if($string === '' || !is_string($purpose) || $purpose === '')
45 return '';
46
47 $encrypted = c_ws_plugin__s2member_utils_encryption::encrypt($string);
48 if($encrypted === '')
49 return '';
50 $payload = c_ws_plugin__s2member_utils_strings::base64_url_safe_encode($encrypted);
51 //261006.0428 Authenticate even older-runtime encryption; use a separate WordPress salt because legacy ciphertext can expose its encryption key.
52 $signature = hash_hmac('sha256', 's2member:registration:v1:'.$purpose.':'.$payload, wp_salt('auth'));
53 return 's2reg1.'.$payload.'.'.$signature;
54 }
55
56 /**
57 * Verifies a registration token before decrypting it.
58 *
59 * @package s2Member\Registrations
60 * @since 261006
61 *
62 * @param string $token Encrypted registration token.
63 * @param string $purpose Expected registration context: `link` or the cookie field name.
64 * @return string Verified plaintext, or an empty string for an invalid token.
65 */
66 public static function decrypt_token($token, $purpose)
67 {
68 if(!is_string($token) || $token === '' || !is_string($purpose) || $purpose === '')
69 return '';
70
71 if(strpos($token, 's2reg1.') === 0)
72 {
73 $parts = explode('.', $token);
74 if(count($parts) !== 3 || $parts[1] === '' || !preg_match('/^[a-f0-9]{64}$/D', $parts[2]))
75 return '';
76 $signature = hash_hmac('sha256', 's2member:registration:v1:'.$purpose.':'.$parts[1], wp_salt('auth'));
77 if(!hash_equals($signature, $parts[2]))
78 return '';
79 return c_ws_plugin__s2member_utils_encryption::decrypt(c_ws_plugin__s2member_utils_strings::base64_url_safe_decode($parts[1]));
80 }
81 //261006.0428 Existing authenticated Defuse registrations remain valid; unsigned legacy registrations must never reach the fallback decryptor.
82 return c_ws_plugin__s2member_utils_defuse::decrypt($token);
83 }
84
85 /**
86 * Generates Registration Access Links.
87 *
88 * @package s2Member\Registrations
89 * @since 3.5
90 *
91 * @param string $subscr_gateway Payment Gateway associated with a Customer.
92 * @param string $subscr_id Unique Subscr. ID associated with Payment Gateway; associated with a Customer.
93 * @param string $custom Custom String value *(as supplied in Shortcode)*; must start with installation domain name.
94 * @param int|string $item_number An s2Member-generated `item_number` *( i.e., `1` for Level 1, or `level|ccaps|fixed-term`, or `sp|ids|expiration` )*.
95 * @param bool $shrink Optional. Defaults to true. If false, the raw registration link will NOT be reduced in size by s2Member's URL shortening system.
96 *
97 * @return string|bool A Registration Access Link on success, else false on failure.
98 */
99 public static function register_link_gen($subscr_gateway = '', $subscr_id = '', $custom = '', $item_number = '', $shrink = TRUE)
100 {
101 foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v;
102 do_action('ws_plugin__s2member_before_register_link_gen', get_defined_vars());
103 unset($__refs, $__v);
104
105 if($subscr_gateway && is_string($subscr_gateway) && $subscr_id && is_string($subscr_id) && $custom && is_string($custom) && $item_number && (is_string($item_number) || is_numeric($item_number)))
106 {
107 $register = self::encrypt_token('subscr_gateway_subscr_id_custom_item_number_time:.:|:.:'.$subscr_gateway.':.:|:.:'.$subscr_id.':.:|:.:'.$custom.':.:|:.:'.$item_number.':.:|:.:'.strtotime('now'), 'link');
108
109 $register_link = home_url('/?s2member_register='.urlencode($register)); // Generate long URL/link.
110 $now = strtotime('now');
111 $register_link_exp_time = apply_filters('ws_plugin__s2member_register_link_exp_time', '2 days', get_defined_vars());
112 $register_link_expiration = strtotime('+'.$register_link_exp_time, $now);
113 $register_link_expiration = ($register_link_expiration && $register_link_expiration > $now) ? max(7 * DAY_IN_SECONDS, ($register_link_expiration - $now) + DAY_IN_SECONDS) : 7 * DAY_IN_SECONDS; //260612 Allow for custom registration link expiration.
114
115 if($shrink && ($shorter_url = c_ws_plugin__s2member_utils_urls::shorten($register_link, '', TRUE, $register_link_expiration)))
116 {
117 $shorter_url_host = c_ws_plugin__s2member_utils_urls::parse_url($shorter_url, PHP_URL_HOST);
118 $home_url_host = c_ws_plugin__s2member_utils_urls::parse_url(home_url('/'), PHP_URL_HOST);
119 $domain_tag = ($shorter_url_host && $home_url_host && strcasecmp($shorter_url_host, $home_url_host) !== 0) ? '#'.$home_url_host : ''; //260612 Personalize external short links only.
120 $register_link = $shorter_url.$domain_tag;
121 }
122 }
123 return apply_filters('ws_plugin__s2member_register_link_gen', ((!empty($register_link)) ? $register_link : FALSE), get_defined_vars());
124 }
125
126 /**
127 * Generates Registration Access Links via AJAX.
128 *
129 * @package s2Member\Registrations
130 * @since 3.5
131 *
132 * @attaches-to ``add_action('wp_ajax_ws_plugin__s2member_reg_access_link_via_ajax');``
133 *
134 * @return null Exits script execution after output is generated for AJAX caller.
135 */
136 public static function reg_access_link_via_ajax()
137 {
138 do_action('ws_plugin__s2member_before_reg_access_link_via_ajax', get_defined_vars());
139
140 status_header(200); // Send a 200 OK status header.
141 header('Content-Type: text/plain; charset=UTF-8'); // Content-Type with UTF-8.
142 while(@ob_end_clean()) ; // Clean any existing output buffers.
143
144 if(current_user_can('create_users')) // Check privileges as well. Ability to create Users?
145
146 if(!empty($_POST['ws_plugin__s2member_reg_access_link_via_ajax']) && is_string($nonce = $_POST['ws_plugin__s2member_reg_access_link_via_ajax']) && wp_verify_nonce($nonce, 'ws-plugin--s2member-reg-access-link-via-ajax'))
147
148 if(($_p = c_ws_plugin__s2member_utils_strings::trim_deep(stripslashes_deep($_POST))) && isset ($_p['s2member_reg_access_link_subscr_gateway'], $_p['s2member_reg_access_link_subscr_id'], $_p['s2member_reg_access_link_custom'], $_p['s2member_reg_access_link_item_number']))
149 $register_link = c_ws_plugin__s2member_register_access::register_link_gen((string)$_p['s2member_reg_access_link_subscr_gateway'], (string)$_p['s2member_reg_access_link_subscr_id'], (string)$_p['s2member_reg_access_link_custom'], (string)$_p['s2member_reg_access_link_item_number']);
150
151 exit(apply_filters('ws_plugin__s2member_reg_access_link_via_ajax', ((!empty($register_link)) ? $register_link : ''), get_defined_vars()));
152 }
153
154 /**
155 * Checks registration cookies.
156 *
157 * @package s2Member\Registrations
158 * @since 110707
159 *
160 * @return array|bool An array of cookies if they're OK, else false.
161 */
162 public static function reg_cookies_ok()
163 {
164 global $wpdb;
165 /** @var $wpdb \wpdb */
166
167 do_action('ws_plugin__s2member_before_reg_cookies_ok', get_defined_vars());
168
169 if(isset ($_COOKIE['s2member_subscr_gateway'], $_COOKIE['s2member_subscr_id'], $_COOKIE['s2member_custom'], $_COOKIE['s2member_item_number']))
170 if(($subscr_gateway = self::decrypt_token((string)$_COOKIE['s2member_subscr_gateway'], 'subscr_gateway')) && ($subscr_id = self::decrypt_token((string)$_COOKIE['s2member_subscr_id'], 'subscr_id')) && preg_match('/^'.preg_quote(preg_replace('/\:([0-9]+)$/', '', $_SERVER['HTTP_HOST']), '/').'/i', ($custom = self::decrypt_token((string)$_COOKIE['s2member_custom'], 'custom'))) && preg_match($GLOBALS['WS_PLUGIN__']['s2member']['c']['membership_item_number_w_level_regex'], ($item_number = self::decrypt_token((string)$_COOKIE['s2member_item_number'], 'item_number'))) && !$wpdb->get_var("SELECT `user_id` FROM `".$wpdb->usermeta."` WHERE `meta_key` = '".$wpdb->prefix."s2member_subscr_id' AND `meta_value` = '".esc_sql($subscr_id)."' LIMIT 1"))
171 $reg_cookies_ok = $reg_cookies = array('subscr_gateway' => $subscr_gateway, 'subscr_id' => $subscr_id, 'custom' => $custom, 'item_number' => $item_number);
172
173 return apply_filters('ws_plugin__s2member_reg_cookies_ok', !empty($reg_cookies_ok) && !empty($reg_cookies) ? $reg_cookies : FALSE, get_defined_vars());
174 }
175 }
176 }
177