File: spelling.php

package info (click to toggle)
imp4 4.1.3-4
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 14,988 kB
  • ctags: 3,720
  • sloc: xml: 17,038; php: 16,350; makefile: 64
file content (280 lines) | stat: -rw-r--r-- 9,186 bytes parent folder | download
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
 * $Horde: imp/spelling.php,v 2.67.6.7 2006/01/01 21:28:52 jan Exp $
 *
 * Copyright 1999-2006 Charles J. Hagenbuch <chuck@horde.org>
 * Copyright 1999-2006 Jon Parise <jon@horde.org>
 *
 * See the enclosed file COPYING for license information (GPL).  If you
 * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
 */

define('IMP_SPELL_CHANGE',     1);
define('IMP_SPELL_CHANGE_ALL', 2);
define('IMP_SPELL_IGNORE',     3);
define('IMP_SPELL_IGNORE_ALL', 4);

/* Base list of words to ignore. */
$ignore_list = array(
    'com', 'cc', 'www', 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
    'aug', 'sep', 'oct', 'nov', 'dec', 'fwd', 'dns', 'http', 'ca', 'html',
    'tm', 'mmunity', 'co', 'op', 'https', 'netscape', 'webmail', 'bcc',
    'jpg', 'gif', 'email', 'tel', 'ie', 'eg'
);

/**
 * Find the offset of a given word.
 *
 * @param string $message  The text of the message.
 * @param string $word     The word to find.
 * @param integer $start   The offset.
 *
 * @return integer  The offset of the word.
 */
function _findOffset($message, $word, $start)
{
    $offset = $start;
    $pos = -1;

    /* If all things are right the word is at offset - 1 */
    if ($start > 3) {
        $start -= 3;
    } else {
        $start = 0;
    }
    while (($pos == -1) && ($start >= 0)) {
        $pos = String::pos($message, $word, $start);
        if (($pos == '') && !is_int($pos)) {
            $start--;
            $pos = -1;
        }
    }

    return $pos;
}


/**
 * Highlight in error in a given message.
 *
 * @param string $error    The misspelled word.
 * @param string $message  The text of the message.
 * @param integer $start   The offset.
 *
 * @return string  The string with the error highlighted.
 */
function _highlightError($error, $message, $offset)
{
    $pos = strpos($message, $error, ((($offset - 1) > 0) ? ($offset - 1) : 0));
    if (($pos - 15) > 0) {
        $start = $pos - 15;
    } else {
        $start = 0;
    }
    $length = String::length($error) + 30;

    $message = substr_replace($message, '<span style="color:#ff0000">' . $error . '</span>', $pos, String::length($error));
    $message = String::substr($message, $start, $length + 28);

    return $message;
}

/* Fetch and clean form data.  These are spelling.php specific form variables
 * only. */
$f_opt = Util::getFormData('opt');
$f_subs = Util::getFormData('subs');
$f_oldword = Util::getFormData('oldword');
$f_subtext = Util::getFormData('subtext');
$f_wordoffset = Util::getFormData('wordoffset');
$f_oldmsg = Util::getFormData('oldmsg', Util::getFormData('message'));
$f_currmsg = Util::getFormData('currmsg', $f_oldmsg);
$f_newmsg = Util::getFormData('newmsg');
$f_done_action = Util::getFormData('done_action');

/* Get from data from compose.php input that is used locally in
 * spelling.php. */
$f_rtemode = Util::getFormData('rtemode');

/* $f_ignoreall is an array - we need to unserialize the data. */
$ignoreall = array();
if (($f_ignoreall = Util::getFormData('ignoreall'))) {
    $f_ignoreall = unserialize($f_ignoreall);
} else {
    $f_ignoreall = array();
}

