Files.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. $metadata= array();
  252. foreach ($headers as $type => $value) {
  253. if (strpos($type,self::METADATA_CONTAINER_HEADER)!==false) {
  254. $metadata[strtolower(substr($type, $count))]= $value;
  255. }
  256. }
  257. $data= array (
  258. 'name' => $container,
  259. 'count' => $result->getHeader(self::CONTAINER_OBJ_COUNT),
  260. 'bytes' => $result->getHeader(self::CONTAINER_BYTES_USE),
  261. 'metadata' => $metadata
  262. );
  263. return $data;
  264. case '404':
  265. $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND;
  266. break;
  267. default:
  268. $this->errorMsg= $result->getBody();
  269. break;
  270. }
  271. $this->errorCode= $status;
  272. return false;
  273. }
  274. /**
  275. * Get a container
  276. *
  277. * @param string $container
  278. * @return Container|boolean
  279. */
  280. public function getContainer($container) {
  281. $result= $this->getMetadataContainer($container);
  282. if (!empty($result)) {
  283. return new Zend_Service_Rackspace_Files_Container($this,$result);
  284. }
  285. return false;
  286. }
  287. /**
  288. * Get an object in a container
  289. *
  290. * @param string $container
  291. * @param string $object
  292. * @param array $headers
  293. * @return Zend_Service_Rackspace_Files_Object|boolean
  294. */
  295. public function getObject($container,$object,$headers=array())
  296. {
  297. if (empty($container)) {
  298. require_once 'Zend/Service/Rackspace/Exception.php';
  299. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  300. }
  301. if (empty($object)) {
  302. require_once 'Zend/Service/Rackspace/Exception.php';
  303. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  304. }
  305. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'GET',$headers);
  306. $status= $result->getStatus();
  307. switch ($status) {
  308. case '200': // break intentionally omitted
  309. $data= array(
  310. 'name' => $object,
  311. 'container' => $container,
  312. 'hash' => $result->getHeader(self::HEADER_HASH),
  313. 'bytes' => $result->getHeader(self::HEADER_CONTENT_LENGTH),
  314. 'last_modified' => $result->getHeader(self::HEADER_LAST_MODIFIED),
  315. 'content_type' => $result->getHeader(self::HEADER_CONTENT_TYPE),
  316. 'content' => $result->getBody()
  317. );
  318. return new Zend_Service_Rackspace_Files_Object($this,$data);
  319. case '404':
  320. $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND;
  321. break;
  322. default:
  323. $this->errorMsg= $result->getBody();
  324. break;
  325. }
  326. $this->errorCode= $status;
  327. return false;
  328. }
  329. /**
  330. * Store a file in a container
  331. *
  332. * @param string $container
  333. * @param string $object
  334. * @param string $content
  335. * @param array $metadata
  336. * @return boolean
  337. */
  338. public function storeObject($container,$object,$content,$metadata=array()) {
  339. if (empty($container)) {
  340. require_once 'Zend/Service/Rackspace/Exception.php';
  341. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  342. }
  343. if (empty($object)) {
  344. require_once 'Zend/Service/Rackspace/Exception.php';
  345. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  346. }
  347. if (empty($content)) {
  348. require_once 'Zend/Service/Rackspace/Exception.php';
  349. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_CONTENT);
  350. }
  351. if (!empty($metadata) && is_array($metadata)) {
  352. foreach ($metadata as $key => $value) {
  353. $headers[self::METADATA_OBJECT_HEADER.$key]= $value;
  354. }
  355. }
  356. $headers[self::HEADER_HASH]= md5($content);
  357. $headers[self::HEADER_CONTENT_LENGTH]= strlen($content);
  358. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'PUT',$headers,null,$content);
  359. $status= $result->getStatus();
  360. switch ($status) {
  361. case '201': // break intentionally omitted
  362. return true;
  363. case '412':
  364. $this->errorMsg= self::ERROR_OBJECT_MISSING_PARAM;
  365. break;
  366. case '422':
  367. $this->errorMsg= self::ERROR_OBJECT_CHECKSUM;
  368. break;
  369. default:
  370. $this->errorMsg= $result->getBody();
  371. break;
  372. }
  373. $this->errorCode= $status;
  374. return false;
  375. }
  376. /**
  377. * Delete an object in a container
  378. *
  379. * @param string $container
  380. * @param string $object
  381. * @return boolean
  382. */
  383. public function deleteObject($container,$object) {
  384. if (empty($container)) {
  385. require_once 'Zend/Service/Rackspace/Exception.php';
  386. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  387. }
  388. if (empty($object)) {
  389. require_once 'Zend/Service/Rackspace/Exception.php';
  390. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  391. }
  392. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'DELETE');
  393. $status= $result->getStatus();
  394. switch ($status) {
  395. case '204': // break intentionally omitted
  396. return true;
  397. case '404':
  398. $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND;
  399. break;
  400. default:
  401. $this->errorMsg= $result->getBody();
  402. break;
  403. }
  404. $this->errorCode= $status;
  405. return false;
  406. }
  407. /**
  408. * Copy an object from a container to another
  409. *
  410. * @param string $container_source
  411. * @param string $obj_source
  412. * @param string $container_dest
  413. * @param string $obj_dest
  414. * @param array $metadata
  415. * @param string $content_type
  416. * @return boolean
  417. */
  418. public function copyObject($container_source,$obj_source,$container_dest,$obj_dest,$metadata=array(),$content_type=null) {
  419. if (empty($container_source)) {
  420. require_once 'Zend/Service/Rackspace/Exception.php';
  421. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_SOURCE_CONTAINER);
  422. }
  423. if (empty($obj_source)) {
  424. require_once 'Zend/Service/Rackspace/Exception.php';
  425. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_SOURCE_OBJECT);
  426. }
  427. if (empty($container_dest)) {
  428. require_once 'Zend/Service/Rackspace/Exception.php';
  429. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_DEST_CONTAINER);
  430. }
  431. if (empty($obj_dest)) {
  432. require_once 'Zend/Service/Rackspace/Exception.php';
  433. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_DEST_OBJECT);
  434. }
  435. $headers= array(
  436. self::HEADER_COPY_FROM => '/'.rawurlencode($container_source).'/'.rawurlencode($obj_source),
  437. self::HEADER_CONTENT_LENGTH => 0
  438. );
  439. if (!empty($content_type)) {
  440. $headers[self::HEADER_CONTENT_TYPE]= $content_type;
  441. }
  442. if (!empty($metadata) && is_array($metadata)) {
  443. foreach ($metadata as $key => $value) {
  444. $headers[self::METADATA_OBJECT_HEADER.$key]= $value;
  445. }
  446. }
  447. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container_dest).'/'.rawurlencode($obj_dest),'PUT',$headers);
  448. $status= $result->getStatus();
  449. switch ($status) {
  450. case '201': // break intentionally omitted
  451. return true;
  452. default:
  453. $this->errorMsg= $result->getBody();
  454. break;
  455. }
  456. $this->errorCode= $status;
  457. return false;
  458. }
  459. /**
  460. * Get the metadata of an object
  461. *
  462. * @param string $container
  463. * @param string $object
  464. * @return array|boolean
  465. */
  466. public function getMetadataObject($container,$object) {
  467. if (empty($container)) {
  468. require_once 'Zend/Service/Rackspace/Exception.php';
  469. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  470. }
  471. if (empty($object)) {
  472. require_once 'Zend/Service/Rackspace/Exception.php';
  473. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  474. }
  475. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'HEAD');
  476. $status= $result->getStatus();
  477. switch ($status) {
  478. case '200': // break intentionally omitted
  479. $headers= $result->getHeaders();
  480. $count= strlen(self::METADATA_OBJECT_HEADER);
  481. $metadata= array();
  482. foreach ($headers as $type => $value) {
  483. if (strpos($type,self::METADATA_OBJECT_HEADER)!==false) {
  484. $metadata[strtolower(substr($type, $count))]= $value;
  485. }
  486. }
  487. $data= array (
  488. 'name' => $object,
  489. 'container' => $container,
  490. 'hash' => $result->getHeader(self::HEADER_HASH),
  491. 'bytes' => $result->getHeader(self::HEADER_CONTENT_LENGTH),
  492. 'content_type' => $result->getHeader(self::HEADER_CONTENT_TYPE),
  493. 'last_modified' => $result->getHeader(self::HEADER_LAST_MODIFIED),
  494. 'metadata' => $metadata
  495. );
  496. return $data;
  497. case '404':
  498. $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND;
  499. break;
  500. default:
  501. $this->errorMsg= $result->getBody();
  502. break;
  503. }
  504. $this->errorCode= $status;
  505. return false;
  506. }
  507. /**
  508. * Set the metadata of a object in a container
  509. * The old metadata values are replaced with the new one
  510. *
  511. * @param string $container
  512. * @param string $object
  513. * @param array $metadata
  514. * @return boolean
  515. */
  516. public function setMetadataObject($container,$object,$metadata)
  517. {
  518. if (empty($container)) {
  519. require_once 'Zend/Service/Rackspace/Exception.php';
  520. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  521. }
  522. if (empty($object)) {
  523. require_once 'Zend/Service/Rackspace/Exception.php';
  524. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  525. }
  526. if (empty($metadata) || !is_array($metadata)) {
  527. require_once 'Zend/Service/Rackspace/Exception.php';
  528. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_OBJECT);
  529. }
  530. $headers=array();
  531. foreach ($metadata as $key => $value) {
  532. $headers[self::METADATA_OBJECT_HEADER.$key]= $value;
  533. }
  534. $result= $this->httpCall($this->getStorageUrl().'/'.rawurlencode($container).'/'.rawurlencode($object),'POST',$headers);
  535. $status= $result->getStatus();
  536. switch ($status) {
  537. case '202': // break intentionally omitted
  538. return true;
  539. case '404':
  540. $this->errorMsg= self::ERROR_OBJECT_NOT_FOUND;
  541. break;
  542. default:
  543. $this->errorMsg= $result->getBody();
  544. break;
  545. }
  546. $this->errorCode= $status;
  547. return false;
  548. }
  549. /**
  550. * Enable the CDN for a container
  551. *
  552. * @param string $container
  553. * @param integer $ttl
  554. * @return array|boolean
  555. */
  556. public function enableCdnContainer ($container,$ttl=self::CDN_TTL_MIN) {
  557. if (empty($container)) {
  558. require_once 'Zend/Service/Rackspace/Exception.php';
  559. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  560. }
  561. $headers=array();
  562. if (is_numeric($ttl) && ($ttl>=self::CDN_TTL_MIN) && ($ttl<=self::CDN_TTL_MAX)) {
  563. $headers[self::CDN_TTL]= $ttl;
  564. } else {
  565. require_once 'Zend/Service/Rackspace/Exception.php';
  566. throw new Zend_Service_Rackspace_Exception(self::ERROR_CDN_TTL_OUT_OF_RANGE);
  567. }
  568. $result= $this->httpCall($this->getCdnUrl().'/'.rawurlencode($container),'PUT',$headers);
  569. $status= $result->getStatus();
  570. switch ($status) {
  571. case '201':
  572. case '202': // break intentionally omitted
  573. $data= array (
  574. 'cdn_uri' => $result->getHeader(self::CDN_URI),
  575. 'cdn_uri_ssl' => $result->getHeader(self::CDN_SSL_URI)
  576. );
  577. return $data;
  578. case '404':
  579. $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND;
  580. break;
  581. default:
  582. $this->errorMsg= $result->getBody();
  583. break;
  584. }
  585. $this->errorCode= $status;
  586. return false;
  587. }
  588. /**
  589. * Update the attribute of a CDN container
  590. *
  591. * @param string $container
  592. * @param integer $ttl
  593. * @param boolean $cdn_enabled
  594. * @param boolean $log
  595. * @return boolean
  596. */
  597. public function updateCdnContainer($container,$ttl=null,$cdn_enabled=null,$log=null)
  598. {
  599. if (empty($container)) {
  600. require_once 'Zend/Service/Rackspace/Exception.php';
  601. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  602. }
  603. if (empty($ttl) && (!isset($cdn_enabled)) && (!isset($log))) {
  604. require_once 'Zend/Service/Rackspace/Exception.php';
  605. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_UPDATE_CDN);
  606. }
  607. $headers=array();
  608. if (isset($ttl)) {
  609. if (is_numeric($ttl) && ($ttl>=self::CDN_TTL_MIN) && ($ttl<=self::CDN_TTL_MAX)) {
  610. $headers[self::CDN_TTL]= $ttl;
  611. } else {
  612. require_once 'Zend/Service/Rackspace/Exception.php';
  613. throw new Zend_Service_Rackspace_Exception(self::ERROR_CDN_TTL_OUT_OF_RANGE);
  614. }
  615. }
  616. if (isset($cdn_enabled)) {
  617. if ($cdn_enabled===true) {
  618. $headers[self::CDN_ENABLED]= 'true';
  619. } else {
  620. $headers[self::CDN_ENABLED]= 'false';
  621. }
  622. }
  623. if (isset($log)) {
  624. if ($log===true) {
  625. $headers[self::CDN_LOG_RETENTION]= 'true';
  626. } else {
  627. $headers[self::CDN_LOG_RETENTION]= 'false';
  628. }
  629. }
  630. $result= $this->httpCall($this->getCdnUrl().'/'.rawurlencode($container),'POST',$headers);
  631. $status= $result->getStatus();
  632. switch ($status) {
  633. case '200':
  634. case '202': // break intentionally omitted
  635. return true;
  636. case '404':
  637. $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND;
  638. break;
  639. default:
  640. $this->errorMsg= $result->getBody();
  641. break;
  642. }
  643. $this->errorCode= $status;
  644. return false;
  645. }
  646. /**
  647. * Get the information of a Cdn container
  648. *
  649. * @param string $container
  650. * @return array|boolean
  651. */
  652. public function getInfoCdnContainer($container) {
  653. if (empty($container)) {
  654. require_once 'Zend/Service/Rackspace/Exception.php';
  655. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME_CONTAINER);
  656. }
  657. $result= $this->httpCall($this->getCdnUrl().'/'.rawurlencode($container),'HEAD');
  658. $status= $result->getStatus();
  659. switch ($status) {
  660. case '204': // break intentionally omitted
  661. $data= array (
  662. 'ttl' => $result->getHeader(self::CDN_TTL),
  663. 'cdn_uri' => $result->getHeader(self::CDN_URI),
  664. 'cdn_uri_ssl' => $result->getHeader(self::CDN_SSL_URI)
  665. );
  666. $data['cdn_enabled']= (strtolower($result->getHeader(self::CDN_ENABLED))!=='false');
  667. $data['log_retention']= (strtolower($result->getHeader(self::CDN_LOG_RETENTION))!=='false');
  668. return $data;
  669. case '404':
  670. $this->errorMsg= self::ERROR_CONTAINER_NOT_FOUND;
  671. break;
  672. default:
  673. $this->errorMsg= $result->getBody();
  674. break;
  675. }
  676. $this->errorCode= $status;
  677. return false;
  678. }
  679. }