PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_jce/editor/tiny_mce/plugins/spellchecker/classes/googlespell.php

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