PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Service/Rackspace/Files.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 686 lines | 522 code | 3 blank | 161 comment | 67 complexity | 77ccdf2d60f57948e8f53d9598376d62 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Rackspace
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once 'Zend/Service/Rackspace/Abstract.php';
  22. require_once 'Zend/Service/Rackspace/Files/ContainerList.php';
  23. require_once 'Zend/Service/Rackspace/Files/ObjectList.php';
  24. require_once 'Zend/Service/Rackspace/Files/Container.php';
  25. require_once 'Zend/Service/Rackspace/Files/Object.php';
  26. class Zend_Service_Rackspace_Files extends Zend_Service_Rackspace_Abstract
  27. {
  28. const ERROR_CONTAINER_NOT_EMPTY = 'The container is not empty, I cannot delete it.';
  29. const ERROR_CONTAINER_NOT_FOUND = 'The container was not found.';
  30. const ERROR_OBJECT_NOT_FOUND = 'The object was not found.';
  31. const ERROR_OBJECT_MISSING_PARAM = 'Missing Content-Length or Content-Type header in the request';
  32. const ERROR_OBJECT_CHECKSUM = 'Checksum of the file content failed';
  33. const ERROR_CONTAINER_EXIST = 'The container already exists';
  34. const ERROR_PARAM_NO_NAME_CONTAINER = 'You must specify the container name';
  35. const ERROR_PARAM_NO_NAME_OBJECT = 'You must specify the object name';
  36. const ERROR_PARAM_NO_CONTENT = 'You must specify the content of the object';
  37. const ERROR_PARAM_NO_NAME_SOURCE_CONTAINER = 'You must specify the source container name';
  38. const ERROR_PARAM_NO_NAME_SOURCE_OBJECT = 'You must specify the source object name';
  39. const ERROR_PARAM_NO_NAME_DEST_CONTAINER = 'You must specify the destination container name';
  40. const ERROR_PARAM_NO_NAME_DEST_OBJECT = 'You must specify the destination object name';
  41. const ERROR_PARAM_NO_METADATA = 'You must specify the metadata array';
  42. const ERROR_CDN_TTL_OUT_OF_RANGE = 'TTL must be a number in seconds, min is 900 sec and maximum is 1577836800 (50 years)';
  43. const ERROR_PARAM_UPDATE_CDN = 'You must specify at least one the parameters: ttl, cdn_enabled or log_retention';
  44. const HEADER_CONTENT_TYPE = 'Content-Type';
  45. const HEADER_HASH = 'Etag';
  46. const HEADER_LAST_MODIFIED = 'Last-Modified';
  47. const HEADER_CONTENT_LENGTH = 'Content-Length';
  48. const HEADER_COPY_FROM = 'X-Copy-From';
  49. const METADATA_OBJECT_HEADER = "X-Object-Meta-";
  50. const METADATA_CONTAINER_HEADER = "X-Container-Meta-";
  51. const CDN_URI = "X-CDN-URI";
  52. const CDN_SSL_URI = "X-CDN-SSL-URI";
  53. const CDN_ENABLED = "X-CDN-Enabled";
  54. const CDN_LOG_RETENTION = "X-Log-Retention";
  55. const CDN_ACL_USER_AGENT = "X-User-Agent-ACL";
  56. const CDN_ACL_REFERRER = "X-Referrer-ACL";
  57. const CDN_TTL = "X-TTL";
  58. const CDN_TTL_MIN = 900;
  59. const CDN_TTL_MAX = 1577836800;
  60. const CDN_EMAIL = "X-Purge-Email";
  61. const ACCOUNT_CONTAINER_COUNT = "X-Account-Container-Count";
  62. const ACCOUNT_BYTES_USED = "X-Account-Bytes-Used";
  63. const ACCOUNT_OBJ_COUNT = "X-Account-Object-Count";
  64. const CONTAINER_OBJ_COUNT = "X-Container-Object-Count";
  65. const CONTAINER_BYTES_USE = "X-Container-Bytes-Used";
  66. const MANIFEST_OBJECT_HEADER = "X-Object-Manifest";
  67. /**
  68. * Return the total count of containers
  69. *
  70. * @return integer
  71. */
  72. public function getCountContainers()
  73. {
  74. $data= $this->getInfoAccount();
  75. return $data['tot_containers'];
  76. }
  77. /**
  78. * Return the size in bytes of all the containers
  79. *
  80. * @return integer
  81. */
  82. public function getSizeContainers()
  83. {
  84. $data= $this->getInfoAccount();
  85. return $data['size_containers'];
  86. }
  87. /**
  88. * Return the count of objects contained in all the containers
  89. *
  90. * @return integer
  91. */
  92. public function getCountObjects()
  93. {
  94. $data= $this->getInfoAccount();
  95. return $data['tot_objects'];
  96. }
  97. /**
  98. * Get all the containers
  99. *
  100. * @param array $options
  101. * @return Zend_Service_Rackspace_Files_ContainerList|boolean
  102. */
  103. public function getContainers($options=array())
  104. {
  105. $result= $this->httpCall($this->getStorageUrl(),'GET',null,$options);
  106. if ($result->isSuccessful()) {
  107. return new Zend_Service_Rackspace_Files_ContainerList($this,json_decode($result->getBody(),true));
  108. }
  109. return false;
  110. }
  111. /**
  112. * Get all the CDN containers
  113. *
  114. * @param array $options
  115. * @return array|boolean
  116. */
  117. public function getCdnContainers($options=array())
  118. {
  119. $options['enabled_only']= true;
  120. $result= $this->httpCall($this->getCdnUrl(),'GET',null,$options);
  121. if ($result->isSuccessful()) {
  122. return new Zend_Service_Rackspace_Files_ContainerList($this,json_decode($result->getBody(),true));
  123. }
  124. return false;
  125. }
  126. /**
  127. * Get the metadata information of the accounts:
  128. * - total count containers
  129. * - size in bytes of all the containers
  130. * - total objects in all the containers
  131. *
  132. * @return array|boolean
  133. */
  134. public function getInfoAccount()
  135. {
  136. $result= $this->httpCall($this->getStorageUrl(),'HEAD');
  137. if ($result->isSuccessful()) {
  138. $output= array(
  139. 'tot_containers' => $result->getHeader(self::ACCOUNT_CONTAINER_COUNT),
  140. 'size_containers' => $result->getHeader(self::ACCOUNT_BYTES_USED),
  141. 'tot_objects' => $result->getHeader(self::ACCOUNT_OBJ_COUNT)
  142. );
  143. return $output;
  144. }
  145. return false;
  146. }
  147. /**
  148. * Get all the objects of a container
  149. *
  150. * @param string $container
  151. * @param array $options
  152. * @return Zend_Service_Rackspace_Files_ObjectList|boolean
  153. */
  154. public function getObjects($container,$options=array())
  155. {
  156. if (empty($container)) {
  157. require_once 'Zend/Service/Rackspace/Exception.php';
  158. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  159. }
  160. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container),'GET',null,$options);
  161. if ($result->isSuccessful()) {
  162. return new Zend_Service_Rackspace_Files_ObjectList($this,json_decode($result->getBody(),true),$container);
  163. }
  164. return false;
  165. }
  166. /**
  167. * Create a container
  168. *
  169. * @param string $container
  170. * @param array $metadata
  171. * @return Zend_Service_Rackspace_Files_Container|boolean
  172. */
  173. public function createContainer($container,$metadata=array())
  174. {
  175. if (empty($container)) {
  176. require_once 'Zend/Service/Rackspace/Exception.php';
  177. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  178. }
  179. $headers=array();
  180. if (!empty($metadata)) {
  181. foreach ($metadata as $key => $value) {
  182. $headers[self::METADATA_CONTAINER_HEADER.rawurlencode(strtolower($key))]= rawurlencode($value);
  183. }
  184. }
  185. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container),'PUT',$headers);
  186. $status= $result->getStatus();
  187. switch ($status) {
  188. case '201': // break intentionally omitted
  189. $data= array(
  190. 'name' => $container
  191. );
  192. return new Zend_Service_Rackspace_Files_Container($this,$data);
  193. case '202':
  194. $this->errorMsg= self::ERROR_CONTAINER_EXIST;
  195. break;
  196. default:
  197. $this->errorMsg= $result->getBody();
  198. break;
  199. }
  200. $this->errorCode= $status;
  201. return false;
  202. }
  203. /**
  204. * Delete a container (only if it's empty)
  205. *
  206. * @param sting $container
  207. * @return boolean
  208. */
  209. public function deleteContainer($container)
  210. {
  211. if (empty($container)) {
  212. require_once 'Zend/Service/Rackspace/Exception.php';
  213. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  214. }
  215. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container),'DELETE');
  216. $status= $result->getStatus();
  217. switch ($status) {
  218. case '204': // break intentionally omitted
  219. return true;
  220. case '409':
  221. $this->errorMsg= self::ERROR_CONTAINER_NOT_EMPTY;
  222. break;
  223. case '404':
  224. $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND;
  225. break;
  226. default:
  227. $this->errorMsg= $result->getBody();
  228. break;
  229. }
  230. $this->errorCode= $status;
  231. return false;
  232. }
  233. /**
  234. * Get the metadata of a container
  235. *
  236. * @param string $container
  237. * @return array|boolean
  238. */
  239. public function getMetadataContainer($container)
  240. {
  241. if (empty($container)) {
  242. require_once 'Zend/Service/Rackspace/Exception.php';
  243. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  244. }
  245. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container),'HEAD');
  246. $status= $result->getStatus();
  247. switch ($status) {
  248. case '204': // break intentionally omitted
  249. $headers= $result->getHeaders();
  250. $count= strlen(self::METADATA_CONTAINER_HEADER);
  251. // Zend_Http_Response alters header name in array key, so match our header to what will be in the headers array
  252. $headerName = ucwords(strtolower(self::METADATA_CONTAINER_HEADER));
  253. $metadata= array();
  254. foreach ($headers as $type => $value) {
  255. if (strpos($type,$headerName)!==false) {
  256. $metadata[strtolower(substr($type, $count))]= $value;
  257. }
  258. }
  259. $data= array (
  260. 'name' => $container,
  261. 'count' => $result->getHeader(self::CONTAINER_OBJ_COUNT),
  262. 'bytes' => $result->getHeader(self::CONTAINER_BYTES_USE),
  263. 'metadata' => $metadata
  264. );
  265. return $data;
  266. case '404':
  267. $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND;
  268. break;
  269. default:
  270. $this->errorMsg= $result->getBody();
  271. break;
  272. }
  273. $this->errorCode= $status;
  274. return false;
  275. }
  276. /**
  277. * Get a container
  278. *
  279. * @param string $container
  280. * @return Container|boolean
  281. */
  282. public function getContainer($container) {
  283. $result= $this->getMetadataContainer($container);
  284. if (!empty($result)) {
  285. return new Zend_Service_Rackspace_Files_Container($this,$result);
  286. }
  287. return false;
  288. }
  289. /**
  290. * Get an object in a container
  291. *
  292. * @param string $container
  293. * @param string $object
  294. * @param array $headers
  295. * @return Zend_Service_Rackspace_Files_Object|boolean
  296. */
  297. public function getObject($container,$object,$headers=array())
  298. {
  299. if (empty($container)) {
  300. require_once 'Zend/Service/Rackspace/Exception.php';
  301. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  302. }
  303. if (empty($object)) {
  304. require_once 'Zend/Service/Rackspace/Exception.php';
  305. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  306. }
  307. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'GET',$headers);
  308. $status= $result->getStatus();
  309. switch ($status) {
  310. case '200': // break intentionally omitted
  311. $data= array(
  312. 'name' => $object,
  313. 'container' => $container,
  314. 'hash' => $result->getHeader(self::HEADER_HASH),
  315. 'bytes' => $result->getHeader(self::HEADER_CONTENT_LENGTH),
  316. 'last_modified' => $result->getHeader(self::HEADER_LAST_MODIFIED),
  317. 'content_type' => $result->getHeader(self::HEADER_CONTENT_TYPE),
  318. 'content' => $result->getBody()
  319. );
  320. return new Zend_Service_Rackspace_Files_Object($this,$data);
  321. case '404':
  322. $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND;
  323. break;
  324. default:
  325. $this->errorMsg= $result->getBody();
  326. break;
  327. }
  328. $this->errorCode= $status;
  329. return false;
  330. }
  331. /**
  332. * Store a file in a container
  333. *
  334. * @param string $container
  335. * @param string $object
  336. * @param string $content
  337. * @param array $metadata
  338. * @return boolean
  339. */
  340. public function storeObject($container,$object,$content,$metadata=array()) {
  341. if (empty($container)) {
  342. require_once 'Zend/Service/Rackspace/Exception.php';
  343. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  344. }
  345. if (empty($object)) {
  346. require_once 'Zend/Service/Rackspace/Exception.php';
  347. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  348. }
  349. if (empty($content)) {
  350. require_once 'Zend/Service/Rackspace/Exception.php';
  351. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_CONTENT);
  352. }
  353. if (!empty($metadata) && is_array($metadata)) {
  354. foreach ($metadata as $key => $value) {
  355. $headers[self::METADATA_OBJECT_HEADER.$key]= $value;
  356. }
  357. }
  358. $headers[self::HEADER_HASH]= md5($content);
  359. $headers[self::HEADER_CONTENT_LENGTH]= strlen($content);
  360. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'PUT',$headers,null,$content);
  361. $status= $result->getStatus();
  362. switch ($status) {
  363. case '201': // break intentionally omitted
  364. return true;
  365. case '412':
  366. $this->errorMsg= self::ERROR_OBJECT_MISSING_PARAM;
  367. break;
  368. case '422':
  369. $this->errorMsg= self::ERROR_OBJECT_CHECKSUM;
  370. break;
  371. default:
  372. $this->errorMsg= $result->getBody();
  373. break;
  374. }
  375. $this->errorCode= $status;
  376. return false;
  377. }
  378. /**
  379. * Delete an object in a container
  380. *
  381. * @param string $container
  382. * @param string $object
  383. * @return boolean
  384. */
  385. public function deleteObject($container,$object) {
  386. if (empty($container)) {
  387. require_once 'Zend/Service/Rackspace/Exception.php';
  388. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  389. }
  390. if (empty($object)) {
  391. require_once 'Zend/Service/Rackspace/Exception.php';
  392. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  393. }
  394. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'DELETE');
  395. $status= $result->getStatus();
  396. switch ($status) {
  397. case '204': // break intentionally omitted
  398. return true;
  399. case '404':
  400. $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND;
  401. break;
  402. default:
  403. $this->errorMsg= $result->getBody();
  404. break;
  405. }
  406. $this->errorCode= $status;
  407. return false;
  408. }
  409. /**
  410. * Copy an object from a container to another
  411. *
  412. * @param string $container_source
  413. * @param string $obj_source
  414. * @param string $container_dest
  415. * @param string $obj_dest
  416. * @param array $metadata
  417. * @param string $content_type
  418. * @return boolean
  419. */
  420. public function copyObject($container_source,$obj_source,$container_dest,$obj_dest,$metadata=array(),$content_type=null) {
  421. if (empty($container_source)) {
  422. require_once 'Zend/Service/Rackspace/Exception.php';
  423. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_SOURCE_CONTAINER);
  424. }
  425. if (empty($obj_source)) {
  426. require_once 'Zend/Service/Rackspace/Exception.php';
  427. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_SOURCE_OBJECT);
  428. }
  429. if (empty($container_dest)) {
  430. require_once 'Zend/Service/Rackspace/Exception.php';
  431. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_DEST_CONTAINER);
  432. }
  433. if (empty($obj_dest)) {
  434. require_once 'Zend/Service/Rackspace/Exception.php';
  435. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_DEST_OBJECT);
  436. }
  437. $headers= array(
  438. self::HEADER_COPY_FROM => '/'.rawurlencode($container_source).'/'.rawurlencode($obj_source),
  439. self::HEADER_CONTENT_LENGTH => 0
  440. );
  441. if (!empty($content_type)) {
  442. $headers[self::HEADER_CONTENT_TYPE]= $content_type;
  443. }
  444. if (!empty($metadata) && is_array($metadata)) {
  445. foreach ($metadata as $key => $value) {
  446. $headers[self::METADATA_OBJECT_HEADER.$key]= $value;
  447. }
  448. }
  449. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container_dest).'/'.rawurlencode($obj_dest),'PUT',$headers);
  450. $status= $result->getStatus();
  451. switch ($status) {
  452. case '201': // break intentionally omitted
  453. return true;
  454. default:
  455. $this->errorMsg= $result->getBody();
  456. break;
  457. }
  458. $this->errorCode= $status;
  459. return false;
  460. }
  461. /**
  462. * Get the metadata of an object
  463. *
  464. * @param string $container
  465. * @param string $object
  466. * @return array|boolean
  467. */
  468. public function getMetadataObject($container,$object) {
  469. if (empty($container)) {
  470. require_once 'Zend/Service/Rackspace/Exception.php';
  471. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  472. }
  473. if (empty($object)) {
  474. require_once 'Zend/Service/Rackspace/Exception.php';
  475. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  476. }
  477. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'HEAD');
  478. $status= $result->getStatus();
  479. switch ($status) {
  480. case '200': // break intentionally omitted
  481. $headers= $result->getHeaders();
  482. $count= strlen(self::METADATA_OBJECT_HEADER);
  483. // Zend_Http_Response alters header name in array key, so match our header to what will be in the headers array
  484. $headerName = ucwords(strtolower(self::METADATA_OBJECT_HEADER));
  485. $metadata= array();
  486. foreach ($headers as $type => $value) {
  487. if (strpos($type,$headerName)!==false) {
  488. $metadata[strtolower(substr($type, $count))]= $value;
  489. }
  490. }
  491. $data= array (
  492. 'name' => $object,
  493. 'container' => $container,
  494. 'hash' => $result->getHeader(self::HEADER_HASH),
  495. 'bytes' => $result->getHeader(self::HEADER_CONTENT_LENGTH),
  496. 'content_type' => $result->getHeader(self::HEADER_CONTENT_TYPE),
  497. 'last_modified' => $result->getHeader(self::HEADER_LAST_MODIFIED),
  498. 'metadata' => $metadata
  499. );
  500. return $data;
  501. case '404':
  502. $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND;
  503. break;
  504. default:
  505. $this->errorMsg= $result->getBody();
  506. break;
  507. }
  508. $this->errorCode= $status;
  509. return false;
  510. }
  511. /**
  512. * Set the metadata of a object in a container
  513. * The old metadata values are replaced with the new one
  514. *
  515. * @param string $container
  516. * @param string $object
  517. * @param array $metadata
  518. * @return boolean
  519. */
  520. public function setMetadataObject($container,$object,$metadata)
  521. {
  522. if (empty($container)) {
  523. require_once 'Zend/Service/Rackspace/Exception.php';
  524. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  525. }
  526. if (empty($object)) {
  527. require_once 'Zend/Service/Rackspace/Exception.php';
  528. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  529. }
  530. if (empty($metadata) || !is_array($metadata)) {
  531. require_once 'Zend/Service/Rackspace/Exception.php';
  532. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  533. }
  534. $headers=array();
  535. foreach ($metadata as $key => $value) {
  536. $headers[self::METADATA_OBJECT_HEADER.$key]= $value;
  537. }
  538. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'POST',$headers);
  539. $status= $result->getStatus();
  540. switch ($status) {
  541. case '202': // break intentionally omitted
  542. return true;
  543. case '404':
  544. $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND;
  545. break;
  546. default:
  547. $this->errorMsg= $result->getBody();
  548. break;
  549. }
  550. $this->errorCode= $status;
  551. return false;
  552. }
  553. /**
  554. * Enable the CDN for a container
  555. *
  556. * @param string $container
  557. * @param integer $ttl
  558. * @return array|boolean
  559. */
  560. public function enableCdnContainer ($container,$ttl=self::CDN_TTL_MIN) {
  561. if (empty($container)) {
  562. require_once 'Zend/Service/Rackspace/Exception.php';
  563. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  564. }
  565. $headers=array();
  566. if (is_numeric($ttl) && ($ttl>=self::CDN_TTL_MIN) && ($ttl<=self::CDN_TTL_MAX)) {
  567. $headers[self::CDN_TTL]= $ttl;
  568. } else {
  569. require_once 'Zend/Service/Rackspace/Exception.php';
  570. throw new Zend_Service_Rackspace_Exception(self::ERROR_CDN_TTL_OUT_OF_RANGE);
  571. }
  572. $result= $this->httpCall($this->getCdnUrl().'/'.rawurlencode($container),'PUT',$headers);
  573. $status= $result->getStatus();
  574. switch ($status) {
  575. case '201':
  576. case '202': // break intentionally omitted
  577. $data= array (
  578. 'cdn_uri' => $result->getHeader(self::CDN_URI),
  579. 'cdn_uri_ssl' => $result->getHeader(self::CDN_SSL_URI)
  580. );
  581. return $data;
  582. case '404':
  583. $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND;
  584. break;
  585. default:
  586. $this->errorMsg= $result->getBody();
  587. break;
  588. }
  589. $this->errorCode= $status;
  590. return false;
  591. }
  592. /**
  593. * Update the attribute of a CDN container
  594. *
  595. * @param string $container
  596. * @param integer $ttl
  597. * @param boolean $cdn_enabled
  598. * @param boolean $log
  599. * @return boolean
  600. */
  601. public function updateCdnContainer($container,$ttl=null,$cdn_enabled=null,$log=null)
  602. {
  603. if (empty($container)) {
  604. require_once 'Zend/Service/Rackspace/Exception.php';
  605. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  606. }
  607. if (empty($ttl) && (!isset($cdn_enabled)) && (!isset($log))) {
  608. require_once 'Zend/Service/Rackspace/Exception.php';
  609. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_UPDATE_CDN);
  610. }
  611. $headers=array();
  612. if (isset($ttl)) {
  613. if (is_numeric($ttl) && ($ttl>=self::CDN_TTL_MIN) && ($ttl<=self::CDN_TTL_MAX)) {
  614. $headers[self::CDN_TTL]= $ttl;
  615. } else {
  616. require_once 'Zend/Service/Rackspace/Exception.php';
  617. throw new Zend_Service_Rackspace_Exception(self::ERROR_CDN_TTL_OUT_OF_RANGE);
  618. }
  619. }
  620. if (isset($cdn_enabled)) {
  621. if ($cdn_enabled===true) {
  622. $headers[self::CDN_ENABLED]= 'true';
  623. } else {
  624. $headers[self::CDN_ENABLED]= 'false';
  625. }
  626. }
  627. if (isset($log)) {
  628. if ($log===true) {
  629. $headers[self::CDN_LOG_RETENTION]= 'true';
  630. } else {
  631. $headers[self::CDN_LOG_RETENTION]= 'false';
  632. }
  633. }
  634. $result= $this->httpCall($this->getCdnUrl().'/'.rawurlencode($container),'POST',$headers);
  635. $status= $result->getStatus();
  636. switch ($status) {
  637. case '200':
  638. case '202': // break intentionally omitted
  639. return true;
  640. case '404':
  641. $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND;
  642. break;
  643. default:
  644. $this->errorMsg= $result->getBody();
  645. break;
  646. }
  647. $this->errorCode= $status;
  648. return false;
  649. }
  650. /**
  651. * Get the information of a Cdn container
  652. *
  653. * @param string $container
  654. * @return array|boolean
  655. */
  656. public function getInfoCdnContainer($container) {
  657. if (empty($container)) {
  658. require_once 'Zend/Service/Rackspace/Exception.php';
  659. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  660. }
  661. $result= $this->httpCall($this->getCdnUrl().'/'.rawurlencode($container),'HEAD');
  662. $status= $result->getStatus();
  663. switch ($status) {
  664. case '204': // break intentionally omitted
  665. $data= array (
  666. 'ttl' => $result->getHeader(self::CDN_TTL),
  667. 'cdn_uri' => $result->getHeader(self::CDN_URI),
  668. 'cdn_uri_ssl' => $result->getHeader(self::CDN_SSL_URI)
  669. );
  670. $data['cdn_enabled']= (strtolower($result->getHeader(self::CDN_ENABLED))!=='false');
  671. $data['log_retention']= (strtolower($result->getHeader(self::CDN_LOG_RETENTION))!=='false');
  672. return $data;
  673. case '404':
  674. $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND;
  675. break;
  676. default:
  677. $this->errorMsg= $result->getBody();
  678. break;
  679. }
  680. $this->errorCode= $status;
  681. return false;
  682. }
  683. }