3 * Command line spellcheck class
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;
12 * $spell = new cmd_spell($params);
13 * // check $spell->error buffer
15 * $text = "Quick brownn fox brownn\n\nbrownn squirrel.\ntwentytwo owttnewt";
17 * $results = $spell->check_text($text);
18 * // check $spell->error buffer
21 * @copyright 1999-2017 The SquirrelMail Project Team
22 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
25 * @subpackage squirrelspell
29 * Command line spellcheck class, compatible with ispell and aspell.
31 * @subpackage squirrelspell
33 class cmd_spell
extends squirrelspell
{
37 var $spell_command = '';
38 var $userdic = array();
40 * Controls which function is used to execute ispell. proc_open()
41 * should be used in PHP 4.3+. exec() can be used in older PHP versions.
44 var $use_proc_open = false;
53 var $missed_words = array();
56 * Constructor function
57 * @param array $aParams
59 function cmd_spell($aParams=array()) {
60 if (! isset($aParams['spell_command'])) {
61 return $this->set_error('Spellcheck command is not set.');
63 $this->spell_command
= $aParams['spell_command'];
66 if (isset($aParams['userdic'])) {
67 $this->userdic
= $aParams['userdic'];
70 if (isset($aParams['use_proc_open'])) {
71 $this->use_proc_open
= (bool) $aParams['use_proc_open'];
74 if (isset($aParams['temp_dir'])) {
75 $this->temp_dir
= $aParams['temp_dir'];
76 // add slash to attachment directory, if it does not end with slash.
77 if (substr($this->temp_dir
, -1) != '/') {
78 $this->temp_dir
= $this->temp_dir
. '/';
80 } elseif (!$this->use_proc_open
) {
81 return $this->set_error('Temporally directory is not set.');
84 if (isset($aParams['debug']) && (bool) $aParams['debug']) {
86 error_reporting(E_ALL
);
87 ini_set('display_errors',1);
93 * @param string $sText
94 * @return mixed array with command output or false.
96 function proc_open_spell($sText) {
97 $descriptorspec = array(
98 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
99 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
100 2 => array('pipe', 'w'), // stderr is a pipe that the child will write to
104 $spell_proc = proc_open($this->spell_command
, $descriptorspec, $pipes);
106 $spell_proc = @proc_open
($this->spell_command
, $descriptorspec, $pipes);
109 if ( ! is_resource($spell_proc) ) {
110 return $this->set_error(sprintf(_("Could not run the spellchecker command (%s)."),
111 $this->spell_command
));
114 if ( ! @fwrite
($pipes[0],$sText) ) {
115 $this->set_error(_("Error while writing to pipe."));
116 // close all three $pipes here.
117 for($i=0; $i<=2; $i++
) {
118 // disable all fclose error messages
126 $sqspell_output = array();
127 for($i=1; $i<=2; $i++
) {
128 while(!feof($pipes[$i])) {
129 array_push($sqspell_output, rtrim(fgetss($pipes[$i],999),"\r\n"));
134 if (proc_close($spell_proc)) {
136 foreach ($sqspell_output as $line) {
137 $error.= $line . "\n";
139 return $this->set_error($error);
141 return $sqspell_output;
146 * @param string $sText
147 * @return mixed array with command output or false.
149 function exec_spell($sText) {
150 // find unused file in attachment directory
152 $floc = $this->temp_dir
. md5($sText . microtime());
153 } while (file_exists($floc));
156 $fp = fopen($floc, 'w');
158 $fp = @fopen
($floc, 'w');
160 if ( ! is_resource($fp) ) {
161 return $this->set_error(sprintf(_("Could not open temporary file '%s'."),
165 if ( ! @fwrite
($fp, $sText) ) {
166 $this->set_error(sprintf(_("Error while writing to temporary file '%s'."),
168 // close file descriptor
174 exec("$this->spell_command < $floc 2>&1", $sqspell_output, $exitcode);
180 foreach ($sqspell_output as $line) {
181 $error.= $line . "\n";
183 return $this->set_error($error);
185 return $sqspell_output;
190 * Prepares string for ispell/aspell parsing
192 * Function adds an extra space at the beginning of each line. This way
193 * ispell/aspell don't treat these as command characters.
194 * @param string $sText
197 function prepare_text($sText) {
198 // prepend space to every sqspell_new_text line
199 $sText = str_replace("\r\n","\n",$sText);
201 foreach (explode("\n",$sText) as $line) {
202 $ret.= ' ' . $line . "\n";
208 * Checks block of text
209 * @param string $sText text
212 function check_text($sText) {
213 $this->missed_words
= array();
215 $sText = $this->prepare_text($sText);
217 if ($this->use_proc_open
) {
218 $sqspell_output = $this->proc_open_spell($sText);
220 $sqspell_output = $this->exec_spell($sText);
224 * Define some variables to be used during the processing.
228 * Now we process the output of sqspell_command (ispell or aspell in
229 * ispell compatibility mode, whichever). I'm going to be scarce on
230 * comments here, since you can just look at the ispell/aspell output
231 * and figure out what's going on. ;) The best way to describe this is
234 for ($i=0; $i<sizeof($sqspell_output); $i++
){
235 switch (substr($sqspell_output[$i], 0, 1)){
238 * Ispell adds empty lines when an end of line is reached
244 * Line begins with "&".
245 * This means there's a misspelled word and a few suggestions.
248 list($left, $right) = explode(": ", $sqspell_output[$i]);
249 $tmparray = explode(" ", $left);
250 $sqspell_word=$tmparray[1];
252 * Check if the word is in user dictionary.
254 if (! in_array($sqspell_word,$this->userdic
)){
255 $sqspell_symb=intval($tmparray[3])-1;
257 if (!isset($this->missed_words
[$sqspell_word])) {
258 foreach(explode(',',$right) as $word) {
259 $this->missed_words
[$sqspell_word]['suggestions'][] = trim($word);
263 $this->missed_words
[$sqspell_word]['locations'][] = "$current_line:$sqspell_symb";
267 * Line begins with "#".
268 * This means a misspelled word and no suggestions.
271 $tmparray = explode(" ", $sqspell_output[$i]);
272 $sqspell_word=$tmparray[1];
275 * Check if the word is in user dictionary.
277 if (!in_array($sqspell_word,$this->userdic
)){
278 $sqspell_symb=intval($tmparray[2])-1;
280 $this->missed_words
[$sqspell_word]['suggestions'] = array();
282 $this->missed_words
[$sqspell_word]['locations'][] = "$current_line:$sqspell_symb";
287 return $this->missed_words
;
293 * Define the command used to spellcheck the document.
295 #$sqspell_command=$SQSPELL_APP[$sqspell_use_app];