@@ -83,6 +83,9 @@ |
||
83 | 83 | $this->isAdmin = $isAdmin; |
84 | 84 | } |
85 | 85 | |
86 | + /** |
|
87 | + * @param string $tagId |
|
88 | + */ |
|
86 | 89 | function createFile($tagId, $data = null) { |
87 | 90 | try { |
88 | 91 | if (!$this->isAdmin) { |
@@ -26,7 +26,6 @@ |
||
26 | 26 | use Sabre\DAV\Exception\BadRequest; |
27 | 27 | use Sabre\DAV\Exception\PreconditionFailed; |
28 | 28 | use Sabre\DAV\ICollection; |
29 | - |
|
30 | 29 | use OCP\SystemTag\ISystemTagManager; |
31 | 30 | use OCP\SystemTag\ISystemTagObjectMapper; |
32 | 31 | use OCP\SystemTag\ISystemTag; |
@@ -38,165 +38,165 @@ |
||
38 | 38 | */ |
39 | 39 | class SystemTagsObjectMappingCollection implements ICollection { |
40 | 40 | |
41 | - /** |
|
42 | - * @var string |
|
43 | - */ |
|
44 | - private $objectId; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - private $objectType; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var ISystemTagManager |
|
53 | - */ |
|
54 | - private $tagManager; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var ISystemTagObjectMapper |
|
58 | - */ |
|
59 | - private $tagMapper; |
|
60 | - |
|
61 | - /** |
|
62 | - * Whether to return results only visible for admins |
|
63 | - * |
|
64 | - * @var bool |
|
65 | - */ |
|
66 | - private $isAdmin; |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * Constructor |
|
71 | - * |
|
72 | - * @param string $objectId object id |
|
73 | - * @param string $objectType object type |
|
74 | - * @param bool $isAdmin whether to return results visible only for admins |
|
75 | - * @param ISystemTagManager $tagManager |
|
76 | - * @param ISystemTagObjectMapper $tagMapper |
|
77 | - */ |
|
78 | - public function __construct($objectId, $objectType, $isAdmin, $tagManager, $tagMapper) { |
|
79 | - $this->tagManager = $tagManager; |
|
80 | - $this->tagMapper = $tagMapper; |
|
81 | - $this->objectId = $objectId; |
|
82 | - $this->objectType = $objectType; |
|
83 | - $this->isAdmin = $isAdmin; |
|
84 | - } |
|
85 | - |
|
86 | - function createFile($tagId, $data = null) { |
|
87 | - try { |
|
88 | - if (!$this->isAdmin) { |
|
89 | - $tag = $this->tagManager->getTagsByIds($tagId); |
|
90 | - $tag = current($tag); |
|
91 | - if (!$tag->isUserVisible()) { |
|
92 | - throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign'); |
|
93 | - } |
|
94 | - if (!$tag->isUserAssignable()) { |
|
95 | - throw new Forbidden('No permission to assign tag ' . $tag->getId()); |
|
96 | - } |
|
97 | - } |
|
98 | - $this->tagMapper->assignTags($this->objectId, $this->objectType, $tagId); |
|
99 | - } catch (TagNotFoundException $e) { |
|
100 | - throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign'); |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - function createDirectory($name) { |
|
105 | - throw new Forbidden('Permission denied to create collections'); |
|
106 | - } |
|
107 | - |
|
108 | - function getChild($tagId) { |
|
109 | - try { |
|
110 | - if ($this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true)) { |
|
111 | - $tag = $this->tagManager->getTagsByIds([$tagId]); |
|
112 | - $tag = current($tag); |
|
113 | - if ($this->isAdmin || $tag->isUserVisible()) { |
|
114 | - return $this->makeNode($tag); |
|
115 | - } |
|
116 | - } |
|
117 | - throw new NotFound('Tag with id ' . $tagId . ' not present for object ' . $this->objectId); |
|
118 | - } catch (\InvalidArgumentException $e) { |
|
119 | - throw new BadRequest('Invalid tag id', 0, $e); |
|
120 | - } catch (TagNotFoundException $e) { |
|
121 | - throw new NotFound('Tag with id ' . $tagId . ' not found', 0, $e); |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - function getChildren() { |
|
126 | - $tagIds = current($this->tagMapper->getTagIdsForObjects([$this->objectId], $this->objectType)); |
|
127 | - if (empty($tagIds)) { |
|
128 | - return []; |
|
129 | - } |
|
130 | - $tags = $this->tagManager->getTagsByIds($tagIds); |
|
131 | - if (!$this->isAdmin) { |
|
132 | - // filter out non-visible tags |
|
133 | - $tags = array_filter($tags, function($tag) { |
|
134 | - return $tag->isUserVisible(); |
|
135 | - }); |
|
136 | - } |
|
137 | - return array_values(array_map(function($tag) { |
|
138 | - return $this->makeNode($tag); |
|
139 | - }, $tags)); |
|
140 | - } |
|
141 | - |
|
142 | - function childExists($tagId) { |
|
143 | - try { |
|
144 | - $result = ($this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true)); |
|
145 | - if ($this->isAdmin || !$result) { |
|
146 | - return $result; |
|
147 | - } |
|
148 | - |
|
149 | - // verify if user is allowed to see this tag |
|
150 | - $tag = $this->tagManager->getTagsByIds($tagId); |
|
151 | - $tag = current($tag); |
|
152 | - if (!$tag->isUserVisible()) { |
|
153 | - return false; |
|
154 | - } |
|
155 | - return true; |
|
156 | - } catch (\InvalidArgumentException $e) { |
|
157 | - throw new BadRequest('Invalid tag id', 0, $e); |
|
158 | - } catch (TagNotFoundException $e) { |
|
159 | - return false; |
|
160 | - } |
|
161 | - } |
|
162 | - |
|
163 | - function delete() { |
|
164 | - throw new Forbidden('Permission denied to delete this collection'); |
|
165 | - } |
|
166 | - |
|
167 | - function getName() { |
|
168 | - return $this->objectId; |
|
169 | - } |
|
170 | - |
|
171 | - function setName($name) { |
|
172 | - throw new Forbidden('Permission denied to rename this collection'); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Returns the last modification time, as a unix timestamp |
|
177 | - * |
|
178 | - * @return int |
|
179 | - */ |
|
180 | - function getLastModified() { |
|
181 | - return null; |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Create a sabre node for the mapping of the |
|
186 | - * given system tag to the collection's object |
|
187 | - * |
|
188 | - * @param ISystemTag $tag |
|
189 | - * |
|
190 | - * @return SystemTagNode |
|
191 | - */ |
|
192 | - private function makeNode(ISystemTag $tag) { |
|
193 | - return new SystemTagMappingNode( |
|
194 | - $tag, |
|
195 | - $this->objectId, |
|
196 | - $this->objectType, |
|
197 | - $this->isAdmin, |
|
198 | - $this->tagManager, |
|
199 | - $this->tagMapper |
|
200 | - ); |
|
201 | - } |
|
41 | + /** |
|
42 | + * @var string |
|
43 | + */ |
|
44 | + private $objectId; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + private $objectType; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var ISystemTagManager |
|
53 | + */ |
|
54 | + private $tagManager; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var ISystemTagObjectMapper |
|
58 | + */ |
|
59 | + private $tagMapper; |
|
60 | + |
|
61 | + /** |
|
62 | + * Whether to return results only visible for admins |
|
63 | + * |
|
64 | + * @var bool |
|
65 | + */ |
|
66 | + private $isAdmin; |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * Constructor |
|
71 | + * |
|
72 | + * @param string $objectId object id |
|
73 | + * @param string $objectType object type |
|
74 | + * @param bool $isAdmin whether to return results visible only for admins |
|
75 | + * @param ISystemTagManager $tagManager |
|
76 | + * @param ISystemTagObjectMapper $tagMapper |
|
77 | + */ |
|
78 | + public function __construct($objectId, $objectType, $isAdmin, $tagManager, $tagMapper) { |
|
79 | + $this->tagManager = $tagManager; |
|
80 | + $this->tagMapper = $tagMapper; |
|
81 | + $this->objectId = $objectId; |
|
82 | + $this->objectType = $objectType; |
|
83 | + $this->isAdmin = $isAdmin; |
|
84 | + } |
|
85 | + |
|
86 | + function createFile($tagId, $data = null) { |
|
87 | + try { |
|
88 | + if (!$this->isAdmin) { |
|
89 | + $tag = $this->tagManager->getTagsByIds($tagId); |
|
90 | + $tag = current($tag); |
|
91 | + if (!$tag->isUserVisible()) { |
|
92 | + throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign'); |
|
93 | + } |
|
94 | + if (!$tag->isUserAssignable()) { |
|
95 | + throw new Forbidden('No permission to assign tag ' . $tag->getId()); |
|
96 | + } |
|
97 | + } |
|
98 | + $this->tagMapper->assignTags($this->objectId, $this->objectType, $tagId); |
|
99 | + } catch (TagNotFoundException $e) { |
|
100 | + throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign'); |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + function createDirectory($name) { |
|
105 | + throw new Forbidden('Permission denied to create collections'); |
|
106 | + } |
|
107 | + |
|
108 | + function getChild($tagId) { |
|
109 | + try { |
|
110 | + if ($this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true)) { |
|
111 | + $tag = $this->tagManager->getTagsByIds([$tagId]); |
|
112 | + $tag = current($tag); |
|
113 | + if ($this->isAdmin || $tag->isUserVisible()) { |
|
114 | + return $this->makeNode($tag); |
|
115 | + } |
|
116 | + } |
|
117 | + throw new NotFound('Tag with id ' . $tagId . ' not present for object ' . $this->objectId); |
|
118 | + } catch (\InvalidArgumentException $e) { |
|
119 | + throw new BadRequest('Invalid tag id', 0, $e); |
|
120 | + } catch (TagNotFoundException $e) { |
|
121 | + throw new NotFound('Tag with id ' . $tagId . ' not found', 0, $e); |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + function getChildren() { |
|
126 | + $tagIds = current($this->tagMapper->getTagIdsForObjects([$this->objectId], $this->objectType)); |
|
127 | + if (empty($tagIds)) { |
|
128 | + return []; |
|
129 | + } |
|
130 | + $tags = $this->tagManager->getTagsByIds($tagIds); |
|
131 | + if (!$this->isAdmin) { |
|
132 | + // filter out non-visible tags |
|
133 | + $tags = array_filter($tags, function($tag) { |
|
134 | + return $tag->isUserVisible(); |
|
135 | + }); |
|
136 | + } |
|
137 | + return array_values(array_map(function($tag) { |
|
138 | + return $this->makeNode($tag); |
|
139 | + }, $tags)); |
|
140 | + } |
|
141 | + |
|
142 | + function childExists($tagId) { |
|
143 | + try { |
|
144 | + $result = ($this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true)); |
|
145 | + if ($this->isAdmin || !$result) { |
|
146 | + return $result; |
|
147 | + } |
|
148 | + |
|
149 | + // verify if user is allowed to see this tag |
|
150 | + $tag = $this->tagManager->getTagsByIds($tagId); |
|
151 | + $tag = current($tag); |
|
152 | + if (!$tag->isUserVisible()) { |
|
153 | + return false; |
|
154 | + } |
|
155 | + return true; |
|
156 | + } catch (\InvalidArgumentException $e) { |
|
157 | + throw new BadRequest('Invalid tag id', 0, $e); |
|
158 | + } catch (TagNotFoundException $e) { |
|
159 | + return false; |
|
160 | + } |
|
161 | + } |
|
162 | + |
|
163 | + function delete() { |
|
164 | + throw new Forbidden('Permission denied to delete this collection'); |
|
165 | + } |
|
166 | + |
|
167 | + function getName() { |
|
168 | + return $this->objectId; |
|
169 | + } |
|
170 | + |
|
171 | + function setName($name) { |
|
172 | + throw new Forbidden('Permission denied to rename this collection'); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Returns the last modification time, as a unix timestamp |
|
177 | + * |
|
178 | + * @return int |
|
179 | + */ |
|
180 | + function getLastModified() { |
|
181 | + return null; |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Create a sabre node for the mapping of the |
|
186 | + * given system tag to the collection's object |
|
187 | + * |
|
188 | + * @param ISystemTag $tag |
|
189 | + * |
|
190 | + * @return SystemTagNode |
|
191 | + */ |
|
192 | + private function makeNode(ISystemTag $tag) { |
|
193 | + return new SystemTagMappingNode( |
|
194 | + $tag, |
|
195 | + $this->objectId, |
|
196 | + $this->objectType, |
|
197 | + $this->isAdmin, |
|
198 | + $this->tagManager, |
|
199 | + $this->tagMapper |
|
200 | + ); |
|
201 | + } |
|
202 | 202 | } |
@@ -89,15 +89,15 @@ discard block |
||
89 | 89 | $tag = $this->tagManager->getTagsByIds($tagId); |
90 | 90 | $tag = current($tag); |
91 | 91 | if (!$tag->isUserVisible()) { |
92 | - throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign'); |
|
92 | + throw new PreconditionFailed('Tag with id '.$tagId.' does not exist, cannot assign'); |
|
93 | 93 | } |
94 | 94 | if (!$tag->isUserAssignable()) { |
95 | - throw new Forbidden('No permission to assign tag ' . $tag->getId()); |
|
95 | + throw new Forbidden('No permission to assign tag '.$tag->getId()); |
|
96 | 96 | } |
97 | 97 | } |
98 | 98 | $this->tagMapper->assignTags($this->objectId, $this->objectType, $tagId); |
99 | 99 | } catch (TagNotFoundException $e) { |
100 | - throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign'); |
|
100 | + throw new PreconditionFailed('Tag with id '.$tagId.' does not exist, cannot assign'); |
|
101 | 101 | } |
102 | 102 | } |
103 | 103 | |
@@ -114,11 +114,11 @@ discard block |
||
114 | 114 | return $this->makeNode($tag); |
115 | 115 | } |
116 | 116 | } |
117 | - throw new NotFound('Tag with id ' . $tagId . ' not present for object ' . $this->objectId); |
|
117 | + throw new NotFound('Tag with id '.$tagId.' not present for object '.$this->objectId); |
|
118 | 118 | } catch (\InvalidArgumentException $e) { |
119 | 119 | throw new BadRequest('Invalid tag id', 0, $e); |
120 | 120 | } catch (TagNotFoundException $e) { |
121 | - throw new NotFound('Tag with id ' . $tagId . ' not found', 0, $e); |
|
121 | + throw new NotFound('Tag with id '.$tagId.' not found', 0, $e); |
|
122 | 122 | } |
123 | 123 | } |
124 | 124 |
@@ -26,7 +26,6 @@ |
||
26 | 26 | use Sabre\DAV\Exception\MethodNotAllowed; |
27 | 27 | use Sabre\DAV\Exception\NotFound; |
28 | 28 | use Sabre\DAV\ICollection; |
29 | - |
|
30 | 29 | use OCP\SystemTag\ISystemTagManager; |
31 | 30 | use OCP\SystemTag\ISystemTagObjectMapper; |
32 | 31 | use OCP\IUserSession; |
@@ -39,146 +39,146 @@ |
||
39 | 39 | */ |
40 | 40 | class SystemTagsObjectTypeCollection implements ICollection { |
41 | 41 | |
42 | - /** |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - private $objectType; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var ISystemTagManager |
|
49 | - */ |
|
50 | - private $tagManager; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var ISystemTagObjectMapper |
|
54 | - */ |
|
55 | - private $tagMapper; |
|
56 | - |
|
57 | - /** |
|
58 | - * @var IGroupManager |
|
59 | - */ |
|
60 | - private $groupManager; |
|
61 | - |
|
62 | - /** |
|
63 | - * @var IUserSession |
|
64 | - */ |
|
65 | - private $userSession; |
|
66 | - |
|
67 | - /** |
|
68 | - * @var IRootFolder |
|
69 | - **/ |
|
70 | - protected $fileRoot; |
|
71 | - |
|
72 | - /** |
|
73 | - * Constructor |
|
74 | - * |
|
75 | - * @param string $objectType object type |
|
76 | - * @param ISystemTagManager $tagManager |
|
77 | - * @param ISystemTagObjectMapper $tagMapper |
|
78 | - * @param IUserSession $userSession |
|
79 | - * @param IGroupManager $groupManager |
|
80 | - * @param IRootFolder $fileRoot |
|
81 | - */ |
|
82 | - public function __construct( |
|
83 | - $objectType, |
|
84 | - ISystemTagManager $tagManager, |
|
85 | - ISystemTagObjectMapper $tagMapper, |
|
86 | - IUserSession $userSession, |
|
87 | - IGroupManager $groupManager, |
|
88 | - IRootFolder $fileRoot |
|
89 | - ) { |
|
90 | - $this->tagManager = $tagManager; |
|
91 | - $this->tagMapper = $tagMapper; |
|
92 | - $this->objectType = $objectType; |
|
93 | - $this->userSession = $userSession; |
|
94 | - $this->groupManager = $groupManager; |
|
95 | - $this->fileRoot = $fileRoot; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Returns whether the currently logged in user is an administrator |
|
100 | - */ |
|
101 | - private function isAdmin() { |
|
102 | - $user = $this->userSession->getUser(); |
|
103 | - if ($user !== null) { |
|
104 | - return $this->groupManager->isAdmin($user->getUID()); |
|
105 | - } |
|
106 | - return false; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @param string $name |
|
111 | - * @param resource|string $data Initial payload |
|
112 | - * @throws Forbidden |
|
113 | - */ |
|
114 | - function createFile($name, $data = null) { |
|
115 | - throw new Forbidden('Permission denied to create nodes'); |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @param string $name |
|
120 | - */ |
|
121 | - function createDirectory($name) { |
|
122 | - throw new Forbidden('Permission denied to create collections'); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param string $objectId |
|
127 | - */ |
|
128 | - function getChild($objectId) { |
|
129 | - // make sure the object exists and is reachable |
|
130 | - if(!$this->childExists($objectId)) { |
|
131 | - throw new NotFound('Entity does not exist or is not available'); |
|
132 | - } |
|
133 | - return new SystemTagsObjectMappingCollection( |
|
134 | - $objectId, |
|
135 | - $this->objectType, |
|
136 | - $this->isAdmin(), |
|
137 | - $this->tagManager, |
|
138 | - $this->tagMapper |
|
139 | - ); |
|
140 | - } |
|
141 | - |
|
142 | - function getChildren() { |
|
143 | - // do not list object ids |
|
144 | - throw new MethodNotAllowed(); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param string $name |
|
149 | - */ |
|
150 | - function childExists($name) { |
|
151 | - // TODO: make this more abstract |
|
152 | - if ($this->objectType === 'files') { |
|
153 | - // make sure the object is reachable for the current user |
|
154 | - $userId = $this->userSession->getUser()->getUID(); |
|
155 | - $nodes = $this->fileRoot->getUserFolder($userId)->getById(intval($name)); |
|
156 | - return !empty($nodes); |
|
157 | - } |
|
158 | - return true; |
|
159 | - } |
|
160 | - |
|
161 | - function delete() { |
|
162 | - throw new Forbidden('Permission denied to delete this collection'); |
|
163 | - } |
|
164 | - |
|
165 | - function getName() { |
|
166 | - return $this->objectType; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @param string $name |
|
171 | - */ |
|
172 | - function setName($name) { |
|
173 | - throw new Forbidden('Permission denied to rename this collection'); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Returns the last modification time, as a unix timestamp |
|
178 | - * |
|
179 | - * @return int |
|
180 | - */ |
|
181 | - function getLastModified() { |
|
182 | - return null; |
|
183 | - } |
|
42 | + /** |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + private $objectType; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var ISystemTagManager |
|
49 | + */ |
|
50 | + private $tagManager; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var ISystemTagObjectMapper |
|
54 | + */ |
|
55 | + private $tagMapper; |
|
56 | + |
|
57 | + /** |
|
58 | + * @var IGroupManager |
|
59 | + */ |
|
60 | + private $groupManager; |
|
61 | + |
|
62 | + /** |
|
63 | + * @var IUserSession |
|
64 | + */ |
|
65 | + private $userSession; |
|
66 | + |
|
67 | + /** |
|
68 | + * @var IRootFolder |
|
69 | + **/ |
|
70 | + protected $fileRoot; |
|
71 | + |
|
72 | + /** |
|
73 | + * Constructor |
|
74 | + * |
|
75 | + * @param string $objectType object type |
|
76 | + * @param ISystemTagManager $tagManager |
|
77 | + * @param ISystemTagObjectMapper $tagMapper |
|
78 | + * @param IUserSession $userSession |
|
79 | + * @param IGroupManager $groupManager |
|
80 | + * @param IRootFolder $fileRoot |
|
81 | + */ |
|
82 | + public function __construct( |
|
83 | + $objectType, |
|
84 | + ISystemTagManager $tagManager, |
|
85 | + ISystemTagObjectMapper $tagMapper, |
|
86 | + IUserSession $userSession, |
|
87 | + IGroupManager $groupManager, |
|
88 | + IRootFolder $fileRoot |
|
89 | + ) { |
|
90 | + $this->tagManager = $tagManager; |
|
91 | + $this->tagMapper = $tagMapper; |
|
92 | + $this->objectType = $objectType; |
|
93 | + $this->userSession = $userSession; |
|
94 | + $this->groupManager = $groupManager; |
|
95 | + $this->fileRoot = $fileRoot; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Returns whether the currently logged in user is an administrator |
|
100 | + */ |
|
101 | + private function isAdmin() { |
|
102 | + $user = $this->userSession->getUser(); |
|
103 | + if ($user !== null) { |
|
104 | + return $this->groupManager->isAdmin($user->getUID()); |
|
105 | + } |
|
106 | + return false; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @param string $name |
|
111 | + * @param resource|string $data Initial payload |
|
112 | + * @throws Forbidden |
|
113 | + */ |
|
114 | + function createFile($name, $data = null) { |
|
115 | + throw new Forbidden('Permission denied to create nodes'); |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @param string $name |
|
120 | + */ |
|
121 | + function createDirectory($name) { |
|
122 | + throw new Forbidden('Permission denied to create collections'); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param string $objectId |
|
127 | + */ |
|
128 | + function getChild($objectId) { |
|
129 | + // make sure the object exists and is reachable |
|
130 | + if(!$this->childExists($objectId)) { |
|
131 | + throw new NotFound('Entity does not exist or is not available'); |
|
132 | + } |
|
133 | + return new SystemTagsObjectMappingCollection( |
|
134 | + $objectId, |
|
135 | + $this->objectType, |
|
136 | + $this->isAdmin(), |
|
137 | + $this->tagManager, |
|
138 | + $this->tagMapper |
|
139 | + ); |
|
140 | + } |
|
141 | + |
|
142 | + function getChildren() { |
|
143 | + // do not list object ids |
|
144 | + throw new MethodNotAllowed(); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param string $name |
|
149 | + */ |
|
150 | + function childExists($name) { |
|
151 | + // TODO: make this more abstract |
|
152 | + if ($this->objectType === 'files') { |
|
153 | + // make sure the object is reachable for the current user |
|
154 | + $userId = $this->userSession->getUser()->getUID(); |
|
155 | + $nodes = $this->fileRoot->getUserFolder($userId)->getById(intval($name)); |
|
156 | + return !empty($nodes); |
|
157 | + } |
|
158 | + return true; |
|
159 | + } |
|
160 | + |
|
161 | + function delete() { |
|
162 | + throw new Forbidden('Permission denied to delete this collection'); |
|
163 | + } |
|
164 | + |
|
165 | + function getName() { |
|
166 | + return $this->objectType; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @param string $name |
|
171 | + */ |
|
172 | + function setName($name) { |
|
173 | + throw new Forbidden('Permission denied to rename this collection'); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Returns the last modification time, as a unix timestamp |
|
178 | + * |
|
179 | + * @return int |
|
180 | + */ |
|
181 | + function getLastModified() { |
|
182 | + return null; |
|
183 | + } |
|
184 | 184 | } |
@@ -120,7 +120,7 @@ |
||
120 | 120 | */ |
121 | 121 | function getChild($objectId) { |
122 | 122 | // make sure the object exists and is reachable |
123 | - if(!$this->childExists($objectId)) { |
|
123 | + if (!$this->childExists($objectId)) { |
|
124 | 124 | throw new NotFound('Entity does not exist or is not available'); |
125 | 125 | } |
126 | 126 | return new SystemTagsObjectMappingCollection( |
@@ -27,11 +27,8 @@ |
||
27 | 27 | use OCA\Files_external\NotFoundException; |
28 | 28 | use OCA\Files_external\Service\GlobalStoragesService; |
29 | 29 | use Symfony\Component\Console\Command\Command; |
30 | -use Symfony\Component\Console\Helper\Table; |
|
31 | -use Symfony\Component\Console\Helper\TableHelper; |
|
32 | 30 | use Symfony\Component\Console\Input\InputArgument; |
33 | 31 | use Symfony\Component\Console\Input\InputInterface; |
34 | -use Symfony\Component\Console\Input\InputOption; |
|
35 | 32 | use Symfony\Component\Console\Output\OutputInterface; |
36 | 33 | |
37 | 34 | class Config extends Base { |
@@ -32,87 +32,87 @@ |
||
32 | 32 | use Symfony\Component\Console\Output\OutputInterface; |
33 | 33 | |
34 | 34 | class Config extends Base { |
35 | - /** |
|
36 | - * @var GlobalStoragesService |
|
37 | - */ |
|
38 | - protected $globalService; |
|
35 | + /** |
|
36 | + * @var GlobalStoragesService |
|
37 | + */ |
|
38 | + protected $globalService; |
|
39 | 39 | |
40 | - function __construct(GlobalStoragesService $globalService) { |
|
41 | - parent::__construct(); |
|
42 | - $this->globalService = $globalService; |
|
43 | - } |
|
40 | + function __construct(GlobalStoragesService $globalService) { |
|
41 | + parent::__construct(); |
|
42 | + $this->globalService = $globalService; |
|
43 | + } |
|
44 | 44 | |
45 | - protected function configure() { |
|
46 | - $this |
|
47 | - ->setName('files_external:config') |
|
48 | - ->setDescription('Manage backend configuration for a mount') |
|
49 | - ->addArgument( |
|
50 | - 'mount_id', |
|
51 | - InputArgument::REQUIRED, |
|
52 | - 'The id of the mount to edit' |
|
53 | - )->addArgument( |
|
54 | - 'key', |
|
55 | - InputArgument::REQUIRED, |
|
56 | - 'key of the config option to set/get' |
|
57 | - )->addArgument( |
|
58 | - 'value', |
|
59 | - InputArgument::OPTIONAL, |
|
60 | - 'value to set the config option to, when no value is provided the existing value will be printed' |
|
61 | - ); |
|
62 | - parent::configure(); |
|
63 | - } |
|
45 | + protected function configure() { |
|
46 | + $this |
|
47 | + ->setName('files_external:config') |
|
48 | + ->setDescription('Manage backend configuration for a mount') |
|
49 | + ->addArgument( |
|
50 | + 'mount_id', |
|
51 | + InputArgument::REQUIRED, |
|
52 | + 'The id of the mount to edit' |
|
53 | + )->addArgument( |
|
54 | + 'key', |
|
55 | + InputArgument::REQUIRED, |
|
56 | + 'key of the config option to set/get' |
|
57 | + )->addArgument( |
|
58 | + 'value', |
|
59 | + InputArgument::OPTIONAL, |
|
60 | + 'value to set the config option to, when no value is provided the existing value will be printed' |
|
61 | + ); |
|
62 | + parent::configure(); |
|
63 | + } |
|
64 | 64 | |
65 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
66 | - $mountId = $input->getArgument('mount_id'); |
|
67 | - $key = $input->getArgument('key'); |
|
68 | - try { |
|
69 | - $mount = $this->globalService->getStorage($mountId); |
|
70 | - } catch (NotFoundException $e) { |
|
71 | - $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
72 | - return 404; |
|
73 | - } |
|
65 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
66 | + $mountId = $input->getArgument('mount_id'); |
|
67 | + $key = $input->getArgument('key'); |
|
68 | + try { |
|
69 | + $mount = $this->globalService->getStorage($mountId); |
|
70 | + } catch (NotFoundException $e) { |
|
71 | + $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
72 | + return 404; |
|
73 | + } |
|
74 | 74 | |
75 | - $value = $input->getArgument('value'); |
|
76 | - if ($value) { |
|
77 | - $this->setOption($mount, $key, $value, $output); |
|
78 | - } else { |
|
79 | - $this->getOption($mount, $key, $output); |
|
80 | - } |
|
81 | - } |
|
75 | + $value = $input->getArgument('value'); |
|
76 | + if ($value) { |
|
77 | + $this->setOption($mount, $key, $value, $output); |
|
78 | + } else { |
|
79 | + $this->getOption($mount, $key, $output); |
|
80 | + } |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * @param StorageConfig $mount |
|
85 | - * @param string $key |
|
86 | - * @param OutputInterface $output |
|
87 | - */ |
|
88 | - protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { |
|
89 | - if ($key === 'mountpoint' || $key === 'mount_point') { |
|
90 | - $value = $mount->getMountPoint(); |
|
91 | - } else { |
|
92 | - $value = $mount->getBackendOption($key); |
|
93 | - } |
|
94 | - if (!is_string($value)) { // show bools and objects correctly |
|
95 | - $value = json_encode($value); |
|
96 | - } |
|
97 | - $output->writeln($value); |
|
98 | - } |
|
83 | + /** |
|
84 | + * @param StorageConfig $mount |
|
85 | + * @param string $key |
|
86 | + * @param OutputInterface $output |
|
87 | + */ |
|
88 | + protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { |
|
89 | + if ($key === 'mountpoint' || $key === 'mount_point') { |
|
90 | + $value = $mount->getMountPoint(); |
|
91 | + } else { |
|
92 | + $value = $mount->getBackendOption($key); |
|
93 | + } |
|
94 | + if (!is_string($value)) { // show bools and objects correctly |
|
95 | + $value = json_encode($value); |
|
96 | + } |
|
97 | + $output->writeln($value); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * @param StorageConfig $mount |
|
102 | - * @param string $key |
|
103 | - * @param string $value |
|
104 | - * @param OutputInterface $output |
|
105 | - */ |
|
106 | - protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { |
|
107 | - $decoded = json_decode($value, true); |
|
108 | - if (!is_null($decoded)) { |
|
109 | - $value = $decoded; |
|
110 | - } |
|
111 | - if ($key === 'mountpoint' || $key === 'mount_point') { |
|
112 | - $mount->setMountPoint($value); |
|
113 | - } else { |
|
114 | - $mount->setBackendOption($key, $value); |
|
115 | - } |
|
116 | - $this->globalService->updateStorage($mount); |
|
117 | - } |
|
100 | + /** |
|
101 | + * @param StorageConfig $mount |
|
102 | + * @param string $key |
|
103 | + * @param string $value |
|
104 | + * @param OutputInterface $output |
|
105 | + */ |
|
106 | + protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { |
|
107 | + $decoded = json_decode($value, true); |
|
108 | + if (!is_null($decoded)) { |
|
109 | + $value = $decoded; |
|
110 | + } |
|
111 | + if ($key === 'mountpoint' || $key === 'mount_point') { |
|
112 | + $mount->setMountPoint($value); |
|
113 | + } else { |
|
114 | + $mount->setBackendOption($key, $value); |
|
115 | + } |
|
116 | + $this->globalService->updateStorage($mount); |
|
117 | + } |
|
118 | 118 | } |
@@ -68,7 +68,7 @@ |
||
68 | 68 | try { |
69 | 69 | $mount = $this->globalService->getStorage($mountId); |
70 | 70 | } catch (NotFoundException $e) { |
71 | - $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
71 | + $output->writeln('<error>Mount with id "'.$mountId.' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
72 | 72 | return 404; |
73 | 73 | } |
74 | 74 |
@@ -230,6 +230,9 @@ |
||
230 | 230 | return false; |
231 | 231 | } |
232 | 232 | |
233 | + /** |
|
234 | + * @param string $path |
|
235 | + */ |
|
233 | 236 | private function batchDelete ($path = null) { |
234 | 237 | $params = array( |
235 | 238 | 'Bucket' => $this->bucket |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | namespace OC\Files\Storage; |
38 | 38 | |
39 | 39 | set_include_path(get_include_path() . PATH_SEPARATOR . |
40 | - \OC_App::getAppPath('files_external') . '/3rdparty/aws-sdk-php'); |
|
40 | + \OC_App::getAppPath('files_external') . '/3rdparty/aws-sdk-php'); |
|
41 | 41 | require 'aws-autoloader.php'; |
42 | 42 | |
43 | 43 | use Aws\S3\S3Client; |
@@ -46,590 +46,590 @@ discard block |
||
46 | 46 | |
47 | 47 | class AmazonS3 extends \OC\Files\Storage\Common { |
48 | 48 | |
49 | - /** |
|
50 | - * @var \Aws\S3\S3Client |
|
51 | - */ |
|
52 | - private $connection; |
|
53 | - /** |
|
54 | - * @var string |
|
55 | - */ |
|
56 | - private $bucket; |
|
57 | - /** |
|
58 | - * @var array |
|
59 | - */ |
|
60 | - private static $tmpFiles = array(); |
|
61 | - /** |
|
62 | - * @var array |
|
63 | - */ |
|
64 | - private $params; |
|
65 | - /** |
|
66 | - * @var bool |
|
67 | - */ |
|
68 | - private $test = false; |
|
69 | - /** |
|
70 | - * @var int |
|
71 | - */ |
|
72 | - private $timeout = 15; |
|
73 | - /** |
|
74 | - * @var int in seconds |
|
75 | - */ |
|
76 | - private $rescanDelay = 10; |
|
77 | - |
|
78 | - /** |
|
79 | - * @param string $path |
|
80 | - * @return string correctly encoded path |
|
81 | - */ |
|
82 | - private function normalizePath($path) { |
|
83 | - $path = trim($path, '/'); |
|
84 | - |
|
85 | - if (!$path) { |
|
86 | - $path = '.'; |
|
87 | - } |
|
88 | - |
|
89 | - return $path; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * when running the tests wait to let the buckets catch up |
|
94 | - */ |
|
95 | - private function testTimeout() { |
|
96 | - if ($this->test) { |
|
97 | - sleep($this->timeout); |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - private function isRoot($path) { |
|
102 | - return $path === '.'; |
|
103 | - } |
|
104 | - |
|
105 | - private function cleanKey($path) { |
|
106 | - if ($this->isRoot($path)) { |
|
107 | - return '/'; |
|
108 | - } |
|
109 | - return $path; |
|
110 | - } |
|
111 | - |
|
112 | - public function __construct($params) { |
|
113 | - if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) { |
|
114 | - throw new \Exception("Access Key, Secret and Bucket have to be configured."); |
|
115 | - } |
|
116 | - |
|
117 | - $this->id = 'amazon::' . $params['bucket']; |
|
118 | - $this->updateLegacyId($params); |
|
119 | - |
|
120 | - $this->bucket = $params['bucket']; |
|
121 | - $this->test = isset($params['test']); |
|
122 | - $this->timeout = (!isset($params['timeout'])) ? 15 : $params['timeout']; |
|
123 | - $this->rescanDelay = (!isset($params['rescanDelay'])) ? 10 : $params['rescanDelay']; |
|
124 | - $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region']; |
|
125 | - $params['hostname'] = empty($params['hostname']) ? 's3.amazonaws.com' : $params['hostname']; |
|
126 | - if (!isset($params['port']) || $params['port'] === '') { |
|
127 | - $params['port'] = ($params['use_ssl'] === false) ? 80 : 443; |
|
128 | - } |
|
129 | - $this->params = $params; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Updates old storage ids (v0.2.1 and older) that are based on key and secret to new ones based on the bucket name. |
|
134 | - * TODO Do this in an update.php. requires iterating over all users and loading the mount.json from their home |
|
135 | - * |
|
136 | - * @param array $params |
|
137 | - */ |
|
138 | - public function updateLegacyId (array $params) { |
|
139 | - $oldId = 'amazon::' . $params['key'] . md5($params['secret']); |
|
140 | - |
|
141 | - // find by old id or bucket |
|
142 | - $stmt = \OC::$server->getDatabaseConnection()->prepare( |
|
143 | - 'SELECT `numeric_id`, `id` FROM `*PREFIX*storages` WHERE `id` IN (?, ?)' |
|
144 | - ); |
|
145 | - $stmt->execute(array($oldId, $this->id)); |
|
146 | - while ($row = $stmt->fetch()) { |
|
147 | - $storages[$row['id']] = $row['numeric_id']; |
|
148 | - } |
|
149 | - |
|
150 | - if (isset($storages[$this->id]) && isset($storages[$oldId])) { |
|
151 | - // if both ids exist, delete the old storage and corresponding filecache entries |
|
152 | - \OC\Files\Cache\Storage::remove($oldId); |
|
153 | - } else if (isset($storages[$oldId])) { |
|
154 | - // if only the old id exists do an update |
|
155 | - $stmt = \OC::$server->getDatabaseConnection()->prepare( |
|
156 | - 'UPDATE `*PREFIX*storages` SET `id` = ? WHERE `id` = ?' |
|
157 | - ); |
|
158 | - $stmt->execute(array($this->id, $oldId)); |
|
159 | - } |
|
160 | - // only the bucket based id may exist, do nothing |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Remove a file or folder |
|
165 | - * |
|
166 | - * @param string $path |
|
167 | - * @return bool |
|
168 | - */ |
|
169 | - protected function remove($path) { |
|
170 | - // remember fileType to reduce http calls |
|
171 | - $fileType = $this->filetype($path); |
|
172 | - if ($fileType === 'dir') { |
|
173 | - return $this->rmdir($path); |
|
174 | - } else if ($fileType === 'file') { |
|
175 | - return $this->unlink($path); |
|
176 | - } else { |
|
177 | - return false; |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - public function mkdir($path) { |
|
182 | - $path = $this->normalizePath($path); |
|
183 | - |
|
184 | - if ($this->is_dir($path)) { |
|
185 | - return false; |
|
186 | - } |
|
187 | - |
|
188 | - try { |
|
189 | - $this->getConnection()->putObject(array( |
|
190 | - 'Bucket' => $this->bucket, |
|
191 | - 'Key' => $path . '/', |
|
192 | - 'Body' => '', |
|
193 | - 'ContentType' => 'httpd/unix-directory' |
|
194 | - )); |
|
195 | - $this->testTimeout(); |
|
196 | - } catch (S3Exception $e) { |
|
197 | - \OCP\Util::logException('files_external', $e); |
|
198 | - return false; |
|
199 | - } |
|
200 | - |
|
201 | - return true; |
|
202 | - } |
|
203 | - |
|
204 | - public function file_exists($path) { |
|
205 | - return $this->filetype($path) !== false; |
|
206 | - } |
|
207 | - |
|
208 | - |
|
209 | - public function rmdir($path) { |
|
210 | - $path = $this->normalizePath($path); |
|
211 | - |
|
212 | - if ($this->isRoot($path)) { |
|
213 | - return $this->clearBucket(); |
|
214 | - } |
|
215 | - |
|
216 | - if (!$this->file_exists($path)) { |
|
217 | - return false; |
|
218 | - } |
|
219 | - |
|
220 | - return $this->batchDelete($path); |
|
221 | - } |
|
222 | - |
|
223 | - protected function clearBucket() { |
|
224 | - try { |
|
225 | - $this->getConnection()->clearBucket($this->bucket); |
|
226 | - return true; |
|
227 | - // clearBucket() is not working with Ceph, so if it fails we try the slower approach |
|
228 | - } catch (\Exception $e) { |
|
229 | - return $this->batchDelete(); |
|
230 | - } |
|
231 | - return false; |
|
232 | - } |
|
233 | - |
|
234 | - private function batchDelete ($path = null) { |
|
235 | - $params = array( |
|
236 | - 'Bucket' => $this->bucket |
|
237 | - ); |
|
238 | - if ($path !== null) { |
|
239 | - $params['Prefix'] = $path . '/'; |
|
240 | - } |
|
241 | - try { |
|
242 | - // Since there are no real directories on S3, we need |
|
243 | - // to delete all objects prefixed with the path. |
|
244 | - do { |
|
245 | - // instead of the iterator, manually loop over the list ... |
|
246 | - $objects = $this->getConnection()->listObjects($params); |
|
247 | - // ... so we can delete the files in batches |
|
248 | - $this->getConnection()->deleteObjects(array( |
|
249 | - 'Bucket' => $this->bucket, |
|
250 | - 'Objects' => $objects['Contents'] |
|
251 | - )); |
|
252 | - $this->testTimeout(); |
|
253 | - // we reached the end when the list is no longer truncated |
|
254 | - } while ($objects['IsTruncated']); |
|
255 | - } catch (S3Exception $e) { |
|
256 | - \OCP\Util::logException('files_external', $e); |
|
257 | - return false; |
|
258 | - } |
|
259 | - return true; |
|
260 | - } |
|
261 | - |
|
262 | - public function opendir($path) { |
|
263 | - $path = $this->normalizePath($path); |
|
264 | - |
|
265 | - if ($this->isRoot($path)) { |
|
266 | - $path = ''; |
|
267 | - } else { |
|
268 | - $path .= '/'; |
|
269 | - } |
|
270 | - |
|
271 | - try { |
|
272 | - $files = array(); |
|
273 | - $result = $this->getConnection()->getIterator('ListObjects', array( |
|
274 | - 'Bucket' => $this->bucket, |
|
275 | - 'Delimiter' => '/', |
|
276 | - 'Prefix' => $path |
|
277 | - ), array('return_prefixes' => true)); |
|
278 | - |
|
279 | - foreach ($result as $object) { |
|
280 | - if (isset($object['Key']) && $object['Key'] === $path) { |
|
281 | - // it's the directory itself, skip |
|
282 | - continue; |
|
283 | - } |
|
284 | - $file = basename( |
|
285 | - isset($object['Key']) ? $object['Key'] : $object['Prefix'] |
|
286 | - ); |
|
287 | - $files[] = $file; |
|
288 | - } |
|
289 | - |
|
290 | - return IteratorDirectory::wrap($files); |
|
291 | - } catch (S3Exception $e) { |
|
292 | - \OCP\Util::logException('files_external', $e); |
|
293 | - return false; |
|
294 | - } |
|
295 | - } |
|
296 | - |
|
297 | - public function stat($path) { |
|
298 | - $path = $this->normalizePath($path); |
|
299 | - |
|
300 | - try { |
|
301 | - $stat = array(); |
|
302 | - if ($this->is_dir($path)) { |
|
303 | - //folders don't really exist |
|
304 | - $stat['size'] = -1; //unknown |
|
305 | - $stat['mtime'] = time() - $this->rescanDelay * 1000; |
|
306 | - } else { |
|
307 | - $result = $this->getConnection()->headObject(array( |
|
308 | - 'Bucket' => $this->bucket, |
|
309 | - 'Key' => $path |
|
310 | - )); |
|
311 | - |
|
312 | - $stat['size'] = $result['ContentLength'] ? $result['ContentLength'] : 0; |
|
313 | - if ($result['Metadata']['lastmodified']) { |
|
314 | - $stat['mtime'] = strtotime($result['Metadata']['lastmodified']); |
|
315 | - } else { |
|
316 | - $stat['mtime'] = strtotime($result['LastModified']); |
|
317 | - } |
|
318 | - } |
|
319 | - $stat['atime'] = time(); |
|
320 | - |
|
321 | - return $stat; |
|
322 | - } catch(S3Exception $e) { |
|
323 | - \OCP\Util::logException('files_external', $e); |
|
324 | - return false; |
|
325 | - } |
|
326 | - } |
|
327 | - |
|
328 | - public function filetype($path) { |
|
329 | - $path = $this->normalizePath($path); |
|
330 | - |
|
331 | - if ($this->isRoot($path)) { |
|
332 | - return 'dir'; |
|
333 | - } |
|
334 | - |
|
335 | - try { |
|
336 | - if ($this->getConnection()->doesObjectExist($this->bucket, $path)) { |
|
337 | - return 'file'; |
|
338 | - } |
|
339 | - if ($this->getConnection()->doesObjectExist($this->bucket, $path.'/')) { |
|
340 | - return 'dir'; |
|
341 | - } |
|
342 | - } catch (S3Exception $e) { |
|
343 | - \OCP\Util::logException('files_external', $e); |
|
344 | - return false; |
|
345 | - } |
|
346 | - |
|
347 | - return false; |
|
348 | - } |
|
349 | - |
|
350 | - public function unlink($path) { |
|
351 | - $path = $this->normalizePath($path); |
|
352 | - |
|
353 | - if ($this->is_dir($path)) { |
|
354 | - return $this->rmdir($path); |
|
355 | - } |
|
356 | - |
|
357 | - try { |
|
358 | - $this->getConnection()->deleteObject(array( |
|
359 | - 'Bucket' => $this->bucket, |
|
360 | - 'Key' => $path |
|
361 | - )); |
|
362 | - $this->testTimeout(); |
|
363 | - } catch (S3Exception $e) { |
|
364 | - \OCP\Util::logException('files_external', $e); |
|
365 | - return false; |
|
366 | - } |
|
367 | - |
|
368 | - return true; |
|
369 | - } |
|
370 | - |
|
371 | - public function fopen($path, $mode) { |
|
372 | - $path = $this->normalizePath($path); |
|
373 | - |
|
374 | - switch ($mode) { |
|
375 | - case 'r': |
|
376 | - case 'rb': |
|
377 | - $tmpFile = \OCP\Files::tmpFile(); |
|
378 | - self::$tmpFiles[$tmpFile] = $path; |
|
379 | - |
|
380 | - try { |
|
381 | - $this->getConnection()->getObject(array( |
|
382 | - 'Bucket' => $this->bucket, |
|
383 | - 'Key' => $path, |
|
384 | - 'SaveAs' => $tmpFile |
|
385 | - )); |
|
386 | - } catch (S3Exception $e) { |
|
387 | - \OCP\Util::logException('files_external', $e); |
|
388 | - return false; |
|
389 | - } |
|
390 | - |
|
391 | - return fopen($tmpFile, 'r'); |
|
392 | - case 'w': |
|
393 | - case 'wb': |
|
394 | - case 'a': |
|
395 | - case 'ab': |
|
396 | - case 'r+': |
|
397 | - case 'w+': |
|
398 | - case 'wb+': |
|
399 | - case 'a+': |
|
400 | - case 'x': |
|
401 | - case 'x+': |
|
402 | - case 'c': |
|
403 | - case 'c+': |
|
404 | - if (strrpos($path, '.') !== false) { |
|
405 | - $ext = substr($path, strrpos($path, '.')); |
|
406 | - } else { |
|
407 | - $ext = ''; |
|
408 | - } |
|
409 | - $tmpFile = \OCP\Files::tmpFile($ext); |
|
410 | - \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack')); |
|
411 | - if ($this->file_exists($path)) { |
|
412 | - $source = $this->fopen($path, 'r'); |
|
413 | - file_put_contents($tmpFile, $source); |
|
414 | - } |
|
415 | - self::$tmpFiles[$tmpFile] = $path; |
|
416 | - |
|
417 | - return fopen('close://' . $tmpFile, $mode); |
|
418 | - } |
|
419 | - return false; |
|
420 | - } |
|
421 | - |
|
422 | - public function touch($path, $mtime = null) { |
|
423 | - $path = $this->normalizePath($path); |
|
424 | - |
|
425 | - $metadata = array(); |
|
426 | - if (is_null($mtime)) { |
|
427 | - $mtime = time(); |
|
428 | - } |
|
429 | - $metadata = [ |
|
430 | - 'lastmodified' => gmdate(\Aws\Common\Enum\DateFormat::RFC1123, $mtime) |
|
431 | - ]; |
|
432 | - |
|
433 | - $fileType = $this->filetype($path); |
|
434 | - try { |
|
435 | - if ($fileType !== false) { |
|
436 | - if ($fileType === 'dir' && ! $this->isRoot($path)) { |
|
437 | - $path .= '/'; |
|
438 | - } |
|
439 | - $this->getConnection()->copyObject([ |
|
440 | - 'Bucket' => $this->bucket, |
|
441 | - 'Key' => $this->cleanKey($path), |
|
442 | - 'Metadata' => $metadata, |
|
443 | - 'CopySource' => $this->bucket . '/' . $path, |
|
444 | - 'MetadataDirective' => 'REPLACE', |
|
445 | - ]); |
|
446 | - $this->testTimeout(); |
|
447 | - } else { |
|
448 | - $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
449 | - $this->getConnection()->putObject([ |
|
450 | - 'Bucket' => $this->bucket, |
|
451 | - 'Key' => $this->cleanKey($path), |
|
452 | - 'Metadata' => $metadata, |
|
453 | - 'Body' => '', |
|
454 | - 'ContentType' => $mimeType, |
|
455 | - 'MetadataDirective' => 'REPLACE', |
|
456 | - ]); |
|
457 | - $this->testTimeout(); |
|
458 | - } |
|
459 | - } catch (S3Exception $e) { |
|
460 | - \OCP\Util::logException('files_external', $e); |
|
461 | - return false; |
|
462 | - } |
|
463 | - |
|
464 | - return true; |
|
465 | - } |
|
466 | - |
|
467 | - public function copy($path1, $path2) { |
|
468 | - $path1 = $this->normalizePath($path1); |
|
469 | - $path2 = $this->normalizePath($path2); |
|
470 | - |
|
471 | - if ($this->is_file($path1)) { |
|
472 | - try { |
|
473 | - $this->getConnection()->copyObject(array( |
|
474 | - 'Bucket' => $this->bucket, |
|
475 | - 'Key' => $this->cleanKey($path2), |
|
476 | - 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1) |
|
477 | - )); |
|
478 | - $this->testTimeout(); |
|
479 | - } catch (S3Exception $e) { |
|
480 | - \OCP\Util::logException('files_external', $e); |
|
481 | - return false; |
|
482 | - } |
|
483 | - } else { |
|
484 | - $this->remove($path2); |
|
485 | - |
|
486 | - try { |
|
487 | - $this->getConnection()->copyObject(array( |
|
488 | - 'Bucket' => $this->bucket, |
|
489 | - 'Key' => $path2 . '/', |
|
490 | - 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1 . '/') |
|
491 | - )); |
|
492 | - $this->testTimeout(); |
|
493 | - } catch (S3Exception $e) { |
|
494 | - \OCP\Util::logException('files_external', $e); |
|
495 | - return false; |
|
496 | - } |
|
497 | - |
|
498 | - $dh = $this->opendir($path1); |
|
499 | - if (is_resource($dh)) { |
|
500 | - while (($file = readdir($dh)) !== false) { |
|
501 | - if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
502 | - continue; |
|
503 | - } |
|
504 | - |
|
505 | - $source = $path1 . '/' . $file; |
|
506 | - $target = $path2 . '/' . $file; |
|
507 | - $this->copy($source, $target); |
|
508 | - } |
|
509 | - } |
|
510 | - } |
|
511 | - |
|
512 | - return true; |
|
513 | - } |
|
514 | - |
|
515 | - public function rename($path1, $path2) { |
|
516 | - $path1 = $this->normalizePath($path1); |
|
517 | - $path2 = $this->normalizePath($path2); |
|
518 | - |
|
519 | - if ($this->is_file($path1)) { |
|
520 | - |
|
521 | - if ($this->copy($path1, $path2) === false) { |
|
522 | - return false; |
|
523 | - } |
|
524 | - |
|
525 | - if ($this->unlink($path1) === false) { |
|
526 | - $this->unlink($path2); |
|
527 | - return false; |
|
528 | - } |
|
529 | - } else { |
|
530 | - |
|
531 | - if ($this->copy($path1, $path2) === false) { |
|
532 | - return false; |
|
533 | - } |
|
534 | - |
|
535 | - if ($this->rmdir($path1) === false) { |
|
536 | - $this->rmdir($path2); |
|
537 | - return false; |
|
538 | - } |
|
539 | - } |
|
540 | - |
|
541 | - return true; |
|
542 | - } |
|
543 | - |
|
544 | - public function test() { |
|
545 | - $test = $this->getConnection()->getBucketAcl(array( |
|
546 | - 'Bucket' => $this->bucket, |
|
547 | - )); |
|
548 | - if (isset($test) && !is_null($test->getPath('Owner/ID'))) { |
|
549 | - return true; |
|
550 | - } |
|
551 | - return false; |
|
552 | - } |
|
553 | - |
|
554 | - public function getId() { |
|
555 | - return $this->id; |
|
556 | - } |
|
557 | - |
|
558 | - /** |
|
559 | - * Returns the connection |
|
560 | - * |
|
561 | - * @return S3Client connected client |
|
562 | - * @throws \Exception if connection could not be made |
|
563 | - */ |
|
564 | - public function getConnection() { |
|
565 | - if (!is_null($this->connection)) { |
|
566 | - return $this->connection; |
|
567 | - } |
|
568 | - |
|
569 | - $scheme = ($this->params['use_ssl'] === false) ? 'http' : 'https'; |
|
570 | - $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
|
571 | - |
|
572 | - $this->connection = S3Client::factory(array( |
|
573 | - 'key' => $this->params['key'], |
|
574 | - 'secret' => $this->params['secret'], |
|
575 | - 'base_url' => $base_url, |
|
576 | - 'region' => $this->params['region'], |
|
577 | - S3Client::COMMAND_PARAMS => [ |
|
578 | - 'PathStyle' => $this->params['use_path_style'], |
|
579 | - ], |
|
580 | - )); |
|
581 | - |
|
582 | - if (!$this->connection->isValidBucketName($this->bucket)) { |
|
583 | - throw new \Exception("The configured bucket name is invalid."); |
|
584 | - } |
|
585 | - |
|
586 | - if (!$this->connection->doesBucketExist($this->bucket)) { |
|
587 | - try { |
|
588 | - $this->connection->createBucket(array( |
|
589 | - 'Bucket' => $this->bucket |
|
590 | - )); |
|
591 | - $this->connection->waitUntilBucketExists(array( |
|
592 | - 'Bucket' => $this->bucket, |
|
593 | - 'waiter.interval' => 1, |
|
594 | - 'waiter.max_attempts' => 15 |
|
595 | - )); |
|
596 | - $this->testTimeout(); |
|
597 | - } catch (S3Exception $e) { |
|
598 | - \OCP\Util::logException('files_external', $e); |
|
599 | - throw new \Exception('Creation of bucket failed. '.$e->getMessage()); |
|
600 | - } |
|
601 | - } |
|
602 | - |
|
603 | - return $this->connection; |
|
604 | - } |
|
605 | - |
|
606 | - public function writeBack($tmpFile) { |
|
607 | - if (!isset(self::$tmpFiles[$tmpFile])) { |
|
608 | - return false; |
|
609 | - } |
|
610 | - |
|
611 | - try { |
|
612 | - $this->getConnection()->putObject(array( |
|
613 | - 'Bucket' => $this->bucket, |
|
614 | - 'Key' => $this->cleanKey(self::$tmpFiles[$tmpFile]), |
|
615 | - 'SourceFile' => $tmpFile, |
|
616 | - 'ContentType' => \OC::$server->getMimeTypeDetector()->detect($tmpFile), |
|
617 | - 'ContentLength' => filesize($tmpFile) |
|
618 | - )); |
|
619 | - $this->testTimeout(); |
|
620 | - |
|
621 | - unlink($tmpFile); |
|
622 | - } catch (S3Exception $e) { |
|
623 | - \OCP\Util::logException('files_external', $e); |
|
624 | - return false; |
|
625 | - } |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * check if curl is installed |
|
630 | - */ |
|
631 | - public static function checkDependencies() { |
|
632 | - return true; |
|
633 | - } |
|
49 | + /** |
|
50 | + * @var \Aws\S3\S3Client |
|
51 | + */ |
|
52 | + private $connection; |
|
53 | + /** |
|
54 | + * @var string |
|
55 | + */ |
|
56 | + private $bucket; |
|
57 | + /** |
|
58 | + * @var array |
|
59 | + */ |
|
60 | + private static $tmpFiles = array(); |
|
61 | + /** |
|
62 | + * @var array |
|
63 | + */ |
|
64 | + private $params; |
|
65 | + /** |
|
66 | + * @var bool |
|
67 | + */ |
|
68 | + private $test = false; |
|
69 | + /** |
|
70 | + * @var int |
|
71 | + */ |
|
72 | + private $timeout = 15; |
|
73 | + /** |
|
74 | + * @var int in seconds |
|
75 | + */ |
|
76 | + private $rescanDelay = 10; |
|
77 | + |
|
78 | + /** |
|
79 | + * @param string $path |
|
80 | + * @return string correctly encoded path |
|
81 | + */ |
|
82 | + private function normalizePath($path) { |
|
83 | + $path = trim($path, '/'); |
|
84 | + |
|
85 | + if (!$path) { |
|
86 | + $path = '.'; |
|
87 | + } |
|
88 | + |
|
89 | + return $path; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * when running the tests wait to let the buckets catch up |
|
94 | + */ |
|
95 | + private function testTimeout() { |
|
96 | + if ($this->test) { |
|
97 | + sleep($this->timeout); |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + private function isRoot($path) { |
|
102 | + return $path === '.'; |
|
103 | + } |
|
104 | + |
|
105 | + private function cleanKey($path) { |
|
106 | + if ($this->isRoot($path)) { |
|
107 | + return '/'; |
|
108 | + } |
|
109 | + return $path; |
|
110 | + } |
|
111 | + |
|
112 | + public function __construct($params) { |
|
113 | + if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) { |
|
114 | + throw new \Exception("Access Key, Secret and Bucket have to be configured."); |
|
115 | + } |
|
116 | + |
|
117 | + $this->id = 'amazon::' . $params['bucket']; |
|
118 | + $this->updateLegacyId($params); |
|
119 | + |
|
120 | + $this->bucket = $params['bucket']; |
|
121 | + $this->test = isset($params['test']); |
|
122 | + $this->timeout = (!isset($params['timeout'])) ? 15 : $params['timeout']; |
|
123 | + $this->rescanDelay = (!isset($params['rescanDelay'])) ? 10 : $params['rescanDelay']; |
|
124 | + $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region']; |
|
125 | + $params['hostname'] = empty($params['hostname']) ? 's3.amazonaws.com' : $params['hostname']; |
|
126 | + if (!isset($params['port']) || $params['port'] === '') { |
|
127 | + $params['port'] = ($params['use_ssl'] === false) ? 80 : 443; |
|
128 | + } |
|
129 | + $this->params = $params; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Updates old storage ids (v0.2.1 and older) that are based on key and secret to new ones based on the bucket name. |
|
134 | + * TODO Do this in an update.php. requires iterating over all users and loading the mount.json from their home |
|
135 | + * |
|
136 | + * @param array $params |
|
137 | + */ |
|
138 | + public function updateLegacyId (array $params) { |
|
139 | + $oldId = 'amazon::' . $params['key'] . md5($params['secret']); |
|
140 | + |
|
141 | + // find by old id or bucket |
|
142 | + $stmt = \OC::$server->getDatabaseConnection()->prepare( |
|
143 | + 'SELECT `numeric_id`, `id` FROM `*PREFIX*storages` WHERE `id` IN (?, ?)' |
|
144 | + ); |
|
145 | + $stmt->execute(array($oldId, $this->id)); |
|
146 | + while ($row = $stmt->fetch()) { |
|
147 | + $storages[$row['id']] = $row['numeric_id']; |
|
148 | + } |
|
149 | + |
|
150 | + if (isset($storages[$this->id]) && isset($storages[$oldId])) { |
|
151 | + // if both ids exist, delete the old storage and corresponding filecache entries |
|
152 | + \OC\Files\Cache\Storage::remove($oldId); |
|
153 | + } else if (isset($storages[$oldId])) { |
|
154 | + // if only the old id exists do an update |
|
155 | + $stmt = \OC::$server->getDatabaseConnection()->prepare( |
|
156 | + 'UPDATE `*PREFIX*storages` SET `id` = ? WHERE `id` = ?' |
|
157 | + ); |
|
158 | + $stmt->execute(array($this->id, $oldId)); |
|
159 | + } |
|
160 | + // only the bucket based id may exist, do nothing |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Remove a file or folder |
|
165 | + * |
|
166 | + * @param string $path |
|
167 | + * @return bool |
|
168 | + */ |
|
169 | + protected function remove($path) { |
|
170 | + // remember fileType to reduce http calls |
|
171 | + $fileType = $this->filetype($path); |
|
172 | + if ($fileType === 'dir') { |
|
173 | + return $this->rmdir($path); |
|
174 | + } else if ($fileType === 'file') { |
|
175 | + return $this->unlink($path); |
|
176 | + } else { |
|
177 | + return false; |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + public function mkdir($path) { |
|
182 | + $path = $this->normalizePath($path); |
|
183 | + |
|
184 | + if ($this->is_dir($path)) { |
|
185 | + return false; |
|
186 | + } |
|
187 | + |
|
188 | + try { |
|
189 | + $this->getConnection()->putObject(array( |
|
190 | + 'Bucket' => $this->bucket, |
|
191 | + 'Key' => $path . '/', |
|
192 | + 'Body' => '', |
|
193 | + 'ContentType' => 'httpd/unix-directory' |
|
194 | + )); |
|
195 | + $this->testTimeout(); |
|
196 | + } catch (S3Exception $e) { |
|
197 | + \OCP\Util::logException('files_external', $e); |
|
198 | + return false; |
|
199 | + } |
|
200 | + |
|
201 | + return true; |
|
202 | + } |
|
203 | + |
|
204 | + public function file_exists($path) { |
|
205 | + return $this->filetype($path) !== false; |
|
206 | + } |
|
207 | + |
|
208 | + |
|
209 | + public function rmdir($path) { |
|
210 | + $path = $this->normalizePath($path); |
|
211 | + |
|
212 | + if ($this->isRoot($path)) { |
|
213 | + return $this->clearBucket(); |
|
214 | + } |
|
215 | + |
|
216 | + if (!$this->file_exists($path)) { |
|
217 | + return false; |
|
218 | + } |
|
219 | + |
|
220 | + return $this->batchDelete($path); |
|
221 | + } |
|
222 | + |
|
223 | + protected function clearBucket() { |
|
224 | + try { |
|
225 | + $this->getConnection()->clearBucket($this->bucket); |
|
226 | + return true; |
|
227 | + // clearBucket() is not working with Ceph, so if it fails we try the slower approach |
|
228 | + } catch (\Exception $e) { |
|
229 | + return $this->batchDelete(); |
|
230 | + } |
|
231 | + return false; |
|
232 | + } |
|
233 | + |
|
234 | + private function batchDelete ($path = null) { |
|
235 | + $params = array( |
|
236 | + 'Bucket' => $this->bucket |
|
237 | + ); |
|
238 | + if ($path !== null) { |
|
239 | + $params['Prefix'] = $path . '/'; |
|
240 | + } |
|
241 | + try { |
|
242 | + // Since there are no real directories on S3, we need |
|
243 | + // to delete all objects prefixed with the path. |
|
244 | + do { |
|
245 | + // instead of the iterator, manually loop over the list ... |
|
246 | + $objects = $this->getConnection()->listObjects($params); |
|
247 | + // ... so we can delete the files in batches |
|
248 | + $this->getConnection()->deleteObjects(array( |
|
249 | + 'Bucket' => $this->bucket, |
|
250 | + 'Objects' => $objects['Contents'] |
|
251 | + )); |
|
252 | + $this->testTimeout(); |
|
253 | + // we reached the end when the list is no longer truncated |
|
254 | + } while ($objects['IsTruncated']); |
|
255 | + } catch (S3Exception $e) { |
|
256 | + \OCP\Util::logException('files_external', $e); |
|
257 | + return false; |
|
258 | + } |
|
259 | + return true; |
|
260 | + } |
|
261 | + |
|
262 | + public function opendir($path) { |
|
263 | + $path = $this->normalizePath($path); |
|
264 | + |
|
265 | + if ($this->isRoot($path)) { |
|
266 | + $path = ''; |
|
267 | + } else { |
|
268 | + $path .= '/'; |
|
269 | + } |
|
270 | + |
|
271 | + try { |
|
272 | + $files = array(); |
|
273 | + $result = $this->getConnection()->getIterator('ListObjects', array( |
|
274 | + 'Bucket' => $this->bucket, |
|
275 | + 'Delimiter' => '/', |
|
276 | + 'Prefix' => $path |
|
277 | + ), array('return_prefixes' => true)); |
|
278 | + |
|
279 | + foreach ($result as $object) { |
|
280 | + if (isset($object['Key']) && $object['Key'] === $path) { |
|
281 | + // it's the directory itself, skip |
|
282 | + continue; |
|
283 | + } |
|
284 | + $file = basename( |
|
285 | + isset($object['Key']) ? $object['Key'] : $object['Prefix'] |
|
286 | + ); |
|
287 | + $files[] = $file; |
|
288 | + } |
|
289 | + |
|
290 | + return IteratorDirectory::wrap($files); |
|
291 | + } catch (S3Exception $e) { |
|
292 | + \OCP\Util::logException('files_external', $e); |
|
293 | + return false; |
|
294 | + } |
|
295 | + } |
|
296 | + |
|
297 | + public function stat($path) { |
|
298 | + $path = $this->normalizePath($path); |
|
299 | + |
|
300 | + try { |
|
301 | + $stat = array(); |
|
302 | + if ($this->is_dir($path)) { |
|
303 | + //folders don't really exist |
|
304 | + $stat['size'] = -1; //unknown |
|
305 | + $stat['mtime'] = time() - $this->rescanDelay * 1000; |
|
306 | + } else { |
|
307 | + $result = $this->getConnection()->headObject(array( |
|
308 | + 'Bucket' => $this->bucket, |
|
309 | + 'Key' => $path |
|
310 | + )); |
|
311 | + |
|
312 | + $stat['size'] = $result['ContentLength'] ? $result['ContentLength'] : 0; |
|
313 | + if ($result['Metadata']['lastmodified']) { |
|
314 | + $stat['mtime'] = strtotime($result['Metadata']['lastmodified']); |
|
315 | + } else { |
|
316 | + $stat['mtime'] = strtotime($result['LastModified']); |
|
317 | + } |
|
318 | + } |
|
319 | + $stat['atime'] = time(); |
|
320 | + |
|
321 | + return $stat; |
|
322 | + } catch(S3Exception $e) { |
|
323 | + \OCP\Util::logException('files_external', $e); |
|
324 | + return false; |
|
325 | + } |
|
326 | + } |
|
327 | + |
|
328 | + public function filetype($path) { |
|
329 | + $path = $this->normalizePath($path); |
|
330 | + |
|
331 | + if ($this->isRoot($path)) { |
|
332 | + return 'dir'; |
|
333 | + } |
|
334 | + |
|
335 | + try { |
|
336 | + if ($this->getConnection()->doesObjectExist($this->bucket, $path)) { |
|
337 | + return 'file'; |
|
338 | + } |
|
339 | + if ($this->getConnection()->doesObjectExist($this->bucket, $path.'/')) { |
|
340 | + return 'dir'; |
|
341 | + } |
|
342 | + } catch (S3Exception $e) { |
|
343 | + \OCP\Util::logException('files_external', $e); |
|
344 | + return false; |
|
345 | + } |
|
346 | + |
|
347 | + return false; |
|
348 | + } |
|
349 | + |
|
350 | + public function unlink($path) { |
|
351 | + $path = $this->normalizePath($path); |
|
352 | + |
|
353 | + if ($this->is_dir($path)) { |
|
354 | + return $this->rmdir($path); |
|
355 | + } |
|
356 | + |
|
357 | + try { |
|
358 | + $this->getConnection()->deleteObject(array( |
|
359 | + 'Bucket' => $this->bucket, |
|
360 | + 'Key' => $path |
|
361 | + )); |
|
362 | + $this->testTimeout(); |
|
363 | + } catch (S3Exception $e) { |
|
364 | + \OCP\Util::logException('files_external', $e); |
|
365 | + return false; |
|
366 | + } |
|
367 | + |
|
368 | + return true; |
|
369 | + } |
|
370 | + |
|
371 | + public function fopen($path, $mode) { |
|
372 | + $path = $this->normalizePath($path); |
|
373 | + |
|
374 | + switch ($mode) { |
|
375 | + case 'r': |
|
376 | + case 'rb': |
|
377 | + $tmpFile = \OCP\Files::tmpFile(); |
|
378 | + self::$tmpFiles[$tmpFile] = $path; |
|
379 | + |
|
380 | + try { |
|
381 | + $this->getConnection()->getObject(array( |
|
382 | + 'Bucket' => $this->bucket, |
|
383 | + 'Key' => $path, |
|
384 | + 'SaveAs' => $tmpFile |
|
385 | + )); |
|
386 | + } catch (S3Exception $e) { |
|
387 | + \OCP\Util::logException('files_external', $e); |
|
388 | + return false; |
|
389 | + } |
|
390 | + |
|
391 | + return fopen($tmpFile, 'r'); |
|
392 | + case 'w': |
|
393 | + case 'wb': |
|
394 | + case 'a': |
|
395 | + case 'ab': |
|
396 | + case 'r+': |
|
397 | + case 'w+': |
|
398 | + case 'wb+': |
|
399 | + case 'a+': |
|
400 | + case 'x': |
|
401 | + case 'x+': |
|
402 | + case 'c': |
|
403 | + case 'c+': |
|
404 | + if (strrpos($path, '.') !== false) { |
|
405 | + $ext = substr($path, strrpos($path, '.')); |
|
406 | + } else { |
|
407 | + $ext = ''; |
|
408 | + } |
|
409 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
410 | + \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack')); |
|
411 | + if ($this->file_exists($path)) { |
|
412 | + $source = $this->fopen($path, 'r'); |
|
413 | + file_put_contents($tmpFile, $source); |
|
414 | + } |
|
415 | + self::$tmpFiles[$tmpFile] = $path; |
|
416 | + |
|
417 | + return fopen('close://' . $tmpFile, $mode); |
|
418 | + } |
|
419 | + return false; |
|
420 | + } |
|
421 | + |
|
422 | + public function touch($path, $mtime = null) { |
|
423 | + $path = $this->normalizePath($path); |
|
424 | + |
|
425 | + $metadata = array(); |
|
426 | + if (is_null($mtime)) { |
|
427 | + $mtime = time(); |
|
428 | + } |
|
429 | + $metadata = [ |
|
430 | + 'lastmodified' => gmdate(\Aws\Common\Enum\DateFormat::RFC1123, $mtime) |
|
431 | + ]; |
|
432 | + |
|
433 | + $fileType = $this->filetype($path); |
|
434 | + try { |
|
435 | + if ($fileType !== false) { |
|
436 | + if ($fileType === 'dir' && ! $this->isRoot($path)) { |
|
437 | + $path .= '/'; |
|
438 | + } |
|
439 | + $this->getConnection()->copyObject([ |
|
440 | + 'Bucket' => $this->bucket, |
|
441 | + 'Key' => $this->cleanKey($path), |
|
442 | + 'Metadata' => $metadata, |
|
443 | + 'CopySource' => $this->bucket . '/' . $path, |
|
444 | + 'MetadataDirective' => 'REPLACE', |
|
445 | + ]); |
|
446 | + $this->testTimeout(); |
|
447 | + } else { |
|
448 | + $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
449 | + $this->getConnection()->putObject([ |
|
450 | + 'Bucket' => $this->bucket, |
|
451 | + 'Key' => $this->cleanKey($path), |
|
452 | + 'Metadata' => $metadata, |
|
453 | + 'Body' => '', |
|
454 | + 'ContentType' => $mimeType, |
|
455 | + 'MetadataDirective' => 'REPLACE', |
|
456 | + ]); |
|
457 | + $this->testTimeout(); |
|
458 | + } |
|
459 | + } catch (S3Exception $e) { |
|
460 | + \OCP\Util::logException('files_external', $e); |
|
461 | + return false; |
|
462 | + } |
|
463 | + |
|
464 | + return true; |
|
465 | + } |
|
466 | + |
|
467 | + public function copy($path1, $path2) { |
|
468 | + $path1 = $this->normalizePath($path1); |
|
469 | + $path2 = $this->normalizePath($path2); |
|
470 | + |
|
471 | + if ($this->is_file($path1)) { |
|
472 | + try { |
|
473 | + $this->getConnection()->copyObject(array( |
|
474 | + 'Bucket' => $this->bucket, |
|
475 | + 'Key' => $this->cleanKey($path2), |
|
476 | + 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1) |
|
477 | + )); |
|
478 | + $this->testTimeout(); |
|
479 | + } catch (S3Exception $e) { |
|
480 | + \OCP\Util::logException('files_external', $e); |
|
481 | + return false; |
|
482 | + } |
|
483 | + } else { |
|
484 | + $this->remove($path2); |
|
485 | + |
|
486 | + try { |
|
487 | + $this->getConnection()->copyObject(array( |
|
488 | + 'Bucket' => $this->bucket, |
|
489 | + 'Key' => $path2 . '/', |
|
490 | + 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1 . '/') |
|
491 | + )); |
|
492 | + $this->testTimeout(); |
|
493 | + } catch (S3Exception $e) { |
|
494 | + \OCP\Util::logException('files_external', $e); |
|
495 | + return false; |
|
496 | + } |
|
497 | + |
|
498 | + $dh = $this->opendir($path1); |
|
499 | + if (is_resource($dh)) { |
|
500 | + while (($file = readdir($dh)) !== false) { |
|
501 | + if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
502 | + continue; |
|
503 | + } |
|
504 | + |
|
505 | + $source = $path1 . '/' . $file; |
|
506 | + $target = $path2 . '/' . $file; |
|
507 | + $this->copy($source, $target); |
|
508 | + } |
|
509 | + } |
|
510 | + } |
|
511 | + |
|
512 | + return true; |
|
513 | + } |
|
514 | + |
|
515 | + public function rename($path1, $path2) { |
|
516 | + $path1 = $this->normalizePath($path1); |
|
517 | + $path2 = $this->normalizePath($path2); |
|
518 | + |
|
519 | + if ($this->is_file($path1)) { |
|
520 | + |
|
521 | + if ($this->copy($path1, $path2) === false) { |
|
522 | + return false; |
|
523 | + } |
|
524 | + |
|
525 | + if ($this->unlink($path1) === false) { |
|
526 | + $this->unlink($path2); |
|
527 | + return false; |
|
528 | + } |
|
529 | + } else { |
|
530 | + |
|
531 | + if ($this->copy($path1, $path2) === false) { |
|
532 | + return false; |
|
533 | + } |
|
534 | + |
|
535 | + if ($this->rmdir($path1) === false) { |
|
536 | + $this->rmdir($path2); |
|
537 | + return false; |
|
538 | + } |
|
539 | + } |
|
540 | + |
|
541 | + return true; |
|
542 | + } |
|
543 | + |
|
544 | + public function test() { |
|
545 | + $test = $this->getConnection()->getBucketAcl(array( |
|
546 | + 'Bucket' => $this->bucket, |
|
547 | + )); |
|
548 | + if (isset($test) && !is_null($test->getPath('Owner/ID'))) { |
|
549 | + return true; |
|
550 | + } |
|
551 | + return false; |
|
552 | + } |
|
553 | + |
|
554 | + public function getId() { |
|
555 | + return $this->id; |
|
556 | + } |
|
557 | + |
|
558 | + /** |
|
559 | + * Returns the connection |
|
560 | + * |
|
561 | + * @return S3Client connected client |
|
562 | + * @throws \Exception if connection could not be made |
|
563 | + */ |
|
564 | + public function getConnection() { |
|
565 | + if (!is_null($this->connection)) { |
|
566 | + return $this->connection; |
|
567 | + } |
|
568 | + |
|
569 | + $scheme = ($this->params['use_ssl'] === false) ? 'http' : 'https'; |
|
570 | + $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
|
571 | + |
|
572 | + $this->connection = S3Client::factory(array( |
|
573 | + 'key' => $this->params['key'], |
|
574 | + 'secret' => $this->params['secret'], |
|
575 | + 'base_url' => $base_url, |
|
576 | + 'region' => $this->params['region'], |
|
577 | + S3Client::COMMAND_PARAMS => [ |
|
578 | + 'PathStyle' => $this->params['use_path_style'], |
|
579 | + ], |
|
580 | + )); |
|
581 | + |
|
582 | + if (!$this->connection->isValidBucketName($this->bucket)) { |
|
583 | + throw new \Exception("The configured bucket name is invalid."); |
|
584 | + } |
|
585 | + |
|
586 | + if (!$this->connection->doesBucketExist($this->bucket)) { |
|
587 | + try { |
|
588 | + $this->connection->createBucket(array( |
|
589 | + 'Bucket' => $this->bucket |
|
590 | + )); |
|
591 | + $this->connection->waitUntilBucketExists(array( |
|
592 | + 'Bucket' => $this->bucket, |
|
593 | + 'waiter.interval' => 1, |
|
594 | + 'waiter.max_attempts' => 15 |
|
595 | + )); |
|
596 | + $this->testTimeout(); |
|
597 | + } catch (S3Exception $e) { |
|
598 | + \OCP\Util::logException('files_external', $e); |
|
599 | + throw new \Exception('Creation of bucket failed. '.$e->getMessage()); |
|
600 | + } |
|
601 | + } |
|
602 | + |
|
603 | + return $this->connection; |
|
604 | + } |
|
605 | + |
|
606 | + public function writeBack($tmpFile) { |
|
607 | + if (!isset(self::$tmpFiles[$tmpFile])) { |
|
608 | + return false; |
|
609 | + } |
|
610 | + |
|
611 | + try { |
|
612 | + $this->getConnection()->putObject(array( |
|
613 | + 'Bucket' => $this->bucket, |
|
614 | + 'Key' => $this->cleanKey(self::$tmpFiles[$tmpFile]), |
|
615 | + 'SourceFile' => $tmpFile, |
|
616 | + 'ContentType' => \OC::$server->getMimeTypeDetector()->detect($tmpFile), |
|
617 | + 'ContentLength' => filesize($tmpFile) |
|
618 | + )); |
|
619 | + $this->testTimeout(); |
|
620 | + |
|
621 | + unlink($tmpFile); |
|
622 | + } catch (S3Exception $e) { |
|
623 | + \OCP\Util::logException('files_external', $e); |
|
624 | + return false; |
|
625 | + } |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * check if curl is installed |
|
630 | + */ |
|
631 | + public static function checkDependencies() { |
|
632 | + return true; |
|
633 | + } |
|
634 | 634 | |
635 | 635 | } |
@@ -36,8 +36,8 @@ discard block |
||
36 | 36 | |
37 | 37 | namespace OC\Files\Storage; |
38 | 38 | |
39 | -set_include_path(get_include_path() . PATH_SEPARATOR . |
|
40 | - \OC_App::getAppPath('files_external') . '/3rdparty/aws-sdk-php'); |
|
39 | +set_include_path(get_include_path().PATH_SEPARATOR. |
|
40 | + \OC_App::getAppPath('files_external').'/3rdparty/aws-sdk-php'); |
|
41 | 41 | require 'aws-autoloader.php'; |
42 | 42 | |
43 | 43 | use Aws\S3\S3Client; |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | throw new \Exception("Access Key, Secret and Bucket have to be configured."); |
115 | 115 | } |
116 | 116 | |
117 | - $this->id = 'amazon::' . $params['bucket']; |
|
117 | + $this->id = 'amazon::'.$params['bucket']; |
|
118 | 118 | $this->updateLegacyId($params); |
119 | 119 | |
120 | 120 | $this->bucket = $params['bucket']; |
@@ -135,8 +135,8 @@ discard block |
||
135 | 135 | * |
136 | 136 | * @param array $params |
137 | 137 | */ |
138 | - public function updateLegacyId (array $params) { |
|
139 | - $oldId = 'amazon::' . $params['key'] . md5($params['secret']); |
|
138 | + public function updateLegacyId(array $params) { |
|
139 | + $oldId = 'amazon::'.$params['key'].md5($params['secret']); |
|
140 | 140 | |
141 | 141 | // find by old id or bucket |
142 | 142 | $stmt = \OC::$server->getDatabaseConnection()->prepare( |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | try { |
189 | 189 | $this->getConnection()->putObject(array( |
190 | 190 | 'Bucket' => $this->bucket, |
191 | - 'Key' => $path . '/', |
|
191 | + 'Key' => $path.'/', |
|
192 | 192 | 'Body' => '', |
193 | 193 | 'ContentType' => 'httpd/unix-directory' |
194 | 194 | )); |
@@ -231,12 +231,12 @@ discard block |
||
231 | 231 | return false; |
232 | 232 | } |
233 | 233 | |
234 | - private function batchDelete ($path = null) { |
|
234 | + private function batchDelete($path = null) { |
|
235 | 235 | $params = array( |
236 | 236 | 'Bucket' => $this->bucket |
237 | 237 | ); |
238 | 238 | if ($path !== null) { |
239 | - $params['Prefix'] = $path . '/'; |
|
239 | + $params['Prefix'] = $path.'/'; |
|
240 | 240 | } |
241 | 241 | try { |
242 | 242 | // Since there are no real directories on S3, we need |
@@ -319,7 +319,7 @@ discard block |
||
319 | 319 | $stat['atime'] = time(); |
320 | 320 | |
321 | 321 | return $stat; |
322 | - } catch(S3Exception $e) { |
|
322 | + } catch (S3Exception $e) { |
|
323 | 323 | \OCP\Util::logException('files_external', $e); |
324 | 324 | return false; |
325 | 325 | } |
@@ -414,7 +414,7 @@ discard block |
||
414 | 414 | } |
415 | 415 | self::$tmpFiles[$tmpFile] = $path; |
416 | 416 | |
417 | - return fopen('close://' . $tmpFile, $mode); |
|
417 | + return fopen('close://'.$tmpFile, $mode); |
|
418 | 418 | } |
419 | 419 | return false; |
420 | 420 | } |
@@ -433,14 +433,14 @@ discard block |
||
433 | 433 | $fileType = $this->filetype($path); |
434 | 434 | try { |
435 | 435 | if ($fileType !== false) { |
436 | - if ($fileType === 'dir' && ! $this->isRoot($path)) { |
|
436 | + if ($fileType === 'dir' && !$this->isRoot($path)) { |
|
437 | 437 | $path .= '/'; |
438 | 438 | } |
439 | 439 | $this->getConnection()->copyObject([ |
440 | 440 | 'Bucket' => $this->bucket, |
441 | 441 | 'Key' => $this->cleanKey($path), |
442 | 442 | 'Metadata' => $metadata, |
443 | - 'CopySource' => $this->bucket . '/' . $path, |
|
443 | + 'CopySource' => $this->bucket.'/'.$path, |
|
444 | 444 | 'MetadataDirective' => 'REPLACE', |
445 | 445 | ]); |
446 | 446 | $this->testTimeout(); |
@@ -473,7 +473,7 @@ discard block |
||
473 | 473 | $this->getConnection()->copyObject(array( |
474 | 474 | 'Bucket' => $this->bucket, |
475 | 475 | 'Key' => $this->cleanKey($path2), |
476 | - 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1) |
|
476 | + 'CopySource' => S3Client::encodeKey($this->bucket.'/'.$path1) |
|
477 | 477 | )); |
478 | 478 | $this->testTimeout(); |
479 | 479 | } catch (S3Exception $e) { |
@@ -486,8 +486,8 @@ discard block |
||
486 | 486 | try { |
487 | 487 | $this->getConnection()->copyObject(array( |
488 | 488 | 'Bucket' => $this->bucket, |
489 | - 'Key' => $path2 . '/', |
|
490 | - 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1 . '/') |
|
489 | + 'Key' => $path2.'/', |
|
490 | + 'CopySource' => S3Client::encodeKey($this->bucket.'/'.$path1.'/') |
|
491 | 491 | )); |
492 | 492 | $this->testTimeout(); |
493 | 493 | } catch (S3Exception $e) { |
@@ -502,8 +502,8 @@ discard block |
||
502 | 502 | continue; |
503 | 503 | } |
504 | 504 | |
505 | - $source = $path1 . '/' . $file; |
|
506 | - $target = $path2 . '/' . $file; |
|
505 | + $source = $path1.'/'.$file; |
|
506 | + $target = $path2.'/'.$file; |
|
507 | 507 | $this->copy($source, $target); |
508 | 508 | } |
509 | 509 | } |
@@ -567,7 +567,7 @@ discard block |
||
567 | 567 | } |
568 | 568 | |
569 | 569 | $scheme = ($this->params['use_ssl'] === false) ? 'http' : 'https'; |
570 | - $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
|
570 | + $base_url = $scheme.'://'.$this->params['hostname'].':'.$this->params['port'].'/'; |
|
571 | 571 | |
572 | 572 | $this->connection = S3Client::factory(array( |
573 | 573 | 'key' => $this->params['key'], |
@@ -75,6 +75,9 @@ |
||
75 | 75 | return false; |
76 | 76 | } |
77 | 77 | |
78 | + /** |
|
79 | + * @param string $path |
|
80 | + */ |
|
78 | 81 | private function setMetaData($path, $metaData) { |
79 | 82 | $this->metaData[ltrim($path, '/')] = $metaData; |
80 | 83 | } |
@@ -38,320 +38,320 @@ |
||
38 | 38 | |
39 | 39 | class Dropbox extends \OC\Files\Storage\Common { |
40 | 40 | |
41 | - private $dropbox; |
|
42 | - private $root; |
|
43 | - private $id; |
|
44 | - private $metaData = array(); |
|
45 | - private $oauth; |
|
41 | + private $dropbox; |
|
42 | + private $root; |
|
43 | + private $id; |
|
44 | + private $metaData = array(); |
|
45 | + private $oauth; |
|
46 | 46 | |
47 | - private static $tempFiles = array(); |
|
47 | + private static $tempFiles = array(); |
|
48 | 48 | |
49 | - public function __construct($params) { |
|
50 | - if (isset($params['configured']) && $params['configured'] == 'true' |
|
51 | - && isset($params['app_key']) |
|
52 | - && isset($params['app_secret']) |
|
53 | - && isset($params['token']) |
|
54 | - && isset($params['token_secret']) |
|
55 | - ) { |
|
56 | - $this->root = isset($params['root']) ? $params['root'] : ''; |
|
57 | - $this->id = 'dropbox::'.$params['app_key'] . $params['token']. '/' . $this->root; |
|
58 | - $this->oauth = new \Dropbox_OAuth_Curl($params['app_key'], $params['app_secret']); |
|
59 | - $this->oauth->setToken($params['token'], $params['token_secret']); |
|
60 | - // note: Dropbox_API connection is lazy |
|
61 | - $this->dropbox = new \Dropbox_API($this->oauth, 'auto'); |
|
62 | - } else { |
|
63 | - throw new \Exception('Creating \OC\Files\Storage\Dropbox storage failed'); |
|
64 | - } |
|
65 | - } |
|
49 | + public function __construct($params) { |
|
50 | + if (isset($params['configured']) && $params['configured'] == 'true' |
|
51 | + && isset($params['app_key']) |
|
52 | + && isset($params['app_secret']) |
|
53 | + && isset($params['token']) |
|
54 | + && isset($params['token_secret']) |
|
55 | + ) { |
|
56 | + $this->root = isset($params['root']) ? $params['root'] : ''; |
|
57 | + $this->id = 'dropbox::'.$params['app_key'] . $params['token']. '/' . $this->root; |
|
58 | + $this->oauth = new \Dropbox_OAuth_Curl($params['app_key'], $params['app_secret']); |
|
59 | + $this->oauth->setToken($params['token'], $params['token_secret']); |
|
60 | + // note: Dropbox_API connection is lazy |
|
61 | + $this->dropbox = new \Dropbox_API($this->oauth, 'auto'); |
|
62 | + } else { |
|
63 | + throw new \Exception('Creating \OC\Files\Storage\Dropbox storage failed'); |
|
64 | + } |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @param string $path |
|
69 | - */ |
|
70 | - private function deleteMetaData($path) { |
|
71 | - $path = ltrim($this->root.$path, '/'); |
|
72 | - if (isset($this->metaData[$path])) { |
|
73 | - unset($this->metaData[$path]); |
|
74 | - return true; |
|
75 | - } |
|
76 | - return false; |
|
77 | - } |
|
67 | + /** |
|
68 | + * @param string $path |
|
69 | + */ |
|
70 | + private function deleteMetaData($path) { |
|
71 | + $path = ltrim($this->root.$path, '/'); |
|
72 | + if (isset($this->metaData[$path])) { |
|
73 | + unset($this->metaData[$path]); |
|
74 | + return true; |
|
75 | + } |
|
76 | + return false; |
|
77 | + } |
|
78 | 78 | |
79 | - private function setMetaData($path, $metaData) { |
|
80 | - $this->metaData[ltrim($path, '/')] = $metaData; |
|
81 | - } |
|
79 | + private function setMetaData($path, $metaData) { |
|
80 | + $this->metaData[ltrim($path, '/')] = $metaData; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Returns the path's metadata |
|
85 | - * @param string $path path for which to return the metadata |
|
86 | - * @param bool $list if true, also return the directory's contents |
|
87 | - * @return mixed directory contents if $list is true, file metadata if $list is |
|
88 | - * false, null if the file doesn't exist or "false" if the operation failed |
|
89 | - */ |
|
90 | - private function getDropBoxMetaData($path, $list = false) { |
|
91 | - $path = ltrim($this->root.$path, '/'); |
|
92 | - if ( ! $list && isset($this->metaData[$path])) { |
|
93 | - return $this->metaData[$path]; |
|
94 | - } else { |
|
95 | - if ($list) { |
|
96 | - try { |
|
97 | - $response = $this->dropbox->getMetaData($path); |
|
98 | - } catch (\Dropbox_Exception_Forbidden $e) { |
|
99 | - throw new StorageNotAvailableException('Dropbox API rate limit exceeded', StorageNotAvailableException::STATUS_ERROR, $e); |
|
100 | - } catch (\Exception $exception) { |
|
101 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
102 | - return false; |
|
103 | - } |
|
104 | - $contents = array(); |
|
105 | - if ($response && isset($response['contents'])) { |
|
106 | - // Cache folder's contents |
|
107 | - foreach ($response['contents'] as $file) { |
|
108 | - if (!isset($file['is_deleted']) || !$file['is_deleted']) { |
|
109 | - $this->setMetaData($path.'/'.basename($file['path']), $file); |
|
110 | - $contents[] = $file; |
|
111 | - } |
|
112 | - } |
|
113 | - unset($response['contents']); |
|
114 | - } |
|
115 | - if (!isset($response['is_deleted']) || !$response['is_deleted']) { |
|
116 | - $this->setMetaData($path, $response); |
|
117 | - } |
|
118 | - // Return contents of folder only |
|
119 | - return $contents; |
|
120 | - } else { |
|
121 | - try { |
|
122 | - $requestPath = $path; |
|
123 | - if ($path === '.') { |
|
124 | - $requestPath = ''; |
|
125 | - } |
|
83 | + /** |
|
84 | + * Returns the path's metadata |
|
85 | + * @param string $path path for which to return the metadata |
|
86 | + * @param bool $list if true, also return the directory's contents |
|
87 | + * @return mixed directory contents if $list is true, file metadata if $list is |
|
88 | + * false, null if the file doesn't exist or "false" if the operation failed |
|
89 | + */ |
|
90 | + private function getDropBoxMetaData($path, $list = false) { |
|
91 | + $path = ltrim($this->root.$path, '/'); |
|
92 | + if ( ! $list && isset($this->metaData[$path])) { |
|
93 | + return $this->metaData[$path]; |
|
94 | + } else { |
|
95 | + if ($list) { |
|
96 | + try { |
|
97 | + $response = $this->dropbox->getMetaData($path); |
|
98 | + } catch (\Dropbox_Exception_Forbidden $e) { |
|
99 | + throw new StorageNotAvailableException('Dropbox API rate limit exceeded', StorageNotAvailableException::STATUS_ERROR, $e); |
|
100 | + } catch (\Exception $exception) { |
|
101 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
102 | + return false; |
|
103 | + } |
|
104 | + $contents = array(); |
|
105 | + if ($response && isset($response['contents'])) { |
|
106 | + // Cache folder's contents |
|
107 | + foreach ($response['contents'] as $file) { |
|
108 | + if (!isset($file['is_deleted']) || !$file['is_deleted']) { |
|
109 | + $this->setMetaData($path.'/'.basename($file['path']), $file); |
|
110 | + $contents[] = $file; |
|
111 | + } |
|
112 | + } |
|
113 | + unset($response['contents']); |
|
114 | + } |
|
115 | + if (!isset($response['is_deleted']) || !$response['is_deleted']) { |
|
116 | + $this->setMetaData($path, $response); |
|
117 | + } |
|
118 | + // Return contents of folder only |
|
119 | + return $contents; |
|
120 | + } else { |
|
121 | + try { |
|
122 | + $requestPath = $path; |
|
123 | + if ($path === '.') { |
|
124 | + $requestPath = ''; |
|
125 | + } |
|
126 | 126 | |
127 | - $response = $this->dropbox->getMetaData($requestPath, 'false'); |
|
128 | - if (!isset($response['is_deleted']) || !$response['is_deleted']) { |
|
129 | - $this->setMetaData($path, $response); |
|
130 | - return $response; |
|
131 | - } |
|
132 | - return null; |
|
133 | - } catch (\Dropbox_Exception_Forbidden $e) { |
|
134 | - throw new StorageNotAvailableException('Dropbox API rate limit exceeded', StorageNotAvailableException::STATUS_ERROR, $e); |
|
135 | - } catch (\Exception $exception) { |
|
136 | - if ($exception instanceof \Dropbox_Exception_NotFound) { |
|
137 | - // don't log, might be a file_exist check |
|
138 | - return false; |
|
139 | - } |
|
140 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
141 | - return false; |
|
142 | - } |
|
143 | - } |
|
144 | - } |
|
145 | - } |
|
127 | + $response = $this->dropbox->getMetaData($requestPath, 'false'); |
|
128 | + if (!isset($response['is_deleted']) || !$response['is_deleted']) { |
|
129 | + $this->setMetaData($path, $response); |
|
130 | + return $response; |
|
131 | + } |
|
132 | + return null; |
|
133 | + } catch (\Dropbox_Exception_Forbidden $e) { |
|
134 | + throw new StorageNotAvailableException('Dropbox API rate limit exceeded', StorageNotAvailableException::STATUS_ERROR, $e); |
|
135 | + } catch (\Exception $exception) { |
|
136 | + if ($exception instanceof \Dropbox_Exception_NotFound) { |
|
137 | + // don't log, might be a file_exist check |
|
138 | + return false; |
|
139 | + } |
|
140 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
141 | + return false; |
|
142 | + } |
|
143 | + } |
|
144 | + } |
|
145 | + } |
|
146 | 146 | |
147 | - public function getId(){ |
|
148 | - return $this->id; |
|
149 | - } |
|
147 | + public function getId(){ |
|
148 | + return $this->id; |
|
149 | + } |
|
150 | 150 | |
151 | - public function mkdir($path) { |
|
152 | - $path = $this->root.$path; |
|
153 | - try { |
|
154 | - $this->dropbox->createFolder($path); |
|
155 | - return true; |
|
156 | - } catch (\Exception $exception) { |
|
157 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
158 | - return false; |
|
159 | - } |
|
160 | - } |
|
151 | + public function mkdir($path) { |
|
152 | + $path = $this->root.$path; |
|
153 | + try { |
|
154 | + $this->dropbox->createFolder($path); |
|
155 | + return true; |
|
156 | + } catch (\Exception $exception) { |
|
157 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
158 | + return false; |
|
159 | + } |
|
160 | + } |
|
161 | 161 | |
162 | - public function rmdir($path) { |
|
163 | - return $this->unlink($path); |
|
164 | - } |
|
162 | + public function rmdir($path) { |
|
163 | + return $this->unlink($path); |
|
164 | + } |
|
165 | 165 | |
166 | - public function opendir($path) { |
|
167 | - $contents = $this->getDropBoxMetaData($path, true); |
|
168 | - if ($contents !== false) { |
|
169 | - $files = array(); |
|
170 | - foreach ($contents as $file) { |
|
171 | - $files[] = basename($file['path']); |
|
172 | - } |
|
173 | - return IteratorDirectory::wrap($files); |
|
174 | - } |
|
175 | - return false; |
|
176 | - } |
|
166 | + public function opendir($path) { |
|
167 | + $contents = $this->getDropBoxMetaData($path, true); |
|
168 | + if ($contents !== false) { |
|
169 | + $files = array(); |
|
170 | + foreach ($contents as $file) { |
|
171 | + $files[] = basename($file['path']); |
|
172 | + } |
|
173 | + return IteratorDirectory::wrap($files); |
|
174 | + } |
|
175 | + return false; |
|
176 | + } |
|
177 | 177 | |
178 | - public function stat($path) { |
|
179 | - $metaData = $this->getDropBoxMetaData($path); |
|
180 | - if ($metaData) { |
|
181 | - $stat['size'] = $metaData['bytes']; |
|
182 | - $stat['atime'] = time(); |
|
183 | - $stat['mtime'] = (isset($metaData['modified'])) ? strtotime($metaData['modified']) : time(); |
|
184 | - return $stat; |
|
185 | - } |
|
186 | - return false; |
|
187 | - } |
|
178 | + public function stat($path) { |
|
179 | + $metaData = $this->getDropBoxMetaData($path); |
|
180 | + if ($metaData) { |
|
181 | + $stat['size'] = $metaData['bytes']; |
|
182 | + $stat['atime'] = time(); |
|
183 | + $stat['mtime'] = (isset($metaData['modified'])) ? strtotime($metaData['modified']) : time(); |
|
184 | + return $stat; |
|
185 | + } |
|
186 | + return false; |
|
187 | + } |
|
188 | 188 | |
189 | - public function filetype($path) { |
|
190 | - if ($path == '' || $path == '/') { |
|
191 | - return 'dir'; |
|
192 | - } else { |
|
193 | - $metaData = $this->getDropBoxMetaData($path); |
|
194 | - if ($metaData) { |
|
195 | - if ($metaData['is_dir'] == 'true') { |
|
196 | - return 'dir'; |
|
197 | - } else { |
|
198 | - return 'file'; |
|
199 | - } |
|
200 | - } |
|
201 | - } |
|
202 | - return false; |
|
203 | - } |
|
189 | + public function filetype($path) { |
|
190 | + if ($path == '' || $path == '/') { |
|
191 | + return 'dir'; |
|
192 | + } else { |
|
193 | + $metaData = $this->getDropBoxMetaData($path); |
|
194 | + if ($metaData) { |
|
195 | + if ($metaData['is_dir'] == 'true') { |
|
196 | + return 'dir'; |
|
197 | + } else { |
|
198 | + return 'file'; |
|
199 | + } |
|
200 | + } |
|
201 | + } |
|
202 | + return false; |
|
203 | + } |
|
204 | 204 | |
205 | - public function file_exists($path) { |
|
206 | - if ($path == '' || $path == '/') { |
|
207 | - return true; |
|
208 | - } |
|
209 | - if ($this->getDropBoxMetaData($path)) { |
|
210 | - return true; |
|
211 | - } |
|
212 | - return false; |
|
213 | - } |
|
205 | + public function file_exists($path) { |
|
206 | + if ($path == '' || $path == '/') { |
|
207 | + return true; |
|
208 | + } |
|
209 | + if ($this->getDropBoxMetaData($path)) { |
|
210 | + return true; |
|
211 | + } |
|
212 | + return false; |
|
213 | + } |
|
214 | 214 | |
215 | - public function unlink($path) { |
|
216 | - try { |
|
217 | - $this->dropbox->delete($this->root.$path); |
|
218 | - $this->deleteMetaData($path); |
|
219 | - return true; |
|
220 | - } catch (\Exception $exception) { |
|
221 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
222 | - return false; |
|
223 | - } |
|
224 | - } |
|
215 | + public function unlink($path) { |
|
216 | + try { |
|
217 | + $this->dropbox->delete($this->root.$path); |
|
218 | + $this->deleteMetaData($path); |
|
219 | + return true; |
|
220 | + } catch (\Exception $exception) { |
|
221 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
222 | + return false; |
|
223 | + } |
|
224 | + } |
|
225 | 225 | |
226 | - public function rename($path1, $path2) { |
|
227 | - try { |
|
228 | - // overwrite if target file exists and is not a directory |
|
229 | - $destMetaData = $this->getDropBoxMetaData($path2); |
|
230 | - if (isset($destMetaData) && $destMetaData !== false && !$destMetaData['is_dir']) { |
|
231 | - $this->unlink($path2); |
|
232 | - } |
|
233 | - $this->dropbox->move($this->root.$path1, $this->root.$path2); |
|
234 | - $this->deleteMetaData($path1); |
|
235 | - return true; |
|
236 | - } catch (\Exception $exception) { |
|
237 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
238 | - return false; |
|
239 | - } |
|
240 | - } |
|
226 | + public function rename($path1, $path2) { |
|
227 | + try { |
|
228 | + // overwrite if target file exists and is not a directory |
|
229 | + $destMetaData = $this->getDropBoxMetaData($path2); |
|
230 | + if (isset($destMetaData) && $destMetaData !== false && !$destMetaData['is_dir']) { |
|
231 | + $this->unlink($path2); |
|
232 | + } |
|
233 | + $this->dropbox->move($this->root.$path1, $this->root.$path2); |
|
234 | + $this->deleteMetaData($path1); |
|
235 | + return true; |
|
236 | + } catch (\Exception $exception) { |
|
237 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
238 | + return false; |
|
239 | + } |
|
240 | + } |
|
241 | 241 | |
242 | - public function copy($path1, $path2) { |
|
243 | - $path1 = $this->root.$path1; |
|
244 | - $path2 = $this->root.$path2; |
|
245 | - try { |
|
246 | - $this->dropbox->copy($path1, $path2); |
|
247 | - return true; |
|
248 | - } catch (\Exception $exception) { |
|
249 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
250 | - return false; |
|
251 | - } |
|
252 | - } |
|
242 | + public function copy($path1, $path2) { |
|
243 | + $path1 = $this->root.$path1; |
|
244 | + $path2 = $this->root.$path2; |
|
245 | + try { |
|
246 | + $this->dropbox->copy($path1, $path2); |
|
247 | + return true; |
|
248 | + } catch (\Exception $exception) { |
|
249 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
250 | + return false; |
|
251 | + } |
|
252 | + } |
|
253 | 253 | |
254 | - public function fopen($path, $mode) { |
|
255 | - $path = $this->root.$path; |
|
256 | - switch ($mode) { |
|
257 | - case 'r': |
|
258 | - case 'rb': |
|
259 | - try { |
|
260 | - // slashes need to stay |
|
261 | - $encodedPath = str_replace('%2F', '/', rawurlencode(trim($path, '/'))); |
|
262 | - $downloadUrl = 'https://api-content.dropbox.com/1/files/auto/' . $encodedPath; |
|
263 | - $headers = $this->oauth->getOAuthHeader($downloadUrl, [], 'GET'); |
|
254 | + public function fopen($path, $mode) { |
|
255 | + $path = $this->root.$path; |
|
256 | + switch ($mode) { |
|
257 | + case 'r': |
|
258 | + case 'rb': |
|
259 | + try { |
|
260 | + // slashes need to stay |
|
261 | + $encodedPath = str_replace('%2F', '/', rawurlencode(trim($path, '/'))); |
|
262 | + $downloadUrl = 'https://api-content.dropbox.com/1/files/auto/' . $encodedPath; |
|
263 | + $headers = $this->oauth->getOAuthHeader($downloadUrl, [], 'GET'); |
|
264 | 264 | |
265 | - $client = \OC::$server->getHTTPClientService()->newClient(); |
|
266 | - try { |
|
267 | - $response = $client->get($downloadUrl, [ |
|
268 | - 'headers' => $headers, |
|
269 | - 'stream' => true, |
|
270 | - ]); |
|
271 | - } catch (RequestException $e) { |
|
272 | - if (!is_null($e->getResponse())) { |
|
273 | - if ($e->getResponse()->getStatusCode() === 404) { |
|
274 | - return false; |
|
275 | - } else { |
|
276 | - throw $e; |
|
277 | - } |
|
278 | - } else { |
|
279 | - throw $e; |
|
280 | - } |
|
281 | - } |
|
265 | + $client = \OC::$server->getHTTPClientService()->newClient(); |
|
266 | + try { |
|
267 | + $response = $client->get($downloadUrl, [ |
|
268 | + 'headers' => $headers, |
|
269 | + 'stream' => true, |
|
270 | + ]); |
|
271 | + } catch (RequestException $e) { |
|
272 | + if (!is_null($e->getResponse())) { |
|
273 | + if ($e->getResponse()->getStatusCode() === 404) { |
|
274 | + return false; |
|
275 | + } else { |
|
276 | + throw $e; |
|
277 | + } |
|
278 | + } else { |
|
279 | + throw $e; |
|
280 | + } |
|
281 | + } |
|
282 | 282 | |
283 | - $handle = $response->getBody(); |
|
284 | - return RetryWrapper::wrap($handle); |
|
285 | - } catch (\Exception $exception) { |
|
286 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
287 | - return false; |
|
288 | - } |
|
289 | - case 'w': |
|
290 | - case 'wb': |
|
291 | - case 'a': |
|
292 | - case 'ab': |
|
293 | - case 'r+': |
|
294 | - case 'w+': |
|
295 | - case 'wb+': |
|
296 | - case 'a+': |
|
297 | - case 'x': |
|
298 | - case 'x+': |
|
299 | - case 'c': |
|
300 | - case 'c+': |
|
301 | - if (strrpos($path, '.') !== false) { |
|
302 | - $ext = substr($path, strrpos($path, '.')); |
|
303 | - } else { |
|
304 | - $ext = ''; |
|
305 | - } |
|
306 | - $tmpFile = \OCP\Files::tmpFile($ext); |
|
307 | - \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack')); |
|
308 | - if ($this->file_exists($path)) { |
|
309 | - $source = $this->fopen($path, 'r'); |
|
310 | - file_put_contents($tmpFile, $source); |
|
311 | - } |
|
312 | - self::$tempFiles[$tmpFile] = $path; |
|
313 | - return fopen('close://'.$tmpFile, $mode); |
|
314 | - } |
|
315 | - return false; |
|
316 | - } |
|
283 | + $handle = $response->getBody(); |
|
284 | + return RetryWrapper::wrap($handle); |
|
285 | + } catch (\Exception $exception) { |
|
286 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
287 | + return false; |
|
288 | + } |
|
289 | + case 'w': |
|
290 | + case 'wb': |
|
291 | + case 'a': |
|
292 | + case 'ab': |
|
293 | + case 'r+': |
|
294 | + case 'w+': |
|
295 | + case 'wb+': |
|
296 | + case 'a+': |
|
297 | + case 'x': |
|
298 | + case 'x+': |
|
299 | + case 'c': |
|
300 | + case 'c+': |
|
301 | + if (strrpos($path, '.') !== false) { |
|
302 | + $ext = substr($path, strrpos($path, '.')); |
|
303 | + } else { |
|
304 | + $ext = ''; |
|
305 | + } |
|
306 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
307 | + \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack')); |
|
308 | + if ($this->file_exists($path)) { |
|
309 | + $source = $this->fopen($path, 'r'); |
|
310 | + file_put_contents($tmpFile, $source); |
|
311 | + } |
|
312 | + self::$tempFiles[$tmpFile] = $path; |
|
313 | + return fopen('close://'.$tmpFile, $mode); |
|
314 | + } |
|
315 | + return false; |
|
316 | + } |
|
317 | 317 | |
318 | - public function writeBack($tmpFile) { |
|
319 | - if (isset(self::$tempFiles[$tmpFile])) { |
|
320 | - $handle = fopen($tmpFile, 'r'); |
|
321 | - try { |
|
322 | - $this->dropbox->putFile(self::$tempFiles[$tmpFile], $handle); |
|
323 | - unlink($tmpFile); |
|
324 | - $this->deleteMetaData(self::$tempFiles[$tmpFile]); |
|
325 | - } catch (\Exception $exception) { |
|
326 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
327 | - } |
|
328 | - } |
|
329 | - } |
|
318 | + public function writeBack($tmpFile) { |
|
319 | + if (isset(self::$tempFiles[$tmpFile])) { |
|
320 | + $handle = fopen($tmpFile, 'r'); |
|
321 | + try { |
|
322 | + $this->dropbox->putFile(self::$tempFiles[$tmpFile], $handle); |
|
323 | + unlink($tmpFile); |
|
324 | + $this->deleteMetaData(self::$tempFiles[$tmpFile]); |
|
325 | + } catch (\Exception $exception) { |
|
326 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
327 | + } |
|
328 | + } |
|
329 | + } |
|
330 | 330 | |
331 | - public function free_space($path) { |
|
332 | - try { |
|
333 | - $info = $this->dropbox->getAccountInfo(); |
|
334 | - return $info['quota_info']['quota'] - $info['quota_info']['normal']; |
|
335 | - } catch (\Exception $exception) { |
|
336 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
337 | - return false; |
|
338 | - } |
|
339 | - } |
|
331 | + public function free_space($path) { |
|
332 | + try { |
|
333 | + $info = $this->dropbox->getAccountInfo(); |
|
334 | + return $info['quota_info']['quota'] - $info['quota_info']['normal']; |
|
335 | + } catch (\Exception $exception) { |
|
336 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
337 | + return false; |
|
338 | + } |
|
339 | + } |
|
340 | 340 | |
341 | - public function touch($path, $mtime = null) { |
|
342 | - if ($this->file_exists($path)) { |
|
343 | - return false; |
|
344 | - } else { |
|
345 | - $this->file_put_contents($path, ''); |
|
346 | - } |
|
347 | - return true; |
|
348 | - } |
|
341 | + public function touch($path, $mtime = null) { |
|
342 | + if ($this->file_exists($path)) { |
|
343 | + return false; |
|
344 | + } else { |
|
345 | + $this->file_put_contents($path, ''); |
|
346 | + } |
|
347 | + return true; |
|
348 | + } |
|
349 | 349 | |
350 | - /** |
|
351 | - * check if curl is installed |
|
352 | - */ |
|
353 | - public static function checkDependencies() { |
|
354 | - return true; |
|
355 | - } |
|
350 | + /** |
|
351 | + * check if curl is installed |
|
352 | + */ |
|
353 | + public static function checkDependencies() { |
|
354 | + return true; |
|
355 | + } |
|
356 | 356 | |
357 | 357 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | use Icewind\Streams\RetryWrapper; |
35 | 35 | use OCP\Files\StorageNotAvailableException; |
36 | 36 | |
37 | -require_once __DIR__ . '/../3rdparty/Dropbox/autoload.php'; |
|
37 | +require_once __DIR__.'/../3rdparty/Dropbox/autoload.php'; |
|
38 | 38 | |
39 | 39 | class Dropbox extends \OC\Files\Storage\Common { |
40 | 40 | |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | && isset($params['token_secret']) |
55 | 55 | ) { |
56 | 56 | $this->root = isset($params['root']) ? $params['root'] : ''; |
57 | - $this->id = 'dropbox::'.$params['app_key'] . $params['token']. '/' . $this->root; |
|
57 | + $this->id = 'dropbox::'.$params['app_key'].$params['token'].'/'.$this->root; |
|
58 | 58 | $this->oauth = new \Dropbox_OAuth_Curl($params['app_key'], $params['app_secret']); |
59 | 59 | $this->oauth->setToken($params['token'], $params['token_secret']); |
60 | 60 | // note: Dropbox_API connection is lazy |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | */ |
90 | 90 | private function getDropBoxMetaData($path, $list = false) { |
91 | 91 | $path = ltrim($this->root.$path, '/'); |
92 | - if ( ! $list && isset($this->metaData[$path])) { |
|
92 | + if (!$list && isset($this->metaData[$path])) { |
|
93 | 93 | return $this->metaData[$path]; |
94 | 94 | } else { |
95 | 95 | if ($list) { |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | } |
145 | 145 | } |
146 | 146 | |
147 | - public function getId(){ |
|
147 | + public function getId() { |
|
148 | 148 | return $this->id; |
149 | 149 | } |
150 | 150 | |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | try { |
260 | 260 | // slashes need to stay |
261 | 261 | $encodedPath = str_replace('%2F', '/', rawurlencode(trim($path, '/'))); |
262 | - $downloadUrl = 'https://api-content.dropbox.com/1/files/auto/' . $encodedPath; |
|
262 | + $downloadUrl = 'https://api-content.dropbox.com/1/files/auto/'.$encodedPath; |
|
263 | 263 | $headers = $this->oauth->getOAuthHeader($downloadUrl, [], 'GET'); |
264 | 264 | |
265 | 265 | $client = \OC::$server->getHTTPClientService()->newClient(); |
@@ -32,7 +32,6 @@ |
||
32 | 32 | */ |
33 | 33 | namespace OC\Files\Storage; |
34 | 34 | use Icewind\Streams\IteratorDirectory; |
35 | - |
|
36 | 35 | use Icewind\Streams\RetryWrapper; |
37 | 36 | use phpseclib\Net\SFTP\Stream; |
38 | 37 |
@@ -41,428 +41,428 @@ |
||
41 | 41 | * provide access to SFTP servers. |
42 | 42 | */ |
43 | 43 | class SFTP extends \OC\Files\Storage\Common { |
44 | - private $host; |
|
45 | - private $user; |
|
46 | - private $root; |
|
47 | - private $port = 22; |
|
48 | - |
|
49 | - private $auth; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var SFTP |
|
53 | - */ |
|
54 | - protected $client; |
|
55 | - |
|
56 | - /** |
|
57 | - * @param string $host protocol://server:port |
|
58 | - * @return array [$server, $port] |
|
59 | - */ |
|
60 | - private function splitHost($host) { |
|
61 | - $input = $host; |
|
62 | - if (strpos($host, '://') === false) { |
|
63 | - // add a protocol to fix parse_url behavior with ipv6 |
|
64 | - $host = 'http://' . $host; |
|
65 | - } |
|
66 | - |
|
67 | - $parsed = parse_url($host); |
|
68 | - if(is_array($parsed) && isset($parsed['port'])) { |
|
69 | - return [$parsed['host'], $parsed['port']]; |
|
70 | - } else if (is_array($parsed)) { |
|
71 | - return [$parsed['host'], 22]; |
|
72 | - } else { |
|
73 | - return [$input, 22]; |
|
74 | - } |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * {@inheritdoc} |
|
79 | - */ |
|
80 | - public function __construct($params) { |
|
81 | - // Register sftp:// |
|
82 | - Stream::register(); |
|
83 | - |
|
84 | - $parsedHost = $this->splitHost($params['host']); |
|
85 | - |
|
86 | - $this->host = $parsedHost[0]; |
|
87 | - $this->port = $parsedHost[1]; |
|
88 | - |
|
89 | - if (!isset($params['user'])) { |
|
90 | - throw new \UnexpectedValueException('no authentication parameters specified'); |
|
91 | - } |
|
92 | - $this->user = $params['user']; |
|
93 | - |
|
94 | - if (isset($params['public_key_auth'])) { |
|
95 | - $this->auth = $params['public_key_auth']; |
|
96 | - } elseif (isset($params['password'])) { |
|
97 | - $this->auth = $params['password']; |
|
98 | - } else { |
|
99 | - throw new \UnexpectedValueException('no authentication parameters specified'); |
|
100 | - } |
|
101 | - |
|
102 | - $this->root |
|
103 | - = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; |
|
104 | - |
|
105 | - if ($this->root[0] != '/') { |
|
106 | - $this->root = '/' . $this->root; |
|
107 | - } |
|
108 | - |
|
109 | - if (substr($this->root, -1, 1) != '/') { |
|
110 | - $this->root .= '/'; |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Returns the connection. |
|
116 | - * |
|
117 | - * @return \phpseclib\Net\SFTP connected client instance |
|
118 | - * @throws \Exception when the connection failed |
|
119 | - */ |
|
120 | - public function getConnection() { |
|
121 | - if (!is_null($this->client)) { |
|
122 | - return $this->client; |
|
123 | - } |
|
124 | - |
|
125 | - $hostKeys = $this->readHostKeys(); |
|
126 | - $this->client = new \phpseclib\Net\SFTP($this->host, $this->port); |
|
127 | - |
|
128 | - // The SSH Host Key MUST be verified before login(). |
|
129 | - $currentHostKey = $this->client->getServerPublicHostKey(); |
|
130 | - if (array_key_exists($this->host, $hostKeys)) { |
|
131 | - if ($hostKeys[$this->host] != $currentHostKey) { |
|
132 | - throw new \Exception('Host public key does not match known key'); |
|
133 | - } |
|
134 | - } else { |
|
135 | - $hostKeys[$this->host] = $currentHostKey; |
|
136 | - $this->writeHostKeys($hostKeys); |
|
137 | - } |
|
138 | - |
|
139 | - if (!$this->client->login($this->user, $this->auth)) { |
|
140 | - throw new \Exception('Login failed'); |
|
141 | - } |
|
142 | - return $this->client; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * {@inheritdoc} |
|
147 | - */ |
|
148 | - public function test() { |
|
149 | - if ( |
|
150 | - !isset($this->host) |
|
151 | - || !isset($this->user) |
|
152 | - ) { |
|
153 | - return false; |
|
154 | - } |
|
155 | - return $this->getConnection()->nlist() !== false; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * {@inheritdoc} |
|
160 | - */ |
|
161 | - public function getId(){ |
|
162 | - $id = 'sftp::' . $this->user . '@' . $this->host; |
|
163 | - if ($this->port !== 22) { |
|
164 | - $id .= ':' . $this->port; |
|
165 | - } |
|
166 | - // note: this will double the root slash, |
|
167 | - // we should not change it to keep compatible with |
|
168 | - // old storage ids |
|
169 | - $id .= '/' . $this->root; |
|
170 | - return $id; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @return string |
|
175 | - */ |
|
176 | - public function getHost() { |
|
177 | - return $this->host; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @return string |
|
182 | - */ |
|
183 | - public function getRoot() { |
|
184 | - return $this->root; |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * @return mixed |
|
189 | - */ |
|
190 | - public function getUser() { |
|
191 | - return $this->user; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * @param string $path |
|
196 | - * @return string |
|
197 | - */ |
|
198 | - private function absPath($path) { |
|
199 | - return $this->root . $this->cleanPath($path); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * @return string|false |
|
204 | - */ |
|
205 | - private function hostKeysPath() { |
|
206 | - try { |
|
207 | - $storage_view = \OCP\Files::getStorage('files_external'); |
|
208 | - if ($storage_view) { |
|
209 | - return \OC::$server->getConfig()->getSystemValue('datadirectory') . |
|
210 | - $storage_view->getAbsolutePath('') . |
|
211 | - 'ssh_hostKeys'; |
|
212 | - } |
|
213 | - } catch (\Exception $e) { |
|
214 | - } |
|
215 | - return false; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * @param $keys |
|
220 | - * @return bool |
|
221 | - */ |
|
222 | - protected function writeHostKeys($keys) { |
|
223 | - try { |
|
224 | - $keyPath = $this->hostKeysPath(); |
|
225 | - if ($keyPath && file_exists($keyPath)) { |
|
226 | - $fp = fopen($keyPath, 'w'); |
|
227 | - foreach ($keys as $host => $key) { |
|
228 | - fwrite($fp, $host . '::' . $key . "\n"); |
|
229 | - } |
|
230 | - fclose($fp); |
|
231 | - return true; |
|
232 | - } |
|
233 | - } catch (\Exception $e) { |
|
234 | - } |
|
235 | - return false; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * @return array |
|
240 | - */ |
|
241 | - protected function readHostKeys() { |
|
242 | - try { |
|
243 | - $keyPath = $this->hostKeysPath(); |
|
244 | - if (file_exists($keyPath)) { |
|
245 | - $hosts = array(); |
|
246 | - $keys = array(); |
|
247 | - $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
248 | - if ($lines) { |
|
249 | - foreach ($lines as $line) { |
|
250 | - $hostKeyArray = explode("::", $line, 2); |
|
251 | - if (count($hostKeyArray) == 2) { |
|
252 | - $hosts[] = $hostKeyArray[0]; |
|
253 | - $keys[] = $hostKeyArray[1]; |
|
254 | - } |
|
255 | - } |
|
256 | - return array_combine($hosts, $keys); |
|
257 | - } |
|
258 | - } |
|
259 | - } catch (\Exception $e) { |
|
260 | - } |
|
261 | - return array(); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * {@inheritdoc} |
|
266 | - */ |
|
267 | - public function mkdir($path) { |
|
268 | - try { |
|
269 | - return $this->getConnection()->mkdir($this->absPath($path)); |
|
270 | - } catch (\Exception $e) { |
|
271 | - return false; |
|
272 | - } |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * {@inheritdoc} |
|
277 | - */ |
|
278 | - public function rmdir($path) { |
|
279 | - try { |
|
280 | - $result = $this->getConnection()->delete($this->absPath($path), true); |
|
281 | - // workaround: stray stat cache entry when deleting empty folders |
|
282 | - // see https://github.com/phpseclib/phpseclib/issues/706 |
|
283 | - $this->getConnection()->clearStatCache(); |
|
284 | - return $result; |
|
285 | - } catch (\Exception $e) { |
|
286 | - return false; |
|
287 | - } |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * {@inheritdoc} |
|
292 | - */ |
|
293 | - public function opendir($path) { |
|
294 | - try { |
|
295 | - $list = $this->getConnection()->nlist($this->absPath($path)); |
|
296 | - if ($list === false) { |
|
297 | - return false; |
|
298 | - } |
|
299 | - |
|
300 | - $id = md5('sftp:' . $path); |
|
301 | - $dirStream = array(); |
|
302 | - foreach($list as $file) { |
|
303 | - if ($file != '.' && $file != '..') { |
|
304 | - $dirStream[] = $file; |
|
305 | - } |
|
306 | - } |
|
307 | - return IteratorDirectory::wrap($dirStream); |
|
308 | - } catch(\Exception $e) { |
|
309 | - return false; |
|
310 | - } |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * {@inheritdoc} |
|
315 | - */ |
|
316 | - public function filetype($path) { |
|
317 | - try { |
|
318 | - $stat = $this->getConnection()->stat($this->absPath($path)); |
|
319 | - if ($stat['type'] == NET_SFTP_TYPE_REGULAR) { |
|
320 | - return 'file'; |
|
321 | - } |
|
322 | - |
|
323 | - if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) { |
|
324 | - return 'dir'; |
|
325 | - } |
|
326 | - } catch (\Exception $e) { |
|
327 | - |
|
328 | - } |
|
329 | - return false; |
|
330 | - } |
|
331 | - |
|
332 | - /** |
|
333 | - * {@inheritdoc} |
|
334 | - */ |
|
335 | - public function file_exists($path) { |
|
336 | - try { |
|
337 | - return $this->getConnection()->stat($this->absPath($path)) !== false; |
|
338 | - } catch (\Exception $e) { |
|
339 | - return false; |
|
340 | - } |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * {@inheritdoc} |
|
345 | - */ |
|
346 | - public function unlink($path) { |
|
347 | - try { |
|
348 | - return $this->getConnection()->delete($this->absPath($path), true); |
|
349 | - } catch (\Exception $e) { |
|
350 | - return false; |
|
351 | - } |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * {@inheritdoc} |
|
356 | - */ |
|
357 | - public function fopen($path, $mode) { |
|
358 | - try { |
|
359 | - $absPath = $this->absPath($path); |
|
360 | - switch($mode) { |
|
361 | - case 'r': |
|
362 | - case 'rb': |
|
363 | - if ( !$this->file_exists($path)) { |
|
364 | - return false; |
|
365 | - } |
|
366 | - case 'w': |
|
367 | - case 'wb': |
|
368 | - case 'a': |
|
369 | - case 'ab': |
|
370 | - case 'r+': |
|
371 | - case 'w+': |
|
372 | - case 'wb+': |
|
373 | - case 'a+': |
|
374 | - case 'x': |
|
375 | - case 'x+': |
|
376 | - case 'c': |
|
377 | - case 'c+': |
|
378 | - $context = stream_context_create(array('sftp' => array('session' => $this->getConnection()))); |
|
379 | - $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
380 | - return RetryWrapper::wrap($handle); |
|
381 | - } |
|
382 | - } catch (\Exception $e) { |
|
383 | - } |
|
384 | - return false; |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * {@inheritdoc} |
|
389 | - */ |
|
390 | - public function touch($path, $mtime=null) { |
|
391 | - try { |
|
392 | - if (!is_null($mtime)) { |
|
393 | - return false; |
|
394 | - } |
|
395 | - if (!$this->file_exists($path)) { |
|
396 | - $this->getConnection()->put($this->absPath($path), ''); |
|
397 | - } else { |
|
398 | - return false; |
|
399 | - } |
|
400 | - } catch (\Exception $e) { |
|
401 | - return false; |
|
402 | - } |
|
403 | - return true; |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * @param string $path |
|
408 | - * @param string $target |
|
409 | - * @throws \Exception |
|
410 | - */ |
|
411 | - public function getFile($path, $target) { |
|
412 | - $this->getConnection()->get($path, $target); |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * @param string $path |
|
417 | - * @param string $target |
|
418 | - * @throws \Exception |
|
419 | - */ |
|
420 | - public function uploadFile($path, $target) { |
|
421 | - $this->getConnection()->put($target, $path, NET_SFTP_LOCAL_FILE); |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * {@inheritdoc} |
|
426 | - */ |
|
427 | - public function rename($source, $target) { |
|
428 | - try { |
|
429 | - if (!$this->is_dir($target) && $this->file_exists($target)) { |
|
430 | - $this->unlink($target); |
|
431 | - } |
|
432 | - return $this->getConnection()->rename( |
|
433 | - $this->absPath($source), |
|
434 | - $this->absPath($target) |
|
435 | - ); |
|
436 | - } catch (\Exception $e) { |
|
437 | - return false; |
|
438 | - } |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * {@inheritdoc} |
|
443 | - */ |
|
444 | - public function stat($path) { |
|
445 | - try { |
|
446 | - $stat = $this->getConnection()->stat($this->absPath($path)); |
|
447 | - |
|
448 | - $mtime = $stat ? $stat['mtime'] : -1; |
|
449 | - $size = $stat ? $stat['size'] : 0; |
|
450 | - |
|
451 | - return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1); |
|
452 | - } catch (\Exception $e) { |
|
453 | - return false; |
|
454 | - } |
|
455 | - } |
|
456 | - |
|
457 | - /** |
|
458 | - * @param string $path |
|
459 | - * @return string |
|
460 | - */ |
|
461 | - public function constructUrl($path) { |
|
462 | - // Do not pass the password here. We want to use the Net_SFTP object |
|
463 | - // supplied via stream context or fail. We only supply username and |
|
464 | - // hostname because this might show up in logs (they are not used). |
|
465 | - $url = 'sftp://'.$this->user.'@'.$this->host.':'.$this->port.$this->root.$path; |
|
466 | - return $url; |
|
467 | - } |
|
44 | + private $host; |
|
45 | + private $user; |
|
46 | + private $root; |
|
47 | + private $port = 22; |
|
48 | + |
|
49 | + private $auth; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var SFTP |
|
53 | + */ |
|
54 | + protected $client; |
|
55 | + |
|
56 | + /** |
|
57 | + * @param string $host protocol://server:port |
|
58 | + * @return array [$server, $port] |
|
59 | + */ |
|
60 | + private function splitHost($host) { |
|
61 | + $input = $host; |
|
62 | + if (strpos($host, '://') === false) { |
|
63 | + // add a protocol to fix parse_url behavior with ipv6 |
|
64 | + $host = 'http://' . $host; |
|
65 | + } |
|
66 | + |
|
67 | + $parsed = parse_url($host); |
|
68 | + if(is_array($parsed) && isset($parsed['port'])) { |
|
69 | + return [$parsed['host'], $parsed['port']]; |
|
70 | + } else if (is_array($parsed)) { |
|
71 | + return [$parsed['host'], 22]; |
|
72 | + } else { |
|
73 | + return [$input, 22]; |
|
74 | + } |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * {@inheritdoc} |
|
79 | + */ |
|
80 | + public function __construct($params) { |
|
81 | + // Register sftp:// |
|
82 | + Stream::register(); |
|
83 | + |
|
84 | + $parsedHost = $this->splitHost($params['host']); |
|
85 | + |
|
86 | + $this->host = $parsedHost[0]; |
|
87 | + $this->port = $parsedHost[1]; |
|
88 | + |
|
89 | + if (!isset($params['user'])) { |
|
90 | + throw new \UnexpectedValueException('no authentication parameters specified'); |
|
91 | + } |
|
92 | + $this->user = $params['user']; |
|
93 | + |
|
94 | + if (isset($params['public_key_auth'])) { |
|
95 | + $this->auth = $params['public_key_auth']; |
|
96 | + } elseif (isset($params['password'])) { |
|
97 | + $this->auth = $params['password']; |
|
98 | + } else { |
|
99 | + throw new \UnexpectedValueException('no authentication parameters specified'); |
|
100 | + } |
|
101 | + |
|
102 | + $this->root |
|
103 | + = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; |
|
104 | + |
|
105 | + if ($this->root[0] != '/') { |
|
106 | + $this->root = '/' . $this->root; |
|
107 | + } |
|
108 | + |
|
109 | + if (substr($this->root, -1, 1) != '/') { |
|
110 | + $this->root .= '/'; |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Returns the connection. |
|
116 | + * |
|
117 | + * @return \phpseclib\Net\SFTP connected client instance |
|
118 | + * @throws \Exception when the connection failed |
|
119 | + */ |
|
120 | + public function getConnection() { |
|
121 | + if (!is_null($this->client)) { |
|
122 | + return $this->client; |
|
123 | + } |
|
124 | + |
|
125 | + $hostKeys = $this->readHostKeys(); |
|
126 | + $this->client = new \phpseclib\Net\SFTP($this->host, $this->port); |
|
127 | + |
|
128 | + // The SSH Host Key MUST be verified before login(). |
|
129 | + $currentHostKey = $this->client->getServerPublicHostKey(); |
|
130 | + if (array_key_exists($this->host, $hostKeys)) { |
|
131 | + if ($hostKeys[$this->host] != $currentHostKey) { |
|
132 | + throw new \Exception('Host public key does not match known key'); |
|
133 | + } |
|
134 | + } else { |
|
135 | + $hostKeys[$this->host] = $currentHostKey; |
|
136 | + $this->writeHostKeys($hostKeys); |
|
137 | + } |
|
138 | + |
|
139 | + if (!$this->client->login($this->user, $this->auth)) { |
|
140 | + throw new \Exception('Login failed'); |
|
141 | + } |
|
142 | + return $this->client; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * {@inheritdoc} |
|
147 | + */ |
|
148 | + public function test() { |
|
149 | + if ( |
|
150 | + !isset($this->host) |
|
151 | + || !isset($this->user) |
|
152 | + ) { |
|
153 | + return false; |
|
154 | + } |
|
155 | + return $this->getConnection()->nlist() !== false; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * {@inheritdoc} |
|
160 | + */ |
|
161 | + public function getId(){ |
|
162 | + $id = 'sftp::' . $this->user . '@' . $this->host; |
|
163 | + if ($this->port !== 22) { |
|
164 | + $id .= ':' . $this->port; |
|
165 | + } |
|
166 | + // note: this will double the root slash, |
|
167 | + // we should not change it to keep compatible with |
|
168 | + // old storage ids |
|
169 | + $id .= '/' . $this->root; |
|
170 | + return $id; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @return string |
|
175 | + */ |
|
176 | + public function getHost() { |
|
177 | + return $this->host; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @return string |
|
182 | + */ |
|
183 | + public function getRoot() { |
|
184 | + return $this->root; |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * @return mixed |
|
189 | + */ |
|
190 | + public function getUser() { |
|
191 | + return $this->user; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * @param string $path |
|
196 | + * @return string |
|
197 | + */ |
|
198 | + private function absPath($path) { |
|
199 | + return $this->root . $this->cleanPath($path); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * @return string|false |
|
204 | + */ |
|
205 | + private function hostKeysPath() { |
|
206 | + try { |
|
207 | + $storage_view = \OCP\Files::getStorage('files_external'); |
|
208 | + if ($storage_view) { |
|
209 | + return \OC::$server->getConfig()->getSystemValue('datadirectory') . |
|
210 | + $storage_view->getAbsolutePath('') . |
|
211 | + 'ssh_hostKeys'; |
|
212 | + } |
|
213 | + } catch (\Exception $e) { |
|
214 | + } |
|
215 | + return false; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * @param $keys |
|
220 | + * @return bool |
|
221 | + */ |
|
222 | + protected function writeHostKeys($keys) { |
|
223 | + try { |
|
224 | + $keyPath = $this->hostKeysPath(); |
|
225 | + if ($keyPath && file_exists($keyPath)) { |
|
226 | + $fp = fopen($keyPath, 'w'); |
|
227 | + foreach ($keys as $host => $key) { |
|
228 | + fwrite($fp, $host . '::' . $key . "\n"); |
|
229 | + } |
|
230 | + fclose($fp); |
|
231 | + return true; |
|
232 | + } |
|
233 | + } catch (\Exception $e) { |
|
234 | + } |
|
235 | + return false; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * @return array |
|
240 | + */ |
|
241 | + protected function readHostKeys() { |
|
242 | + try { |
|
243 | + $keyPath = $this->hostKeysPath(); |
|
244 | + if (file_exists($keyPath)) { |
|
245 | + $hosts = array(); |
|
246 | + $keys = array(); |
|
247 | + $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
248 | + if ($lines) { |
|
249 | + foreach ($lines as $line) { |
|
250 | + $hostKeyArray = explode("::", $line, 2); |
|
251 | + if (count($hostKeyArray) == 2) { |
|
252 | + $hosts[] = $hostKeyArray[0]; |
|
253 | + $keys[] = $hostKeyArray[1]; |
|
254 | + } |
|
255 | + } |
|
256 | + return array_combine($hosts, $keys); |
|
257 | + } |
|
258 | + } |
|
259 | + } catch (\Exception $e) { |
|
260 | + } |
|
261 | + return array(); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * {@inheritdoc} |
|
266 | + */ |
|
267 | + public function mkdir($path) { |
|
268 | + try { |
|
269 | + return $this->getConnection()->mkdir($this->absPath($path)); |
|
270 | + } catch (\Exception $e) { |
|
271 | + return false; |
|
272 | + } |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * {@inheritdoc} |
|
277 | + */ |
|
278 | + public function rmdir($path) { |
|
279 | + try { |
|
280 | + $result = $this->getConnection()->delete($this->absPath($path), true); |
|
281 | + // workaround: stray stat cache entry when deleting empty folders |
|
282 | + // see https://github.com/phpseclib/phpseclib/issues/706 |
|
283 | + $this->getConnection()->clearStatCache(); |
|
284 | + return $result; |
|
285 | + } catch (\Exception $e) { |
|
286 | + return false; |
|
287 | + } |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * {@inheritdoc} |
|
292 | + */ |
|
293 | + public function opendir($path) { |
|
294 | + try { |
|
295 | + $list = $this->getConnection()->nlist($this->absPath($path)); |
|
296 | + if ($list === false) { |
|
297 | + return false; |
|
298 | + } |
|
299 | + |
|
300 | + $id = md5('sftp:' . $path); |
|
301 | + $dirStream = array(); |
|
302 | + foreach($list as $file) { |
|
303 | + if ($file != '.' && $file != '..') { |
|
304 | + $dirStream[] = $file; |
|
305 | + } |
|
306 | + } |
|
307 | + return IteratorDirectory::wrap($dirStream); |
|
308 | + } catch(\Exception $e) { |
|
309 | + return false; |
|
310 | + } |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * {@inheritdoc} |
|
315 | + */ |
|
316 | + public function filetype($path) { |
|
317 | + try { |
|
318 | + $stat = $this->getConnection()->stat($this->absPath($path)); |
|
319 | + if ($stat['type'] == NET_SFTP_TYPE_REGULAR) { |
|
320 | + return 'file'; |
|
321 | + } |
|
322 | + |
|
323 | + if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) { |
|
324 | + return 'dir'; |
|
325 | + } |
|
326 | + } catch (\Exception $e) { |
|
327 | + |
|
328 | + } |
|
329 | + return false; |
|
330 | + } |
|
331 | + |
|
332 | + /** |
|
333 | + * {@inheritdoc} |
|
334 | + */ |
|
335 | + public function file_exists($path) { |
|
336 | + try { |
|
337 | + return $this->getConnection()->stat($this->absPath($path)) !== false; |
|
338 | + } catch (\Exception $e) { |
|
339 | + return false; |
|
340 | + } |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * {@inheritdoc} |
|
345 | + */ |
|
346 | + public function unlink($path) { |
|
347 | + try { |
|
348 | + return $this->getConnection()->delete($this->absPath($path), true); |
|
349 | + } catch (\Exception $e) { |
|
350 | + return false; |
|
351 | + } |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * {@inheritdoc} |
|
356 | + */ |
|
357 | + public function fopen($path, $mode) { |
|
358 | + try { |
|
359 | + $absPath = $this->absPath($path); |
|
360 | + switch($mode) { |
|
361 | + case 'r': |
|
362 | + case 'rb': |
|
363 | + if ( !$this->file_exists($path)) { |
|
364 | + return false; |
|
365 | + } |
|
366 | + case 'w': |
|
367 | + case 'wb': |
|
368 | + case 'a': |
|
369 | + case 'ab': |
|
370 | + case 'r+': |
|
371 | + case 'w+': |
|
372 | + case 'wb+': |
|
373 | + case 'a+': |
|
374 | + case 'x': |
|
375 | + case 'x+': |
|
376 | + case 'c': |
|
377 | + case 'c+': |
|
378 | + $context = stream_context_create(array('sftp' => array('session' => $this->getConnection()))); |
|
379 | + $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
380 | + return RetryWrapper::wrap($handle); |
|
381 | + } |
|
382 | + } catch (\Exception $e) { |
|
383 | + } |
|
384 | + return false; |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * {@inheritdoc} |
|
389 | + */ |
|
390 | + public function touch($path, $mtime=null) { |
|
391 | + try { |
|
392 | + if (!is_null($mtime)) { |
|
393 | + return false; |
|
394 | + } |
|
395 | + if (!$this->file_exists($path)) { |
|
396 | + $this->getConnection()->put($this->absPath($path), ''); |
|
397 | + } else { |
|
398 | + return false; |
|
399 | + } |
|
400 | + } catch (\Exception $e) { |
|
401 | + return false; |
|
402 | + } |
|
403 | + return true; |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * @param string $path |
|
408 | + * @param string $target |
|
409 | + * @throws \Exception |
|
410 | + */ |
|
411 | + public function getFile($path, $target) { |
|
412 | + $this->getConnection()->get($path, $target); |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * @param string $path |
|
417 | + * @param string $target |
|
418 | + * @throws \Exception |
|
419 | + */ |
|
420 | + public function uploadFile($path, $target) { |
|
421 | + $this->getConnection()->put($target, $path, NET_SFTP_LOCAL_FILE); |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * {@inheritdoc} |
|
426 | + */ |
|
427 | + public function rename($source, $target) { |
|
428 | + try { |
|
429 | + if (!$this->is_dir($target) && $this->file_exists($target)) { |
|
430 | + $this->unlink($target); |
|
431 | + } |
|
432 | + return $this->getConnection()->rename( |
|
433 | + $this->absPath($source), |
|
434 | + $this->absPath($target) |
|
435 | + ); |
|
436 | + } catch (\Exception $e) { |
|
437 | + return false; |
|
438 | + } |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * {@inheritdoc} |
|
443 | + */ |
|
444 | + public function stat($path) { |
|
445 | + try { |
|
446 | + $stat = $this->getConnection()->stat($this->absPath($path)); |
|
447 | + |
|
448 | + $mtime = $stat ? $stat['mtime'] : -1; |
|
449 | + $size = $stat ? $stat['size'] : 0; |
|
450 | + |
|
451 | + return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1); |
|
452 | + } catch (\Exception $e) { |
|
453 | + return false; |
|
454 | + } |
|
455 | + } |
|
456 | + |
|
457 | + /** |
|
458 | + * @param string $path |
|
459 | + * @return string |
|
460 | + */ |
|
461 | + public function constructUrl($path) { |
|
462 | + // Do not pass the password here. We want to use the Net_SFTP object |
|
463 | + // supplied via stream context or fail. We only supply username and |
|
464 | + // hostname because this might show up in logs (they are not used). |
|
465 | + $url = 'sftp://'.$this->user.'@'.$this->host.':'.$this->port.$this->root.$path; |
|
466 | + return $url; |
|
467 | + } |
|
468 | 468 | } |
@@ -61,11 +61,11 @@ discard block |
||
61 | 61 | $input = $host; |
62 | 62 | if (strpos($host, '://') === false) { |
63 | 63 | // add a protocol to fix parse_url behavior with ipv6 |
64 | - $host = 'http://' . $host; |
|
64 | + $host = 'http://'.$host; |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | $parsed = parse_url($host); |
68 | - if(is_array($parsed) && isset($parsed['port'])) { |
|
68 | + if (is_array($parsed) && isset($parsed['port'])) { |
|
69 | 69 | return [$parsed['host'], $parsed['port']]; |
70 | 70 | } else if (is_array($parsed)) { |
71 | 71 | return [$parsed['host'], 22]; |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | // Register sftp:// |
82 | 82 | Stream::register(); |
83 | 83 | |
84 | - $parsedHost = $this->splitHost($params['host']); |
|
84 | + $parsedHost = $this->splitHost($params['host']); |
|
85 | 85 | |
86 | 86 | $this->host = $parsedHost[0]; |
87 | 87 | $this->port = $parsedHost[1]; |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; |
104 | 104 | |
105 | 105 | if ($this->root[0] != '/') { |
106 | - $this->root = '/' . $this->root; |
|
106 | + $this->root = '/'.$this->root; |
|
107 | 107 | } |
108 | 108 | |
109 | 109 | if (substr($this->root, -1, 1) != '/') { |
@@ -158,15 +158,15 @@ discard block |
||
158 | 158 | /** |
159 | 159 | * {@inheritdoc} |
160 | 160 | */ |
161 | - public function getId(){ |
|
162 | - $id = 'sftp::' . $this->user . '@' . $this->host; |
|
161 | + public function getId() { |
|
162 | + $id = 'sftp::'.$this->user.'@'.$this->host; |
|
163 | 163 | if ($this->port !== 22) { |
164 | - $id .= ':' . $this->port; |
|
164 | + $id .= ':'.$this->port; |
|
165 | 165 | } |
166 | 166 | // note: this will double the root slash, |
167 | 167 | // we should not change it to keep compatible with |
168 | 168 | // old storage ids |
169 | - $id .= '/' . $this->root; |
|
169 | + $id .= '/'.$this->root; |
|
170 | 170 | return $id; |
171 | 171 | } |
172 | 172 | |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | * @return string |
197 | 197 | */ |
198 | 198 | private function absPath($path) { |
199 | - return $this->root . $this->cleanPath($path); |
|
199 | + return $this->root.$this->cleanPath($path); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | /** |
@@ -206,8 +206,8 @@ discard block |
||
206 | 206 | try { |
207 | 207 | $storage_view = \OCP\Files::getStorage('files_external'); |
208 | 208 | if ($storage_view) { |
209 | - return \OC::$server->getConfig()->getSystemValue('datadirectory') . |
|
210 | - $storage_view->getAbsolutePath('') . |
|
209 | + return \OC::$server->getConfig()->getSystemValue('datadirectory'). |
|
210 | + $storage_view->getAbsolutePath(''). |
|
211 | 211 | 'ssh_hostKeys'; |
212 | 212 | } |
213 | 213 | } catch (\Exception $e) { |
@@ -225,7 +225,7 @@ discard block |
||
225 | 225 | if ($keyPath && file_exists($keyPath)) { |
226 | 226 | $fp = fopen($keyPath, 'w'); |
227 | 227 | foreach ($keys as $host => $key) { |
228 | - fwrite($fp, $host . '::' . $key . "\n"); |
|
228 | + fwrite($fp, $host.'::'.$key."\n"); |
|
229 | 229 | } |
230 | 230 | fclose($fp); |
231 | 231 | return true; |
@@ -297,15 +297,15 @@ discard block |
||
297 | 297 | return false; |
298 | 298 | } |
299 | 299 | |
300 | - $id = md5('sftp:' . $path); |
|
300 | + $id = md5('sftp:'.$path); |
|
301 | 301 | $dirStream = array(); |
302 | - foreach($list as $file) { |
|
302 | + foreach ($list as $file) { |
|
303 | 303 | if ($file != '.' && $file != '..') { |
304 | 304 | $dirStream[] = $file; |
305 | 305 | } |
306 | 306 | } |
307 | 307 | return IteratorDirectory::wrap($dirStream); |
308 | - } catch(\Exception $e) { |
|
308 | + } catch (\Exception $e) { |
|
309 | 309 | return false; |
310 | 310 | } |
311 | 311 | } |
@@ -357,10 +357,10 @@ discard block |
||
357 | 357 | public function fopen($path, $mode) { |
358 | 358 | try { |
359 | 359 | $absPath = $this->absPath($path); |
360 | - switch($mode) { |
|
360 | + switch ($mode) { |
|
361 | 361 | case 'r': |
362 | 362 | case 'rb': |
363 | - if ( !$this->file_exists($path)) { |
|
363 | + if (!$this->file_exists($path)) { |
|
364 | 364 | return false; |
365 | 365 | } |
366 | 366 | case 'w': |
@@ -387,7 +387,7 @@ discard block |
||
387 | 387 | /** |
388 | 388 | * {@inheritdoc} |
389 | 389 | */ |
390 | - public function touch($path, $mtime=null) { |
|
390 | + public function touch($path, $mtime = null) { |
|
391 | 391 | try { |
392 | 392 | if (!is_null($mtime)) { |
393 | 393 | return false; |
@@ -259,7 +259,7 @@ |
||
259 | 259 | * |
260 | 260 | * @param int $id |
261 | 261 | * @param string $token |
262 | - * @return array |
|
262 | + * @return string |
|
263 | 263 | */ |
264 | 264 | private function getShare($id, $token) { |
265 | 265 | $query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ? AND `token` = ? AND `share_type` = ?'); |
@@ -33,281 +33,281 @@ |
||
33 | 33 | |
34 | 34 | class Server2Server { |
35 | 35 | |
36 | - /** |
|
37 | - * create a new share |
|
38 | - * |
|
39 | - * @param array $params |
|
40 | - * @return \OC_OCS_Result |
|
41 | - */ |
|
42 | - public function createShare($params) { |
|
43 | - |
|
44 | - if (!$this->isS2SEnabled(true)) { |
|
45 | - return new \OC_OCS_Result(null, 503, 'Server does not support federated cloud sharing'); |
|
46 | - } |
|
47 | - |
|
48 | - $remote = isset($_POST['remote']) ? $_POST['remote'] : null; |
|
49 | - $token = isset($_POST['token']) ? $_POST['token'] : null; |
|
50 | - $name = isset($_POST['name']) ? $_POST['name'] : null; |
|
51 | - $owner = isset($_POST['owner']) ? $_POST['owner'] : null; |
|
52 | - $shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null; |
|
53 | - $remoteId = isset($_POST['remoteId']) ? (int)$_POST['remoteId'] : null; |
|
54 | - |
|
55 | - if ($remote && $token && $name && $owner && $remoteId && $shareWith) { |
|
56 | - |
|
57 | - if(!\OCP\Util::isValidFileName($name)) { |
|
58 | - return new \OC_OCS_Result(null, 400, 'The mountpoint name contains invalid characters.'); |
|
59 | - } |
|
60 | - |
|
61 | - // FIXME this should be a method in the user management instead |
|
62 | - \OCP\Util::writeLog('files_sharing', 'shareWith before, ' . $shareWith, \OCP\Util::DEBUG); |
|
63 | - \OCP\Util::emitHook( |
|
64 | - '\OCA\Files_Sharing\API\Server2Server', |
|
65 | - 'preLoginNameUsedAsUserName', |
|
66 | - array('uid' => &$shareWith) |
|
67 | - ); |
|
68 | - \OCP\Util::writeLog('files_sharing', 'shareWith after, ' . $shareWith, \OCP\Util::DEBUG); |
|
69 | - |
|
70 | - if (!\OCP\User::userExists($shareWith)) { |
|
71 | - return new \OC_OCS_Result(null, 400, 'User does not exists'); |
|
72 | - } |
|
73 | - |
|
74 | - \OC_Util::setupFS($shareWith); |
|
75 | - |
|
76 | - $discoveryManager = new DiscoveryManager( |
|
77 | - \OC::$server->getMemCacheFactory(), |
|
78 | - \OC::$server->getHTTPClientService() |
|
79 | - ); |
|
80 | - $externalManager = new \OCA\Files_Sharing\External\Manager( |
|
81 | - \OC::$server->getDatabaseConnection(), |
|
82 | - \OC\Files\Filesystem::getMountManager(), |
|
83 | - \OC\Files\Filesystem::getLoader(), |
|
84 | - \OC::$server->getHTTPHelper(), |
|
85 | - \OC::$server->getNotificationManager(), |
|
86 | - $discoveryManager, |
|
87 | - $shareWith |
|
88 | - ); |
|
89 | - |
|
90 | - try { |
|
91 | - $externalManager->addShare($remote, $token, '', $name, $owner, false, $shareWith, $remoteId); |
|
92 | - $shareId = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share_external'); |
|
93 | - |
|
94 | - $user = $owner . '@' . $this->cleanupRemote($remote); |
|
95 | - |
|
96 | - \OC::$server->getActivityManager()->publishActivity( |
|
97 | - Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_RECEIVED, array($user, trim($name, '/')), '', array(), |
|
98 | - '', '', $shareWith, Activity::TYPE_REMOTE_SHARE, Activity::PRIORITY_LOW); |
|
99 | - |
|
100 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
101 | - |
|
102 | - $notificationManager = \OC::$server->getNotificationManager(); |
|
103 | - $notification = $notificationManager->createNotification(); |
|
104 | - $notification->setApp('files_sharing') |
|
105 | - ->setUser($shareWith) |
|
106 | - ->setDateTime(new \DateTime()) |
|
107 | - ->setObject('remote_share', $shareId) |
|
108 | - ->setSubject('remote_share', [$user, trim($name, '/')]); |
|
109 | - |
|
110 | - $declineAction = $notification->createAction(); |
|
111 | - $declineAction->setLabel('decline') |
|
112 | - ->setLink($urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'ocs/v1.php/apps/files_sharing/api/v1/remote_shares/pending/' . $shareId)), 'DELETE'); |
|
113 | - $notification->addAction($declineAction); |
|
114 | - |
|
115 | - $acceptAction = $notification->createAction(); |
|
116 | - $acceptAction->setLabel('accept') |
|
117 | - ->setLink($urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'ocs/v1.php/apps/files_sharing/api/v1/remote_shares/pending/' . $shareId)), 'POST'); |
|
118 | - $notification->addAction($acceptAction); |
|
119 | - |
|
120 | - $notificationManager->notify($notification); |
|
121 | - |
|
122 | - return new \OC_OCS_Result(); |
|
123 | - } catch (\Exception $e) { |
|
124 | - \OCP\Util::writeLog('files_sharing', 'server can not add remote share, ' . $e->getMessage(), \OCP\Util::ERROR); |
|
125 | - return new \OC_OCS_Result(null, 500, 'internal server error, was not able to add share from ' . $remote); |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - return new \OC_OCS_Result(null, 400, 'server can not add remote share, missing parameter'); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * accept server-to-server share |
|
134 | - * |
|
135 | - * @param array $params |
|
136 | - * @return \OC_OCS_Result |
|
137 | - */ |
|
138 | - public function acceptShare($params) { |
|
139 | - |
|
140 | - if (!$this->isS2SEnabled()) { |
|
141 | - return new \OC_OCS_Result(null, 503, 'Server does not support federated cloud sharing'); |
|
142 | - } |
|
143 | - |
|
144 | - $id = $params['id']; |
|
145 | - $token = isset($_POST['token']) ? $_POST['token'] : null; |
|
146 | - $share = self::getShare($id, $token); |
|
147 | - |
|
148 | - if ($share) { |
|
149 | - list($file, $link) = self::getFile($share['uid_owner'], $share['file_source']); |
|
150 | - |
|
151 | - $event = \OC::$server->getActivityManager()->generateEvent(); |
|
152 | - $event->setApp(Activity::FILES_SHARING_APP) |
|
153 | - ->setType(Activity::TYPE_REMOTE_SHARE) |
|
154 | - ->setAffectedUser($share['uid_owner']) |
|
155 | - ->setSubject(Activity::SUBJECT_REMOTE_SHARE_ACCEPTED, [$share['share_with'], basename($file)]) |
|
156 | - ->setObject('files', $share['file_source'], $file) |
|
157 | - ->setLink($link); |
|
158 | - \OC::$server->getActivityManager()->publish($event); |
|
159 | - } |
|
160 | - |
|
161 | - return new \OC_OCS_Result(); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * decline server-to-server share |
|
166 | - * |
|
167 | - * @param array $params |
|
168 | - * @return \OC_OCS_Result |
|
169 | - */ |
|
170 | - public function declineShare($params) { |
|
171 | - |
|
172 | - if (!$this->isS2SEnabled()) { |
|
173 | - return new \OC_OCS_Result(null, 503, 'Server does not support federated cloud sharing'); |
|
174 | - } |
|
175 | - |
|
176 | - $id = $params['id']; |
|
177 | - $token = isset($_POST['token']) ? $_POST['token'] : null; |
|
178 | - |
|
179 | - $share = $this->getShare($id, $token); |
|
180 | - |
|
181 | - if ($share) { |
|
182 | - // userId must be set to the user who unshares |
|
183 | - \OCP\Share::unshare($share['item_type'], $share['item_source'], $share['share_type'], $share['share_with'], $share['uid_owner']); |
|
184 | - |
|
185 | - list($file, $link) = $this->getFile($share['uid_owner'], $share['file_source']); |
|
186 | - |
|
187 | - $event = \OC::$server->getActivityManager()->generateEvent(); |
|
188 | - $event->setApp(Activity::FILES_SHARING_APP) |
|
189 | - ->setType(Activity::TYPE_REMOTE_SHARE) |
|
190 | - ->setAffectedUser($share['uid_owner']) |
|
191 | - ->setSubject(Activity::SUBJECT_REMOTE_SHARE_DECLINED, [$share['share_with'], basename($file)]) |
|
192 | - ->setObject('files', $share['file_source'], $file) |
|
193 | - ->setLink($link); |
|
194 | - \OC::$server->getActivityManager()->publish($event); |
|
195 | - } |
|
196 | - |
|
197 | - return new \OC_OCS_Result(); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * remove server-to-server share if it was unshared by the owner |
|
202 | - * |
|
203 | - * @param array $params |
|
204 | - * @return \OC_OCS_Result |
|
205 | - */ |
|
206 | - public function unshare($params) { |
|
207 | - |
|
208 | - if (!$this->isS2SEnabled()) { |
|
209 | - return new \OC_OCS_Result(null, 503, 'Server does not support federated cloud sharing'); |
|
210 | - } |
|
211 | - |
|
212 | - $id = $params['id']; |
|
213 | - $token = isset($_POST['token']) ? $_POST['token'] : null; |
|
214 | - |
|
215 | - $query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share_external` WHERE `remote_id` = ? AND `share_token` = ?'); |
|
216 | - $query->execute(array($id, $token)); |
|
217 | - $share = $query->fetchRow(); |
|
218 | - |
|
219 | - if ($token && $id && !empty($share)) { |
|
220 | - |
|
221 | - $remote = $this->cleanupRemote($share['remote']); |
|
222 | - |
|
223 | - $owner = $share['owner'] . '@' . $remote; |
|
224 | - $mountpoint = $share['mountpoint']; |
|
225 | - $user = $share['user']; |
|
226 | - |
|
227 | - $query = \OCP\DB::prepare('DELETE FROM `*PREFIX*share_external` WHERE `remote_id` = ? AND `share_token` = ?'); |
|
228 | - $query->execute(array($id, $token)); |
|
229 | - |
|
230 | - if ($share['accepted']) { |
|
231 | - $path = trim($mountpoint, '/'); |
|
232 | - } else { |
|
233 | - $path = trim($share['name'], '/'); |
|
234 | - } |
|
235 | - |
|
236 | - $notificationManager = \OC::$server->getNotificationManager(); |
|
237 | - $notification = $notificationManager->createNotification(); |
|
238 | - $notification->setApp('files_sharing') |
|
239 | - ->setUser($share['user']) |
|
240 | - ->setObject('remote_share', (int) $share['id']); |
|
241 | - $notificationManager->markProcessed($notification); |
|
242 | - |
|
243 | - \OC::$server->getActivityManager()->publishActivity( |
|
244 | - Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_UNSHARED, array($owner, $path), '', array(), |
|
245 | - '', '', $user, Activity::TYPE_REMOTE_SHARE, Activity::PRIORITY_MEDIUM); |
|
246 | - } |
|
247 | - |
|
248 | - return new \OC_OCS_Result(); |
|
249 | - } |
|
250 | - |
|
251 | - private function cleanupRemote($remote) { |
|
252 | - $remote = substr($remote, strpos($remote, '://') + 3); |
|
253 | - |
|
254 | - return rtrim($remote, '/'); |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * get share |
|
259 | - * |
|
260 | - * @param int $id |
|
261 | - * @param string $token |
|
262 | - * @return array |
|
263 | - */ |
|
264 | - private function getShare($id, $token) { |
|
265 | - $query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ? AND `token` = ? AND `share_type` = ?'); |
|
266 | - $query->execute(array($id, $token, \OCP\Share::SHARE_TYPE_REMOTE)); |
|
267 | - $share = $query->fetchRow(); |
|
268 | - |
|
269 | - return $share; |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * get file |
|
274 | - * |
|
275 | - * @param string $user |
|
276 | - * @param int $fileSource |
|
277 | - * @return array with internal path of the file and a absolute link to it |
|
278 | - */ |
|
279 | - private function getFile($user, $fileSource) { |
|
280 | - \OC_Util::setupFS($user); |
|
281 | - |
|
282 | - try { |
|
283 | - $file = \OC\Files\Filesystem::getPath($fileSource); |
|
284 | - } catch (NotFoundException $e) { |
|
285 | - $file = null; |
|
286 | - } |
|
287 | - $args = \OC\Files\Filesystem::is_dir($file) ? array('dir' => $file) : array('dir' => dirname($file), 'scrollto' => $file); |
|
288 | - $link = \OCP\Util::linkToAbsolute('files', 'index.php', $args); |
|
289 | - |
|
290 | - return array($file, $link); |
|
291 | - |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * check if server-to-server sharing is enabled |
|
296 | - * |
|
297 | - * @param bool $incoming |
|
298 | - * @return bool |
|
299 | - */ |
|
300 | - private function isS2SEnabled($incoming = false) { |
|
301 | - |
|
302 | - $result = \OCP\App::isEnabled('files_sharing'); |
|
303 | - |
|
304 | - if ($incoming) { |
|
305 | - $result = $result && \OCA\Files_Sharing\Helper::isIncomingServer2serverShareEnabled(); |
|
306 | - } else { |
|
307 | - $result = $result && \OCA\Files_Sharing\Helper::isOutgoingServer2serverShareEnabled(); |
|
308 | - } |
|
309 | - |
|
310 | - return $result; |
|
311 | - } |
|
36 | + /** |
|
37 | + * create a new share |
|
38 | + * |
|
39 | + * @param array $params |
|
40 | + * @return \OC_OCS_Result |
|
41 | + */ |
|
42 | + public function createShare($params) { |
|
43 | + |
|
44 | + if (!$this->isS2SEnabled(true)) { |
|
45 | + return new \OC_OCS_Result(null, 503, 'Server does not support federated cloud sharing'); |
|
46 | + } |
|
47 | + |
|
48 | + $remote = isset($_POST['remote']) ? $_POST['remote'] : null; |
|
49 | + $token = isset($_POST['token']) ? $_POST['token'] : null; |
|
50 | + $name = isset($_POST['name']) ? $_POST['name'] : null; |
|
51 | + $owner = isset($_POST['owner']) ? $_POST['owner'] : null; |
|
52 | + $shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null; |
|
53 | + $remoteId = isset($_POST['remoteId']) ? (int)$_POST['remoteId'] : null; |
|
54 | + |
|
55 | + if ($remote && $token && $name && $owner && $remoteId && $shareWith) { |
|
56 | + |
|
57 | + if(!\OCP\Util::isValidFileName($name)) { |
|
58 | + return new \OC_OCS_Result(null, 400, 'The mountpoint name contains invalid characters.'); |
|
59 | + } |
|
60 | + |
|
61 | + // FIXME this should be a method in the user management instead |
|
62 | + \OCP\Util::writeLog('files_sharing', 'shareWith before, ' . $shareWith, \OCP\Util::DEBUG); |
|
63 | + \OCP\Util::emitHook( |
|
64 | + '\OCA\Files_Sharing\API\Server2Server', |
|
65 | + 'preLoginNameUsedAsUserName', |
|
66 | + array('uid' => &$shareWith) |
|
67 | + ); |
|
68 | + \OCP\Util::writeLog('files_sharing', 'shareWith after, ' . $shareWith, \OCP\Util::DEBUG); |
|
69 | + |
|
70 | + if (!\OCP\User::userExists($shareWith)) { |
|
71 | + return new \OC_OCS_Result(null, 400, 'User does not exists'); |
|
72 | + } |
|
73 | + |
|
74 | + \OC_Util::setupFS($shareWith); |
|
75 | + |
|
76 | + $discoveryManager = new DiscoveryManager( |
|
77 | + \OC::$server->getMemCacheFactory(), |
|
78 | + \OC::$server->getHTTPClientService() |
|
79 | + ); |
|
80 | + $externalManager = new \OCA\Files_Sharing\External\Manager( |
|
81 | + \OC::$server->getDatabaseConnection(), |
|
82 | + \OC\Files\Filesystem::getMountManager(), |
|
83 | + \OC\Files\Filesystem::getLoader(), |
|
84 | + \OC::$server->getHTTPHelper(), |
|
85 | + \OC::$server->getNotificationManager(), |
|
86 | + $discoveryManager, |
|
87 | + $shareWith |
|
88 | + ); |
|
89 | + |
|
90 | + try { |
|
91 | + $externalManager->addShare($remote, $token, '', $name, $owner, false, $shareWith, $remoteId); |
|
92 | + $shareId = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share_external'); |
|
93 | + |
|
94 | + $user = $owner . '@' . $this->cleanupRemote($remote); |
|
95 | + |
|
96 | + \OC::$server->getActivityManager()->publishActivity( |
|
97 | + Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_RECEIVED, array($user, trim($name, '/')), '', array(), |
|
98 | + '', '', $shareWith, Activity::TYPE_REMOTE_SHARE, Activity::PRIORITY_LOW); |
|
99 | + |
|
100 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
101 | + |
|
102 | + $notificationManager = \OC::$server->getNotificationManager(); |
|
103 | + $notification = $notificationManager->createNotification(); |
|
104 | + $notification->setApp('files_sharing') |
|
105 | + ->setUser($shareWith) |
|
106 | + ->setDateTime(new \DateTime()) |
|
107 | + ->setObject('remote_share', $shareId) |
|
108 | + ->setSubject('remote_share', [$user, trim($name, '/')]); |
|
109 | + |
|
110 | + $declineAction = $notification->createAction(); |
|
111 | + $declineAction->setLabel('decline') |
|
112 | + ->setLink($urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'ocs/v1.php/apps/files_sharing/api/v1/remote_shares/pending/' . $shareId)), 'DELETE'); |
|
113 | + $notification->addAction($declineAction); |
|
114 | + |
|
115 | + $acceptAction = $notification->createAction(); |
|
116 | + $acceptAction->setLabel('accept') |
|
117 | + ->setLink($urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'ocs/v1.php/apps/files_sharing/api/v1/remote_shares/pending/' . $shareId)), 'POST'); |
|
118 | + $notification->addAction($acceptAction); |
|
119 | + |
|
120 | + $notificationManager->notify($notification); |
|
121 | + |
|
122 | + return new \OC_OCS_Result(); |
|
123 | + } catch (\Exception $e) { |
|
124 | + \OCP\Util::writeLog('files_sharing', 'server can not add remote share, ' . $e->getMessage(), \OCP\Util::ERROR); |
|
125 | + return new \OC_OCS_Result(null, 500, 'internal server error, was not able to add share from ' . $remote); |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + return new \OC_OCS_Result(null, 400, 'server can not add remote share, missing parameter'); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * accept server-to-server share |
|
134 | + * |
|
135 | + * @param array $params |
|
136 | + * @return \OC_OCS_Result |
|
137 | + */ |
|
138 | + public function acceptShare($params) { |
|
139 | + |
|
140 | + if (!$this->isS2SEnabled()) { |
|
141 | + return new \OC_OCS_Result(null, 503, 'Server does not support federated cloud sharing'); |
|
142 | + } |
|
143 | + |
|
144 | + $id = $params['id']; |
|
145 | + $token = isset($_POST['token']) ? $_POST['token'] : null; |
|
146 | + $share = self::getShare($id, $token); |
|
147 | + |
|
148 | + if ($share) { |
|
149 | + list($file, $link) = self::getFile($share['uid_owner'], $share['file_source']); |
|
150 | + |
|
151 | + $event = \OC::$server->getActivityManager()->generateEvent(); |
|
152 | + $event->setApp(Activity::FILES_SHARING_APP) |
|
153 | + ->setType(Activity::TYPE_REMOTE_SHARE) |
|
154 | + ->setAffectedUser($share['uid_owner']) |
|
155 | + ->setSubject(Activity::SUBJECT_REMOTE_SHARE_ACCEPTED, [$share['share_with'], basename($file)]) |
|
156 | + ->setObject('files', $share['file_source'], $file) |
|
157 | + ->setLink($link); |
|
158 | + \OC::$server->getActivityManager()->publish($event); |
|
159 | + } |
|
160 | + |
|
161 | + return new \OC_OCS_Result(); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * decline server-to-server share |
|
166 | + * |
|
167 | + * @param array $params |
|
168 | + * @return \OC_OCS_Result |
|
169 | + */ |
|
170 | + public function declineShare($params) { |
|
171 | + |
|
172 | + if (!$this->isS2SEnabled()) { |
|
173 | + return new \OC_OCS_Result(null, 503, 'Server does not support federated cloud sharing'); |
|
174 | + } |
|
175 | + |
|
176 | + $id = $params['id']; |
|
177 | + $token = isset($_POST['token']) ? $_POST['token'] : null; |
|
178 | + |
|
179 | + $share = $this->getShare($id, $token); |
|
180 | + |
|
181 | + if ($share) { |
|
182 | + // userId must be set to the user who unshares |
|
183 | + \OCP\Share::unshare($share['item_type'], $share['item_source'], $share['share_type'], $share['share_with'], $share['uid_owner']); |
|
184 | + |
|
185 | + list($file, $link) = $this->getFile($share['uid_owner'], $share['file_source']); |
|
186 | + |
|
187 | + $event = \OC::$server->getActivityManager()->generateEvent(); |
|
188 | + $event->setApp(Activity::FILES_SHARING_APP) |
|
189 | + ->setType(Activity::TYPE_REMOTE_SHARE) |
|
190 | + ->setAffectedUser($share['uid_owner']) |
|
191 | + ->setSubject(Activity::SUBJECT_REMOTE_SHARE_DECLINED, [$share['share_with'], basename($file)]) |
|
192 | + ->setObject('files', $share['file_source'], $file) |
|
193 | + ->setLink($link); |
|
194 | + \OC::$server->getActivityManager()->publish($event); |
|
195 | + } |
|
196 | + |
|
197 | + return new \OC_OCS_Result(); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * remove server-to-server share if it was unshared by the owner |
|
202 | + * |
|
203 | + * @param array $params |
|
204 | + * @return \OC_OCS_Result |
|
205 | + */ |
|
206 | + public function unshare($params) { |
|
207 | + |
|
208 | + if (!$this->isS2SEnabled()) { |
|
209 | + return new \OC_OCS_Result(null, 503, 'Server does not support federated cloud sharing'); |
|
210 | + } |
|
211 | + |
|
212 | + $id = $params['id']; |
|
213 | + $token = isset($_POST['token']) ? $_POST['token'] : null; |
|
214 | + |
|
215 | + $query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share_external` WHERE `remote_id` = ? AND `share_token` = ?'); |
|
216 | + $query->execute(array($id, $token)); |
|
217 | + $share = $query->fetchRow(); |
|
218 | + |
|
219 | + if ($token && $id && !empty($share)) { |
|
220 | + |
|
221 | + $remote = $this->cleanupRemote($share['remote']); |
|
222 | + |
|
223 | + $owner = $share['owner'] . '@' . $remote; |
|
224 | + $mountpoint = $share['mountpoint']; |
|
225 | + $user = $share['user']; |
|
226 | + |
|
227 | + $query = \OCP\DB::prepare('DELETE FROM `*PREFIX*share_external` WHERE `remote_id` = ? AND `share_token` = ?'); |
|
228 | + $query->execute(array($id, $token)); |
|
229 | + |
|
230 | + if ($share['accepted']) { |
|
231 | + $path = trim($mountpoint, '/'); |
|
232 | + } else { |
|
233 | + $path = trim($share['name'], '/'); |
|
234 | + } |
|
235 | + |
|
236 | + $notificationManager = \OC::$server->getNotificationManager(); |
|
237 | + $notification = $notificationManager->createNotification(); |
|
238 | + $notification->setApp('files_sharing') |
|
239 | + ->setUser($share['user']) |
|
240 | + ->setObject('remote_share', (int) $share['id']); |
|
241 | + $notificationManager->markProcessed($notification); |
|
242 | + |
|
243 | + \OC::$server->getActivityManager()->publishActivity( |
|
244 | + Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_UNSHARED, array($owner, $path), '', array(), |
|
245 | + '', '', $user, Activity::TYPE_REMOTE_SHARE, Activity::PRIORITY_MEDIUM); |
|
246 | + } |
|
247 | + |
|
248 | + return new \OC_OCS_Result(); |
|
249 | + } |
|
250 | + |
|
251 | + private function cleanupRemote($remote) { |
|
252 | + $remote = substr($remote, strpos($remote, '://') + 3); |
|
253 | + |
|
254 | + return rtrim($remote, '/'); |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * get share |
|
259 | + * |
|
260 | + * @param int $id |
|
261 | + * @param string $token |
|
262 | + * @return array |
|
263 | + */ |
|
264 | + private function getShare($id, $token) { |
|
265 | + $query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ? AND `token` = ? AND `share_type` = ?'); |
|
266 | + $query->execute(array($id, $token, \OCP\Share::SHARE_TYPE_REMOTE)); |
|
267 | + $share = $query->fetchRow(); |
|
268 | + |
|
269 | + return $share; |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * get file |
|
274 | + * |
|
275 | + * @param string $user |
|
276 | + * @param int $fileSource |
|
277 | + * @return array with internal path of the file and a absolute link to it |
|
278 | + */ |
|
279 | + private function getFile($user, $fileSource) { |
|
280 | + \OC_Util::setupFS($user); |
|
281 | + |
|
282 | + try { |
|
283 | + $file = \OC\Files\Filesystem::getPath($fileSource); |
|
284 | + } catch (NotFoundException $e) { |
|
285 | + $file = null; |
|
286 | + } |
|
287 | + $args = \OC\Files\Filesystem::is_dir($file) ? array('dir' => $file) : array('dir' => dirname($file), 'scrollto' => $file); |
|
288 | + $link = \OCP\Util::linkToAbsolute('files', 'index.php', $args); |
|
289 | + |
|
290 | + return array($file, $link); |
|
291 | + |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * check if server-to-server sharing is enabled |
|
296 | + * |
|
297 | + * @param bool $incoming |
|
298 | + * @return bool |
|
299 | + */ |
|
300 | + private function isS2SEnabled($incoming = false) { |
|
301 | + |
|
302 | + $result = \OCP\App::isEnabled('files_sharing'); |
|
303 | + |
|
304 | + if ($incoming) { |
|
305 | + $result = $result && \OCA\Files_Sharing\Helper::isIncomingServer2serverShareEnabled(); |
|
306 | + } else { |
|
307 | + $result = $result && \OCA\Files_Sharing\Helper::isOutgoingServer2serverShareEnabled(); |
|
308 | + } |
|
309 | + |
|
310 | + return $result; |
|
311 | + } |
|
312 | 312 | |
313 | 313 | } |
@@ -50,22 +50,22 @@ discard block |
||
50 | 50 | $name = isset($_POST['name']) ? $_POST['name'] : null; |
51 | 51 | $owner = isset($_POST['owner']) ? $_POST['owner'] : null; |
52 | 52 | $shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null; |
53 | - $remoteId = isset($_POST['remoteId']) ? (int)$_POST['remoteId'] : null; |
|
53 | + $remoteId = isset($_POST['remoteId']) ? (int) $_POST['remoteId'] : null; |
|
54 | 54 | |
55 | 55 | if ($remote && $token && $name && $owner && $remoteId && $shareWith) { |
56 | 56 | |
57 | - if(!\OCP\Util::isValidFileName($name)) { |
|
57 | + if (!\OCP\Util::isValidFileName($name)) { |
|
58 | 58 | return new \OC_OCS_Result(null, 400, 'The mountpoint name contains invalid characters.'); |
59 | 59 | } |
60 | 60 | |
61 | 61 | // FIXME this should be a method in the user management instead |
62 | - \OCP\Util::writeLog('files_sharing', 'shareWith before, ' . $shareWith, \OCP\Util::DEBUG); |
|
62 | + \OCP\Util::writeLog('files_sharing', 'shareWith before, '.$shareWith, \OCP\Util::DEBUG); |
|
63 | 63 | \OCP\Util::emitHook( |
64 | 64 | '\OCA\Files_Sharing\API\Server2Server', |
65 | 65 | 'preLoginNameUsedAsUserName', |
66 | 66 | array('uid' => &$shareWith) |
67 | 67 | ); |
68 | - \OCP\Util::writeLog('files_sharing', 'shareWith after, ' . $shareWith, \OCP\Util::DEBUG); |
|
68 | + \OCP\Util::writeLog('files_sharing', 'shareWith after, '.$shareWith, \OCP\Util::DEBUG); |
|
69 | 69 | |
70 | 70 | if (!\OCP\User::userExists($shareWith)) { |
71 | 71 | return new \OC_OCS_Result(null, 400, 'User does not exists'); |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | $externalManager->addShare($remote, $token, '', $name, $owner, false, $shareWith, $remoteId); |
92 | 92 | $shareId = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share_external'); |
93 | 93 | |
94 | - $user = $owner . '@' . $this->cleanupRemote($remote); |
|
94 | + $user = $owner.'@'.$this->cleanupRemote($remote); |
|
95 | 95 | |
96 | 96 | \OC::$server->getActivityManager()->publishActivity( |
97 | 97 | Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_RECEIVED, array($user, trim($name, '/')), '', array(), |
@@ -109,20 +109,20 @@ discard block |
||
109 | 109 | |
110 | 110 | $declineAction = $notification->createAction(); |
111 | 111 | $declineAction->setLabel('decline') |
112 | - ->setLink($urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'ocs/v1.php/apps/files_sharing/api/v1/remote_shares/pending/' . $shareId)), 'DELETE'); |
|
112 | + ->setLink($urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'ocs/v1.php/apps/files_sharing/api/v1/remote_shares/pending/'.$shareId)), 'DELETE'); |
|
113 | 113 | $notification->addAction($declineAction); |
114 | 114 | |
115 | 115 | $acceptAction = $notification->createAction(); |
116 | 116 | $acceptAction->setLabel('accept') |
117 | - ->setLink($urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'ocs/v1.php/apps/files_sharing/api/v1/remote_shares/pending/' . $shareId)), 'POST'); |
|
117 | + ->setLink($urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'ocs/v1.php/apps/files_sharing/api/v1/remote_shares/pending/'.$shareId)), 'POST'); |
|
118 | 118 | $notification->addAction($acceptAction); |
119 | 119 | |
120 | 120 | $notificationManager->notify($notification); |
121 | 121 | |
122 | 122 | return new \OC_OCS_Result(); |
123 | 123 | } catch (\Exception $e) { |
124 | - \OCP\Util::writeLog('files_sharing', 'server can not add remote share, ' . $e->getMessage(), \OCP\Util::ERROR); |
|
125 | - return new \OC_OCS_Result(null, 500, 'internal server error, was not able to add share from ' . $remote); |
|
124 | + \OCP\Util::writeLog('files_sharing', 'server can not add remote share, '.$e->getMessage(), \OCP\Util::ERROR); |
|
125 | + return new \OC_OCS_Result(null, 500, 'internal server error, was not able to add share from '.$remote); |
|
126 | 126 | } |
127 | 127 | } |
128 | 128 | |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | |
221 | 221 | $remote = $this->cleanupRemote($share['remote']); |
222 | 222 | |
223 | - $owner = $share['owner'] . '@' . $remote; |
|
223 | + $owner = $share['owner'].'@'.$remote; |
|
224 | 224 | $mountpoint = $share['mountpoint']; |
225 | 225 | $user = $share['user']; |
226 | 226 |
@@ -31,7 +31,6 @@ |
||
31 | 31 | use OCP\Files\IRootFolder; |
32 | 32 | use OCP\Share; |
33 | 33 | use OCP\Share\IManager; |
34 | - |
|
35 | 34 | use OCP\Share\Exceptions\ShareNotFound; |
36 | 35 | use OCP\Share\Exceptions\GenericShareException; |
37 | 36 |
@@ -37,694 +37,694 @@ |
||
37 | 37 | |
38 | 38 | class Share20OCS { |
39 | 39 | |
40 | - /** @var IManager */ |
|
41 | - private $shareManager; |
|
42 | - /** @var IGroupManager */ |
|
43 | - private $groupManager; |
|
44 | - /** @var IUserManager */ |
|
45 | - private $userManager; |
|
46 | - /** @var IRequest */ |
|
47 | - private $request; |
|
48 | - /** @var IRootFolder */ |
|
49 | - private $rootFolder; |
|
50 | - /** @var IUrlGenerator */ |
|
51 | - private $urlGenerator; |
|
52 | - /** @var IUser */ |
|
53 | - private $currentUser; |
|
54 | - |
|
55 | - /** |
|
56 | - * Share20OCS constructor. |
|
57 | - * |
|
58 | - * @param IManager $shareManager |
|
59 | - * @param IGroupManager $groupManager |
|
60 | - * @param IUserManager $userManager |
|
61 | - * @param IRequest $request |
|
62 | - * @param IRootFolder $rootFolder |
|
63 | - * @param IURLGenerator $urlGenerator |
|
64 | - * @param IUser $currentUser |
|
65 | - */ |
|
66 | - public function __construct( |
|
67 | - IManager $shareManager, |
|
68 | - IGroupManager $groupManager, |
|
69 | - IUserManager $userManager, |
|
70 | - IRequest $request, |
|
71 | - IRootFolder $rootFolder, |
|
72 | - IURLGenerator $urlGenerator, |
|
73 | - IUser $currentUser |
|
74 | - ) { |
|
75 | - $this->shareManager = $shareManager; |
|
76 | - $this->userManager = $userManager; |
|
77 | - $this->groupManager = $groupManager; |
|
78 | - $this->request = $request; |
|
79 | - $this->rootFolder = $rootFolder; |
|
80 | - $this->urlGenerator = $urlGenerator; |
|
81 | - $this->currentUser = $currentUser; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Convert an IShare to an array for OCS output |
|
86 | - * |
|
87 | - * @param \OCP\Share\IShare $share |
|
88 | - * @return array |
|
89 | - * @throws NotFoundException In case the node can't be resolved. |
|
90 | - */ |
|
91 | - protected function formatShare(\OCP\Share\IShare $share) { |
|
92 | - $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
93 | - $shareOwner = $this->userManager->get($share->getShareOwner()); |
|
94 | - $result = [ |
|
95 | - 'id' => $share->getId(), |
|
96 | - 'share_type' => $share->getShareType(), |
|
97 | - 'uid_owner' => $share->getSharedBy(), |
|
98 | - 'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(), |
|
99 | - 'permissions' => $share->getPermissions(), |
|
100 | - 'stime' => $share->getShareTime()->getTimestamp(), |
|
101 | - 'parent' => null, |
|
102 | - 'expiration' => null, |
|
103 | - 'token' => null, |
|
104 | - 'uid_file_owner' => $share->getShareOwner(), |
|
105 | - 'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(), |
|
106 | - ]; |
|
107 | - |
|
108 | - $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); |
|
109 | - $nodes = $userFolder->getById($share->getNodeId()); |
|
110 | - |
|
111 | - if (empty($nodes)) { |
|
112 | - throw new NotFoundException(); |
|
113 | - } |
|
114 | - |
|
115 | - $node = $nodes[0]; |
|
116 | - $result['path'] = $userFolder->getRelativePath($node->getPath()); |
|
117 | - |
|
118 | - if ($node instanceOf \OCP\Files\Folder) { |
|
119 | - $result['item_type'] = 'folder'; |
|
120 | - } else { |
|
121 | - $result['item_type'] = 'file'; |
|
122 | - } |
|
123 | - $result['mimetype'] = $node->getMimeType(); |
|
124 | - $result['storage_id'] = $node->getStorage()->getId(); |
|
125 | - $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); |
|
126 | - $result['item_source'] = $node->getId(); |
|
127 | - $result['file_source'] = $node->getId(); |
|
128 | - $result['file_parent'] = $node->getParent()->getId(); |
|
129 | - $result['file_target'] = $share->getTarget(); |
|
130 | - |
|
131 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
132 | - $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
133 | - $result['share_with'] = $share->getSharedWith(); |
|
134 | - $result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith(); |
|
135 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
136 | - $result['share_with'] = $share->getSharedWith(); |
|
137 | - $result['share_with_displayname'] = $share->getSharedWith(); |
|
138 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
139 | - |
|
140 | - $result['share_with'] = $share->getPassword(); |
|
141 | - $result['share_with_displayname'] = $share->getPassword(); |
|
142 | - |
|
143 | - $result['token'] = $share->getToken(); |
|
144 | - $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]); |
|
145 | - |
|
146 | - $expiration = $share->getExpirationDate(); |
|
147 | - if ($expiration !== null) { |
|
148 | - $result['expiration'] = $expiration->format('Y-m-d 00:00:00'); |
|
149 | - } |
|
150 | - |
|
151 | - } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) { |
|
152 | - $result['share_with'] = $share->getSharedWith(); |
|
153 | - $result['share_with_displayname'] = $share->getSharedWith(); |
|
154 | - $result['token'] = $share->getToken(); |
|
155 | - } |
|
156 | - |
|
157 | - $result['mail_send'] = $share->getMailSend() ? 1 : 0; |
|
158 | - |
|
159 | - return $result; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Get a specific share by id |
|
164 | - * |
|
165 | - * @param string $id |
|
166 | - * @return \OC_OCS_Result |
|
167 | - */ |
|
168 | - public function getShare($id) { |
|
169 | - if (!$this->shareManager->shareApiEnabled()) { |
|
170 | - return new \OC_OCS_Result(null, 404, 'Share API is disabled'); |
|
171 | - } |
|
172 | - |
|
173 | - // Try both our default, and our federated provider.. |
|
174 | - $share = null; |
|
175 | - |
|
176 | - // First check if it is an internal share. |
|
177 | - |
|
178 | - try { |
|
179 | - $share = $this->shareManager->getShareById('ocinternal:'.$id); |
|
180 | - } catch (ShareNotFound $e) { |
|
181 | - // Ignore for now |
|
182 | - //return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
183 | - } |
|
184 | - |
|
185 | - if ($share === null) { |
|
186 | - if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
187 | - return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
188 | - } |
|
189 | - |
|
190 | - try { |
|
191 | - $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id); |
|
192 | - } catch (ShareNotFound $e) { |
|
193 | - return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
194 | - } |
|
195 | - } |
|
196 | - |
|
197 | - if ($this->canAccessShare($share)) { |
|
198 | - try { |
|
199 | - $share = $this->formatShare($share); |
|
200 | - return new \OC_OCS_Result([$share]); |
|
201 | - } catch (NotFoundException $e) { |
|
202 | - //Fall trough |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Delete a share |
|
211 | - * |
|
212 | - * @param string $id |
|
213 | - * @return \OC_OCS_Result |
|
214 | - */ |
|
215 | - public function deleteShare($id) { |
|
216 | - if (!$this->shareManager->shareApiEnabled()) { |
|
217 | - return new \OC_OCS_Result(null, 404, 'Share API is disabled'); |
|
218 | - } |
|
219 | - |
|
220 | - // Try both our default and our federated provider |
|
221 | - $share = null; |
|
222 | - |
|
223 | - try { |
|
224 | - $share = $this->shareManager->getShareById('ocinternal:' . $id); |
|
225 | - } catch (ShareNotFound $e) { |
|
226 | - //Ignore for now |
|
227 | - //return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
228 | - } |
|
229 | - |
|
230 | - // Could not find the share as internal share... maybe it is a federated share |
|
231 | - if ($share === null) { |
|
232 | - if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
233 | - return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
234 | - } |
|
235 | - |
|
236 | - try { |
|
237 | - $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id); |
|
238 | - } catch (ShareNotFound $e) { |
|
239 | - return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
240 | - } |
|
241 | - } |
|
242 | - |
|
243 | - if (!$this->canAccessShare($share, false)) { |
|
244 | - return new \OC_OCS_Result(null, 404, 'could not delete share'); |
|
245 | - } |
|
246 | - |
|
247 | - $this->shareManager->deleteShare($share); |
|
248 | - |
|
249 | - return new \OC_OCS_Result(); |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * @return \OC_OCS_Result |
|
254 | - */ |
|
255 | - public function createShare() { |
|
256 | - $share = $this->shareManager->newShare(); |
|
257 | - |
|
258 | - if (!$this->shareManager->shareApiEnabled()) { |
|
259 | - return new \OC_OCS_Result(null, 404, 'Share API is disabled'); |
|
260 | - } |
|
261 | - |
|
262 | - // Verify path |
|
263 | - $path = $this->request->getParam('path', null); |
|
264 | - if ($path === null) { |
|
265 | - return new \OC_OCS_Result(null, 404, 'please specify a file or folder path'); |
|
266 | - } |
|
267 | - |
|
268 | - $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); |
|
269 | - try { |
|
270 | - $path = $userFolder->get($path); |
|
271 | - } catch (\OCP\Files\NotFoundException $e) { |
|
272 | - return new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist'); |
|
273 | - } |
|
274 | - |
|
275 | - $share->setNode($path); |
|
276 | - |
|
277 | - // Parse permissions (if available) |
|
278 | - $permissions = $this->request->getParam('permissions', null); |
|
279 | - if ($permissions === null) { |
|
280 | - $permissions = \OCP\Constants::PERMISSION_ALL; |
|
281 | - } else { |
|
282 | - $permissions = (int)$permissions; |
|
283 | - } |
|
284 | - |
|
285 | - if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) { |
|
286 | - return new \OC_OCS_Result(null, 404, 'invalid permissions'); |
|
287 | - } |
|
288 | - |
|
289 | - // Shares always require read permissions |
|
290 | - $permissions |= \OCP\Constants::PERMISSION_READ; |
|
291 | - |
|
292 | - if ($path instanceof \OCP\Files\File) { |
|
293 | - // Single file shares should never have delete or create permissions |
|
294 | - $permissions &= ~\OCP\Constants::PERMISSION_DELETE; |
|
295 | - $permissions &= ~\OCP\Constants::PERMISSION_CREATE; |
|
296 | - } |
|
297 | - |
|
298 | - /* |
|
40 | + /** @var IManager */ |
|
41 | + private $shareManager; |
|
42 | + /** @var IGroupManager */ |
|
43 | + private $groupManager; |
|
44 | + /** @var IUserManager */ |
|
45 | + private $userManager; |
|
46 | + /** @var IRequest */ |
|
47 | + private $request; |
|
48 | + /** @var IRootFolder */ |
|
49 | + private $rootFolder; |
|
50 | + /** @var IUrlGenerator */ |
|
51 | + private $urlGenerator; |
|
52 | + /** @var IUser */ |
|
53 | + private $currentUser; |
|
54 | + |
|
55 | + /** |
|
56 | + * Share20OCS constructor. |
|
57 | + * |
|
58 | + * @param IManager $shareManager |
|
59 | + * @param IGroupManager $groupManager |
|
60 | + * @param IUserManager $userManager |
|
61 | + * @param IRequest $request |
|
62 | + * @param IRootFolder $rootFolder |
|
63 | + * @param IURLGenerator $urlGenerator |
|
64 | + * @param IUser $currentUser |
|
65 | + */ |
|
66 | + public function __construct( |
|
67 | + IManager $shareManager, |
|
68 | + IGroupManager $groupManager, |
|
69 | + IUserManager $userManager, |
|
70 | + IRequest $request, |
|
71 | + IRootFolder $rootFolder, |
|
72 | + IURLGenerator $urlGenerator, |
|
73 | + IUser $currentUser |
|
74 | + ) { |
|
75 | + $this->shareManager = $shareManager; |
|
76 | + $this->userManager = $userManager; |
|
77 | + $this->groupManager = $groupManager; |
|
78 | + $this->request = $request; |
|
79 | + $this->rootFolder = $rootFolder; |
|
80 | + $this->urlGenerator = $urlGenerator; |
|
81 | + $this->currentUser = $currentUser; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Convert an IShare to an array for OCS output |
|
86 | + * |
|
87 | + * @param \OCP\Share\IShare $share |
|
88 | + * @return array |
|
89 | + * @throws NotFoundException In case the node can't be resolved. |
|
90 | + */ |
|
91 | + protected function formatShare(\OCP\Share\IShare $share) { |
|
92 | + $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
93 | + $shareOwner = $this->userManager->get($share->getShareOwner()); |
|
94 | + $result = [ |
|
95 | + 'id' => $share->getId(), |
|
96 | + 'share_type' => $share->getShareType(), |
|
97 | + 'uid_owner' => $share->getSharedBy(), |
|
98 | + 'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(), |
|
99 | + 'permissions' => $share->getPermissions(), |
|
100 | + 'stime' => $share->getShareTime()->getTimestamp(), |
|
101 | + 'parent' => null, |
|
102 | + 'expiration' => null, |
|
103 | + 'token' => null, |
|
104 | + 'uid_file_owner' => $share->getShareOwner(), |
|
105 | + 'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(), |
|
106 | + ]; |
|
107 | + |
|
108 | + $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); |
|
109 | + $nodes = $userFolder->getById($share->getNodeId()); |
|
110 | + |
|
111 | + if (empty($nodes)) { |
|
112 | + throw new NotFoundException(); |
|
113 | + } |
|
114 | + |
|
115 | + $node = $nodes[0]; |
|
116 | + $result['path'] = $userFolder->getRelativePath($node->getPath()); |
|
117 | + |
|
118 | + if ($node instanceOf \OCP\Files\Folder) { |
|
119 | + $result['item_type'] = 'folder'; |
|
120 | + } else { |
|
121 | + $result['item_type'] = 'file'; |
|
122 | + } |
|
123 | + $result['mimetype'] = $node->getMimeType(); |
|
124 | + $result['storage_id'] = $node->getStorage()->getId(); |
|
125 | + $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); |
|
126 | + $result['item_source'] = $node->getId(); |
|
127 | + $result['file_source'] = $node->getId(); |
|
128 | + $result['file_parent'] = $node->getParent()->getId(); |
|
129 | + $result['file_target'] = $share->getTarget(); |
|
130 | + |
|
131 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
|
132 | + $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
133 | + $result['share_with'] = $share->getSharedWith(); |
|
134 | + $result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith(); |
|
135 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
136 | + $result['share_with'] = $share->getSharedWith(); |
|
137 | + $result['share_with_displayname'] = $share->getSharedWith(); |
|
138 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
139 | + |
|
140 | + $result['share_with'] = $share->getPassword(); |
|
141 | + $result['share_with_displayname'] = $share->getPassword(); |
|
142 | + |
|
143 | + $result['token'] = $share->getToken(); |
|
144 | + $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]); |
|
145 | + |
|
146 | + $expiration = $share->getExpirationDate(); |
|
147 | + if ($expiration !== null) { |
|
148 | + $result['expiration'] = $expiration->format('Y-m-d 00:00:00'); |
|
149 | + } |
|
150 | + |
|
151 | + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) { |
|
152 | + $result['share_with'] = $share->getSharedWith(); |
|
153 | + $result['share_with_displayname'] = $share->getSharedWith(); |
|
154 | + $result['token'] = $share->getToken(); |
|
155 | + } |
|
156 | + |
|
157 | + $result['mail_send'] = $share->getMailSend() ? 1 : 0; |
|
158 | + |
|
159 | + return $result; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Get a specific share by id |
|
164 | + * |
|
165 | + * @param string $id |
|
166 | + * @return \OC_OCS_Result |
|
167 | + */ |
|
168 | + public function getShare($id) { |
|
169 | + if (!$this->shareManager->shareApiEnabled()) { |
|
170 | + return new \OC_OCS_Result(null, 404, 'Share API is disabled'); |
|
171 | + } |
|
172 | + |
|
173 | + // Try both our default, and our federated provider.. |
|
174 | + $share = null; |
|
175 | + |
|
176 | + // First check if it is an internal share. |
|
177 | + |
|
178 | + try { |
|
179 | + $share = $this->shareManager->getShareById('ocinternal:'.$id); |
|
180 | + } catch (ShareNotFound $e) { |
|
181 | + // Ignore for now |
|
182 | + //return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
183 | + } |
|
184 | + |
|
185 | + if ($share === null) { |
|
186 | + if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
187 | + return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
188 | + } |
|
189 | + |
|
190 | + try { |
|
191 | + $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id); |
|
192 | + } catch (ShareNotFound $e) { |
|
193 | + return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
194 | + } |
|
195 | + } |
|
196 | + |
|
197 | + if ($this->canAccessShare($share)) { |
|
198 | + try { |
|
199 | + $share = $this->formatShare($share); |
|
200 | + return new \OC_OCS_Result([$share]); |
|
201 | + } catch (NotFoundException $e) { |
|
202 | + //Fall trough |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Delete a share |
|
211 | + * |
|
212 | + * @param string $id |
|
213 | + * @return \OC_OCS_Result |
|
214 | + */ |
|
215 | + public function deleteShare($id) { |
|
216 | + if (!$this->shareManager->shareApiEnabled()) { |
|
217 | + return new \OC_OCS_Result(null, 404, 'Share API is disabled'); |
|
218 | + } |
|
219 | + |
|
220 | + // Try both our default and our federated provider |
|
221 | + $share = null; |
|
222 | + |
|
223 | + try { |
|
224 | + $share = $this->shareManager->getShareById('ocinternal:' . $id); |
|
225 | + } catch (ShareNotFound $e) { |
|
226 | + //Ignore for now |
|
227 | + //return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
228 | + } |
|
229 | + |
|
230 | + // Could not find the share as internal share... maybe it is a federated share |
|
231 | + if ($share === null) { |
|
232 | + if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
233 | + return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
234 | + } |
|
235 | + |
|
236 | + try { |
|
237 | + $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id); |
|
238 | + } catch (ShareNotFound $e) { |
|
239 | + return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
240 | + } |
|
241 | + } |
|
242 | + |
|
243 | + if (!$this->canAccessShare($share, false)) { |
|
244 | + return new \OC_OCS_Result(null, 404, 'could not delete share'); |
|
245 | + } |
|
246 | + |
|
247 | + $this->shareManager->deleteShare($share); |
|
248 | + |
|
249 | + return new \OC_OCS_Result(); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * @return \OC_OCS_Result |
|
254 | + */ |
|
255 | + public function createShare() { |
|
256 | + $share = $this->shareManager->newShare(); |
|
257 | + |
|
258 | + if (!$this->shareManager->shareApiEnabled()) { |
|
259 | + return new \OC_OCS_Result(null, 404, 'Share API is disabled'); |
|
260 | + } |
|
261 | + |
|
262 | + // Verify path |
|
263 | + $path = $this->request->getParam('path', null); |
|
264 | + if ($path === null) { |
|
265 | + return new \OC_OCS_Result(null, 404, 'please specify a file or folder path'); |
|
266 | + } |
|
267 | + |
|
268 | + $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); |
|
269 | + try { |
|
270 | + $path = $userFolder->get($path); |
|
271 | + } catch (\OCP\Files\NotFoundException $e) { |
|
272 | + return new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist'); |
|
273 | + } |
|
274 | + |
|
275 | + $share->setNode($path); |
|
276 | + |
|
277 | + // Parse permissions (if available) |
|
278 | + $permissions = $this->request->getParam('permissions', null); |
|
279 | + if ($permissions === null) { |
|
280 | + $permissions = \OCP\Constants::PERMISSION_ALL; |
|
281 | + } else { |
|
282 | + $permissions = (int)$permissions; |
|
283 | + } |
|
284 | + |
|
285 | + if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) { |
|
286 | + return new \OC_OCS_Result(null, 404, 'invalid permissions'); |
|
287 | + } |
|
288 | + |
|
289 | + // Shares always require read permissions |
|
290 | + $permissions |= \OCP\Constants::PERMISSION_READ; |
|
291 | + |
|
292 | + if ($path instanceof \OCP\Files\File) { |
|
293 | + // Single file shares should never have delete or create permissions |
|
294 | + $permissions &= ~\OCP\Constants::PERMISSION_DELETE; |
|
295 | + $permissions &= ~\OCP\Constants::PERMISSION_CREATE; |
|
296 | + } |
|
297 | + |
|
298 | + /* |
|
299 | 299 | * Hack for https://github.com/owncloud/core/issues/22587 |
300 | 300 | * We check the permissions via webdav. But the permissions of the mount point |
301 | 301 | * do not equal the share permissions. Here we fix that for federated mounts. |
302 | 302 | */ |
303 | - if ($path->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
304 | - $permissions &= ~($permissions & ~$path->getPermissions()); |
|
305 | - } |
|
306 | - |
|
307 | - $shareWith = $this->request->getParam('shareWith', null); |
|
308 | - $shareType = (int)$this->request->getParam('shareType', '-1'); |
|
309 | - |
|
310 | - if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
311 | - // Valid user is required to share |
|
312 | - if ($shareWith === null || !$this->userManager->userExists($shareWith)) { |
|
313 | - return new \OC_OCS_Result(null, 404, 'please specify a valid user'); |
|
314 | - } |
|
315 | - $share->setSharedWith($shareWith); |
|
316 | - $share->setPermissions($permissions); |
|
317 | - } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
318 | - if (!$this->shareManager->allowGroupSharing()) { |
|
319 | - return new \OC_OCS_Result(null, 404, 'group sharing is disabled by the administrator'); |
|
320 | - } |
|
321 | - |
|
322 | - // Valid group is required to share |
|
323 | - if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) { |
|
324 | - return new \OC_OCS_Result(null, 404, 'please specify a valid group'); |
|
325 | - } |
|
326 | - $share->setSharedWith($shareWith); |
|
327 | - $share->setPermissions($permissions); |
|
328 | - } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { |
|
329 | - //Can we even share links? |
|
330 | - if (!$this->shareManager->shareApiAllowLinks()) { |
|
331 | - return new \OC_OCS_Result(null, 404, 'public link sharing is disabled by the administrator'); |
|
332 | - } |
|
333 | - |
|
334 | - /* |
|
303 | + if ($path->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
304 | + $permissions &= ~($permissions & ~$path->getPermissions()); |
|
305 | + } |
|
306 | + |
|
307 | + $shareWith = $this->request->getParam('shareWith', null); |
|
308 | + $shareType = (int)$this->request->getParam('shareType', '-1'); |
|
309 | + |
|
310 | + if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
|
311 | + // Valid user is required to share |
|
312 | + if ($shareWith === null || !$this->userManager->userExists($shareWith)) { |
|
313 | + return new \OC_OCS_Result(null, 404, 'please specify a valid user'); |
|
314 | + } |
|
315 | + $share->setSharedWith($shareWith); |
|
316 | + $share->setPermissions($permissions); |
|
317 | + } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
|
318 | + if (!$this->shareManager->allowGroupSharing()) { |
|
319 | + return new \OC_OCS_Result(null, 404, 'group sharing is disabled by the administrator'); |
|
320 | + } |
|
321 | + |
|
322 | + // Valid group is required to share |
|
323 | + if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) { |
|
324 | + return new \OC_OCS_Result(null, 404, 'please specify a valid group'); |
|
325 | + } |
|
326 | + $share->setSharedWith($shareWith); |
|
327 | + $share->setPermissions($permissions); |
|
328 | + } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { |
|
329 | + //Can we even share links? |
|
330 | + if (!$this->shareManager->shareApiAllowLinks()) { |
|
331 | + return new \OC_OCS_Result(null, 404, 'public link sharing is disabled by the administrator'); |
|
332 | + } |
|
333 | + |
|
334 | + /* |
|
335 | 335 | * For now we only allow 1 link share. |
336 | 336 | * Return the existing link share if this is a duplicate |
337 | 337 | */ |
338 | - $existingShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0); |
|
339 | - if (!empty($existingShares)) { |
|
340 | - return new \OC_OCS_Result($this->formatShare($existingShares[0])); |
|
341 | - } |
|
342 | - |
|
343 | - $publicUpload = $this->request->getParam('publicUpload', null); |
|
344 | - if ($publicUpload === 'true') { |
|
345 | - // Check if public upload is allowed |
|
346 | - if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { |
|
347 | - return new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator'); |
|
348 | - } |
|
349 | - |
|
350 | - // Public upload can only be set for folders |
|
351 | - if ($path instanceof \OCP\Files\File) { |
|
352 | - return new \OC_OCS_Result(null, 404, 'public upload is only possible for public shared folders'); |
|
353 | - } |
|
354 | - |
|
355 | - $share->setPermissions( |
|
356 | - \OCP\Constants::PERMISSION_READ | |
|
357 | - \OCP\Constants::PERMISSION_CREATE | |
|
358 | - \OCP\Constants::PERMISSION_UPDATE |
|
359 | - ); |
|
360 | - } else { |
|
361 | - $share->setPermissions(\OCP\Constants::PERMISSION_READ); |
|
362 | - } |
|
363 | - |
|
364 | - // Set password |
|
365 | - $password = $this->request->getParam('password', ''); |
|
366 | - |
|
367 | - if ($password !== '') { |
|
368 | - $share->setPassword($password); |
|
369 | - } |
|
370 | - |
|
371 | - //Expire date |
|
372 | - $expireDate = $this->request->getParam('expireDate', ''); |
|
373 | - |
|
374 | - if ($expireDate !== '') { |
|
375 | - try { |
|
376 | - $expireDate = $this->parseDate($expireDate); |
|
377 | - $share->setExpirationDate($expireDate); |
|
378 | - } catch (\Exception $e) { |
|
379 | - return new \OC_OCS_Result(null, 404, 'Invalid Date. Format must be YYYY-MM-DD.'); |
|
380 | - } |
|
381 | - } |
|
382 | - |
|
383 | - } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) { |
|
384 | - if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
385 | - return new \OC_OCS_Result(null, 403, 'Sharing '.$path.' failed, because the backend does not allow shares from type '.$shareType); |
|
386 | - } |
|
387 | - |
|
388 | - $share->setSharedWith($shareWith); |
|
389 | - $share->setPermissions($permissions); |
|
390 | - } else { |
|
391 | - return new \OC_OCS_Result(null, 400, "unknown share type"); |
|
392 | - } |
|
393 | - |
|
394 | - $share->setShareType($shareType); |
|
395 | - $share->setSharedBy($this->currentUser->getUID()); |
|
396 | - |
|
397 | - try { |
|
398 | - $share = $this->shareManager->createShare($share); |
|
399 | - } catch (GenericShareException $e) { |
|
400 | - $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
|
401 | - return new \OC_OCS_Result(null, $code, $e->getHint()); |
|
402 | - }catch (\Exception $e) { |
|
403 | - return new \OC_OCS_Result(null, 403, $e->getMessage()); |
|
404 | - } |
|
405 | - |
|
406 | - $share = $this->formatShare($share); |
|
407 | - return new \OC_OCS_Result($share); |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * @param \OCP\Files\File|\OCP\Files\Folder $node |
|
412 | - * @return \OC_OCS_Result |
|
413 | - */ |
|
414 | - private function getSharedWithMe($node = null) { |
|
415 | - $userShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, -1, 0); |
|
416 | - $groupShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0); |
|
417 | - |
|
418 | - $shares = array_merge($userShares, $groupShares); |
|
419 | - |
|
420 | - $formatted = []; |
|
421 | - foreach ($shares as $share) { |
|
422 | - if ($this->canAccessShare($share)) { |
|
423 | - try { |
|
424 | - $formatted[] = $this->formatShare($share); |
|
425 | - } catch (NotFoundException $e) { |
|
426 | - // Ignore this share |
|
427 | - } |
|
428 | - } |
|
429 | - } |
|
430 | - |
|
431 | - return new \OC_OCS_Result($formatted); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * @param \OCP\Files\Folder $folder |
|
436 | - * @return \OC_OCS_Result |
|
437 | - */ |
|
438 | - private function getSharesInDir($folder) { |
|
439 | - if (!($folder instanceof \OCP\Files\Folder)) { |
|
440 | - return new \OC_OCS_Result(null, 400, "not a directory"); |
|
441 | - } |
|
442 | - |
|
443 | - $nodes = $folder->getDirectoryListing(); |
|
444 | - /** @var \OCP\Share\IShare[] $shares */ |
|
445 | - $shares = []; |
|
446 | - foreach ($nodes as $node) { |
|
447 | - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, false, -1, 0)); |
|
448 | - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $node, false, -1, 0)); |
|
449 | - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $node, false, -1, 0)); |
|
450 | - if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
451 | - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_REMOTE, $node, false, -1, 0)); |
|
452 | - } |
|
453 | - } |
|
454 | - |
|
455 | - $formatted = []; |
|
456 | - foreach ($shares as $share) { |
|
457 | - try { |
|
458 | - $formatted[] = $this->formatShare($share); |
|
459 | - } catch (NotFoundException $e) { |
|
460 | - //Ignore this share |
|
461 | - } |
|
462 | - } |
|
463 | - |
|
464 | - return new \OC_OCS_Result($formatted); |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * The getShares function. |
|
469 | - * |
|
470 | - * - Get shares by the current user |
|
471 | - * - Get shares by the current user and reshares (?reshares=true) |
|
472 | - * - Get shares with the current user (?shared_with_me=true) |
|
473 | - * - Get shares for a specific path (?path=...) |
|
474 | - * - Get all shares in a folder (?subfiles=true&path=..) |
|
475 | - * |
|
476 | - * @return \OC_OCS_Result |
|
477 | - */ |
|
478 | - public function getShares() { |
|
479 | - if (!$this->shareManager->shareApiEnabled()) { |
|
480 | - return new \OC_OCS_Result(); |
|
481 | - } |
|
482 | - |
|
483 | - $sharedWithMe = $this->request->getParam('shared_with_me', null); |
|
484 | - $reshares = $this->request->getParam('reshares', null); |
|
485 | - $subfiles = $this->request->getParam('subfiles'); |
|
486 | - $path = $this->request->getParam('path', null); |
|
487 | - |
|
488 | - if ($path !== null) { |
|
489 | - $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); |
|
490 | - try { |
|
491 | - $path = $userFolder->get($path); |
|
492 | - } catch (\OCP\Files\NotFoundException $e) { |
|
493 | - return new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist'); |
|
494 | - } |
|
495 | - } |
|
496 | - |
|
497 | - if ($sharedWithMe === 'true') { |
|
498 | - return $this->getSharedWithMe($path); |
|
499 | - } |
|
500 | - |
|
501 | - if ($subfiles === 'true') { |
|
502 | - return $this->getSharesInDir($path); |
|
503 | - } |
|
504 | - |
|
505 | - if ($reshares === 'true') { |
|
506 | - $reshares = true; |
|
507 | - } else { |
|
508 | - $reshares = false; |
|
509 | - } |
|
510 | - |
|
511 | - // Get all shares |
|
512 | - $userShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $path, $reshares, -1, 0); |
|
513 | - $groupShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0); |
|
514 | - $linkShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0); |
|
515 | - $shares = array_merge($userShares, $groupShares, $linkShares); |
|
516 | - |
|
517 | - if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
518 | - $federatedShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0); |
|
519 | - $shares = array_merge($shares, $federatedShares); |
|
520 | - } |
|
521 | - |
|
522 | - |
|
523 | - $formatted = []; |
|
524 | - foreach ($shares as $share) { |
|
525 | - try { |
|
526 | - $formatted[] = $this->formatShare($share); |
|
527 | - } catch (NotFoundException $e) { |
|
528 | - //Ignore share |
|
529 | - } |
|
530 | - } |
|
531 | - |
|
532 | - return new \OC_OCS_Result($formatted); |
|
533 | - } |
|
534 | - |
|
535 | - /** |
|
536 | - * @param int $id |
|
537 | - * @return \OC_OCS_Result |
|
538 | - */ |
|
539 | - public function updateShare($id) { |
|
540 | - if (!$this->shareManager->shareApiEnabled()) { |
|
541 | - return new \OC_OCS_Result(null, 404, 'Share API is disabled'); |
|
542 | - } |
|
543 | - |
|
544 | - // Try both our default and our federated provider |
|
545 | - $share = null; |
|
546 | - |
|
547 | - try { |
|
548 | - $share = $this->shareManager->getShareById('ocinternal:' . $id); |
|
549 | - } catch (ShareNotFound $e) { |
|
550 | - //Ignore for now |
|
551 | - //return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
552 | - } |
|
553 | - |
|
554 | - // Could not find the share as internal share... maybe it is a federated share |
|
555 | - if ($share === null) { |
|
556 | - if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
557 | - return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
558 | - } |
|
559 | - |
|
560 | - try { |
|
561 | - $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id); |
|
562 | - } catch (ShareNotFound $e) { |
|
563 | - return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
564 | - } |
|
565 | - } |
|
566 | - |
|
567 | - if (!$this->canAccessShare($share, false)) { |
|
568 | - return new \OC_OCS_Result(null, 404, 'wrong share Id, share doesn\'t exist.'); |
|
569 | - } |
|
570 | - |
|
571 | - $permissions = $this->request->getParam('permissions', null); |
|
572 | - $password = $this->request->getParam('password', null); |
|
573 | - $publicUpload = $this->request->getParam('publicUpload', null); |
|
574 | - $expireDate = $this->request->getParam('expireDate', null); |
|
575 | - |
|
576 | - /* |
|
338 | + $existingShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0); |
|
339 | + if (!empty($existingShares)) { |
|
340 | + return new \OC_OCS_Result($this->formatShare($existingShares[0])); |
|
341 | + } |
|
342 | + |
|
343 | + $publicUpload = $this->request->getParam('publicUpload', null); |
|
344 | + if ($publicUpload === 'true') { |
|
345 | + // Check if public upload is allowed |
|
346 | + if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { |
|
347 | + return new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator'); |
|
348 | + } |
|
349 | + |
|
350 | + // Public upload can only be set for folders |
|
351 | + if ($path instanceof \OCP\Files\File) { |
|
352 | + return new \OC_OCS_Result(null, 404, 'public upload is only possible for public shared folders'); |
|
353 | + } |
|
354 | + |
|
355 | + $share->setPermissions( |
|
356 | + \OCP\Constants::PERMISSION_READ | |
|
357 | + \OCP\Constants::PERMISSION_CREATE | |
|
358 | + \OCP\Constants::PERMISSION_UPDATE |
|
359 | + ); |
|
360 | + } else { |
|
361 | + $share->setPermissions(\OCP\Constants::PERMISSION_READ); |
|
362 | + } |
|
363 | + |
|
364 | + // Set password |
|
365 | + $password = $this->request->getParam('password', ''); |
|
366 | + |
|
367 | + if ($password !== '') { |
|
368 | + $share->setPassword($password); |
|
369 | + } |
|
370 | + |
|
371 | + //Expire date |
|
372 | + $expireDate = $this->request->getParam('expireDate', ''); |
|
373 | + |
|
374 | + if ($expireDate !== '') { |
|
375 | + try { |
|
376 | + $expireDate = $this->parseDate($expireDate); |
|
377 | + $share->setExpirationDate($expireDate); |
|
378 | + } catch (\Exception $e) { |
|
379 | + return new \OC_OCS_Result(null, 404, 'Invalid Date. Format must be YYYY-MM-DD.'); |
|
380 | + } |
|
381 | + } |
|
382 | + |
|
383 | + } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) { |
|
384 | + if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
385 | + return new \OC_OCS_Result(null, 403, 'Sharing '.$path.' failed, because the backend does not allow shares from type '.$shareType); |
|
386 | + } |
|
387 | + |
|
388 | + $share->setSharedWith($shareWith); |
|
389 | + $share->setPermissions($permissions); |
|
390 | + } else { |
|
391 | + return new \OC_OCS_Result(null, 400, "unknown share type"); |
|
392 | + } |
|
393 | + |
|
394 | + $share->setShareType($shareType); |
|
395 | + $share->setSharedBy($this->currentUser->getUID()); |
|
396 | + |
|
397 | + try { |
|
398 | + $share = $this->shareManager->createShare($share); |
|
399 | + } catch (GenericShareException $e) { |
|
400 | + $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
|
401 | + return new \OC_OCS_Result(null, $code, $e->getHint()); |
|
402 | + }catch (\Exception $e) { |
|
403 | + return new \OC_OCS_Result(null, 403, $e->getMessage()); |
|
404 | + } |
|
405 | + |
|
406 | + $share = $this->formatShare($share); |
|
407 | + return new \OC_OCS_Result($share); |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * @param \OCP\Files\File|\OCP\Files\Folder $node |
|
412 | + * @return \OC_OCS_Result |
|
413 | + */ |
|
414 | + private function getSharedWithMe($node = null) { |
|
415 | + $userShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, -1, 0); |
|
416 | + $groupShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0); |
|
417 | + |
|
418 | + $shares = array_merge($userShares, $groupShares); |
|
419 | + |
|
420 | + $formatted = []; |
|
421 | + foreach ($shares as $share) { |
|
422 | + if ($this->canAccessShare($share)) { |
|
423 | + try { |
|
424 | + $formatted[] = $this->formatShare($share); |
|
425 | + } catch (NotFoundException $e) { |
|
426 | + // Ignore this share |
|
427 | + } |
|
428 | + } |
|
429 | + } |
|
430 | + |
|
431 | + return new \OC_OCS_Result($formatted); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * @param \OCP\Files\Folder $folder |
|
436 | + * @return \OC_OCS_Result |
|
437 | + */ |
|
438 | + private function getSharesInDir($folder) { |
|
439 | + if (!($folder instanceof \OCP\Files\Folder)) { |
|
440 | + return new \OC_OCS_Result(null, 400, "not a directory"); |
|
441 | + } |
|
442 | + |
|
443 | + $nodes = $folder->getDirectoryListing(); |
|
444 | + /** @var \OCP\Share\IShare[] $shares */ |
|
445 | + $shares = []; |
|
446 | + foreach ($nodes as $node) { |
|
447 | + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, false, -1, 0)); |
|
448 | + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $node, false, -1, 0)); |
|
449 | + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $node, false, -1, 0)); |
|
450 | + if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
451 | + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_REMOTE, $node, false, -1, 0)); |
|
452 | + } |
|
453 | + } |
|
454 | + |
|
455 | + $formatted = []; |
|
456 | + foreach ($shares as $share) { |
|
457 | + try { |
|
458 | + $formatted[] = $this->formatShare($share); |
|
459 | + } catch (NotFoundException $e) { |
|
460 | + //Ignore this share |
|
461 | + } |
|
462 | + } |
|
463 | + |
|
464 | + return new \OC_OCS_Result($formatted); |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * The getShares function. |
|
469 | + * |
|
470 | + * - Get shares by the current user |
|
471 | + * - Get shares by the current user and reshares (?reshares=true) |
|
472 | + * - Get shares with the current user (?shared_with_me=true) |
|
473 | + * - Get shares for a specific path (?path=...) |
|
474 | + * - Get all shares in a folder (?subfiles=true&path=..) |
|
475 | + * |
|
476 | + * @return \OC_OCS_Result |
|
477 | + */ |
|
478 | + public function getShares() { |
|
479 | + if (!$this->shareManager->shareApiEnabled()) { |
|
480 | + return new \OC_OCS_Result(); |
|
481 | + } |
|
482 | + |
|
483 | + $sharedWithMe = $this->request->getParam('shared_with_me', null); |
|
484 | + $reshares = $this->request->getParam('reshares', null); |
|
485 | + $subfiles = $this->request->getParam('subfiles'); |
|
486 | + $path = $this->request->getParam('path', null); |
|
487 | + |
|
488 | + if ($path !== null) { |
|
489 | + $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); |
|
490 | + try { |
|
491 | + $path = $userFolder->get($path); |
|
492 | + } catch (\OCP\Files\NotFoundException $e) { |
|
493 | + return new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist'); |
|
494 | + } |
|
495 | + } |
|
496 | + |
|
497 | + if ($sharedWithMe === 'true') { |
|
498 | + return $this->getSharedWithMe($path); |
|
499 | + } |
|
500 | + |
|
501 | + if ($subfiles === 'true') { |
|
502 | + return $this->getSharesInDir($path); |
|
503 | + } |
|
504 | + |
|
505 | + if ($reshares === 'true') { |
|
506 | + $reshares = true; |
|
507 | + } else { |
|
508 | + $reshares = false; |
|
509 | + } |
|
510 | + |
|
511 | + // Get all shares |
|
512 | + $userShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $path, $reshares, -1, 0); |
|
513 | + $groupShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0); |
|
514 | + $linkShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0); |
|
515 | + $shares = array_merge($userShares, $groupShares, $linkShares); |
|
516 | + |
|
517 | + if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
518 | + $federatedShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0); |
|
519 | + $shares = array_merge($shares, $federatedShares); |
|
520 | + } |
|
521 | + |
|
522 | + |
|
523 | + $formatted = []; |
|
524 | + foreach ($shares as $share) { |
|
525 | + try { |
|
526 | + $formatted[] = $this->formatShare($share); |
|
527 | + } catch (NotFoundException $e) { |
|
528 | + //Ignore share |
|
529 | + } |
|
530 | + } |
|
531 | + |
|
532 | + return new \OC_OCS_Result($formatted); |
|
533 | + } |
|
534 | + |
|
535 | + /** |
|
536 | + * @param int $id |
|
537 | + * @return \OC_OCS_Result |
|
538 | + */ |
|
539 | + public function updateShare($id) { |
|
540 | + if (!$this->shareManager->shareApiEnabled()) { |
|
541 | + return new \OC_OCS_Result(null, 404, 'Share API is disabled'); |
|
542 | + } |
|
543 | + |
|
544 | + // Try both our default and our federated provider |
|
545 | + $share = null; |
|
546 | + |
|
547 | + try { |
|
548 | + $share = $this->shareManager->getShareById('ocinternal:' . $id); |
|
549 | + } catch (ShareNotFound $e) { |
|
550 | + //Ignore for now |
|
551 | + //return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
552 | + } |
|
553 | + |
|
554 | + // Could not find the share as internal share... maybe it is a federated share |
|
555 | + if ($share === null) { |
|
556 | + if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
557 | + return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
558 | + } |
|
559 | + |
|
560 | + try { |
|
561 | + $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id); |
|
562 | + } catch (ShareNotFound $e) { |
|
563 | + return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
|
564 | + } |
|
565 | + } |
|
566 | + |
|
567 | + if (!$this->canAccessShare($share, false)) { |
|
568 | + return new \OC_OCS_Result(null, 404, 'wrong share Id, share doesn\'t exist.'); |
|
569 | + } |
|
570 | + |
|
571 | + $permissions = $this->request->getParam('permissions', null); |
|
572 | + $password = $this->request->getParam('password', null); |
|
573 | + $publicUpload = $this->request->getParam('publicUpload', null); |
|
574 | + $expireDate = $this->request->getParam('expireDate', null); |
|
575 | + |
|
576 | + /* |
|
577 | 577 | * expirationdate, password and publicUpload only make sense for link shares |
578 | 578 | */ |
579 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
580 | - if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) { |
|
581 | - return new \OC_OCS_Result(null, 400, 'Wrong or no update parameter given'); |
|
582 | - } |
|
583 | - |
|
584 | - $newPermissions = null; |
|
585 | - if ($publicUpload === 'true') { |
|
586 | - $newPermissions = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE; |
|
587 | - } else if ($publicUpload === 'false') { |
|
588 | - $newPermissions = \OCP\Constants::PERMISSION_READ; |
|
589 | - } |
|
590 | - |
|
591 | - if ($permissions !== null) { |
|
592 | - $newPermissions = (int)$permissions; |
|
593 | - } |
|
594 | - |
|
595 | - if ($newPermissions !== null && |
|
596 | - $newPermissions !== \OCP\Constants::PERMISSION_READ && |
|
597 | - $newPermissions !== (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE) && |
|
598 | - $newPermissions !== (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)) { |
|
599 | - return new \OC_OCS_Result(null, 400, 'can\'t change permission for public link share'); |
|
600 | - } |
|
601 | - |
|
602 | - if ($newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)) { |
|
603 | - if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { |
|
604 | - return new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator'); |
|
605 | - } |
|
606 | - |
|
607 | - if (!($share->getNode() instanceof \OCP\Files\Folder)) { |
|
608 | - return new \OC_OCS_Result(null, 400, "public upload is only possible for public shared folders"); |
|
609 | - } |
|
610 | - } |
|
611 | - |
|
612 | - if ($newPermissions !== null) { |
|
613 | - $share->setPermissions($newPermissions); |
|
614 | - $permissions = $newPermissions; |
|
615 | - } |
|
616 | - |
|
617 | - if ($expireDate === '') { |
|
618 | - $share->setExpirationDate(null); |
|
619 | - } else if ($expireDate !== null) { |
|
620 | - try { |
|
621 | - $expireDate = $this->parseDate($expireDate); |
|
622 | - } catch (\Exception $e) { |
|
623 | - return new \OC_OCS_Result(null, 400, $e->getMessage()); |
|
624 | - } |
|
625 | - $share->setExpirationDate($expireDate); |
|
626 | - } |
|
627 | - |
|
628 | - if ($password === '') { |
|
629 | - $share->setPassword(null); |
|
630 | - } else if ($password !== null) { |
|
631 | - $share->setPassword($password); |
|
632 | - } |
|
633 | - |
|
634 | - } else { |
|
635 | - // For other shares only permissions is valid. |
|
636 | - if ($permissions === null) { |
|
637 | - return new \OC_OCS_Result(null, 400, 'Wrong or no update parameter given'); |
|
638 | - } else { |
|
639 | - $permissions = (int)$permissions; |
|
640 | - $share->setPermissions($permissions); |
|
641 | - } |
|
642 | - } |
|
643 | - |
|
644 | - if ($permissions !== null && $share->getShareOwner() !== $this->currentUser->getUID()) { |
|
645 | - /* Check if this is an incomming share */ |
|
646 | - $incomingShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0); |
|
647 | - $incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0)); |
|
648 | - |
|
649 | - if (!empty($incomingShares)) { |
|
650 | - $maxPermissions = 0; |
|
651 | - foreach ($incomingShares as $incomingShare) { |
|
652 | - $maxPermissions |= $incomingShare->getPermissions(); |
|
653 | - } |
|
654 | - |
|
655 | - if ($share->getPermissions() & ~$maxPermissions) { |
|
656 | - return new \OC_OCS_Result(null, 404, 'Cannot increase permissions'); |
|
657 | - } |
|
658 | - } |
|
659 | - } |
|
660 | - |
|
661 | - |
|
662 | - try { |
|
663 | - $share = $this->shareManager->updateShare($share); |
|
664 | - } catch (\Exception $e) { |
|
665 | - return new \OC_OCS_Result(null, 400, $e->getMessage()); |
|
666 | - } |
|
667 | - |
|
668 | - return new \OC_OCS_Result($this->formatShare($share)); |
|
669 | - } |
|
670 | - |
|
671 | - /** |
|
672 | - * @param \OCP\Share\IShare $share |
|
673 | - * @param bool $checkGroups |
|
674 | - * @return bool |
|
675 | - */ |
|
676 | - protected function canAccessShare(\OCP\Share\IShare $share, $checkGroups = true) { |
|
677 | - // A file with permissions 0 can't be accessed by us. So Don't show it |
|
678 | - if ($share->getPermissions() === 0) { |
|
679 | - return false; |
|
680 | - } |
|
681 | - |
|
682 | - // Owner of the file and the sharer of the file can always get share |
|
683 | - if ($share->getShareOwner() === $this->currentUser->getUID() || |
|
684 | - $share->getSharedBy() === $this->currentUser->getUID() |
|
685 | - ) { |
|
686 | - return true; |
|
687 | - } |
|
688 | - |
|
689 | - // If the share is shared with you (or a group you are a member of) |
|
690 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
691 | - $share->getSharedWith() === $this->currentUser->getUID()) { |
|
692 | - return true; |
|
693 | - } |
|
694 | - |
|
695 | - if ($checkGroups && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
696 | - $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
697 | - if ($sharedWith->inGroup($this->currentUser)) { |
|
698 | - return true; |
|
699 | - } |
|
700 | - } |
|
701 | - |
|
702 | - return false; |
|
703 | - } |
|
704 | - |
|
705 | - /** |
|
706 | - * Make sure that the passed date is valid ISO 8601 |
|
707 | - * So YYYY-MM-DD |
|
708 | - * If not throw an exception |
|
709 | - * |
|
710 | - * @param string $expireDate |
|
711 | - * |
|
712 | - * @throws \Exception |
|
713 | - * @return \DateTime |
|
714 | - */ |
|
715 | - private function parseDate($expireDate) { |
|
716 | - try { |
|
717 | - $date = new \DateTime($expireDate); |
|
718 | - } catch (\Exception $e) { |
|
719 | - throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); |
|
720 | - } |
|
721 | - |
|
722 | - if ($date === false) { |
|
723 | - throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); |
|
724 | - } |
|
725 | - |
|
726 | - $date->setTime(0,0,0); |
|
727 | - |
|
728 | - return $date; |
|
729 | - } |
|
579 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
|
580 | + if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) { |
|
581 | + return new \OC_OCS_Result(null, 400, 'Wrong or no update parameter given'); |
|
582 | + } |
|
583 | + |
|
584 | + $newPermissions = null; |
|
585 | + if ($publicUpload === 'true') { |
|
586 | + $newPermissions = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE; |
|
587 | + } else if ($publicUpload === 'false') { |
|
588 | + $newPermissions = \OCP\Constants::PERMISSION_READ; |
|
589 | + } |
|
590 | + |
|
591 | + if ($permissions !== null) { |
|
592 | + $newPermissions = (int)$permissions; |
|
593 | + } |
|
594 | + |
|
595 | + if ($newPermissions !== null && |
|
596 | + $newPermissions !== \OCP\Constants::PERMISSION_READ && |
|
597 | + $newPermissions !== (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE) && |
|
598 | + $newPermissions !== (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)) { |
|
599 | + return new \OC_OCS_Result(null, 400, 'can\'t change permission for public link share'); |
|
600 | + } |
|
601 | + |
|
602 | + if ($newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)) { |
|
603 | + if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { |
|
604 | + return new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator'); |
|
605 | + } |
|
606 | + |
|
607 | + if (!($share->getNode() instanceof \OCP\Files\Folder)) { |
|
608 | + return new \OC_OCS_Result(null, 400, "public upload is only possible for public shared folders"); |
|
609 | + } |
|
610 | + } |
|
611 | + |
|
612 | + if ($newPermissions !== null) { |
|
613 | + $share->setPermissions($newPermissions); |
|
614 | + $permissions = $newPermissions; |
|
615 | + } |
|
616 | + |
|
617 | + if ($expireDate === '') { |
|
618 | + $share->setExpirationDate(null); |
|
619 | + } else if ($expireDate !== null) { |
|
620 | + try { |
|
621 | + $expireDate = $this->parseDate($expireDate); |
|
622 | + } catch (\Exception $e) { |
|
623 | + return new \OC_OCS_Result(null, 400, $e->getMessage()); |
|
624 | + } |
|
625 | + $share->setExpirationDate($expireDate); |
|
626 | + } |
|
627 | + |
|
628 | + if ($password === '') { |
|
629 | + $share->setPassword(null); |
|
630 | + } else if ($password !== null) { |
|
631 | + $share->setPassword($password); |
|
632 | + } |
|
633 | + |
|
634 | + } else { |
|
635 | + // For other shares only permissions is valid. |
|
636 | + if ($permissions === null) { |
|
637 | + return new \OC_OCS_Result(null, 400, 'Wrong or no update parameter given'); |
|
638 | + } else { |
|
639 | + $permissions = (int)$permissions; |
|
640 | + $share->setPermissions($permissions); |
|
641 | + } |
|
642 | + } |
|
643 | + |
|
644 | + if ($permissions !== null && $share->getShareOwner() !== $this->currentUser->getUID()) { |
|
645 | + /* Check if this is an incomming share */ |
|
646 | + $incomingShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0); |
|
647 | + $incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0)); |
|
648 | + |
|
649 | + if (!empty($incomingShares)) { |
|
650 | + $maxPermissions = 0; |
|
651 | + foreach ($incomingShares as $incomingShare) { |
|
652 | + $maxPermissions |= $incomingShare->getPermissions(); |
|
653 | + } |
|
654 | + |
|
655 | + if ($share->getPermissions() & ~$maxPermissions) { |
|
656 | + return new \OC_OCS_Result(null, 404, 'Cannot increase permissions'); |
|
657 | + } |
|
658 | + } |
|
659 | + } |
|
660 | + |
|
661 | + |
|
662 | + try { |
|
663 | + $share = $this->shareManager->updateShare($share); |
|
664 | + } catch (\Exception $e) { |
|
665 | + return new \OC_OCS_Result(null, 400, $e->getMessage()); |
|
666 | + } |
|
667 | + |
|
668 | + return new \OC_OCS_Result($this->formatShare($share)); |
|
669 | + } |
|
670 | + |
|
671 | + /** |
|
672 | + * @param \OCP\Share\IShare $share |
|
673 | + * @param bool $checkGroups |
|
674 | + * @return bool |
|
675 | + */ |
|
676 | + protected function canAccessShare(\OCP\Share\IShare $share, $checkGroups = true) { |
|
677 | + // A file with permissions 0 can't be accessed by us. So Don't show it |
|
678 | + if ($share->getPermissions() === 0) { |
|
679 | + return false; |
|
680 | + } |
|
681 | + |
|
682 | + // Owner of the file and the sharer of the file can always get share |
|
683 | + if ($share->getShareOwner() === $this->currentUser->getUID() || |
|
684 | + $share->getSharedBy() === $this->currentUser->getUID() |
|
685 | + ) { |
|
686 | + return true; |
|
687 | + } |
|
688 | + |
|
689 | + // If the share is shared with you (or a group you are a member of) |
|
690 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
|
691 | + $share->getSharedWith() === $this->currentUser->getUID()) { |
|
692 | + return true; |
|
693 | + } |
|
694 | + |
|
695 | + if ($checkGroups && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
|
696 | + $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
697 | + if ($sharedWith->inGroup($this->currentUser)) { |
|
698 | + return true; |
|
699 | + } |
|
700 | + } |
|
701 | + |
|
702 | + return false; |
|
703 | + } |
|
704 | + |
|
705 | + /** |
|
706 | + * Make sure that the passed date is valid ISO 8601 |
|
707 | + * So YYYY-MM-DD |
|
708 | + * If not throw an exception |
|
709 | + * |
|
710 | + * @param string $expireDate |
|
711 | + * |
|
712 | + * @throws \Exception |
|
713 | + * @return \DateTime |
|
714 | + */ |
|
715 | + private function parseDate($expireDate) { |
|
716 | + try { |
|
717 | + $date = new \DateTime($expireDate); |
|
718 | + } catch (\Exception $e) { |
|
719 | + throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); |
|
720 | + } |
|
721 | + |
|
722 | + if ($date === false) { |
|
723 | + throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); |
|
724 | + } |
|
725 | + |
|
726 | + $date->setTime(0,0,0); |
|
727 | + |
|
728 | + return $date; |
|
729 | + } |
|
730 | 730 | } |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | } |
189 | 189 | |
190 | 190 | try { |
191 | - $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id); |
|
191 | + $share = $this->shareManager->getShareById('ocFederatedSharing:'.$id); |
|
192 | 192 | } catch (ShareNotFound $e) { |
193 | 193 | return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
194 | 194 | } |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | $share = null; |
222 | 222 | |
223 | 223 | try { |
224 | - $share = $this->shareManager->getShareById('ocinternal:' . $id); |
|
224 | + $share = $this->shareManager->getShareById('ocinternal:'.$id); |
|
225 | 225 | } catch (ShareNotFound $e) { |
226 | 226 | //Ignore for now |
227 | 227 | //return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | } |
235 | 235 | |
236 | 236 | try { |
237 | - $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id); |
|
237 | + $share = $this->shareManager->getShareById('ocFederatedSharing:'.$id); |
|
238 | 238 | } catch (ShareNotFound $e) { |
239 | 239 | return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
240 | 240 | } |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | if ($permissions === null) { |
280 | 280 | $permissions = \OCP\Constants::PERMISSION_ALL; |
281 | 281 | } else { |
282 | - $permissions = (int)$permissions; |
|
282 | + $permissions = (int) $permissions; |
|
283 | 283 | } |
284 | 284 | |
285 | 285 | if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) { |
@@ -305,7 +305,7 @@ discard block |
||
305 | 305 | } |
306 | 306 | |
307 | 307 | $shareWith = $this->request->getParam('shareWith', null); |
308 | - $shareType = (int)$this->request->getParam('shareType', '-1'); |
|
308 | + $shareType = (int) $this->request->getParam('shareType', '-1'); |
|
309 | 309 | |
310 | 310 | if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
311 | 311 | // Valid user is required to share |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | } catch (GenericShareException $e) { |
400 | 400 | $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
401 | 401 | return new \OC_OCS_Result(null, $code, $e->getHint()); |
402 | - }catch (\Exception $e) { |
|
402 | + } catch (\Exception $e) { |
|
403 | 403 | return new \OC_OCS_Result(null, 403, $e->getMessage()); |
404 | 404 | } |
405 | 405 | |
@@ -545,7 +545,7 @@ discard block |
||
545 | 545 | $share = null; |
546 | 546 | |
547 | 547 | try { |
548 | - $share = $this->shareManager->getShareById('ocinternal:' . $id); |
|
548 | + $share = $this->shareManager->getShareById('ocinternal:'.$id); |
|
549 | 549 | } catch (ShareNotFound $e) { |
550 | 550 | //Ignore for now |
551 | 551 | //return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
@@ -558,7 +558,7 @@ discard block |
||
558 | 558 | } |
559 | 559 | |
560 | 560 | try { |
561 | - $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id); |
|
561 | + $share = $this->shareManager->getShareById('ocFederatedSharing:'.$id); |
|
562 | 562 | } catch (ShareNotFound $e) { |
563 | 563 | return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); |
564 | 564 | } |
@@ -589,7 +589,7 @@ discard block |
||
589 | 589 | } |
590 | 590 | |
591 | 591 | if ($permissions !== null) { |
592 | - $newPermissions = (int)$permissions; |
|
592 | + $newPermissions = (int) $permissions; |
|
593 | 593 | } |
594 | 594 | |
595 | 595 | if ($newPermissions !== null && |
@@ -636,7 +636,7 @@ discard block |
||
636 | 636 | if ($permissions === null) { |
637 | 637 | return new \OC_OCS_Result(null, 400, 'Wrong or no update parameter given'); |
638 | 638 | } else { |
639 | - $permissions = (int)$permissions; |
|
639 | + $permissions = (int) $permissions; |
|
640 | 640 | $share->setPermissions($permissions); |
641 | 641 | } |
642 | 642 | } |
@@ -723,7 +723,7 @@ discard block |
||
723 | 723 | throw new \Exception('Invalid date. Format must be YYYY-MM-DD'); |
724 | 724 | } |
725 | 725 | |
726 | - $date->setTime(0,0,0); |
|
726 | + $date->setTime(0, 0, 0); |
|
727 | 727 | |
728 | 728 | return $date; |
729 | 729 | } |
@@ -399,7 +399,7 @@ |
||
399 | 399 | } catch (GenericShareException $e) { |
400 | 400 | $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
401 | 401 | return new \OC_OCS_Result(null, $code, $e->getHint()); |
402 | - }catch (\Exception $e) { |
|
402 | + } catch (\Exception $e) { |
|
403 | 403 | return new \OC_OCS_Result(null, 403, $e->getMessage()); |
404 | 404 | } |
405 | 405 |
@@ -325,6 +325,9 @@ |
||
325 | 325 | return $result; |
326 | 326 | } |
327 | 327 | |
328 | + /** |
|
329 | + * @param string $mountPoint |
|
330 | + */ |
|
328 | 331 | public function removeShare($mountPoint) { |
329 | 332 | $mountPoint = $this->stripPath($mountPoint); |
330 | 333 | $hash = md5($mountPoint); |
@@ -34,387 +34,387 @@ |
||
34 | 34 | use OCP\Notification\IManager; |
35 | 35 | |
36 | 36 | class Manager { |
37 | - const STORAGE = '\OCA\Files_Sharing\External\Storage'; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var string |
|
41 | - */ |
|
42 | - private $uid; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var \OCP\IDBConnection |
|
46 | - */ |
|
47 | - private $connection; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var \OC\Files\Mount\Manager |
|
51 | - */ |
|
52 | - private $mountManager; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var \OCP\Files\Storage\IStorageFactory |
|
56 | - */ |
|
57 | - private $storageLoader; |
|
58 | - |
|
59 | - /** |
|
60 | - * @var \OC\HTTPHelper |
|
61 | - */ |
|
62 | - private $httpHelper; |
|
63 | - |
|
64 | - /** |
|
65 | - * @var IManager |
|
66 | - */ |
|
67 | - private $notificationManager; |
|
68 | - /** @var DiscoveryManager */ |
|
69 | - private $discoveryManager; |
|
70 | - |
|
71 | - /** |
|
72 | - * @param \OCP\IDBConnection $connection |
|
73 | - * @param \OC\Files\Mount\Manager $mountManager |
|
74 | - * @param \OCP\Files\Storage\IStorageFactory $storageLoader |
|
75 | - * @param \OC\HTTPHelper $httpHelper |
|
76 | - * @param IManager $notificationManager |
|
77 | - * @param DiscoveryManager $discoveryManager |
|
78 | - * @param string $uid |
|
79 | - */ |
|
80 | - public function __construct(\OCP\IDBConnection $connection, |
|
81 | - \OC\Files\Mount\Manager $mountManager, |
|
82 | - \OCP\Files\Storage\IStorageFactory $storageLoader, |
|
83 | - \OC\HTTPHelper $httpHelper, |
|
84 | - IManager $notificationManager, |
|
85 | - DiscoveryManager $discoveryManager, |
|
86 | - $uid) { |
|
87 | - $this->connection = $connection; |
|
88 | - $this->mountManager = $mountManager; |
|
89 | - $this->storageLoader = $storageLoader; |
|
90 | - $this->httpHelper = $httpHelper; |
|
91 | - $this->uid = $uid; |
|
92 | - $this->notificationManager = $notificationManager; |
|
93 | - $this->discoveryManager = $discoveryManager; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * add new server-to-server share |
|
98 | - * |
|
99 | - * @param string $remote |
|
100 | - * @param string $token |
|
101 | - * @param string $password |
|
102 | - * @param string $name |
|
103 | - * @param string $owner |
|
104 | - * @param boolean $accepted |
|
105 | - * @param string $user |
|
106 | - * @param int $remoteId |
|
107 | - * @return Mount|null |
|
108 | - */ |
|
109 | - public function addShare($remote, $token, $password, $name, $owner, $accepted=false, $user = null, $remoteId = -1) { |
|
110 | - |
|
111 | - $user = $user ? $user : $this->uid; |
|
112 | - $accepted = $accepted ? 1 : 0; |
|
113 | - $name = Filesystem::normalizePath('/' . $name); |
|
114 | - |
|
115 | - if (!$accepted) { |
|
116 | - // To avoid conflicts with the mount point generation later, |
|
117 | - // we only use a temporary mount point name here. The real |
|
118 | - // mount point name will be generated when accepting the share, |
|
119 | - // using the original share item name. |
|
120 | - $tmpMountPointName = '{{TemporaryMountPointName#' . $name . '}}'; |
|
121 | - $mountPoint = $tmpMountPointName; |
|
122 | - $hash = md5($tmpMountPointName); |
|
123 | - $data = [ |
|
124 | - 'remote' => $remote, |
|
125 | - 'share_token' => $token, |
|
126 | - 'password' => $password, |
|
127 | - 'name' => $name, |
|
128 | - 'owner' => $owner, |
|
129 | - 'user' => $user, |
|
130 | - 'mountpoint' => $mountPoint, |
|
131 | - 'mountpoint_hash' => $hash, |
|
132 | - 'accepted' => $accepted, |
|
133 | - 'remote_id' => $remoteId, |
|
134 | - ]; |
|
135 | - |
|
136 | - $i = 1; |
|
137 | - while (!$this->connection->insertIfNotExist('*PREFIX*share_external', $data, ['user', 'mountpoint_hash'])) { |
|
138 | - // The external share already exists for the user |
|
139 | - $data['mountpoint'] = $tmpMountPointName . '-' . $i; |
|
140 | - $data['mountpoint_hash'] = md5($data['mountpoint']); |
|
141 | - $i++; |
|
142 | - } |
|
143 | - return null; |
|
144 | - } |
|
145 | - |
|
146 | - $mountPoint = Files::buildNotExistingFileName('/', $name); |
|
147 | - $mountPoint = Filesystem::normalizePath('/' . $mountPoint); |
|
148 | - $hash = md5($mountPoint); |
|
149 | - |
|
150 | - $query = $this->connection->prepare(' |
|
37 | + const STORAGE = '\OCA\Files_Sharing\External\Storage'; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var string |
|
41 | + */ |
|
42 | + private $uid; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var \OCP\IDBConnection |
|
46 | + */ |
|
47 | + private $connection; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var \OC\Files\Mount\Manager |
|
51 | + */ |
|
52 | + private $mountManager; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var \OCP\Files\Storage\IStorageFactory |
|
56 | + */ |
|
57 | + private $storageLoader; |
|
58 | + |
|
59 | + /** |
|
60 | + * @var \OC\HTTPHelper |
|
61 | + */ |
|
62 | + private $httpHelper; |
|
63 | + |
|
64 | + /** |
|
65 | + * @var IManager |
|
66 | + */ |
|
67 | + private $notificationManager; |
|
68 | + /** @var DiscoveryManager */ |
|
69 | + private $discoveryManager; |
|
70 | + |
|
71 | + /** |
|
72 | + * @param \OCP\IDBConnection $connection |
|
73 | + * @param \OC\Files\Mount\Manager $mountManager |
|
74 | + * @param \OCP\Files\Storage\IStorageFactory $storageLoader |
|
75 | + * @param \OC\HTTPHelper $httpHelper |
|
76 | + * @param IManager $notificationManager |
|
77 | + * @param DiscoveryManager $discoveryManager |
|
78 | + * @param string $uid |
|
79 | + */ |
|
80 | + public function __construct(\OCP\IDBConnection $connection, |
|
81 | + \OC\Files\Mount\Manager $mountManager, |
|
82 | + \OCP\Files\Storage\IStorageFactory $storageLoader, |
|
83 | + \OC\HTTPHelper $httpHelper, |
|
84 | + IManager $notificationManager, |
|
85 | + DiscoveryManager $discoveryManager, |
|
86 | + $uid) { |
|
87 | + $this->connection = $connection; |
|
88 | + $this->mountManager = $mountManager; |
|
89 | + $this->storageLoader = $storageLoader; |
|
90 | + $this->httpHelper = $httpHelper; |
|
91 | + $this->uid = $uid; |
|
92 | + $this->notificationManager = $notificationManager; |
|
93 | + $this->discoveryManager = $discoveryManager; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * add new server-to-server share |
|
98 | + * |
|
99 | + * @param string $remote |
|
100 | + * @param string $token |
|
101 | + * @param string $password |
|
102 | + * @param string $name |
|
103 | + * @param string $owner |
|
104 | + * @param boolean $accepted |
|
105 | + * @param string $user |
|
106 | + * @param int $remoteId |
|
107 | + * @return Mount|null |
|
108 | + */ |
|
109 | + public function addShare($remote, $token, $password, $name, $owner, $accepted=false, $user = null, $remoteId = -1) { |
|
110 | + |
|
111 | + $user = $user ? $user : $this->uid; |
|
112 | + $accepted = $accepted ? 1 : 0; |
|
113 | + $name = Filesystem::normalizePath('/' . $name); |
|
114 | + |
|
115 | + if (!$accepted) { |
|
116 | + // To avoid conflicts with the mount point generation later, |
|
117 | + // we only use a temporary mount point name here. The real |
|
118 | + // mount point name will be generated when accepting the share, |
|
119 | + // using the original share item name. |
|
120 | + $tmpMountPointName = '{{TemporaryMountPointName#' . $name . '}}'; |
|
121 | + $mountPoint = $tmpMountPointName; |
|
122 | + $hash = md5($tmpMountPointName); |
|
123 | + $data = [ |
|
124 | + 'remote' => $remote, |
|
125 | + 'share_token' => $token, |
|
126 | + 'password' => $password, |
|
127 | + 'name' => $name, |
|
128 | + 'owner' => $owner, |
|
129 | + 'user' => $user, |
|
130 | + 'mountpoint' => $mountPoint, |
|
131 | + 'mountpoint_hash' => $hash, |
|
132 | + 'accepted' => $accepted, |
|
133 | + 'remote_id' => $remoteId, |
|
134 | + ]; |
|
135 | + |
|
136 | + $i = 1; |
|
137 | + while (!$this->connection->insertIfNotExist('*PREFIX*share_external', $data, ['user', 'mountpoint_hash'])) { |
|
138 | + // The external share already exists for the user |
|
139 | + $data['mountpoint'] = $tmpMountPointName . '-' . $i; |
|
140 | + $data['mountpoint_hash'] = md5($data['mountpoint']); |
|
141 | + $i++; |
|
142 | + } |
|
143 | + return null; |
|
144 | + } |
|
145 | + |
|
146 | + $mountPoint = Files::buildNotExistingFileName('/', $name); |
|
147 | + $mountPoint = Filesystem::normalizePath('/' . $mountPoint); |
|
148 | + $hash = md5($mountPoint); |
|
149 | + |
|
150 | + $query = $this->connection->prepare(' |
|
151 | 151 | INSERT INTO `*PREFIX*share_external` |
152 | 152 | (`remote`, `share_token`, `password`, `name`, `owner`, `user`, `mountpoint`, `mountpoint_hash`, `accepted`, `remote_id`) |
153 | 153 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
154 | 154 | '); |
155 | - $query->execute(array($remote, $token, $password, $name, $owner, $user, $mountPoint, $hash, $accepted, $remoteId)); |
|
156 | - |
|
157 | - $options = array( |
|
158 | - 'remote' => $remote, |
|
159 | - 'token' => $token, |
|
160 | - 'password' => $password, |
|
161 | - 'mountpoint' => $mountPoint, |
|
162 | - 'owner' => $owner |
|
163 | - ); |
|
164 | - return $this->mountShare($options); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * get share |
|
169 | - * |
|
170 | - * @param int $id share id |
|
171 | - * @return mixed share of false |
|
172 | - */ |
|
173 | - public function getShare($id) { |
|
174 | - $getShare = $this->connection->prepare(' |
|
155 | + $query->execute(array($remote, $token, $password, $name, $owner, $user, $mountPoint, $hash, $accepted, $remoteId)); |
|
156 | + |
|
157 | + $options = array( |
|
158 | + 'remote' => $remote, |
|
159 | + 'token' => $token, |
|
160 | + 'password' => $password, |
|
161 | + 'mountpoint' => $mountPoint, |
|
162 | + 'owner' => $owner |
|
163 | + ); |
|
164 | + return $this->mountShare($options); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * get share |
|
169 | + * |
|
170 | + * @param int $id share id |
|
171 | + * @return mixed share of false |
|
172 | + */ |
|
173 | + public function getShare($id) { |
|
174 | + $getShare = $this->connection->prepare(' |
|
175 | 175 | SELECT `id`, `remote`, `remote_id`, `share_token`, `name`, `owner`, `user`, `mountpoint`, `accepted` |
176 | 176 | FROM `*PREFIX*share_external` |
177 | 177 | WHERE `id` = ? AND `user` = ?'); |
178 | - $result = $getShare->execute(array($id, $this->uid)); |
|
178 | + $result = $getShare->execute(array($id, $this->uid)); |
|
179 | 179 | |
180 | - return $result ? $getShare->fetch() : false; |
|
181 | - } |
|
180 | + return $result ? $getShare->fetch() : false; |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * accept server-to-server share |
|
185 | - * |
|
186 | - * @param int $id |
|
187 | - * @return bool True if the share could be accepted, false otherwise |
|
188 | - */ |
|
189 | - public function acceptShare($id) { |
|
183 | + /** |
|
184 | + * accept server-to-server share |
|
185 | + * |
|
186 | + * @param int $id |
|
187 | + * @return bool True if the share could be accepted, false otherwise |
|
188 | + */ |
|
189 | + public function acceptShare($id) { |
|
190 | 190 | |
191 | - $share = $this->getShare($id); |
|
191 | + $share = $this->getShare($id); |
|
192 | 192 | |
193 | - if ($share) { |
|
194 | - $mountPoint = Files::buildNotExistingFileName('/', $share['name']); |
|
195 | - $mountPoint = Filesystem::normalizePath('/' . $mountPoint); |
|
196 | - $hash = md5($mountPoint); |
|
193 | + if ($share) { |
|
194 | + $mountPoint = Files::buildNotExistingFileName('/', $share['name']); |
|
195 | + $mountPoint = Filesystem::normalizePath('/' . $mountPoint); |
|
196 | + $hash = md5($mountPoint); |
|
197 | 197 | |
198 | - $acceptShare = $this->connection->prepare(' |
|
198 | + $acceptShare = $this->connection->prepare(' |
|
199 | 199 | UPDATE `*PREFIX*share_external` |
200 | 200 | SET `accepted` = ?, |
201 | 201 | `mountpoint` = ?, |
202 | 202 | `mountpoint_hash` = ? |
203 | 203 | WHERE `id` = ? AND `user` = ?'); |
204 | - $acceptShare->execute(array(1, $mountPoint, $hash, $id, $this->uid)); |
|
205 | - $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'accept'); |
|
204 | + $acceptShare->execute(array(1, $mountPoint, $hash, $id, $this->uid)); |
|
205 | + $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'accept'); |
|
206 | 206 | |
207 | - \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $share['remote']]); |
|
207 | + \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $share['remote']]); |
|
208 | 208 | |
209 | - $this->processNotification($id); |
|
210 | - return true; |
|
211 | - } |
|
209 | + $this->processNotification($id); |
|
210 | + return true; |
|
211 | + } |
|
212 | 212 | |
213 | - return false; |
|
214 | - } |
|
213 | + return false; |
|
214 | + } |
|
215 | 215 | |
216 | - /** |
|
217 | - * decline server-to-server share |
|
218 | - * |
|
219 | - * @param int $id |
|
220 | - * @return bool True if the share could be declined, false otherwise |
|
221 | - */ |
|
222 | - public function declineShare($id) { |
|
216 | + /** |
|
217 | + * decline server-to-server share |
|
218 | + * |
|
219 | + * @param int $id |
|
220 | + * @return bool True if the share could be declined, false otherwise |
|
221 | + */ |
|
222 | + public function declineShare($id) { |
|
223 | 223 | |
224 | - $share = $this->getShare($id); |
|
224 | + $share = $this->getShare($id); |
|
225 | 225 | |
226 | - if ($share) { |
|
227 | - $removeShare = $this->connection->prepare(' |
|
226 | + if ($share) { |
|
227 | + $removeShare = $this->connection->prepare(' |
|
228 | 228 | DELETE FROM `*PREFIX*share_external` WHERE `id` = ? AND `user` = ?'); |
229 | - $removeShare->execute(array($id, $this->uid)); |
|
230 | - $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
|
231 | - |
|
232 | - $this->processNotification($id); |
|
233 | - return true; |
|
234 | - } |
|
235 | - |
|
236 | - return false; |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * @param int $remoteShare |
|
241 | - */ |
|
242 | - public function processNotification($remoteShare) { |
|
243 | - $filter = $this->notificationManager->createNotification(); |
|
244 | - $filter->setApp('files_sharing') |
|
245 | - ->setUser($this->uid) |
|
246 | - ->setObject('remote_share', (int) $remoteShare); |
|
247 | - $this->notificationManager->markProcessed($filter); |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * inform remote server whether server-to-server share was accepted/declined |
|
252 | - * |
|
253 | - * @param string $remote |
|
254 | - * @param string $token |
|
255 | - * @param int $remoteId Share id on the remote host |
|
256 | - * @param string $feedback |
|
257 | - * @return boolean |
|
258 | - */ |
|
259 | - private function sendFeedbackToRemote($remote, $token, $remoteId, $feedback) { |
|
260 | - |
|
261 | - $url = rtrim($remote, '/') . $this->discoveryManager->getShareEndpoint($remote) . '/' . $remoteId . '/' . $feedback . '?format=' . \OCP\Share::RESPONSE_FORMAT; |
|
262 | - $fields = array('token' => $token); |
|
263 | - |
|
264 | - $result = $this->httpHelper->post($url, $fields); |
|
265 | - $status = json_decode($result['result'], true); |
|
266 | - |
|
267 | - return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * remove '/user/files' from the path and trailing slashes |
|
272 | - * |
|
273 | - * @param string $path |
|
274 | - * @return string |
|
275 | - */ |
|
276 | - protected function stripPath($path) { |
|
277 | - $prefix = '/' . $this->uid . '/files'; |
|
278 | - return rtrim(substr($path, strlen($prefix)), '/'); |
|
279 | - } |
|
280 | - |
|
281 | - public function getMount($data) { |
|
282 | - $data['manager'] = $this; |
|
283 | - $mountPoint = '/' . $this->uid . '/files' . $data['mountpoint']; |
|
284 | - $data['mountpoint'] = $mountPoint; |
|
285 | - $data['certificateManager'] = \OC::$server->getCertificateManager($this->uid); |
|
286 | - return new Mount(self::STORAGE, $mountPoint, $data, $this, $this->storageLoader); |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * @param array $data |
|
291 | - * @return Mount |
|
292 | - */ |
|
293 | - protected function mountShare($data) { |
|
294 | - $mount = $this->getMount($data); |
|
295 | - $this->mountManager->addMount($mount); |
|
296 | - return $mount; |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * @return \OC\Files\Mount\Manager |
|
301 | - */ |
|
302 | - public function getMountManager() { |
|
303 | - return $this->mountManager; |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * @param string $source |
|
308 | - * @param string $target |
|
309 | - * @return bool |
|
310 | - */ |
|
311 | - public function setMountPoint($source, $target) { |
|
312 | - $source = $this->stripPath($source); |
|
313 | - $target = $this->stripPath($target); |
|
314 | - $sourceHash = md5($source); |
|
315 | - $targetHash = md5($target); |
|
316 | - |
|
317 | - $query = $this->connection->prepare(' |
|
229 | + $removeShare->execute(array($id, $this->uid)); |
|
230 | + $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
|
231 | + |
|
232 | + $this->processNotification($id); |
|
233 | + return true; |
|
234 | + } |
|
235 | + |
|
236 | + return false; |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * @param int $remoteShare |
|
241 | + */ |
|
242 | + public function processNotification($remoteShare) { |
|
243 | + $filter = $this->notificationManager->createNotification(); |
|
244 | + $filter->setApp('files_sharing') |
|
245 | + ->setUser($this->uid) |
|
246 | + ->setObject('remote_share', (int) $remoteShare); |
|
247 | + $this->notificationManager->markProcessed($filter); |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * inform remote server whether server-to-server share was accepted/declined |
|
252 | + * |
|
253 | + * @param string $remote |
|
254 | + * @param string $token |
|
255 | + * @param int $remoteId Share id on the remote host |
|
256 | + * @param string $feedback |
|
257 | + * @return boolean |
|
258 | + */ |
|
259 | + private function sendFeedbackToRemote($remote, $token, $remoteId, $feedback) { |
|
260 | + |
|
261 | + $url = rtrim($remote, '/') . $this->discoveryManager->getShareEndpoint($remote) . '/' . $remoteId . '/' . $feedback . '?format=' . \OCP\Share::RESPONSE_FORMAT; |
|
262 | + $fields = array('token' => $token); |
|
263 | + |
|
264 | + $result = $this->httpHelper->post($url, $fields); |
|
265 | + $status = json_decode($result['result'], true); |
|
266 | + |
|
267 | + return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * remove '/user/files' from the path and trailing slashes |
|
272 | + * |
|
273 | + * @param string $path |
|
274 | + * @return string |
|
275 | + */ |
|
276 | + protected function stripPath($path) { |
|
277 | + $prefix = '/' . $this->uid . '/files'; |
|
278 | + return rtrim(substr($path, strlen($prefix)), '/'); |
|
279 | + } |
|
280 | + |
|
281 | + public function getMount($data) { |
|
282 | + $data['manager'] = $this; |
|
283 | + $mountPoint = '/' . $this->uid . '/files' . $data['mountpoint']; |
|
284 | + $data['mountpoint'] = $mountPoint; |
|
285 | + $data['certificateManager'] = \OC::$server->getCertificateManager($this->uid); |
|
286 | + return new Mount(self::STORAGE, $mountPoint, $data, $this, $this->storageLoader); |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * @param array $data |
|
291 | + * @return Mount |
|
292 | + */ |
|
293 | + protected function mountShare($data) { |
|
294 | + $mount = $this->getMount($data); |
|
295 | + $this->mountManager->addMount($mount); |
|
296 | + return $mount; |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * @return \OC\Files\Mount\Manager |
|
301 | + */ |
|
302 | + public function getMountManager() { |
|
303 | + return $this->mountManager; |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * @param string $source |
|
308 | + * @param string $target |
|
309 | + * @return bool |
|
310 | + */ |
|
311 | + public function setMountPoint($source, $target) { |
|
312 | + $source = $this->stripPath($source); |
|
313 | + $target = $this->stripPath($target); |
|
314 | + $sourceHash = md5($source); |
|
315 | + $targetHash = md5($target); |
|
316 | + |
|
317 | + $query = $this->connection->prepare(' |
|
318 | 318 | UPDATE `*PREFIX*share_external` |
319 | 319 | SET `mountpoint` = ?, `mountpoint_hash` = ? |
320 | 320 | WHERE `mountpoint_hash` = ? |
321 | 321 | AND `user` = ? |
322 | 322 | '); |
323 | - $result = (bool)$query->execute(array($target, $targetHash, $sourceHash, $this->uid)); |
|
323 | + $result = (bool)$query->execute(array($target, $targetHash, $sourceHash, $this->uid)); |
|
324 | 324 | |
325 | - return $result; |
|
326 | - } |
|
325 | + return $result; |
|
326 | + } |
|
327 | 327 | |
328 | - public function removeShare($mountPoint) { |
|
329 | - $mountPoint = $this->stripPath($mountPoint); |
|
330 | - $hash = md5($mountPoint); |
|
328 | + public function removeShare($mountPoint) { |
|
329 | + $mountPoint = $this->stripPath($mountPoint); |
|
330 | + $hash = md5($mountPoint); |
|
331 | 331 | |
332 | - $getShare = $this->connection->prepare(' |
|
332 | + $getShare = $this->connection->prepare(' |
|
333 | 333 | SELECT `remote`, `share_token`, `remote_id` |
334 | 334 | FROM `*PREFIX*share_external` |
335 | 335 | WHERE `mountpoint_hash` = ? AND `user` = ?'); |
336 | - $result = $getShare->execute(array($hash, $this->uid)); |
|
336 | + $result = $getShare->execute(array($hash, $this->uid)); |
|
337 | 337 | |
338 | - if ($result) { |
|
339 | - $share = $getShare->fetch(); |
|
340 | - $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
|
341 | - } |
|
338 | + if ($result) { |
|
339 | + $share = $getShare->fetch(); |
|
340 | + $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
|
341 | + } |
|
342 | 342 | |
343 | - $query = $this->connection->prepare(' |
|
343 | + $query = $this->connection->prepare(' |
|
344 | 344 | DELETE FROM `*PREFIX*share_external` |
345 | 345 | WHERE `mountpoint_hash` = ? |
346 | 346 | AND `user` = ? |
347 | 347 | '); |
348 | - return (bool)$query->execute(array($hash, $this->uid)); |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * remove all shares for user $uid if the user was deleted |
|
353 | - * |
|
354 | - * @param string $uid |
|
355 | - * @return bool |
|
356 | - */ |
|
357 | - public function removeUserShares($uid) { |
|
358 | - $getShare = $this->connection->prepare(' |
|
348 | + return (bool)$query->execute(array($hash, $this->uid)); |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * remove all shares for user $uid if the user was deleted |
|
353 | + * |
|
354 | + * @param string $uid |
|
355 | + * @return bool |
|
356 | + */ |
|
357 | + public function removeUserShares($uid) { |
|
358 | + $getShare = $this->connection->prepare(' |
|
359 | 359 | SELECT `remote`, `share_token`, `remote_id` |
360 | 360 | FROM `*PREFIX*share_external` |
361 | 361 | WHERE `user` = ?'); |
362 | - $result = $getShare->execute(array($uid)); |
|
362 | + $result = $getShare->execute(array($uid)); |
|
363 | 363 | |
364 | - if ($result) { |
|
365 | - $shares = $getShare->fetchAll(); |
|
366 | - foreach($shares as $share) { |
|
367 | - $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
|
368 | - } |
|
369 | - } |
|
364 | + if ($result) { |
|
365 | + $shares = $getShare->fetchAll(); |
|
366 | + foreach($shares as $share) { |
|
367 | + $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
|
368 | + } |
|
369 | + } |
|
370 | 370 | |
371 | - $query = $this->connection->prepare(' |
|
371 | + $query = $this->connection->prepare(' |
|
372 | 372 | DELETE FROM `*PREFIX*share_external` |
373 | 373 | WHERE `user` = ? |
374 | 374 | '); |
375 | - return (bool)$query->execute(array($uid)); |
|
376 | - } |
|
377 | - |
|
378 | - /** |
|
379 | - * return a list of shares which are not yet accepted by the user |
|
380 | - * |
|
381 | - * @return array list of open server-to-server shares |
|
382 | - */ |
|
383 | - public function getOpenShares() { |
|
384 | - return $this->getShares(false); |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * return a list of shares wich are accepted by the user |
|
389 | - * |
|
390 | - * @return array list of accepted server-to-server shares |
|
391 | - */ |
|
392 | - public function getAcceptedShares() { |
|
393 | - return $this->getShares(true); |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * return a list of shares for the user |
|
398 | - * |
|
399 | - * @param bool|null $accepted True for accepted only, |
|
400 | - * false for not accepted, |
|
401 | - * null for all shares of the user |
|
402 | - * @return array list of open server-to-server shares |
|
403 | - */ |
|
404 | - private function getShares($accepted) { |
|
405 | - $query = 'SELECT `id`, `remote`, `remote_id`, `share_token`, `name`, `owner`, `user`, `mountpoint`, `accepted` |
|
375 | + return (bool)$query->execute(array($uid)); |
|
376 | + } |
|
377 | + |
|
378 | + /** |
|
379 | + * return a list of shares which are not yet accepted by the user |
|
380 | + * |
|
381 | + * @return array list of open server-to-server shares |
|
382 | + */ |
|
383 | + public function getOpenShares() { |
|
384 | + return $this->getShares(false); |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * return a list of shares wich are accepted by the user |
|
389 | + * |
|
390 | + * @return array list of accepted server-to-server shares |
|
391 | + */ |
|
392 | + public function getAcceptedShares() { |
|
393 | + return $this->getShares(true); |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * return a list of shares for the user |
|
398 | + * |
|
399 | + * @param bool|null $accepted True for accepted only, |
|
400 | + * false for not accepted, |
|
401 | + * null for all shares of the user |
|
402 | + * @return array list of open server-to-server shares |
|
403 | + */ |
|
404 | + private function getShares($accepted) { |
|
405 | + $query = 'SELECT `id`, `remote`, `remote_id`, `share_token`, `name`, `owner`, `user`, `mountpoint`, `accepted` |
|
406 | 406 | FROM `*PREFIX*share_external` |
407 | 407 | WHERE `user` = ?'; |
408 | - $parameters = [$this->uid]; |
|
409 | - if (!is_null($accepted)) { |
|
410 | - $query .= ' AND `accepted` = ?'; |
|
411 | - $parameters[] = (int) $accepted; |
|
412 | - } |
|
413 | - $query .= ' ORDER BY `id` ASC'; |
|
414 | - |
|
415 | - $shares = $this->connection->prepare($query); |
|
416 | - $result = $shares->execute($parameters); |
|
417 | - |
|
418 | - return $result ? $shares->fetchAll() : []; |
|
419 | - } |
|
408 | + $parameters = [$this->uid]; |
|
409 | + if (!is_null($accepted)) { |
|
410 | + $query .= ' AND `accepted` = ?'; |
|
411 | + $parameters[] = (int) $accepted; |
|
412 | + } |
|
413 | + $query .= ' ORDER BY `id` ASC'; |
|
414 | + |
|
415 | + $shares = $this->connection->prepare($query); |
|
416 | + $result = $shares->execute($parameters); |
|
417 | + |
|
418 | + return $result ? $shares->fetchAll() : []; |
|
419 | + } |
|
420 | 420 | } |
@@ -106,18 +106,18 @@ discard block |
||
106 | 106 | * @param int $remoteId |
107 | 107 | * @return Mount|null |
108 | 108 | */ |
109 | - public function addShare($remote, $token, $password, $name, $owner, $accepted=false, $user = null, $remoteId = -1) { |
|
109 | + public function addShare($remote, $token, $password, $name, $owner, $accepted = false, $user = null, $remoteId = -1) { |
|
110 | 110 | |
111 | 111 | $user = $user ? $user : $this->uid; |
112 | 112 | $accepted = $accepted ? 1 : 0; |
113 | - $name = Filesystem::normalizePath('/' . $name); |
|
113 | + $name = Filesystem::normalizePath('/'.$name); |
|
114 | 114 | |
115 | 115 | if (!$accepted) { |
116 | 116 | // To avoid conflicts with the mount point generation later, |
117 | 117 | // we only use a temporary mount point name here. The real |
118 | 118 | // mount point name will be generated when accepting the share, |
119 | 119 | // using the original share item name. |
120 | - $tmpMountPointName = '{{TemporaryMountPointName#' . $name . '}}'; |
|
120 | + $tmpMountPointName = '{{TemporaryMountPointName#'.$name.'}}'; |
|
121 | 121 | $mountPoint = $tmpMountPointName; |
122 | 122 | $hash = md5($tmpMountPointName); |
123 | 123 | $data = [ |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | $i = 1; |
137 | 137 | while (!$this->connection->insertIfNotExist('*PREFIX*share_external', $data, ['user', 'mountpoint_hash'])) { |
138 | 138 | // The external share already exists for the user |
139 | - $data['mountpoint'] = $tmpMountPointName . '-' . $i; |
|
139 | + $data['mountpoint'] = $tmpMountPointName.'-'.$i; |
|
140 | 140 | $data['mountpoint_hash'] = md5($data['mountpoint']); |
141 | 141 | $i++; |
142 | 142 | } |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | } |
145 | 145 | |
146 | 146 | $mountPoint = Files::buildNotExistingFileName('/', $name); |
147 | - $mountPoint = Filesystem::normalizePath('/' . $mountPoint); |
|
147 | + $mountPoint = Filesystem::normalizePath('/'.$mountPoint); |
|
148 | 148 | $hash = md5($mountPoint); |
149 | 149 | |
150 | 150 | $query = $this->connection->prepare(' |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | |
193 | 193 | if ($share) { |
194 | 194 | $mountPoint = Files::buildNotExistingFileName('/', $share['name']); |
195 | - $mountPoint = Filesystem::normalizePath('/' . $mountPoint); |
|
195 | + $mountPoint = Filesystem::normalizePath('/'.$mountPoint); |
|
196 | 196 | $hash = md5($mountPoint); |
197 | 197 | |
198 | 198 | $acceptShare = $this->connection->prepare(' |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | */ |
259 | 259 | private function sendFeedbackToRemote($remote, $token, $remoteId, $feedback) { |
260 | 260 | |
261 | - $url = rtrim($remote, '/') . $this->discoveryManager->getShareEndpoint($remote) . '/' . $remoteId . '/' . $feedback . '?format=' . \OCP\Share::RESPONSE_FORMAT; |
|
261 | + $url = rtrim($remote, '/').$this->discoveryManager->getShareEndpoint($remote).'/'.$remoteId.'/'.$feedback.'?format='.\OCP\Share::RESPONSE_FORMAT; |
|
262 | 262 | $fields = array('token' => $token); |
263 | 263 | |
264 | 264 | $result = $this->httpHelper->post($url, $fields); |
@@ -274,13 +274,13 @@ discard block |
||
274 | 274 | * @return string |
275 | 275 | */ |
276 | 276 | protected function stripPath($path) { |
277 | - $prefix = '/' . $this->uid . '/files'; |
|
277 | + $prefix = '/'.$this->uid.'/files'; |
|
278 | 278 | return rtrim(substr($path, strlen($prefix)), '/'); |
279 | 279 | } |
280 | 280 | |
281 | 281 | public function getMount($data) { |
282 | 282 | $data['manager'] = $this; |
283 | - $mountPoint = '/' . $this->uid . '/files' . $data['mountpoint']; |
|
283 | + $mountPoint = '/'.$this->uid.'/files'.$data['mountpoint']; |
|
284 | 284 | $data['mountpoint'] = $mountPoint; |
285 | 285 | $data['certificateManager'] = \OC::$server->getCertificateManager($this->uid); |
286 | 286 | return new Mount(self::STORAGE, $mountPoint, $data, $this, $this->storageLoader); |
@@ -320,7 +320,7 @@ discard block |
||
320 | 320 | WHERE `mountpoint_hash` = ? |
321 | 321 | AND `user` = ? |
322 | 322 | '); |
323 | - $result = (bool)$query->execute(array($target, $targetHash, $sourceHash, $this->uid)); |
|
323 | + $result = (bool) $query->execute(array($target, $targetHash, $sourceHash, $this->uid)); |
|
324 | 324 | |
325 | 325 | return $result; |
326 | 326 | } |
@@ -345,7 +345,7 @@ discard block |
||
345 | 345 | WHERE `mountpoint_hash` = ? |
346 | 346 | AND `user` = ? |
347 | 347 | '); |
348 | - return (bool)$query->execute(array($hash, $this->uid)); |
|
348 | + return (bool) $query->execute(array($hash, $this->uid)); |
|
349 | 349 | } |
350 | 350 | |
351 | 351 | /** |
@@ -363,7 +363,7 @@ discard block |
||
363 | 363 | |
364 | 364 | if ($result) { |
365 | 365 | $shares = $getShare->fetchAll(); |
366 | - foreach($shares as $share) { |
|
366 | + foreach ($shares as $share) { |
|
367 | 367 | $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
368 | 368 | } |
369 | 369 | } |
@@ -372,7 +372,7 @@ discard block |
||
372 | 372 | DELETE FROM `*PREFIX*share_external` |
373 | 373 | WHERE `user` = ? |
374 | 374 | '); |
375 | - return (bool)$query->execute(array($uid)); |
|
375 | + return (bool) $query->execute(array($uid)); |
|
376 | 376 | } |
377 | 377 | |
378 | 378 | /** |