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_atd.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | |
  6. | Copyright (C) 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 for afterthedeadline services |
  14. +-----------------------------------------------------------------------+
  15. | Author: Thomas Bruederli <roundcube@gmail.com> |
  16. +-----------------------------------------------------------------------+
  17. */
  18. /**
  19. * Spellchecking backend implementation to work with an After the Deadline service
  20. * See http://www.afterthedeadline.com/ for more information
  21. *
  22. * @package Framework
  23. * @subpackage Utils
  24. */
  25. class rcube_spellcheck_atd extends rcube_spellcheck_engine
  26. {
  27. const SERVICE_HOST = 'service.afterthedeadline.com';
  28. const SERVICE_PORT = 80;
  29. private $matches = array();
  30. private $content;
  31. private $langhosts = array(
  32. 'fr' => 'fr.',
  33. 'de' => 'de.',
  34. 'pt' => 'pt.',
  35. 'es' => 'es.',
  36. );
  37. /**
  38. * Return a list of languages supported by this backend
  39. *
  40. * @see rcube_spellcheck_engine::languages()
  41. */
  42. function languages()
  43. {
  44. $langs = array_values($this->langhosts);
  45. $langs[] = 'en';
  46. return $langs;
  47. }
  48. /**
  49. * Set content and check spelling
  50. *
  51. * @see rcube_spellcheck_engine::check()
  52. */
  53. function check($text)
  54. {
  55. $this->content = $text;
  56. // spell check uri is configured
  57. $rcube = rcube::get_instance();
  58. $url = $rcube->config->get('spellcheck_uri');
  59. $key = $rcube->config->get('spellcheck_atd_key');
  60. if ($url) {
  61. $a_uri = parse_url($url);
  62. $ssl = ($a_uri['scheme'] == 'https' || $a_uri['scheme'] == 'ssl');
  63. $port = $a_uri['port'] ?: ($ssl ? 443 : 80);
  64. $host = ($ssl ? 'ssl://' : '') . $a_uri['host'];
  65. $path = $a_uri['path'] . ($a_uri['query'] ? '?'.$a_uri['query'] : '') . $this->lang;
  66. }
  67. else {
  68. $host = self::SERVICE_HOST;
  69. $port = self::SERVICE_PORT;
  70. $path = '/checkDocument';
  71. // prefix host for other languages than 'en'
  72. $lang = substr($this->lang, 0, 2);
  73. if ($this->langhosts[$lang])
  74. $host = $this->langhosts[$lang] . $host;
  75. }
  76. $postdata = 'data=' . urlencode($text);
  77. if (!empty($key))
  78. $postdata .= '&key=' . urlencode($key);
  79. $response = $headers = '';
  80. $in_header = true;
  81. if ($fp = fsockopen($host, $port, $errno, $errstr, 30)) {
  82. $out = "POST $path HTTP/1.0\r\n";
  83. $out .= "Host: " . str_replace('ssl://', '', $host) . "\r\n";
  84. $out .= "Content-Length: " . strlen($postdata) . "\r\n";
  85. $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  86. $out .= "Connection: Close\r\n\r\n";
  87. $out .= $postdata;
  88. fwrite($fp, $out);
  89. while (!feof($fp)) {
  90. if ($in_header) {
  91. $line = fgets($fp, 512);
  92. $headers .= $line;
  93. if (trim($line) == '')
  94. $in_header = false;
  95. }
  96. else {
  97. $response .= fgets($fp, 1024);
  98. }
  99. }
  100. fclose($fp);
  101. }
  102. // parse HTTP response headers
  103. if (preg_match('!^HTTP/1.\d (\d+)(.+)!', $headers, $m)) {
  104. $http_status = $m[1];
  105. if ($http_status != '200')
  106. $this->error = 'HTTP ' . $m[1] . $m[2];
  107. }
  108. if (!$response) {
  109. $this->error = "Empty result from spelling engine";
  110. }
  111. try {
  112. $result = new SimpleXMLElement($response);
  113. }
  114. catch (Exception $e) {
  115. $this->error = "Unexpected response from server: " . $response;
  116. return array();
  117. }
  118. foreach ($result->error as $error) {
  119. if (strval($error->type) == 'spelling') {
  120. $word = strval($error->string);
  121. // skip exceptions
  122. if ($this->dictionary->is_exception($word)) {
  123. continue;
  124. }
  125. $prefix = strval($error->precontext);
  126. $start = $prefix ? mb_strpos($text, $prefix) : 0;
  127. $pos = mb_strpos($text, $word, $start);
  128. $len = mb_strlen($word);
  129. $num = 0;
  130. $match = array($word, $pos, $len, null, array());
  131. foreach ($error->suggestions->option as $option) {
  132. $match[4][] = strval($option);
  133. if (++$num == self::MAX_SUGGESTIONS)
  134. break;
  135. }
  136. $matches[] = $match;
  137. }
  138. }
  139. $this->matches = $matches;
  140. return $matches;
  141. }
  142. /**
  143. * Returns suggestions for the specified word
  144. *
  145. * @see rcube_spellcheck_engine::get_words()
  146. */
  147. function get_suggestions($word)
  148. {
  149. $matches = $word ? $this->check($word) : $this->matches;
  150. if ($matches[0][4]) {
  151. return $matches[0][4];
  152. }
  153. return array();
  154. }
  155. /**
  156. * Returns misspelled words
  157. *
  158. * @see rcube_spellcheck_engine::get_suggestions()
  159. */
  160. function get_words($text = null)
  161. {
  162. if ($text) {
  163. $matches = $this->check($text);
  164. }
  165. else {
  166. $matches = $this->matches;
  167. $text = $this->content;
  168. }
  169. $result = array();
  170. foreach ($matches as $m) {
  171. $result[] = mb_substr($text, $m[1], $m[2], RCUBE_CHARSET);
  172. }
  173. return $result;
  174. }
  175. }