1: <?php
2: /**
3: * The Horde_Text_Filter_Emoticons:: class finds emoticon strings in a block
4: * of text and does a transformation on them.
5: *
6: * By default, this filter does not do any transformation to the emoticon.
7: *
8: * Parameters:
9: * <pre>
10: * entities - (boolean) Use HTML entity versions of the patterns?
11: * DEFAULT: false
12: * </pre>
13: *
14: * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
15: *
16: * See the enclosed file COPYING for license information (LGPL). If you
17: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
18: *
19: * @author Marko Djukic <marko@oblo.com>
20: * @category Horde
21: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
22: * @package Text_Filter
23: */
24: class Horde_Text_Filter_Emoticons extends Horde_Text_Filter_Base
25: {
26: /**
27: * Filter parameters.
28: *
29: * @var array
30: */
31: protected $_params = array(
32: 'entities' => false
33: );
34:
35: /* List complex strings before simpler ones, otherwise for example :((
36: * would be matched against :( before :(( is found. */
37: protected $_emoticons = array(
38: ':/' => 'frustrated', ':-/' => 'frustrated',
39: // ':*>' => 'blush',
40: ':e' => 'disappointed',
41: '=:)$' => 'mrt',
42: '#|' => 'hangover', '#-|' => 'hangover',
43: ':-@' => 'shout', ':@' => 'shout',
44: ':((' => 'bigfrown', ':C' => 'bigfrown',
45: ':S' => 'dazed', ':-S' => 'dazed',
46: 'X@' => 'angry',
47: 'X(' => 'mad',
48: // '>:)' => 'devil', '>:-)' => 'devil',
49: // '>:p' => 'deviltongue', '>:-p' => 'deviltongue',
50: // '>:p' => 'raspberry', '>:P' => 'raspberry',
51: // '&)' => 'punk',
52: // '&p' => 'punktongue',
53: // '=&)' => 'punkmohawk',
54: ':]' => 'grin',
55: '#[' => 'hurt', '#(' => 'hurt', '#-[' => 'hurt', '#-(' => 'hurt',
56: ':O' => 'embarrassed', ':-O' => 'embarrassed',
57: ':[' => 'sad',
58: // '>:@' => 'enraged',
59: // ':&' => 'annoyed',
60: '=(' => 'worried', '=-(' => 'worried',
61: ':|=' => 'vampire',
62: ':-(' => 'frown', ':(' => 'frown',
63: ':D' => 'biggrin', ':-D' => 'biggrin', ':d' => 'biggrin', ':-d' => 'biggrin',
64: // '8)' => 'cool',
65: // In English, 8PM occurs sufficiently often to specifically
66: // search for and exclude
67: //
68: // '8D' => 'coolgrin',
69: ':p' => 'tongueout', ':P' => 'tongueout', //
70: '?:(' => 'confused', '%-(' => 'confused',
71: // ':)&' => 'love',
72: 'O;-)' => 'angelwink',
73: ';]' => 'winkgrin',
74: ';p' => 'winktongue', ';P' => 'winktongue', //
75: ':|' => 'indifferent', ':-|' => 'indifferent',
76: '!|' => 'tired', '!-I' => 'tired',
77: '|I' => 'asleep', '|-I' => 'asleep',
78: 'O:)' => 'angel', 'O:-)' => 'angel',
79: 'O;)' => 'angelwink',
80: ';-)' => 'wink', ';)' => 'wink',
81: ':#)' => 'clown', ':o)' => 'clown',
82: ':)' => 'smile', ':-)' => 'smile',
83: );
84:
85: /**
86: * Returns a hash with replace patterns.
87: *
88: * @return array Patterns hash.
89: */
90: public function getPatterns()
91: {
92: /* Build the patterns. */
93: $patterns = array_keys($this->getIcons());
94: if ($this->_params['entities']) {
95: $patterns = array_map('htmlspecialchars', $patterns);
96: $beg_pattern = '(^|\s|<br />| )(';
97: $end_pattern = ')(?=\s|<br />| )';
98: } else {
99: $beg_pattern = '(^|\s)(';
100: $end_pattern = ')(?=\s)';
101: }
102: $patterns = array_map('preg_quote', $patterns);
103:
104: /* Check for a smiley either immediately at the start of a line or
105: * following a space. Use {} as the preg delimiters as this is not
106: * found in any smiley. */
107: $regexp = '{' . $beg_pattern . implode('|', $patterns) . $end_pattern . '}';
108:
109: return array('regexp_callback' => array(
110: $regexp => array($this, 'emoticonReplace')
111: ));
112: }
113:
114: /**
115: * Returns the replacement emoticon text.
116: *
117: * @param array $matches Matches from preg_replace_callback().
118: *
119: * @return string The replacement text.
120: */
121: public function emoticonReplace($matches)
122: {
123: return $matches[1] . $this->getIcon($matches[2]) . (empty($matches[3]) ? '' : $matches[3]);
124: }
125:
126: /**
127: * Return the replacement emoticon text.
128: *
129: * @param string $icon The emoticon name.
130: *
131: * @return string The replacement text.
132: */
133: public function getIcon($icon)
134: {
135: return $icon;
136: }
137:
138: /**
139: * Returns a hash with all emoticons and names or the name of a single
140: * emoticon.
141: *
142: * @param string $icon If set, return the name for that emoticon only.
143: *
144: * @return array|string Patterns hash or emoticon name.
145: */
146: public function getIcons($icon = null)
147: {
148: return is_null($icon)
149: ? $this->_emoticons
150: : (isset($this->_emoticons[$icon]) ? $this->_emoticons[$icon] : null);
151: }
152:
153: }
154: