ucloud storage php doc
 All Data Structures Namespaces Files Functions Variables
cloudfiles-kt.php
Go to the documentation of this file.
1 <?php
63 require_once("cloudfiles_exceptions.php");
64 require("cloudfiles_http-kt.php");
65 define("DEFAULT_CF_API_VERSION", 1);
66 define("MAX_CONTAINER_NAME_LEN", 256);
67 define("MAX_OBJECT_NAME_LEN", 1024);
68 define("MAX_OBJECT_SIZE", 5*1024*1024*1024+1);
69 define("AUTHURL", "https://api.ucloudbiz.olleh.com/storage/v1/auth");
70 define("JPAUTHURL", "https://api.ucloudbiz.olleh.com/storage/v1/authjp");
100 {
101  public $dbug;
102  public $username;
103  public $api_key;
104  public $auth_host;
105  public $account;
106 
110  public $storage_url;
111  public $auth_token;
112  public $cfs_http; // added by KT(2013.09.17)
113 
123  {
124 
125  $this->dbug = False;
126  $this->username = $username;
127  $this->api_key = $api_key;
128  $this->auth_host = $auth_host;
129 
130  $this->storage_url = NULL;
131  $this->auth_token = NULL;
132 
133  $this->cfs_http = new CF_Http(DEFAULT_CF_API_VERSION);
134  }
135 
157  function ssl_use_cabundle($path=NULL)
158  {
159  $this->cfs_http->ssl_use_cabundle($path);
160  }
161 
186  {
187  list($status,$reason,$surl,$atoken) =
188  $this->cfs_http->authenticate($this->username, $this->api_key, $this->auth_host);
189 
190  if ($status == 401) {
191  throw new AuthenticationException("Invalid username or access key.");
192  }
193  if ($status < 200 || $status > 299) {
194  throw new InvalidResponseException(
195  "Unexpected response (".$status."): ".$reason);
196  }
197 
198  if (!($surl || $curl) || !$atoken) {
199  throw new InvalidResponseException(
200  "Expected headers missing from auth service.");
201  }
202  $this->storage_url = $surl;
203  $this->auth_token = $atoken;
204  return True;
205  }
206 
224  {
225  if(!$storage_url)
226  {
227  throw new SyntaxException("Missing Required Interface URL's!");
228  return False;
229  }
230  if(!$auth_token)
231  {
232  throw new SyntaxException("Missing Auth Token!");
233  return False;
234  }
235 
236  $this->storage_url = $storage_url;
237  $this->auth_token = $auth_token;
238  return True;
239  }
240 
255  {
256  $arr = array();
257  $arr['storage_url'] = $this->storage_url;
258  $arr['auth_token'] = $this->auth_token;
259 
260  return $arr;
261  }
262 
271  function authenticated()
272  {
273  if (!($this->storage_url || !$this->auth_token)) {
274  return False;
275  }
276  return True;
277  }
278 
282  function setDebug($bool)
283  {
284  $this->dbug = $bool;
285  $this->cfs_http->setDebug($bool);
286  }
287 }
288 
321 {
322  public $dbug;
323  public $cfs_http;
324  public $cfs_auth;
325 
349  {
350  $this->cfs_http = $cfs_auth->cfs_http;
351  $this->cfs_auth = $cfs_auth;
352  if (!$this->cfs_auth->authenticated()) {
353  $e = "Need to pass in a previously authenticated ";
354  $e .= "CF_Authentication instance.";
355  throw new AuthenticationException($e);
356  }
357  $this->cfs_http->setCFAuth($this->cfs_auth);
358  $this->dbug = False;
359  }
360 
366  function setDebug($bool)
367  {
368  $this->dbug = (boolean) $bool;
369  $this->cfs_http->setDebug($this->dbug);
370  }
371 
385  public function close()
386  {
387  $this->cfs_http->close();
388  }
389 
410  function get_info()
411  {
412  list($status, $reason, $container_count, $total_bytes) =
413  $this->cfs_http->head_account();
414  if ($status == 401 && $this->_re_auth()) {
415  return $this->get_info();
416  }
417  if ($status < 200 || $status > 299) {
418  throw new InvalidResponseException(
419  "Invalid response (".$status."): ".$this->cfs_http->get_error());
420  }
421  return array($container_count, $total_bytes);
422  }
423 
444  function create_container($container_name=NULL)
445  {
446  if ($container_name != "0" and !isset($container_name))
447  throw new SyntaxException("Container name not set.");
448 
449  if (!isset($container_name) or $container_name == "")
450  throw new SyntaxException("Container name not set.");
451 
452  if (strpos($container_name, "/") !== False) {
453  $r = "Container name '".$container_name;
454  $r .= "' cannot contain a '/' character.";
455  throw new SyntaxException($r);
456  }
457  if (strlen($container_name) > MAX_CONTAINER_NAME_LEN) {
458  throw new SyntaxException(sprintf(
459  "Container name exeeds %d bytes.",
461  }
462 
463  $return_code = $this->cfs_http->create_container($container_name);
464  if (!$return_code) {
465  throw new InvalidResponseException("Invalid response ("
466  . $return_code. "): " . $this->cfs_http->get_error());
467  }
468  if ($return_code == 401 && $this->_re_auth()) {
469  return $this->create_container($container_name);
470  }
471  if ($return_code != 201 && $return_code != 202) {
472  throw new InvalidResponseException(
473  "Invalid response (".$return_code."): "
474  . $this->cfs_http->get_error());
475  }
476  return new CF_Container($this->cfs_auth, $this->cfs_http, $container_name);
477  }
478 
501  function delete_container($container=NULL)
502  {
503  $container_name = NULL;
504 
505  if (is_object($container)) {
506  if (get_class($container) == "CF_Container") {
507  $container_name = $container->name;
508  }
509  }
510  if (is_string($container)) {
511  $container_name = $container;
512  }
513 
514  if ($container_name != "0" and !isset($container_name))
515  throw new SyntaxException("Must specify container object or name.");
516 
517  $return_code = $this->cfs_http->delete_container($container_name);
518 
519  if (!$return_code) {
520  throw new InvalidResponseException("Failed to obtain http response");
521  }
522  if ($return_code == 401 && $this->_re_auth()) {
523  return $this->delete_container($container);
524  }
525  if ($return_code == 409) {
526  throw new NonEmptyContainerException(
527  "Container must be empty prior to removing it.");
528  }
529  if ($return_code == 404) {
530  throw new NoSuchContainerException(
531  "Specified container did not exist to delete.");
532  }
533  if ($return_code != 204) {
534  throw new InvalidResponseException(
535  "Invalid response (".$return_code."): "
536  . $this->cfs_http->get_error());
537  }
538  return True;
539  }
540 
563  function get_container($container_name=NULL)
564  {
565  list($status, $reason, $count, $bytes) =
566  $this->cfs_http->head_container($container_name);
567  if ($status == 401 && $this->_re_auth()) {
568  return $this->get_container($container_name);
569  }
570  if ($status == 404) {
571  throw new NoSuchContainerException("Container not found.");
572  }
573  if ($status < 200 || $status > 299) {
574  throw new InvalidResponseException(
575  "Invalid response: ".$this->cfs_http->get_error());
576  }
577  return new CF_Container($this->cfs_auth, $this->cfs_http,
578  $container_name, $count, $bytes);
579  }
580 
605  function get_containers($limit=0, $marker=NULL)
606  {
607  list($status, $reason, $container_info) =
608  $this->cfs_http->list_containers_info($limit, $marker);
609  if ($status == 401 && $this->_re_auth()) {
610  return $this->get_containers();
611  }
612  if ($status < 200 || $status > 299) {
613  throw new InvalidResponseException(
614  "Invalid response: ".$this->cfs_http->get_error());
615  }
616  $containers = array();
617  foreach ($container_info as $name => $info) {
618  $containers[] = new CF_Container($this->cfs_auth, $this->cfs_http,
619  $info['name'], $info["count"], $info["bytes"], False);
620  }
621  return $containers;
622  }
623 
649  function list_containers($limit=0, $marker=NULL)
650  {
651  list($status, $reason, $containers) =
652  $this->cfs_http->list_containers($limit, $marker);
653  if ($status == 401 && $this->_re_auth()) {
654  return $this->list_containers($limit, $marker);
655  }
656  if ($status < 200 || $status > 299) {
657  throw new InvalidResponseException(
658  "Invalid response (".$status."): ".$this->cfs_http->get_error());
659  }
660  return $containers;
661  }
662 
697  function list_containers_info($limit=0, $marker=NULL)
698  {
699  list($status, $reason, $container_info) =
700  $this->cfs_http->list_containers_info($limit, $marker);
701  if ($status == 401 && $this->_re_auth()) {
702  return $this->list_containers_info($limit, $marker);
703  }
704  if ($status < 200 || $status > 299) {
705  throw new InvalidResponseException(
706  "Invalid response (".$status."): ".$this->cfs_http->get_error());
707  }
708  return $container_info;
709  }
710 
747  function set_read_progress_function($func_name)
748  {
749  $this->cfs_http->setReadProgressFunc($func_name);
750  }
751 
783  function set_write_progress_function($func_name)
784  {
785  $this->cfs_http->setWriteProgressFunc($func_name);
786  }
787 
809  function ssl_use_cabundle($path=NULL)
810  {
811  $this->cfs_http->ssl_use_cabundle($path);
812  }
813 
814  private function _re_auth()
815  {
816  $this->cfs_auth->authenticate();
817  $this->cfs_http->setCFAuth($this->cfs_auth);
818  return True;
819  }
820 }
821 
836 {
837  public $cfs_auth;
838  public $cfs_http;
839  public $name;
841  public $bytes_used;
842  public $metadata;
843 
856  function __construct(&$cfs_auth, &$cfs_http, $name, $count=0,
857  $bytes=0)
858  {
859  if (strlen($name) > MAX_CONTAINER_NAME_LEN) {
860  throw new SyntaxException("Container name exceeds "
861  . "maximum allowed length.");
862  }
863  if (strpos($name, "/") !== False) {
864  throw new SyntaxException(
865  "Container names cannot contain a '/' character.");
866  }
867  $this->cfs_auth = $cfs_auth;
868  $this->cfs_http = $cfs_http;
869  $this->name = $name;
870  $this->object_count = $count;
871  $this->bytes_used = $bytes;
872  $this->metadata = array();
873  }
874 
882  function __toString()
883  {
884  $me = sprintf("name: %s, count: %.0f, bytes: %.0f",
885  $this->name, $this->object_count, $this->bytes_used);
886  return $me;
887  }
888 
912  function create_object($obj_name=NULL)
913  {
914  return new CF_Object($this, $obj_name);
915  }
916 
941  function get_object($obj_name=NULL)
942  {
943  return new CF_Object($this, $obj_name, True);
944  }
945 
990  function list_objects($limit=0, $marker=NULL, $prefix=NULL, $path=NULL)
991  {
992  list($status, $reason, $obj_list) =
993  $this->cfs_http->list_objects($this->name, $limit,
994  $marker, $prefix, $path);
995  if ($status < 200 || $status > 299) {
996  throw new InvalidResponseException(
997  "Invalid response (".$status."): ".$this->cfs_http->get_error());
998  }
999  return $obj_list;
1000  }
1001 
1046  function get_objects($limit=0, $marker=NULL, $prefix=NULL, $path=NULL, $delimiter=NULL)
1047  {
1048  list($status, $reason, $obj_array) =
1049  $this->cfs_http->get_objects($this->name, $limit,
1050  $marker, $prefix, $path, $delimiter);
1051  if ($status < 200 || $status > 299) {
1052  throw new InvalidResponseException(
1053  "Invalid response (".$status."): ".$this->cfs_http->get_error());
1054  }
1055  $objects = array();
1056  foreach ($obj_array as $obj) {
1057  if(!isset($obj['subdir'])) {
1058  $tmp = new CF_Object($this, $obj["name"], False, False);
1059  $tmp->content_type = $obj["content_type"];
1060  $tmp->content_length = (float) $obj["bytes"];
1061  $tmp->set_etag($obj["hash"]);
1062  $tmp->last_modified = $obj["last_modified"];
1063  $objects[] = $tmp;
1064  }
1065  }
1066  return $objects;
1067  }
1068 
1098  function copy_object_to($obj,$container_target,$dest_obj_name=NULL,$metadata=NULL,$headers=NULL)
1099  {
1100  $obj_name = NULL;
1101  if (is_object($obj)) {
1102  if (get_class($obj) == "CF_Object") {
1103  $obj_name = $obj->name;
1104  }
1105  }
1106  if (is_string($obj)) {
1107  $obj_name = $obj;
1108  }
1109  if (!$obj_name) {
1110  throw new SyntaxException("Object name not set.");
1111  }
1112 
1113  if ($dest_obj_name === NULL) {
1114  $dest_obj_name = $obj_name;
1115  }
1116 
1117  $container_name_target = NULL;
1118  if (is_object($container_target)) {
1119  if (get_class($container_target) == "CF_Container") {
1120  $container_name_target = $container_target->name;
1121  }
1122  }
1123  if (is_string($container_target)) {
1124  $container_name_target = $container_target;
1125  }
1126  if (!$container_name_target) {
1127  throw new SyntaxException("Container name target not set.");
1128  }
1129 
1130  $status = $this->cfs_http->copy_object($obj_name,$dest_obj_name,$this->name,$container_name_target,$metadata,$headers);
1131  if ($status == 404) {
1132  $m = "Specified object '".$this->name."/".$obj_name;
1133  $m.= "' did not exist as source to copy from or '".$container_name_target."' did not exist as target to copy to.";
1134  throw new NoSuchObjectException($m);
1135  }
1136  if ($status < 200 || $status > 299) {
1137  throw new InvalidResponseException(
1138  "Invalid response (".$status."): ".$this->cfs_http->get_error());
1139  }
1140  return true;
1141  }
1142 
1172  function copy_object_from($obj,$container_source,$dest_obj_name=NULL,$metadata=NULL,$headers=NULL)
1173  {
1174  $obj_name = NULL;
1175  if (is_object($obj)) {
1176  if (get_class($obj) == "CF_Object") {
1177  $obj_name = $obj->name;
1178  }
1179  }
1180  if (is_string($obj)) {
1181  $obj_name = $obj;
1182  }
1183  if (!$obj_name) {
1184  throw new SyntaxException("Object name not set.");
1185  }
1186 
1187  if ($dest_obj_name === NULL) {
1188  $dest_obj_name = $obj_name;
1189  }
1190 
1191  $container_name_source = NULL;
1192  if (is_object($container_source)) {
1193  if (get_class($container_source) == "CF_Container") {
1194  $container_name_source = $container_source->name;
1195  }
1196  }
1197  if (is_string($container_source)) {
1198  $container_name_source = $container_source;
1199  }
1200  if (!$container_name_source) {
1201  throw new SyntaxException("Container name source not set.");
1202  }
1203 
1204  $status = $this->cfs_http->copy_object($obj_name,$dest_obj_name,$container_name_source,$this->name,$metadata,$headers);
1205  if ($status == 404) {
1206  $m = "Specified object '".$container_name_source."/".$obj_name;
1207  $m.= "' did not exist as source to copy from or '".$this->name."/".$obj_name."' did not exist as target to copy to.";
1208  throw new NoSuchObjectException($m);
1209  }
1210  if ($status < 200 || $status > 299) {
1211  throw new InvalidResponseException(
1212  "Invalid response (".$status."): ".$this->cfs_http->get_error());
1213  }
1214 
1215  return true;
1216  }
1217 
1247  function move_object_to($obj,$container_target,$dest_obj_name=NULL,$metadata=NULL,$headers=NULL)
1248  {
1249  $retVal = false;
1250 
1251  if(self::copy_object_to($obj,$container_target,$dest_obj_name,$metadata,$headers)) {
1252  $retVal = self::delete_object($obj,$this->name);
1253  }
1254 
1255  return $retVal;
1256  }
1257 
1287  function move_object_from($obj,$container_source,$dest_obj_name=NULL,$metadata=NULL,$headers=NULL)
1288  {
1289  $retVal = false;
1290 
1291  if(self::copy_object_from($obj,$container_source,$dest_obj_name,$metadata,$headers)) {
1292  $retVal = self::delete_object($obj,$container_source);
1293  }
1294 
1295  return $retVal;
1296  }
1297 
1324  function delete_object($obj,$container=NULL)
1325  {
1326  $obj_name = NULL;
1327  if (is_object($obj)) {
1328  if (get_class($obj) == "CF_Object") {
1329  $obj_name = $obj->name;
1330  }
1331  }
1332  if (is_string($obj)) {
1333  $obj_name = $obj;
1334  }
1335  if (!$obj_name) {
1336  throw new SyntaxException("Object name not set.");
1337  }
1338 
1339  $container_name = NULL;
1340 
1341  if($container === NULL) {
1342  $container_name = $this->name;
1343  }
1344  else {
1345  if (is_object($container)) {
1346  if (get_class($container) == "CF_Container") {
1347  $container_name = $container->name;
1348  }
1349  }
1350  if (is_string($container)) {
1351  $container_name = $container;
1352  }
1353  if (!$container_name) {
1354  throw new SyntaxException("Container name source not set.");
1355  }
1356  }
1357 
1358  $status = $this->cfs_http->delete_object($container_name, $obj_name);
1359  if ($status == 404) {
1360  $m = "Specified object '".$container_name."/".$obj_name;
1361  $m.= "' did not exist to delete.";
1362  throw new NoSuchObjectException($m);
1363  }
1364  if ($status != 204) {
1365  throw new InvalidResponseException(
1366  "Invalid response (".$status."): ".$this->cfs_http->get_error());
1367  }
1368  return True;
1369  }
1370 
1395  function enableStaticWebsiteConfig($index=NULL,$error=NULL,$listings=NULL,$css=NULL) {
1396  $usermetadata = array();
1397  if($index != NULL) {
1398  $usermetadata[] = CONTAINER_METADATA_HEADER_PREFIX . "Web-Index:" . $index;
1399  }
1400  if($error != NULL) {
1401  $usermetadata[] = CONTAINER_METADATA_HEADER_PREFIX . "Web-Error:" . $error;
1402  }
1403  if($listings != NULL) {
1404  $usermetadata[] = CONTAINER_METADATA_HEADER_PREFIX . "Web-Listings:" . $listings;
1405  }
1406  if($error != NULL) {
1407  $usermetadata[] = CONTAINER_METADATA_HEADER_PREFIX . "Web-Listings-Css:" . $css;
1408  }
1409  $usermetadata[] = "X-Container-Read: " . ".r:*";
1410  return $this->addUpdate_container_metadata($usermetadata);
1411  }
1412 
1432  $usermetadata = array();
1433  $usermetadata[] = CONTAINER_REMOVE_METADATA_HEADER_PREFIX . "Web-Index:tempValue";
1434  $usermetadata[] = CONTAINER_REMOVE_METADATA_HEADER_PREFIX . "Web-Error:tempValue";
1435  $usermetadata[] = CONTAINER_REMOVE_METADATA_HEADER_PREFIX . "Web-Listings:tempValue";
1436  $usermetadata[] = CONTAINER_REMOVE_METADATA_HEADER_PREFIX . "Web-Listings-Css:tempValue";
1437  $usermetadata[] = "X-Remove-Container-Read: " . ".r:*";
1438  return $this->addUpdate_container_metadata($usermetadata);
1439  }
1440 
1462  $usermetadata = array();
1463  $usermetadata[] = CONTAINER_METADATA_HEADER_PREFIX . "Access-Log-Delivery:true";
1464  return $this->addUpdate_container_metadata($usermetadata);
1465  }
1466 
1486  $usermetadata = array();
1487  $usermetadata[] = CONTAINER_REMOVE_METADATA_HEADER_PREFIX . "Access-Log-Delivery:true";
1488  return $this->addUpdate_container_metadata($usermetadata);
1489  }
1490 
1510  function make_public() {
1511  $usermetadata = array();
1512  $usermetadata[] = "X-Container-Read: .r:*";
1513  return $this->addUpdate_container_metadata($usermetadata);
1514  }
1515 
1534  function make_private() {
1535  $usermetadata = array();
1536  $usermetadata[] = "X-Remove-Container-Read: .r:*";
1537  return $this->addUpdate_container_metadata($usermetadata);
1538  }
1539 
1563  function addUpdate_container_user_metadata($key, $value) {
1564  $usermetadata = array();
1565  $user_header = "";
1566  $user_value = "";
1567  if (is_string($key)) {
1568  $user_header = CONTAINER_METADATA_HEADER_PREFIX . $key;
1569  }
1570  if (is_string($value)) {
1571  $usermetadata[] = $user_header . ": " . $value;
1572  }
1573  return $this->addUpdate_container_metadata($usermetadata);
1574  }
1575 
1597  $usermetadata = array();
1598  $user_header = "";
1599  $user_value = "";
1600  if (is_string($key)) {
1601  $user_header = CONTAINER_REMOVE_METADATA_HEADER_PREFIX . $key;
1602  }
1603  $usermetadata[] = $user_header . ": tempValue";
1604  return $this->addUpdate_container_metadata($usermetadata);
1605  }
1606 
1617  function create_paths($path_name)
1618  {
1619  if ($path_name[0] == '/') {
1620  $path_name = mb_substr($path_name, 0, 1);
1621  }
1622  $elements = explode('/', $path_name, -1);
1623  $build_path = "";
1624  foreach ($elements as $idx => $val) {
1625  if (!$build_path) {
1626  $build_path = $val;
1627  } else {
1628  $build_path .= "/" . $val;
1629  }
1630  $obj = new CF_Object($this, $build_path);
1631  $obj->content_type = "application/directory";
1632  $obj->write(".", 1);
1633  }
1634  }
1635 
1636  private function addUpdate_container_metadata($usermetadata) {
1637  if (!is_array($usermetadata)) {
1638  throw new SyntaxException("Metadata array is empty");
1639  }
1640 
1641  $this->metadata = $usermetadata;
1642  $status = $this->cfs_http->post_container($this);
1643 
1644  if ($status == 404) {
1645  $m = "Specified object '".$container_name."/".$obj_name;
1646  $m.= "' did not exist to delete.";
1647  throw new NoSuchObjectException($m);
1648  }
1649  if ($status != 204) {
1650  throw new InvalidResponseException(
1651  "Invalid response (".$status."): ".$this->cfs_http->get_error());
1652  }
1653  return True;
1654  }
1655 }
1656 
1657 
1668 {
1669  public $container;
1670  public $name;
1674  public $metadata;
1675  public $headers;
1676  public $manifest;
1677  private $etag;
1678 
1686  function __construct(&$container, $name, $force_exists=False, $dohead=True)
1687  {
1688  if ($name[0] == "/") {
1689  $r = "Object name '".$name;
1690  $r .= "' cannot contain begin with a '/' character.";
1691  throw new SyntaxException($r);
1692  }
1693  if (strlen($name) > MAX_OBJECT_NAME_LEN) {
1694  throw new SyntaxException("Object name exceeds "
1695  . "maximum allowed length.");
1696  }
1697  $this->container = $container;
1698  $this->name = $name;
1699  $this->etag = NULL;
1700  $this->_etag_override = False;
1701  $this->last_modified = NULL;
1702  $this->content_type = NULL;
1703  $this->content_length = 0;
1704  $this->metadata = array();
1705  $this->headers = array();
1706  $this->manifest = NULL;
1707  if ($dohead) {
1708  if (!$this->_initialize() && $force_exists) {
1709  throw new NoSuchObjectException("No such object '".$name."'");
1710  }
1711  }
1712  }
1713 
1721  function __toString()
1722  {
1723  return $this->container->name . "/" . $this->name;
1724  }
1725 
1748  function _guess_content_type($handle) {
1749  if ($this->content_type)
1750  return;
1751 
1752  if (function_exists("finfo_open")) {
1753  $local_magic = dirname(__FILE__) . "/share/magic";
1754  $finfo = @finfo_open(FILEINFO_MIME, $local_magic);
1755 
1756  if (!$finfo)
1757  $finfo = @finfo_open(FILEINFO_MIME);
1758 
1759  if ($finfo) {
1760 
1761  if (is_file((string)$handle))
1762  $ct = @finfo_file($finfo, $handle);
1763  else
1764  $ct = @finfo_buffer($finfo, $handle);
1765 
1766  /* PHP 5.3 fileinfo display extra information like
1767  charset so we remove everything after the ; since
1768  we are not into that stuff */
1769  if ($ct) {
1770  $extra_content_type_info = strpos($ct, "; ");
1771  if ($extra_content_type_info)
1772  $ct = substr($ct, 0, $extra_content_type_info);
1773  }
1774 
1775  if ($ct && $ct != 'application/octet-stream')
1776  $this->content_type = $ct;
1777 
1778  @finfo_close($finfo);
1779  }
1780  }
1781 
1782  if (!$this->content_type && (string)is_file($handle) && function_exists("mime_content_type")) {
1783  $this->content_type = @mime_content_type($handle);
1784  }
1785 
1786  if (!$this->content_type) {
1787  throw new BadContentTypeException("Required Content-Type not set");
1788  }
1789  return True;
1790  }
1791 
1792 
1821  function read($hdrs=array())
1822  {
1823  list($status, $reason, $data) =
1824  $this->container->cfs_http->get_object_to_string($this, $hdrs);
1825  #if ($status == 401 && $this->_re_auth()) {
1826  # return $this->read($hdrs);
1827  #}
1828  if (($status < 200) || ($status > 299
1829  && $status != 412 && $status != 304)) {
1830  throw new InvalidResponseException("Invalid response (".$status."): "
1831  . $this->container->cfs_http->get_error());
1832  }
1833  return $data;
1834  }
1835 
1878  function stream(&$fp, $hdrs=array())
1879  {
1880  list($status, $reason) =
1881  $this->container->cfs_http->get_object_to_stream($this,$fp,$hdrs);
1882  #if ($status == 401 && $this->_re_auth()) {
1883  # return $this->stream($fp, $hdrs);
1884  #}
1885  if (($status < 200) || ($status > 299
1886  && $status != 412 && $status != 304)) {
1887  throw new InvalidResponseException("Invalid response (".$status."): "
1888  .$reason);
1889  }
1890  return True;
1891  }
1892 
1929  function sync_metadata()
1930  {
1931  if (!empty($this->metadata) || !empty($this->headers) || $this->manifest) {
1932  $status = $this->container->cfs_http->update_object($this);
1933  #if ($status == 401 && $this->_re_auth()) {
1934  # return $this->sync_metadata();
1935  #}
1936  if ($status != 202) {
1937  throw new InvalidResponseException("Invalid response ("
1938  .$status."): ".$this->container->cfs_http->get_error());
1939  }
1940  return True;
1941  }
1942  return False;
1943  }
1944 
1972  function sync_manifest()
1973  {
1974  return $this->sync_metadata();
1975  }
1976 
2007  function write($data=NULL, $bytes=0, $verify=True)
2008  {
2009  if (!$data && !is_string($data)) {
2010  throw new SyntaxException("Missing data source.");
2011  }
2012  if ($bytes > MAX_OBJECT_SIZE) {
2013  throw new SyntaxException("Bytes exceeds maximum object size.");
2014  }
2015  if ($verify) {
2016  if (!$this->_etag_override) {
2017  $this->etag = $this->compute_md5sum($data);
2018  }
2019  } else {
2020  $this->etag = NULL;
2021  }
2022 
2023  $close_fh = False;
2024  if (!is_resource($data)) {
2025  # A hack to treat string data as a file handle. php://memory feels
2026  # like a better option, but it seems to break on Windows so use
2027  # a temporary file instead.
2028  #
2029  $fp = fopen("php://temp", "wb+");
2030  #$fp = fopen("php://memory", "wb+");
2031  fwrite($fp, $data, strlen($data));
2032  rewind($fp);
2033  $close_fh = True;
2034  $this->content_length = (float) strlen($data);
2035  if ($this->content_length > MAX_OBJECT_SIZE) {
2036  throw new SyntaxException("Data exceeds maximum object size");
2037  }
2038  $ct_data = substr($data, 0, 64);
2039  } else {
2040  $this->content_length = $bytes;
2041  $fp = $data;
2042  $ct_data = fread($data, 64);
2043  rewind($data);
2044  }
2045 
2046  $this->_guess_content_type($ct_data);
2047 
2048  list($status, $reason, $etag) =
2049  $this->container->cfs_http->put_object($this, $fp);
2050  #if ($status == 401 && $this->_re_auth()) {
2051  # return $this->write($data, $bytes, $verify);
2052  #}
2053  if ($status == 412) {
2054  if ($close_fh) { fclose($fp); }
2055  throw new SyntaxException("Missing Content-Type header");
2056  }
2057  if ($status == 422) {
2058  if ($close_fh) { fclose($fp); }
2059  throw new MisMatchedChecksumException(
2060  "Supplied and computed checksums do not match.");
2061  }
2062  if ($status != 201) {
2063  if ($close_fh) { fclose($fp); }
2064  throw new InvalidResponseException("Invalid response (".$status."): "
2065  . $this->container->cfs_http->get_error());
2066  }
2067  if (!$verify) {
2068  $this->etag = $etag;
2069  }
2070  if ($close_fh) { fclose($fp); }
2071  return True;
2072  }
2073 
2103  function load_from_filename($filename, $verify=True)
2104  {
2105  $fp = @fopen($filename, "r");
2106  if (!$fp) {
2107  throw new IOException("Could not open file for reading: ".$filename);
2108  }
2109 
2110  clearstatcache();
2111 
2112  $size = (float) sprintf("%u", filesize($filename));
2113  if ($size > MAX_OBJECT_SIZE) {
2114  throw new SyntaxException("File size exceeds maximum object size.");
2115  }
2116 
2117  $this->_guess_content_type($filename);
2118  $this->write($fp, $size, $verify);
2119  fclose($fp);
2120  return True;
2121  }
2122 
2147  function save_to_filename($filename)
2148  {
2149  $fp = @fopen($filename, "wb");
2150  if (!$fp) {
2151  throw new IOException("Could not open file for writing: ".$filename);
2152  }
2153  $result = $this->stream($fp);
2154  fclose($fp);
2155  return $result;
2156  }
2157 
2167  function set_etag($etag)
2168  {
2169  $this->etag = $etag;
2170  $this->_etag_override = True;
2171  }
2172 
2180  function getETag()
2181  {
2182  return $this->etag;
2183  }
2184 
2199  function compute_md5sum(&$data)
2200  {
2201 
2202  if (function_exists("hash_init") && is_resource($data)) {
2203  $ctx = hash_init('md5');
2204  while (!feof($data)) {
2205  $buffer = fgets($data, 65536);
2206  hash_update($ctx, $buffer);
2207  }
2208  $md5 = hash_final($ctx, false);
2209  rewind($data);
2210  } elseif ((string)is_file($data)) {
2211  $md5 = md5_file($data);
2212  } else {
2213  $md5 = md5($data);
2214  }
2215  return $md5;
2216  }
2217 
2221  private function _initialize()
2222  {
2223  list($status, $reason, $etag, $last_modified, $content_type,
2225  $this->container->cfs_http->head_object($this);
2226  if ($status == 404) {
2227  return False;
2228  }
2229  if ($status < 200 || $status > 299) {
2230  throw new InvalidResponseException("Invalid response (".$status."): "
2231  . $this->container->cfs_http->get_error());
2232  }
2233  $this->etag = $etag;
2234  $this->last_modified = $last_modified;
2235  $this->content_type = $content_type;
2236  $this->content_length = $content_length;
2237  $this->metadata = $metadata;
2238  $this->headers = $headers;
2239  $this->manifest = $manifest;
2240  return True;
2241  }
2242 }
2243 ?>
const AUTHURL
enableStaticWebsiteConfig($index=NULL, $error=NULL, $listings=NULL, $css=NULL)
delete_container($container=NULL)
list_containers_info($limit=0, $marker=NULL)
load_from_filename($filename, $verify=True)
_guess_content_type($handle)
const MAX_OBJECT_NAME_LEN
delete_container_user_metadata($key)
set_etag($etag)
const MAX_CONTAINER_NAME_LEN
list_containers($limit=0, $marker=NULL)
get_objects($limit=0, $marker=NULL, $prefix=NULL, $path=NULL, $delimiter=NULL)
stream(&$fp, $hdrs=array())
set_read_progress_function($func_name)
move_object_from($obj, $container_source, $dest_obj_name=NULL, $metadata=NULL, $headers=NULL)
addUpdate_container_user_metadata($key, $value)
compute_md5sum(&$data)
ssl_use_cabundle($path=NULL)
copy_object_to($obj, $container_target, $dest_obj_name=NULL, $metadata=NULL, $headers=NULL)
read($hdrs=array())
create_object($obj_name=NULL)
ssl_use_cabundle($path=NULL)
set_write_progress_function($func_name)
__construct(&$container, $name, $force_exists=False, $dohead=True)
create_container($container_name=NULL)
__construct(&$cfs_auth, &$cfs_http, $name, $count=0, $bytes=0)
write($data=NULL, $bytes=0, $verify=True)
delete_object($obj, $container=NULL)
get_object($obj_name=NULL)
save_to_filename($filename)
__construct($username=NULL, $api_key=NULL, $auth_host=AUTHURL)
const MAX_OBJECT_SIZE
list_objects($limit=0, $marker=NULL, $prefix=NULL, $path=NULL)
move_object_to($obj, $container_target, $dest_obj_name=NULL, $metadata=NULL, $headers=NULL)
load_cached_credentials($auth_token, $storage_url)
copy_object_from($obj, $container_source, $dest_obj_name=NULL, $metadata=NULL, $headers=NULL)
authenticate($version=DEFAULT_CF_API_VERSION)
__construct($cfs_auth)
get_containers($limit=0, $marker=NULL)
get_container($container_name=NULL)
const DEFAULT_CF_API_VERSION
create_paths($path_name)