1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
5
|
|
|
* |
6
|
|
|
* @author Arthur Schiwon <[email protected]> |
7
|
|
|
* @author Lukas Reschke <[email protected]> |
8
|
|
|
* @author Morris Jobke <[email protected]> |
9
|
|
|
* @author Robin Appelman <[email protected]> |
10
|
|
|
* @author Roeland Jago Douma <[email protected]> |
11
|
|
|
* @author Thomas Müller <[email protected]> |
12
|
|
|
* |
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\Avatar; |
30
|
|
|
|
31
|
|
|
use OC\User\Manager; |
32
|
|
|
use OCP\Files\IAppData; |
33
|
|
|
use OCP\Files\NotFoundException; |
34
|
|
|
use OCP\IAvatar; |
35
|
|
|
use OCP\IAvatarManager; |
36
|
|
|
use OCP\IConfig; |
37
|
|
|
use OCP\ILogger; |
38
|
|
|
use OCP\IL10N; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This class implements methods to access Avatar functionality |
42
|
|
|
*/ |
43
|
|
|
class AvatarManager implements IAvatarManager { |
44
|
|
|
|
45
|
|
|
/** @var Manager */ |
46
|
|
|
private $userManager; |
47
|
|
|
|
48
|
|
|
/** @var IAppData */ |
49
|
|
|
private $appData; |
50
|
|
|
|
51
|
|
|
/** @var IL10N */ |
52
|
|
|
private $l; |
53
|
|
|
|
54
|
|
|
/** @var ILogger */ |
55
|
|
|
private $logger; |
56
|
|
|
|
57
|
|
|
/** @var IConfig */ |
58
|
|
|
private $config; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* AvatarManager constructor. |
62
|
|
|
* |
63
|
|
|
* @param Manager $userManager |
64
|
|
|
* @param IAppData $appData |
65
|
|
|
* @param IL10N $l |
66
|
|
|
* @param ILogger $logger |
67
|
|
|
* @param IConfig $config |
68
|
|
|
*/ |
69
|
|
|
public function __construct( |
70
|
|
|
Manager $userManager, |
71
|
|
|
IAppData $appData, |
72
|
|
|
IL10N $l, |
73
|
|
|
ILogger $logger, |
74
|
|
|
IConfig $config) { |
75
|
|
|
$this->userManager = $userManager; |
76
|
|
|
$this->appData = $appData; |
77
|
|
|
$this->l = $l; |
78
|
|
|
$this->logger = $logger; |
79
|
|
|
$this->config = $config; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* return a user specific instance of \OCP\IAvatar |
84
|
|
|
* @see \OCP\IAvatar |
85
|
|
|
* @param string $userId the ownCloud user id |
86
|
|
|
* @return \OCP\IAvatar |
87
|
|
|
* @throws \Exception In case the username is potentially dangerous |
88
|
|
|
* @throws NotFoundException In case there is no user folder yet |
89
|
|
|
*/ |
90
|
|
|
public function getAvatar(string $userId) : IAvatar { |
91
|
|
|
$user = $this->userManager->get($userId); |
92
|
|
|
if ($user === null) { |
93
|
|
|
throw new \Exception('user does not exist'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below) |
97
|
|
|
$userId = $user->getUID(); |
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
$folder = $this->appData->getFolder($userId); |
101
|
|
|
} catch (NotFoundException $e) { |
102
|
|
|
$folder = $this->appData->newFolder($userId); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Clear generated avatars |
110
|
|
|
*/ |
111
|
|
|
public function clearCachedAvatars() { |
112
|
|
|
$users = $this->config->getUsersForUserValue('avatar', 'generated', 'true'); |
113
|
|
|
foreach($users as $userId) { |
114
|
|
|
try { |
115
|
|
|
$folder = $this->appData->getFolder($userId); |
116
|
|
|
$folder->delete(); |
117
|
|
|
} catch (NotFoundException $e) { |
118
|
|
|
$this->logger->debug("No cache for the user $userId. Ignoring..."); |
119
|
|
|
} |
120
|
|
|
$this->config->setUserValue($userId, 'avatar', 'generated', 'false'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns a GuestAvatar. |
126
|
|
|
* |
127
|
|
|
* @param string $name The guest name, e.g. "Albert". |
128
|
|
|
* @return IAvatar |
129
|
|
|
*/ |
130
|
|
|
public function getGuestAvatar(string $name): IAvatar { |
131
|
|
|
return new GuestAvatar($name, $this->logger); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|