nextcloud/apps/files_external/lib/swift.php

587 lines
13 KiB
PHP
Raw Normal View History

<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
2012-09-07 20:30:48 +04:00
namespace OC\Files\Storage;
require_once 'php-cloudfiles/cloudfiles.php';
2012-09-07 20:30:48 +04:00
class SWIFT extends \OC\Files\Storage\Common{
private $id;
private $host;
private $root;
private $user;
private $token;
private $secure;
private $ready = false;
/**
* @var \CF_Authentication auth
*/
private $auth;
/**
* @var \CF_Connection conn
*/
private $conn;
/**
* @var \CF_Container rootContainer
*/
private $rootContainer;
private static $tempFiles=array();
private $objects=array();
private $containers=array();
const SUBCONTAINER_FILE='.subcontainers';
/**
* translate directory path to container name
* @param string $path
* @return string
*/
2012-09-07 17:22:01 +04:00
private function getContainerName($path) {
2012-12-03 21:02:22 +04:00
$path=trim(trim($this->root, '/') . "/".$path, '/.');
2012-11-02 22:53:02 +04:00
return str_replace('/', '\\', $path);
}
/**
* get container by path
* @param string $path
* @return \CF_Container
*/
2012-09-07 17:22:01 +04:00
private function getContainer($path) {
if ($path=='' or $path=='/') {
return $this->rootContainer;
}
if (isset($this->containers[$path])) {
return $this->containers[$path];
}
try {
$container=$this->conn->get_container($this->getContainerName($path));
$this->containers[$path]=$container;
return $container;
2012-12-03 21:02:22 +04:00
} catch(\NoSuchContainerException $e) {
return null;
}
}
/**
* create container
* @param string $path
* @return \CF_Container
*/
2012-09-07 17:22:01 +04:00
private function createContainer($path) {
if ($path=='' or $path=='/' or $path=='.') {
return $this->conn->create_container($this->getContainerName($path));
}
$parent=dirname($path);
if ($parent=='' or $parent=='/' or $parent=='.') {
$parentContainer=$this->rootContainer;
} else {
if ( ! $this->containerExists($parent)) {
$parentContainer=$this->createContainer($parent);
} else {
$parentContainer=$this->getContainer($parent);
}
}
2012-10-24 00:53:54 +04:00
$this->addSubContainer($parentContainer, basename($path));
return $this->conn->create_container($this->getContainerName($path));
}
/**
* get object by path
* @param string $path
* @return \CF_Object
*/
2012-09-07 17:22:01 +04:00
private function getObject($path) {
if (isset($this->objects[$path])) {
return $this->objects[$path];
}
$container=$this->getContainer(dirname($path));
if (is_null($container)) {
return null;
} else {
2012-10-11 17:52:21 +04:00
if ($path=="/" or $path=='') {
return null;
}
try {
$obj=$container->get_object(basename($path));
$this->objects[$path]=$obj;
return $obj;
2012-12-03 21:02:22 +04:00
} catch(\NoSuchObjectException $e) {
return null;
}
}
}
/**
* get the names of all objects in a container
* @param CF_Container
* @return array
*/
2012-09-07 17:22:01 +04:00
private function getObjects($container) {
if (is_null($container)) {
return array();
} else {
$files=$container->get_objects();
foreach ($files as &$file) {
$file=$file->name;
}
return $files;
}
}
/**
* create object
* @param string $path
* @return \CF_Object
*/
2012-09-07 17:22:01 +04:00
private function createObject($path) {
$container=$this->getContainer(dirname($path));
if ( ! is_null($container)) {
2012-10-11 17:52:21 +04:00
$container=$this->createContainer(dirname($path));
}
return $container->create_object(basename($path));
}
/**
* check if an object exists
* @param string
* @return bool
*/
2012-09-07 17:22:01 +04:00
private function objectExists($path) {
return !is_null($this->getObject($path));
}
/**
* check if container for path exists
* @param string $path
* @return bool
*/
2012-09-07 17:22:01 +04:00
private function containerExists($path) {
return !is_null($this->getContainer($path));
}
/**
* get the list of emulated sub containers
* @param \CF_Container $container
* @return array
*/
2012-09-07 17:22:01 +04:00
private function getSubContainers($container) {
$tmpFile=\OCP\Files::tmpFile();
$obj=$this->getSubContainerFile($container);
try {
$obj->save_to_filename($tmpFile);
2012-12-03 21:02:22 +04:00
} catch(\Exception $e) {
return array();
}
$obj->save_to_filename($tmpFile);
$containers=file($tmpFile);
unlink($tmpFile);
foreach ($containers as &$sub) {
$sub=trim($sub);
}
return $containers;
}
/**
* add an emulated sub container
* @param \CF_Container $container
* @param string $name
* @return bool
*/
2012-11-02 22:53:02 +04:00
private function addSubContainer($container, $name) {
if ( ! $name) {
return false;
}
$tmpFile=\OCP\Files::tmpFile();
$obj=$this->getSubContainerFile($container);
try {
$obj->save_to_filename($tmpFile);
$containers=file($tmpFile);
foreach ($containers as &$sub) {
$sub=trim($sub);
}
2012-11-07 20:18:56 +04:00
if(array_search($name, $containers) !== false) {
unlink($tmpFile);
return false;
} else {
2012-11-04 14:10:46 +04:00
$fh=fopen($tmpFile, 'a');
2012-11-07 20:18:56 +04:00
fwrite($fh,$name . "\n");
}
2012-12-03 21:02:22 +04:00
} catch(\Exception $e) {
2012-11-07 20:18:56 +04:00
file_put_contents($tmpFile, $name . "\n");
}
$obj->load_from_filename($tmpFile);
unlink($tmpFile);
return true;
}
/**
* remove an emulated sub container
* @param \CF_Container $container
* @param string $name
* @return bool
*/
2012-11-02 22:53:02 +04:00
private function removeSubContainer($container, $name) {
if ( ! $name) {
return false;
}
$tmpFile=\OCP\Files::tmpFile();
$obj=$this->getSubContainerFile($container);
try {
$obj->save_to_filename($tmpFile);
$containers=file($tmpFile);
2012-12-03 21:02:22 +04:00
} catch (\Exception $e) {
return false;
}
foreach ($containers as &$sub) {
$sub=trim($sub);
}
2012-11-02 22:53:02 +04:00
$i=array_search($name, $containers);
if ($i===false) {
unlink($tmpFile);
return false;
} else {
unset($containers[$i]);
2012-11-02 22:53:02 +04:00
file_put_contents($tmpFile, implode("\n", $containers)."\n");
}
$obj->load_from_filename($tmpFile);
unlink($tmpFile);
return true;
}
/**
* ensure a subcontainer file exists and return it's object
* @param \CF_Container $container
* @return \CF_Object
*/
2012-09-07 17:22:01 +04:00
private function getSubContainerFile($container) {
try {
return $container->get_object(self::SUBCONTAINER_FILE);
} catch(NoSuchObjectException $e) {
return $container->create_object(self::SUBCONTAINER_FILE);
}
}
2012-09-07 17:22:01 +04:00
public function __construct($params) {
$this->token=$params['token'];
$this->host=$params['host'];
$this->user=$params['user'];
$this->root=isset($params['root'])?$params['root']:'/';
if (isset($params['secure'])) {
if (is_string($params['secure'])) {
$this->secure = ($params['secure'] === 'true');
} else {
$this->secure = (bool)$params['secure'];
}
} else {
$this->secure = false;
}
if ( ! $this->root || $this->root[0]!='/') {
$this->root='/'.$this->root;
}
}
private function init(){
if($this->ready){
return;
}
$this->ready = true;
2012-09-07 20:30:48 +04:00
$this->auth = new \CF_Authentication($this->user, $this->token, null, $this->host);
$this->auth->authenticate();
2012-08-29 10:42:49 +04:00
2012-09-07 20:30:48 +04:00
$this->conn = new \CF_Connection($this->auth);
if ( ! $this->containerExists('/')) {
$this->rootContainer=$this->createContainer('/');
} else {
$this->rootContainer=$this->getContainer('/');
}
}
public function getId(){
return $this->id;
}
2012-09-07 17:22:01 +04:00
public function mkdir($path) {
$this->init();
if ($this->containerExists($path)) {
return false;
} else {
$this->createContainer($path);
return true;
}
}
2012-09-07 17:22:01 +04:00
public function rmdir($path) {
$this->init();
2012-12-03 21:02:22 +04:00
if (!$this->containerExists($path)) {
return false;
} else {
$this->emptyContainer($path);
if ($path!='' and $path!='/') {
$parentContainer=$this->getContainer(dirname($path));
2012-10-24 00:53:54 +04:00
$this->removeSubContainer($parentContainer, basename($path));
}
2012-08-29 10:42:49 +04:00
$this->conn->delete_container($this->getContainerName($path));
unset($this->containers[$path]);
return true;
}
}
2012-09-07 17:22:01 +04:00
private function emptyContainer($path) {
$container=$this->getContainer($path);
if (is_null($container)) {
return;
}
$subContainers=$this->getSubContainers($container);
foreach ($subContainers as $sub) {
if ($sub) {
$this->emptyContainer($path.'/'.$sub);
$this->conn->delete_container($this->getContainerName($path.'/'.$sub));
unset($this->containers[$path.'/'.$sub]);
}
}
$objects=$this->getObjects($container);
foreach ($objects as $object) {
$container->delete_object($object);
unset($this->objects[$path.'/'.$object]);
}
}
2012-09-07 17:22:01 +04:00
public function opendir($path) {
$this->init();
$container=$this->getContainer($path);
$files=$this->getObjects($container);
2012-11-02 22:53:02 +04:00
$i=array_search(self::SUBCONTAINER_FILE, $files);
if ($i!==false) {
unset($files[$i]);
}
$subContainers=$this->getSubContainers($container);
2012-11-02 22:53:02 +04:00
$files=array_merge($files, $subContainers);
$id=$this->getContainerName($path);
2012-09-07 20:30:48 +04:00
\OC_FakeDirStream::$dirs[$id]=$files;
return opendir('fakedir://'.$id);
}
2012-09-07 17:22:01 +04:00
public function filetype($path) {
$this->init();
if ($this->containerExists($path)) {
return 'dir';
} else {
return 'file';
}
}
2012-09-07 17:22:01 +04:00
public function isReadable($path) {
return true;
}
2012-09-07 17:22:01 +04:00
public function isUpdatable($path) {
return true;
}
2012-09-07 17:22:01 +04:00
public function file_exists($path) {
$this->init();
if ($this->is_dir($path)) {
return true;
} else {
return $this->objectExists($path);
}
}
2012-09-07 17:22:01 +04:00
public function file_get_contents($path) {
$this->init();
$obj=$this->getObject($path);
if (is_null($obj)) {
return false;
}
return $obj->read();
}
2012-11-02 22:53:02 +04:00
public function file_put_contents($path, $content) {
$this->init();
$obj=$this->getObject($path);
if (is_null($obj)) {
$container=$this->getContainer(dirname($path));
if (is_null($container)) {
return false;
}
$obj=$container->create_object(basename($path));
}
$this->resetMTime($obj);
return $obj->write($content);
}
2012-09-07 17:22:01 +04:00
public function unlink($path) {
$this->init();
if ($this->containerExists($path)) {
2012-10-11 17:52:21 +04:00
return $this->rmdir($path);
}
if ($this->objectExists($path)) {
$container=$this->getContainer(dirname($path));
$container->delete_object(basename($path));
unset($this->objects[$path]);
} else {
return false;
}
}
2012-11-02 22:53:02 +04:00
public function fopen($path, $mode) {
$this->init();
2012-09-07 17:22:01 +04:00
switch($mode) {
case 'r':
case 'rb':
2012-10-11 17:52:21 +04:00
$obj=$this->getObject($path);
if (is_null($obj)) {
return false;
}
$fp = fopen('php://temp', 'r+');
$obj->stream($fp);
2012-08-29 10:42:49 +04:00
rewind($fp);
return $fp;
case 'w':
case 'wb':
case 'a':
case 'ab':
case 'r+':
case 'w+':
case 'wb+':
case 'a+':
case 'x':
case 'x+':
case 'c':
case 'c+':
$tmpFile=$this->getTmpFile($path);
2012-11-04 14:10:46 +04:00
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this, 'writeBack');
self::$tempFiles[$tmpFile]=$path;
2012-11-02 22:53:02 +04:00
return fopen('close://'.$tmpFile, $mode);
}
}
2012-09-07 17:22:01 +04:00
public function writeBack($tmpFile) {
if (isset(self::$tempFiles[$tmpFile])) {
2012-10-24 00:53:54 +04:00
$this->fromTmpFile($tmpFile, self::$tempFiles[$tmpFile]);
unlink($tmpFile);
}
}
2012-09-07 17:22:01 +04:00
public function free_space($path) {
2012-10-11 17:52:21 +04:00
return 1024*1024*1024*8;
}
2012-11-02 22:53:02 +04:00
public function touch($path, $mtime=null) {
$this->init();
$obj=$this->getObject($path);
if (is_null($obj)) {
return false;
}
if (is_null($mtime)) {
$mtime=time();
}
2012-08-29 10:42:49 +04:00
//emulate setting mtime with metadata
$obj->metadata['Mtime']=$mtime;
$obj->sync_metadata();
}
2012-11-02 22:53:02 +04:00
public function rename($path1, $path2) {
$this->init();
$sourceContainer=$this->getContainer(dirname($path1));
$targetContainer=$this->getContainer(dirname($path2));
2012-11-02 22:53:02 +04:00
$result=$sourceContainer->move_object_to(basename($path1), $targetContainer, basename($path2));
unset($this->objects[$path1]);
if ($result) {
$targetObj=$this->getObject($path2);
$this->resetMTime($targetObj);
}
return $result;
}
2012-11-02 22:53:02 +04:00
public function copy($path1, $path2) {
$this->init();
$sourceContainer=$this->getContainer(dirname($path1));
$targetContainer=$this->getContainer(dirname($path2));
2012-11-02 22:53:02 +04:00
$result=$sourceContainer->copy_object_to(basename($path1), $targetContainer, basename($path2));
if ($result) {
$targetObj=$this->getObject($path2);
$this->resetMTime($targetObj);
}
return $result;
}
2012-09-07 17:22:01 +04:00
public function stat($path) {
$this->init();
2012-10-11 17:52:21 +04:00
$container=$this->getContainer($path);
if ( ! is_null($container)) {
2012-10-11 17:52:21 +04:00
return array(
'mtime'=>-1,
'size'=>$container->bytes_used,
'ctime'=>-1
);
}
$obj=$this->getObject($path);
2012-10-11 17:52:21 +04:00
if (is_null($obj)) {
return false;
}
if (isset($obj->metadata['Mtime']) and $obj->metadata['Mtime']>-1) {
$mtime=$obj->metadata['Mtime'];
} else {
$mtime=strtotime($obj->last_modified);
}
return array(
'mtime'=>$mtime,
'size'=>$obj->content_length,
'ctime'=>-1,
);
}
2012-09-07 17:22:01 +04:00
private function getTmpFile($path) {
$this->init();
$obj=$this->getObject($path);
if ( ! is_null($obj)) {
$tmpFile=\OCP\Files::tmpFile();
$obj->save_to_filename($tmpFile);
return $tmpFile;
} else {
return \OCP\Files::tmpFile();
}
}
2012-11-02 22:53:02 +04:00
private function fromTmpFile($tmpFile, $path) {
$this->init();
$obj=$this->getObject($path);
if (is_null($obj)) {
$obj=$this->createObject($path);
}
$obj->load_from_filename($tmpFile);
$this->resetMTime($obj);
}
/**
* remove custom mtime metadata
* @param \CF_Object $obj
*/
2012-09-07 17:22:01 +04:00
private function resetMTime($obj) {
if (isset($obj->metadata['Mtime'])) {
$obj->metadata['Mtime']=-1;
$obj->sync_metadata();
}
}
}