* Copy a remote storage Object to a target Container
* Given an Object instance or name and a target Container instance or name, copy copies the remote Object
* and all associated metadata.
* # ... authentication code excluded (see previous examples) ...
* $conn = new CF_Connection($auth);
* $images = $conn->get_container("my photos");
* $images->copy_object_to("disco_dancing.jpg","container_target");
* @param obj $obj name or instance of Object to copy
* @param obj $container_target name or instance of target Container
* @param string $dest_obj_name name of target object (optional - uses source name if omitted)
* @param array $metadata metadata array for new object (optional)
* @param array $headers header fields array for the new object (optional)
* @return boolean <kbd>true</kbd> if successfully copied
* @throws SyntaxException invalid Object/Container name
* @throws NoSuchObjectException remote Object does not exist
* @throws InvalidResponseException unexpected response
function copy_object_to($obj,$container_target,$dest_obj_name=NULL,$metadata=NULL,$headers=NULL)
if (get_class($obj) == "UpdraftPlus_CF_Object") {
throw new SyntaxException("Object name not set.");
if ($dest_obj_name === NULL) {
$dest_obj_name = $obj_name;
$container_name_target = NULL;
if (is_object($container_target)) {
if (get_class($container_target) == "UpdraftPlus_CF_Container") {
$container_name_target = $container_target->name;
if (is_string($container_target)) {
$container_name_target = $container_target;
if (!$container_name_target) {
throw new SyntaxException("Container name target not set.");
$status = $this->cfs_http->copy_object($obj_name,$dest_obj_name,$this->name,$container_name_target,$metadata,$headers);
$m = "Specified object '".$this->name."/".$obj_name;
$m.= "' did not exist as source to copy from or '".$container_name_target."' did not exist as target to copy to.";
throw new NoSuchObjectException($m);
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
* Copy a remote storage Object from a source Container
* Given an Object instance or name and a source Container instance or name, copy copies the remote Object
* and all associated metadata.
* # ... authentication code excluded (see previous examples) ...
* $conn = new CF_Connection($auth);
* $images = $conn->get_container("my photos");
* $images->copy_object_from("disco_dancing.jpg","container_source");
* @param obj $obj name or instance of Object to copy
* @param obj $container_source name or instance of source Container
* @param string $dest_obj_name name of target object (optional - uses source name if omitted)
* @param array $metadata metadata array for new object (optional)
* @param array $headers header fields array for the new object (optional)
* @return boolean <kbd>true</kbd> if successfully copied
* @throws SyntaxException invalid Object/Container name
* @throws NoSuchObjectException remote Object does not exist
* @throws InvalidResponseException unexpected response
function copy_object_from($obj,$container_source,$dest_obj_name=NULL,$metadata=NULL,$headers=NULL)
if (get_class($obj) == "UpdraftPlus_CF_Object") {
throw new SyntaxException("Object name not set.");
if ($dest_obj_name === NULL) {
$dest_obj_name = $obj_name;
$container_name_source = NULL;
if (is_object($container_source)) {
if (get_class($container_source) == "UpdraftPlus_CF_Container") {
$container_name_source = $container_source->name;
if (is_string($container_source)) {
$container_name_source = $container_source;
if (!$container_name_source) {
throw new SyntaxException("Container name source not set.");
$status = $this->cfs_http->copy_object($obj_name,$dest_obj_name,$container_name_source,$this->name,$metadata,$headers);
$m = "Specified object '".$container_name_source."/".$obj_name;
$m.= "' did not exist as source to copy from or '".$this->name."/".$obj_name."' did not exist as target to copy to.";
throw new NoSuchObjectException($m);
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
* Move a remote storage Object to a target Container
* Given an Object instance or name and a target Container instance or name, move copies the remote Object
* and all associated metadata and deletes the source Object afterwards
* # ... authentication code excluded (see previous examples) ...
* $conn = new CF_Connection($auth);
* $images = $conn->get_container("my photos");
* $images->move_object_to("disco_dancing.jpg","container_target");
* @param obj $obj name or instance of Object to move
* @param obj $container_target name or instance of target Container
* @param string $dest_obj_name name of target object (optional - uses source name if omitted)
* @param array $metadata metadata array for new object (optional)
* @param array $headers header fields array for the new object (optional)
* @return boolean <kbd>true</kbd> if successfully moved
* @throws SyntaxException invalid Object/Container name
* @throws NoSuchObjectException remote Object does not exist
* @throws InvalidResponseException unexpected response
function move_object_to($obj,$container_target,$dest_obj_name=NULL,$metadata=NULL,$headers=NULL)
if(self::copy_object_to($obj,$container_target,$dest_obj_name,$metadata,$headers)) {
$retVal = self::delete_object($obj,$this->name);
* Move a remote storage Object from a source Container
* Given an Object instance or name and a source Container instance or name, move copies the remote Object
* and all associated metadata and deletes the source Object afterwards
* # ... authentication code excluded (see previous examples) ...
* $conn = new CF_Connection($auth);
* $images = $conn->get_container("my photos");
* $images->move_object_from("disco_dancing.jpg","container_target");
* @param obj $obj name or instance of Object to move
* @param obj $container_source name or instance of target Container
* @param string $dest_obj_name name of target object (optional - uses source name if omitted)
* @param array $metadata metadata array for new object (optional)
* @param array $headers header fields array for the new object (optional)
* @return boolean <kbd>true</kbd> if successfully moved
* @throws SyntaxException invalid Object/Container name
* @throws NoSuchObjectException remote Object does not exist
* @throws InvalidResponseException unexpected response
function move_object_from($obj,$container_source,$dest_obj_name=NULL,$metadata=NULL,$headers=NULL)
if(self::copy_object_from($obj,$container_source,$dest_obj_name,$metadata,$headers)) {
$retVal = self::delete_object($obj,$container_source);
* Delete a remote storage Object
* Given an Object instance or name, permanently remove the remote Object
* and all associated metadata.
* # ... authentication code excluded (see previous examples) ...
* $conn = new CF_Connection($auth);
* $images = $conn->get_container("my photos");
* # Delete specific object
* $images->delete_object("disco_dancing.jpg");
* @param obj $obj name or instance of Object to delete
* @param obj $container name or instance of Container in which the object resides (optional)
* @return boolean <kbd>True</kbd> if successfully removed
* @throws SyntaxException invalid Object name
* @throws NoSuchObjectException remote Object does not exist
* @throws InvalidResponseException unexpected response
function delete_object($obj,$container=NULL)
if (get_class($obj) == "UpdraftPlus_CF_Object") {
throw new SyntaxException("Object name not set.");
if($container === NULL) {
$container_name = $this->name;
if (is_object($container)) {
if (get_class($container) == "UpdraftPlus_CF_Container") {
$container_name = $container->name;
if (is_string($container)) {
$container_name = $container;
throw new SyntaxException("Container name source not set.");
$status = $this->cfs_http->delete_object($container_name, $obj_name);
#if ($status == 401 && $this->_re_auth()) {
# return $this->delete_object($obj);
$m = "Specified object '".$container_name."/".$obj_name;
$m.= "' did not exist to delete.";
throw new NoSuchObjectException($m);
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
* Helper function to create "path" elements for a given Object name
* Given an Object whos name contains '/' path separators, this function
* will create the "directory marker" Objects of one byte with the
* Content-Type of "application/directory".
* It assumes the last element of the full path is the "real" Object
* and does NOT create a remote storage Object for that last element.
function create_paths($path_name)
if ($path_name[0] == '/') {
$path_name = mb_substr($path_name, 0, 1);
$elements = explode('/', $path_name, -1);
foreach ($elements as $idx => $val) {
$build_path .= "/" . $val;
$obj = new UpdraftPlus_CF_Object($this, $build_path);
$obj->content_type = "application/directory";
* Internal method to grab CDN/Container info if appropriate to do so
* @throws InvalidResponseException unexpected response
private function _cdn_initialize()
list($status, $reason, $cdn_enabled, $cdn_ssl_uri, $cdn_streaming_uri, $cdn_uri, $cdn_ttl,
$cdn_log_retention, $cdn_acl_user_agent, $cdn_acl_referrer) =
$this->cfs_http->head_cdn_container($this->name);
#if ($status == 401 && $this->_re_auth()) {
# return $this->_cdn_initialize();
if (!in_array($status, array(204,404))) {
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
$this->cdn_enabled = $cdn_enabled;
$this->cdn_streaming_uri = $cdn_streaming_uri;
$this->cdn_ssl_uri = $cdn_ssl_uri;
$this->cdn_uri = $cdn_uri;
$this->cdn_ttl = $cdn_ttl;
$this->cdn_log_retention = $cdn_log_retention;
$this->cdn_acl_user_agent = $cdn_acl_user_agent;
$this->cdn_acl_referrer = $cdn_acl_referrer;
#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);
* An Object is analogous to a file on a conventional filesystem. You can
* read data from, or write data to your Objects. You can also associate
* arbitrary metadata with them.
* @package php-cloudfiles
class UpdraftPlus_CF_Object
* @param obj $container CF_Container instance
* @param string $name name of Object
* @param boolean $force_exists if set, throw an error if Object doesn't exist
function __construct(&$container, $name, $force_exists=False, $dohead=True)
$r = "Object name '".$name;
$r .= "' cannot contain begin with a '/' character.";
throw new SyntaxException($r);
if (strlen($name) > MAX_OBJECT_NAME_LEN) {
throw new SyntaxException("Object name exceeds "
. "maximum allowed length.");
$this->container = $container;
$this->_etag_override = False;
$this->last_modified = NULL;
$this->content_type = NULL;
$this->content_length = 0;
$this->metadata = array();
$this->headers = array();
if (!$this->_initialize() && $force_exists) {
throw new NoSuchObjectException("No such object '".$name."'");
* String representation of Object
* Pretty print the Object's location and name
* @return string Object information
return $this->container->name . "/" . $this->name;
* Internal check to get the proper mimetype.
* This function would go over the available PHP methods to get
* By default it will try to use the PHP fileinfo library which is
* available from PHP 5.3 or as an PECL extension
* (http://pecl.php.net/package/Fileinfo).
* It will get the magic file by default from the system wide file
* which is usually available in /usr/share/magic on Unix or try
* to use the file specified in the source directory of the API
* if fileinfo is not available it will try to use the internal
* mime_content_type function.
* @param string $handle name of file or buffer to guess the type from
* @return boolean <kbd>True</kbd> if successful
* @throws BadContentTypeException
function _guess_content_type($handle) {
if (function_exists("finfo_open")) {
$local_magic = dirname(__FILE__) . "/share/magic";
$finfo = @finfo_open(FILEINFO_MIME, $local_magic);
$finfo = @finfo_open(FILEINFO_MIME);
if (is_file((string)$handle))
$ct = @finfo_file($finfo, $handle);
$ct = @finfo_buffer($finfo, $handle);
/* PHP 5.3 fileinfo display extra information like
charset so we remove everything after the ; since
we are not into that stuff */
$extra_content_type_info = strpos($ct, "; ");
if ($extra_content_type_info)
$ct = substr($ct, 0, $extra_content_type_info);
if ($ct && $ct != 'application/octet-stream')
$this->content_type = $ct;