PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/concreteOLD/js/tiny_mce/plugins/spellchecker/classes/GoogleSpell.php

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