4 +-----------------------------------------------------------------------+
5 | program/steps/utils/spell_html_googie.inc |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2005-2010, Roundcube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Submit request to Google's spell checking engine |
14 +-----------------------------------------------------------------------+
15 | Author: Aleksander Machniak <alec@alec.pl> |
16 +-----------------------------------------------------------------------+
18 $Id: spell_googie.inc 3780 2010-06-23 09:55:08Z alec $
22 function json_error($str)
24 echo '{"error":{"errstr":"' . addslashes($str) . '","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}';
28 function googie_get($host, $port, $path, $data)
31 if ($fp = fsockopen($host, $port, $errno, $errstr, 30)) {
32 $out = "POST $path HTTP/1.0\r\n";
33 $out .= "Host: " . str_replace('ssl://', '', $host) . "\r\n";
34 $out .= "Content-Length: " . strlen($data) . "\r\n";
35 $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
36 $out .= "Connection: Close\r\n\r\n";
41 $store .= fgets($fp, 128);
46 json_error("Empty result from spelling engine");
50 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $store, $matches, PREG_SET_ORDER);
55 $REMOTE_REQUEST = TRUE;
58 $data = file_get_contents('php://input');
61 $request = json_decode($data, true);
64 $lang = $request['params'][0];
65 $data = $request['params'][1];
66 $result['id'] = $request['id'];
69 $host = "ssl://www.google.com";
71 $path = "/tbproxy/spell?lang=$lang";
73 // spell check uri is configured
74 if (!empty($CONFIG['spellcheck_uri']))
76 $a_uri = parse_url($CONFIG['spellcheck_uri']);
77 $ssl = ($a_uri['scheme']=='https' || $a_uri['scheme']=='ssl');
78 $port = $a_uri['port'] ? $a_uri['port'] : ($ssl ? 443 : 80);
79 $host = ($ssl ? 'ssl://' : '') . $a_uri['host'];
80 $path = $a_uri['path'] . ($a_uri['query'] ? '?'.$a_uri['query'] : '') . $lang;
83 $wordstr = implode("\n", (array) $data);
84 $data = '<?xml version="1.0" encoding="utf-8" ?>'
85 .'<spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1">'
86 .'<text>' . $wordstr . '</text>'
88 $matches = googie_get($host, $port, $path, $data);
90 if ($request['method'] == 'checkWords') {
91 $result['result'] = array();
92 for ($i=0, $len=count($matches); $i<$len; $i++)
93 $result['result'][] = mb_substr($wordstr, $matches[$i][1], $matches[$i][2], RCMAIL_CHARSET);
95 else if ($request['method'] == 'getSuggestions') {
97 $suggestions = explode("\t", $matches[0][4]);
98 if (sizeof($suggestions)>MAX_SUGGESTIONS)
99 $suggestions = array_slice($suggestions, 0, MAX_SUGGESTIONS);
100 $result['result'] = $suggestions;
103 $result['result'] = array();
107 header("Content-Type: text/xml; charset=".RCMAIL_CHARSET);
108 echo json_encode($result);