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

Last change on this file since 95 was 21, checked in by gogo, 15 years ago

Setting more properties.

  • Property svn:eol-style set to native
  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
File size: 5.7 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
50if (trim($text) != "")
51{
52    if ($fd = fopen($temptext, 'w'))
53    {
54        $textarray = explode("\n", $text);
55        fwrite ($fd, "!\n");
56        foreach ($textarray as $key=>$value)
57        {
58            // adding the carat to each line prevents the use of aspell commands within the text...
59            fwrite($fd, "^$value\n");
60        }
61        fclose($fd);
62        chmod($temptext, 0777);
63        // next run aspell
64        $return = shell_exec($aspellcommand . ' 2>&1');
65        // echo $return;
66        unlink($temptext);
67        $returnarray = explode("\n", $return);
68        $returnlines = count($returnarray);
69//print_r(htmlentities($return));
70        $textlines = count($textarray);
71
72        $varlines = '<script type="text/javascript">var suggested_words = { ';
73        $infolines = 'var spellcheck_info = {';
74
75        $lineindex = -1;
76        $poscorrect = 0;
77        $counter = 0;
78        $suggest_count = 0;
79        foreach ($returnarray as $key=>$value)
80        {
81            // if there is a correction here, processes it, else move the $textarray pointer to the next line
82            if (substr($value, 0, 1) == '&')
83            {
84               $counter=$counter+1;
85                $correction = explode(' ', $value);
86                $word = $correction[1];
87                $suggest_count += $correction[2];
88                $absposition = substr($correction[3], 0, -1) - 1;
89                $position = $absposition + $poscorrect;
90                $niceposition = $lineindex.','.$absposition;
91                $suggstart = strpos($value, ':') + 2;
92                $suggestions = substr($value, $suggstart);
93                $suggestionarray = explode(', ', $suggestions);
94
95                $beforeword = substr($textarray[$lineindex], 0, $position);
96                $afterword = substr($textarray[$lineindex], $position + strlen($word));
97                $textarray[$lineindex] = $beforeword.'<span class="HA-spellcheck-error">'.$word.'</span>'.$afterword;
98
99             $suggestion_list = '';
100                foreach ($suggestionarray as $key=>$value)
101                {
102                    $suggestion_list .= $value.',';
103                }
104                $suggestion_list = substr($suggestion_list, 0, strlen($suggestion_list) - 1);
105                $varlines .= '"'.$word.'":"'.$suggestion_list.'",';
106
107                $poscorrect = $poscorrect + 41;
108            }
109            elseif (substr($value, 0, 1) == '#')
110            {
111                $correction = explode(' ', $value);
112                $word = $correction[1];
113                $absposition = $correction[2] - 1;
114                $position = $absposition + $poscorrect;
115                $niceposition = $lineindex.','.$absposition;
116                $beforeword = substr($textarray[$lineindex], 0, $position);
117                $afterword = substr($textarray[$lineindex], $position + strlen($word));
118                $textarray[$lineindex] = $beforeword.$word.$afterword;
119                $textarray[$lineindex] = $beforeword.'<span class="HA-spellcheck-error">'.$word.'</span><span class="HA-spellcheck-suggestions">'.$word.'</span>'.$afterword;
120//                $poscorrect = $poscorrect;
121                $poscorrect = $poscorrect + 88 + strlen($word);
122            }
123            else
124            {
125                //print "Done with line $lineindex, next line...<br><br>";
126                $poscorrect = 0;
127                $lineindex = $lineindex + 1;
128            }
129         }
130     }
131     else
132     {
133       $return = 'failed to open!';
134     }
135}
136$infolines .= '"Language Used":"'.$lang.'",';
137$infolines .= '"Mispelled words":"'.$counter.'",';
138$infolines .= '"Total words suggested":"'.$suggest_count.'",';
139$infolines .= '"Total Lines Checked":"'.$returnlines.'"';
140$infolines .= '};';
141$varlines = substr($varlines, 0, strlen($varlines) - 1);
142echo $varlines.'};'.$infolines.'</script>';
143
144echo '</head>
145<body onload="window.parent.finishedSpellChecking();">';
146
147foreach ($textarray as $key=>$value)
148{
149  echo $value;
150}
151
152$dictionaries = str_replace(chr(10),",", shell_exec($aspelldictionaries));
153echo '<div id="HA-spellcheck-dictionaries">'.$dictionaries.'</div>';
154
155echo '</body></html>';
156//echo '<div id="HA-spellcheck-dictionaries">en_US,es,fr</div></body></html>';
157?>
Note: See TracBrowser for help on using the repository browser.