nextcloud/apps/files_external/lib/irods.php

153 lines
3.7 KiB
PHP
Raw Normal View History

<?php
/**
* Copyright (c) 2013 Thomas Müller <thomas.mueller@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OC\Files\Storage;
set_include_path(get_include_path() . PATH_SEPARATOR .
\OC_App::getAppPath('files_external') . '/3rdparty/irodsphp/prods/src');
ob_start();
2013-04-26 19:37:41 +04:00
require_once 'ProdsConfig.inc.php';
require_once 'ProdsStreamer.class.php';
ob_end_clean();
class iRODS extends \OC\Files\Storage\StreamWrapper{
private $password;
private $user;
private $host;
private $port;
private $zone;
private $root;
private $use_logon_credentials;
2013-04-26 19:37:41 +04:00
private $auth_mode;
public function __construct($params) {
2013-09-10 14:34:41 +04:00
if (isset($params['host'])) {
$this->host = $params['host'];
2013-09-10 14:34:41 +04:00
$this->port = isset($params['port']) ? $params['port'] : 1247;
$this->user = isset($params['user']) ? $params['user'] : '';
$this->password = isset($params['password']) ? $params['password'] : '';
2013-09-11 02:49:45 +04:00
$this->use_logon_credentials = $params['use_logon_credentials'] === 'true' ? true : false;
$this->zone = $params['zone'];
$this->auth_mode = isset($params['auth_mode']) ? $params['auth_mode'] : '';
$this->root = isset($params['root']) ? $params['root'] : '/';
if ( ! $this->root || $this->root[0] !== '/') {
$this->root='/'.$this->root;
}
// take user and password from the session
2013-09-11 02:49:45 +04:00
if ($this->use_logon_credentials && \OC::$session->exists('irods-credentials'))
{
2013-09-10 14:34:41 +04:00
$params = \OC::$session->get('irods-credentials');
$this->user = $params['uid'];
$this->password = $params['password'];
}
//create the root folder if necessary
if ( ! $this->is_dir('')) {
$this->mkdir('');
}
} else {
throw new \Exception();
}
}
public static function login( $params ) {
2013-09-10 14:34:41 +04:00
\OC::$session->set('irods-credentials', $params);
}
public function getId(){
return 'irods::' . $this->user . '@' . $this->host . '/' . $this->root;
}
/**
2013-07-16 13:35:48 +04:00
* construct the rods url
* @param string $path
* @return string
*/
public function constructUrl($path) {
$path = rtrim($path,'/');
if ( $path === '' || $path[0] !== '/') {
$path = '/'.$path;
}
// adding auth method
$userWithZone = $this->user.'.'.$this->zone;
if ($this->auth_mode !== '') {
$userWithZone .= '.'.$this->auth_mode;
2013-04-26 19:37:41 +04:00
}
2013-07-16 13:35:48 +04:00
// url wrapper schema is named rods
return 'rods://'.$userWithZone.':'.$this->password.'@'.$this->host.':'.$this->port.$this->root.$path;
}
public function filetype($path) {
return @filetype($this->constructUrl($path));
}
public function mkdir($path) {
return @mkdir($this->constructUrl($path));
}
public function touch($path, $mtime=null) {
// we cannot set a time
if ($mtime != null) {
return false;
}
$path = $this->constructUrl($path);
// if the file doesn't exist we create it
if (!file_exists($path)) {
file_put_contents($path, '');
return true;
}
// mtime updates are not supported
return false;
}
/**
* check if a file or folder has been updated since $time
* @param string $path
* @param int $time
* @return bool
*/
public function hasUpdated($path,$time) {
// this it a work around for folder mtimes -> we loop it's content
if ( $this->is_dir($path)) {
$actualTime=$this->collectionMTime($path);
return $actualTime>$time;
}
$actualTime=$this->filemtime($path);
return $actualTime>$time;
}
/**
* get the best guess for the modification time of an iRODS collection
*/
private function collectionMTime($path) {
$dh = $this->opendir($path);
$lastCTime = $this->filemtime($path);
while (($file = readdir($dh)) !== false) {
if ($file != '.' and $file != '..') {
$time = $this->filemtime($file);
if ($time > $lastCTime) {
$lastCTime = $time;
}
}
}
return $lastCTime;
}
}