PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/web/concrete/js/tiny_mce/plugins/spellchecker/classes/GoogleSpell.php

https://bitbucket.org/hudsonite/concrete5
PHP | 161 lines | 104 code | 27 blank | 30 comment | 23 complexity | c99d7a517ad91ae5c265ce3cb04cc95f MD5 | raw file
  1. <?php
  2. /**
  3. * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
  4. *
  5. * @package MCManager.includes
  6. * @author Moxiecode
  7. * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
  8. */
  9. class GoogleSpell extends SpellChecker {
  10. /**
  11. * Spellchecks an array of words.
  12. *
  13. * @param {String} $lang Language code like sv or en.
  14. * @param {Array} $words Array of words to spellcheck.
  15. * @return {Array} Array of misspelled words.
  16. */
  17. function &checkWords($lang, $words) {
  18. $wordstr = implode(' ', $words);
  19. $matches = $this->_getMatches($lang, $wordstr);
  20. $words = array();
  21. for ($i=0; $i<count($matches); $i++)
  22. $words[] = $this->_unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));
  23. return $words;
  24. }
  25. /**
  26. * Returns suggestions of for a specific word.
  27. *
  28. * @param {String} $lang Language code like sv or en.
  29. * @param {String} $word Specific word to get suggestions for.
  30. * @return {Array} Array of suggestions for the specified word.
  31. */
  32. function &getSuggestions($lang, $word) {
  33. $sug = array();
  34. $osug = array();
  35. $matches = $this->_getMatches($lang, $word);
  36. if (count($matches) > 0)
  37. $sug = explode("\t", utf8_encode($this->_unhtmlentities($matches[0][4])));
  38. // Remove empty
  39. foreach ($sug as $item) {
  40. if ($item)
  41. $osug[] = $item;
  42. }
  43. return $osug;
  44. }
  45. protected function &_getMatches($lang, $str) {
  46. $lang = preg_replace('/[^a-z\-]/i', '', $lang); // Sanitize, remove everything but a-z or -
  47. $str = preg_replace('/[\x00-\x1F\x7F]/', '', $str); // Sanitize, remove all control characters
  48. $server = "www.google.com";
  49. $port = 443;
  50. $path = "/tbproxy/spell?lang=" . $lang . "&hl=en";
  51. $host = "www.google.com";
  52. $url = "https://" . $server;
  53. // Setup XML request
  54. $xml = '<'.'?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';
  55. $header = "POST ".$path." HTTP/1.0 \r\n";
  56. $header .= "MIME-Version: 1.0 \r\n";
  57. $header .= "Content-type: application/PTI26 \r\n";
  58. $header .= "Content-length: ".strlen($xml)." \r\n";
  59. $header .= "Content-transfer-encoding: text \r\n";
  60. $header .= "Request-number: 1 \r\n";
  61. $header .= "Document-type: Request \r\n";
  62. $header .= "Interface-Version: Test 1.4 \r\n";
  63. $header .= "Connection: close \r\n\r\n";
  64. $header .= $xml;
  65. // Use curl if it exists
  66. if (function_exists('curl_init')) {
  67. // Use curl
  68. $ch = curl_init();
  69. curl_setopt($ch, CURLOPT_URL,$url);
  70. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  71. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
  72. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  73. $xml = curl_exec($ch);
  74. curl_close($ch);
  75. } else {
  76. // Use raw sockets
  77. $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
  78. if ($fp) {
  79. // Send request
  80. fwrite($fp, $header);
  81. // Read response
  82. $xml = "";
  83. while (!feof($fp))
  84. $xml .= fgets($fp, 128);
  85. fclose($fp);
  86. } else
  87. echo "Could not open SSL connection to google.";
  88. }
  89. // Grab and parse content
  90. $matches = array();
  91. preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
  92. return $matches;
  93. }
  94. protected function _unhtmlentities($string) {
  95. $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
  96. $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
  97. $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  98. $trans_tbl = array_flip($trans_tbl);
  99. return strtr($string, $trans_tbl);
  100. }
  101. }
  102. // Patch in multibyte support
  103. if (!function_exists('mb_substr')) {
  104. function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
  105. $limit = strlen($str);
  106. for ($s = 0; $start > 0;--$start) {// found the real start
  107. if ($s >= $limit)
  108. break;
  109. if ($str[$s] <= "\x7F")
  110. ++$s;
  111. else {
  112. ++$s; // skip length
  113. while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
  114. ++$s;
  115. }
  116. }
  117. if ($len == '')
  118. return substr($str, $s);
  119. else
  120. for ($e = $s; $len > 0; --$len) {//found the real end
  121. if ($e >= $limit)
  122. break;
  123. if ($str[$e] <= "\x7F")
  124. ++$e;
  125. else {
  126. ++$e;//skip length
  127. while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
  128. ++$e;
  129. }
  130. }
  131. return substr($str, $s, $e - $s);
  132. }
  133. }
  134. ?>