|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
4
|
|
|
* @author Christopher Schäpers <[email protected]> |
|
5
|
|
|
* @author Joas Schilling <[email protected]> |
|
6
|
|
|
* @author Lukas Reschke <[email protected]> |
|
7
|
|
|
* @author Morris Jobke <[email protected]> |
|
8
|
|
|
* @author Robin Appelman <[email protected]> |
|
9
|
|
|
* @author Robin McCorkell <[email protected]> |
|
10
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
11
|
|
|
* @author Thomas Müller <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|
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 OCP\Files\Folder; |
|
33
|
|
|
use OCP\Files\File; |
|
34
|
|
|
use OCP\IL10N; |
|
35
|
|
|
use OC_Image; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* This class gets and sets users avatars. |
|
39
|
|
|
*/ |
|
40
|
|
|
|
|
41
|
|
|
class Avatar implements \OCP\IAvatar { |
|
42
|
|
|
/** @var Folder */ |
|
43
|
|
|
private $folder; |
|
44
|
|
|
|
|
45
|
|
|
/** @var IL10N */ |
|
46
|
|
|
private $l; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* constructor |
|
50
|
|
|
* |
|
51
|
|
|
* @param Folder $folder The folder where the avatars are |
|
52
|
|
|
* @param IL10N $l |
|
53
|
|
|
*/ |
|
54
|
8 |
|
public function __construct (Folder $folder, IL10N $l) { |
|
55
|
8 |
|
$this->folder = $folder; |
|
56
|
8 |
|
$this->l = $l; |
|
57
|
8 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* get the users avatar |
|
61
|
|
|
* @param int $size size in px of the avatar, avatars are square, defaults to 64 |
|
62
|
|
|
* @return boolean|\OCP\IImage containing the avatar or false if there's no image |
|
63
|
|
|
*/ |
|
64
|
3 |
|
public function get ($size = 64) { |
|
65
|
3 |
|
if ($this->folder->nodeExists('avatar.jpg')) { |
|
66
|
1 |
|
$ext = 'jpg'; |
|
67
|
3 |
|
} elseif ($this->folder->nodeExists('avatar.png')) { |
|
68
|
1 |
|
$ext = 'png'; |
|
69
|
1 |
|
} else { |
|
70
|
1 |
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
$avatar = new OC_Image(); |
|
74
|
2 |
|
if ($this->folder->nodeExists('avatar.' . $size . '.' . $ext)) { |
|
75
|
|
|
/** @var File $node */ |
|
76
|
1 |
|
$node = $this->folder->get('avatar.' . $size . '.' . $ext); |
|
77
|
1 |
|
$avatar->loadFromData($node->getContent()); |
|
78
|
1 |
|
} else { |
|
79
|
|
|
/** @var File $node */ |
|
80
|
1 |
|
$node = $this->folder->get('avatar.' . $ext); |
|
81
|
1 |
|
$avatar->loadFromData($node->getContent()); |
|
82
|
1 |
|
$avatar->resize($size); |
|
83
|
1 |
|
$this->folder->newFile('avatar.' . $size . '.' . $ext)->putContent($avatar->data()); |
|
84
|
|
|
} |
|
85
|
2 |
|
return $avatar; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Check if an avatar exists for the user |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
3 |
|
public function exists() { |
|
94
|
3 |
|
return $this->folder->nodeExists('avatar.jpg') || $this->folder->nodeExists('avatar.png'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* sets the users avatar |
|
99
|
|
|
* @param \OCP\IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
100
|
|
|
* @throws \Exception if the provided file is not a jpg or png image |
|
101
|
|
|
* @throws \Exception if the provided image is not valid |
|
102
|
|
|
* @throws \OC\NotSquareException if the image is not square |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function set ($data) { |
|
106
|
|
|
|
|
107
|
1 |
|
if($data instanceOf \OCP\IImage) { |
|
108
|
|
|
$img = $data; |
|
109
|
|
|
$data = $img->data(); |
|
110
|
|
|
} else { |
|
111
|
1 |
|
$img = new OC_Image($data); |
|
112
|
|
|
} |
|
113
|
1 |
|
$type = substr($img->mimeType(), -3); |
|
114
|
1 |
|
if ($type === 'peg') { |
|
115
|
|
|
$type = 'jpg'; |
|
116
|
|
|
} |
|
117
|
1 |
|
if ($type !== 'jpg' && $type !== 'png') { |
|
118
|
|
|
throw new \Exception($this->l->t("Unknown filetype")); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
if (!$img->valid()) { |
|
122
|
|
|
throw new \Exception($this->l->t("Invalid image")); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
if (!($img->height() === $img->width())) { |
|
126
|
|
|
throw new \OC\NotSquareException(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
$this->remove(); |
|
130
|
1 |
|
$this->folder->newFile('avatar.'.$type)->putContent($data); |
|
|
|
|
|
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* remove the users avatar |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function remove () { |
|
138
|
|
|
try { |
|
139
|
1 |
|
$this->folder->get('avatar.jpg')->delete(); |
|
140
|
1 |
|
} catch (\OCP\Files\NotFoundException $e) {} |
|
|
|
|
|
|
141
|
|
|
try { |
|
142
|
1 |
|
$this->folder->get('avatar.png')->delete(); |
|
143
|
1 |
|
} catch (\OCP\Files\NotFoundException $e) {} |
|
|
|
|
|
|
144
|
1 |
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
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.