Completed
Pull Request — master (#315)
by Joas
04:21 queued 02:19
created

lib/Controller/RemoteActivity.php (12 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Activity\Controller;
23
24
use OCA\Activity\Extension\Files;
25
use OCP\App\IAppManager;
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\Http\DataResponse;
28
use OCP\AppFramework\OCSController;
29
use OCP\Files\InvalidPathException;
30
use OCP\Files\IRootFolder;
31
use OCP\Files\NotFoundException;
32
use OCP\IDBConnection;
33
use OCP\IRequest;
34
use OCP\Activity\IManager as IActivityManager;
35
use OCP\IUser;
36
use OCP\IUserManager;
37
38
class RemoteActivity extends OCSController {
39
40
	/** @var IDBConnection */
41
	protected $db;
42
43
	/** @var IUserManager */
44
	protected $userManager;
45
46
	/** @var IAppManager */
47
	protected $appManager;
48
49
	/** @var IRootFolder */
50
	protected $rootFolder;
51
52
	/** @var IActivityManager */
53
	protected $activityManager;
54
55
	public function __construct($appName,
56
								IRequest $request,
57
								IDBConnection $db,
58
								IUserManager $userManager,
59
								IAppManager $appManager,
60
								IRootFolder $rootFolder,
61
								IActivityManager $activityManager) {
62
		parent::__construct($appName, $request);
63
		$this->db = $db;
64
		$this->userManager = $userManager;
65
		$this->appManager = $appManager;
66
		$this->rootFolder = $rootFolder;
67
		$this->activityManager = $activityManager;
68
	}
69
70
	/**
71
	 * @PublicPage
72
	 * @NoCSRFRequired
73
	 *
74
	 * @param string $token
75
	 * @param string[] $to
76
	 * @param string[] $actor
77
	 * @param string $type
78
	 * @param string $updated
79
	 * @param string[] $object
80
	 * @param string[] $target
81
	 * @param string[] $origin
82
	 * @return DataResponse
83
	 */
84
	public function receiveActivity($token, array $to, array $actor, $type, $updated, array $object = [], array $target = [], array $origin = []) {
85
86
		$date = \DateTime::createFromFormat(\DateTime::W3C, $updated);
87
		if ($date === false) {
88
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
89
		}
90
		$time = $date->getTimestamp();
91
92
		\OC::$server->getLogger()->warning(json_encode(func_get_args()));
93 View Code Duplication
		if (!isset($to['type'], $to['name']) || $to['type'] !== 'Person') {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
95
		}
96
97
		$user = $this->userManager->get($to['name']);
98
		if (!$user instanceof IUser) {
0 ignored issues
show
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
99
			return new DataResponse([], Http::STATUS_NOT_FOUND);
100
		}
101
102 View Code Duplication
		if (!isset($actor['type'], $actor['name']) || $actor['type'] !== 'Person') {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
104
		}
105
106
		if ($user->getCloudId() === $actor['name']) {
107
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
108
		}
109
110
		if (!$this->appManager->isInstalled('federatedfilesharing')) {
111
			return new DataResponse([], Http::STATUS_NOT_FOUND);
112
		}
113
114
		$query = $this->db->getQueryBuilder();
115
		$query->select('*')
116
			->from('share_external')
117
			->where($query->expr()->eq('share_token', $query->createNamedParameter($token)))
118
			->andWhere($query->expr()->eq('user', $query->createNamedParameter($user->getUID())));
119
120
		$result = $query->execute();
121
		$share = $result->fetch();
122
		$result->closeCursor();
123
124
		if (!is_array($share) || strpos($share['mountpoint'], '{{TemporaryMountPointName#') === 0) {
125
			return new DataResponse([], Http::STATUS_NOT_FOUND);
126
		}
127
128
		$internalType = $this->translateType($type);
129
		if ($internalType === '') {
130
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
131
		}
132
133
		$path2 = null;
134
		if ($type === 'Move') {
135 View Code Duplication
			if (!isset($target['type'], $target['name']) || $target['type'] !== 'Document') {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
				return new DataResponse([], Http::STATUS_BAD_REQUEST);
137
			}
138
139 View Code Duplication
			if (!isset($origin['type'], $origin['name']) || $origin['type'] !== 'Document') {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
				return new DataResponse([], Http::STATUS_BAD_REQUEST);
141
			}
142
143
			$path = $share['mountpoint'] . $target['name'];
144
			$path2 = $share['mountpoint'] . $origin['name'];
145
		} else {
146 View Code Duplication
			if (!isset($object['type'], $object['name']) || $object['type'] !== 'Document') {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
				return new DataResponse([], Http::STATUS_BAD_REQUEST);
148
			}
149
150
			$path = $share['mountpoint'] . $object['name'];
151
		}
152
153
		$subject = $this->getSubject($type, $path, $path2);
154
		if ($subject === '') {
155
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
156
		}
157
158
		$userFolder = $this->rootFolder->getUserFolder($user->getUID());
159
		try {
160
			$node = $userFolder->get($path);
161
			$fileId = $node->getId();
162
		} catch (NotFoundException $e) {
0 ignored issues
show
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
163
			return new DataResponse([], Http::STATUS_NOT_FOUND);
164
		} catch (InvalidPathException $e) {
0 ignored issues
show
The class OCP\Files\InvalidPathException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
165
			return new DataResponse([], Http::STATUS_NOT_FOUND);
166
		}
167
168
		if ($path2 !== null) {
169
			$secondPath = [$fileId => $path2];
170
			if ($subject === 'moved_by') {
171
				try {
172
					$parent = $node->getParent();
173
					$secondPath = [$parent->getId() => dirname($path2)];
174
				} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
175
				} catch (InvalidPathException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
The class OCP\Files\InvalidPathException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
176
				}
177
			}
178
			$subjectParams = [$secondPath, $actor['name'], [$fileId => $path]];
179
		} else {
180
			$subjectParams = [[$fileId => $path], $actor['name']];
181
		}
182
183
		$event = $this->activityManager->generateEvent();
184
		try {
185
			$event->setAffectedUser($user->getUID())
186
				->setApp('files')
187
				->setType($internalType)
188
				->setAuthor($actor['name'])
189
				->setObject('files', $fileId, $path)
190
				->setSubject($subject, $subjectParams)
191
				->setTimestamp($time);
192
			$this->activityManager->publish($event);
193
		} catch (\InvalidArgumentException $e) {
194
			return new DataResponse(['activity'], Http::STATUS_BAD_REQUEST);
195
		} catch (\BadMethodCallException $e) {
196
			return new DataResponse(['sending'], Http::STATUS_BAD_REQUEST);
197
		}
198
199
		return new DataResponse();
200
	}
201
202
	protected function getSubject($type, $path, $path2) {
203
		switch ($type) {
204
			case 'Create':
205
				return 'created_by';
206
			case 'Move':
207
				if ($path2 === null) {
208
					return '';
209
				}
210
				if (basename($path) === basename($path2)) {
211
					return 'moved_by';
212
				}
213
				return 'renamed_by';
214
			case 'Update':
215
				return 'changed_by';
216
			case 'Delete':
217
				return 'deleted_by';
218
		}
219
		return '';
220
	}
221
222
	/**
223
	 * @param string $type
224
	 * @return string
225
	 */
226
	protected function translateType($type) {
227
		switch ($type) {
228
			case 'Create':
229
				return Files::TYPE_SHARE_CREATED;
230
			case 'Move':
231
			case 'Update':
232
				return Files::TYPE_SHARE_CHANGED;
233
			case 'Delete':
234
				return Files::TYPE_SHARE_DELETED;
235
		}
236
		return '';
237
	}
238
}
239