Example #1
0
File: Cf.php Project: alx/SBek-Arak
 /**
  * Tests CF
  *
  * @param string $error
  * @return boolean
  */
 function test(&$error)
 {
     /**
      * Test S3 first
      */
     if (!parent::test($error)) {
         return false;
     }
     /**
      * Search active CF distribution
      */
     $dists = @$this->_s3->listDistributions();
     if (!$dists) {
         $error = 'Unable to list distributions.';
         return false;
     }
     $search = sprintf('%s.s3.amazonaws.com', $this->_config['bucket']);
     $dist = false;
     if ($dists) {
         foreach ((array) $dists as $_dist) {
             if (isset($_dist['origin']) && $_dist['origin'] == $search) {
                 $dist = $_dist;
                 break;
             }
         }
     }
     if (!$dist) {
         $error = sprintf('Distribution for bucket "%s" not found.', $this->_config['bucket']);
         return false;
     }
     if (!$dist['enabled']) {
         $error = sprintf('Distribution for bucket "%s" is disabled.', $this->_config['bucket']);
         return false;
     }
     if ($this->_config['cname'] != '') {
         $cnames = isset($dist['cnames']) ? (array) $dist['cnames'] : array();
         if (!in_array($this->_config['cname'], $cnames)) {
             $error = sprintf('Domain name %s is not in distribution CNAME list.', $this->_config['cname']);
             return false;
         }
     } elseif ($this->_config['id'] != '') {
         $domain = $this->get_domain();
         if ($domain != $dist['domain']) {
             $error = sprintf('Distribution domain name mismatch (%s != %s).', $domain, $dist['domain']);
             return false;
         }
     }
     return true;
 }
Example #2
0
 /**
  * Create bucket
  *
  * @param string $container_id
  * @param string $error
  * @return boolean
  */
 function create_container(&$container_id, &$error)
 {
     if ($this->type == W3TC_CDN_CF_TYPE_S3) {
         if (!parent::create_container($container_id, $error)) {
             return false;
         }
     } elseif ($this->type == W3TC_CDN_CF_TYPE_CUSTOM) {
         if (!$this->_init($error)) {
             return false;
         }
     }
     $cnames = array();
     if (!empty($this->_config['cname'])) {
         $domains = (array) $this->_config['cname'];
         foreach ($domains as $domain) {
             $_domains = array_map('trim', explode(',', $domain));
             foreach ($_domains as $_domain) {
                 $cnames[] = $_domain;
             }
         }
     }
     $origin = $this->_get_origin();
     $this->_set_error_handler();
     $dist = @$this->_s3->createDistribution($origin, $this->type, true, $cnames);
     $this->_restore_error_handler();
     if (!$dist) {
         $error = sprintf('Unable to create distribution for origin %s (%s).', $origin, $this->_get_last_error());
         return false;
     }
     $matches = null;
     if (preg_match('~^(.+)\\.cloudfront\\.net$~', $dist['domain'], $matches)) {
         $container_id = $matches[1];
     }
     return true;
 }