Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
831 florian 1
<?php
2
 
3
// A library of common functions and definitions for
4
// the smileys mod. This library is only loaded when
5
// initializing or saving module settings.
6
 
7
if(!defined("PHORUM") && !defined("PHORUM_ADMIN")) return;
8
 
9
// A match for filtering files that are accepted as smiley images.
10
define('MOD_SMILEYS_IMAGE_MATCH', '/^.+\.(gif|png|jpg|jpeg)$/i');
11
 
12
// A match for matching absolute file paths. Paths that I could think of:
13
// UNIX path        /...
14
// URL              proto://...
15
// Windows path     X:\... or X:/...
16
// Windows net path \\...
17
define('MOD_SMILEYS_ABSPATH_MATCH', '!^/|^\w+://|^\w:[/\\\\]|^\\\\\\\\!i');
18
 
19
// The default smiley prefix path.
20
global $MOD_SMILEY_DEFAULT_PREFIX;
21
$MOD_SMILEY_DEFAULT_PREFIX = './mods/smileys/images/';
22
 
23
// The default list of smileys to install upon initial setup.
24
global $MOD_SMILEY_DEFAULT_SMILEYS;
25
$MOD_SMILEY_DEFAULT_SMILEYS = array(
26
    "(:P)"      => "smiley25.gif spinning smiley sticking its tongue out",
27
    "(td)"      => "smiley23.gif thumbs up",
28
    "(tu)"      => "smiley24.gif thumbs down",
29
    ":)-D"      => "smiley15.gif smileys with beer",
30
    ">:D<"      => "smiley14.gif the finger smiley",
31
    "(:D"       => "smiley12.gif smiling bouncing smiley",
32
    "8-)"       => "smilie8.gif  eye rolling smiley",
33
    ":)o"       => "smiley16.gif drinking smiley",
34
    "::o"       => "smilie10.gif eye popping smiley",
35
    "B)-"       => "smilie7.gif  smoking smiley",
36
    ":("        => "smilie2.gif  sad smiley",
37
    ":)"        => "smilie1.gif  smiling smiley",
38
    ":?"        => "smiley17.gif moody smiley",
39
    ":D"        => "smilie5.gif  grinning smiley",
40
    ":P"        => "smilie6.gif  tongue sticking out smiley",
41
    ":S"        => "smilie11.gif confused smiley",
42
    ":X"        => "smilie9.gif  angry smiley",
43
    ":o"        => "smilie4.gif  yawning smiley",
44
    ";)"        => "smilie3.gif  winking smiley",
45
    "B)"        => "cool.gif     cool smiley",
46
    "X("        => "hot.gif      hot smiley",
47
);
48
 
49
/**
50
 * Sets up initial settings for the smileys mod or upgrades
51
 * the settings from old versions.
52
 * @return modinfo - Updated module information.
53
 */
54
function phorum_mod_smileys_initsettings()
55
{
56
    $PHORUM = $GLOBALS["PHORUM"];
57
    global $MOD_SMILEY_DEFAULT_PREFIX;
58
    global $MOD_SMILEY_DEFAULT_SMILEYS;
59
    $modinfo = isset($PHORUM["mod_smileys"]) ? $PHORUM["mod_smileys"] : array();
60
 
61
    // Keep track if we need to store settings in the database.
62
    $do_db_update = false;
63
 
64
    // Set default for the image prefix path.
65
    if(! isset($modinfo['prefix'])) {
66
        $modinfo['prefix'] = $MOD_SMILEY_DEFAULT_PREFIX;
67
        // So phorum_mod_smileys_available() sees it right away.
68
        $GLOBALS["PHORUM"]["mod_smileys"]["prefix"] = $MOD_SMILEY_DEFAULT_PREFIX;
69
        $do_db_update = true;
70
    }
71
 
72
    // Set a default list of smileys or upgrade from existing smiley mod.
73
    if (! isset($modinfo['smileys']))
74
    {
75
        $modinfo['smileys'] = array();
76
 
77
        // Check if we have smileys from the previous version of the
78
        // smiley mod. These were stored at the same level as the
79
        // settings.
80
        $upgrade_list = array();
81
        if (isset($PHORUM["mod_smileys"])) {
82
            foreach ($PHORUM["mod_smileys"] as $id => $smiley) {
83
                if (is_numeric($id)) {
84
                    $upgrade_list[$id] = $smiley;
85
                }
86
            }
87
        }
88
 
89
        // We have an existing list of smileys to upgrade. Move the
90
        // smileys to their new location.
91
        if (count($upgrade_list)) {
92
            foreach ($upgrade_list as $id => $smiley) {
93
                unset($modinfo[$id]);
94
                $modinfo["smileys"][$id] = $smiley;
95
            }
96
            $do_db_update = true;
97
        }
98
        // Set an initial list of smileys.
99
        else {
100
            foreach ($MOD_SMILEY_DEFAULT_SMILEYS as $search => $data) {
101
                list($smiley, $alt) = preg_split('/\s+/', $data, 2);
102
                $modinfo["smileys"][] = array(
103
                    "search"    => $search,
104
                    "alt"       => $alt,
105
                    "smiley"    => $smiley,
106
                    "uses"      => 2,
107
                );
108
            }
109
            $do_db_update = true;
110
        }
111
    }
112
 
113
    // Store the changed settings in the database. Errors are
114
    // silently ignored here, to keep them away from end-users.
115
    if ($do_db_update) {
116
        list($modinfo, $message) = phorum_mod_smileys_store($modinfo);
117
        $GLOBALS["PHORUM"]["mod_smileys"] = $modinfo;
118
        return $modinfo;
119
    }
120
}
121
 
