summaryrefslogtreecommitdiff
path: root/lib/Kolab/DAV/Node.php
blob: b8d3542a89384a27e4943c23ae644d1a8f68bb05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php

/**
 * SabreDAV File Backend implementation for Kolab.
 *
 * @author Aleksander Machniak <machniak@kolabsys.com>
 *
 * Copyright (C) 2013, Kolab Systems AG <contact@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

namespace Kolab\DAV;

use \rcube;
use \rcube_mime;
use \Exception;

/**
 * Node class
 */
class Node implements \Sabre\DAV\INode
{
    /**
     * The path to the current node
     *
     * @var string
     */
    protected $path;

    /**
     * The file API backend class
     *
     * @var file_api_storage
     */
    protected $backend;

    /**
     * Internal node data (e.g. file parameters)
     *
     * @var array
     */
    protected $data;

    /**
     * Parent node
     *
     * @var Kolab\DAV\Node
     */
    protected $parent;


    /**
     * @brief Sets up the node, expects a full path name
     * @param string         $path   Node name with path
     * @param Kolab\DAV\Node $parent Parent node
     * @param array          $data   Node data
     *
     * @return void
     */
    public function __construct($path, $parent = null, $data = array())
    {
        $this->data    = $data;
        $this->path    = $path;
        $this->parent  = $parent;
        $this->backend = Backend::get_instance()->get_backend();

        if ($this->path == Collection::ROOT_DIRECTORY) {
            $this->path = '';
        }
        else if (strpos($this->path, Collection::ROOT_DIRECTORY . '/') === 0) {
            $this->path = substr($this->path, strlen(Collection::ROOT_DIRECTORY . '/'));
        }
    }

    /**
     * Returns the last modification time
     *
     * In this case, it will simply return the current time
     *
     * @return int
     */
    public function getLastModified()
    {
        return $this->data['modified'] ? $this->data['modified'] : null;
    }

    /**
     * Deletes the current node (folder)
     *
     * @throws Sabre\DAV\Exception\Forbidden
     * @return void
     */
    public function delete()
    {
        try {
            $this->backend->folder_delete($this->path);
        }
        catch (Exception $e) {
            throw new \Sabre\DAV\Exception\Forbidden($e->getMessage());
        }

        // reset cache
        if ($this->parent) {
            $this->parent->children = null;
        }
    }

    /**
     * Renames the node
     *
     * @throws Sabre\DAV\Exception\Forbidden
     * @param string $name The new name
     * @return void
     */
    public function setName($name)
    {
        $path = explode('/', $this->path);
        array_pop($path);
        $newname = implode('/', $path) . '/' . $name;

        $method = (is_a($this, 'Kolab\\DAV\\File') ? 'file' : 'folder') . '_move';

        try {
            $this->backend->$method($this->path, $newname);
        }
        catch (Exception $e) {
            throw new \Sabre\DAV\Exception\Forbidden($e->getMessage());
        }

        // reset cache
        if ($this->parent) {
            $this->parent->children = null;
        }
    }

    /**
     * @brief Returns the name of the node
     * @return string
     */
    public function getName()
    {
        if ($this->path === '') {
            return Collection::ROOT_DIRECTORY;
        }

        return array_pop(explode('/', $this->path));
    }

    /**
     * Build file data array to pass into backend
     */
    protected function fileData($name, $data = null)
    {
        if ($this->data && $this->data['type']) {
            $type = $this->data['type'];
        }
        else {
            $type = 'application/octet-stream';
        }

        // $data can be a resource or a string
        if (is_resource($data)) {
            // $data can be php://input or php://temp
            // php://input is not seekable, we need to "convert"
            // it to seekable resource, fstat/rewind later will work
            $meta = stream_get_meta_data($data);
            if (!$meta['seekable']) {
                $new_data = fopen('php://temp','r+');
                stream_copy_to_stream($data, $new_data);
                rewind($new_data);
                $data = $new_data;
            }

            $content = stream_get_contents($data, 1024000, 0);
            rewind($data);
        }
        else {
            $content = &$data;
        }

        $filedata = array(
            'content' => $data,
            'type'    => rcube_mime::file_content_type($content, $name, $type, true),
        );

        return $filedata;
    }
}