switch ($actionID) {
case 'spell_check_forward':
    for ($i = 0; $i < count($f_opt); $i++) {
        $skipword = false;

        /* If they have an word with no suggestions and they
           dont type in a replacement, ignore it. */
        if (!empty($f_subtext[$i])) {
            $replacement = $f_subtext[$i];
        } else {
            if ($f_subs[$i] == '0') {
                $ignoreall = $f_ignoreall;
                $ignoreall[] = String::lower($f_oldword[$i], true);
                $skipword = true;
            } else {
                $replacement = $f_subs[$i];
            }
        }

        if (!$skipword) {
            $pos = -1;

            $realoffset = $f_wordoffset[$i];

            /* Just in case things are whackily out. */
            $msg_length = String::length($f_currmsg);
            if ($realoffset > $msg_length) {
                $realoffset = $msg_length - 1;
            }

            $pos = _findOffset($f_currmsg, $f_oldword[$i], $realoffset);

            switch ($f_opt[$i]) {
            case IMP_SPELL_IGNORE:
                $consume = $pos + String::length($f_oldword[$i]);
                $addition = String::substr($f_currmsg, 0, $consume);
                $f_newmsg .= $addition;
                $f_currmsg = String::substr($f_currmsg, $consume);

                /* Adjust offsets, as they could be wildly out */
                for ($msgnum = 0; $msgnum < count($f_wordoffset); $msgnum++) {
                    $f_wordoffset[$msgnum] -= $consume;
                }
                break;

            case IMP_SPELL_IGNORE_ALL:
                $ignoreall = $f_ignoreall;
                $ignoreall[] = String::lower($f_oldword[$i], true);
                break;

            case IMP_SPELL_CHANGE_ALL:
                $f_currmsg = str_replace($f_oldword[$i], $replacement, $f_currmsg);
                break;

            case IMP_SPELL_CHANGE:
                if (!in_array(String::lower($f_oldword[$i], true), $f_ignoreall)) {
                    /* Let's try and keep those offsets semi correct. */
                    $adjoffset = String::length($replacement) - String::length($f_oldword[$i]);
                    for ($msgnum = 0; $msgnum < count($f_wordoffset); $msgnum++) {
                        $f_wordoffset[$msgnum] += $adjoffset;
                    }

                    $tempmessage  = String::substr($f_currmsg, 0, $pos);
                    $tempmessage .= $replacement;
                    $tempmessage .= String::substr($f_currmsg, $pos + String::length($f_oldword[$i]));
                    $f_currmsg = $tempmessage;
                }
                break;
            }
        }
    }
    break;

case 'spell_check_send':
    $f_done_action = 'send';
    /* FALLTHROUGH */

default:
    $f_message = $f_currmsg;

    /* Have to start another wordlist to incorporate into the spell
       check dictionary methinks. */
    $ignoreall = $ignore_list;
    break;
}

/* Special treatment depending on language (quotes are not equally treated
   by ispell in english and in french). */
switch ($language) {
case 'fr_FR':
    $tocheck = str_replace("'", "\\'", escapeShellCmd($f_currmsg));
    break;

default:
    $tocheck = $f_currmsg;
    break;
}

$spellchecker = $conf['utils']['spellchecker'];

/* Protect against lines beginning with "special" characters as defined at
   http://aspell.net/man-html/Through-A-Pipe.html#Through%20A%20Pipe */
if (strpos($spellchecker, 'aspell') !== false) {
    $tocheck = '^' . str_replace("\n", "\n^", $tocheck);
}

/* Save the message to a temporary file. */
$spellFile = Horde::getTempFile('spell');
$fp = fopen($spellFile, 'w');
fwrite($fp, $tocheck);
fclose($fp);

/* Run the actual spell check. */
if (empty($spellchecker)) {
    $notification->push(_("No spellchecking program configured."), 'horde.error');
} else {
    /* Retrieve any spelling options. */
    $spell_opt = '';
    if (isset($nls['spelling'][$language])) {
        $spell_opt = $nls['spelling'][$language];
    }
    if (strpos($spellchecker, 'aspell') != false) {
        $spell_opt .= ' -e';
    }
    if ($f_rtemode) {
        // Try to use the Spellchecker's HTML mode
        if (strpos($spellchecker, 'ispell') !== false) {
            $spell_opt .= ' -h';
        } elseif (strpos($spellchecker, 'aspell') !== false) {
            $spell_opt .= ' -H';
        }
    }

    exec(escapeshellcmd($spellchecker . ' -a ' . $spell_opt) . ' < ' . $spellFile, $warnings);
}

$msg = '';
for ($i = 0; $i < count($warnings); $i++) {
    if (substr($warnings[$i], 0, 1) == '&') {
        $parts = explode(': ', $warnings[$i]);
        $info = explode(' ', $parts[0]);
        if (preg_match('|^[A-Z]*$|', $info[1])) {
            $ignoreall[] = String::lower($info[1], true);
        } else {
            $error[] = array($info[1], $info[3], $parts[1]);
        }
    }
    if (preg_match('|^#|', $warnings[$i])) {
        $info = explode(' ', $warnings[$i]);
        if (preg_match('|^[A-Z]*$|', $info[1])) {
            $ignoreall[] = String::lower($info[1], true);
        } else {
            $error[] = array($info[1], $info[2], '');
        }
    }
}

/* Generate a pretty word-wrapped version for displaying. */
$display_msg = $f_message = "\n" . $f_newmsg . $f_currmsg;
if (!$f_rtemode) {
    $display_msg = htmlspecialchars($display_msg);
    $display_msg = String::wrap($display_msg, 80, "\n", NLS::getCharset(), true);
}
if ($browser->hasQuirk('double_linebreak_textarea')) {
    $display_msg = preg_replace('/(\r?\n){3}/', '$1', $display_msg);
}

/* Alter the "Done" behavior if we're sending the message after spell check. */
if ($f_done_action === 'send') {
    $spell_check_done_action = 'send_message';
    $spell_check_done_caption = _("(Send message after applying all changes made thus far.  Changes on the current screen will NOT be applied.)");
} else {
    $spell_check_done_action = 'spell_check_done';
    $spell_check_done_caption = _("(Return to the compose screen after applying all changes made thus far.  Changes on the current screen will NOT be applied.)");
}