122
/**
123
 * Reads in the list of available smiley images.
124
 * @return smileys - An array of smiley image filenames.
125
 */
126
function phorum_mod_smileys_available()
127
{
128
    $PHORUM = $GLOBALS["PHORUM"];
129
 
130
    $available_smileys = array();
131
    if(file_exists($PHORUM['mod_smileys']['prefix'])){
132
        $d = dir($PHORUM['mod_smileys']['prefix']);
133
        while($entry=$d->read()) {
134
            if(preg_match(MOD_SMILEYS_IMAGE_MATCH, $entry)) {
135
                $available_smileys[$entry]=$entry;
136
            }
137
        }
138
    }
139
    asort($available_smileys);
140
    return $available_smileys;
141
}
142
 
143
/**
144
 * Compiles replacement arrays for the smileys mod and stores the
145
 * data for the module in the database.
146
 * @param modinfo - The configuration array for mod_smileys.
147
 * @return result - An array containing two elements:
148
 *                  updated module info or NULL on failure and
149
 *                  a message that can be displayed to the user.
150
 */
151
function phorum_mod_smileys_store($modinfo)
152
{
153
    // Get the current list of available smiley images.
154
    $available_smileys = phorum_mod_smileys_available();
155
 
156
    // Sort the smileys by length. We need to do this to replace the
157
    // longest smileys matching strings first. Else for example the
158
    // smiley ":)-D" could end up as (smileyimage)-D, because ":)"
159
    // was replaced first.
160
    uasort($modinfo["smileys"],'phorum_mod_smileys_sortbylength');
161
 
162
    // Create and fill replacement arrays for subject and body.
163
    $smiley_subject_key = array();
164
    $smiley_subject_val = array();
165
    $smiley_body_key = array();
166
    $smiley_body_val = array();
167
    $seen_images = array();
168
    foreach ($modinfo["smileys"] as $id => $smiley)
169
    {
170
        // Check if the smiley image is available. Skip and keep track
171
        // of missing smiley images.
172
        $active = isset($available_smileys[$smiley["smiley"]]) ? true : false;
173
        $modinfo["smileys"][$id]['active'] = $active;
174
        if (! $active) continue;
175
 
176
        // Check if the smiley image has been seen before. If is has, mark
177
        // the current smiley as being an alias. This is used in the editor
178
        // smiley help, to show only one version of a smiley image.
179
        $is_alias = isset($seen_images[$smiley["smiley"]]) ? true : false;
180
        $seen_images[$smiley["smiley"]] = 1;
181
        $modinfo["smileys"][$id]["is_alias"] = $is_alias;
182
 
183
        // Create HTML image code for the smiley.
184
        $prefix = $modinfo["prefix"];
185
        $src = htmlspecialchars("$prefix{$smiley['smiley']}");
186
        $alttxt = empty($smiley['alt']) ? $smiley["search"] : $smiley["alt"];
187
        $alt = htmlspecialchars($alttxt);
188
        $img = "<img class=\"mod_smileys_img\" src=\"$src\" alt=\"$alt\" title=\"$alt\"/>";
189
 
190
        // Below we use htmlspecialchars() on the search string.
191
        // This is done, because the smiley mod is run after formatting
192
        // by Phorum, so characters like < and > are HTML escaped.
193
 
194
        // Body only replace (0) or subject and body replace (2).
195
        if ($smiley['uses'] == 0 || $smiley['uses'] == 2) {
196
            $smiley_body_key[] = htmlspecialchars($smiley['search']);
197
            $smiley_body_val[] = $img;
198
        }
199
 
200
        // Subject only replace (1) or subject and body replace (2).
201
        if ($smiley['uses'] == 1 || $smiley['uses'] == 2) {
202
            $smiley_subject_key[] = htmlspecialchars($smiley['search']);
203
            $smiley_subject_val[] = $img;
204
        }
205
    }
206
 
207
    // Store replacement arrays in the module settings.
208
    $modinfo["replacements"] = array(
209
        "subject" => count($smiley_subject_key)
210
                   ? array($smiley_subject_key, $smiley_subject_val)
211
                   : NULL,
212
        "body"    => count($smiley_body_key)
213
                   ? array($smiley_body_key, $smiley_body_val)
214
                   : NULL
215
    );
216
 
217
    // For quickly determining if the smiley replacements must be run.
218
    $modinfo["do_smileys"] = $modinfo["replacements"]["subject"] != NULL ||
219
                             $modinfo["replacements"]["body"] != NULL;
220
 
221
    // Store the module settings in the database.
222
    if (! phorum_db_update_settings(array("mod_smileys" => $modinfo))) {
223
        return array(NULL, "Saving the smiley settings to the database failed.");
224
    } else {
225
        return array($modinfo, "The smiley settings were successfully saved.");
226
    }
227
}
228
 
229
/**
230
 * A callback function for sorting smileys by their search string length.
231
 * usage: uasort($array_of_smileys, 'phorum_mod_smileys_sortbylength');
232
 */
233
function phorum_mod_smileys_sortbylength($a, $b) {
234
    if (isset($a["search"]) && isset($b["search"])) {
235
        if (strlen($a["search"]) == strlen($b["search"])) {
236
            return strcmp($a["search"], $b["search"]);
237
        } else {
238
            return strlen($a["search"]) < strlen($b["search"]);
239
        }
240
    } else {
241
        return 0;
242
    }
243
}
244
?>