示例#1
0
 /**
  * Creates an Amazon S3/CloudFront Distro.
  *
  * @package optimizeMember\Files
  * @since 110926
  *
  * @param str $distro_type Required: `downloads|streaming`.
  * @return array Array containing a true `success` and `distro_[distro_type]_id`, `distro_[distro_type]_dname` elements on success, else a failure array.
  * 	Failure array will contain a failure `code`, and a failure `message`.
  */
 public static function amazon_cf_create_distro($distro_type = FALSE)
 {
     if ($distro_type && is_string($distro_type) && in_array($distro_type, array("downloads", "streaming"))) {
         foreach ($GLOBALS["WS_PLUGIN__"]["optimizemember"]["o"] as $option => $option_value) {
             if (preg_match("/^amazon_cf_files_/", $option) && ($option = preg_replace("/^amazon_cf_files_/", "", $option))) {
                 $cfc[$option] = $option_value;
             }
         }
         /**/
         $s3c["bucket"] = $GLOBALS["WS_PLUGIN__"]["optimizemember"]["o"]["amazon_s3_files_bucket"];
         $cfc["access_key"] = $s3c["access_key"] = $GLOBALS["WS_PLUGIN__"]["optimizemember"]["o"]["amazon_s3_files_access_key"];
         $cfc["secret_key"] = $s3c["secret_key"] = $GLOBALS["WS_PLUGIN__"]["optimizemember"]["o"]["amazon_s3_files_secret_key"];
         /**/
         $cf_domain = "cloudfront.amazonaws.com";
         $cf_date = gmdate("D, d M Y H:i:s") . " GMT";
         $cf_signature = base64_encode(c_ws_plugin__optimizemember_files_in::amazon_cf_sign($cf_date));
         /**/
         if ($distro_type === "downloads") {
             $cf_location = "/2010-11-01/distribution";
             $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"]);
             $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 optimizeMember, for S3 Bucket: %s.", "s2member-admin", "s2member"), $s3c["bucket"])) . '</Comment><Enabled>true</Enabled><DefaultRootObject>index.html</DefaultRootObject><TrustedSigners><Self/></TrustedSigners></DistributionConfig>';
             $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));
             /**/
             if (($cf_response = c_ws_plugin__optimizemember_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)) {
                 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)) {
                     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]));
                 } else {
                     /* Else, we use a default error code and message. */
                     return array("success" => false, "code" => -97, "message" => _x("Unable to create/read Amazon CloudFront Downloads Distro. Unexpected response.", "s2member-admin", "s2member"));
                 }
             } else {
                 if (isset($cf_response["code"], $cf_response["message"])) {
                     /* 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. */
                     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"]));
                 } else {
                     /* Else, we use a default error code and message. */
                     return array("success" => false, "code" => -98, "message" => _x("Unable to create Amazon CloudFront Downloads Distro. Connection failed.", "s2member-admin", "s2member"));
                 }
             }
         } else {
             if ($distro_type === "streaming") {
                 $cf_location = "/2010-11-01/streaming-distribution";
                 $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"]);
                 $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 optimizeMember, for S3 Bucket: %s.", "s2member-admin", "s2member"), $s3c["bucket"])) . '</Comment><Enabled>true</Enabled><DefaultRootObject>index.html</DefaultRootObject><TrustedSigners><Self/></TrustedSigners></StreamingDistributionConfig>';
                 $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));
                 /**/
                 if (($cf_response = c_ws_plugin__optimizemember_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)) {
                     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)) {
                         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]));
                     } else {
                         /* Else, we use a default error code and message. */
                         return array("success" => false, "code" => -97, "message" => _x("Unable to create/read Amazon CloudFront Streaming Distro. Unexpected response.", "s2member-admin", "s2member"));
                     }
                 } else {
                     if (isset($cf_response["code"], $cf_response["message"])) {
                         /* 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. */
                         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"]));
                     } else {
                         /* Else, we use a default error code and message. */
                         return array("success" => false, "code" => -98, "message" => _x("Unable to create Amazon CloudFront Streaming Distro. Connection failed.", "s2member-admin", "s2member"));
                     }
                 }
             }
         }
     } else {
         /* Else, we use a default error code and message. */
         return array("success" => false, "code" => -99, "message" => _x("Unable to create Amazon CloudFront Distro. Invalid Distro type.", "s2member-admin", "s2member"));
     }
 }