You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcube_spellcheck_googie.php 5.9KB

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