PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/etc/apps/webmail/program/lib/Roundcube/rcube_spellcheck_googie.php

https://github.com/raiman264/zpanelx
PHP | 176 lines | 103 code | 26 blank | 47 comment | 19 complexity | 109ec165e88e52e2facbea74137ce84c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, LGPL-2.1, CC-BY-SA-4.0, GPL-3.0
  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. // spell check uri is configured
  54. $url = rcube::get_instance()->config->get('spellcheck_uri');
  55. if ($url) {
  56. $a_uri = parse_url($url);
  57. $ssl = ($a_uri['scheme'] == 'https' || $a_uri['scheme'] == 'ssl');
  58. $port = $a_uri['port'] ? $a_uri['port'] : ($ssl ? 443 : 80);
  59. $host = ($ssl ? 'ssl://' : '') . $a_uri['host'];
  60. $path = $a_uri['path'] . ($a_uri['query'] ? '?'.$a_uri['query'] : '') . $this->lang;
  61. }
  62. else {
  63. $host = self::GOOGIE_HOST;
  64. $port = self::GOOGIE_PORT;
  65. $path = '/tbproxy/spell?lang=' . $this->lang;
  66. }
  67. $path .= sprintf('&key=%06d', $_SESSION['user_id']);
  68. $gtext = '<?xml version="1.0" encoding="utf-8" ?>'
  69. .'<spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1">'
  70. .'<text>' . htmlspecialchars($text, ENT_QUOTES, RCUBE_CHARSET) . '</text>'
  71. .'</spellrequest>';
  72. $store = '';
  73. if ($fp = fsockopen($host, $port, $errno, $errstr, 30)) {
  74. $out = "POST $path HTTP/1.0\r\n";
  75. $out .= "Host: " . str_replace('ssl://', '', $host) . "\r\n";
  76. $out .= "User-Agent: Roundcube Webmail/" . RCMAIL_VERSION . " (Googiespell Wrapper)\r\n";
  77. $out .= "Content-Length: " . strlen($gtext) . "\r\n";
  78. $out .= "Content-Type: text/xml\r\n";
  79. $out .= "Connection: Close\r\n\r\n";
  80. $out .= $gtext;
  81. fwrite($fp, $out);
  82. while (!feof($fp))
  83. $store .= fgets($fp, 128);
  84. fclose($fp);
  85. }
  86. // parse HTTP response
  87. if (preg_match('!^HTTP/1.\d (\d+)(.+)!', $store, $m)) {
  88. $http_status = $m[1];
  89. if ($http_status != '200') {
  90. $this->error = 'HTTP ' . $m[1] . $m[2];
  91. $this->error .= "\n" . $store;
  92. }
  93. }
  94. if (!$store) {
  95. $this->error = "Empty result from spelling engine";
  96. }
  97. else if (preg_match('/<spellresult error="([^"]+)"/', $store, $m) && $m[1]) {
  98. $this->error = "Error code $m[1] returned";
  99. $this->error .= preg_match('/<errortext>([^<]+)/', $store, $m) ? ": " . html_entity_decode($m[1]) : '';
  100. }
  101. preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $store, $matches, PREG_SET_ORDER);
  102. // skip exceptions (if appropriate options are enabled)
  103. foreach ($matches as $idx => $m) {
  104. $word = mb_substr($text, $m[1], $m[2], RCUBE_CHARSET);
  105. // skip exceptions
  106. if ($this->dictionary->is_exception($word)) {
  107. unset($matches[$idx]);
  108. }
  109. }
  110. $this->matches = $matches;
  111. return $matches;
  112. }
  113. /**
  114. * Returns suggestions for the specified word
  115. *
  116. * @see rcube_spellcheck_engine::get_words()
  117. */
  118. function get_suggestions($word)
  119. {
  120. $matches = $word ? $this->check($word) : $this->matches;
  121. if ($matches[0][4]) {
  122. $suggestions = explode("\t", $matches[0][4]);
  123. if (sizeof($suggestions) > self::MAX_SUGGESTIONS) {
  124. $suggestions = array_slice($suggestions, 0, self::MAX_SUGGESTIONS);
  125. }
  126. return $suggestions;
  127. }
  128. return array();
  129. }
  130. /**
  131. * Returns misspelled words
  132. *
  133. * @see rcube_spellcheck_engine::get_suggestions()
  134. */
  135. function get_words($text = null)
  136. {
  137. if ($text) {
  138. $matches = $this->check($text);
  139. }
  140. else {
  141. $matches = $this->matches;
  142. $text = $this->content;
  143. }
  144. $result = array();
  145. foreach ($matches as $m) {
  146. $result[] = mb_substr($text, $m[1], $m[2], RCUBE_CHARSET);
  147. }
  148. return $result;
  149. }
  150. }