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

Last change on this file since 285 was 273, checked in by gogo, 14 years ago

Apply #381

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