Commit | Line | Data |
---|---|---|
dcc2332d | 1 | <?php |
a9229942 | 2 | |
dcc2332d | 3 | namespace wcf\data\smiley; |
a9229942 | 4 | |
dcc2332d | 5 | use wcf\data\DatabaseObject; |
a53c38ea | 6 | use wcf\data\ITitledObject; |
dcc2332d MW |
7 | use wcf\system\WCF; |
8 | use wcf\util\StringUtil; | |
9 | ||
10 | /** | |
11 | * Represents a smiley. | |
a9229942 TD |
12 | * |
13 | * @author Alexander Ebert | |
14 | * @copyright 2001-2019 WoltLab GmbH | |
15 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
a9229942 TD |
16 | * |
17 | * @property-read int $smileyID unique id of the smiley | |
18 | * @property-read int $packageID id of the package which delivers the smiley | |
19 | * @property-read int|null $categoryID id of the category the smiley belongs to or `null` if it belongs to the default category | |
20 | * @property-read string $smileyPath path to the smiley file relative to wcf's default path | |
d18f6ade | 21 | * @property-read string $smileyPath2x path to the smiley file relative to wcf's default path (2x version) |
a9229942 TD |
22 | * @property-read string $smileyTitle title of the smiley or name of language item that contains the title |
23 | * @property-read string $smileyCode code used for displaying the smiley | |
24 | * @property-read string $aliases alternative codes used for displaying the smiley | |
25 | * @property-read int $showOrder position of the smiley in relation to the other smileys in the same category | |
dcc2332d | 26 | */ |
a9229942 TD |
27 | class Smiley extends DatabaseObject implements ITitledObject |
28 | { | |
29 | protected $height; | |
30 | ||
d18f6ade SG |
31 | protected $width; |
32 | ||
a9229942 TD |
33 | public $smileyCodes; |
34 | ||
35 | /** | |
36 | * @inheritDoc | |
37 | * @since 5.2 | |
38 | */ | |
a4a634aa | 39 | public function getTitle(): string |
a9229942 TD |
40 | { |
41 | return WCF::getLanguage()->get($this->smileyTitle); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Returns the url to this smiley. | |
46 | * | |
47 | * @return string | |
48 | */ | |
49 | public function getURL() | |
50 | { | |
51 | return WCF::getPath() . $this->smileyPath; | |
52 | } | |
53 | ||
54 | /** | |
55 | * Returns the url to the 2x version of the smiley. | |
56 | * | |
57 | * @return string | |
58 | */ | |
59 | public function getURL2x() | |
60 | { | |
61 | return ($this->smileyPath2x) ? WCF::getPath() . $this->smileyPath2x : ''; | |
62 | } | |
63 | ||
64 | /** | |
65 | * Returns all aliases for this smiley. | |
66 | * | |
67 | * @return string[] | |
68 | */ | |
69 | public function getAliases() | |
70 | { | |
71 | if (!$this->aliases) { | |
72 | return []; | |
73 | } | |
74 | ||
75 | return \explode("\n", StringUtil::unifyNewlines($this->aliases)); | |
76 | } | |
77 | ||
78 | /** | |
d18f6ade | 79 | * Returns the dimensions of the smiley. |
a9229942 | 80 | * |
d18f6ade SG |
81 | * @since 5.4 |
82 | * @return int[] | |
a9229942 | 83 | */ |
d18f6ade | 84 | public function getDimensions() |
a9229942 TD |
85 | { |
86 | if ($this->height === null) { | |
d18f6ade | 87 | $this->height = $this->width = 0; |
a9229942 TD |
88 | |
89 | $file = WCF_DIR . $this->smileyPath; | |
0c7578ed | 90 | if (\file_exists($file) && \preg_match('~\.(gif|jpe?g|png|webp)$~', $file)) { |
a9229942 TD |
91 | $data = \getimagesize($file); |
92 | if ($data !== false) { | |
d18f6ade SG |
93 | // The first two indices of `getimagesize()` represent the image dimensions. |
94 | [$this->width, $this->height] = $data; | |
a9229942 TD |
95 | } |
96 | } | |
97 | } | |
98 | ||
d18f6ade SG |
99 | return [ |
100 | 'width' => $this->width, | |
101 | 'height' => $this->height, | |
102 | ]; | |
103 | } | |
104 | ||
105 | /** | |
106 | * Returns the height of the smiley. | |
107 | * | |
108 | * @return int | |
109 | */ | |
110 | public function getHeight() | |
111 | { | |
112 | return $this->getDimensions()['height']; | |
113 | } | |
114 | ||
115 | /** | |
116 | * Returns the width of the smiley. | |
117 | * | |
118 | * @since 5.4 | |
119 | * @return int | |
120 | */ | |
121 | public function getWidth() | |
122 | { | |
123 | return $this->getDimensions()['width']; | |
a9229942 TD |
124 | } |
125 | ||
126 | /** | |
127 | * Returns the html code to render the smiley. | |
128 | * | |
129 | * @param string $class (additional) class(es) of the smiley element | |
130 | * @return string | |
131 | */ | |
add09366 | 132 | public function getHtml($class = '') |
a9229942 TD |
133 | { |
134 | $srcset = ($this->smileyPath2x) ? ' srcset="' . StringUtil::encodeHTML($this->getURL2x()) . ' 2x"' : ''; | |
135 | $height = ($this->getHeight()) ? ' height="' . $this->getHeight() . '"' : ''; | |
d18f6ade | 136 | $width = ($this->getWidth()) ? ' width="' . $this->getWidth() . '"' : ''; |
9e9fe58f | 137 | |
4ba8a9af | 138 | return \sprintf( |
d3cef062 | 139 | '<img src="%s" alt="%s" title="%s" class="%s" %s %s %s loading="eager" translate="no">', |
4ba8a9af AE |
140 | StringUtil::encodeHTML($this->getURL()), |
141 | StringUtil::encodeHTML($this->smileyCode), | |
142 | StringUtil::encodeHTML(WCF::getLanguage()->get($this->smileyTitle)), | |
143 | 'smiley' . ($class ? " {$class}" : ''), | |
144 | $srcset, | |
145 | $height, | |
146 | $width, | |
147 | ); | |
a9229942 | 148 | } |
dcc2332d | 149 | } |