PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Service/Rackspace/Files.php

https://bitbucket.org/winponta/zend
PHP | 691 lines | 525 code | 3 blank | 163 comment | 68 complexity | 62f0c032acacfb0eb8dfb91768d5ff77 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. * @param string $content_type
  339. *
  340. * @return boolean
  341. */
  342. public function storeObject($container,$object,$content,$metadata=array(),$content_type=null) {
  343. if (empty($container)) {
  344. require_once 'Zend/Service/Rackspace/Exception.php';
  345. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  346. }
  347. if (empty($object)) {
  348. require_once 'Zend/Service/Rackspace/Exception.php';
  349. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  350. }
  351. if (empty($content)) {
  352. require_once 'Zend/Service/Rackspace/Exception.php';
  353. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_CONTENT);
  354. }
  355. if (!empty($content_type)) {
  356. $headers[self::HEADER_CONTENT_TYPE]= $content_type;
  357. }
  358. if (!empty($metadata) && is_array($metadata)) {
  359. foreach ($metadata as $key => $value) {
  360. $headers[self::METADATA_OBJECT_HEADER.$key]= $value;
  361. }
  362. }
  363. $headers[self::HEADER_HASH]= md5($content);
  364. $headers[self::HEADER_CONTENT_LENGTH]= strlen($content);
  365. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'PUT',$headers,null,$content);
  366. $status= $result->getStatus();
  367. switch ($status) {
  368. case '201': // break intentionally omitted
  369. return true;
  370. case '412':
  371. $this->errorMsg= self::ERROR_OBJECT_MISSING_PARAM;
  372. break;
  373. case '422':
  374. $this->errorMsg= self::ERROR_OBJECT_CHECKSUM;
  375. break;
  376. default:
  377. $this->errorMsg= $result->getBody();
  378. break;
  379. }
  380. $this->errorCode= $status;
  381. return false;
  382. }
  383. /**
  384. * Delete an object in a container
  385. *
  386. * @param string $container
  387. * @param string $object
  388. * @return boolean
  389. */
  390. public function deleteObject($container,$object) {
  391. if (empty($container)) {
  392. require_once 'Zend/Service/Rackspace/Exception.php';
  393. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  394. }
  395. if (empty($object)) {
  396. require_once 'Zend/Service/Rackspace/Exception.php';
  397. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  398. }
  399. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'DELETE');
  400. $status= $result->getStatus();
  401. switch ($status) {
  402. case '204': // break intentionally omitted
  403. return true;
  404. case '404':
  405. $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND;
  406. break;
  407. default:
  408. $this->errorMsg= $result->getBody();
  409. break;
  410. }
  411. $this->errorCode= $status;
  412. return false;
  413. }
  414. /**
  415. * Copy an object from a container to another
  416. *
  417. * @param string $container_source
  418. * @param string $obj_source
  419. * @param string $container_dest
  420. * @param string $obj_dest
  421. * @param array $metadata
  422. * @param string $content_type
  423. * @return boolean
  424. */
  425. public function copyObject($container_source,$obj_source,$container_dest,$obj_dest,$metadata=array(),$content_type=null) {
  426. if (empty($container_source)) {
  427. require_once 'Zend/Service/Rackspace/Exception.php';
  428. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_SOURCE_CONTAINER);
  429. }
  430. if (empty($obj_source)) {
  431. require_once 'Zend/Service/Rackspace/Exception.php';
  432. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_SOURCE_OBJECT);
  433. }
  434. if (empty($container_dest)) {
  435. require_once 'Zend/Service/Rackspace/Exception.php';
  436. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_DEST_CONTAINER);
  437. }
  438. if (empty($obj_dest)) {
  439. require_once 'Zend/Service/Rackspace/Exception.php';
  440. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_DEST_OBJECT);
  441. }
  442. $headers= array(
  443. self::HEADER_COPY_FROM => '/'.rawurlencode($container_source).'/'.rawurlencode($obj_source),
  444. self::HEADER_CONTENT_LENGTH => 0
  445. );
  446. if (!empty($content_type)) {
  447. $headers[self::HEADER_CONTENT_TYPE]= $content_type;
  448. }
  449. if (!empty($metadata) && is_array($metadata)) {
  450. foreach ($metadata as $key => $value) {
  451. $headers[self::METADATA_OBJECT_HEADER.$key]= $value;
  452. }
  453. }
  454. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container_dest).'/'.rawurlencode($obj_dest),'PUT',$headers);
  455. $status= $result->getStatus();
  456. switch ($status) {
  457. case '201': // break intentionally omitted
  458. return true;
  459. default:
  460. $this->errorMsg= $result->getBody();
  461. break;
  462. }
  463. $this->errorCode= $status;
  464. return false;
  465. }
  466. /**
  467. * Get the metadata of an object
  468. *
  469. * @param string $container
  470. * @param string $object
  471. * @return array|boolean
  472. */
  473. public function getMetadataObject($container,$object) {
  474. if (empty($container)) {
  475. require_once 'Zend/Service/Rackspace/Exception.php';
  476. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  477. }
  478. if (empty($object)) {
  479. require_once 'Zend/Service/Rackspace/Exception.php';
  480. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  481. }
  482. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'HEAD');
  483. $status= $result->getStatus();
  484. switch ($status) {
  485. case '200': // break intentionally omitted
  486. $headers= $result->getHeaders();
  487. $count= strlen(self::METADATA_OBJECT_HEADER);
  488. // Zend_Http_Response alters header name in array key, so match our header to what will be in the headers array
  489. $headerName = ucwords(strtolower(self::METADATA_OBJECT_HEADER));
  490. $metadata= array();
  491. foreach ($headers as $type => $value) {
  492. if (strpos($type,$headerName)!==false) {
  493. $metadata[strtolower(substr($type, $count))]= $value;
  494. }
  495. }
  496. $data= array (
  497. 'name' => $object,
  498. 'container' => $container,
  499. 'hash' => $result->getHeader(self::HEADER_HASH),
  500. 'bytes' => $result->getHeader(self::HEADER_CONTENT_LENGTH),
  501. 'content_type' => $result->getHeader(self::HEADER_CONTENT_TYPE),
  502. 'last_modified' => $result->getHeader(self::HEADER_LAST_MODIFIED),
  503. 'metadata' => $metadata
  504. );
  505. return $data;
  506. case '404':
  507. $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND;
  508. break;
  509. default:
  510. $this->errorMsg= $result->getBody();
  511. break;
  512. }
  513. $this->errorCode= $status;
  514. return false;
  515. }
  516. /**
  517. * Set the metadata of a object in a container
  518. * The old metadata values are replaced with the new one
  519. *
  520. * @param string $container
  521. * @param string $object
  522. * @param array $metadata
  523. * @return boolean
  524. */
  525. public function setMetadataObject($container,$object,$metadata)
  526. {
  527. if (empty($container)) {
  528. require_once 'Zend/Service/Rackspace/Exception.php';
  529. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  530. }
  531. if (empty($object)) {
  532. require_once 'Zend/Service/Rackspace/Exception.php';
  533. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  534. }
  535. if (empty($metadata) || !is_array($metadata)) {
  536. require_once 'Zend/Service/Rackspace/Exception.php';
  537. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  538. }
  539. $headers=array();
  540. foreach ($metadata as $key => $value) {
  541. $headers[self::METADATA_OBJECT_HEADER.$key]= $value;
  542. }
  543. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'POST',$headers);
  544. $status= $result->getStatus();
  545. switch ($status) {
  546. case '202': // break intentionally omitted
  547. return true;
  548. case '404':
  549. $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND;
  550. break;
  551. default:
  552. $this->errorMsg= $result->getBody();
  553. break;
  554. }
  555. $this->errorCode= $status;
  556. return false;
  557. }
  558. /**
  559. * Enable the CDN for a container
  560. *
  561. * @param string $container
  562. * @param integer $ttl
  563. * @return array|boolean
  564. */
  565. public function enableCdnContainer ($container,$ttl=self::CDN_TTL_MIN) {
  566. if (empty($container)) {
  567. require_once 'Zend/Service/Rackspace/Exception.php';
  568. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  569. }
  570. $headers=array();
  571. if (is_numeric($ttl) && ($ttl>=self::CDN_TTL_MIN) && ($ttl<=self::CDN_TTL_MAX)) {
  572. $headers[self::CDN_TTL]= $ttl;
  573. } else {
  574. require_once 'Zend/Service/Rackspace/Exception.php';
  575. throw new Zend_Service_Rackspace_Exception(self::ERROR_CDN_TTL_OUT_OF_RANGE);
  576. }
  577. $result= $this->httpCall($this->getCdnUrl().'/'.rawurlencode($container),'PUT',$headers);
  578. $status= $result->getStatus();
  579. switch ($status) {
  580. case '201':
  581. case '202': // break intentionally omitted
  582. $data= array (
  583. 'cdn_uri' => $result->getHeader(self::CDN_URI),
  584. 'cdn_uri_ssl' => $result->getHeader(self::CDN_SSL_URI)
  585. );
  586. return $data;
  587. case '404':
  588. $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND;
  589. break;
  590. default:
  591. $this->errorMsg= $result->getBody();
  592. break;
  593. }
  594. $this->errorCode= $status;
  595. return false;
  596. }
  597. /**
  598. * Update the attribute of a CDN container
  599. *
  600. * @param string $container
  601. * @param integer $ttl
  602. * @param boolean $cdn_enabled
  603. * @param boolean $log
  604. * @return boolean
  605. */
  606. public function updateCdnContainer($container,$ttl=null,$cdn_enabled=null,$log=null)
  607. {
  608. if (empty($container)) {
  609. require_once 'Zend/Service/Rackspace/Exception.php';
  610. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  611. }
  612. if (empty($ttl) && (!isset($cdn_enabled)) && (!isset($log))) {
  613. require_once 'Zend/Service/Rackspace/Exception.php';
  614. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_UPDATE_CDN);
  615. }
  616. $headers=array();
  617. if (isset($ttl)) {
  618. if (is_numeric($ttl) && ($ttl>=self::CDN_TTL_MIN) && ($ttl<=self::CDN_TTL_MAX)) {
  619. $headers[self::CDN_TTL]= $ttl;
  620. } else {
  621. require_once 'Zend/Service/Rackspace/Exception.php';
  622. throw new Zend_Service_Rackspace_Exception(self::ERROR_CDN_TTL_OUT_OF_RANGE);
  623. }
  624. }
  625. if (isset($cdn_enabled)) {
  626. if ($cdn_enabled===true) {
  627. $headers[self::CDN_ENABLED]= 'true';
  628. } else {
  629. $headers[self::CDN_ENABLED]= 'false';
  630. }
  631. }
  632. if (isset($log)) {
  633. if ($log===true) {
  634. $headers[self::CDN_LOG_RETENTION]= 'true';
  635. } else {
  636. $headers[self::CDN_LOG_RETENTION]= 'false';
  637. }
  638. }
  639. $result= $this->httpCall($this->getCdnUrl().'/'.rawurlencode($container),'POST',$headers);
  640. $status= $result->getStatus();
  641. switch ($status) {
  642. case '200':
  643. case '202': // break intentionally omitted
  644. return true;
  645. case '404':
  646. $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND;
  647. break;
  648. default:
  649. $this->errorMsg= $result->getBody();
  650. break;
  651. }
  652. $this->errorCode= $status;
  653. return false;
  654. }
  655. /**
  656. * Get the information of a Cdn container
  657. *
  658. * @param string $container
  659. * @return array|boolean
  660. */
  661. public function getInfoCdnContainer($container) {
  662. if (empty($container)) {
  663. require_once 'Zend/Service/Rackspace/Exception.php';
  664. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  665. }
  666. $result= $this->httpCall($this->getCdnUrl().'/'.rawurlencode($container),'HEAD');
  667. $status= $result->getStatus();
  668. switch ($status) {
  669. case '204': // break intentionally omitted
  670. $data= array (
  671. 'ttl' => $result->getHeader(self::CDN_TTL),
  672. 'cdn_uri' => $result->getHeader(self::CDN_URI),
  673. 'cdn_uri_ssl' => $result->getHeader(self::CDN_SSL_URI)
  674. );
  675. $data['cdn_enabled']= (strtolower($result->getHeader(self::CDN_ENABLED))!=='false');
  676. $data['log_retention']= (strtolower($result->getHeader(self::CDN_LOG_RETENTION))!=='false');
  677. return $data;
  678. case '404':
  679. $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND;
  680. break;
  681. default:
  682. $this->errorMsg= $result->getBody();
  683. break;
  684. }
  685. $this->errorCode= $status;
  686. return false;
  687. }
  688. }