~pvigo/+junk/owncloud-14.04

« back to all changes in this revision

Viewing changes to usr/share/owncloud/apps/files_external/lib/irods.php

  • Committer: Pablo Vigo
  • Date: 2014-12-15 13:36:46 UTC
  • Revision ID: pvigo@xtec.cat-20141215133646-7d6it90e1dbsijc2
2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * Copyright (c) 2013 Thomas Müller <thomas.mueller@owncloud.com>
4
 
 * This file is licensed under the Affero General Public License version 3 or
5
 
 * later.
6
 
 * See the COPYING-README file.
7
 
 */
8
 
 
9
 
namespace OC\Files\Storage;
10
 
 
11
 
set_include_path(get_include_path() . PATH_SEPARATOR .
12
 
        '/usr/share/php/irods/prods/src');
13
 
 
14
 
ob_start();
15
 
require_once 'ProdsConfig.inc.php';
16
 
require_once 'ProdsStreamer.class.php';
17
 
ob_end_clean();
18
 
 
19
 
class iRODS extends \OC\Files\Storage\StreamWrapper{
20
 
        private $password;
21
 
        private $user;
22
 
        private $host;
23
 
        private $port;
24
 
        private $zone;
25
 
        private $root;
26
 
        private $use_logon_credentials;
27
 
        private $auth_mode;
28
 
 
29
 
        public function __construct($params) {
30
 
                if (isset($params['host'])) {
31
 
                        $this->host = $params['host'];
32
 
                        $this->port = isset($params['port']) ? $params['port'] : 1247;
33
 
                        $this->user = isset($params['user']) ? $params['user'] : '';
34
 
                        $this->password = isset($params['password']) ? $params['password'] : '';
35
 
                        $this->use_logon_credentials = ($params['use_logon_credentials'] === 'true');
36
 
                        $this->zone = $params['zone'];
37
 
                        $this->auth_mode = isset($params['auth_mode']) ? $params['auth_mode'] : '';
38
 
 
39
 
                        $this->root = isset($params['root']) ? $params['root'] : '/';
40
 
                        if ( ! $this->root || $this->root[0] !== '/') {
41
 
                                $this->root='/'.$this->root;
42
 
                        }
43
 
 
44
 
                        // take user and password from the session
45
 
                        if ($this->use_logon_credentials && \OC::$session->exists('irods-credentials'))
46
 
                        {
47
 
                                $params = \OC::$session->get('irods-credentials');
48
 
                                $this->user = $params['uid'];
49
 
                                $this->password = $params['password'];
50
 
                        }
51
 
 
52
 
                        //create the root folder if necessary
53
 
                        if ( ! $this->is_dir('')) {
54
 
                                $this->mkdir('');
55
 
                        }
56
 
                } else {
57
 
                        throw new \Exception();
58
 
                }
59
 
 
60
 
        }
61
 
 
62
 
        public static function login( $params ) {
63
 
                \OC::$session->set('irods-credentials', $params);
64
 
        }
65
 
 
66
 
        public function getId(){
67
 
                return 'irods::' . $this->user . '@' . $this->host . '/' . $this->root;
68
 
        }
69
 
 
70
 
        /**
71
 
         * construct the rods url
72
 
         * @param string $path
73
 
         * @return string
74
 
         */
75
 
        public function constructUrl($path) {
76
 
                $path = rtrim($path,'/');
77
 
                if ( $path === '' || $path[0] !== '/') {
78
 
                        $path = '/'.$path;
79
 
                }
80
 
 
81
 
                // adding auth method
82
 
                $userWithZone = $this->user.'.'.$this->zone;
83
 
                if ($this->auth_mode !== '') {
84
 
                        $userWithZone .= '.'.$this->auth_mode;
85
 
                }
86
 
 
87
 
                // url wrapper schema is named rods
88
 
                return 'rods://'.$userWithZone.':'.$this->password.'@'.$this->host.':'.$this->port.$this->root.$path;
89
 
        }
90
 
 
91
 
        public function filetype($path) {
92
 
                return @filetype($this->constructUrl($path));
93
 
        }
94
 
 
95
 
        public function mkdir($path) {
96
 
                return @mkdir($this->constructUrl($path));
97
 
        }
98
 
 
99
 
        public function touch($path, $mtime=null) {
100
 
 
101
 
                // we cannot set a time
102
 
                if ($mtime != null) {
103
 
                        return false;
104
 
                }
105
 
 
106
 
                $path = $this->constructUrl($path);
107
 
 
108
 
                // if the file doesn't exist we create it
109
 
                if (!file_exists($path)) {
110
 
                        file_put_contents($path, '');
111
 
                        return true;
112
 
                }
113
 
 
114
 
                // mtime updates are not supported
115
 
                return false;
116
 
        }
117
 
 
118
 
        /**
119
 
         * check if a file or folder has been updated since $time
120
 
         * @param string $path
121
 
         * @param int $time
122
 
         * @return bool
123
 
         */
124
 
        public function hasUpdated($path,$time) {
125
 
                // this it a work around for folder mtimes -> we loop it's content
126
 
                if ( $this->is_dir($path)) {
127
 
                        $actualTime=$this->collectionMTime($path);
128
 
                        return $actualTime>$time;
129
 
                }
130
 
 
131
 
                $actualTime=$this->filemtime($path);
132
 
                return $actualTime>$time;
133
 
        }
134
 
 
135
 
        /**
136
 
         * get the best guess for the modification time of an iRODS collection
137
 
         */
138
 
        private function collectionMTime($path) {
139
 
                $dh = $this->opendir($path);
140
 
                $lastCTime = $this->filemtime($path);
141
 
                if(is_resource($dh)) {
142
 
                        while (($file = readdir($dh)) !== false) {
143
 
                                if ($file != '.' and $file != '..') {
144
 
                                        $time = $this->filemtime($file);
145
 
                                        if ($time > $lastCTime) {
146
 
                                                $lastCTime = $time;
147
 
                                        }
148
 
                                }
149
 
                        }
150
 
                }
151
 
                return $lastCTime;
152
 
        }
153
 
 
154
 
}