dddc6984874ddccf66d84dc0fcb81b48b5944be3
3 * Command line spellcheck class
6 * Quick brownn fox brownn
11 * @(#) International Ispell Version 3.1.20 10/10/95, patch 1
13 * & brownn 5 7: brown, Browne, browns, brown n, brown-n
15 * & brownn 5 18: brown, Browne, browns, brown n, brown-n
18 * & brownn 5 1: brown, Browne, browns, brown n, brown-n
21 * & twentytwo 2 1: twenty two, twenty-two
27 * $params['spell_command'] = 'ispell -d american -a';
28 * $params['use_proc_open'] = false; // (check_php_version(4,3))
29 * $params['temp_dir'] = '/tmp/'; // $attachment_dir
30 * $params['userdic'] = array(); // user's dictionary
31 * $params['debug'] = true;
33 * $spell = new cmd_spell($params);
34 * // check $spell->error buffer
36 * $text = "Quick brownn fox brownn\n\nbrownn squirrel.\ntwentytwo owttnewt";
38 * $results = $spell->check_text($text);
39 * // check $spell->error buffer
42 * @copyright © 1999-2006 The SquirrelMail Project Team
43 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
46 * @subpackage squirrelspell
50 * Command line spellcheck class, compatible with ispell and aspell.
52 * @subpackage squirrelspell
54 class cmd_spell
extends squirrelspell
{
58 var $spell_command = '';
59 var $userdic = array();
61 * Controls which function is used to execute ispell. proc_open()
62 * should be used in PHP 4.3+. exec() can be used in older PHP versions.
65 var $use_proc_open = false;
74 var $missed_words = array();
77 * Constructor function
78 * @param array $aParams
80 function cmd_spell($aParams=array()) {
81 if (! isset($aParams['spell_command'])) {
82 return $this->set_error('Spellcheck command is not set.');
84 $this->spell_command
= $aParams['spell_command'];
87 if (isset($aParams['userdic'])) {
88 $this->userdic
= $aParams['userdic'];
91 if (isset($aParams['use_proc_open'])) {
92 $this->use_proc_open
= (bool) $aParams['use_proc_open'];
95 if (isset($aParams['temp_dir'])) {
96 $this->temp_dir
= $aParams['temp_dir'];
97 // add slash to attachment directory, if it does not end with slash.
98 if (substr($this->temp_dir
, -1) != '/') {
99 $this->temp_dir
= $this->temp_dir
. '/';
101 } elseif (!$this->use_proc_open
) {
102 return $this->set_error('Temporally directory is not set.');
105 if (isset($aParams['debug']) && (bool) $aParams['debug']) {
107 error_reporting(E_ALL
);
108 ini_set('display_errors',1);
114 * @param string $sText
115 * @return mixed array with command output or false.
117 function proc_open_spell($sText) {
118 $descriptorspec = array(
119 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
120 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
121 2 => array('pipe', 'w'), // stderr is a pipe that the child will write to
125 $spell_proc = proc_open($this->spell_command
, $descriptorspec, $pipes);
127 $spell_proc = @proc_open
($this->spell_command
, $descriptorspec, $pipes);
130 if ( ! is_resource($spell_proc) ) {
131 return $this->set_error(sprintf(_("Could not run the spellchecker command (%s)."),
132 $this->spell_command
));
135 if ( ! @fwrite
($pipes[0],$sText) ) {
136 $this->set_error(_("Error while writing to pipe."));
137 // close all three $pipes here.
138 for($i=0; $i<=2; $i++
) {
139 // disable all fclose error messages
147 $sqspell_output = array();
148 for($i=1; $i<=2; $i++
) {
149 while(!feof($pipes[$i])) {
150 array_push($sqspell_output, rtrim(fgetss($pipes[$i],999),"\r\n"));
155 if (proc_close($spell_proc)) {
157 foreach ($sqspell_output as $line) {
158 $error.= $line . "\n";
160 return $this->set_error($error);
162 return $sqspell_output;
167 * @param string $sText
168 * @return mixed array with command output or false.
170 function exec_spell($sText) {
171 // find unused file in attachment directory
173 $floc = $this->temp_dir
. md5($sText . microtime());
174 } while (file_exists($floc));
177 $fp = fopen($floc, 'w');
179 $fp = @fopen
($floc, 'w');
181 if ( ! is_resource($fp) ) {
182 return $this->set_error(sprintf(_("Could not open temporary file '%s'."),
186 if ( ! @fwrite
($fp, $sText) ) {
187 $this->set_error(sprintf(_("Error while writing to temporary file '%s'."),
189 // close file descriptor
195 exec("$this->spell_command < $floc 2>&1", $sqspell_output, $exitcode);
201 foreach ($sqspell_output as $line) {
202 $error.= $line . "\n";
204 return $this->set_error($error);
206 return $sqspell_output;
211 * Prepares string for ispell/aspell parsing
213 * Function adds an extra space at the beginning of each line. This way
214 * ispell/aspell don't treat these as command characters.
215 * @param string $sText
218 function prepare_text($sText) {
219 // prepend space to every sqspell_new_text line
220 $sText = str_replace("\r\n","\n",$sText);
222 foreach (explode("\n",$sText) as $line) {
223 $ret.= ' ' . $line . "\n";
229 * Checks block of text
230 * @param string $sText text
233 function check_text($sText) {
234 $this->missed_words
= array();
236 $sText = $this->prepare_text($sText);
238 if ($this->use_proc_open
) {
239 $sqspell_output = $this->proc_open_spell($sText);
241 $sqspell_output = $this->exec_spell($sText);
245 * Define some variables to be used during the processing.
249 * Now we process the output of sqspell_command (ispell or aspell in
250 * ispell compatibility mode, whichever). I'm going to be scarce on
251 * comments here, since you can just look at the ispell/aspell output
252 * and figure out what's going on. ;) The best way to describe this is
255 for ($i=0; $i<sizeof($sqspell_output); $i++
){
256 switch (substr($sqspell_output[$i], 0, 1)){
259 * Ispell adds empty lines when an end of line is reached
265 * Line begins with "&".
266 * This means there's a misspelled word and a few suggestions.
269 list($left, $right) = explode(": ", $sqspell_output[$i]);
270 $tmparray = explode(" ", $left);
271 $sqspell_word=$tmparray[1];
273 * Check if the word is in user dictionary.
275 if (! in_array($sqspell_word,$this->userdic
)){
276 $sqspell_symb=intval($tmparray[3])-1;
278 if (!isset($this->missed_words
[$sqspell_word])) {
279 foreach(explode(',',$right) as $word) {
280 $this->missed_words
[$sqspell_word]['suggestions'][] = trim($word);
284 $this->missed_words
[$sqspell_word]['locations'][] = "$current_line:$sqspell_symb";
288 * Line begins with "#".
289 * This means a misspelled word and no suggestions.
292 $tmparray = explode(" ", $sqspell_output[$i]);
293 $sqspell_word=$tmparray[1];
296 * Check if the word is in user dictionary.
298 if (!in_array($sqspell_word,$this->userdic
)){
299 $sqspell_symb=intval($tmparray[2])-1;
301 $this->missed_words
[$sqspell_word]['suggestions'] = array();
303 $this->missed_words
[$sqspell_word]['locations'][] = "$current_line:$sqspell_symb";
308 return $this->missed_words
;
314 * Define the command used to spellcheck the document.
316 #$sqspell_command=$SQSPELL_APP[$sqspell_use_app];