<?php
/**
 * $Id: GoogleSpell.php,v 1.9 2013/12/03 00:31:46 dave Exp $
 *
 * @package MCManager.includes
 * @author Moxiecode
 * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
 */

// CUSTOM CHANGES - added better error check for when curl and openssl aren't installed
// CUSTOM CHANGES - swiched to google JSON API: http://www.tinymce.com/forum/viewtopic.php?pid=106391#p106391 XML API no longer works
// CUSTOM CHANGES - ... search for "CUSTOM CHANGES" to find changes.
class GoogleSpell extends SpellChecker {
  /**
   * Spellchecks an array of words.
   *
   * @param {String} $lang Language code like sv or en.
   * @param {Array} $words Array of words to spellcheck.
   * @return {Array} Array of misspelled words.
   */
  function &checkWords($lang, $words) {
    $wordstr = implode(' ', $words);
    $matches = $this->_getMatches($lang, $wordstr);
    $words = array();

    for ($i=0; $i<count($matches); $i++)
      $words[] = $this->_unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));

    return $words;
  }

  /**
   * Returns suggestions of for a specific word.
   *
   * @param {String} $lang Language code like sv or en.
   * @param {String} $word Specific word to get suggestions for.
   * @return {Array} Array of suggestions for the specified word.
   */
  function &getSuggestions($lang, $word) {
    $sug = array();
    $osug = array();
    $matches = $this->_getMatches($lang, $word);

    if (count($matches) > 0)
      $sug = explode("\t", utf8_encode($this->_unhtmlentities($matches[0][4])));

    // Remove empty
    foreach ($sug as $item) {
      if ($item)
        $osug[] = $item;
    }

    return $osug;
  }

  protected function &_getMatches($lang, $str) {
    $lang = preg_replace('/[^a-z\-]/i', '', $lang); // Sanitize, remove everything but a-z or -
    $str = preg_replace('/[\x00-\x1F\x7F]/', '', $str); // Sanitize, remove all control characters
    $server = "www.googleapis.com";
    $port = 443;
    $path = "/rpc";
    $host = "www.googleapis.com";
    $url = "https://" . $server . $path;

    // Setup JSON request
    $json = '{"method":"spelling.check","apiVersion":"v2","params":{"text":"'.$str.'","language":"'.$lang.'","originCountry":"USA","key":"AIzaSyCLlKc60a3z7lo8deV-hAyDU7rHYgL4HZg"}}';

    $header  = "POST ".$url." HTTP/1.0\r\n";
    $header .= "Content-type: application/json\r\n";
    $header .= "Content-length: ".strlen($json)."\r\n";
    $header .= "Connection: close\r\n\r\n";
    $header .= $json;

    // Use curl if it exists
    if (function_exists('curl_init')) {
      // Use curl
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      $response = curl_exec($ch);
      curl_close($ch);
    } else {
      // Use raw sockets
      $fp = @fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
      if ($fp) {
        // Send request
        fwrite($fp, $header);

        // Read response
        $response = "";
        while (!feof($fp))
          $response .= fgets($fp, 128);

        fclose($fp);
      } else {
        //echo "Could not open SSL connection to google.";
        // CUSTOM CHANGES
        echo("Could not establish SSL connection to $server:$port. Server Admins: Install these PHP extensions (curl, openssl) and check outbound firewall.");
      }
      list( $header, $response ) = preg_split( '/([\r\n][\r\n])\\1/', $response, 2 );
    }

    // Grab and parse content
    $json = json_decode($response, true);
    $matches = array();
    $misspellings = $json['result']['spellingCheckResponse']['misspellings'];
    if ($misspellings) {
      foreach ($misspellings as $misspelling) {
        $suggestions = array();
        if ($misspelling['suggestions']) {
          foreach ($misspelling['suggestions'] as $suggestion) {
            $suggestions[] = $suggestion['suggestion'];
          }
        }
        $matches[] = array('', $misspelling['charStart'], $misspelling['charLength'], '', implode("\t", $suggestions));
      }
    }

    return $matches;
  }

  protected function _unhtmlentities($string) {
    $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
    $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);

    $trans_tbl = get_html_translation_table(HTML_ENTITIES);
    $trans_tbl = array_flip($trans_tbl);

    return strtr($string, $trans_tbl);
  }
}

// Patch in multibyte support
if (!function_exists('mb_substr')) {
  function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
    $limit = strlen($str);

    for ($s = 0; $start > 0;--$start) {// found the real start
      if ($s >= $limit)
        break;

      if ($str[$s] <= "\x7F")
        ++$s;
      else {
        ++$s; // skip length

        while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
          ++$s;
      }
    }

    if ($len == '')
      return substr($str, $s);
    else
      for ($e = $s; $len > 0; --$len) {//found the real end
        if ($e >= $limit)
          break;

        if ($str[$e] <= "\x7F")
          ++$e;
        else {
          ++$e;//skip length

          while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
            ++$e;
        }
      }

    return substr($str, $s, $e - $s);
  }
}

?>
