cmd_spell.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * Command line spellcheck class
  4. *
  5. * $params = array();
  6. * $params['spell_command'] = 'ispell -d american -a';
  7. * $params['use_proc_open'] = false; // (check_php_version(4,3))
  8. * $params['temp_dir'] = '/tmp/'; // $attachment_dir
  9. * $params['userdic'] = array(); // user's dictionary
  10. * $params['debug'] = true;
  11. *
  12. * $spell = new cmd_spell($params);
  13. * // check $spell->error buffer
  14. *
  15. * $text = "Quick brownn fox brownn\n\nbrownn squirrel.\ntwentytwo owttnewt";
  16. *
  17. * $results = $spell->check_text($text);
  18. * // check $spell->error buffer
  19. * // parse $results
  20. *
  21. * @copyright &copy; 1999-2007 The SquirrelMail Project Team
  22. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  23. * @version $Id$
  24. * @package plugins
  25. * @subpackage squirrelspell
  26. */
  27. /**
  28. * Command line spellcheck class, compatible with ispell and aspell.
  29. * @package plugins
  30. * @subpackage squirrelspell
  31. */
  32. class cmd_spell extends squirrelspell {
  33. /**
  34. * @var string
  35. */
  36. var $spell_command = '';
  37. var $userdic = array();
  38. /**
  39. * Controls which function is used to execute ispell. proc_open()
  40. * should be used in PHP 4.3+. exec() can be used in older PHP versions.
  41. * @var boolean
  42. */
  43. var $use_proc_open = false;
  44. /**
  45. * @var string
  46. */
  47. var $temp_dir = '';
  48. /**
  49. */
  50. var $debug = false;
  51. var $missed_words = array();
  52. /**
  53. * Constructor function
  54. * @param array $aParams
  55. */
  56. function cmd_spell($aParams=array()) {
  57. if (! isset($aParams['spell_command'])) {
  58. return $this->set_error('Spellcheck command is not set.');
  59. } else {
  60. $this->spell_command = $aParams['spell_command'];
  61. }
  62. if (isset($aParams['userdic'])) {
  63. $this->userdic = $aParams['userdic'];
  64. }
  65. if (isset($aParams['use_proc_open'])) {
  66. $this->use_proc_open = (bool) $aParams['use_proc_open'];
  67. }
  68. if (isset($aParams['temp_dir'])) {
  69. $this->temp_dir = $aParams['temp_dir'];
  70. // add slash to attachment directory, if it does not end with slash.
  71. if (substr($this->temp_dir, -1) != '/') {
  72. $this->temp_dir = $this->temp_dir . '/';
  73. }
  74. } elseif (!$this->use_proc_open) {
  75. return $this->set_error('Temporally directory is not set.');
  76. }
  77. if (isset($aParams['debug']) && (bool) $aParams['debug']) {
  78. $this->debug = true;
  79. error_reporting(E_ALL);
  80. ini_set('display_errors',1);
  81. }
  82. }
  83. /**
  84. * @param string $sText
  85. * @return mixed array with command output or false.
  86. */
  87. function proc_open_spell($sText) {
  88. $descriptorspec = array(
  89. 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
  90. 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
  91. 2 => array('pipe', 'w'), // stderr is a pipe that the child will write to
  92. );
  93. if ($this->debug) {
  94. $spell_proc = proc_open($this->spell_command, $descriptorspec, $pipes);
  95. } else {
  96. $spell_proc = @proc_open($this->spell_command, $descriptorspec, $pipes);
  97. }
  98. if ( ! is_resource($spell_proc) ) {
  99. return $this->set_error(sprintf(_("Could not run the spellchecker command (%s)."),
  100. $this->spell_command));
  101. }
  102. if ( ! @fwrite($pipes[0],$sText) ) {
  103. $this->set_error(_("Error while writing to pipe."));
  104. // close all three $pipes here.
  105. for($i=0; $i<=2; $i++) {
  106. // disable all fclose error messages
  107. @fclose($pipes[$i]);
  108. }
  109. return false;
  110. }
  111. fclose($pipes[0]);
  112. $sqspell_output = array();
  113. for($i=1; $i<=2; $i++) {
  114. while(!feof($pipes[$i])) {
  115. array_push($sqspell_output, rtrim(fgetss($pipes[$i],999),"\r\n"));
  116. }
  117. fclose($pipes[$i]);
  118. }
  119. if (proc_close($spell_proc)) {
  120. $error = '';
  121. foreach ($sqspell_output as $line) {
  122. $error.= $line . "\n";
  123. }
  124. return $this->set_error($error);
  125. } else {
  126. return $sqspell_output;
  127. }
  128. }
  129. /**
  130. * @param string $sText
  131. * @return mixed array with command output or false.
  132. */
  133. function exec_spell($sText) {
  134. // find unused file in attachment directory
  135. do {
  136. $floc = $this->temp_dir . md5($sText . microtime());
  137. } while (file_exists($floc));
  138. if ($this->debug) {
  139. $fp = fopen($floc, 'w');
  140. } else {
  141. $fp = @fopen($floc, 'w');
  142. }
  143. if ( ! is_resource($fp) ) {
  144. return $this->set_error(sprintf(_("Could not open temporary file '%s'."),
  145. $floc) );
  146. }
  147. if ( ! @fwrite($fp, $sText) ) {
  148. $this->set_error(sprintf(_("Error while writing to temporary file '%s'."),
  149. $floc) );
  150. // close file descriptor
  151. fclose($fp);
  152. return false;
  153. }
  154. fclose($fp);
  155. exec("$this->spell_command < $floc 2>&1", $sqspell_output, $exitcode);
  156. unlink($floc);
  157. if ($exitcode) {
  158. $error = '';
  159. foreach ($sqspell_output as $line) {
  160. $error.= $line . "\n";
  161. }
  162. return $this->set_error($error);
  163. } else {
  164. return $sqspell_output;
  165. }
  166. }
  167. /**
  168. * Prepares string for ispell/aspell parsing
  169. *
  170. * Function adds an extra space at the beginning of each line. This way
  171. * ispell/aspell don't treat these as command characters.
  172. * @param string $sText
  173. * @return string
  174. */
  175. function prepare_text($sText) {
  176. // prepend space to every sqspell_new_text line
  177. $sText = str_replace("\r\n","\n",$sText);
  178. $ret = '';
  179. foreach (explode("\n",$sText) as $line) {
  180. $ret.= ' ' . $line . "\n";
  181. }
  182. return $ret;
  183. }
  184. /**
  185. * Checks block of text
  186. * @param string $sText text
  187. * @return array
  188. */
  189. function check_text($sText) {
  190. $this->missed_words = array();
  191. $sText = $this->prepare_text($sText);
  192. if ($this->use_proc_open) {
  193. $sqspell_output = $this->proc_open_spell($sText);
  194. } else {
  195. $sqspell_output = $this->exec_spell($sText);
  196. }
  197. /**
  198. * Define some variables to be used during the processing.
  199. */
  200. $current_line=0;
  201. /**
  202. * Now we process the output of sqspell_command (ispell or aspell in
  203. * ispell compatibility mode, whichever). I'm going to be scarce on
  204. * comments here, since you can just look at the ispell/aspell output
  205. * and figure out what's going on. ;) The best way to describe this is
  206. * "Dark Magic".
  207. */
  208. for ($i=0; $i<sizeof($sqspell_output); $i++){
  209. switch (substr($sqspell_output[$i], 0, 1)){
  210. /**
  211. * Line is empty.
  212. * Ispell adds empty lines when an end of line is reached
  213. */
  214. case '':
  215. $current_line++;
  216. break;
  217. /**
  218. * Line begins with "&".
  219. * This means there's a misspelled word and a few suggestions.
  220. */
  221. case '&':
  222. list($left, $right) = explode(": ", $sqspell_output[$i]);
  223. $tmparray = explode(" ", $left);
  224. $sqspell_word=$tmparray[1];
  225. /**
  226. * Check if the word is in user dictionary.
  227. */
  228. if (! in_array($sqspell_word,$this->userdic)){
  229. $sqspell_symb=intval($tmparray[3])-1;
  230. // add suggestions
  231. if (!isset($this->missed_words[$sqspell_word])) {
  232. foreach(explode(',',$right) as $word) {
  233. $this->missed_words[$sqspell_word]['suggestions'][] = trim($word);
  234. }
  235. }
  236. // add location
  237. $this->missed_words[$sqspell_word]['locations'][] = "$current_line:$sqspell_symb";
  238. }
  239. break;
  240. /**
  241. * Line begins with "#".
  242. * This means a misspelled word and no suggestions.
  243. */
  244. case '#':
  245. $tmparray = explode(" ", $sqspell_output[$i]);
  246. $sqspell_word=$tmparray[1];
  247. /**
  248. *
  249. * Check if the word is in user dictionary.
  250. */
  251. if (!in_array($sqspell_word,$this->userdic)){
  252. $sqspell_symb=intval($tmparray[2])-1;
  253. // no suggestions
  254. $this->missed_words[$sqspell_word]['suggestions'] = array();
  255. // add location
  256. $this->missed_words[$sqspell_word]['locations'][] = "$current_line:$sqspell_symb";
  257. }
  258. break;
  259. }
  260. }
  261. return $this->missed_words;
  262. }
  263. }
  264. /**
  265. * Define the command used to spellcheck the document.
  266. */
  267. #$sqspell_command=$SQSPELL_APP[$sqspell_use_app];