comparison program/lib/Roundcube/rcube_spellcheck_googie.php @ 0:4681f974d28b

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:52:31 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4681f974d28b
1 <?php
2
3 /**
4 +-----------------------------------------------------------------------+
5 | This file is part of the Roundcube Webmail client |
6 | |
7 | Copyright (C) 2008-2013, The Roundcube Dev Team |
8 | |
9 | Licensed under the GNU General Public License version 3 or |
10 | any later version with exceptions for skins & plugins. |
11 | See the README file for a full license statement. |
12 | |
13 | PURPOSE: |
14 | Spellchecking backend implementation to work with Googiespell |
15 +-----------------------------------------------------------------------+
16 | Author: Aleksander Machniak <machniak@kolabsys.com> |
17 | Author: Thomas Bruederli <roundcube@gmail.com> |
18 +-----------------------------------------------------------------------+
19 */
20
21 /**
22 * Spellchecking backend implementation to work with a Googiespell service
23 *
24 * @package Framework
25 * @subpackage Utils
26 */
27 class rcube_spellcheck_googie extends rcube_spellcheck_engine
28 {
29 const GOOGIE_HOST = 'ssl://spell.roundcube.net';
30 const GOOGIE_PORT = 443;
31
32 private $matches = array();
33 private $content;
34
35 /**
36 * Return a list of languages supported by this backend
37 *
38 * @see rcube_spellcheck_engine::languages()
39 */
40 function languages()
41 {
42 return array('am','ar','ar','bg','br','ca','cs','cy','da',
43 'de_CH','de_DE','el','en_GB','en_US',
44 'eo','es','et','eu','fa','fi','fr_FR','ga','gl','gl',
45 'he','hr','hu','hy','is','it','ku','lt','lv','nl',
46 'pl','pt_BR','pt_PT','ro','ru',
47 'sk','sl','sv','uk');
48 }
49
50 /**
51 * Set content and check spelling
52 *
53 * @see rcube_spellcheck_engine::check()
54 */
55 function check($text)
56 {
57 $this->content = $text;
58
59 if (empty($text)) {
60 return $this->matches = array();
61 }
62
63 // spell check uri is configured
64 $url = rcube::get_instance()->config->get('spellcheck_uri');
65
66 if ($url) {
67 $a_uri = parse_url($url);
68 $ssl = ($a_uri['scheme'] == 'https' || $a_uri['scheme'] == 'ssl');
69 $port = $a_uri['port'] ? $a_uri['port'] : ($ssl ? 443 : 80);
70 $host = ($ssl ? 'ssl://' : '') . $a_uri['host'];
71 $path = $a_uri['path'] . ($a_uri['query'] ? '?'.$a_uri['query'] : '') . $this->lang;
72 }
73 else {
74 $host = self::GOOGIE_HOST;
75 $port = self::GOOGIE_PORT;
76 $path = '/tbproxy/spell?lang=' . $this->lang;
77 }
78
79 $path .= sprintf('&key=%06d', $_SESSION['user_id']);
80
81 $gtext = '<?xml version="1.0" encoding="utf-8" ?>'
82 .'<spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1">'
83 .'<text>' . htmlspecialchars($text, ENT_QUOTES, RCUBE_CHARSET) . '</text>'
84 .'</spellrequest>';
85
86 $store = '';
87 if ($fp = fsockopen($host, $port, $errno, $errstr, 30)) {
88 $out = "POST $path HTTP/1.0\r\n";
89 $out .= "Host: " . str_replace('ssl://', '', $host) . "\r\n";
90 $out .= "User-Agent: Roundcube Webmail/" . RCUBE_VERSION . " (Googiespell Wrapper)\r\n";
91 $out .= "Content-Length: " . strlen($gtext) . "\r\n";
92 $out .= "Content-Type: text/xml\r\n";
93 $out .= "Connection: Close\r\n\r\n";
94 $out .= $gtext;
95 fwrite($fp, $out);
96
97 while (!feof($fp))
98 $store .= fgets($fp, 128);
99 fclose($fp);
100 }
101
102 // parse HTTP response
103 if (preg_match('!^HTTP/1.\d (\d+)(.+)!', $store, $m)) {
104 $http_status = $m[1];
105 if ($http_status != '200') {
106 $this->error = 'HTTP ' . $m[1] . rtrim($m[2]);
107 }
108 }
109
110 if (!$store) {
111 $this->error = "Empty result from spelling engine";
112 }
113 else if (preg_match('/<spellresult error="([^"]+)"/', $store, $m) && $m[1]) {
114 $this->error = "Error code $m[1] returned";
115 $this->error .= preg_match('/<errortext>([^<]+)/', $store, $m) ? ": " . html_entity_decode($m[1]) : '';
116 }
117
118 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $store, $matches, PREG_SET_ORDER);
119
120 // skip exceptions (if appropriate options are enabled)
121 foreach ($matches as $idx => $m) {
122 $word = mb_substr($text, $m[1], $m[2], RCUBE_CHARSET);
123 // skip exceptions
124 if ($this->dictionary->is_exception($word)) {
125 unset($matches[$idx]);
126 }
127 }
128
129 $this->matches = $matches;
130 return $matches;
131 }
132
133 /**
134 * Returns suggestions for the specified word
135 *
136 * @see rcube_spellcheck_engine::get_words()
137 */
138 function get_suggestions($word)
139 {
140 $matches = $word ? $this->check($word) : $this->matches;
141
142 if ($matches[0][4]) {
143 $suggestions = explode("\t", $matches[0][4]);
144 if (count($suggestions) > self::MAX_SUGGESTIONS) {
145 $suggestions = array_slice($suggestions, 0, self::MAX_SUGGESTIONS);
146 }
147
148 return $suggestions;
149 }
150
151 return array();
152 }
153
154 /**
155 * Returns misspelled words
156 *
157 * @see rcube_spellcheck_engine::get_suggestions()
158 */
159 function get_words($text = null)
160 {
161 if ($text) {
162 $matches = $this->check($text);
163 }
164 else {
165 $matches = $this->matches;
166 $text = $this->content;
167 }
168
169 $result = array();
170
171 foreach ($matches as $m) {
172 $result[] = mb_substr($text, $m[1], $m[2], RCUBE_CHARSET);
173 }
174
175 return $result;
176 }
177 }