Source Code Documentation


s2member\includes\classes\files-in.inc.php


  1. <?php
  2. /**
  3. * File Download routines for s2Member (inner processing 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\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_in"))
  21. {
  22. /**
  23. * File Download routines for s2Member (inner processing routines).
  24. *
  25. * @package s2Member\Files
  26. * @since 3.5
  27. */
  28. class c_ws_plugin__s2member_files_in
  29. {
  30. /**
  31. * Handles Download Access permissions.
  32. *
  33. * @package s2Member\Files
  34. * @since 3.5
  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`, `check_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)
  45. {
  46. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  47. do_action("ws_plugin__s2member_before_file_download_access", get_defined_vars());
  48. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  49.  
  50. $_g = !empty($_GET) ? $_GET : array();
  51. $_g = c_ws_plugin__s2member_utils_strings::trim_deep(stripslashes_deep($_g));
  52.  
  53. $creating = /* Creating URL? */ (is_array($create = $create_file_download_url)) ? true : false;
  54. $serving = /* If NOT creating a File Download URL, we're serving one. */ (!$creating) ? true : false;
  55. $serving_range = $range = /* Default values (so these variables DO get defined at all times). */ false;
  56. if /* If we're serving, let's see if we're serving a byte-range request here. */($serving)
  57. {
  58. $range = (string)@$_SERVER["HTTP_RANGE"];
  59. if(!$range && function_exists("apache_request_headers"))
  60. // Note: ``apache_request_headers()`` works in FastCGI too, starting w/ PHP v5.4.
  61. foreach((array)apache_request_headers() as $_header => $_value)
  62. if(is_string($_header) && strcasecmp($_header, "range") === 0)
  63. $range = $_value;
  64. if /* Serving a range? */($range)
  65. $serving_range = true;
  66. unset($_header, $_value);
  67. }
  68.  
  69. $req["file_download"] = ($creating) ? @$create["file_download"] : @$_g["s2member_file_download"];
  70. $req["file_download_key"] = ($creating) ? @$create["file_download_key"] : @$_g["s2member_file_download_key"];
  71.  
  72. $req["file_stream"] = ($creating) ? @$create["file_stream"] : @$_g["s2member_file_stream"];
  73. $req["file_inline"] = ($creating) ? @$create["file_inline"] : @$_g["s2member_file_inline"];
  74. $req["file_storage"] = ($creating) ? @$create["file_storage"] : @$_g["s2member_file_storage"];
  75. $req["file_remote"] = ($creating) ? @$create["file_remote"] : @$_g["s2member_file_remote"];
  76. $req["file_ssl"] = ($creating) ? @$create["file_ssl"] : @$_g["s2member_file_ssl"];
  77.  
  78. $req["file_rewrite"] = ($creating) ? @$create["file_rewrite"] : /* N/A. */ null;
  79. $req["file_rewrite_base"] = ($creating) ? @$create["file_rewrite_base"] : /* N/A. */ null;
  80.  
  81. $req["skip_confirmation"] = ($creating) ? @$create["skip_confirmation"] : /* N/A. */ null;
  82. $req["url_to_storage_source"] = ($creating) ? @$create["url_to_storage_source"] : /* N/A. */ null;
  83. $req["count_against_user"] = ($creating) ? @$create["count_against_user"] : /* N/A. */ null;
  84. $req["check_user"] = ($creating) ? @$create["check_user"] : /* N/A. */ null;
  85.  
  86. if($req["file_download"] && is_string($req["file_download"]) && ($req["file_download"] = trim($req["file_download"], "/")))
  87. if(strpos($req["file_download"], "..") === false && strpos(basename($req["file_download"]), ".") !== 0)
  88. {
  89. $using_amazon_cf_storage = ((!$req["file_storage"] || strcasecmp((string)$req["file_storage"], "cf") === 0) && c_ws_plugin__s2member_utils_conds::using_amazon_cf_storage()) ? true : false;
  90. $using_amazon_s3_storage = ((!$req["file_storage"] || strcasecmp((string)$req["file_storage"], "s3") === 0) && c_ws_plugin__s2member_utils_conds::using_amazon_s3_storage()) ? true : false;
  91. $using_amazon_storage = /* Either? */ ($using_amazon_cf_storage || $using_amazon_s3_storage) ? true : false;
  92.  
  93. $excluded = apply_filters("ws_plugin__s2member_check_file_download_access_excluded", false, get_defined_vars());
  94. $valid_file_download_key = ($req["file_download_key"] && is_string($req["file_download_key"]) && $creating && (!isset($req["check_user"]) || !filter_var($req["check_user"], FILTER_VALIDATE_BOOLEAN)) && (!isset($req["count_against_user"]) || !filter_var($req["count_against_user"], FILTER_VALIDATE_BOOLEAN))) ? true : false;
  95. $valid_file_download_key = (!$valid_file_download_key && $req["file_download_key"] && is_string($req["file_download_key"])) ? c_ws_plugin__s2member_files_in::check_file_download_key($req["file_download"], $req["file_download_key"]) : false;
  96. $checking_user = ($excluded || $valid_file_download_key || ($creating && (!isset($req["check_user"]) || !filter_var($req["check_user"], FILTER_VALIDATE_BOOLEAN)) && (!isset($req["count_against_user"]) || !filter_var($req["count_against_user"], FILTER_VALIDATE_BOOLEAN)))) ? false : true;
  97. $updating_user_counter = ($serving_range || !$checking_user || ($creating && (!isset($req["count_against_user"]) || !filter_var($req["count_against_user"], FILTER_VALIDATE_BOOLEAN)))) ? false : true;
  98.  
  99. if( /* In either case, the following routines apply whenever we ARE ``$checking_user``. */($serving || $creating) && $checking_user)
  100. {
  101. if(!$using_amazon_storage && !file_exists($GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["files_dir"]."/".$req["file_download"]))
  102. {
  103. if /* We only need this section when/if we're actually serving. */($serving)
  104. {
  105. status_header(404);
  106. header("Content-Type: text/html; charset=UTF-8");
  107. while (@ob_end_clean ()); // Clean any existing output buffers.
  108. exit(_x('<strong>404: Sorry, file not found.</strong> Please contact Support for assistance.', "s2member-front", "s2member"));
  109. }
  110. else // Else return false.
  111. return false;
  112. }
  113.  
  114. else if($req["file_download_key"] && is_string($req["file_download_key"]) && !$valid_file_download_key)
  115. {
  116. if /* We only need this section when/if we're actually serving. */($serving)
  117. {
  118. status_header(503);
  119. header("Content-Type: text/html; charset=UTF-8");
  120. while (@ob_end_clean ()); // Clean any existing output buffers.
  121. exit(_x('<strong>503 (Invalid Key):</strong> Sorry, your access to this file has expired. Please contact Support for assistance.', "s2member-front", "s2member"));
  122. }
  123. else // Else return false.
  124. return false;
  125. }
  126.  
  127. else if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["membership_options_page"] || ($file_downloads_enabled_by_site_owner = $min_level_4_downloads = c_ws_plugin__s2member_files::min_level_4_downloads()) === false)
  128. {
  129. if /* We only need remote functionality when/if we're actually serving. */($serving)
  130. if(!has_filter("ws_plugin__s2member_check_file_download_access_user", "c_ws_plugin__s2member_files_in::check_file_remote_authorization"))
  131. add_filter("ws_plugin__s2member_check_file_download_access_user", "c_ws_plugin__s2member_files_in::check_file_remote_authorization", 10, 2);
  132.  
  133. if /* We only need remote functionality when/if we're actually serving. */($creating)
  134. if(has_filter("ws_plugin__s2member_check_file_download_access_user", "c_ws_plugin__s2member_files_in::check_file_remote_authorization"))
  135. remove_filter("ws_plugin__s2member_check_file_download_access_user", "c_ws_plugin__s2member_files_in::check_file_remote_authorization", 10, 2);
  136.  
  137. if((isset($file_downloads_enabled_by_site_owner, $min_level_4_downloads) && $file_downloads_enabled_by_site_owner === false) || ($file_downloads_enabled_by_site_owner = $min_level_4_downloads = c_ws_plugin__s2member_files::min_level_4_downloads()) === false)
  138. {
  139. if /* We only need this section when/if we're actually serving. */($serving)
  140. {
  141. status_header(503);
  142. header("Content-Type: text/html; charset=UTF-8");
  143. while (@ob_end_clean ()); // Clean any existing output buffers.
  144. exit(_x('<strong>503: Basic File Downloads are NOT enabled yet.</strong> Please contact Support for assistance. If you are the site owner, please configure: <code>s2Member -› Download Options -› Basic Download Restrictions</code>.', "s2member-front", "s2member"));
  145. }
  146. else // Else return false.
  147. return false;
  148. }
  149.  
  150. else if(!is_object($user = apply_filters("ws_plugin__s2member_check_file_download_access_user", ((is_user_logged_in()) ? wp_get_current_user() : false), get_defined_vars())) || empty($user->ID) || !($user_id = $user->ID) || !is_array($user_file_downloads = c_ws_plugin__s2member_files::user_downloads($user)) || (!$user->has_cap("administrator") && (!$user_file_downloads["allowed"] || !$user_file_downloads["allowed_days"])))
  151. {
  152. if(preg_match("/(?:^|\/)access[_\-]s2member[_\-]level([0-9]+)\//", $req["file_download"], $m) && strlen($req_level = $m[1]) && (!is_object($user) || empty($user->ID) || !$user->has_cap("access_s2member_level".$req_level)))
  153. {
  154. if /* We only need this section when/if we're actually serving. */($serving)
  155. c_ws_plugin__s2member_mo_page::wp_redirect_w_mop_vars /* Configure MOP Vars here. */("file", $req["file_download"], "level", $req_level, $_SERVER["REQUEST_URI"]).exit();
  156.  
  157. else // Else return false.
  158. return false;
  159. }
  160.  
  161. else if(preg_match("/(?:^|\/)access[_\-]s2member[_\-]ccap[_\-](.+?)\//", $req["file_download"], $m) && strlen($req_ccap = preg_replace("/-/", "_", $m[1])) && (!is_object($user) || empty($user->ID) || !$user->has_cap("access_s2member_ccap_".$req_ccap)))
  162. {
  163. if /* We only need this section when/if we're actually serving. */($serving)
  164. c_ws_plugin__s2member_mo_page::wp_redirect_w_mop_vars /* Configure MOP Vars here. */("file", $req["file_download"], "ccap", $req_ccap, $_SERVER["REQUEST_URI"]).exit();
  165.  
  166. else // Else return false.
  167. return false;
  168. }
  169.  
  170. else if /* We only need this section when/if we're actually serving. */($serving)
  171. c_ws_plugin__s2member_mo_page::wp_redirect_w_mop_vars /* Configure MOP Vars here. */("file", $req["file_download"], "level", $min_level_4_downloads, $_SERVER["REQUEST_URI"]).exit();
  172.  
  173. else // Else return false.
  174. return false;
  175. }
  176.  
  177. else if(preg_match("/(?:^|\/)access[_\-]s2member[_\-]level([0-9]+)\//", $req["file_download"], $m) && strlen($req_level = $m[1]) && !$user->has_cap("access_s2member_level".$req_level))
  178. {
  179. if /* We only need this section when/if we're actually serving. */($serving)
  180. c_ws_plugin__s2member_mo_page::wp_redirect_w_mop_vars /* Configure MOP Vars here. */("file", $req["file_download"], "level", $req_level, $_SERVER["REQUEST_URI"]).exit();
  181.  
  182. else // Else return false.
  183. return false;
  184. }
  185.  
  186. else if(preg_match("/(?:^|\/)access[_\-]s2member[_\-]ccap[_\-](.+?)\//", $req["file_download"], $m) && strlen($req_ccap = preg_replace("/-/", "_", $m[1])) && !$user->has_cap("access_s2member_ccap_".$req_ccap))
  187. {
  188. if /* We only need this section when/if we're actually serving. */($serving)
  189. c_ws_plugin__s2member_mo_page::wp_redirect_w_mop_vars /* Configure MOP Vars here. */("file", $req["file_download"], "ccap", $req_ccap, $_SERVER["REQUEST_URI"]).exit();
  190.  
  191. else // Else return false.
  192. return false;
  193. }
  194.  
  195. else if /* In either case, the following routines apply. */($serving || $creating)
  196. {
  197. $user_previous_file_downloads = /* Downloads the User has already; in current period/cycle. */ 0;
  198. $user_already_downloaded_this_file = $user_already_downloaded_a_streaming_variation_of_this_file = false;
  199.  
  200. $user_file_download_access_log = (is_array($user_file_download_access_log = get_user_option("s2member_file_download_access_log", $user_id))) ? $user_file_download_access_log : array();
  201. $user_file_download_access_arc = (is_array($user_file_download_access_arc = get_user_option("s2member_file_download_access_arc", $user_id))) ? $user_file_download_access_arc : array();
  202.  
  203. $streaming_file_extns = c_ws_plugin__s2member_utils_strings::preg_quote_deep($GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["streaming_file_extns"], "/");
  204. $streaming_variations = /* Only count one streaming media file variation. */ "/\.(".implode("|", $streaming_file_extns).")$/i";
  205.  
  206. foreach($user_file_download_access_log as $user_file_download_access_log_entry_key => $user_file_download_access_log_entry)
  207. {
  208. if( /* Weed out corrupt/empty log entries. */isset($user_file_download_access_log_entry["date"], $user_file_download_access_log_entry["file"]))
  209. {
  210. if(strtotime($user_file_download_access_log_entry["date"]) < strtotime("-".$user_file_downloads["allowed_days"]." days"))
  211. {
  212. unset /* Remove it from the `log`. */($user_file_download_access_log[$user_file_download_access_log_entry_key]);
  213. $user_file_download_access_arc[] = /* Move `log` entry to the `archive` now. */ $user_file_download_access_log_entry;
  214. }
  215. else if(strtotime($user_file_download_access_log_entry["date"]) >= strtotime("-".$user_file_downloads["allowed_days"]." days"))
  216. {
  217. $user_previous_file_downloads++; // Previous files always count against this User/Member.
  218.  
  219. $_user_file_download_access_log_entry = &$user_file_download_access_log[$user_file_download_access_log_entry_key];
  220. $_user_already_downloaded_this_file = $_user_already_downloaded_a_streaming_variation_of_this_file = false;
  221.  
  222. if /* Already downloaded this file? If yes, mark this flag as true. */($user_file_download_access_log_entry["file"] === $req["file_download"])
  223. $user_already_downloaded_this_file = $_user_already_downloaded_this_file = /* Already downloaded this file? If yes, mark as true. */ true;
  224.  
  225. else if(preg_replace($streaming_variations, "", $user_file_download_access_log_entry["file"]) === preg_replace($streaming_variations, "", $req["file_download"]))
  226. $user_already_downloaded_this_file = $_user_already_downloaded_this_file = $user_already_downloaded_a_streaming_variation_of_this_file = $_user_already_downloaded_a_streaming_variation_of_this_file = true;
  227.  
  228. if( /* Updating counter? */$updating_user_counter && ($_user_already_downloaded_this_file || $_user_already_downloaded_a_streaming_variation_of_this_file))
  229. {
  230. $_user_file_download_access_log_entry /* First, we update the last download time for this file. */["ltime"] = time();
  231.  
  232. if( /* Backward compatibility here. Is this even set? */!empty($user_file_download_access_log_entry["counter"]))
  233. $_user_file_download_access_log_entry["counter"] = (int)$user_file_download_access_log_entry["counter"] + 1;
  234. else // Backward compatibility here. Default value to `1`, if this is NOT even set yet.
  235. $_user_file_download_access_log_entry["counter"] = 1 + 1;
  236. }
  237. }
  238. }
  239. else // Weed out empty log entries. Some older versions of s2Member may have corrupt/empty log entries.
  240. unset /* Remove. */($user_file_download_access_log[$user_file_download_access_log_entry_key]);
  241. }
  242. if( /* Updating counter? */$updating_user_counter && /* Do we need a new log entry for this file? */ !$user_already_downloaded_this_file && !$user_already_downloaded_a_streaming_variation_of_this_file)
  243. $user_file_download_access_log[] = array("date" => date("Y-m-d"), "time" => time(), "ltime" => time(), "file" => $req["file_download"], "counter" => 1);
  244.  
  245. if($user_previous_file_downloads >= $user_file_downloads["allowed"] && !$user_already_downloaded_this_file && !$user_already_downloaded_a_streaming_variation_of_this_file && !$user->has_cap("administrator"))
  246. {
  247. if /* We only need this section when/if we're actually serving. */($serving)
  248. wp_redirect(add_query_arg(urlencode_deep(array("_s2member_seeking" => array("type" => "file", "file" => $req["file_download"], "_uri" => base64_encode($_SERVER["REQUEST_URI"])), "s2member_seeking" => "file-".$req["file_download"])), get_page_link($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["file_download_limit_exceeded_page"])), apply_filters("ws_plugin__s2member_content_redirect_status", 301, get_defined_vars())).exit();
  249.  
  250. else // Else return false.
  251. return false;
  252. }
  253. else if /* Save/update counter? By default, we do NOT update the counter when a URL is simply being created for access. */($updating_user_counter)
  254. update_user_option($user_id, "s2member_file_download_access_log", c_ws_plugin__s2member_utils_arrays::array_unique($user_file_download_access_log)).update_user_option($user_id, "s2member_file_download_access_arc", c_ws_plugin__s2member_utils_arrays::array_unique($user_file_download_access_arc));
  255. }
  256. }
  257. }
  258. else // Otherwise, we're either NOT ``$checking_user``; or permission was granted with a valid File Download Key.
  259. {
  260. if(!$using_amazon_storage && !file_exists($GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["files_dir"]."/".$req["file_download"]))
  261. {
  262. if /* We only need this section when/if we're actually serving. */($serving)
  263. {
  264. status_header(404);
  265. header("Content-Type: text/html; charset=UTF-8");
  266. while (@ob_end_clean ()); // Clean any existing output buffers.
  267. exit(_x('<strong>404: Sorry, file not found.</strong> Please contact Support for assistance.', "s2member-front", "s2member"));
  268. }
  269. else // Else return false.
  270. return false;
  271. }
  272. }
  273.  
  274. if /* In either case, the following routines apply. */($serving || $creating)
  275. {
  276. $basename = basename($req["file_download"]);
  277. $mimetypes = parse_ini_file(dirname(dirname(dirname(__FILE__)))."/includes/mime-types.ini");
  278. $extension = strtolower(substr($req["file_download"], strrpos($req["file_download"], ".") + 1));
  279.  
  280. $key = ($req["file_download_key"] && is_string($req["file_download_key"])) ? $req["file_download_key"] : false;
  281.  
  282. $stream = (isset($req["file_stream"])) ? filter_var($req["file_stream"], FILTER_VALIDATE_BOOLEAN) : ((in_array($extension, preg_split("/[\r\n\t\s;,]+/", $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["file_download_stream_extensions"]))) ? true : false);
  283. $inline = (!$stream && isset($req["file_inline"])) ? filter_var($req["file_inline"], FILTER_VALIDATE_BOOLEAN) : (($stream || in_array($extension, preg_split("/[\r\n\t\s;,]+/", $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["file_download_inline_extensions"]))) ? true : false);
  284. $ssl = (isset($req["file_ssl"])) ? filter_var($req["file_ssl"], FILTER_VALIDATE_BOOLEAN) : ((is_ssl()) ? true : false);
  285. $storage = ($req["file_storage"] && is_string($req["file_storage"])) ? strtolower($req["file_storage"]) : false;
  286. $remote = (isset($req["file_remote"])) ? filter_var($req["file_remote"], FILTER_VALIDATE_BOOLEAN) : false;
  287.  
  288. $_basename_dir_app_data = c_ws_plugin__s2member_utils_dirs::basename_dir_app_data($GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["files_dir"]);
  289. $rewrite_base_guess = (is_dir(dirname($GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["dir"])."/".$_basename_dir_app_data)) ? dirname($GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["dir_url"])."/".$_basename_dir_app_data : content_url("/".$_basename_dir_app_data);
  290. $rewrite_base = ($req["file_rewrite_base"] && is_string($req["file_rewrite_base"])) ? $req["file_rewrite_base"] : false;
  291. $rewrite = $rewriting = (!$rewrite_base && isset($req["file_rewrite"])) ? filter_var($req["file_rewrite"], FILTER_VALIDATE_BOOLEAN) : (($rewrite_base) ? true : false);
  292. unset /* A little housekeeping here. */($_basename_dir_app_data);
  293.  
  294. $skip_confirmation = (isset($req["skip_confirmation"])) ? filter_var($req["skip_confirmation"], FILTER_VALIDATE_BOOLEAN) : false;
  295. $url_to_storage_source = (isset($req["url_to_storage_source"])) ? filter_var($req["url_to_storage_source"], FILTER_VALIDATE_BOOLEAN) : false;
  296.  
  297. $pathinfo = (!$using_amazon_storage) ? pathinfo(($file = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["files_dir"]."/".$req["file_download"])) : array();
  298. $mimetype = ($mimetypes[$extension]) ? $mimetypes[$extension] : "application/octet-stream";
  299. $disposition = (($inline) ? "inline" : "attachment")."; filename=\"".c_ws_plugin__s2member_utils_strings::esc_dq($basename)."\"; filename*=UTF-8''".rawurlencode($basename);
  300. $length = (!$using_amazon_storage && $file) ? filesize($file) : -1;
  301.  
  302. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  303. do_action("ws_plugin__s2member_during_file_download_access", get_defined_vars());
  304. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  305.  
  306. if($using_amazon_storage && $using_amazon_cf_storage && ($serving || ($creating && $url_to_storage_source)))
  307. {
  308. if /* We only need this section when/if we're actually serving. */($serving)
  309. wp_redirect(c_ws_plugin__s2member_files_in::amazon_cf_url($req["file_download"], $stream, $inline, $ssl, $basename, $mimetype)).exit();
  310.  
  311. else // Else return File Download URL.
  312. return apply_filters("ws_plugin__s2member_file_download_access_url", c_ws_plugin__s2member_files_in::amazon_cf_url($req["file_download"], $stream, $inline, $ssl, $basename, $mimetype), get_defined_vars());
  313. }
  314.  
  315. else if($using_amazon_storage && $using_amazon_s3_storage && ($serving || ($creating && $url_to_storage_source)))
  316. {
  317. if /* We only need this section when/if we're actually serving. */($serving)
  318. wp_redirect(c_ws_plugin__s2member_files_in::amazon_s3_url($req["file_download"], $stream, $inline, $ssl, $basename, $mimetype)).exit();
  319.  
  320. else // Else return File Download URL.
  321. return apply_filters("ws_plugin__s2member_file_download_access_url", c_ws_plugin__s2member_files_in::amazon_s3_url($req["file_download"], $stream, $inline, $ssl, $basename, $mimetype), get_defined_vars());
  322. }
  323.  
  324. else if /* Creating a rewrite URL, pointing to local storage. */($creating && $rewriting)
  325. { // Note: we don't URL encode unreserved chars. Improves media player compatibility.
  326. $_url_e_key = ($key) ? c_ws_plugin__s2member_utils_strings::urldecode_ur_chars_deep(urlencode($key)) : "";
  327. $_url_e_storage = ($storage) ? c_ws_plugin__s2member_utils_strings::urldecode_ur_chars_deep(urlencode($storage)) : "";
  328. $_url_e_file = c_ws_plugin__s2member_utils_strings::urldecode_ur_chars_deep(urlencode($req["file_download"]));
  329. $_url_e_file = str_ireplace("%2F", "/", $_url_e_file);
  330.  
  331. $url = ($rewrite_base) ? rtrim($rewrite_base, "/") : rtrim($rewrite_base_guess, "/");
  332. $url .= (isset($req["file_download_key"])) ? (($key && $_url_e_key) ? "/s2member-file-download-key-".$_url_e_key : "") : "";
  333. $url .= (isset($req["file_stream"])) ? (($stream) ? "/s2member-file-stream" : "/s2member-file-stream-no") : "";
  334. $url .= (isset($req["file_inline"])) ? (($inline) ? "/s2member-file-inline" : "/s2member-file-inline-no") : "";
  335. $url .= (isset($req["file_storage"])) ? (($storage && $_url_e_storage) ? "/s2member-file-storage-".$_url_e_storage : "") : "";
  336. $url .= (isset($req["file_remote"])) ? (($remote) ? "/s2member-file-remote" : "/s2member-file-remote-no") : "";
  337. $url .= (isset($req["skip_confirmation"])) ? (($skip_confirmation) ? "/s2member-skip-confirmation" : "/s2member-skip-confirmation-no") : "";
  338.  
  339. $url = /* File Download Access URL via `mod_rewrite` functionality. */ $url."/".$_url_e_file;
  340. $url = ($ssl) ? preg_replace("/^https?/", "https", $url) : preg_replace("/^https?/", "http", $url);
  341.  
  342. return apply_filters("ws_plugin__s2member_file_download_access_url", $url, get_defined_vars());
  343. }
  344.  
  345. else if /* Else we're creating a URL w/ a query-string; w/ local storage. */($creating)
  346. { // Note: we don't URL encode unreserved chars. Improves media player compatibility.
  347. $_url_e_key = ($key) ? c_ws_plugin__s2member_utils_strings::urldecode_ur_chars_deep(urlencode($key)) : "";
  348. $_url_e_storage = ($storage) ? c_ws_plugin__s2member_utils_strings::urldecode_ur_chars_deep(urlencode($storage)) : "";
  349. $_url_e_file = c_ws_plugin__s2member_utils_strings::urldecode_ur_chars_deep(urlencode($req["file_download"]));
  350. $_url_e_file = str_ireplace("%2F", "/", $_url_e_file);
  351.  
  352. $url = (isset($req["file_download_key"])) ? (($key && $_url_e_key) ? "&s2member_file_download_key=".$_url_e_key : "") : "";
  353. $url .= (isset($req["file_stream"])) ? (($stream) ? "&s2member_file_stream=yes" : "&s2member_file_stream=no") : "";
  354. $url .= (isset($req["file_inline"])) ? (($inline) ? "&s2member_file_inline=yes" : "&s2member_file_inline=no") : "";
  355. $url .= (isset($req["file_storage"])) ? (($storage && $_url_e_storage) ? "&s2member_file_storage=".$_url_e_storage : "") : "";
  356. $url .= (isset($req["file_remote"])) ? (($remote) ? "&s2member_file_remote=yes" : "&s2member_file_remote=no") : "";
  357. $url .= (isset($req["skip_confirmation"])) ? (($skip_confirmation) ? "&s2member_skip_confirmation=yes" : "&s2member_skip_confirmation=no") : "";
  358.  
  359. $url = site_url("/?".ltrim($url."&s2member_file_download=/".$_url_e_file, "&"));
  360. $url = ($ssl) ? preg_replace("/^https?/", "https", $url) : preg_replace("/^https?/", "http", $url);
  361.  
  362. return apply_filters("ws_plugin__s2member_file_download_access_url", $url, get_defined_vars());
  363. }
  364.  
  365. else if /* Else, ``if ($serving)``, use local storage. */($serving)
  366. {
  367. @set_time_limit(0);
  368.  
  369. @ini_set("zlib.output_compression", 0);
  370. if(function_exists("apache_setenv"))
  371. @apache_setenv("no-gzip", "1");
  372.  
  373. while /* Cleans existing output buffers. */(@ob_end_clean());
  374.  
  375. if /* Requesting a specific byte range? */($range)
  376. {
  377. if /* Invalid range? */(strpos($range, "=") === FALSE)
  378. {
  379. status_header(416);
  380. nocache_headers();
  381. header("Content-Encoding:");
  382. header("Accept-Ranges: bytes");
  383. header("Content-Type: ".$mimetype);
  384. header("Content-Length: ".$length);
  385. header("Content-Disposition: ".$disposition);
  386. exit /* Stop here (invalid). */();
  387. }
  388. list($range_type, $byte_range) = preg_split("/\s*\=\s*/", $range, 2);
  389.  
  390. $range_type = strtolower(trim($range_type));
  391. $byte_range = trim($byte_range);
  392.  
  393. if /* Invalid range type? */($range_type !== "bytes")
  394. {
  395. status_header(416);
  396. nocache_headers();
  397. header("Content-Encoding:");
  398. header("Accept-Ranges: bytes");
  399. header("Content-Type: ".$mimetype);
  400. header("Content-Length: ".$length);
  401. header("Content-Disposition: ".$disposition);
  402. exit /* Stop here (invalid). */();
  403. }
  404. $byte_ranges = preg_split("/\s*,\s*/", $byte_range);
  405.  
  406. if /* Invalid byte range? */(strpos($byte_ranges[0], "-") === FALSE)
  407. {
  408. status_header(416);
  409. nocache_headers();
  410. header("Content-Encoding:");
  411. header("Accept-Ranges: bytes");
  412. header("Content-Type: ".$mimetype);
  413. header("Content-Length: ".$length);
  414. header("Content-Disposition: ".$disposition);
  415. exit /* Stop here (invalid). */();
  416. }
  417.  
  418. // Only dealing with the first byte range. Others are simply ignored here.
  419. list($byte_range_start, $byte_range_stops) = preg_split("/\s*\-\s*/", $byte_ranges[0], 2);
  420.  
  421. $byte_range_start = trim($byte_range_start);
  422. $byte_range_stops = trim($byte_range_stops);
  423.  
  424. $byte_range_start = ($byte_range_start === "") ? NULL : (integer)$byte_range_start;
  425. $byte_range_stops = ($byte_range_stops === "") ? NULL : (integer)$byte_range_stops;
  426.  
  427. if(!isset($byte_range_start) && $byte_range_stops > 0 && $byte_range_stops <= $length)
  428. {
  429. $byte_range_start = $length - $byte_range_stops;
  430. $byte_range_stops = /* The last X number of bytes. */ $length - 1;
  431. }
  432. else if(!isset($byte_range_stops) && $byte_range_start >= 0 && $byte_range_start < $length - 1)
  433. {
  434. $byte_range_stops = /* To the end of the file in this case. */ $length - 1;
  435. }
  436. else if(isset($byte_range_start, $byte_range_stops) && $byte_range_start >= 0 && $byte_range_start < $length - 1 && $byte_range_stops > $byte_range_start && $byte_range_stops <= $length - 1) {
  437. // Nothing to do in this case, starts/stops already defined properly.
  438. }
  439. else // We have an invalid byte range.
  440. {
  441. status_header(416);
  442. nocache_headers();
  443. header("Content-Encoding:");
  444. header("Accept-Ranges: bytes");
  445. header("Content-Type: ".$mimetype);
  446. header("Content-Length: ".$length);
  447. header("Content-Disposition: ".$disposition);
  448. exit /* Stop here (invalid). */();
  449. }
  450. // Range.
  451. status_header(206);
  452. nocache_headers();
  453. header("Content-Encoding:");
  454. header("Accept-Ranges: bytes");
  455. header("Content-Type: ".$mimetype);
  456. header("Content-Range: bytes ".$byte_range_start."-".$byte_range_stops."/".$length);
  457. $byte_range_size = $byte_range_stops - $byte_range_start + 1;
  458. header("Content-Length: ".$byte_range_size);
  459. header("Content-Disposition: ".$disposition);
  460. }
  461. else // A normal request (NOT a specific byte range).
  462. {
  463. status_header(200);
  464. nocache_headers();
  465. header("Content-Encoding:");
  466. header("Accept-Ranges: bytes");
  467. header("Content-Type: ".$mimetype);
  468. header("Content-Length: ".$length);
  469. header("Content-Disposition: ".$disposition);
  470. }
  471. if(is_resource($resource = fopen($file, "rb")))
  472. {
  473. if($range)
  474. {
  475. $_bytes_to_read = $byte_range_size;
  476. fseek($resource, $byte_range_start);
  477. }
  478. else // Entire file.
  479. $_bytes_to_read = $length;
  480.  
  481. $chunk_size = apply_filters("ws_plugin__s2member_file_downloads_chunk_size", 2097152, get_defined_vars());
  482.  
  483. while /* We have bytes to read here. */($_bytes_to_read)
  484. {
  485. $_bytes_to_read -= ($_reading = ($_bytes_to_read > $chunk_size) ? $chunk_size : $_bytes_to_read);
  486. echo /* Serve file in chunks (default chunk size is 2MB). */ fread($resource, $_reading);
  487. flush /* Flush each chunk to the browser as it is served (avoids high memory consumption). */();
  488. }
  489. fclose /* Close file resource handle. */($resource);
  490. unset /* Housekeeping. */($_bytes_to_read, $_reading);
  491. }
  492. exit /* Stop execution now (the file has been served). */();
  493. }
  494. }
  495. }
  496.  
  497. else if /* We only need this section when/if we're actually serving. */($serving && $req["file_download"])
  498. {
  499. status_header(503);
  500. header("Content-Type: text/html; charset=UTF-8");
  501. while (@ob_end_clean ()); // Clean any existing output buffers.
  502. exit(_x('<strong>503: Access denied.</strong> Invalid File Download specs.', "s2member-front", "s2member"));
  503. }
  504. else if /* We only need this section when/if we're creating a URL. */($creating)
  505. return false;
  506.  
  507. do_action("ws_plugin__s2member_after_file_download_access", get_defined_vars());
  508.  
  509. return ($creating) ? /* If creating, false. */ false : null;
  510. }
  511. /**
  512. * Generates a File Download URL for access to a file protected by s2Member.
  513. *
  514. * @package s2Member\Files
  515. * @since 110926
  516. *
  517. * @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.
  518. * 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`, `check_user`.
  519. * @param bool $get_streamer_array Optional. Defaults to `false`. If `true`, this function will return an array with the following elements: `streamer`, `file`, `url`. For further details, please review this section in your Dashboard: `s2Member -› Download Options -› JW Player® & RTMP Protocol Examples`.
  520. * @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.
  521. *
  522. * @see s2Member\API_Functions\s2member_file_download_url()
  523. */
  524. public static function create_file_download_url($config = FALSE, $get_streamer_array = FALSE)
  525. {
  526. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  527. do_action("ws_plugin__s2member_before_create_file_download_url", get_defined_vars());
  528. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  529.  
  530. $config = (is_array($config)) ? $config : /* This absolutely MUST be an array. */ array();
  531.  
  532. $config["file_download"] = (isset($config["file_download"]) && is_string($config["file_download"])) ? trim($config["file_download"], "/") : @$config["file_download"];
  533. $config["file_download_key"] = (isset($config["file_download"]) && is_string($config["file_download"]) && !empty($config["file_download_key"])) ? c_ws_plugin__s2member_files::file_download_key($config["file_download"], ((in_array($config["file_download_key"], array("ip-forever", "universal", "cache-compatible"))) ? $config["file_download_key"] : false)) : @$config["file_download_key"];
  534.  
  535. $config["url_to_storage_source"] = /* Force a streaming URL here via ``$get_streamer_array``? */ ($get_streamer_array) ? true : @$config["url_to_storage_source"];
  536. $config["file_stream"] = /* Force a streaming URL here via ``$get_streamer_array``? */ ($get_streamer_array) ? true : @$config["file_stream"];
  537.  
  538. if(($_url = c_ws_plugin__s2member_files_in::check_file_download_access /* Successfully created a URL to the file? */(($create_file_download_url = $config))))
  539. {
  540. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  541. do_action("ws_plugin__s2member_during_create_file_download_url", get_defined_vars());
  542. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  543.  
  544. $extension = strtolower(substr($config["file_download"], strrpos($config["file_download"], ".") + 1));
  545. $streaming = (isset($config["file_stream"])) ? filter_var($config["file_stream"], FILTER_VALIDATE_BOOLEAN) : ((in_array($extension, preg_split("/[\r\n\t\s;,]+/", $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["file_download_stream_extensions"]))) ? true : false);
  546. $ssl = (isset($config["file_ssl"])) ? filter_var($config["file_ssl"], FILTER_VALIDATE_BOOLEAN) : ((is_ssl()) ? true : false);
  547.  
  548. if($get_streamer_array && $streaming && ($cfx = "/cfx/st") && ($cfx_pos = strpos($_url, $cfx)) !== false && ($streamer = substr($_url, 0, $cfx_pos + strlen($cfx))) && ($url = c_ws_plugin__s2member_files_in::check_file_download_access(array_merge($config, array("file_stream" => false, "check_user" => false, "count_against_user" => false)))))
  549. $return = array("streamer" => $streamer, "prefix" => $extension.":", "file" => preg_replace("/^".preg_quote($streamer, "/")."\//", "", $_url), "url" => preg_replace("/^.+?\:/", (($ssl) ? "https:" : "http:"), $url));
  550.  
  551. else if($get_streamer_array && $streaming && is_array($ups = c_ws_plugin__s2member_utils_urls::parse_url($_url)) && isset($ups["scheme"], $ups["host"]) && ($streamer = $ups["scheme"]."://".$ups["host"].((!empty($ups["port"])) ? ":".$ups["port"] : "")) && ($url = c_ws_plugin__s2member_files_in::check_file_download_access(array_merge($config, array("file_stream" => false, "check_user" => false, "count_against_user" => false)))))
  552. $return = array("streamer" => $streamer, "prefix" => $extension.":", "file" => preg_replace("/^".preg_quote($streamer, "/")."\//", "", $_url), "url" => preg_replace("/^.+?\:/", (($ssl) ? "https:" : "http:"), $url));
  553.  
  554. else if /* If streamer, we MUST return false here; unable to acquire streamer/file. */($get_streamer_array)
  555. $return = /* We MUST return false here, unable to acquire streamer/file. */ false;
  556.  
  557. else // Else return URL string ( ``$get_streamer_array`` is false ).
  558. $return = /* Else return URL string. */ $_url;
  559. }
  560.  
  561. return apply_filters("ws_plugin__s2member_create_file_download_url", ((isset($return)) ? $return : false), get_defined_vars());
  562. }
  563. /**
  564. * Checks Header Authorization for Remote File Downloads.
  565. *
  566. * @package s2Member\Files
  567. * @since 110926
  568. *
  569. * @attaches-to ``add_filter("ws_plugin__s2member_check_file_download_access_user");``
  570. *
  571. * @param obj $user Expects a WP_User object passed in by the Filter.
  572. * @return obj A `WP_User` object, possibly obtained through Header Authorization.
  573. */
  574. public static function check_file_remote_authorization($user = FALSE)
  575. {
  576. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  577. do_action("ws_plugin__s2member_before_check_file_remote_authorization", get_defined_vars());
  578. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  579.  
  580. $_g = c_ws_plugin__s2member_utils_strings::trim_deep(stripslashes_deep(((!empty($_GET)) ? $_GET : array())));
  581.  
  582. if(!is_object($user) && isset($_g["s2member_file_remote"]) && filter_var($_g["s2member_file_remote"], FILTER_VALIDATE_BOOLEAN))
  583. {
  584. do_action("ws_plugin__s2member_during_check_file_remote_authorization_before", get_defined_vars());
  585.  
  586. if(empty($_SERVER["PHP_AUTH_USER"]) || empty($_SERVER["PHP_AUTH_PW"]) || !user_pass_ok($_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]))
  587. {
  588. header('WWW-Authenticate: Basic realm="'.c_ws_plugin__s2member_utils_strings::esc_dq(strip_tags(_x("Members Only", "s2member-front", "s2member"))).'"');
  589.  
  590. status_header /* Send an unauthorized 401 status header now. */(401);
  591. header /* Content-Type with UTF-8. */("Content-Type: text/html; charset=UTF-8");
  592. while (@ob_end_clean ()); // Clean any existing output buffers.
  593.  
  594. exit(_x('<strong>401:</strong> Sorry, access denied.', "s2member-front", "s2member"));
  595. }
  596. else if(is_object($_user = new WP_User($_SERVER["PHP_AUTH_USER"])) && !empty($_user->ID))
  597. $user = /* Now assign ``$user``. */ $_user;
  598.  
  599. do_action("ws_plugin__s2member_during_check_file_remote_authorization_after", get_defined_vars());
  600. }
  601. return apply_filters("ws_plugin__s2member_check_file_remote_authorization", $user, get_defined_vars());
  602. }
  603. /**
  604. * Checks a File Download Key for validity.
  605. *
  606. * @package s2Member\Files
  607. * @since 110926
  608. *
  609. * @param str $file Input File Download to validate.
  610. * @param str $key Input File Download Key to validate.
  611. * @return bool True if valid, else false.
  612. */
  613. public static function check_file_download_key($file = FALSE, $key = FALSE)
  614. {
  615. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  616. do_action("_ws_plugin__s2member_before_check_file_download_key", get_defined_vars());
  617. unset /* Unset defined __refs, __v. */ ($__refs, $__v);
  618.  
  619. if($file && is_string($file) && ($file = trim($file, "/")) && $key && is_string($key))
  620. {
  621. if($key === c_ws_plugin__s2member_files::file_download_key($file) || $key === c_ws_plugin__s2member_files::file_download_key("/".$file))
  622. $valid = /* File Download Key is valid. */ true;
  623. else if($key === c_ws_plugin__s2member_files::file_download_key($file, "ip-forever") || $key === c_ws_plugin__s2member_files::file_download_key("/".$file, "ip-forever"))
  624. $valid = /* File Download Key is valid. */ true;
  625. else if($key === c_ws_plugin__s2member_files::file_download_key($file, "universal") || $key === c_ws_plugin__s2member_files::file_download_key("/".$file, "universal"))
  626. $valid = /* File Download Key is valid. */ true;
  627. }
  628. return apply_filters("ws_plugin__s2member_check_file_download_key", ((isset($valid) && $valid) ? true : false), get_defined_vars());
  629. }
  630. /**
  631. * Creates an Amazon® S3 HMAC-SHA1 signature.
  632. *
  633. * @package s2Member\Files
  634. * @since 110524RC
  635. *
  636. * @param str $string Input string/data, to be signed by this routine.
  637. * @return str An HMAC-SHA1 signature for Amazon® S3.
  638. */
  639. public static function amazon_s3_sign($string = FALSE)
  640. {
  641. $s3c["secret_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_secret_key"];
  642.  
  643. return c_ws_plugin__s2member_utils_strings::hmac_sha1_sign((string)$string, $s3c["secret_key"]);
  644. }
  645. /**
  646. * Creates an Amazon® S3 HMAC-SHA1 signature URL.
  647. *
  648. * @package s2Member\Files
  649. * @since 110926
  650. *
  651. * @param str $file Input file path, to be signed by this routine.
  652. * @param bool $stream Is this resource file to be served as streaming media?
  653. * @param bool $inline Is this resource file to be served inline, or no?
  654. * @param bool $ssl Is this resource file to be served via SSL, or no?
  655. * @param str $basename The absolute basename of the resource file.
  656. * @param str $mimetype The MIME content-type of the resource file.
  657. * @return str An HMAC-SHA1 signature URL for Amazon® S3.
  658. */
  659. public static function amazon_s3_url($file = FALSE, $stream = FALSE, $inline = FALSE, $ssl = FALSE, $basename = FALSE, $mimetype = FALSE)
  660. {
  661. $file = /* Trim / force string. */ trim((string)$file, "/");
  662. $url_e_file = c_ws_plugin__s2member_utils_strings::urldecode_ur_chars_deep(urlencode($file));
  663. $url_e_file = str_ireplace("%2F", "/", $url_e_file);
  664.  
  665. foreach($GLOBALS["WS_PLUGIN__"]["s2member"]["o"] as $option => $option_value)
  666. if(preg_match("/^amazon_s3_files_/", $option) && ($option = preg_replace("/^amazon_s3_files_/", "", $option)))
  667. $s3c[$option] = $option_value;
  668.  
  669. $s3c["expires"] = strtotime("+".apply_filters("ws_plugin__s2member_amazon_s3_file_expires_time", "24 hours", get_defined_vars()));
  670.  
  671. $s3_file = add_query_arg(c_ws_plugin__s2member_utils_strings::urldecode_ur_chars_deep(urlencode_deep(array("response-cache-control" => ($s3_cache_control = "no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0"), "response-content-disposition" => ($s3_content_disposition = (((bool)$inline) ? "inline" : "attachment").'; filename="'.(string)$basename.'"'), "response-content-type" => ($s3_content_type = (string)$mimetype), "response-expires" => ($s3_expires = gmdate("D, d M Y H:i:s", strtotime("-1 week"))." GMT")))), "/".$url_e_file);
  672. $s3_raw_file = add_query_arg(array("response-cache-control" => $s3_cache_control, "response-content-disposition" => $s3_content_disposition, "response-content-type" => $s3_content_type, "response-expires" => $s3_expires), "/".$url_e_file);
  673. $s3_signature = base64_encode(c_ws_plugin__s2member_files_in::amazon_s3_sign("GET\n\n\n".$s3c["expires"]."\n"."/".$s3c["bucket"].$s3_raw_file));
  674.  
  675. $s3_url = ((strtolower($s3c["bucket"]) !== $s3c["bucket"])) ? "http".(($ssl) ? "s" : "")."://s3.amazonaws.com/".$s3c["bucket"].$s3_file : "http".(($ssl) ? "s" : "")."://".$s3c["bucket"].".s3.amazonaws.com".$s3_file;
  676.  
  677. return add_query_arg(c_ws_plugin__s2member_utils_strings::urldecode_ur_chars_deep (urlencode_deep(array("AWSAccessKeyId" => $s3c["access_key"], "Expires" => $s3c["expires"], "Signature" => $s3_signature))), $s3_url);
  678. }
  679. /**
  680. * Auto-configures an Amazon® S3 Bucket's ACLs.
  681. *
  682. * @package s2Member\Files
  683. * @since 110926
  684. *
  685. * @return array Array containing a true `success` element on success, else a failure array.
  686. * Failure array will contain a failure `code`, and a failure `message`.
  687. */
  688. public static function amazon_s3_auto_configure_acls()
  689. {
  690. foreach($GLOBALS["WS_PLUGIN__"]["s2member"]["o"] as $option => $option_value)
  691. if(preg_match("/^amazon_s3_files_/", $option) && ($option = preg_replace("/^amazon_s3_files_/", "", $option)))
  692. $s3c[$option] = $option_value;
  693.  
  694. $cfc["distros_s3_access_id"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_cf_files_distros_s3_access_id"];
  695.  
  696. if /* Must have Amazon® S3 Bucket/Keys. */($s3c["bucket"] && $s3c["access_key"] && $s3c["secret_key"])
  697. {
  698. $s3_date = gmdate("D, d M Y H:i:s")." GMT";
  699. $s3_location = ((strtolower($s3c["bucket"]) !== $s3c["bucket"])) ? "/".$s3c["bucket"]."/?acl" : "/?acl";
  700. $s3_domain = ((strtolower($s3c["bucket"]) !== $s3c["bucket"])) ? "s3.amazonaws.com" : $s3c["bucket"].".s3.amazonaws.com";
  701. $s3_signature = base64_encode(c_ws_plugin__s2member_files_in::amazon_s3_sign("GET\n\n\n".$s3_date."\n/".$s3c["bucket"]."/?acl"));
  702. $s3_args = array("method" => "GET", "redirection" => 0, "headers" => array("Host" => $s3_domain, "Date" => $s3_date, "Authorization" => "AWS ".$s3c["access_key"].":".$s3_signature));
  703.  
  704. if(($s3_response = c_ws_plugin__s2member_utils_urls::remote("https://".$s3_domain.$s3_location, false, array_merge($s3_args, array("timeout" => 20)), "array")) && $s3_response["code"] === 200)
  705. {
  706. if(preg_match("/\<Owner\>(.+?)\<\/Owner\>/is", $s3_response["body"], $s3_owner_tag) && preg_match("/\<ID\>(.+?)\<\/ID\>/is", $s3_owner_tag[1], $s3_owner_id_tag) && (preg_match("/\<DisplayName\>(.*?)\<\/DisplayName\>/is", $s3_owner_tag[1], $s3_owner_display_name_tag) || ($s3_owner_display_name_tag = array("-", "Owner"))))
  707. {
  708. $s3_owner = array("access_id" => trim($s3_owner_id_tag[1]), "display_name" => trim($s3_owner_display_name_tag[1]));
  709. $s3_acls_xml = '<AccessControlPolicy><Owner><ID>'.esc_html($s3_owner["access_id"]).'</ID><DisplayName>'.esc_html($s3_owner["display_name"]).'</DisplayName></Owner><AccessControlList><Grant><Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser"><ID>'.esc_html($s3_owner["access_id"]).'</ID><DisplayName>'.esc_html($s3_owner["display_name"]).'</DisplayName></Grantee><Permission>FULL_CONTROL</Permission></Grant>'.(($cfc["distros_s3_access_id"]) ? '<Grant><Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser"><ID>'.esc_html($cfc["distros_s3_access_id"]).'</ID><DisplayName>s2Member/CloudFront</DisplayName></Grantee><Permission>READ</Permission></Grant>' : '').'</AccessControlList></AccessControlPolicy>';
  710. $s3_signature = base64_encode(c_ws_plugin__s2member_files_in::amazon_s3_sign("PUT\n\napplication/xml\n".$s3_date."\n/".$s3c["bucket"]."/?acl"));
  711. $s3_args = array("method" => "PUT", "redirection" => 0, "body" => $s3_acls_xml, "headers" => array("Host" => $s3_domain, "Content-Type" => "application/xml", "Date" => $s3_date, "Authorization" => "AWS ".$s3c["access_key"].":".$s3_signature));
  712.  
  713. if(($s3_response = c_ws_plugin__s2member_utils_urls::remote("https://".$s3_domain.$s3_location, false, array_merge($s3_args, array("timeout" => 20)), "array")) && $s3_response["code"] === 200)
  714. {
  715. $s3_location = ((strtolower($s3c["bucket"]) !== $s3c["bucket"])) ? "/".$s3c["bucket"]."/?policy" : "/?policy";
  716. ($s3_policy_id = md5(uniqid("s2Member/CloudFront:", true))).($s3_policy_sid = md5(uniqid("s2Member/CloudFront:", true)));
  717. $s3_policy_json = '{"Version":"2008-10-17","Id":"'.c_ws_plugin__s2member_utils_strings::esc_dq($s3_policy_id).'","Statement":[{"Sid":"'.c_ws_plugin__s2member_utils_strings::esc_dq($s3_policy_sid).'","Effect":"Allow","Principal":{"CanonicalUser":"'.c_ws_plugin__s2member_utils_strings::esc_dq($cfc["distros_s3_access_id"]).'"},"Action":"s3:GetObject","Resource":"arn:aws:s3:::'.c_ws_plugin__s2member_utils_strings::esc_dq($s3c["bucket"]).'/*"}]}';
  718. $s3_signature = base64_encode(c_ws_plugin__s2member_files_in::amazon_s3_sign("PUT\n\napplication/json\n".$s3_date."\n/".$s3c["bucket"]."/?policy"));
  719. $s3_args = array("method" => "PUT", "redirection" => 0, "body" => $s3_policy_json, "headers" => array("Host" => $s3_domain, "Content-Type" => "application/json", "Date" => $s3_date, "Authorization" => "AWS ".$s3c["access_key"].":".$s3_signature));
  720.  
  721. if(!$cfc["distros_s3_access_id"] || (($s3_response = c_ws_plugin__s2member_utils_urls::remote("https://".$s3_domain.$s3_location, false, array_merge($s3_args, array("timeout" => 20)), "array")) && ($s3_response["code"] === 200 || $s3_response["code"] === 204 /* Also OK. */)))
  722. {
  723. $s3_location = ((strtolower($s3c["bucket"]) !== $s3c["bucket"])) ? "/".$s3c["bucket"]."/crossdomain.xml" : "/crossdomain.xml";
  724. $s3_policy_xml = trim(c_ws_plugin__s2member_utilities::evl(file_get_contents(dirname(dirname(__FILE__))."/templates/cfg-files/s2-cross-xml.php")));
  725. $s3_signature = base64_encode(c_ws_plugin__s2member_files_in::amazon_s3_sign("PUT\n\ntext/xml\n".$s3_date."\nx-amz-acl:public-read\n/".$s3c["bucket"]."/crossdomain.xml"));
  726. $s3_args = array("method" => "PUT", "redirection" => 0, "body" => $s3_policy_xml, "headers" => array("Host" => $s3_domain, "Content-Type" => "text/xml", "Date" => $s3_date, "X-Amz-Acl" => "public-read", "Authorization" => "AWS ".$s3c["access_key"].":".$s3_signature));
  727.  
  728. if(($s3_response = c_ws_plugin__s2member_utils_urls::remote("https://".$s3_domain.$s3_location, false, array_merge($s3_args, array("timeout" => 20)), "array")) && $s3_response["code"] === 200)
  729. return /* Successfully configured Amazon® S3 Bucket ACLs and Policy. */ array("success" => true, "code" => null, "message" => null);
  730.  
  731. else if(isset($s3_response["code"], $s3_response["message"]))
  732. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® S3 API call. Feel free to exclude `%s` if you like. */
  733. return array("success" => false, "code" => $s3_response["code"], "message" => sprintf(_x("Unable to update existing Amazon® S3 Cross-Domain Policy. %s", "s2member-admin", "s2member"), $s3_response["message"]));
  734.  
  735. else // Else, we use a default error code and message.
  736. return array("success" => false, "code" => -94, "message" => _x("Unable to update existing Amazon® S3 Cross-Domain Policy. Connection failed.", "s2member-admin", "s2member"));
  737. }
  738. else if(isset($s3_response["code"], $s3_response["message"]))
  739. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® S3 API call. Feel free to exclude `%s` if you like. */
  740. return array("success" => false, "code" => $s3_response["code"], "message" => sprintf(_x("Unable to update existing Amazon® S3 Bucket Policy. %s", "s2member-admin", "s2member"), $s3_response["message"]));
  741.  
  742. else // Else, we use a default error code and message.
  743. return array("success" => false, "code" => -95, "message" => _x("Unable to update existing Amazon® S3 Bucket Policy. Connection failed.", "s2member-admin", "s2member"));
  744. }
  745. else if(isset($s3_response["code"], $s3_response["message"]))
  746. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® S3 API call. Feel free to exclude `%s` if you like. */
  747. return array("success" => false, "code" => $s3_response["code"], "message" => sprintf(_x("Unable to update existing Amazon® S3 Bucket ACLs. %s", "s2member-admin", "s2member"), $s3_response["message"]));
  748.  
  749. else // Else, we use a default error code and message.
  750. return array("success" => false, "code" => -96, "message" => _x("Unable to update existing Amazon® S3 Bucket ACLs. Connection failed.", "s2member-admin", "s2member"));
  751. }
  752. else // Else, we use a default error code and message.
  753. return array("success" => false, "code" => -97, "message" => _x("Unable to acquire/read existing Amazon® S3 Bucket ACLs. Unexpected response.", "s2member-admin", "s2member"));
  754. }
  755. else if(isset($s3_response["code"], $s3_response["message"]))
  756. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® S3 API call. Feel free to exclude `%s` if you like. */
  757. return array("success" => false, "code" => $s3_response["code"], "message" => sprintf(_x("Unable to acquire existing Amazon® S3 Bucket ACLs. %s", "s2member-admin", "s2member"), $s3_response["message"]));
  758.  
  759. else // Else, we use a default error code and message.
  760. return array("success" => false, "code" => -98, "message" => _x("Unable to acquire existing Amazon® S3 Bucket ACLs. Connection failed.", "s2member-admin", "s2member"));
  761. }
  762. else // Else, we use a default error code and message.
  763. return array("success" => false, "code" => -99, "message" => _x("Unable to auto-configure existing Amazon® S3 Bucket ACLs. Incomplete Amazon® S3 configuration options. Missing one of: Amazon® S3 Bucket, Access Key, or Secret Key.", "s2member-admin", "s2member"));
  764. }
  765. /**
  766. * Creates an Amazon® CloudFront HMAC-SHA1 signature.
  767. *
  768. * @package s2Member\Files
  769. * @since 110926
  770. *
  771. * @param str $string Input string/data, to be signed by this routine.
  772. * @return str An HMAC-SHA1 signature for Amazon® CloudFront.
  773. */
  774. public static function amazon_cf_sign($string = FALSE)
  775. {
  776. $cfc["secret_key"] = $s3c["secret_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_secret_key"];
  777.  
  778. return c_ws_plugin__s2member_utils_strings::hmac_sha1_sign((string)$string, ($cfc["secret_key"] = $s3c["secret_key"]));
  779. }
  780. /**
  781. * Creates an Amazon® CloudFront RSA-SHA1 signature.
  782. *
  783. * @package s2Member\Files
  784. * @since 110926
  785. *
  786. * @param str $string Input string/data, to be signed by this routine.
  787. * @return str|bool An RSA-SHA1 signature for Amazon® CloudFront, else false on failure.
  788. */
  789. public static function amazon_cf_rsa_sign($string = FALSE)
  790. {
  791. $cfc["private_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_cf_files_private_key"];
  792.  
  793. return c_ws_plugin__s2member_utils_strings::rsa_sha1_sign((string)$string, $cfc["private_key"]);
  794. }
  795. /**
  796. * Creates an Amazon® CloudFront RSA-SHA1 signature URL.
  797. *
  798. * @package s2Member\Files
  799. * @since 110926
  800. *
  801. * @param str $file Input file path, to be signed by this routine.
  802. * @param bool $stream Is this resource file to be served as streaming media?
  803. * @param bool $inline Is this resource file to be served inline, or no?
  804. * @param bool $ssl Is this resource file to be served via SSL, or no?
  805. * @param str $basename The absolute basename of the resource file.
  806. * @param str $mimetype The MIME content-type of the resource file.
  807. * @return str An RSA-SHA1 signature URL for Amazon® CloudFront.
  808. */
  809. public static function amazon_cf_url($file = FALSE, $stream = FALSE, $inline = FALSE, $ssl = FALSE, $basename = FALSE, $mimetype = FALSE)
  810. {
  811. $file = /* Trim & force string. */ trim((string)$file, "/");
  812. $url_e_file = c_ws_plugin__s2member_utils_strings::urldecode_ur_chars_deep(urlencode($file));
  813. $url_e_file = str_ireplace("%2F", "/", $url_e_file);
  814.  
  815. foreach($GLOBALS["WS_PLUGIN__"]["s2member"]["o"] as $option => $option_value)
  816. if(preg_match("/^amazon_cf_files_/", $option) && ($option = preg_replace("/^amazon_cf_files_/", "", $option)))
  817. $cfc[$option] = $option_value;
  818.  
  819. $cfc["expires"] = strtotime("+".apply_filters("ws_plugin__s2member_amazon_cf_file_expires_time", "24 hours", get_defined_vars()));
  820.  
  821. $cf_extn = /* Parses the file extension out so we can scan it in some special scenarios. */ strtolower(substr($file, strrpos($file, ".") + 1));
  822. $cf_ip_res = /* Do NOT restrict access to a particular IP during `localhost` development. The IP may NOT be the same one Amazon® CloudFront sees. */ (c_ws_plugin__s2member_utils_conds::is_localhost()) ? false : true;
  823. $cf_stream_extn_resource_exclusions = array_unique((array)apply_filters("ws_plugin__s2member_amazon_cf_file_streaming_extension_resource_exclusions", array("mp3" /* MP3 files should NOT include an extension in their resource reference. */), get_defined_vars()));
  824. $cf_resource = ($stream) ? ((in_array($cf_extn, $cf_stream_extn_resource_exclusions)) ? substr($file, 0, strrpos($file, ".")) : $file) : "http".(($ssl) ? "s" : "")."://".(($cfc["distro_downloads_cname"]) ? $cfc["distro_downloads_cname"] : $cfc["distro_downloads_dname"])."/".$url_e_file;
  825. $cf_url = ($stream) ? "rtmp".(($ssl) ? "e" : "")."://".(($cfc["distro_streaming_cname"]) ? $cfc["distro_streaming_cname"] : $cfc["distro_streaming_dname"])."/cfx/st/".$file : "http".(($ssl) ? "s" : "")."://".(($cfc["distro_downloads_cname"]) ? $cfc["distro_downloads_cname"] : $cfc["distro_downloads_dname"])."/".$url_e_file;
  826. $cf_policy = '{"Statement":[{"Resource":"'.c_ws_plugin__s2member_utils_strings::esc_dq($cf_resource).'","Condition":{'.(($cf_ip_res) ? '"IpAddress":{"AWS:SourceIp":"'.c_ws_plugin__s2member_utils_strings::esc_dq($_SERVER["REMOTE_ADDR"]).'/32"},' : '').'"DateLessThan":{"AWS:EpochTime":'.(int)$cfc["expires"].'}}}]}';
  827.  
  828. $cf_signature = c_ws_plugin__s2member_files_in::amazon_cf_rsa_sign($cf_policy);
  829. $cf_base64_url_safe_policy = c_ws_plugin__s2member_utils_strings::base64_url_safe_encode($cf_policy, array("+", "=", "/"), array("-", "_", "~"), false);
  830. $cf_base64_url_safe_signature = c_ws_plugin__s2member_utils_strings::base64_url_safe_encode($cf_signature, array("+", "=", "/"), array("-", "_", "~"), false);
  831.  
  832. return add_query_arg(c_ws_plugin__s2member_utils_strings::urldecode_ur_chars_deep(urlencode_deep(array("Policy" => $cf_base64_url_safe_policy, "Signature" => $cf_base64_url_safe_signature, "Key-Pair-Id" => $cfc["private_key_id"]))), $cf_url);
  833. }
  834. /**
  835. * Auto-configures Amazon® S3/CloudFront distros.
  836. *
  837. * @package s2Member\Files
  838. * @since 110926
  839. *
  840. * @return array Array containing a true `success` element on success, else a failure array.
  841. * Failure array will contain a failure `code`, and a failure `message`.
  842. */
  843. public static function amazon_cf_auto_configure_distros()
  844. {
  845. foreach($GLOBALS["WS_PLUGIN__"]["s2member"]["o"] as $option => $option_value)
  846. if(preg_match("/^amazon_cf_files_/", $option) && ($option = preg_replace("/^amazon_cf_files_/", "", $option)))
  847. $cfc[$option] = $option_value;
  848.  
  849. $s3c["bucket"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_bucket"];
  850. $cfc["access_key"] = $s3c["access_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_access_key"];
  851. $cfc["secret_key"] = $s3c["secret_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_secret_key"];
  852.  
  853. if /* We MUST have an Amazon® S3 Bucket and Keys. */($s3c["bucket"] && $s3c["access_key"] && $s3c["secret_key"])
  854. {
  855. if /* We MUST have Amazon® CloudFront Keys in order to auto-configure. */($cfc["private_key"] && $cfc["private_key_id"])
  856. {
  857. if(!$cfc["distro_downloads_id"] || ($cfc["distro_downloads_id"] && ($cf_get_response = c_ws_plugin__s2member_files_in::amazon_cf_get_distro($cfc["distro_downloads_id"], "downloads")) && ($cf_get_response["success"] || $cf_get_response["code"] === 404)))
  858. {
  859. if(!$cfc["distro_downloads_id"] || ($cfc["distro_downloads_id"] && $cf_get_response && !$cf_get_response["success"] && $cf_get_response["code"] === 404))
  860. $cf_distro_downloads_clear = /* Clear, ready for a new one. */ true;
  861.  
  862. else if($cfc["distro_downloads_id"] && $cf_get_response && $cf_get_response["success"] && !$cf_get_response["deployed"])
  863. return array("success" => false, "code" => -86, "message" => _x("Unable to delete existing Amazon® CloudFront Downloads Distro. Still in a `pending` state. Please wait 15 minutes, then try again. There is a certain process that s2Member must strictly adhere to when re-configuring your Amazon® CloudFront Distros. You may have to tick the auto-configure checkbox again, and re-run s2Member's auto-configuration routine many times, because s2Member will likely run into several `pending` challenges, as it works to completely re-configure your Amazon® CloudFront Distros for you. Thanks for your patience. Please wait 15 minutes, then try again.", "s2member-admin", "s2member"));
  864.  
  865. else if($cfc["distro_downloads_id"] && $cf_get_response && $cf_get_response["success"] && $cf_get_response["deployed"] && ($cf_del_response = c_ws_plugin__s2member_files_in::amazon_cf_del_distro($cfc["distro_downloads_id"], $cf_get_response["etag"], $cf_get_response["xml"])) && $cf_del_response["success"])
  866. $cf_distro_downloads_clear = /* Clear, ready for a new one. */ true;
  867.  
  868. else if(isset($cf_del_response["code"], $cf_del_response["message"]))
  869. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  870. return array("success" => false, "code" => $cf_del_response["code"], "message" => sprintf(_x("Unable to delete existing Amazon® CloudFront Downloads Distro. %s", "s2member-admin", "s2member"), $cf_del_response["message"]));
  871.  
  872. if /* Successfully cleared? Ready for a new one? */(isset($cf_distro_downloads_clear) && $cf_distro_downloads_clear)
  873. {
  874. unset /* Unset these before processing additional routines. Prevents problems in error reporting. */($cf_get_response, $cf_del_response);
  875.  
  876. if(!$cfc["distro_streaming_id"] || ($cfc["distro_streaming_id"] && ($cf_get_response = c_ws_plugin__s2member_files_in::amazon_cf_get_distro($cfc["distro_streaming_id"], "streaming")) && ($cf_get_response["success"] || $cf_get_response["code"] === 404)))
  877. {
  878. if(!$cfc["distro_streaming_id"] || ($cfc["distro_streaming_id"] && $cf_get_response && !$cf_get_response["success"] && $cf_get_response["code"] === 404))
  879. $cf_distro_streaming_clear = /* Clear, ready for a new one. */ true;
  880.  
  881. else if($cfc["distro_streaming_id"] && $cf_get_response && $cf_get_response["success"] && !$cf_get_response["deployed"])
  882. return array("success" => false, "code" => -87, "message" => _x("Unable to delete existing Amazon® CloudFront Streaming Distro. Still in a `pending` state. Please wait 15 minutes, then try again. There is a certain process that s2Member must strictly adhere to when re-configuring your Amazon® CloudFront Distros. You may have to tick the auto-configure checkbox again, and re-run s2Member's auto-configuration routine many times, because s2Member will likely run into several `pending` challenges, as it works to completely re-configure your Amazon® CloudFront Distros for you. Thanks for your patience. Please wait 15 minutes, then try again.", "s2member-admin", "s2member"));
  883.  
  884. else if($cfc["distro_streaming_id"] && $cf_get_response && $cf_get_response["success"] && $cf_get_response["deployed"] && ($cf_del_response = c_ws_plugin__s2member_files_in::amazon_cf_del_distro($cfc["distro_streaming_id"], $cf_get_response["etag"], $cf_get_response["xml"])) && $cf_del_response["success"])
  885. $cf_distro_streaming_clear = /* Clear, ready for a new one. */ true;
  886.  
  887. else if(isset($cf_del_response["code"], $cf_del_response["message"]))
  888. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  889. return array("success" => false, "code" => $cf_del_response["code"], "message" => sprintf(_x("Unable to delete existing Amazon® CloudFront Streaming Distro. %s", "s2member-admin", "s2member"), $cf_del_response["message"]));
  890.  
  891. if /* Successfully cleared? Ready for a new one? */(isset($cf_distro_streaming_clear) && $cf_distro_streaming_clear)
  892. {
  893. unset /* Unset these before processing additional routines. Prevents problems in error reporting. */($cf_get_response, $cf_del_response);
  894.  
  895. if(!$cfc["distros_access_id"] || ($cfc["distros_access_id"] && ($cf_get_response = c_ws_plugin__s2member_files_in::amazon_cf_get_access_origin_identity($cfc["distros_access_id"])) && ($cf_get_response["success"] || $cf_get_response["code"] === 404)))
  896. {
  897. if(!$cfc["distros_access_id"] || ($cfc["distros_access_id"] && $cf_get_response && !$cf_get_response["success"] && $cf_get_response["code"] === 404))
  898. $cf_distros_access_clear = /* Clear, ready for a new one. */ true;
  899.  
  900. else if($cfc["distros_access_id"] && $cf_get_response && $cf_get_response["success"] && ($cf_del_response = c_ws_plugin__s2member_files_in::amazon_cf_del_access_origin_identity($cfc["distros_access_id"], $cf_get_response["etag"], $cf_get_response["xml"])) && $cf_del_response["success"])
  901. $cf_distros_access_clear = /* Clear, ready for a new one. */ true;
  902.  
  903. else if(isset($cf_del_response["code"], $cf_del_response["message"]))
  904. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  905. return array("success" => false, "code" => $cf_del_response["code"], "message" => sprintf(_x("Unable to delete existing Amazon® CloudFront Origin Access Identity. %s", "s2member-admin", "s2member"), $cf_del_response["message"]));
  906.  
  907. if /* Successfully cleared? Ready for a new one? */(isset($cf_distros_access_clear) && $cf_distros_access_clear)
  908. {
  909. unset /* Unset these before processing additional routines. Prevents problems in error reporting. */($cf_get_response, $cf_del_response);
  910.  
  911. $cfc = array_merge($cfc, array("distros_access_id" => "", "distros_s3_access_id" => "", "distro_downloads_id" => "", "distro_downloads_dname" => "", "distro_streaming_id" => "", "distro_streaming_dname" => "", "distros_auto_config_status" => ""));
  912. $cf_options = array("ws_plugin__s2member_amazon_cf_files_distros_access_id" => "", "ws_plugin__s2member_amazon_cf_files_distros_s3_access_id" => "", "ws_plugin__s2member_amazon_cf_files_distro_downloads_id" => "", "ws_plugin__s2member_amazon_cf_files_distro_downloads_dname" => "", "ws_plugin__s2member_amazon_cf_files_distro_streaming_id" => "", "ws_plugin__s2member_amazon_cf_files_distro_streaming_dname" => "", "ws_plugin__s2member_amazon_cf_files_distros_auto_config_status" => "");
  913. c_ws_plugin__s2member_menu_pages::update_all_options($cf_options, true, false, false, false, false);
  914.  
  915. if(($cf_response = c_ws_plugin__s2member_files_in::amazon_cf_create_distros_access_origin_identity()) && $cf_response["success"])
  916. {
  917. $cfc = array_merge($cfc, array("distros_access_id" => $cf_response["distros_access_id"], "distros_s3_access_id" => $cf_response["distros_s3_access_id"]));
  918. $cf_options = array("ws_plugin__s2member_amazon_cf_files_distros_access_id" => $cf_response["distros_access_id"], "ws_plugin__s2member_amazon_cf_files_distros_s3_access_id" => $cf_response["distros_s3_access_id"]);
  919. c_ws_plugin__s2member_menu_pages::update_all_options($cf_options, true, false, false, false, false);
  920.  
  921. if(($cf_response = c_ws_plugin__s2member_files_in::amazon_cf_create_distro("downloads")) && $cf_response["success"])
  922. {
  923. $cfc = array_merge($cfc, array("distro_downloads_id" => $cf_response["distro_downloads_id"], "distro_downloads_dname" => $cf_response["distro_downloads_dname"]));
  924. $cf_options = array("ws_plugin__s2member_amazon_cf_files_distro_downloads_id" => $cf_response["distro_downloads_id"], "ws_plugin__s2member_amazon_cf_files_distro_downloads_dname" => $cf_response["distro_downloads_dname"]);
  925. c_ws_plugin__s2member_menu_pages::update_all_options($cf_options, true, false, false, false, false);
  926.  
  927. if(($cf_response = c_ws_plugin__s2member_files_in::amazon_cf_create_distro("streaming")) && $cf_response["success"])
  928. {
  929. $cfc = array_merge($cfc, array("distro_streaming_id" => $cf_response["distro_streaming_id"], "distro_streaming_dname" => $cf_response["distro_streaming_dname"]));
  930. $cf_options = array("ws_plugin__s2member_amazon_cf_files_distro_streaming_id" => $cf_response["distro_streaming_id"], "ws_plugin__s2member_amazon_cf_files_distro_streaming_dname" => $cf_response["distro_streaming_dname"]);
  931. c_ws_plugin__s2member_menu_pages::update_all_options($cf_options, true, false, false, false, false);
  932.  
  933. for($a = 1, $attempts = 4, $sleep = 2, sleep($sleep); $a <= $attempts; $a++, (($a <= $attempts) ? sleep($sleep) : null))
  934. /* Allow a generous propagation time here. Amazon's high-availability services do NOT guarantee real-time updates.
  935. Since we DO need a fully propagated Origin Access Identity now, we need to make several attempts at success.
  936. For further details, please see this thread: <https://forums.aws.amazon.com/message.jspa?messageID=42875>. */
  937. if(($s3_response = c_ws_plugin__s2member_files_in::amazon_s3_auto_configure_acls()) && $s3_response["success"])
  938. {
  939. $cfc = array_merge($cfc, array("distros_auto_config_status" => "configured"));
  940. $cf_options = array("ws_plugin__s2member_amazon_cf_files_distros_auto_config_status" => "configured");
  941. c_ws_plugin__s2member_menu_pages::update_all_options( /* Now configured! */$cf_options, true, false, false, false, false);
  942. return /* Successfully configured Amazon® S3/CloudFront distros. */ array("success" => true, "code" => null, "message" => null);
  943. }
  944. if(isset($s3_response["code"], $s3_response["message"]))
  945. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® S3 API call. Feel free to exclude `%s` if you like. */
  946. return array("success" => false, "code" => $s3_response["code"], "message" => sprintf(_x("Unable to update existing Amazon® S3 ACLs. %s", "s2member-admin", "s2member"), $s3_response["message"]));
  947.  
  948. else // Else, we use a default error code and message.
  949. return array("success" => false, "code" => -88, "message" => _x("Unable to update existing Amazon® S3 ACLs. Connection failed.", "s2member-admin", "s2member"));
  950. }
  951. else if(isset($cf_response["code"], $cf_response["message"]))
  952. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  953. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to create Amazon® CloudFront Streaming Distro. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  954.  
  955. else // Else, we use a default error code and message.
  956. return array("success" => false, "code" => -89, "message" => _x("Unable to create Amazon® CloudFront Streaming Distro. Connection failed.", "s2member-admin", "s2member"));
  957. }
  958. else if(isset($cf_response["code"], $cf_response["message"]))
  959. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  960. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to create Amazon® CloudFront Downloads Distro. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  961.  
  962. else // Else, we use a default error code and message.
  963. return array("success" => false, "code" => -90, "message" => _x("Unable to create Amazon® CloudFront Downloads Distro. Connection failed.", "s2member-admin", "s2member"));
  964. }
  965. else if(isset($cf_response["code"], $cf_response["message"]))
  966. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  967. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to create Amazon® CloudFront Origin Access Identity. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  968.  
  969. else // Else, we use a default error code and message.
  970. return array("success" => false, "code" => -91, "message" => _x("Unable to create Amazon® CloudFront Origin Access Identity. Connection failed.", "s2member-admin", "s2member"));
  971. }
  972. else // Else, we use a default error code and message.
  973. return array("success" => false, "code" => -92, "message" => _x("Unable to clear existing Amazon® CloudFront Origin Access Identity.", "s2member-admin", "s2member"));
  974. }
  975. else if(isset($cf_get_response["code"], $cf_get_response["message"]))
  976. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  977. return array("success" => false, "code" => $cf_get_response["code"], "message" => sprintf(_x("Unable to acquire existing Amazon® CloudFront Origin Access Identity. %s", "s2member-admin", "s2member"), $cf_get_response["message"]));
  978.  
  979. else // Else, we use a default error code and message.
  980. return array("success" => false, "code" => -93, "message" => _x("Unable to acquire existing Amazon® CloudFront Origin Access Identity. Connection failed.", "s2member-admin", "s2member"));
  981. }
  982. else // Else, we use a default error code and message.
  983. return array("success" => false, "code" => -94, "message" => _x("Unable to clear existing Amazon® CloudFront Streaming Distro.", "s2member-admin", "s2member"));
  984. }
  985. else if(isset($cf_get_response["code"], $cf_get_response["message"]))
  986. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  987. return array("success" => false, "code" => $cf_get_response["code"], "message" => sprintf(_x("Unable to acquire existing Amazon® CloudFront Streaming Distro. %s", "s2member-admin", "s2member"), $cf_get_response["message"]));
  988.  
  989. else // Else, we use a default error code and message.
  990. return array("success" => false, "code" => -95, "message" => _x("Unable to acquire existing Amazon® CloudFront Streaming Distro. Connection failed.", "s2member-admin", "s2member"));
  991. }
  992. else // Else, we use a default error code and message.
  993. return array("success" => false, "code" => -96, "message" => _x("Unable to clear existing Amazon® CloudFront Downloads Distro.", "s2member-admin", "s2member"));
  994. }
  995. else if(isset($cf_get_response["code"], $cf_get_response["message"]))
  996. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  997. return array("success" => false, "code" => $cf_get_response["code"], "message" => sprintf(_x("Unable to acquire existing Amazon® CloudFront Downloads Distro. %s", "s2member-admin", "s2member"), $cf_get_response["message"]));
  998.  
  999. else // Else, we use a default error code and message.
  1000. return array("success" => false, "code" => -97, "message" => _x("Unable to acquire existing Amazon® CloudFront Downloads Distro. Connection failed.", "s2member-admin", "s2member"));
  1001. }
  1002. else // Else, we use a default error code and message.
  1003. return array("success" => false, "code" => -98, "message" => _x("Unable to auto-configure Amazon® CloudFront Distros. Incomplete Amazon® CloudFront configuration options. Missing of one: Amazon® CloudFront Private Key-Pair-ID, or Private Key file contents.", "s2member-admin", "s2member"));
  1004. }
  1005. else // Else, we use a default error code and message.
  1006. return array("success" => false, "code" => -99, "message" => _x("Unable to auto-configure Amazon® S3/CloudFront Distros. Incomplete Amazon® S3 configuration options. Missing one of: Amazon® S3 Bucket, Access Key, or Secret Key. You must provide s2Member with an Amazon® S3 configuration before enabling CloudFront.", "s2member-admin", "s2member"));
  1007. }
  1008. /**
  1009. * Acquires an Amazon® S3/CloudFront Access Origin Identity.
  1010. *
  1011. * @package s2Member\Files
  1012. * @since 110926
  1013. *
  1014. * @param str $access_id Required. An Origin Access ID.
  1015. * @return array Array containing a true `success` and `etag`, `xml` elements on success, else a failure array.
  1016. * Failure array will contain a failure `code`, and a failure `message`.
  1017. */
  1018. public static function amazon_cf_get_access_origin_identity($access_id = FALSE)
  1019. {
  1020. if /* Valid parameters? */($access_id && is_string($access_id))
  1021. {
  1022. foreach($GLOBALS["WS_PLUGIN__"]["s2member"]["o"] as $option => $option_value)
  1023. if(preg_match("/^amazon_cf_files_/", $option) && ($option = preg_replace("/^amazon_cf_files_/", "", $option)))
  1024. $cfc[$option] = $option_value;
  1025.  
  1026. $s3c["bucket"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_bucket"];
  1027. $cfc["access_key"] = $s3c["access_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_access_key"];
  1028. $cfc["secret_key"] = $s3c["secret_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_secret_key"];
  1029.  
  1030. $cf_domain = "cloudfront.amazonaws.com";
  1031. $cf_date = gmdate("D, d M Y H:i:s")." GMT";
  1032. $cf_location = "/2010-11-01/origin-access-identity/cloudfront/".$access_id;
  1033. $cf_signature = base64_encode(c_ws_plugin__s2member_files_in::amazon_cf_sign($cf_date));
  1034. $cf_args = array("method" => "GET", "redirection" => 0, "headers" => array("Host" => $cf_domain, "Date" => $cf_date, "Authorization" => "AWS ".$cfc["access_key"].":".$cf_signature));
  1035.  
  1036. if(($cf_response = c_ws_plugin__s2member_utils_urls::remote("https://".$cf_domain.$cf_location, false, array_merge($cf_args, array("timeout" => 20)), "array")) && (($cf_response["code"] === 404 && $cf_response["message"]) || ($cf_response["code"] === 200 && !empty($cf_response["headers"]["etag"]) && !empty($cf_response["body"]))))
  1037. {
  1038. if($cf_response["code"] === 200 && !empty($cf_response["headers"]["etag"]) && !empty($cf_response["body"]))
  1039. return array("success" => true, "code" => null, "message" => null, "etag" => trim($cf_response["headers"]["etag"]), "xml" => trim($cf_response["body"]));
  1040.  
  1041. else /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1042. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Existing Amazon® CloudFront Origin Access Identity NOT found. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  1043. }
  1044. else if(isset($cf_response["code"], $cf_response["message"]))
  1045. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1046. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to acquire existing Amazon® CloudFront Origin Access Identity. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  1047.  
  1048. else // Else, we use a default error code and message.
  1049. return array("success" => false, "code" => -98, "message" => _x("Unable to acquire existing Amazon® CloudFront Origin Access Identity. Connection failed.", "s2member-admin", "s2member"));
  1050. }
  1051. else // Else, we use a default error code and message.
  1052. return array("success" => false, "code" => -99, "message" => _x("Unable to acquire existing Amazon® CloudFront Origin Access Identity. Invalid Access ID.", "s2member-admin", "s2member"));
  1053. }
  1054. /**
  1055. * Deletes an Amazon® S3/CloudFront Access Origin Identity.
  1056. *
  1057. * @package s2Member\Files
  1058. * @since 110926
  1059. *
  1060. * @param str $access_id Required. An Origin Access ID.
  1061. * @param str $access_id_etag Required. An Origin Access ETag header.
  1062. * @param str $access_id_xml Required. An Origin Access Identity's XML configuration.
  1063. * @return array Array containing a true `success` element on success, else a failure array.
  1064. * Failure array will contain a failure `code`, and a failure `message`.
  1065. */
  1066. public static function amazon_cf_del_access_origin_identity($access_id = FALSE, $access_id_etag = FALSE, $access_id_xml = FALSE)
  1067. {
  1068. if($access_id && is_string($access_id) && $access_id_etag && is_string($access_id_etag) && $access_id_xml && is_string($access_id_xml))
  1069. {
  1070. foreach($GLOBALS["WS_PLUGIN__"]["s2member"]["o"] as $option => $option_value)
  1071. if(preg_match("/^amazon_cf_files_/", $option) && ($option = preg_replace("/^amazon_cf_files_/", "", $option)))
  1072. $cfc[$option] = $option_value;
  1073.  
  1074. $s3c["bucket"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_bucket"];
  1075. $cfc["access_key"] = $s3c["access_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_access_key"];
  1076. $cfc["secret_key"] = $s3c["secret_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_secret_key"];
  1077.  
  1078. $cf_domain = "cloudfront.amazonaws.com";
  1079. $cf_date = gmdate("D, d M Y H:i:s")." GMT";
  1080. $cf_location = "/2010-11-01/origin-access-identity/cloudfront/".$access_id;
  1081. $cf_signature = base64_encode(c_ws_plugin__s2member_files_in::amazon_cf_sign($cf_date));
  1082. $cf_args = array("method" => "DELETE", "redirection" => 0, "headers" => array("Host" => $cf_domain, "Date" => $cf_date, "If-Match" => $access_id_etag, "Authorization" => "AWS ".$cfc["access_key"].":".$cf_signature));
  1083.  
  1084. if(($cf_response = c_ws_plugin__s2member_utils_urls::remote("https://".$cf_domain.$cf_location, false, array_merge($cf_args, array("timeout" => 20)), "array")) && ($cf_response["code"] === 200 || $cf_response["code"] === 204 /* Deleted. */))
  1085. return /* Deleted successfully. */ array("success" => true, "code" => null, "message" => null);
  1086.  
  1087. else if(isset($cf_response["code"], $cf_response["message"]))
  1088. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1089. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to delete existing Amazon® CloudFront Origin Access Identity. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  1090.  
  1091. else // Else, we use a default error code and message.
  1092. return array("success" => false, "code" => -98, "message" => _x("Unable to delete existing Amazon® CloudFront Origin Access Identity. Connection failed.", "s2member-admin", "s2member"));
  1093. }
  1094. else // Else, we use a default error code and message.
  1095. return array("success" => false, "code" => -99, "message" => _x("Unable to delete existing Amazon® CloudFront Origin Access Identity. Invalid Access ID, ETag, or XML config.", "s2member-admin", "s2member"));
  1096. }
  1097. /**
  1098. * Creates an Amazon® S3/CloudFront Access Origin Identity for all Distros.
  1099. *
  1100. * @package s2Member\Files
  1101. * @since 110926
  1102. *
  1103. * @return array Array containing a true `success` and `distros_access_id`, `distros_s3_access_id` elements on success, else a failure array.
  1104. * Failure array will contain a failure `code`, and a failure `message`.
  1105. */
  1106. public static function amazon_cf_create_distros_access_origin_identity()
  1107. {
  1108. foreach($GLOBALS["WS_PLUGIN__"]["s2member"]["o"] as $option => $option_value)
  1109. if(preg_match("/^amazon_cf_files_/", $option) && ($option = preg_replace("/^amazon_cf_files_/", "", $option)))
  1110. $cfc[$option] = $option_value;
  1111.  
  1112. $s3c["bucket"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_bucket"];
  1113. $cfc["access_key"] = $s3c["access_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_access_key"];
  1114. $cfc["secret_key"] = $s3c["secret_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_secret_key"];
  1115.  
  1116. $cf_domain = "cloudfront.amazonaws.com";
  1117. $cf_date = gmdate("D, d M Y H:i:s")." GMT";
  1118. $cf_location = "/2010-11-01/origin-access-identity/cloudfront";
  1119. $cf_signature = base64_encode(c_ws_plugin__s2member_files_in::amazon_cf_sign($cf_date));
  1120. $cf_distros_access_reference = time().".".md5("access".$s3c["bucket"].$s3c["access_key"].$s3c["secret_key"].$cfc["private_key"].$cfc["private_key_id"]);
  1121. $cf_distros_access_xml = '<?xml version="1.0" encoding="UTF-8"?><CloudFrontOriginAccessIdentityConfig xmlns="http://cloudfront.amazonaws.com/doc/2010-11-01/"><CallerReference>'.esc_html($cf_distros_access_reference).'</CallerReference><Comment>'.esc_html(sprintf(_x("Created by s2Member, for S3 Bucket: %s.", "s2member-admin", "s2member"), $s3c["bucket"])).'</Comment></CloudFrontOriginAccessIdentityConfig>';
  1122. $cf_args = array("method" => "POST", "redirection" => 0, "body" => $cf_distros_access_xml, "headers" => array("Host" => $cf_domain, "Content-Type" => "application/xml", "Date" => $cf_date, "Authorization" => "AWS ".$cfc["access_key"].":".$cf_signature));
  1123.  
  1124. if(($cf_response = c_ws_plugin__s2member_utils_urls::remote("https://".$cf_domain.$cf_location, false, array_merge($cf_args, array("timeout" => 20)), "array")) && ($cf_response["code"] === 200 || $cf_response["code"] === 201 /* Created. */))
  1125. {
  1126. if(preg_match("/\<CloudFrontOriginAccessIdentity.*?\>(.+?)\<\/CloudFrontOriginAccessIdentity\>/is", $cf_response["body"], $cf_distros_access_tag) && preg_match("/\<Id\>(.+?)\<\/Id\>/is", $cf_distros_access_tag[1], $cf_distros_access_id_tag) && preg_match("/\<S3CanonicalUserId\>(.+?)\<\/S3CanonicalUserId\>/is", $cf_distros_access_tag[1], $cf_distros_s3_access_id_tag))
  1127. return array("success" => true, "code" => null, "message" => null, "distros_access_id" => trim($cf_distros_access_id_tag[1]), "distros_s3_access_id" => trim($cf_distros_s3_access_id_tag[1]));
  1128.  
  1129. else // Else, we use a default error code and message.
  1130. return array("success" => false, "code" => -98, "message" => _x("Unable to create/read Amazon® CloudFront Origin Access Identity. Unexpected response.", "s2member-admin", "s2member"));
  1131. }
  1132. else if(isset($cf_response["code"], $cf_response["message"]))
  1133. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1134. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to create Amazon® CloudFront Origin Access Identity. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  1135.  
  1136. else // Else, we use a default error code and message.
  1137. return array("success" => false, "code" => -99, "message" => _x("Unable to create Amazon® CloudFront Origin Access Identity. Connection failed.", "s2member-admin", "s2member"));
  1138. }
  1139. /**
  1140. * Acquires an Amazon® S3/CloudFront Distro.
  1141. *
  1142. * @package s2Member\Files
  1143. * @since 110926
  1144. *
  1145. * @param str $distro_id Required. A Distro ID.
  1146. * @param str $distro_type Required: `downloads|streaming`.
  1147. * @return array Array containing a true `success` and `etag`, `xml`, `deployed` elements on success, else a failure array.
  1148. * Failure array will contain a failure `code`, and a failure `message`.
  1149. */
  1150. public static function amazon_cf_get_distro($distro_id = FALSE, $distro_type = FALSE)
  1151. {
  1152. if($distro_id && is_string($distro_id) && $distro_type && is_string($distro_type) && in_array($distro_type, array("downloads", "streaming")))
  1153. {
  1154. foreach($GLOBALS["WS_PLUGIN__"]["s2member"]["o"] as $option => $option_value)
  1155. if(preg_match("/^amazon_cf_files_/", $option) && ($option = preg_replace("/^amazon_cf_files_/", "", $option)))
  1156. $cfc[$option] = $option_value;
  1157.  
  1158. $s3c["bucket"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_bucket"];
  1159. $cfc["access_key"] = $s3c["access_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_access_key"];
  1160. $cfc["secret_key"] = $s3c["secret_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_secret_key"];
  1161.  
  1162. $cf_domain = "cloudfront.amazonaws.com";
  1163. $cf_date = gmdate("D, d M Y H:i:s")." GMT";
  1164. $cf_signature = base64_encode(c_ws_plugin__s2member_files_in::amazon_cf_sign($cf_date));
  1165. $cf_location = ($distro_type === "streaming") ? "/2010-11-01/streaming-distribution/".$distro_id : "/2010-11-01/distribution/".$distro_id;
  1166. $cf_args = array("method" => "GET", "redirection" => 0, "headers" => array("Host" => $cf_domain, "Date" => $cf_date, "Authorization" => "AWS ".$cfc["access_key"].":".$cf_signature));
  1167.  
  1168. if(($cf_response = c_ws_plugin__s2member_utils_urls::remote("https://".$cf_domain.$cf_location, false, array_merge($cf_args, array("timeout" => 20)), "array")) && (($cf_response["code"] === 404 && $cf_response["message"]) || ($cf_response["code"] === 200 && !empty($cf_response["headers"]["etag"]) && !empty($cf_response["body"]))))
  1169. {
  1170. if($cf_response["code"] === 200 && !empty($cf_response["headers"]["etag"]) && !empty($cf_response["body"]))
  1171. return array("success" => true, "code" => null, "message" => null, "etag" => trim($cf_response["headers"]["etag"]), "xml" => trim($cf_response["body"]), "deployed" => ((stripos($cf_response["body"], "<Status>Deployed</Status>") !== false) ? true : false));
  1172.  
  1173. else /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1174. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Existing Amazon® CloudFront Distro NOT found. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  1175. }
  1176. else if(isset($cf_response["code"], $cf_response["message"]))
  1177. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1178. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to acquire existing Amazon® CloudFront Distro. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  1179.  
  1180. else // Else, we use a default error code and message.
  1181. return array("success" => false, "code" => -98, "message" => _x("Unable to acquire existing Amazon® CloudFront Distro. Connection failed.", "s2member-admin", "s2member"));
  1182. }
  1183. else // Else, we use a default error code and message.
  1184. return array("success" => false, "code" => -99, "message" => _x("Unable to acquire existing Amazon® CloudFront Distro. Invalid Distro ID and/or Distro type.", "s2member-admin", "s2member"));
  1185. }
  1186. /**
  1187. * Disables an Amazon® S3/CloudFront Distro.
  1188. *
  1189. * @package s2Member\Files
  1190. * @since 110926
  1191. *
  1192. * @param str $distro_id Required. A Distro ID.
  1193. * @param str $distro_id_etag Required. A Distro ETag header.
  1194. * @param str $distro_id_xml Required. A Distro's XML configuration.
  1195. * @return array Array containing a true `success` and `etag`, `xml`, `deployed` elements on success, else a failure array.
  1196. * Failure array will contain a failure `code`, and a failure `message`.
  1197. */
  1198. public static function amazon_cf_disable_distro($distro_id = FALSE, $distro_id_etag = FALSE, $distro_id_xml = FALSE)
  1199. {
  1200. if($distro_id && is_string($distro_id) && $distro_id_etag && is_string($distro_id_etag) && $distro_id_xml && is_string($distro_id_xml) && ($distro_id_type = (stripos($distro_id_xml, "<StreamingDistribution") !== false) ? "streaming" : ((stripos($distro_id_xml, "<Distribution") !== false) ? "downloads" : false)) && preg_match("/\<CallerReference\>(.+?)\<\/CallerReference\>/is", $distro_id_xml, $distro_id_reference_tag) && ($distro_id_reference = $distro_id_reference_tag[1]))
  1201. {
  1202. if /* Only if it has NOT already been disabled. We do NOT need to do it again. */(stripos($distro_id_xml, "<Enabled>false</Enabled>") === false)
  1203. {
  1204. if /* Check distro status before we even begin processing. */(stripos($distro_id_xml, "<Status>Deployed</Status>") !== false)
  1205. {
  1206. foreach($GLOBALS["WS_PLUGIN__"]["s2member"]["o"] as $option => $option_value)
  1207. if(preg_match("/^amazon_cf_files_/", $option) && ($option = preg_replace("/^amazon_cf_files_/", "", $option)))
  1208. $cfc[$option] = $option_value;
  1209.  
  1210. $s3c["bucket"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_bucket"];
  1211. $cfc["access_key"] = $s3c["access_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_access_key"];
  1212. $cfc["secret_key"] = $s3c["secret_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_secret_key"];
  1213.  
  1214. $cf_domain = "cloudfront.amazonaws.com";
  1215. $cf_date = gmdate("D, d M Y H:i:s")." GMT";
  1216. $cf_signature = base64_encode(c_ws_plugin__s2member_files_in::amazon_cf_sign($cf_date));
  1217. $cf_location = ($distro_id_type === "streaming") ? "/2010-11-01/streaming-distribution/".$distro_id."/config" : "/2010-11-01/distribution/".$distro_id."/config";
  1218. $cf_distro_xml = ($distro_id_type === "streaming") ? '<?xml version="1.0" encoding="UTF-8"?><StreamingDistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2010-11-01/"><S3Origin><DNSName>'.esc_html($s3c["bucket"]).'.s3.amazonaws.com</DNSName></S3Origin><CallerReference>'.esc_html($distro_id_reference).'</CallerReference><Enabled>false</Enabled><TrustedSigners><Self/></TrustedSigners></StreamingDistributionConfig>' : '<?xml version="1.0" encoding="UTF-8"?><DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2010-11-01/"><S3Origin><DNSName>'.esc_html($s3c["bucket"]).'.s3.amazonaws.com</DNSName></S3Origin><CallerReference>'.esc_html($distro_id_reference).'</CallerReference><Enabled>false</Enabled><TrustedSigners><Self/></TrustedSigners></DistributionConfig>';
  1219. $cf_args = array("method" => "PUT", "redirection" => 0, "body" => $cf_distro_xml, "headers" => array("Host" => $cf_domain, "Content-Type" => "application/xml", "Date" => $cf_date, "If-Match" => $distro_id_etag, "Authorization" => "AWS ".$cfc["access_key"].":".$cf_signature));
  1220.  
  1221. if(($cf_response = c_ws_plugin__s2member_utils_urls::remote("https://".$cf_domain.$cf_location, false, array_merge($cf_args, array("timeout" => 20)), "array")) && $cf_response["code"] === 200 && !empty($cf_response["headers"]["etag"]) && !empty($cf_response["body"]))
  1222. return array("success" => true, "code" => null, "message" => null, "etag" => trim($cf_response["headers"]["etag"]), "xml" => trim($cf_response["body"]), "deployed" => ((stripos($cf_response["body"], "<Status>Deployed</Status>") !== false) ? true : false));
  1223.  
  1224. else if(isset($cf_response["code"], $cf_response["message"]))
  1225. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1226. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to disable existing Amazon® CloudFront Distro. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  1227.  
  1228. else // Else, we use a default error code and message.
  1229. return array("success" => false, "code" => -97, "message" => _x("Unable to disable existing Amazon® CloudFront Distro. Connection failed.", "s2member-admin", "s2member"));
  1230. }
  1231. else // Else, we use a default error code and message.
  1232. return array("success" => false, "code" => -98, "message" => _x("Existing Amazon® CloudFront Distro cannot be disabled at this time. Still in a `pending` state. Please wait 15 minutes, then try again. There is a certain process that s2Member must strictly adhere to when re-configuring your Amazon® CloudFront Distros. You may have to tick the auto-configure checkbox again, and re-run s2Member's auto-configuration routine many times, because s2Member will likely run into several `pending` challenges, as it works to completely re-configure your Amazon® CloudFront Distros for you. Thanks for your patience. Please wait 15 minutes, then try again.", "s2member-admin", "s2member"));
  1233. }
  1234. else // Else, we use a default error code and message.
  1235. return array("success" => true, "code" => null, "message" => null, "etag" => $distro_id_etag, "xml" => $distro_id_xml, "deployed" => ((stripos($distro_id_xml, "<Status>Deployed</Status>") !== false) ? true : false));
  1236. }
  1237. else // Else, we use a default error code and message.
  1238. return array("success" => false, "code" => -99, "message" => _x("Unable to disable existing Amazon® CloudFront Distro. Invalid Distro ID, ETag, or XML config.", "s2member-admin", "s2member"));
  1239. }
  1240. /**
  1241. * Deletes an Amazon® S3/CloudFront Distro.
  1242. *
  1243. * @package s2Member\Files
  1244. * @since 110926
  1245. *
  1246. * @param str $distro_id Required. A Distro ID.
  1247. * @param str $distro_id_etag Required. A Distro ETag header.
  1248. * @param str $distro_id_xml Required. A Distro's XML configuration.
  1249. * @return array Array containing a true `success` element on success, else a failure array.
  1250. * Failure array will contain a failure `code`, and a failure `message`.
  1251. */
  1252. public static function amazon_cf_del_distro($distro_id = FALSE, $distro_id_etag = FALSE, $distro_id_xml = FALSE)
  1253. {
  1254. if($distro_id && is_string($distro_id) && $distro_id_etag && is_string($distro_id_etag) && $distro_id_xml && is_string($distro_id_xml) && ($distro_id_type = (stripos($distro_id_xml, "<StreamingDistribution") !== false) ? "streaming" : ((stripos($distro_id_xml, "<Distribution") !== false) ? "downloads" : false)) && preg_match("/\<CallerReference\>(.+?)\<\/CallerReference\>/is", $distro_id_xml, $distro_id_reference_tag) && ($distro_id_reference = $distro_id_reference_tag[1]))
  1255. {
  1256. if /* Check distro status before we even begin processing this deletion. */(stripos($distro_id_xml, "<Status>Deployed</Status>") !== false)
  1257. {
  1258. if(($cf_response = c_ws_plugin__s2member_files_in::amazon_cf_disable_distro($distro_id, $distro_id_etag, $distro_id_xml)) && $cf_response["success"])
  1259. {
  1260. if(($cf_response = c_ws_plugin__s2member_files_in::amazon_cf_get_distro($distro_id, $distro_id_type)) && $cf_response["success"] && $cf_response["deployed"])
  1261. {
  1262. foreach($GLOBALS["WS_PLUGIN__"]["s2member"]["o"] as $option => $option_value)
  1263. if(preg_match("/^amazon_cf_files_/", $option) && ($option = preg_replace("/^amazon_cf_files_/", "", $option)))
  1264. $cfc[$option] = $option_value;
  1265.  
  1266. $s3c["bucket"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_bucket"];
  1267. $cfc["access_key"] = $s3c["access_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_access_key"];
  1268. $cfc["secret_key"] = $s3c["secret_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_secret_key"];
  1269.  
  1270. $cf_domain = "cloudfront.amazonaws.com";
  1271. $cf_date = gmdate("D, d M Y H:i:s")." GMT";
  1272. $cf_signature = base64_encode(c_ws_plugin__s2member_files_in::amazon_cf_sign($cf_date));
  1273. $cf_location = ($distro_id_type === "streaming") ? "/2010-11-01/streaming-distribution/".$distro_id : "/2010-11-01/distribution/".$distro_id;
  1274. $cf_args = array("method" => "DELETE", "redirection" => 0, "headers" => array("Host" => $cf_domain, "Date" => $cf_date, "If-Match" => $cf_response["etag"], "Authorization" => "AWS ".$cfc["access_key"].":".$cf_signature));
  1275.  
  1276. if(($cf_response = c_ws_plugin__s2member_utils_urls::remote("https://".$cf_domain.$cf_location, false, array_merge($cf_args, array("timeout" => 20)), "array")) && ($cf_response["code"] === 200 || $cf_response["code"] === 204 /* Deleted. */))
  1277. return /* Deleted successfully. */ array("success" => true, "code" => null, "message" => null);
  1278.  
  1279. else if(isset($cf_response["code"], $cf_response["message"]))
  1280. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1281. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to delete existing Amazon® CloudFront Distro. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  1282.  
  1283. else // Else, we use a default error code and message.
  1284. return array("success" => false, "code" => -94, "message" => _x("Unable to delete existing Amazon® CloudFront Distro. Connection failed.", "s2member-admin", "s2member"));
  1285. }
  1286. else if(isset($cf_response["success"], $cf_response["deployed"]) && $cf_response["success"] && !$cf_response["deployed"])
  1287. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1288. return array("success" => false, "code" => -95, "message" => _x("Existing Amazon® CloudFront Distro cannot be deleted at this time. Still in a `pending` state after having been disabled by s2Member. Please wait 15 minutes, then try again. There is a certain process that s2Member must strictly adhere to when re-configuring your Amazon® CloudFront Distros. You may have to tick the auto-configure checkbox again, and re-run s2Member's auto-configuration routine many times, because s2Member will likely run into several `pending` challenges, as it works to completely re-configure your Amazon® CloudFront Distros for you. Thanks for your patience. Please wait 15 minutes, then try again.", "s2member-admin", "s2member"));
  1289.  
  1290. else if(isset($cf_response["code"], $cf_response["message"]))
  1291. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1292. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to check status of existing Amazon® CloudFront Distro. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  1293.  
  1294. else // Else, we use a default error code and message.
  1295. return array("success" => false, "code" => -96, "message" => _x("Unable to check status of existing Amazon® CloudFront Distro. Connection failed.", "s2member-admin", "s2member"));
  1296. }
  1297. else if(isset($cf_response["code"], $cf_response["message"]))
  1298. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1299. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to disable existing Amazon® CloudFront Distro. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  1300.  
  1301. else // Else, we use a default error code and message.
  1302. return array("success" => false, "code" => -97, "message" => _x("Unable to disable existing Amazon® CloudFront Distro. Connection failed.", "s2member-admin", "s2member"));
  1303. }
  1304. else // Else, we use a default error code and message.
  1305. return array("success" => false, "code" => -98, "message" => _x("Existing Amazon® CloudFront Distro cannot be deleted at this time. Still in a `pending` state. Please wait 15 minutes, then try again. There is a certain process that s2Member must strictly adhere to when re-configuring your Amazon® CloudFront Distros. You may have to tick the auto-configure checkbox again, and re-run s2Member's auto-configuration routine many times, because s2Member will likely run into several `pending` challenges, as it works to completely re-configure your Amazon® CloudFront Distros for you. Thanks for your patience. Please wait 15 minutes, then try again.", "s2member-admin", "s2member"));
  1306. }
  1307. else // Else, we use a default error code and message.
  1308. return array("success" => false, "code" => -99, "message" => _x("Unable to delete existing Amazon® CloudFront Distro. Invalid Distro ID or ETag.", "s2member-admin", "s2member"));
  1309. }
  1310. /**
  1311. * Creates an Amazon® S3/CloudFront Distro.
  1312. *
  1313. * @package s2Member\Files
  1314. * @since 110926
  1315. *
  1316. * @param str $distro_type Required: `downloads|streaming`.
  1317. * @return array Array containing a true `success` and `distro_[distro_type]_id`, `distro_[distro_type]_dname` elements on success, else a failure array.
  1318. * Failure array will contain a failure `code`, and a failure `message`.
  1319. */
  1320. public static function amazon_cf_create_distro($distro_type = FALSE)
  1321. {
  1322. if($distro_type && is_string($distro_type) && in_array($distro_type, array("downloads", "streaming")))
  1323. {
  1324. foreach($GLOBALS["WS_PLUGIN__"]["s2member"]["o"] as $option => $option_value)
  1325. if(preg_match("/^amazon_cf_files_/", $option) && ($option = preg_replace("/^amazon_cf_files_/", "", $option)))
  1326. $cfc[$option] = $option_value;
  1327.  
  1328. $s3c["bucket"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_bucket"];
  1329. $cfc["access_key"] = $s3c["access_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_access_key"];
  1330. $cfc["secret_key"] = $s3c["secret_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["amazon_s3_files_secret_key"];
  1331.  
  1332. $cf_domain = "cloudfront.amazonaws.com";
  1333. $cf_date = gmdate("D, d M Y H:i:s")." GMT";
  1334. $cf_signature = base64_encode(c_ws_plugin__s2member_files_in::amazon_cf_sign($cf_date));
  1335.  
  1336. if /* Create a `downloads` Distro? This uses a different XML schema. */($distro_type === "downloads")
  1337. {
  1338. $cf_location = /* Create distro. */ "/2010-11-01/distribution";
  1339. $cf_distro_downloads_reference = time().".".md5("downloads".$s3c["bucket"].$s3c["access_key"].$s3c["secret_key"].$cfc["private_key"].$cfc["private_key_id"].$cfc["distro_downloads_cname"]);
  1340. $cf_distro_downloads_xml = '<?xml version="1.0" encoding="UTF-8"?><DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2010-11-01/"><S3Origin><DNSName>'.esc_html($s3c["bucket"]).'.s3.amazonaws.com</DNSName><OriginAccessIdentity>origin-access-identity/cloudfront/'.esc_html($cfc["distros_access_id"]).'</OriginAccessIdentity></S3Origin><CallerReference>'.esc_html($cf_distro_downloads_reference).'</CallerReference>'.(($cfc["distro_downloads_cname"]) ? '<CNAME>'.esc_html($cfc["distro_downloads_cname"]).'</CNAME>' : '').'<Comment>'.esc_html(sprintf(_x("Created by s2Member, for S3 Bucket: %s.", "s2member-admin", "s2member"), $s3c["bucket"])).'</Comment><Enabled>true</Enabled><DefaultRootObject>index.html</DefaultRootObject><TrustedSigners><Self/></TrustedSigners></DistributionConfig>';
  1341. $cf_args = array("method" => "POST", "redirection" => 0, "body" => $cf_distro_downloads_xml, "headers" => array("Host" => $cf_domain, "Content-Type" => "application/xml", "Date" => $cf_date, "Authorization" => "AWS ".$cfc["access_key"].":".$cf_signature));
  1342.  
  1343. if(($cf_response = c_ws_plugin__s2member_utils_urls::remote("https://".$cf_domain.$cf_location, false, array_merge($cf_args, array("timeout" => 20)), "array")) && ($cf_response["code"] === 200 || $cf_response["code"] === 201 /* Created. */))
  1344. {
  1345. if(preg_match("/\<Distribution.*?\>(.+?)\<\/Distribution\>/is", $cf_response["body"], $cf_distro_downloads_tag) && preg_match("/\<Id\>(.+?)\<\/Id\>/is", $cf_distro_downloads_tag[1], $cf_distro_downloads_id_tag) && preg_match("/\<DomainName\>(.+?)\<\/DomainName\>/is", $cf_distro_downloads_tag[1], $cf_distro_downloads_dname_tag))
  1346. return array("success" => true, "code" => null, "message" => null, "distro_downloads_id" => trim($cf_distro_downloads_id_tag[1]), "distro_downloads_dname" => trim($cf_distro_downloads_dname_tag[1]));
  1347.  
  1348. else // Else, we use a default error code and message.
  1349. return array("success" => false, "code" => -97, "message" => _x("Unable to create/read Amazon® CloudFront Downloads Distro. Unexpected response.", "s2member-admin", "s2member"));
  1350. }
  1351. else if(isset($cf_response["code"], $cf_response["message"]))
  1352. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1353. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to create Amazon® CloudFront Downloads Distro. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  1354.  
  1355. else // Else, we use a default error code and message.
  1356. return array("success" => false, "code" => -98, "message" => _x("Unable to create Amazon® CloudFront Downloads Distro. Connection failed.", "s2member-admin", "s2member"));
  1357. }
  1358.  
  1359. else if /* Create a `streaming` Distro? A different XML schema. */($distro_type === "streaming")
  1360. {
  1361. $cf_location = /* Create streaming distro. */ "/2010-11-01/streaming-distribution";
  1362. $cf_distro_streaming_reference = time().".".md5("streaming".$s3c["bucket"].$s3c["access_key"].$s3c["secret_key"].$cfc["private_key"].$cfc["private_key_id"].$cfc["distro_streaming_cname"]);
  1363. $cf_distro_streaming_xml = '<?xml version="1.0" encoding="UTF-8"?><StreamingDistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2010-11-01/"><S3Origin><DNSName>'.esc_html($s3c["bucket"]).'.s3.amazonaws.com</DNSName><OriginAccessIdentity>origin-access-identity/cloudfront/'.esc_html($cfc["distros_access_id"]).'</OriginAccessIdentity></S3Origin><CallerReference>'.esc_html($cf_distro_streaming_reference).'</CallerReference>'.(($cfc["distro_streaming_cname"]) ? '<CNAME>'.esc_html($cfc["distro_streaming_cname"]).'</CNAME>' : '').'<Comment>'.esc_html(sprintf(_x("Created by s2Member, for S3 Bucket: %s.", "s2member-admin", "s2member"), $s3c["bucket"])).'</Comment><Enabled>true</Enabled><DefaultRootObject>index.html</DefaultRootObject><TrustedSigners><Self/></TrustedSigners></StreamingDistributionConfig>';
  1364. $cf_args = array("method" => "POST", "redirection" => 0, "body" => $cf_distro_streaming_xml, "headers" => array("Host" => $cf_domain, "Content-Type" => "application/xml", "Date" => $cf_date, "Authorization" => "AWS ".$cfc["access_key"].":".$cf_signature));
  1365.  
  1366. if(($cf_response = c_ws_plugin__s2member_utils_urls::remote("https://".$cf_domain.$cf_location, false, array_merge($cf_args, array("timeout" => 20)), "array")) && ($cf_response["code"] === 200 || $cf_response["code"] === 201 /* Created. */))
  1367. {
  1368. if(preg_match("/\<StreamingDistribution.*?\>(.+?)\<\/StreamingDistribution\>/is", $cf_response["body"], $cf_distro_streaming_tag) && preg_match("/\<Id\>(.+?)\<\/Id\>/is", $cf_distro_streaming_tag[1], $cf_distro_streaming_id_tag) && preg_match("/\<DomainName\>(.+?)\<\/DomainName\>/is", $cf_distro_streaming_tag[1], $cf_distro_streaming_dname_tag))
  1369. return array("success" => true, "code" => null, "message" => null, "distro_streaming_id" => trim($cf_distro_streaming_id_tag[1]), "distro_streaming_dname" => trim($cf_distro_streaming_dname_tag[1]));
  1370.  
  1371. else // Else, we use a default error code and message.
  1372. return array("success" => false, "code" => -97, "message" => _x("Unable to create/read Amazon® CloudFront Streaming Distro. Unexpected response.", "s2member-admin", "s2member"));
  1373. }
  1374. else if(isset($cf_response["code"], $cf_response["message"]))
  1375. /* translators: In this translation, `%s` may be filled with an English message, which comes from the Amazon® CloudFront API call. Feel free to exclude `%s` if you like. */
  1376. return array("success" => false, "code" => $cf_response["code"], "message" => sprintf(_x("Unable to create Amazon® CloudFront Streaming Distro. %s", "s2member-admin", "s2member"), $cf_response["message"]));
  1377.  
  1378. else // Else, we use a default error code and message.
  1379. return array("success" => false, "code" => -98, "message" => _x("Unable to create Amazon® CloudFront Streaming Distro. Connection failed.", "s2member-admin", "s2member"));
  1380. }
  1381. }
  1382. else // Else, we use a default error code and message.
  1383. return array("success" => false, "code" => -99, "message" => _x("Unable to create Amazon® CloudFront Distro. Invalid Distro type.", "s2member-admin", "s2member"));
  1384. }
  1385. }
  1386. }
  1387. ?>

s2member\includes\classes\files-in.inc.php

Source Code Documentation