source: trunk/plugins/SpellChecker/spell-check-logic.php @ 498

Last change on this file since 498 was 498, checked in by gogo, 8 years ago

Add config value SpellChecker.utf8_to_entities which can be set to
false to stop SpellChecker from converting (multi byte) utf8
characters to entities before passing to aspell - ticket:120
indicates this is not required. Config value is true by default for
backwards compatability.

  • Property svn:eol-style set to native
  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
File size: 6.2 KB
Line 
1<?php
2  $text = stripslashes($_POST['content']);
3
4  // Convert UTF-8 multi-bytes into decimal character entities.  This is because
5  // aspell isn't fully utf8-aware - ticket:120 raises the possibility
6  // that this is not required (any more) and so you can turn it off
7  // with editor.config.SpellChecker.utf8_to_entities = false
8  if(!isset($_REQUEST['utf8_to_entitis']) || $_REQUEST['utf8_to_entities'])
9  {
10    $text = preg_replace('/([\xC0-\xDF][\x80-\xBF])/e', "'&#' . utf8_ord('\$1') . ';'", $text);
11    $text = preg_replace('/([\xE0-\xEF][\x80-\xBF][\x80-\xBF])/e',             "'&#' . utf8_ord('\$1') . ';'",  $text);
12    $text = preg_replace('/([\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF])/e', "'&#' . utf8_ord('\$1') . ';'",   $text);
13  }
14 
15  function utf8_ord($chr)
16  {
17    switch(strlen($chr))
18    {
19      case 1 :
20        return ord($chr);
21
22      case 2 :
23        $ord = ord($chr{1}) & 63;
24        $ord = $ord | ((ord($chr{0}) & 31) << 6);
25        return $ord;
26
27      case 3 :
28        $ord = ord($chr{2}) & 63;
29        $ord = $ord | ((ord($chr{1}) & 63) << 6);
30        $ord = $ord | ((ord($chr{0}) & 15) << 12);
31        return $ord;
32
33      case 4 :
34        $ord = ord($chr{3}) & 63;
35        $ord = $ord | ((ord($chr{2}) & 63) << 6);
36        $ord = $ord | ((ord($chr{1}) & 63) << 12);
37        $ord = $ord | ((ord($chr{0}) & 7)  << 18);
38        return $ord;
39
40      default :
41        trigger_error('Character not utf-8', E_USER_ERROR);
42    }
43  }
44
45  require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'aspell_setup.php');
46
47##############################################################################
48header('Content-Type: text/html; charset=utf-8');
49  echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
50<html>
51<head>
52<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
53<link rel="stylesheet" type="text/css" media="all" href="spell-check-style.css" />';
54
55// Lets define some values outside the condition below, in case we have an empty
56// document.                                                                     
57$textarray = array();
58$varlines = '<script type="text/javascript">var suggested_words = { ';
59$infolines = 'var spellcheck_info = {';
60$counter = 0;
61$suggest_count = 0;
62
63if (trim($text) != "")
64{
65    if ($fd = fopen($temptext, 'w'))
66    {
67        $textarray = explode("\n", $text);
68        fwrite ($fd, "!\n");
69        foreach ($textarray as $key=>$value)
70        {
71            // adding the carat to each line prevents the use of aspell commands within the text...
72            fwrite($fd, "^$value\n");
73        }
74        fclose($fd);
75        chmod($temptext, 0777);
76        // next run aspell
77        $return = shell_exec($aspellcommand . ' 2>&1');
78        // echo $return;
79        unlink($temptext);
80        $returnarray = explode("\n", $return);
81        $returnlines = count($returnarray);
82//print_r(htmlentities($return));
83        $textlines = count($textarray);
84
85        $lineindex = -1;
86        $poscorrect = 0;
87        foreach ($returnarray as $key=>$value)
88        {
89            // if there is a correction here, processes it, else move the $textarray pointer to the next line
90            if (substr($value, 0, 1) == '&')
91            {
92               $counter=$counter+1;
93                $correction = explode(' ', $value);
94                $word = $correction[1];
95                $suggest_count += $correction[2];
96                $absposition = substr($correction[3], 0, -1) - 1;
97                $position = $absposition + $poscorrect;
98                $niceposition = $lineindex.','.$absposition;
99                $suggstart = strpos($value, ':') + 2;
100                $suggestions = substr($value, $suggstart);
101                $suggestionarray = explode(', ', $suggestions);
102
103                $beforeword = substr($textarray[$lineindex], 0, $position);
104                $afterword = substr($textarray[$lineindex], $position + strlen($word));
105                $textarray[$lineindex] = $beforeword.'<span class="HA-spellcheck-error">'.$word.'</span>'.$afterword;
106
107             $suggestion_list = '';
108                foreach ($suggestionarray as $key=>$value)
109                {
110                    $suggestion_list .= $value.',';
111                }
112                $suggestion_list = substr($suggestion_list, 0, strlen($suggestion_list) - 1);
113                $varlines .= '"'.$word.'":"'.$suggestion_list.'",';
114
115                $poscorrect = $poscorrect + 41;
116            }
117            elseif (substr($value, 0, 1) == '#')
118            {
119                $correction = explode(' ', $value);
120                $word = $correction[1];
121                $absposition = $correction[2] - 1;
122                $position = $absposition + $poscorrect;
123                $niceposition = $lineindex.','.$absposition;
124                $beforeword = substr($textarray[$lineindex], 0, $position);
125                $afterword = substr($textarray[$lineindex], $position + strlen($word));
126                $textarray[$lineindex] = $beforeword.$word.$afterword;
127                $textarray[$lineindex] = $beforeword.'<span class="HA-spellcheck-error">'.$word.'</span><span class="HA-spellcheck-suggestions">'.$word.'</span>'.$afterword;
128//                $poscorrect = $poscorrect;
129                $poscorrect = $poscorrect + 88 + strlen($word);
130            }
131            else
132            {
133                //print "Done with line $lineindex, next line...<br><br>";
134                $poscorrect = 0;
135                $lineindex = $lineindex + 1;
136            }
137         }
138     }
139     else
140     {
141       // This one isnt used for anything at the moment!
142       $return = 'failed to open!';
143     }
144}
145else
146{
147  $returnlines=0;
148}
149$infolines .= '"Language Used":"'.$lang.'",';
150$infolines .= '"Mispelled words":"'.$counter.'",';
151$infolines .= '"Total words suggested":"'.$suggest_count.'",';
152$infolines .= '"Total Lines Checked":"'.$returnlines.'"';
153$infolines .= '};';
154$varlines = substr($varlines, 0, strlen($varlines) - 1);
155echo $varlines.'};'.$infolines.'</script>';
156
157echo '</head>
158<body onload="window.parent.finishedSpellChecking();">';
159
160foreach ($textarray as $key=>$value)
161{
162  echo $value;
163}
164
165$dictionaries = str_replace(chr(10),",", shell_exec($aspelldictionaries));
166if(ereg(",$",$dictionaries))
167  $dictionaries = ereg_replace(",$","",$dictionaries);
168echo '<div id="HA-spellcheck-dictionaries">'.$dictionaries.'</div>';
169
170echo '</body></html>';
171?>
Note: See TracBrowser for help on using the repository browser.