]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/utils/spell_pspell.inc
Imported Upstream version 0.5.4+dfsg
[roundcube.git] / program / steps / utils / spell_pspell.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/utils/spell_pspell.inc                                  |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Licensed under the GNU GPL                                            |
9  |                                                                       |
10  | PURPOSE:                                                              |
11  |   Use the Pspell extension to check spelling, returns results         |
12  |   compatible with spell_googie.inc.                                   |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Kris Steinhoff <steinhof@umich.edu>                           |
16  +-----------------------------------------------------------------------+
17
18  $Id: spell_pspell.inc 3989 2010-09-25 13:03:53Z alec $
19
20 */
21
22 if (!extension_loaded('pspell')) {
23     raise_error(array(
24       'code' => 500,
25       'type' => 'php',
26       'file' => __FILE__, 'line' => __LINE__,
27       'message' => "Pspell extension not available"), true, false);
28
29     header('HTTP/1.1 404 Not Found');
30     exit;
31 }
32
33 // read input
34 $data = file_get_contents('php://input');
35
36 // parse data (simplexml_load_string breaks CRLFs)
37 $left = strpos($data, '<text>');
38 $right = strrpos($data, '</text>');
39 $text = substr($data, $left+6, $right-($left+6));
40 $text = html_entity_decode($text, ENT_QUOTES, RCMAIL_CHARSET);
41
42 // tokenize
43 $words = preg_split('/[ !"#$%&()*+\\,\/\n:;<=>?@\[\]^_{|}-]+|\.[^\w]/', $text, NULL,  PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE );
44
45 // init spellchecker
46 $plink = pspell_new(get_input_value('lang', RCUBE_INPUT_GET), null, null, RCMAIL_CHARSET, PSPELL_FAST);
47
48 // send output
49 $out = '<?xml version="1.0" encoding="'.RCMAIL_CHARSET.'"?><spellresult charschecked="'.mb_strlen($text).'">';
50
51 $diff = 0;
52 foreach ($words as $w) {
53     $word = trim($w[0]);
54     $pos  = $w[1] - $diff;
55     $len  = mb_strlen($word);
56     if ($word && $plink && preg_match('/[^0-9\.]/', $word)
57         && !pspell_check($plink, $word)) {
58         $suggestions = pspell_suggest($plink, $word);
59         if (sizeof($suggestions)>MAX_SUGGESTIONS)
60           $suggestions = array_slice($suggestions, 0, MAX_SUGGESTIONS);
61
62         $out .= '<c o="'.$pos.'" l="'.$len.'">';
63         $out .= implode("\t", $suggestions);
64         $out .= '</c>';
65     }
66     $diff += (strlen($word) - $len);
67 }
68
69 $out .= '</spellresult>';
70
71 header("Content-Type: text/xml; charset=".RCMAIL_CHARSET);
72 echo $out;
73 exit;
74
75