1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
4
|
|
|
* @author Arthur Schiwon <[email protected]> |
5
|
|
|
* @author Christopher Schäpers <[email protected]> |
6
|
|
|
* @author Lukas Reschke <[email protected]> |
7
|
|
|
* @author Morris Jobke <[email protected]> |
8
|
|
|
* @author Olivier Mehani <[email protected]> |
9
|
|
|
* @author Robin Appelman <[email protected]> |
10
|
|
|
* @author Roeland Jago Douma <[email protected]> |
11
|
|
|
* @author Thomas Müller <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
14
|
|
|
* @license AGPL-3.0 |
15
|
|
|
* |
16
|
|
|
* This code is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
18
|
|
|
* as published by the Free Software Foundation. |
19
|
|
|
* |
20
|
|
|
* This program is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU Affero General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
namespace OC; |
31
|
|
|
|
32
|
|
|
use OC\Files\Storage\File; |
33
|
|
|
use OC\User\User; |
34
|
|
|
use OCP\Files\NotFoundException; |
35
|
|
|
use OCP\Files\Storage\IStorage; |
36
|
|
|
use OCP\Files\StorageNotAvailableException; |
37
|
|
|
use OCP\IAvatar; |
38
|
|
|
use OCP\IImage; |
39
|
|
|
use OCP\IL10N; |
40
|
|
|
use OC_Image; |
41
|
|
|
use OCP\ILogger; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* This class gets and sets users avatars. |
45
|
|
|
*/ |
46
|
|
|
|
47
|
|
|
class Avatar implements IAvatar { |
48
|
|
|
/** @var IStorage */ |
49
|
|
|
private $storage; |
50
|
|
|
/** @var IL10N */ |
51
|
|
|
private $l; |
52
|
|
|
/** @var User */ |
53
|
|
|
private $user; |
54
|
|
|
/** @var ILogger */ |
55
|
|
|
private $logger; |
56
|
|
|
/** @var string */ |
57
|
|
|
private $path; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* constructor |
61
|
|
|
* |
62
|
|
|
* @param IStorage $storage The storage where the avatars are |
63
|
|
|
* @param IL10N $l |
64
|
|
|
* @param User $user |
65
|
|
|
* @param ILogger $logger |
66
|
|
|
*/ |
67
|
|
|
public function __construct(IStorage $storage, IL10N $l, User $user, ILogger $logger) { |
68
|
|
|
$this->storage = $storage; |
69
|
|
|
$this->l = $l; |
70
|
|
|
$this->user = $user; |
71
|
|
|
$this->logger = $logger; |
72
|
|
|
$this->path = $this->buildAvatarPath(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function buildAvatarPath() { |
76
|
|
|
return \substr_replace(\substr_replace(\md5($this->user->getUID()), '/', 4, 0), '/', 2, 0); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
|
|
public function get($size = 64) { |
83
|
|
|
try { |
84
|
|
|
$file = $this->getFile($size); |
85
|
|
|
} catch (NotFoundException $e) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$avatar = new OC_Image(); |
90
|
|
|
$avatar->loadFromData($file->getContent()); |
91
|
|
|
return $avatar; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Check if an avatar exists for the user |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function exists() { |
100
|
|
|
try { |
101
|
|
|
return $this->storage->file_exists("{$this->path}/avatar.jpg") |
102
|
|
|
|| $this->storage->file_exists("{$this->path}/avatar.png"); |
103
|
|
|
} catch (StorageNotAvailableException $e) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* sets the users avatar |
110
|
|
|
* @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
111
|
|
|
* @throws \Exception if the provided file is not a jpg or png image |
112
|
|
|
* @throws \Exception if the provided image is not valid |
113
|
|
|
* @throws NotSquareException if the image is not square |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public function set($data) { |
117
|
|
|
if ($data instanceof IImage) { |
118
|
|
|
$img = $data; |
119
|
|
|
$data = $img->data(); |
120
|
|
|
} else { |
121
|
|
|
$img = new OC_Image($data); |
122
|
|
|
} |
123
|
|
|
$type = \substr($img->mimeType(), -3); |
124
|
|
|
if ($type === 'peg') { |
125
|
|
|
$type = 'jpg'; |
126
|
|
|
} |
127
|
|
|
if ($type !== 'jpg' && $type !== 'png') { |
128
|
|
|
throw new \Exception($this->l->t('Unknown filetype')); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (!$img->valid()) { |
132
|
|
|
throw new \Exception($this->l->t('Invalid image')); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!($img->height() === $img->width())) { |
136
|
|
|
throw new NotSquareException($this->l->t('Avatar image is not square')); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->remove(); |
140
|
|
|
if (!$this->storage->mkdir($this->path)) { |
141
|
|
|
$this->logger->error("Could not create {$this->path} for {$this->user->getUID()}"); |
142
|
|
|
} |
143
|
|
|
$path = "$this->path/avatar.$type"; |
144
|
|
|
if ($this->storage->file_put_contents($path, $data) === false) { |
|
|
|
|
145
|
|
|
$this->logger->error("Failed to save resized avatar for {$this->user->getUID()} to $path"); |
146
|
|
|
} |
147
|
|
|
$this->user->triggerChange('avatar'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* remove the users avatars |
152
|
|
|
*/ |
153
|
|
|
public function remove() { |
154
|
|
|
$this->storage->rmdir($this->path); |
155
|
|
|
$this->user->triggerChange('avatar'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @inheritdoc |
160
|
|
|
*/ |
161
|
|
|
public function getFile($size) { |
162
|
|
|
$ext = $this->getExtension(); |
163
|
|
|
|
164
|
|
|
$basePath = "{$this->path}/avatar.$ext"; |
165
|
|
|
|
166
|
|
|
if ($size === -1) { |
167
|
|
|
$resizedPath = $basePath; |
168
|
|
|
} else { |
169
|
|
|
$resizedPath = "{$this->path}/avatar.$size.$ext"; |
170
|
|
|
} |
171
|
|
|
// do we have the requested size? |
172
|
|
|
if (!$this->storage->file_exists($resizedPath)) { |
173
|
|
|
if ($size <= 0) { |
174
|
|
|
throw new NotFoundException($resizedPath); |
175
|
|
|
} |
176
|
|
|
// do we have a base image? |
177
|
|
|
if (!$this->storage->file_exists($basePath)) { |
178
|
|
|
throw new NotFoundException($basePath); |
179
|
|
|
} |
180
|
|
|
// resize! |
181
|
|
|
$avatar = new OC_Image(); |
182
|
|
|
$data = $this->storage->file_get_contents($basePath); |
183
|
|
|
$avatar->loadFromData($data); |
|
|
|
|
184
|
|
|
if ($size !== -1) { |
185
|
|
|
$avatar->resize($size); |
186
|
|
|
} |
187
|
|
|
$result = $this->storage->file_put_contents($resizedPath, $avatar->data()); |
188
|
|
|
if ($result === false) { |
189
|
|
|
$this->logger->error("Failed to save resized avatar for {$this->user->getUID()} to $resizedPath"); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return new File($this->storage, $resizedPath); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get the extension of the avatar. If there is no avatar throw Exception |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
* @throws NotFoundException |
201
|
|
|
* @throws StorageNotAvailableException |
202
|
|
|
*/ |
203
|
|
|
private function getExtension() { |
204
|
|
|
if ($this->storage->file_exists("{$this->path}/avatar.jpg")) { |
205
|
|
|
return 'jpg'; |
206
|
|
|
} |
207
|
|
|
if ($this->storage->file_exists("{$this->path}/avatar.png")) { |
208
|
|
|
return 'png'; |
209
|
|
|
} |
210
|
|
|
throw new NotFoundException("{$this->path}/avatar.jpg|png"); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.