#if ($status == 401 && $this->_re_auth()) {
# return $this->create_container($container_name);
if ($return_code != 201 && $return_code != 202) {
throw new InvalidResponseException(
"Invalid response (".$return_code."): "
. $this->cfs_http->get_error());
return new UpdraftPlus_CF_Container($this->cfs_auth, $this->cfs_http, $container_name);
* Given either a Container instance or name, remove the remote Container.
* The Container must be empty prior to removing it.
* # ... authentication code excluded (see previous examples) ...
* $conn = new CF_Connection($auth);
* $conn->delete_container("my photos");
* @param string|obj $container container name or instance
* @return boolean <kbd>True</kbd> if successfully deleted
* @throws SyntaxException missing proper argument
* @throws InvalidResponseException invalid response
* @throws NonEmptyContainerException container not empty
* @throws NoSuchContainerException remote container does not exist
function delete_container($container=NULL)
if (is_object($container)) {
if (get_class($container) == "UpdraftPlus_CF_Container") {
$container_name = $container->name;
if (is_string($container)) {
$container_name = $container;
if ($container_name != "0" and !isset($container_name))
throw new SyntaxException("Must specify container object or name.");
$return_code = $this->cfs_http->delete_container($container_name);
throw new InvalidResponseException("Failed to obtain http response");
#if ($status == 401 && $this->_re_auth()) {
# return $this->delete_container($container);
if ($return_code == 409) {
throw new NonEmptyContainerException(
"Container must be empty prior to removing it.");
if ($return_code == 404) {
throw new NoSuchContainerException(
"Specified container did not exist to delete.");
if ($return_code != 204) {
throw new InvalidResponseException(
"Invalid response (".$return_code."): "
. $this->cfs_http->get_error());
* Return a Container instance
* For the given name, return a Container instance if the remote Container
* exists, otherwise throw a Not Found exception.
* # ... authentication code excluded (see previous examples) ...
* $conn = new CF_Connection($auth);
* $images = $conn->get_container("my photos");
* print "Number of Objects: " . $images->count . "\n";
* print "Bytes stored in container: " . $images->bytes . "\n";
* @param string $container_name name of the remote Container
* @return container CF_Container instance
* @throws NoSuchContainerException thrown if no remote Container
* @throws InvalidResponseException unexpected response
function get_container($container_name=NULL)
list($status, $reason, $count, $bytes) =
$this->cfs_http->head_container($container_name);
#if ($status == 401 && $this->_re_auth()) {
# return $this->get_container($container_name);
throw new NoSuchContainerException("Container not found.");
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response: ".$this->cfs_http->get_error());
return new UpdraftPlus_3CF_Container($this->cfs_auth, $this->cfs_http,
$container_name, $count, $bytes);
* Return array of Container instances
* Return an array of CF_Container instances on the account. The instances
* will be fully populated with Container attributes (bytes stored and
* # ... authentication code excluded (see previous examples) ...
* $conn = new CF_Connection($auth);
* $clist = $conn->get_containers();
* foreach ($clist as $cont) {
* print "Container name: " . $cont->name . "\n";
* print "Number of Objects: " . $cont->count . "\n";
* print "Bytes stored in container: " . $cont->bytes . "\n";
* @return array An array of CF_Container instances
* @throws InvalidResponseException unexpected response
function get_containers($limit=0, $marker=NULL)
list($status, $reason, $container_info) =
$this->cfs_http->list_containers_info($limit, $marker);
#if ($status == 401 && $this->_re_auth()) {
# return $this->get_containers();
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response: ".$this->cfs_http->get_error());
foreach ($container_info as $name => $info) {
$containers[] = new UpdraftPlus_CF_Container($this->cfs_auth, $this->cfs_http,
$info['name'], $info["count"], $info["bytes"], False);
* Return list of remote Containers
* Return an array of strings containing the names of all remote Containers.
* # ... authentication code excluded (see previous examples) ...
* $conn = new CF_Connection($auth);
* $container_list = $conn->list_containers();
* print_r($container_list);
* @param integer $limit restrict results to $limit Containers
* @param string $marker return results greater than $marker
* @return array list of remote Containers
* @throws InvalidResponseException unexpected response
function list_containers($limit=0, $marker=NULL)
list($status, $reason, $containers) =
$this->cfs_http->list_containers($limit, $marker);
#if ($status == 401 && $this->_re_auth()) {
# return $this->list_containers($limit, $marker);
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
* Return array of information about remote Containers
* Return a nested array structure of Container info.
* # ... authentication code excluded (see previous examples) ...
* $container_info = $conn->list_containers_info();
* print_r($container_info);
* @param integer $limit restrict results to $limit Containers
* @param string $marker return results greater than $marker
* @return array nested array structure of Container info
* @throws InvalidResponseException unexpected response
function list_containers_info($limit=0, $marker=NULL)
list($status, $reason, $container_info) =
$this->cfs_http->list_containers_info($limit, $marker);
#if ($status == 401 && $this->_re_auth()) {
# return $this->list_containers_info($limit, $marker);
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
* Return list of Containers that have been published to the CDN.
* Return an array of strings containing the names of published Containers.
* Note that this function returns the list of any Container that has
* ever been CDN-enabled regardless of it's existence in the storage
* # ... authentication code excluded (see previous examples) ...
* $conn = new CF_Connection($auth);
* $public_containers = $conn->list_public_containers();
* print_r($public_containers);
* @param bool $enabled_only Will list all containers ever CDN enabled if * set to false or only currently enabled CDN containers if set to true. * Defaults to false.
* @return array list of published Container names
* @throws InvalidResponseException unexpected response
function list_public_containers($enabled_only=False)
list($status, $reason, $containers) =
$this->cfs_http->list_cdn_containers($enabled_only);
#if ($status == 401 && $this->_re_auth()) {
# return $this->list_public_containers();
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
* Set a user-supplied callback function to report download progress
* The callback function is used to report incremental progress of a data
* download functions (e.g. $container->list_objects(), $obj->read(), etc).
* The specified function will be periodically called with the number of
* bytes transferred until the entire download is complete. This callback
* function can be useful for implementing "progress bars" for large
* The specified callback function should take a single integer parameter.
* function read_callback($bytes_transferred) {
* print ">> downloaded " . $bytes_transferred . " bytes.\n";
* # ... do other things ...
* $conn = new CF_Connection($auth_obj);
* $conn->set_read_progress_function("read_callback");
* print_r($conn->list_containers());
* # output would look like this:
* >> downloaded 10 bytes.
* >> downloaded 11 bytes.
* @param string $func_name the name of the user callback function
function set_read_progress_function($func_name)
$this->cfs_http->setReadProgressFunc($func_name);
* Set a user-supplied callback function to report upload progress
* The callback function is used to report incremental progress of a data
* upload functions (e.g. $obj->write() call). The specified function will
* be periodically called with the number of bytes transferred until the
* entire upload is complete. This callback function can be useful
* for implementing "progress bars" for large uploads/downloads.
* The specified callback function should take a single integer parameter.
* function write_callback($bytes_transferred) {
* print ">> uploaded " . $bytes_transferred . " bytes.\n";
* # ... do other things ...
* $conn = new CF_Connection($auth_obj);
* $conn->set_write_progress_function("write_callback");
* $container = $conn->create_container("stuff");
* $obj = $container->create_object("foo");
* $obj->write("The callback function will be called during upload.");
* # output would look like this:
* # >> uploaded 51 bytes.
* @param string $func_name the name of the user callback function
function set_write_progress_function($func_name)
$this->cfs_http->setWriteProgressFunc($func_name);
* Use the Certificate Authority bundle included with this API
* Most versions of PHP with cURL support include an outdated Certificate
* Authority (CA) bundle (the file that lists all valid certificate
* signing authorities). The SSL certificates used by the Cloud Files
* storage system are perfectly valid but have been created/signed by
* a CA not listed in these outdated cURL distributions.
* As a work-around, we've included an updated CA bundle obtained
* directly from cURL's web site (http://curl.haxx.se). You can direct
* the API to use this CA bundle by calling this method prior to making
* any remote calls. The best place to use this method is right after
* the CF_Authentication instance has been instantiated.
* You can specify your own CA bundle by passing in the full pathname
* to the bundle. You can use the included CA bundle by leaving the
* @param string $path Specify path to CA bundle (default to included)
function ssl_use_cabundle($path=NULL)
$this->cfs_http->ssl_use_cabundle($path);
#private function _re_auth()
# $new_auth = new CF_Authentication(
# $this->cfs_auth->username,
# $this->cfs_auth->api_key,
# $this->cfs_auth->auth_host,
# $this->cfs_auth->account);
# $new_auth->authenticate();
# $this->cfs_auth = $new_auth;
# $this->cfs_http->setCFAuth($this->cfs_auth);
* Containers are storage compartments where you put your data (objects).
* A container is similar to a directory or folder on a conventional filesystem
* with the exception that they exist in a flat namespace, you can not create
* containers inside of containers.
* You also have the option of marking a Container as "public" so that the
* Objects stored in the Container are publicly available via the CDN.
* @package php-cloudfiles
class UpdraftPlus_CF_Container
public $cdn_streaming_uri;
public $cdn_log_retention;
public $cdn_acl_user_agent;
public $cdn_acl_referrer;
* Constructor for Container
* @param obj $cfs_auth CF_Authentication instance
* @param obj $cfs_http HTTP connection manager
* @param string $name name of Container
* @param int $count number of Objects stored in this Container
* @param int $bytes number of bytes stored in this Container
* @throws SyntaxException invalid Container name
function __construct(&$cfs_auth, &$cfs_http, $name, $count=0,
if (strlen($name) > MAX_CONTAINER_NAME_LEN) {
throw new SyntaxException("Container name exceeds "
. "maximum allowed length.");
if (strpos($name, "/") !== False) {
throw new SyntaxException(
"Container names cannot contain a '/' character.");
$this->cfs_auth = $cfs_auth;
$this->cfs_http = $cfs_http;
$this->object_count = $count;
$this->bytes_used = $bytes;
$this->metadata = array();
$this->cdn_enabled = NULL;
$this->cdn_ssl_uri = NULL;
$this->cdn_streaming_uri = NULL;
$this->cdn_log_retention = NULL;
$this->cdn_acl_user_agent = NULL;
$this->cdn_acl_referrer = NULL;
if ($this->cfs_http->getCDNMUrl() != NULL && $docdn) {
$this->_cdn_initialize();
* String representation of Container
* Pretty print the Container instance.
* @return string Container details
$me = sprintf("name: %s, count: %.0f, bytes: %.0f",
$this->name, $this->object_count, $this->bytes_used);
if ($this->cfs_http->getCDNMUrl() != NULL) {
$me .= sprintf(", cdn: %s, cdn uri: %s, cdn ttl: %.0f, logs retention: %s",
$this->is_public() ? "Yes" : "No",
$this->cdn_uri, $this->cdn_ttl,
$this->cdn_log_retention ? "Yes" : "No"
if ($this->cdn_acl_user_agent != NULL) {
$me .= ", cdn acl user agent: " . $this->cdn_acl_user_agent;
if ($this->cdn_acl_referrer != NULL) {
$me .= ", cdn acl referrer: " . $this->cdn_acl_referrer;