ee129f663af36e5febaee7dcb3d3100126d99e63
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / smiley / Smiley.class.php
1 <?php
2
3 namespace wcf\data\smiley;
4
5 use wcf\data\DatabaseObject;
6 use wcf\data\ITitledObject;
7 use wcf\system\WCF;
8 use wcf\util\StringUtil;
9
10 /**
11 * Represents a smiley.
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>
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
21 * @property-read string $smileyPath2x path to the smiley file relative to wcf's default path (2x version)
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
26 */
27 class Smiley extends DatabaseObject implements ITitledObject
28 {
29 protected $height;
30
31 protected $width;
32
33 public $smileyCodes;
34
35 /**
36 * @inheritDoc
37 * @since 5.2
38 */
39 public function getTitle(): string
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 /**
79 * Returns the dimensions of the smiley.
80 *
81 * @since 5.4
82 * @return int[]
83 */
84 public function getDimensions()
85 {
86 if ($this->height === null) {
87 $this->height = $this->width = 0;
88
89 $file = WCF_DIR . $this->smileyPath;
90 if (\file_exists($file) && \preg_match('~\.(gif|jpe?g|png|webp)$~', $file)) {
91 $data = \getimagesize($file);
92 if ($data !== false) {
93 // The first two indices of `getimagesize()` represent the image dimensions.
94 [$this->width, $this->height] = $data;
95 }
96 }
97 }
98
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'];
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 */
132 public function getHtml($class = '')
133 {
134 $srcset = ($this->smileyPath2x) ? ' srcset="' . StringUtil::encodeHTML($this->getURL2x()) . ' 2x"' : '';
135 $height = ($this->getHeight()) ? ' height="' . $this->getHeight() . '"' : '';
136 $width = ($this->getWidth()) ? ' width="' . $this->getWidth() . '"' : '';
137
138 return \sprintf(
139 '<img src="%s" alt="%s" title="%s" class="%s" %s %s %s loading="eager" translate="no">',
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 );
148 }
149 }