| 1 |
<?php |
| 2 |
/** |
| 3 |
* Shortcode `[s2Member-PayPal-Button]` ( encryption sub-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\PayPal |
| 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_sc_paypal_button_e")) |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Shortcode `[s2Member-PayPal-Button]` ( encryption sub-routines ). |
| 24 |
* |
| 25 |
* @package s2Member\PayPal |
| 26 |
* @since 3.5 |
| 27 |
*/ |
| 28 |
class c_ws_plugin__s2member_sc_paypal_button_e |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Handles PayPal® Button encryption. |
| 32 |
* |
| 33 |
* This uses the PayPal® API. s2Member will NOT attempt to encrypt Buttons until there is at least a Business Email Address and API Username configured. |
| 34 |
* s2Member also maintains a log of communication with the PayPal® API. If logging is enabled, check: `/wp-content/plugins/s2member-logs/paypal-api.log`. |
| 35 |
* |
| 36 |
* @package s2Member\PayPal |
| 37 |
* @since 3.5 |
| 38 |
* |
| 39 |
* @param str $code The PayPal® Button Code before encryption. |
| 40 |
* @param array $vars An array of defined variables in the scope of the calling Filter. |
| 41 |
* @return str The Resulting PayPal® Button Code *( possibly encrypted, depending on configuration )*. |
| 42 |
*/ |
| 43 |
public static function sc_paypal_button_encryption ($code = FALSE, $vars = FALSE) |
| 44 |
{ |
| 45 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 46 |
do_action ("ws_plugin__s2member_before_sc_paypal_button_encryption", get_defined_vars ()); |
| 47 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 48 |
/**/ |
| 49 |
if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_btn_encryption"] && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_business"] && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_api_username"]) |
| 50 |
{ |
| 51 |
$cache = apply_filters ("ws_plugin__s2member_sc_paypal_button_encryption_cache", true, get_defined_vars ()); /* Are we caching? */ |
| 52 |
/**/ |
| 53 |
eval('$_code = $vars["_code"]; $attr = $vars["attr"];'); /* Let's unpack ( i.e. use shorter references ) to these two important data vars. */ |
| 54 |
/**/ |
| 55 |
if ($cache && ($transient = "s2m_btn_" . md5 ($code . c_ws_plugin__s2member_utilities::ver_checksum ())) && ($cache = get_transient ($transient))) |
| 56 |
$code = $cache; /* Great, so we can use the cached version here to save processing time. Notice the MD5 hash uses $code and NOT $_code. */ |
| 57 |
/**/ |
| 58 |
else if (is_array ($inputs = c_ws_plugin__s2member_utils_forms::form_whips_2_array ($_code)) && !empty ($inputs)) /* Were we able to parse hidden input variables? */ |
| 59 |
{ |
| 60 |
$paypal = array ("METHOD" => "BMCreateButton", "BUTTONCODE" => "ENCRYPTED", "BUTTONTYPE" => (($attr["sp"] || $attr["rr"] === "BN") ? "BUYNOW" : "SUBSCRIBE")); |
| 61 |
/**/ |
| 62 |
$i = 0; /* Initialize incremental variable counter. PayPal® wants these numbered using L_BUTTONVAR{n}; where {n} starts at zero. */ |
| 63 |
foreach ($inputs as $input => $value) /* Now run through each of the input variables that we parsed from the Full Button Code */ |
| 64 |
if (!preg_match ("/^cmd$/i", $input)) /* Don't include the `cmd` var; it will produce major errors in the API response. */ |
| 65 |
{ |
| 66 |
/* The PayPal® API method `BMCreateButton` expects (amount|a1|a3) to include 2 decimal places. */ |
| 67 |
if (preg_match ("/^(amount|a1|a3)$/i", $input)) |
| 68 |
$value = number_format ($value, 2, ".", ""); |
| 69 |
/**/ |
| 70 |
$paypal["L_BUTTONVAR" . $i] = $input . "=" . $value; |
| 71 |
$i++; /* Increment variable counter. */ |
| 72 |
} |
| 73 |
/**/ |
| 74 |
if (($paypal = c_ws_plugin__s2member_paypal_utilities::paypal_api_response ($paypal)) && empty ($paypal["__error"]) && !empty ($paypal["WEBSITECODE"]) && ($code = $paypal["WEBSITECODE"])) |
| 75 |
/* Only proceed if we DID get a valid response from the PayPal® API. This works as a nice fallback; just in case the API connection fails. */ |
| 76 |
{ |
| 77 |
$default_image = "https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif"; /* Default PayPal® image. */ |
| 78 |
/**/ |
| 79 |
$code = preg_replace ("/\<img[^\>]+\>/i", "", $code); /* Remove the 1x1 pixel tracking image that PayPal® sticks in there. */ |
| 80 |
$code = preg_replace ("/(\<input)([^\>]+)(\>)/ie", "'\\1'.rtrim(stripslashes('\\2'),'/').' /\\3'", $code); /* Use XHTML! */ |
| 81 |
/**/ |
| 82 |
$code = ($attr["image"] && $attr["image"] !== "default") ? preg_replace ('/ src\="(.*?)"/', ' src="' . c_ws_plugin__s2member_utils_strings::esc_ds (esc_attr ($attr["image"])) . '"', $code) : preg_replace ('/ src\="(.*?)"/', ' src="' . c_ws_plugin__s2member_utils_strings::esc_ds (esc_attr ($default_image)) . '"', $code); |
| 83 |
/**/ |
| 84 |
$code = ($attr["output"] === "anchor") ? '<a href="' . esc_attr (c_ws_plugin__s2member_utils_forms::form_whips_2_url ($code)) . '"><img src="' . esc_attr (($attr["image"] && $attr["image"] !== "default") ? $attr["image"] : $default_image) . '" style="width:auto; height:auto; border:0;" alt="PayPal®" /></a>' : $code; |
| 85 |
$code = ($attr["output"] === "url") ? c_ws_plugin__s2member_utils_forms::form_whips_2_url ($code) : $code; |
| 86 |
/**/ |
| 87 |
($cache && $transient) ? set_transient ($transient, $code, apply_filters ("ws_plugin__s2member_sc_paypal_button_encryption_cache_exp_time", 3600, get_defined_vars ())) : null; /* Caching? */ |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
/* No WordPress® Filters apply here. */ |
| 92 |
/* Instead, use: `ws_plugin__s2member_sc_paypal_button`. */ |
| 93 |
return $code; /* Button Code. Possibly w/ API encryption applied now. */ |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
?> |