]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/spell_pspell.inc
Imported Upstream version 0.3.1
[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       'type' => 'php',
26       'file' => __FILE__,
27       'message' => "Pspell extension not available"), true, false);
28       
29     header('HTTP/1.1 404 Not Found');
30     exit;
31 }
32
33 // max. number of suggestions for one word
34 define('MAX_SUGGESTIONS', 10);
35
36 // read input
37 $data = file_get_contents('php://input');
38
39 // parse data (simplexml_load_string breaks CRLFs)
40 $left = strpos($data, '<text>');
41 $right = strrpos($data, '</text>');
42 $text = substr($data, $left+6, $right-($left+6));
43 $text = html_entity_decode($text, ENT_QUOTES, RCMAIL_CHARSET);
44
45 // tokenize
46 $words = preg_split('/[ !"#$%&()*+\\,\/\n:;<=>?@\[\]^_{|}-]+|\.[^\w]/', $text, NULL,  PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE );
47
48 // init spellchecker
49 $plink = pspell_new(get_input_value('lang', RCUBE_INPUT_GET), null, null, RCMAIL_CHARSET, PSPELL_FAST);
50
51 // send output
52 $out = '<?xml version="1.0" encoding="'.RCMAIL_CHARSET.'"?><spellresult charschecked="'.mb_strlen($text).'">';
53
54 $diff = 0;
55 foreach ($words as $w) {
56     $word = trim($w[0]);
57     $pos  = $w[1] - $diff;
58     $len  = mb_strlen($word);
59     if ($word && $plink && preg_match('/[^0-9\.]/', $word)
60         && !pspell_check($plink, $word)) {
61         $suggestions = pspell_suggest($plink, $word);
62         if (sizeof($suggestions)>10)
63           $suggestions = array_slice($suggestions, 0, MAX_SUGGESTIONS);
64
65         $out .= '<c o="'.$pos.'" l="'.$len.'">';
66         $out .= implode("\t", $suggestions);
67         $out .= '</c>';
68     }
69     $diff += (strlen($word) - $len);
70 }
71
72 $out .= '</spellresult>';
73
74 header("Content-Type: text/xml");
75 echo $out;
76 exit;
77
78 ?>