PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/js/tinymce/plugins/spellchecker/classes/GoogleSpell.php

https://bitbucket.org/MaheshDhaduk/androidmobiles
PHP | 159 lines | 102 code | 27 blank | 30 comment | 23 complexity | b063fd24b1be9c2bf1445e89ecbf9cf8 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-1.0
  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. function &_getMatches($lang, $str) {
  46. $server = "www.google.com";
  47. $port = 443;
  48. $path = "/tbproxy/spell?lang=" . $lang . "&hl=en";
  49. $host = "www.google.com";
  50. $url = "https://" . $server;
  51. // Setup XML request
  52. $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';
  53. $header = "POST ".$path." HTTP/1.0 \r\n";
  54. $header .= "MIME-Version: 1.0 \r\n";
  55. $header .= "Content-type: application/PTI26 \r\n";
  56. $header .= "Content-length: ".strlen($xml)." \r\n";
  57. $header .= "Content-transfer-encoding: text \r\n";
  58. $header .= "Request-number: 1 \r\n";
  59. $header .= "Document-type: Request \r\n";
  60. $header .= "Interface-Version: Test 1.4 \r\n";
  61. $header .= "Connection: close \r\n\r\n";
  62. $header .= $xml;
  63. // Use curl if it exists
  64. if (function_exists('curl_init')) {
  65. // Use curl
  66. $ch = curl_init();
  67. curl_setopt($ch, CURLOPT_URL,$url);
  68. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  69. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
  70. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  71. $xml = curl_exec($ch);
  72. curl_close($ch);
  73. } else {
  74. // Use raw sockets
  75. $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
  76. if ($fp) {
  77. // Send request
  78. fwrite($fp, $header);
  79. // Read response
  80. $xml = "";
  81. while (!feof($fp))
  82. $xml .= fgets($fp, 128);
  83. fclose($fp);
  84. } else
  85. echo "Could not open SSL connection to google.";
  86. }
  87. // Grab and parse content
  88. $matches = array();
  89. preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
  90. return $matches;
  91. }
  92. function _unhtmlentities($string) {
  93. $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
  94. $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
  95. $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  96. $trans_tbl = array_flip($trans_tbl);
  97. return strtr($string, $trans_tbl);
  98. }
  99. }
  100. // Patch in multibyte support
  101. if (!function_exists('mb_substr')) {
  102. function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
  103. $limit = strlen($str);
  104. for ($s = 0; $start > 0;--$start) {// found the real start
  105. if ($s >= $limit)
  106. break;
  107. if ($str[$s] <= "\x7F")
  108. ++$s;
  109. else {
  110. ++$s; // skip length
  111. while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
  112. ++$s;
  113. }
  114. }
  115. if ($len == '')
  116. return substr($str, $s);
  117. else
  118. for ($e = $s; $len > 0; --$len) {//found the real end
  119. if ($e >= $limit)
  120. break;
  121. if ($str[$e] <= "\x7F")
  122. ++$e;
  123. else {
  124. ++$e;//skip length
  125. while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
  126. ++$e;
  127. }
  128. }
  129. return substr($str, $s, $e - $s);
  130. }
  131. }
  132. ?>