/
EmoticonModule.php
117 lines (95 loc) · 2.73 KB
/
EmoticonModule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* This file is part of the Texy! (https://texy.info)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Texy\Modules;
use Texy;
/**
* Emoticon module.
*/
final class EmoticonModule extends Texy\Module
{
/** @var array supported emoticons and image files */
public $icons = [
':-)' => 'smile.gif',
':-(' => 'sad.gif',
';-)' => 'wink.gif',
':-D' => 'biggrin.gif',
'8-O' => 'eek.gif',
'8-)' => 'cool.gif',
':-?' => 'confused.gif',
':-x' => 'mad.gif',
':-P' => 'razz.gif',
':-|' => 'neutral.gif',
];
/** @var string CSS class for emoticons */
public $class;
/** @var string root of relative images (default value is $texy->imageModule->root) */
public $root;
/** @var string physical location of images on server (default value is $texy->imageModule->fileRoot) */
public $fileRoot;
public function __construct($texy)
{
$this->texy = $texy;
$texy->allowed['emoticon'] = FALSE;
$texy->addHandler('emoticon', [$this, 'solve']);
$texy->addHandler('beforeParse', [$this, 'beforeParse']);
}
public function beforeParse()
{
if (empty($this->texy->allowed['emoticon'])) {
return;
}
krsort($this->icons);
$pattern = [];
foreach ($this->icons as $key => $foo) {
$pattern[] = preg_quote($key, '#') . '+'; // last char can be repeated
}
$this->texy->registerLinePattern(
[$this, 'pattern'],
'#(?<=^|[\x00-\x20])(' . implode('|', $pattern) . ')#',
'emoticon',
'#' . implode('|', $pattern) . '#'
);
}
/**
* Callback for: :-))).
* @return Texy\HtmlElement|string|FALSE
*/
public function pattern(Texy\LineParser $parser, array $matches)
{
$match = $matches[0];
// find the closest match
foreach ($this->icons as $emoticon => $foo) {
if (strncmp($match, $emoticon, strlen($emoticon)) === 0) {
return $this->texy->invokeAroundHandlers('emoticon', $parser, [$emoticon, $match]);
}
}
return FALSE; // tohle se nestane
}
/**
* Finish invocation.
* @return Texy\HtmlElement|FALSE
*/
public function solve(Texy\HandlerInvocation $invocation, $emoticon, $raw)
{
$texy = $this->texy;
$file = $this->icons[$emoticon];
$el = new Texy\HtmlElement('img');
$el->attrs['src'] = Texy\Helpers::prependRoot($file, $this->root === NULL ? $texy->imageModule->root : $this->root);
$el->attrs['alt'] = $raw;
$el->attrs['class'][] = $this->class;
// file path
$file = rtrim($this->fileRoot === NULL ? $texy->imageModule->fileRoot : $this->fileRoot, '/\\') . '/' . $file;
if (@is_file($file)) { // intentionally @
$size = @getImageSize($file); // intentionally @
if (is_array($size)) {
$el->attrs['width'] = $size[0];
$el->attrs['height'] = $size[1];
}
}
$texy->summary['images'][] = $el->attrs['src'];
return $el;
}
}