File indexing completed on 2025-01-26 05:25:27
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Service 0017 * @subpackage Rackspace 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 */ 0021 0022 // require_once 'Zend/Service/Rackspace/Abstract.php'; 0023 // require_once 'Zend/Service/Rackspace/Files/ContainerList.php'; 0024 // require_once 'Zend/Service/Rackspace/Files/ObjectList.php'; 0025 // require_once 'Zend/Service/Rackspace/Files/Container.php'; 0026 // require_once 'Zend/Service/Rackspace/Files/Object.php'; 0027 0028 /** 0029 * Zend_Service_Rackspace_Files 0030 * 0031 * @category Zend 0032 * @package Zend_Service 0033 * @subpackage Rackspace 0034 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0035 * @license http://framework.zend.com/license/new-bsd New BSD License 0036 */ 0037 class Zend_Service_Rackspace_Files extends Zend_Service_Rackspace_Abstract 0038 { 0039 const ERROR_CONTAINER_NOT_EMPTY = 'The container is not empty, I cannot delete it.'; 0040 const ERROR_CONTAINER_NOT_FOUND = 'The container was not found.'; 0041 const ERROR_OBJECT_NOT_FOUND = 'The object was not found.'; 0042 const ERROR_OBJECT_MISSING_PARAM = 'Missing Content-Length or Content-Type header in the request'; 0043 const ERROR_OBJECT_CHECKSUM = 'Checksum of the file content failed'; 0044 const ERROR_CONTAINER_EXIST = 'The container already exists'; 0045 const ERROR_PARAM_NO_NAME_CONTAINER = 'You must specify the container name'; 0046 const ERROR_PARAM_NO_NAME_OBJECT = 'You must specify the object name'; 0047 const ERROR_PARAM_NO_CONTENT = 'You must specify the content of the object'; 0048 const ERROR_PARAM_NO_NAME_SOURCE_CONTAINER = 'You must specify the source container name'; 0049 const ERROR_PARAM_NO_NAME_SOURCE_OBJECT = 'You must specify the source object name'; 0050 const ERROR_PARAM_NO_NAME_DEST_CONTAINER = 'You must specify the destination container name'; 0051 const ERROR_PARAM_NO_NAME_DEST_OBJECT = 'You must specify the destination object name'; 0052 const ERROR_PARAM_NO_METADATA = 'You must specify the metadata array'; 0053 const ERROR_CDN_TTL_OUT_OF_RANGE = 'TTL must be a number in seconds, min is 900 sec and maximum is 1577836800 (50 years)'; 0054 const ERROR_PARAM_UPDATE_CDN = 'You must specify at least one the parameters: ttl, cdn_enabled or log_retention'; 0055 const HEADER_CONTENT_TYPE = 'Content-Type'; 0056 const HEADER_HASH = 'Etag'; 0057 const HEADER_LAST_MODIFIED = 'Last-Modified'; 0058 const HEADER_CONTENT_LENGTH = 'Content-Length'; 0059 const HEADER_COPY_FROM = 'X-Copy-From'; 0060 const METADATA_OBJECT_HEADER = "X-Object-Meta-"; 0061 const METADATA_CONTAINER_HEADER = "X-Container-Meta-"; 0062 const CDN_URI = "X-CDN-URI"; 0063 const CDN_SSL_URI = "X-CDN-SSL-URI"; 0064 const CDN_ENABLED = "X-CDN-Enabled"; 0065 const CDN_LOG_RETENTION = "X-Log-Retention"; 0066 const CDN_ACL_USER_AGENT = "X-User-Agent-ACL"; 0067 const CDN_ACL_REFERRER = "X-Referrer-ACL"; 0068 const CDN_TTL = "X-TTL"; 0069 const CDN_TTL_MIN = 900; 0070 const CDN_TTL_MAX = 1577836800; 0071 const CDN_EMAIL = "X-Purge-Email"; 0072 const ACCOUNT_CONTAINER_COUNT = "X-Account-Container-Count"; 0073 const ACCOUNT_BYTES_USED = "X-Account-Bytes-Used"; 0074 const ACCOUNT_OBJ_COUNT = "X-Account-Object-Count"; 0075 const CONTAINER_OBJ_COUNT = "X-Container-Object-Count"; 0076 const CONTAINER_BYTES_USE = "X-Container-Bytes-Used"; 0077 const MANIFEST_OBJECT_HEADER = "X-Object-Manifest"; 0078 0079 /** 0080 * Return the total count of containers 0081 * 0082 * @return int 0083 */ 0084 public function getCountContainers() 0085 { 0086 $data= $this->getInfoAccount(); 0087 return $data['tot_containers']; 0088 } 0089 /** 0090 * Return the size in bytes of all the containers 0091 * 0092 * @return int 0093 */ 0094 public function getSizeContainers() 0095 { 0096 $data= $this->getInfoAccount(); 0097 return $data['size_containers']; 0098 } 0099 /** 0100 * Return the count of objects contained in all the containers 0101 * 0102 * @return int 0103 */ 0104 public function getCountObjects() 0105 { 0106 $data= $this->getInfoAccount(); 0107 return $data['tot_objects']; 0108 } 0109 /** 0110 * Get all the containers 0111 * 0112 * @param array $options 0113 * @return Zend_Service_Rackspace_Files_ContainerList|bool 0114 */ 0115 public function getContainers($options=array()) 0116 { 0117 $result= $this->httpCall($this->getStorageUrl(),'GET',null,$options); 0118 if ($result->isSuccessful()) { 0119 return new Zend_Service_Rackspace_Files_ContainerList($this,json_decode($result->getBody(),true)); 0120 } 0121 return false; 0122 } 0123 /** 0124 * Get all the CDN containers 0125 * 0126 * @param array $options 0127 * @return array|bool 0128 */ 0129 public function getCdnContainers($options=array()) 0130 { 0131 $options['enabled_only']= true; 0132 $result= $this->httpCall($this->getCdnUrl(),'GET',null,$options); 0133 if ($result->isSuccessful()) { 0134 return new Zend_Service_Rackspace_Files_ContainerList($this,json_decode($result->getBody(),true)); 0135 } 0136 return false; 0137 } 0138 /** 0139 * Get the metadata information of the accounts: 0140 * - total count containers 0141 * - size in bytes of all the containers 0142 * - total objects in all the containers 0143 * 0144 * @return array|bool 0145 */ 0146 public function getInfoAccount() 0147 { 0148 $result= $this->httpCall($this->getStorageUrl(),'HEAD'); 0149 if ($result->isSuccessful()) { 0150 $output= array( 0151 'tot_containers' => $result->getHeader(self::ACCOUNT_CONTAINER_COUNT), 0152 'size_containers' => $result->getHeader(self::ACCOUNT_BYTES_USED), 0153 'tot_objects' => $result->getHeader(self::ACCOUNT_OBJ_COUNT) 0154 ); 0155 return $output; 0156 } 0157 return false; 0158 } 0159 0160 /** 0161 * Get all the objects of a container 0162 * 0163 * Returns a maximum of 10,000 object names. 0164 * 0165 * @param string $container 0166 * @param array $options 0167 * @return bool|Zend_Service_Rackspace_Files_ObjectList 0168 * @throws Zend_Service_Rackspace_Exception 0169 */ 0170 public function getObjects($container,$options=array()) 0171 { 0172 if (empty($container)) { 0173 // require_once 'Zend/Service/Rackspace/Exception.php'; 0174 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER); 0175 } 0176 $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container),'GET',null,$options); 0177 if ($result->isSuccessful()) { 0178 return new Zend_Service_Rackspace_Files_ObjectList($this,json_decode($result->getBody(),true),$container); 0179 } 0180 return false; 0181 } 0182 0183 /** 0184 * Create a container 0185 * 0186 * @param string $container 0187 * @param array $metadata 0188 * @return bool|Zend_Service_Rackspace_Files_Container 0189 * @throws Zend_Service_Rackspace_Exception 0190 */ 0191 public function createContainer($container,$metadata=array()) 0192 { 0193 if (empty($container)) { 0194 // require_once 'Zend/Service/Rackspace/Exception.php'; 0195 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER); 0196 } 0197 $headers=array(); 0198 if (!empty($metadata)) { 0199 foreach ($metadata as $key => $value) { 0200 $headers[self::METADATA_CONTAINER_HEADER.rawurlencode(strtolower($key))]= rawurlencode($value); 0201 } 0202 } 0203 $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container),'PUT',$headers); 0204 $status= $result->getStatus(); 0205 switch ($status) { 0206 case '201': // break intentionally omitted 0207 $data= array( 0208 'name' => $container 0209 ); 0210 return new Zend_Service_Rackspace_Files_Container($this,$data); 0211 case '202': 0212 $this->errorMsg= self::ERROR_CONTAINER_EXIST; 0213 break; 0214 default: 0215 $this->errorMsg= $result->getBody(); 0216 break; 0217 } 0218 $this->errorCode= $status; 0219 return false; 0220 } 0221 0222 /** 0223 * Delete a container (only if it's empty) 0224 * 0225 * @param string $container 0226 * @return bool 0227 * @throws Zend_Service_Rackspace_Exception 0228 */ 0229 public function deleteContainer($container) 0230 { 0231 if (empty($container)) { 0232 // require_once 'Zend/Service/Rackspace/Exception.php'; 0233 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER); 0234 } 0235 $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container),'DELETE'); 0236 $status= $result->getStatus(); 0237 switch ($status) { 0238 case '204': // break intentionally omitted 0239 return true; 0240 case '409': 0241 $this->errorMsg= self::ERROR_CONTAINER_NOT_EMPTY; 0242 break; 0243 case '404': 0244 $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND; 0245 break; 0246 default: 0247 $this->errorMsg= $result->getBody(); 0248 break; 0249 } 0250 $this->errorCode= $status; 0251 return false; 0252 } 0253 0254 /** 0255 * Get the metadata of a container 0256 * 0257 * @param string $container 0258 * @return array|bool 0259 * @throws Zend_Service_Rackspace_Exception 0260 */ 0261 public function getMetadataContainer($container) 0262 { 0263 if (empty($container)) { 0264 // require_once 'Zend/Service/Rackspace/Exception.php'; 0265 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER); 0266 } 0267 $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container),'HEAD'); 0268 $status= $result->getStatus(); 0269 switch ($status) { 0270 case '204': // break intentionally omitted 0271 $headers= $result->getHeaders(); 0272 $count= strlen(self::METADATA_CONTAINER_HEADER); 0273 // Zend_Http_Response alters header name in array key, so match our header to what will be in the headers array 0274 $headerName = ucwords(strtolower(self::METADATA_CONTAINER_HEADER)); 0275 $metadata= array(); 0276 foreach ($headers as $type => $value) { 0277 if (strpos($type,$headerName)!==false) { 0278 $metadata[strtolower(substr($type, $count))]= $value; 0279 } 0280 } 0281 $data= array ( 0282 'name' => $container, 0283 'count' => $result->getHeader(self::CONTAINER_OBJ_COUNT), 0284 'bytes' => $result->getHeader(self::CONTAINER_BYTES_USE), 0285 'metadata' => $metadata 0286 ); 0287 return $data; 0288 case '404': 0289 $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND; 0290 break; 0291 default: 0292 $this->errorMsg= $result->getBody(); 0293 break; 0294 } 0295 $this->errorCode= $status; 0296 return false; 0297 } 0298 /** 0299 * Get a container 0300 * 0301 * @param string $container 0302 * @return Zend_Service_Rackspace_Files_Container|bool 0303 */ 0304 public function getContainer($container) { 0305 $result= $this->getMetadataContainer($container); 0306 if (!empty($result)) { 0307 return new Zend_Service_Rackspace_Files_Container($this,$result); 0308 } 0309 return false; 0310 } 0311 0312 /** 0313 * Get an object in a container 0314 * 0315 * @param string $container 0316 * @param string $object 0317 * @param array $headers 0318 * @return bool|Zend_Service_Rackspace_Files_Object 0319 * @throws Zend_Service_Rackspace_Exception 0320 */ 0321 public function getObject($container,$object,$headers=array()) 0322 { 0323 if (empty($container)) { 0324 // require_once 'Zend/Service/Rackspace/Exception.php'; 0325 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER); 0326 } 0327 if (empty($object)) { 0328 // require_once 'Zend/Service/Rackspace/Exception.php'; 0329 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT); 0330 } 0331 $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'GET',$headers); 0332 $status= $result->getStatus(); 0333 switch ($status) { 0334 case '200': // break intentionally omitted 0335 $data= array( 0336 'name' => $object, 0337 'container' => $container, 0338 'hash' => $result->getHeader(self::HEADER_HASH), 0339 'bytes' => $result->getHeader(self::HEADER_CONTENT_LENGTH), 0340 'last_modified' => $result->getHeader(self::HEADER_LAST_MODIFIED), 0341 'content_type' => $result->getHeader(self::HEADER_CONTENT_TYPE), 0342 'content' => $result->getBody() 0343 ); 0344 return new Zend_Service_Rackspace_Files_Object($this,$data); 0345 case '404': 0346 $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND; 0347 break; 0348 default: 0349 $this->errorMsg= $result->getBody(); 0350 break; 0351 } 0352 $this->errorCode= $status; 0353 return false; 0354 } 0355 0356 /** 0357 * Store a file in a container 0358 * 0359 * @param string $container 0360 * @param string $object 0361 * @param string $content 0362 * @param array $metadata 0363 * @param string $content_type 0364 * @return bool 0365 * @throws Zend_Service_Rackspace_Exception 0366 */ 0367 public function storeObject($container,$object,$content,$metadata=array(),$content_type=null) { 0368 if (empty($container)) { 0369 // require_once 'Zend/Service/Rackspace/Exception.php'; 0370 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER); 0371 } 0372 if (empty($object)) { 0373 // require_once 'Zend/Service/Rackspace/Exception.php'; 0374 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT); 0375 } 0376 if (empty($content)) { 0377 // require_once 'Zend/Service/Rackspace/Exception.php'; 0378 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_CONTENT); 0379 } 0380 if (!empty($content_type)) { 0381 $headers[self::HEADER_CONTENT_TYPE]= $content_type; 0382 } 0383 if (!empty($metadata) && is_array($metadata)) { 0384 foreach ($metadata as $key => $value) { 0385 $headers[self::METADATA_OBJECT_HEADER.$key]= $value; 0386 } 0387 } 0388 $headers[self::HEADER_HASH]= md5($content); 0389 $headers[self::HEADER_CONTENT_LENGTH]= strlen($content); 0390 $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'PUT',$headers,null,$content); 0391 $status= $result->getStatus(); 0392 switch ($status) { 0393 case '201': // break intentionally omitted 0394 return true; 0395 case '412': 0396 $this->errorMsg= self::ERROR_OBJECT_MISSING_PARAM; 0397 break; 0398 case '422': 0399 $this->errorMsg= self::ERROR_OBJECT_CHECKSUM; 0400 break; 0401 default: 0402 $this->errorMsg= $result->getBody(); 0403 break; 0404 } 0405 $this->errorCode= $status; 0406 return false; 0407 } 0408 0409 /** 0410 * Delete an object in a container 0411 * 0412 * @param string $container 0413 * @param string $object 0414 * @return bool 0415 * @throws Zend_Service_Rackspace_Exception 0416 */ 0417 public function deleteObject($container,$object) { 0418 if (empty($container)) { 0419 // require_once 'Zend/Service/Rackspace/Exception.php'; 0420 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER); 0421 } 0422 if (empty($object)) { 0423 // require_once 'Zend/Service/Rackspace/Exception.php'; 0424 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT); 0425 } 0426 $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'DELETE'); 0427 $status= $result->getStatus(); 0428 switch ($status) { 0429 case '204': // break intentionally omitted 0430 return true; 0431 case '404': 0432 $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND; 0433 break; 0434 default: 0435 $this->errorMsg= $result->getBody(); 0436 break; 0437 } 0438 $this->errorCode= $status; 0439 return false; 0440 } 0441 0442 /** 0443 * Copy an object from a container to another 0444 * 0445 * @param string $container_source 0446 * @param string $obj_source 0447 * @param string $container_dest 0448 * @param string $obj_dest 0449 * @param array $metadata 0450 * @param string $content_type 0451 * @return bool 0452 * @throws Zend_Service_Rackspace_Exception 0453 */ 0454 public function copyObject($container_source,$obj_source,$container_dest,$obj_dest,$metadata=array(),$content_type=null) { 0455 if (empty($container_source)) { 0456 // require_once 'Zend/Service/Rackspace/Exception.php'; 0457 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_SOURCE_CONTAINER); 0458 } 0459 if (empty($obj_source)) { 0460 // require_once 'Zend/Service/Rackspace/Exception.php'; 0461 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_SOURCE_OBJECT); 0462 } 0463 if (empty($container_dest)) { 0464 // require_once 'Zend/Service/Rackspace/Exception.php'; 0465 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_DEST_CONTAINER); 0466 } 0467 if (empty($obj_dest)) { 0468 // require_once 'Zend/Service/Rackspace/Exception.php'; 0469 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_DEST_OBJECT); 0470 } 0471 $headers= array( 0472 self::HEADER_COPY_FROM => '/'.rawurlencode($container_source).'/'.rawurlencode($obj_source), 0473 self::HEADER_CONTENT_LENGTH => 0 0474 ); 0475 if (!empty($content_type)) { 0476 $headers[self::HEADER_CONTENT_TYPE]= $content_type; 0477 } 0478 if (!empty($metadata) && is_array($metadata)) { 0479 foreach ($metadata as $key => $value) { 0480 $headers[self::METADATA_OBJECT_HEADER.$key]= $value; 0481 } 0482 } 0483 $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container_dest).'/'.rawurlencode($obj_dest),'PUT',$headers); 0484 $status= $result->getStatus(); 0485 switch ($status) { 0486 case '201': // break intentionally omitted 0487 return true; 0488 default: 0489 $this->errorMsg= $result->getBody(); 0490 break; 0491 } 0492 $this->errorCode= $status; 0493 return false; 0494 } 0495 0496 /** 0497 * Get the metadata of an object 0498 * 0499 * @param string $container 0500 * @param string $object 0501 * @return array|bool 0502 * @throws Zend_Service_Rackspace_Exception 0503 */ 0504 public function getMetadataObject($container,$object) { 0505 if (empty($container)) { 0506 // require_once 'Zend/Service/Rackspace/Exception.php'; 0507 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER); 0508 } 0509 if (empty($object)) { 0510 // require_once 'Zend/Service/Rackspace/Exception.php'; 0511 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT); 0512 } 0513 $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'HEAD'); 0514 $status= $result->getStatus(); 0515 switch ($status) { 0516 case '200': // break intentionally omitted 0517 $headers= $result->getHeaders(); 0518 $count= strlen(self::METADATA_OBJECT_HEADER); 0519 // Zend_Http_Response alters header name in array key, so match our header to what will be in the headers array 0520 $headerName = ucwords(strtolower(self::METADATA_OBJECT_HEADER)); 0521 $metadata= array(); 0522 foreach ($headers as $type => $value) { 0523 if (strpos($type,$headerName)!==false) { 0524 $metadata[strtolower(substr($type, $count))]= $value; 0525 } 0526 } 0527 $data= array ( 0528 'name' => $object, 0529 'container' => $container, 0530 'hash' => $result->getHeader(self::HEADER_HASH), 0531 'bytes' => $result->getHeader(self::HEADER_CONTENT_LENGTH), 0532 'content_type' => $result->getHeader(self::HEADER_CONTENT_TYPE), 0533 'last_modified' => $result->getHeader(self::HEADER_LAST_MODIFIED), 0534 'metadata' => $metadata 0535 ); 0536 return $data; 0537 case '404': 0538 $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND; 0539 break; 0540 default: 0541 $this->errorMsg= $result->getBody(); 0542 break; 0543 } 0544 $this->errorCode= $status; 0545 return false; 0546 } 0547 0548 /** 0549 * Set the metadata of a object in a container 0550 * The old metadata values are replaced with the new one 0551 * 0552 * @param string $container 0553 * @param string $object 0554 * @param array $metadata 0555 * @return bool 0556 * @throws Zend_Service_Rackspace_Exception 0557 */ 0558 public function setMetadataObject($container,$object,$metadata) 0559 { 0560 if (empty($container)) { 0561 // require_once 'Zend/Service/Rackspace/Exception.php'; 0562 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER); 0563 } 0564 if (empty($object)) { 0565 // require_once 'Zend/Service/Rackspace/Exception.php'; 0566 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT); 0567 } 0568 if (empty($metadata) || !is_array($metadata)) { 0569 // require_once 'Zend/Service/Rackspace/Exception.php'; 0570 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT); 0571 } 0572 $headers=array(); 0573 foreach ($metadata as $key => $value) { 0574 $headers[self::METADATA_OBJECT_HEADER.$key]= $value; 0575 } 0576 $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'POST',$headers); 0577 $status= $result->getStatus(); 0578 switch ($status) { 0579 case '202': // break intentionally omitted 0580 return true; 0581 case '404': 0582 $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND; 0583 break; 0584 default: 0585 $this->errorMsg= $result->getBody(); 0586 break; 0587 } 0588 $this->errorCode= $status; 0589 return false; 0590 } 0591 0592 /** 0593 * Enable the CDN for a container 0594 * 0595 * @param string $container 0596 * @param int $ttl 0597 * @return array|bool 0598 * @throws Zend_Service_Rackspace_Exception 0599 */ 0600 public function enableCdnContainer ($container,$ttl=self::CDN_TTL_MIN) { 0601 if (empty($container)) { 0602 // require_once 'Zend/Service/Rackspace/Exception.php'; 0603 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER); 0604 } 0605 $headers=array(); 0606 if (is_numeric($ttl) && ($ttl>=self::CDN_TTL_MIN) && ($ttl<=self::CDN_TTL_MAX)) { 0607 $headers[self::CDN_TTL]= $ttl; 0608 } else { 0609 // require_once 'Zend/Service/Rackspace/Exception.php'; 0610 throw new Zend_Service_Rackspace_Exception(self::ERROR_CDN_TTL_OUT_OF_RANGE); 0611 } 0612 $result= $this->httpCall($this->getCdnUrl().'/'.rawurlencode($container),'PUT',$headers); 0613 $status= $result->getStatus(); 0614 switch ($status) { 0615 case '201': 0616 case '202': // break intentionally omitted 0617 $data= array ( 0618 'cdn_uri' => $result->getHeader(self::CDN_URI), 0619 'cdn_uri_ssl' => $result->getHeader(self::CDN_SSL_URI) 0620 ); 0621 return $data; 0622 case '404': 0623 $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND; 0624 break; 0625 default: 0626 $this->errorMsg= $result->getBody(); 0627 break; 0628 } 0629 $this->errorCode= $status; 0630 return false; 0631 } 0632 0633 /** 0634 * Update the attribute of a CDN container 0635 * 0636 * @param string $container 0637 * @param int $ttl 0638 * @param bool $cdn_enabled 0639 * @param bool $log 0640 * @return bool 0641 * @throws Zend_Service_Rackspace_Exception 0642 */ 0643 public function updateCdnContainer($container,$ttl=null,$cdn_enabled=null,$log=null) 0644 { 0645 if (empty($container)) { 0646 // require_once 'Zend/Service/Rackspace/Exception.php'; 0647 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER); 0648 } 0649 if (empty($ttl) && (!isset($cdn_enabled)) && (!isset($log))) { 0650 // require_once 'Zend/Service/Rackspace/Exception.php'; 0651 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_UPDATE_CDN); 0652 } 0653 $headers=array(); 0654 if (isset($ttl)) { 0655 if (is_numeric($ttl) && ($ttl>=self::CDN_TTL_MIN) && ($ttl<=self::CDN_TTL_MAX)) { 0656 $headers[self::CDN_TTL]= $ttl; 0657 } else { 0658 // require_once 'Zend/Service/Rackspace/Exception.php'; 0659 throw new Zend_Service_Rackspace_Exception(self::ERROR_CDN_TTL_OUT_OF_RANGE); 0660 } 0661 } 0662 if (isset($cdn_enabled)) { 0663 if ($cdn_enabled===true) { 0664 $headers[self::CDN_ENABLED]= 'true'; 0665 } else { 0666 $headers[self::CDN_ENABLED]= 'false'; 0667 } 0668 } 0669 if (isset($log)) { 0670 if ($log===true) { 0671 $headers[self::CDN_LOG_RETENTION]= 'true'; 0672 } else { 0673 $headers[self::CDN_LOG_RETENTION]= 'false'; 0674 } 0675 } 0676 $result= $this->httpCall($this->getCdnUrl().'/'.rawurlencode($container),'POST',$headers); 0677 $status= $result->getStatus(); 0678 switch ($status) { 0679 case '200': 0680 case '202': // break intentionally omitted 0681 return true; 0682 case '404': 0683 $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND; 0684 break; 0685 default: 0686 $this->errorMsg= $result->getBody(); 0687 break; 0688 } 0689 $this->errorCode= $status; 0690 return false; 0691 } 0692 0693 /** 0694 * Get the information of a Cdn container 0695 * 0696 * @param string $container 0697 * @return array|bool 0698 * @throws Zend_Service_Rackspace_Exception 0699 */ 0700 public function getInfoCdnContainer($container) { 0701 if (empty($container)) { 0702 // require_once 'Zend/Service/Rackspace/Exception.php'; 0703 throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER); 0704 } 0705 $result= $this->httpCall($this->getCdnUrl().'/'.rawurlencode($container),'HEAD'); 0706 $status= $result->getStatus(); 0707 switch ($status) { 0708 case '204': // break intentionally omitted 0709 $data= array ( 0710 'ttl' => $result->getHeader(self::CDN_TTL), 0711 'cdn_uri' => $result->getHeader(self::CDN_URI), 0712 'cdn_uri_ssl' => $result->getHeader(self::CDN_SSL_URI) 0713 ); 0714 $data['cdn_enabled']= (strtolower($result->getHeader(self::CDN_ENABLED))!=='false'); 0715 $data['log_retention']= (strtolower($result->getHeader(self::CDN_LOG_RETENTION))!=='false'); 0716 return $data; 0717 case '404': 0718 $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND; 0719 break; 0720 default: 0721 $this->errorMsg= $result->getBody(); 0722 break; 0723 } 0724 $this->errorCode= $status; 0725 return false; 0726 } 0727 }