|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
|
4
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
5
|
|
|
* @author Lukas Reschke <[email protected]> |
|
6
|
|
|
* @author Morris Jobke <[email protected]> |
|
7
|
|
|
* @author Robin Appelman <[email protected]> |
|
8
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
9
|
|
|
* @author Thomas Müller <[email protected]> |
|
10
|
|
|
* @author Vincent Petry <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
|
13
|
|
|
* @license AGPL-3.0 |
|
14
|
|
|
* |
|
15
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
16
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
17
|
|
|
* as published by the Free Software Foundation. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU Affero General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
namespace OC; |
|
30
|
|
|
|
|
31
|
|
|
use OCP\Files\Folder; |
|
32
|
|
|
use OCP\Files\NotFoundException; |
|
33
|
|
|
use OCP\Files\Storage\IStorage; |
|
34
|
|
|
use OCP\IAvatarManager; |
|
35
|
|
|
use OCP\ILogger; |
|
36
|
|
|
use OCP\IUserManager; |
|
37
|
|
|
use OCP\Files\IRootFolder; |
|
38
|
|
|
use OCP\IL10N; |
|
39
|
|
|
use OCP\IUser; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* This class implements methods to access Avatar functionality |
|
43
|
|
|
*/ |
|
44
|
|
|
class AvatarManager implements IAvatarManager { |
|
45
|
|
|
|
|
46
|
|
|
/** @var IUserManager */ |
|
47
|
|
|
private $userManager; |
|
48
|
|
|
|
|
49
|
|
|
/** @var IRootFolder */ |
|
50
|
|
|
private $rootFolder; |
|
51
|
|
|
|
|
52
|
|
|
/** @var IStorage */ |
|
53
|
|
|
private $storage; |
|
54
|
|
|
|
|
55
|
|
|
/** @var IL10N */ |
|
56
|
|
|
private $l; |
|
57
|
|
|
|
|
58
|
|
|
/** @var ILogger */ |
|
59
|
|
|
private $logger; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* AvatarManager constructor. |
|
63
|
|
|
* |
|
64
|
|
|
* @param IUserManager $userManager |
|
65
|
|
|
* @param IRootFolder $rootFolder |
|
66
|
|
|
* @param IL10N $l |
|
67
|
|
|
* @param ILogger $logger |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct( |
|
70
|
|
|
IUserManager $userManager, |
|
71
|
|
|
IRootFolder $rootFolder, |
|
72
|
|
|
IL10N $l, |
|
73
|
|
|
ILogger $logger) { |
|
74
|
|
|
$this->userManager = $userManager; |
|
75
|
|
|
$this->rootFolder = $rootFolder; |
|
76
|
|
|
$this->l = $l; |
|
77
|
|
|
$this->logger = $logger; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* return a user specific instance of \OCP\IAvatar |
|
82
|
|
|
* @see \OCP\IAvatar |
|
83
|
|
|
* @param string $userId the ownCloud user id |
|
84
|
|
|
* @return \OCP\IAvatar |
|
85
|
|
|
* @throws \Exception In case the username is potentially dangerous |
|
86
|
|
|
* @throws NotFoundException In case there is no user folder yet |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getAvatar($userId) { |
|
89
|
|
|
$user = $this->userManager->get($userId); |
|
90
|
|
|
if ($user === null) { |
|
91
|
|
|
throw new \Exception('user does not exist'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$avatarsStorage = $this->getAvatarStorage(); |
|
95
|
|
|
return new Avatar($avatarsStorage, $this->l, $user, $this->logger); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns the avatar folder for the given user |
|
100
|
|
|
* |
|
101
|
|
|
* @return \OCP\Files\Storage\IStorage |
|
102
|
|
|
* |
|
103
|
|
|
* @throws NotFoundException |
|
104
|
|
|
* @throws \OCP\Files\NotPermittedException |
|
105
|
|
|
*/ |
|
106
|
|
|
private function getAvatarStorage() { |
|
107
|
|
|
if (!$this->storage) { |
|
108
|
|
|
try { |
|
109
|
|
|
$this->storage = $this->rootFolder->get('avatars')->getStorage(); |
|
110
|
|
|
} catch (NotFoundException $e) { |
|
111
|
|
|
$this->storage = $this->rootFolder->newFolder('avatars')->getStorage(); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
return $this->storage; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.