| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/steps/utils/spell_html_googie.inc | |
|---|
| 6 | | | |
|---|
| 7 | | This file is part of the Roundcube Webmail client | |
|---|
| 8 | | Copyright (C) 2005-2010, The Roundcube Dev Team | |
|---|
| 9 | | Licensed under the GNU GPL | |
|---|
| 10 | | | |
|---|
| 11 | | PURPOSE: | |
|---|
| 12 | | Submit request to Google's spell checking engine | |
|---|
| 13 | | | |
|---|
| 14 | +-----------------------------------------------------------------------+ |
|---|
| 15 | | Author: Aleksander Machniak <alec@alec.pl> | |
|---|
| 16 | +-----------------------------------------------------------------------+ |
|---|
| 17 | |
|---|
| 18 | $Id: spell_googie.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 | function googie_get($host, $port, $path, $data) |
|---|
| 29 | { |
|---|
| 30 | $store = ''; |
|---|
| 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"; |
|---|
| 37 | $out .= $data; |
|---|
| 38 | fwrite($fp, $out); |
|---|
| 39 | |
|---|
| 40 | while (!feof($fp)) |
|---|
| 41 | $store .= fgets($fp, 128); |
|---|
| 42 | fclose($fp); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | if (!$store) { |
|---|
| 46 | json_error("Empty result from spelling engine"); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | $matches = array(); |
|---|
| 50 | preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $store, $matches, PREG_SET_ORDER); |
|---|
| 51 | |
|---|
| 52 | return $matches; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | $REMOTE_REQUEST = TRUE; |
|---|
| 56 | |
|---|
| 57 | // read input |
|---|
| 58 | $data = file_get_contents('php://input'); |
|---|
| 59 | |
|---|
| 60 | // Decode JSON input |
|---|
| 61 | $request = json_decode($data, true); |
|---|
| 62 | $result = array(); |
|---|
| 63 | |
|---|
| 64 | $lang = $request['params'][0]; |
|---|
| 65 | $data = $request['params'][1]; |
|---|
| 66 | $result['id'] = $request['id']; |
|---|
| 67 | |
|---|
| 68 | // default settings |
|---|
| 69 | $host = "ssl://www.google.com"; |
|---|
| 70 | $port = 443; |
|---|
| 71 | $path = "/tbproxy/spell?lang=$lang"; |
|---|
| 72 | |
|---|
| 73 | // spell check uri is configured |
|---|
| 74 | if (!empty($CONFIG['spellcheck_uri'])) |
|---|
| 75 | { |
|---|
| 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; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 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>' |
|---|
| 87 | .'</spellrequest>'; |
|---|
| 88 | $matches = googie_get($host, $port, $path, $data); |
|---|
| 89 | |
|---|
| 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); |
|---|
| 94 | } |
|---|
| 95 | else if ($request['method'] == 'getSuggestions') { |
|---|
| 96 | if ($matches[0][4]) { |
|---|
| 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; |
|---|
| 101 | } |
|---|
| 102 | else |
|---|
| 103 | $result['result'] = array(); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | // send output |
|---|
| 107 | header("Content-Type: text/xml; charset=".RCMAIL_CHARSET); |
|---|
| 108 | echo json_encode($result); |
|---|
| 109 | exit; |
|---|
| 110 | |
|---|