1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace App\View\Helper; |
14
|
|
|
|
15
|
|
|
use App\Model\Entity\User; |
16
|
|
|
use Cake\View\Helper\HtmlHelper; |
17
|
|
|
use Cake\View\Helper\UrlHelper; |
18
|
|
|
use Identicon\Identicon; |
19
|
|
|
use Saito\RememberTrait; |
20
|
|
|
use Saito\User\CurrentUser\CurrentUserInterface; |
21
|
|
|
use Saito\User\ForumsUserInterface; |
22
|
|
|
use Stopwatch\Lib\Stopwatch; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class UserHelper |
26
|
|
|
* |
27
|
|
|
* @package App\View\Helper |
28
|
|
|
* @property HtmlHelper $Html |
29
|
|
|
* @property UrlHelper $Url |
30
|
|
|
*/ |
31
|
|
|
class UserHelper extends AppHelper |
32
|
|
|
{ |
33
|
|
|
use RememberTrait; |
34
|
|
|
|
35
|
|
|
public $helpers = ['Html', 'Url']; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* banned |
39
|
|
|
* |
40
|
|
|
* @param bool $isBanned banned |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function banned($isBanned) |
44
|
|
|
{ |
45
|
|
|
$out = ''; |
46
|
|
|
if ($isBanned) { |
47
|
|
|
$out = '<i class="fa fa-ban fa-lg"></i>'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $out; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* generates CSS from user-preferences |
55
|
|
|
* |
56
|
|
|
* @param array $User user |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function generateCss(array $User) |
60
|
|
|
{ |
61
|
|
|
$styles = []; |
62
|
|
|
|
63
|
|
|
// colors |
64
|
|
|
$cNew = $User['user_color_new_postings']; |
65
|
|
|
$cOld = $User['user_color_old_postings']; |
66
|
|
|
$cAct = $User['user_color_actual_posting']; |
67
|
|
|
|
68
|
|
|
$aMetatags = ['', ':link', ':visited', ':hover', ':active']; |
69
|
|
|
foreach ($aMetatags as $aMetatag) { |
70
|
|
|
if (!empty($cOld) && $cOld !== '#') { |
71
|
|
|
$styles[] = ".et-root .et$aMetatag, .et-reply .et$aMetatag { color: $cOld; }"; |
72
|
|
|
} |
73
|
|
|
if (!empty($cNew) && $cNew !== '#') { |
74
|
|
|
$styles[] = ".et-new .et$aMetatag { color: $cNew; }"; |
75
|
|
|
} |
76
|
|
|
if (!empty($cAct) && $cAct !== '#') { |
77
|
|
|
$styles[] = ".et-current .et$aMetatag { color: $cAct; }"; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return '<style type="text/css">' . implode(" ", $styles) . '</style>'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Translates user types |
86
|
|
|
* |
87
|
|
|
* @param string $type type |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
public function type($type) |
91
|
|
|
{ |
92
|
|
|
// write out all __() strings for l10n |
93
|
|
|
switch ($type) { |
94
|
|
|
case 'user': |
95
|
|
|
return __('user.type.user'); |
96
|
|
|
case 'mod': |
97
|
|
|
return __('user.type.mod'); |
98
|
|
|
case 'admin': |
99
|
|
|
return __('user.type.admin'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Creates link to user's external (non-Saito) homepage |
105
|
|
|
* |
106
|
|
|
* @param string $url user provided URL-string |
107
|
|
|
* @return string link or escaped string |
108
|
|
|
*/ |
109
|
|
|
public function linkExternalHomepage(string $url): string |
110
|
|
|
{ |
111
|
|
|
$link = $url; |
112
|
|
|
|
113
|
|
|
if (substr($link, 0, 4) == 'www.') { |
114
|
|
|
$link = 'http://' . $link; |
115
|
|
|
} |
116
|
|
|
if (substr($link, 0, 4) == 'http') { |
117
|
|
|
$text = '<i class="fa fa-home fa-lg"></i>'; |
118
|
|
|
|
119
|
|
|
return $this->Html->link($text, $link, ['escape' => false]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return h($url); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Link to user-profile |
127
|
|
|
* |
128
|
|
|
* @param User|ForumsUserInterface $user user |
129
|
|
|
* @param bool|CurrentUserInterface $link link |
130
|
|
|
* @param array $options options |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function linkToUserProfile($user, $link = true, array $options = []): string |
134
|
|
|
{ |
135
|
|
|
$options += [ |
136
|
|
|
'title' => $user->get('username'), |
137
|
|
|
'escape' => true |
138
|
|
|
]; |
139
|
|
|
$id = $user->get('id'); |
140
|
|
|
|
141
|
|
|
$name = $options['title']; |
142
|
|
|
unset($options['title']); |
143
|
|
|
|
144
|
|
|
if (empty($id)) { |
145
|
|
|
// removed user |
146
|
|
|
$html = $name; |
147
|
|
|
} elseif (($link === true) |
148
|
|
|
|| ($link instanceof CurrentUserInterface && $link->isLoggedIn()) |
149
|
|
|
) { |
150
|
|
|
return $this->Html->link($name, '/users/view/' . $id, $options); |
151
|
|
|
} else { |
152
|
|
|
$html = $name; |
153
|
|
|
} |
154
|
|
|
if ($options['escape']) { |
155
|
|
|
$html = h($html); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $html; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get image avatar for user |
163
|
|
|
* |
164
|
|
|
* @param User|ForumsUserInterface $user User |
165
|
|
|
* @param array $options options |
166
|
|
|
* @return string HTML |
167
|
|
|
*/ |
168
|
|
|
public function getAvatar($user, array $options = []) |
169
|
|
|
{ |
170
|
|
|
$getAvatar = function () use ($user, $options) { |
171
|
|
|
Stopwatch::start('UserHelper::getAvatar()'); |
172
|
|
|
$defaults = [ |
173
|
|
|
'class' => 'avatar-image', |
174
|
|
|
'link' => [ |
175
|
|
|
'class' => 'avatar-link', |
176
|
|
|
'escape' => false |
177
|
|
|
], |
178
|
|
|
'size' => 50, |
179
|
|
|
'style' => '', |
180
|
|
|
'tag' => 'span' |
181
|
|
|
]; |
182
|
|
|
$options = array_replace_recursive($defaults, $options); |
183
|
|
|
$size = $options['size']; |
184
|
|
|
|
185
|
|
|
$avatar = $user->get('avatar'); |
186
|
|
|
if ($avatar) { |
187
|
|
|
$userId = $user->get('id'); |
188
|
|
|
$url = "useruploads/users/avatar/{$userId}/square_{$avatar}"; |
189
|
|
|
$imgUri = $this->Url->assetUrl($url); |
190
|
|
|
} else { |
191
|
|
|
$name = $user->get('username'); |
192
|
|
|
$hdpi = 2 * $size; |
193
|
|
|
$imgUri = (new Identicon)->getImageDataUri($name, $hdpi); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$style = "background-image: url({$imgUri});" . $options['style']; |
197
|
|
|
|
198
|
|
|
$html = $this->Html->tag( |
199
|
|
|
$options['tag'], |
200
|
|
|
'', |
201
|
|
|
[ |
202
|
|
|
'class' => $options['class'], |
203
|
|
|
'style' => $style, |
204
|
|
|
] |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
if ($options['link'] !== false) { |
208
|
|
|
$options['link']['title'] = $html; |
209
|
|
|
$html = $this->linkToUserProfile($user, true, $options['link']); |
210
|
|
|
} |
211
|
|
|
Stopwatch::end('UserHelper::getAvatar()'); |
212
|
|
|
|
213
|
|
|
return $html; |
214
|
|
|
}; |
215
|
|
|
|
216
|
|
|
$name = $user->get('username'); |
217
|
|
|
$hash = 'avatar.' . md5($name . serialize($options)); |
218
|
|
|
|
219
|
|
|
return $this->remember($hash, $getAvatar); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|