PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/kraymitchell/saiu
PHP | 198 lines | 101 code | 30 blank | 67 comment | 8 complexity | dc1777a2f198541cdc5897a6339385dc MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, BSD-3-Clause, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * @version $Id: spellchecker.php 221 2011-06-11 17:30:33Z happy_noodle_boy $
  4. * @package JCE
  5. * @copyright Copyright (C) 2005 - 2009 Ryan Demmer. All rights reserved.
  6. * @author Ryan Demmer
  7. * @license GNU/GPL
  8. * JCE is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. */
  13. require_once(WF_EDITOR_LIBRARIES . '/classes/plugin.php');
  14. class WFSpellCheckerPlugin extends WFEditorPlugin
  15. {
  16. /**
  17. * Constructor activating the default information of the class
  18. *
  19. * @access protected
  20. */
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $config = $this->getConfig();
  25. $engine = $this->getEngine();
  26. if (isset($config['general.remote_rpc_url'])) {
  27. $this->remoteRPC();
  28. }
  29. $request = WFRequest::getInstance();
  30. // Setup plugin XHR callback functions
  31. $request->setRequest(array($engine, 'checkWords'));
  32. $request->setRequest(array($engine, 'getSuggestions'));
  33. $request->setRequest(array($engine, 'ignoreWord'));
  34. $request->setRequest(array($engine, 'ignoreWords'));
  35. $request->setRequest(array($engine, 'learnWord'));
  36. $this->execute();
  37. }
  38. /**
  39. * Returns a reference to a plugin object
  40. *
  41. * This method must be invoked as:
  42. * <pre> $advlink =AdvLink::getInstance();</pre>
  43. *
  44. * @access public
  45. * @return JCE The editor object.
  46. * @since 1.5
  47. */
  48. public function &getInstance()
  49. {
  50. static $instance;
  51. if (!is_object($instance)) {
  52. $instance = new WFSpellCheckerPlugin();
  53. }
  54. return $instance;
  55. }
  56. function getConfig()
  57. {
  58. static $config;
  59. if (!is_array($config)) {
  60. $params = $this->getParams();
  61. $config = array(
  62. 'general.engine' => $params->get( 'spellchecker.engine', 'googlespell' ),
  63. // PSpell settings
  64. 'PSpell.mode' => $params->get( 'spellchecker.pspell_mode', 'PSPELL_FAST' ),
  65. 'PSpell.spelling' => $params->get( 'spellchecker.pspell_spelling', '' ),
  66. 'PSpell.jargon' => $params->get( 'spellchecker.pspell_jargon', '' ),
  67. 'PSpell.encoding' => $params->get( 'spellchecker.pspell_encoding', '' ),
  68. 'PSpell.dictionary' => JPATH_BASE . '/' . $params->get( 'spellchecker.pspell_dictionary', '' ),
  69. // PSpellShell settings
  70. 'PSpellShell.mode' => $params->get( 'spellchecker.pspellshell_mode', 'PSPELL_FAST' ),
  71. 'PSpellShell.aspell' => $params->get( 'spellchecker.pspellshell_aspell', '/usr/bin/aspell' ),
  72. 'PSpellShell.tmp' => $params->get( 'spellchecker.pspellshell_tmp', '/tmp' )
  73. );
  74. }
  75. return $config;
  76. }
  77. function &getEngine()
  78. {
  79. static $engine;
  80. $config = $this->getConfig();
  81. if (!is_object($engine)) {
  82. $classname = $config['general.engine'];
  83. require_once( dirname(__FILE__) . '/' . $classname . ".php");
  84. $engine = new $classname($config);
  85. }
  86. return $engine;
  87. }
  88. private function remoteRPC()
  89. {
  90. $config = $this->getConfig();
  91. $url = parse_url($config['general.remote_rpc_url']);
  92. // Setup request
  93. $req = "POST " . $url["path"] . " HTTP/1.0\r\n";
  94. $req .= "Connection: close\r\n";
  95. $req .= "Host: " . $url['host'] . "\r\n";
  96. $req .= "Content-Length: " . strlen($raw) . "\r\n";
  97. $req .= "\r\n" . $raw;
  98. if (!isset($url['port']) || !$url['port'])
  99. $url['port'] = 80;
  100. $errno = $errstr = "";
  101. $socket = fsockopen($url['host'], intval($url['port']), $errno, $errstr, 30);
  102. if ($socket) {
  103. // Send request headers
  104. fputs($socket, $req);
  105. // Read response headers and data
  106. $resp = "";
  107. while (!feof($socket))
  108. $resp .= fgets($socket, 4096);
  109. fclose($socket);
  110. // Split response header/data
  111. $resp = explode("\r\n\r\n", $resp);
  112. echo $resp[1]; // Output body
  113. }
  114. die();
  115. }
  116. }
  117. /**
  118. * @author Moxiecode
  119. * @copyright Copyright (c) 2004-2007, Moxiecode Systems AB, All rights reserved.
  120. */
  121. class SpellChecker {
  122. /**
  123. * Constructor.
  124. *
  125. * @param $config Configuration name/value array.
  126. */
  127. function SpellChecker(&$config) {
  128. $this->_config = $config;
  129. }
  130. /**
  131. * Simple loopback function everything that gets in will be send back.
  132. *
  133. * @param $args.. Arguments.
  134. * @return {Array} Array of all input arguments.
  135. */
  136. function &loopback(/* args.. */) {
  137. return func_get_args();
  138. }
  139. /**
  140. * Spellchecks an array of words.
  141. *
  142. * @param {String} $lang Language code like sv or en.
  143. * @param {Array} $words Array of words to spellcheck.
  144. * @return {Array} Array of misspelled words.
  145. */
  146. function &checkWords($lang, $words) {
  147. return $words;
  148. }
  149. /**
  150. * Returns suggestions of for a specific word.
  151. *
  152. * @param {String} $lang Language code like sv or en.
  153. * @param {String} $word Specific word to get suggestions for.
  154. * @return {Array} Array of suggestions for the specified word.
  155. */
  156. function &getSuggestions($lang, $word) {
  157. return array();
  158. }
  159. /**
  160. * Throws an error message back to the user. This will stop all execution.
  161. *
  162. * @param {String} $str Message to send back to user.
  163. */
  164. function throwError($str) {
  165. die('{"result":null,"id":null,"error":{"errstr":"' . addslashes($str) . '","errfile":"","errline":null,"errcontext":"","level":"FATAL"}}');
  166. }
  167. }
  168. ?>