Ticket #204: spell-check-logic.php

File spell-check-logic.php, 7.2 kB (added by kim@…, 8 years ago)

Revised file

Line 
1<?php
2/* Some enhancements and fixes ny kim@steinhaug.com, www.steinhaug.com     
3                                                                           
4   - When spellchecking empty document, some of the HTML was generated     
5     inside the logic, resulting in faulty HTML code being presented.     
6   - The documented is supposed to be utf-8, but the output was never     
7     converted to utf-8.                                                   
8   - When listing the dictionaries an extra , was presented in the string 
9     which resulted in the JS logic to create empty dictionary in dropdown
10   - Debug mode, run this file directly in browser and file is served as   
11     plain text, its easier to debug this way.                             
12                                                                         */
13
14// Mode for debugging the code :
15$debug = false;
16
17  $text = stripslashes($_POST['content']);
18  if($debug) $text = 'This is a wrrong sppelled word for tesling pruposes';
19
20  // Convert UTF-8 multi-bytes into decimal character entities.  This is because
21  // aspell isn't fully utf8-aware
22  $text = preg_replace('/([\xC0-\xDF][\x80-\xBF])/e', "'&#' . utf8_ord('\$1') . ';'", $text);
23  $text = preg_replace('/([\xE0-\xEF][\x80-\xBF][\x80-\xBF])/e',             "'&#' . utf8_ord('\$1') . ';'"$text);
24  $text = preg_replace('/([\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF])/e', "'&#' . utf8_ord('\$1') . ';'",   $text);
25
26  function utf8_ord($chr)
27  {
28    switch(strlen($chr))
29    {
30      case 1 :
31        return ord($chr);
32
33      case 2 :
34        $ord = ord($chr{1}) & 63;
35        $ord = $ord | ((ord($chr{0}) & 31) << 6);
36        return $ord;
37
38      case 3 :
39        $ord = ord($chr{2}) & 63;
40        $ord = $ord | ((ord($chr{1}) & 63) << 6);
41        $ord = $ord | ((ord($chr{0}) & 15) << 12);
42        return $ord;
43
44      case 4 :
45        $ord = ord($chr{3}) & 63;
46        $ord = $ord | ((ord($chr{2}) & 63) << 6);
47        $ord = $ord | ((ord($chr{1}) & 63) << 12);
48        $ord = $ord | ((ord($chr{0}) & 7)  << 18);
49        return $ord;
50
51      default :
52        trigger_error('Character not utf-8', E_USER_ERROR);
53    }
54  }
55
56  require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'aspell_setup.php');
57
58##############################################################################
59if(!$debug)
60  header('Content-Type: text/html; charset=utf-8');
61else
62  header('Content-Type: text/plain; charset=utf-8');
63
64  echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
65<html>
66<head>
67<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
68<link rel="stylesheet" type="text/css" media="all" href="spell-check-style.css" />';
69
70// Lets define some values outside the condition below, in case we have an empty
71// document.                                                                     
72$textarray = array();
73$varlines = '<script type="text/javascript">var suggested_words = { ';
74$infolines = 'var spellcheck_info = {';
75$counter = 0;
76$suggest_count = 0;
77
78if (trim($text) != "")
79{
80    if ($fd = fopen($temptext, 'w'))
81    {
82        $textarray = explode("\n", $text);
83        fwrite ($fd, "!\n");
84        foreach ($textarray as $key=>$value)
85        {
86            // adding the carat to each line prevents the use of aspell commands within the text...
87            fwrite($fd, "^$value\n");
88        }
89        fclose($fd);
90        chmod($temptext, 0777);
91        // next run aspell
92        $return = shell_exec($aspellcommand . ' 2>&1');
93        // echo $return;
94        unlink($temptext);
95        $returnarray = explode("\n", $return);
96        $returnlines = count($returnarray);
97//print_r(htmlentities($return));
98        $textlines = count($textarray);
99
100        $lineindex = -1;
101        $poscorrect = 0;
102        foreach ($returnarray as $key=>$value)
103        {
104            // if there is a correction here, processes it, else move the $textarray pointer to the next line
105            if (substr($value, 0, 1) == '&')
106            {
107               $counter=$counter+1;
108                $correction = explode(' ', $value);
109                $word = $correction[1];
110                $suggest_count += $correction[2];
111                $absposition = substr($correction[3], 0, -1) - 1;
112                $position = $absposition + $poscorrect;
113                $niceposition = $lineindex.','.$absposition;
114                $suggstart = strpos($value, ':') + 2;
115                $suggestions = substr($value, $suggstart);
116                $suggestionarray = explode(', ', $suggestions);
117
118                $beforeword = substr($textarray[$lineindex], 0, $position);
119                $afterword = substr($textarray[$lineindex], $position + strlen($word));
120                $textarray[$lineindex] = $beforeword.'<span class="HA-spellcheck-error">'.$word.'</span>'.$afterword;
121
122             $suggestion_list = '';
123                foreach ($suggestionarray as $key=>$value)
124                {
125                    $suggestion_list .= $value.',';
126                }
127                $suggestion_list = substr($suggestion_list, 0, strlen($suggestion_list) - 1);
128                $varlines .= '"'.$word.'":"'.$suggestion_list.'",';
129
130                $poscorrect = $poscorrect + 41;
131            }
132            elseif (substr($value, 0, 1) == '#')
133            {
134                $correction = explode(' ', $value);
135                $word = $correction[1];
136                $absposition = $correction[2] - 1;
137                $position = $absposition + $poscorrect;
138                $niceposition = $lineindex.','.$absposition;
139                $beforeword = substr($textarray[$lineindex], 0, $position);
140                $afterword = substr($textarray[$lineindex], $position + strlen($word));
141                $textarray[$lineindex] = $beforeword.$word.$afterword;
142                $textarray[$lineindex] = $beforeword.'<span class="HA-spellcheck-error">'.$word.'</span><span class="HA-spellcheck-suggestions">'.$word.'</span>'.$afterword;
143//                $poscorrect = $poscorrect;
144                $poscorrect = $poscorrect + 88 + strlen($word);
145            }
146            else
147            {
148                //print "Done with line $lineindex, next line...<br><br>";
149                $poscorrect = 0;
150                $lineindex = $lineindex + 1;
151            }
152         }
153     }
154     else
155     {
156       // This one isnt used for anything at the moment!
157       $return = 'failed to open!';
158     }
159}
160$infolines .= '"Language Used":"'.$lang.'",';
161$infolines .= '"Mispelled words":"'.$counter.'",';
162$infolines .= '"Total words suggested":"'.$suggest_count.'",';
163$infolines .= '"Total Lines Checked":"'.$returnlines.'"';
164$infolines .= '};';
165$varlines = substr($varlines, 0, strlen($varlines) - 1);
166echo utf8_encode($varlines).'};'.utf8_encode($infolines).'</script>';
167
168echo '</head>
169<body onload="window.parent.finishedSpellChecking();">';
170
171foreach ($textarray as $key=>$value)
172{
173  echo utf8_encode($value);
174}
175
176$dictionaries = str_replace(chr(10),",", shell_exec($aspelldictionaries));
177if(ereg(",$",$dictionaries))
178  $dictionaries = ereg_replace(",$","",$dictionaries);
179echo '<div id="HA-spellcheck-dictionaries">'.$dictionaries.'</div>';
180
181echo '</body></html>';
182?>