Files.php 28 KB

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