]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/utils/spell_html_pspell.inc
Imported Upstream version 0.5.4+dfsg
[roundcube.git] / program / steps / utils / spell_html_pspell.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/utils/spell_pspell_tiny.inc                             |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2010, Roundcube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Use the Pspell extension to check spelling in TinyMCE               |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Aleksander Machniak <alec@alec.pl>                            |
16  +-----------------------------------------------------------------------+
17
18  $Id: spell_pspell.inc 3780 2010-06-23 09:55:08Z alec $
19
20 */
21
22 function json_error($str)
23 {
24     echo '{"error":{"errstr":"' . addslashes($str) . '","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}';
25     exit;
26 }
27
28 if (!extension_loaded('pspell')) {
29     raise_error(array(
30       'code' => 500,
31       'type' => 'php',
32       'file' => __FILE__, 'line' => __LINE__,
33       'message' => "Pspell extension not available"), true, false);
34
35     json_error("Pspell extension not available");
36 }
37
38 // read input
39 $data = file_get_contents('php://input');
40
41 // Decode JSON input
42 $request = json_decode($data, true);
43 $result = array();
44
45 $lang = $request['params'][0];
46 $data = $request['params'][1];
47 $result['id'] = $request['id'];
48
49 // init spellchecker
50 $plink = pspell_new($lang, null, null, RCMAIL_CHARSET, PSPELL_FAST);
51
52 if (!$plink) {
53     json_error("Unable to load Pspell engine for selected language");
54 }
55
56 if ($request['method'] == 'checkWords') {
57     $result['result'] = array();
58     foreach ((array)$data as $word) {
59         if ($word && preg_match('/[^0-9\.]/', $word)
60                 && !pspell_check($plink, $word)) {
61             $result['result'][] = $word;
62         }
63     }
64 }
65 else if ($request['method'] == 'getSuggestions') {
66     $suggestions = pspell_suggest($plink, $data);
67     if (sizeof($suggestions)>MAX_SUGGESTIONS)
68         $suggestions = array_slice($suggestions, 0, MAX_SUGGESTIONS);
69     $result['result'] = $suggestions;
70 }
71
72 // send output
73 header("Content-Type: text/xml; charset=".RCMAIL_CHARSET);
74 echo json_encode($result);
75 exit;
76