Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  
  10  class GoogleSpell extends SpellChecker {
  11  	 /**

  12  	  * Spellchecks an array of words.

  13  	  *

  14  	  * @param {String} $lang Language code like sv or en.

  15  	  * @param {Array} $words Array of words to spellcheck.

  16  	  * @return {Array} Array of misspelled words.

  17  	  */
  18  	 function &checkWords($lang, $words) {
  19  	 	 $wordstr = implode(' ', $words);
  20  	 	 $matches = $this->_getMatches($lang, $wordstr);
  21  	 	 $words = array();
  22  
  23  	 	 for ($i=0; $i<count($matches); $i++)
  24  	 	 	 $words[] = $this->_unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));
  25  
  26  	 	 return $words;
  27  	 }
  28  
  29  	 /**

  30  	  * Returns suggestions of for a specific word.

  31  	  *

  32  	  * @param {String} $lang Language code like sv or en.

  33  	  * @param {String} $word Specific word to get suggestions for.

  34  	  * @return {Array} Array of suggestions for the specified word.

  35  	  */
  36  	 function &getSuggestions($lang, $word) {
  37  	 	 $sug = array();
  38  	 	 $osug = array();
  39  	 	 $matches = $this->_getMatches($lang, $word);
  40  
  41  	 	 if (count($matches) > 0)
  42  	 	 	 $sug = explode("\t", $this->_unhtmlentities($matches[0][4]));
  43  
  44  	 	 // Remove empty

  45  	 	 foreach ($sug as $item) {
  46  	 	 	 if ($item)
  47  	 	 	 	 $osug[] = $item;
  48  	 	 }
  49  
  50  	 	 return $osug;
  51  	 }
  52  
  53  	 function &_getMatches($lang, $str) {
  54  	 	 $lang = preg_replace('/[^a-z\-]/i', '', $lang); // Sanitize, remove everything but a-z or -

  55  	 	 $str = preg_replace('/[\x00-\x1F\x7F]/', '', $str); // Sanitize, remove all control characters

  56  	 	 $server = "www.google.com";
  57  	 	 $port = 443;
  58  	 	 $path = "/tbproxy/spell?lang=" . $lang . "&hl=en";
  59  	 	 $host = "www.google.com";
  60  	 	 $url = "https://" . $server;
  61  
  62  	 	 // Setup XML request

  63  	 	 $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';
  64  
  65  	 	 $header  = "POST ".$path." HTTP/1.0 \r\n";
  66  	 	 $header .= "MIME-Version: 1.0 \r\n";
  67  	 	 $header .= "Content-type: application/PTI26 \r\n";
  68  	 	 $header .= "Content-length: ".strlen($xml)." \r\n";
  69  	 	 $header .= "Content-transfer-encoding: text \r\n";
  70  	 	 $header .= "Request-number: 1 \r\n";
  71  	 	 $header .= "Document-type: Request \r\n";
  72  	 	 $header .= "Interface-Version: Test 1.4 \r\n";
  73  	 	 $header .= "Connection: close \r\n\r\n";
  74  	 	 $header .= $xml;
  75  
  76  	 	 // Use curl if it exists

  77  	 	 if (function_exists('curl_init')) {
  78  	 	 	 // Use curl

  79  	 	 	 $ch = curl_init();
  80  	 	 	 curl_setopt($ch, CURLOPT_URL,$url);
  81  	 	 	 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  82  	 	 	 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
  83  	 	 	 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  84  	 	 	 if (!empty($this->_config['GoogleSpell.proxyhost'])) {
  85  	 	 	 	 if (!empty($this->_config['GoogleSpell.proxytype']) and ($this->_config['GoogleSpell.proxytype'] === 'SOCKS5')) {
  86                      curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  87  	 	 	 	 } else {
  88                      curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTML);
  89                      curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, FALSE);
  90                  }
  91  	 	 	 	 if (empty($this->_config['GoogleSpell.proxyport'])) {
  92  	 	 	 	 	 curl_setopt($ch, CURLOPT_PROXY, $this->_config['GoogleSpell.proxyhost']);
  93  	 	 	 	 } else {
  94  	 	 	 	 	 curl_setopt($ch, CURLOPT_PROXY, $this->_config['GoogleSpell.proxyhost'].':'.$this->_config['GoogleSpell.proxyport']);
  95  	 	 	 	 }
  96  	 	 	 	 if (!empty($this->_config['GoogleSpell.proxyuser']) and !empty($this->_config['GoogleSpell.proxypassword'])) {
  97  	 	 	 	 	 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->_config['GoogleSpell.proxyuser'].':'.$this->_config['GoogleSpell.proxypassword']);
  98  	 	 	 	 	 if (defined('CURLOPT_PROXYAUTH')) {
  99  	 	 	 	 	 	 // any proxy authentication if PHP 5.1

 100  	 	 	 	 	 	 curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
 101  	 	 	 	 	 }
 102  	 	 	 	 }
 103  	 	 	 }
 104  	 	 	 $xml = curl_exec($ch);
 105  	 	 	 curl_close($ch);
 106  	 	 } else {
 107  	 	 	 // Use raw sockets

 108  	 	 	 $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
 109  	 	 	 if ($fp) {
 110  	 	 	 	 // Send request

 111  	 	 	 	 fwrite($fp, $header);
 112  
 113  	 	 	 	 // Read response

 114  	 	 	 	 $xml = "";
 115  	 	 	 	 while (!feof($fp))
 116  	 	 	 	 	 $xml .= fgets($fp, 128);
 117  
 118  	 	 	 	 fclose($fp);
 119  	 	 	 } else
 120  	 	 	 	 echo "Could not open SSL connection to google.";
 121  	 	 }
 122  
 123  	 	 // Grab and parse content

 124  	 	 $matches = array();
 125  	 	 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
 126  
 127  	 	 return $matches;
 128  	 }
 129  
 130  	function _unhtmlentities($string) {
 131          return core_text::entities_to_utf8($string); // Moodle hack

 132  	 	 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
 133  	 	 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
 134  
 135  	 	 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
 136  	 	 $trans_tbl = array_flip($trans_tbl);
 137  
 138  	 	 return strtr($string, $trans_tbl);
 139  	 }
 140  }
 141  
 142  // Patch in multibyte support

 143  if (!function_exists('mb_substr')) {
 144  	function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
 145  	 	 $limit = strlen($str);
 146  
 147  	 	 for ($s = 0; $start > 0;--$start) {// found the real start
 148  	 	 	 if ($s >= $limit)
 149  	 	 	 	 break;
 150  
 151  	 	 	 if ($str[$s] <= "\x7F")
 152  	 	 	 	 ++$s;
 153  	 	 	 else {
 154  	 	 	 	 ++$s; // skip length

 155  
 156  	 	 	 	 while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
 157  	 	 	 	 	 ++$s;
 158  	 	 	 }
 159  	 	 }
 160  
 161  	 	 if ($len == '')
 162  	 	 	 return substr($str, $s);
 163  	 	 else
 164  	 	 	 for ($e = $s; $len > 0; --$len) {//found the real end
 165  	 	 	 	 if ($e >= $limit)
 166  	 	 	 	 	 break;
 167  
 168  	 	 	 	 if ($str[$e] <= "\x7F")
 169  	 	 	 	 	 ++$e;
 170  	 	 	 	 else {
 171  	 	 	 	 	 ++$e;//skip length

 172  
 173  	 	 	 	 	 while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
 174  	 	 	 	 	 	 ++$e;
 175  	 	 	 	 }
 176  	 	 	 }
 177  
 178  	 	 return substr($str, $s, $e - $s);
 179  	 }
 180  }
 181  
 182  ?>