Merge branch '5.2' into 5.3
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / smiley / Smiley.class.php
1 <?php
2 namespace wcf\data\smiley;
3 use wcf\data\DatabaseObject;
4 use wcf\data\ITitledObject;
5 use wcf\system\WCF;
6 use wcf\util\StringUtil;
7
8 /**
9 * Represents a smiley.
10 *
11 * @author Alexander Ebert
12 * @copyright 2001-2019 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @package WoltLabSuite\Core\Data\Smiley
15 *
16 * @property-read integer $smileyID unique id of the smiley
17 * @property-read integer $packageID id of the package which delivers the smiley
18 * @property-read integer|null $categoryID id of the category the smiley belongs to or `null` if it belongs to the default category
19 * @property-read string $smileyPath path to the smiley file relative to wcf's default path
20 * @property-read string $smileyPath2x path to the smiley file relative to wcf's default path (2x version)
21 * @property-read string $smileyTitle title of the smiley or name of language item that contains the title
22 * @property-read string $smileyCode code used for displaying the smiley
23 * @property-read string $aliases alternative codes used for displaying the smiley
24 * @property-read integer $showOrder position of the smiley in relation to the other smileys in the same category
25 */
26 class Smiley extends DatabaseObject implements ITitledObject {
27 protected $height;
28
29 public $smileyCodes;
30
31 /**
32 * @inheritDoc
33 * @since 5.2
34 */
35 public function getTitle() {
36 return WCF::getLanguage()->get($this->smileyTitle);
37 }
38
39 /**
40 * Returns the url to this smiley.
41 *
42 * @return string
43 */
44 public function getURL() {
45 return WCF::getPath().$this->smileyPath;
46 }
47
48 /**
49 * Returns the url to the 2x version of the smiley.
50 *
51 * @return string
52 */
53 public function getURL2x() {
54 return ($this->smileyPath2x) ? WCF::getPath().$this->smileyPath2x : '';
55 }
56
57 /**
58 * Returns all aliases for this smiley.
59 *
60 * @return string[]
61 */
62 public function getAliases() {
63 if (!$this->aliases) return [];
64
65 return explode("\n", StringUtil::unifyNewlines($this->aliases));
66 }
67
68 /**
69 * Returns the height of the smiley.
70 *
71 * @return integer
72 */
73 public function getHeight() {
74 if ($this->height === null) {
75 $this->height = 0;
76
77 $file = WCF_DIR . $this->smileyPath;
78 if (file_exists($file) && preg_match('~\.(gif|jpe?g|png)$~', $file)) {
79 $data = getimagesize($file);
80 if ($data !== false) {
81 // index '1' contains the height of the image
82 $this->height = $data[1];
83 }
84 }
85 }
86
87 return $this->height;
88 }
89
90 /**
91 * Returns the html code to render the smiley.
92 *
93 * @param string $class (additional) class(es) of the smiley element
94 * @return string
95 */
96 public function getHtml($class = '') {
97 $srcset = ($this->smileyPath2x) ? ' srcset="' . StringUtil::encodeHTML($this->getURL2x()) . ' 2x"' : '';
98 $height = ($this->getHeight()) ? ' height="' . $this->getHeight() . '"' : '';
99 if ($class !== '') {
100 $class = ' ' . $class;
101 }
102
103 $title = StringUtil::encodeHTML(WCF::getLanguage()->get($this->smileyTitle));
104
105 return '<img src="' . StringUtil::encodeHTML($this->getURL()) . '" alt="' . StringUtil::encodeHTML($this->smileyCode) . '" title="' . $title . '" class="smiley' . $class . '"' . $srcset . $height . '>';
106 }
107 }