You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.6 KiB
PHTML
79 lines
2.6 KiB
PHTML
17 years ago
|
<?php
|
||
|
|
||
|
/*
|
||
|
+-----------------------------------------------------------------------+
|
||
15 years ago
|
| program/steps/utils/spell_pspell.inc |
|
||
17 years ago
|
| |
|
||
|
| This file is part of the RoundCube Webmail client |
|
||
|
| Licensed under the GNU GPL |
|
||
|
| |
|
||
|
| PURPOSE: |
|
||
|
| Use the Pspell extension to check spelling, returns results |
|
||
|
| compatible with spell_googie.inc. |
|
||
|
| |
|
||
|
+-----------------------------------------------------------------------+
|
||
|
| Author: Kris Steinhoff <steinhof@umich.edu> |
|
||
|
+-----------------------------------------------------------------------+
|
||
|
|
||
|
$Id$
|
||
|
|
||
|
*/
|
||
|
|
||
|
if (!extension_loaded('pspell')) {
|
||
|
raise_error(array(
|
||
|
'code' => 500,
|
||
16 years ago
|
'type' => 'php',
|
||
15 years ago
|
'file' => __FILE__, 'line' => __LINE__,
|
||
17 years ago
|
'message' => "Pspell extension not available"), true, false);
|
||
15 years ago
|
|
||
17 years ago
|
header('HTTP/1.1 404 Not Found');
|
||
|
exit;
|
||
|
}
|
||
|
|
||
16 years ago
|
// max. number of suggestions for one word
|
||
|
define('MAX_SUGGESTIONS', 10);
|
||
|
|
||
16 years ago
|
// read input
|
||
17 years ago
|
$data = file_get_contents('php://input');
|
||
16 years ago
|
|
||
|
// parse data (simplexml_load_string breaks CRLFs)
|
||
|
$left = strpos($data, '<text>');
|
||
|
$right = strrpos($data, '</text>');
|
||
|
$text = substr($data, $left+6, $right-($left+6));
|
||
16 years ago
|
$text = html_entity_decode($text, ENT_QUOTES, RCMAIL_CHARSET);
|
||
16 years ago
|
|
||
|
// tokenize
|
||
15 years ago
|
$words = preg_split('/[ !"#$%&()*+\\,\/\n:;<=>?@\[\]^_{|}-]+|\.[^\w]/', $text, NULL, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE );
|
||
16 years ago
|
|
||
|
// init spellchecker
|
||
16 years ago
|
$plink = pspell_new(get_input_value('lang', RCUBE_INPUT_GET), null, null, RCMAIL_CHARSET, PSPELL_FAST);
|
||
16 years ago
|
|
||
|
// send output
|
||
16 years ago
|
$out = '<?xml version="1.0" encoding="'.RCMAIL_CHARSET.'"?><spellresult charschecked="'.mb_strlen($text).'">';
|
||
16 years ago
|
|
||
|
$diff = 0;
|
||
17 years ago
|
foreach ($words as $w) {
|
||
16 years ago
|
$word = trim($w[0]);
|
||
16 years ago
|
$pos = $w[1] - $diff;
|
||
16 years ago
|
$len = mb_strlen($word);
|
||
15 years ago
|
if ($word && $plink && preg_match('/[^0-9\.]/', $word)
|
||
|
&& !pspell_check($plink, $word)) {
|
||
17 years ago
|
$suggestions = pspell_suggest($plink, $word);
|
||
16 years ago
|
if (sizeof($suggestions)>10)
|
||
|
$suggestions = array_slice($suggestions, 0, MAX_SUGGESTIONS);
|
||
|
|
||
17 years ago
|
$out .= '<c o="'.$pos.'" l="'.$len.'">';
|
||
|
$out .= implode("\t", $suggestions);
|
||
|
$out .= '</c>';
|
||
|
}
|
||
16 years ago
|
$diff += (strlen($word) - $len);
|
||
17 years ago
|
}
|
||
16 years ago
|
|
||
17 years ago
|
$out .= '</spellresult>';
|
||
|
|
||
15 years ago
|
header("Content-Type: text/xml; charset=".RCMAIL_CHARSET);
|
||
17 years ago
|
echo $out;
|
||
|
exit;
|
||
|
|
||
|
?>
|