File: serendipity_event_emoticate.php

package info (click to toggle)
serendipity 1.0.4-1%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 14,692 kB
  • ctags: 10,206
  • sloc: php: 83,899; sql: 1,024; sh: 597; makefile: 56
file content (205 lines) | stat: -rw-r--r-- 7,615 bytes parent folder | download | duplicates (2)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php # $Id: serendipity_event_emoticate.php 1529 2006-12-01 09:07:52Z garvinhicking $


if (IN_serendipity !== true) {
    die ("Don't hack!");
}

// Probe for a language include with constants. Still include defines later on, if some constants were missing
$probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
if (file_exists($probelang)) {
    include $probelang;
}

include dirname(__FILE__) . '/lang_en.inc.php';

class serendipity_event_emoticate extends serendipity_event
{
    var $title = PLUGIN_EVENT_EMOTICATE_NAME;

    function introspect(&$propbag)
    {
        global $serendipity;

        $propbag->add('name',          PLUGIN_EVENT_EMOTICATE_NAME);
        $propbag->add('description',   PLUGIN_EVENT_EMOTICATE_DESC);
        $propbag->add('stackable',     false);
        $propbag->add('author',        'Serendipity Team');
        $propbag->add('version',       '1.3');
        $propbag->add('requirements',  array(
            'serendipity' => '0.8',
            'smarty'      => '2.6.7',
            'php'         => '4.1.0'
        ));
        $propbag->add('groups', array('MARKUP'));
        $propbag->add('cachable_events', array('frontend_display' => true));
        $propbag->add('event_hooks',   array('frontend_display' => true, 'frontend_comment' => true));

        $this->markup_elements = array(
            array(
              'name'     => 'ENTRY_BODY',
              'element'  => 'body',
            ),
            array(
              'name'     => 'EXTENDED_BODY',
              'element'  => 'extended',
            ),
            array(
              'name'     => 'COMMENT',
              'element'  => 'comment',
            ),
            array(
              'name'     => 'HTML_NUGGET',
              'element'  => 'html_nugget',
            )
        );
        $conf_array = array();
        foreach($this->markup_elements as $element) {
            $conf_array[] = $element['name'];
        }
        $conf_array[] = 'extension';
        $propbag->add('configuration', $conf_array);

    }

    function install() {
        serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
    }

    function uninstall() {
        serendipity_plugin_api::hook_event('backend_cache_purge', $this->title);
        serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
    }

    function getEmoticons() {
        global $serendipity;

        /* Avoid multiple runs of serendipity_getTemplateFile(),
           in other words - if we already have a list of smilies, don't bother looking for another */
        if (isset($this->smilies) && sizeof($this->smilies) != 0) {
            return $this->smilies;
        }

        /* Hijack global variable $serendipity['custom_emoticons'] if it exists */
        $hijack_file = serendipity_getTemplateFile('emoticons.inc.php', 'serendipityPath');
        if (@file_exists($hijack_file)) {
            @include $hijack_file; // This file contains $serendipity['custom_emoticons'] and maybe $serendipity['custom_emoticons_regexp']
            if (isset($serendipity['custom_emoticons']) && is_array($serendipity['custom_emoticons'])) {
                $this->smilies = $serendipity['custom_emoticons'];
                if (is_array($this->smilies) && (!isset($serendipity['custom_emoticons_regexp']) || !$serendipity['custom_emoticons_regexp'])) {
                    foreach($this->smilies AS $key => $val) {
                        unset($this->smilies[$key]);
                        $this->smilies[preg_quote($key, '/')] = $val;
                    }
                }
            }
        }

        if (!isset($this->smilies)) {
            $ext = $this->get_config('extension', 'png');
            $this->smilies = array(
                "\:'\("    => serendipity_getTemplateFile('img/emoticons/cry.'.$ext),

                '\:\-?\)'  => serendipity_getTemplateFile('img/emoticons/smile.'.$ext),

                '\:\|'     => serendipity_getTemplateFile('img/emoticons/normal.'.$ext),

                '\:\-?O'  => serendipity_getTemplateFile('img/emoticons/eek.'.$ext),

                '\:\-?\('  => serendipity_getTemplateFile('img/emoticons/sad.'.$ext),

                '8\-?\)'  => serendipity_getTemplateFile('img/emoticons/cool.'.$ext),

                '\:\-?D'  => serendipity_getTemplateFile('img/emoticons/laugh.'.$ext),

                '\:\-?P'  => serendipity_getTemplateFile('img/emoticons/tongue.'.$ext),

                ';\-?\)'  => serendipity_getTemplateFile('img/emoticons/wink.'.$ext),
            );
        }

        return $this->smilies;
    }

    function humanReadableEmoticon($key) {
        return str_replace(array('-?', '\\'), array('-', ''), $key);
    }

    function generate_content(&$title) {
        $title = $this->title;
    }

    function example() {
        $s  = '<table cellspacing="5" style="margin-left: auto; margin-right: auto">';
        $s .= '<tr>';
        $i = 1;
        foreach($this->getEmoticons() as $key => $value) {
            $s .= '<td style="text-align: center">' . $this->humanReadableEmoticon($key) . '</td><td><img src="'. $value .'"></td>' . "\n";
            if ($i++ % 7 == 0) $s .= '</tr><tr>';
        }
        $s .= '</tr>';
        $s .= '</table>';

        return $s;
    }

    function introspect_config_item($name, &$propbag)
    {
        switch($name) {
        case 'extension':
            $propbag->add('type', 'string');
            $propbag->add('name',        PLUGIN_EVENT_EMOTICATE_EXTENSION);
            $propbag->add('description', PLUGIN_EVENT_EMOTICATE_EXTENSION_BLAHBLAH);
            $propbag->add('default', 'png');
            break;
        default:
            $propbag->add('type',        'boolean');
            $propbag->add('name',        constant($name));
            $propbag->add('description', sprintf(APPLY_MARKUP_TO, constant($name)));
            $propbag->add('default', 'true');
        }
        return true;
    }


    function event_hook($event, &$bag, &$eventData) {
        global $serendipity;
        $hooks = &$bag->get('event_hooks');

        if (isset($hooks[$event])) {
            switch($event) {
                case 'frontend_display':

                    foreach ($this->markup_elements as $temp) {
                        if (serendipity_db_bool($this->get_config($temp['name'], true)) && isset($eventData[$temp['element']])) {
                            $element = &$eventData[$temp['element']];

                            foreach ($this->getEmoticons() as $key => $value) {
                                $element = preg_replace("/([\t\s\.\!>]+)" . $key . "([\t\s\!\.\)<]+|\$)/U",
                                    "$1<img src=\"$value\" alt=\"" . $this->humanReadableEmoticon($key) . "\" style=\"display: inline; vertical-align: bottom;\" class=\"emoticon\" />$2",
                                    $element);
                            }
                        }
                    }
                    return true;
                    break;

                case 'frontend_comment':
                    if (serendipity_db_bool($this->get_config('COMMENT', true))) {
                        echo '<div class="serendipity_commentDirection serendipity_comment_emoticate">' . PLUGIN_EVENT_EMOTICATE_TRANSFORM . '</div>';
                    }
                    return true;
                    break;

              default:
                return false;
            }
        } else {
            return false;
        }
    }

}

/* vim: set sts=4 ts=4 expandtab : */
?>