]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/spell_pspell.inc
Imported Upstream version 0.2~stable
[roundcube.git] / program / steps / mail / spell_pspell.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/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$
19
20 */
21
22 if (!extension_loaded('pspell')) {
23     raise_error(array(
24       'code' => 500,
25       'file' => __FILE__,
26       'message' => "Pspell extension not available"), true, false);
27       
28     header('HTTP/1.1 404 Not Found');
29     exit;
30 }
31
32 // read input
33 $data = file_get_contents('php://input');
34
35 // parse data (simplexml_load_string breaks CRLFs)
36 $left = strpos($data, '<text>');
37 $right = strrpos($data, '</text>');
38 $text = substr($data, $left+6, $right-($left+6));
39
40 // tokenize
41 $words = preg_split('/[ !"#$%&()*+\\,-.\/\n:;<=>?@\[\]^_{|}]+/', $text, NULL,  PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE );
42
43 // init spellchecker
44 $plink = pspell_new(get_input_value('lang', RCUBE_INPUT_GET), null, null, 'utf-8', PSPELL_FAST);
45
46 // send output
47 $out = '<?xml version="1.0" encoding="UTF-8"?><spellresult charschecked="'.rc_strlen($text).'">';
48
49 $diff = 0;
50 foreach ($words as $w) {
51     $word = trim($w[0]);
52     $pos  = $w[1] - $diff;
53     $len  = rc_strlen($word);
54     if ($word && $plink && !pspell_check($plink, $word)) {
55         $suggestions = pspell_suggest($plink, $word);
56         $out .= '<c o="'.$pos.'" l="'.$len.'">';
57         $out .= implode("\t", $suggestions);
58         $out .= '</c>';
59     }
60     $diff += (strlen($word) - $len);
61 }
62
63 $out .= '</spellresult>';
64
65 header("Content-Type: text/xml");
66 echo $out;
67 exit;
68
69 ?>