d1bb0fd7de25527f6dafe638c0fa18cba512b340
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Controller / SpellCheckingController.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Spell checking plugin 'tx_rtehtmlarea_pi1' for the htmlArea RTE extension.
21 *
22 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
23 */
24 class SpellCheckingController {
25
26 /**
27 * @var \TYPO3\CMS\Core\Charset\CharsetConverter
28 */
29 protected $csConvObj;
30
31 // The extension key
32 /**
33 * @var string
34 */
35 public $extKey = 'rtehtmlarea';
36
37 /**
38 * @var string
39 */
40 public $siteUrl;
41
42 /**
43 * @var string
44 */
45 public $charset = 'utf-8';
46
47 /**
48 * @var string
49 */
50 public $parserCharset = 'utf-8';
51
52 /**
53 * @var string
54 */
55 public $defaultAspellEncoding = 'utf-8';
56
57 /**
58 * @var string
59 */
60 public $aspellEncoding;
61
62 /**
63 * @var string
64 */
65 public $result;
66
67 /**
68 * @var string
69 */
70 public $text;
71
72 /**
73 * @var array
74 */
75 public $misspelled = array();
76
77 /**
78 * @var array
79 */
80 public $suggestedWords;
81
82 /**
83 * @var int
84 */
85 public $wordCount = 0;
86
87 /**
88 * @var int
89 */
90 public $suggestionCount = 0;
91
92 /**
93 * @var int
94 */
95 public $suggestedWordCount = 0;
96
97 /**
98 * @var int
99 */
100 public $pspell_link;
101
102 /**
103 * @var string
104 */
105 public $pspellMode = 'normal';
106
107 /**
108 * @var string
109 */
110 public $dictionary;
111
112 /**
113 * @var string
114 */
115 public $AspellDirectory;
116
117 /**
118 * @var bool
119 */
120 public $pspell_is_available;
121
122 /**
123 * @var bool
124 */
125 public $forceCommandMode = 0;
126
127 /**
128 * @var string
129 */
130 public $filePrefix = 'rtehtmlarea_';
131
132 // Pre-FAL backward compatibility
133 protected $uploadFolder = 'uploads/tx_rtehtmlarea/';
134
135 // Path to main dictionary
136 protected $mainDictionaryPath;
137
138 // Path to personal dictionary
139 protected $personalDictionaryPath;
140
141 /**
142 * @var string
143 */
144 public $xmlCharacterData = '';
145
146 /**
147 * Main class of Spell Checker plugin for Typo3 CMS
148 *
149 * @return string content produced by the plugin
150 */
151 public function main() {
152 $this->csConvObj = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Charset\CharsetConverter::class);
153 // Setting start time
154 $time_start = microtime(TRUE);
155 $this->pspell_is_available = in_array('pspell', get_loaded_extensions());
156 $this->AspellDirectory = trim($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey]['plugins']['SpellChecker']['AspellDirectory']) ?: '/usr/bin/aspell';
157 // Setting command mode if requested and available
158 $this->forceCommandMode = trim($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey]['plugins']['SpellChecker']['forceCommandMode']) ?: 0;
159 if (!$this->pspell_is_available || $this->forceCommandMode) {
160 $AspellVersionString = explode('Aspell', shell_exec($this->AspellDirectory . ' -v'));
161 $AspellVersion = substr($AspellVersionString[1], 0, 4);
162 if (doubleval($AspellVersion) < doubleval('0.5') && (!$this->pspell_is_available || $this->forceCommandMode)) {
163 echo 'Configuration problem: Aspell version ' . $AspellVersion . ' too old. Spell checking cannot be performed in command mode.';
164 }
165 $this->defaultAspellEncoding = trim(shell_exec($this->AspellDirectory . ' config encoding'));
166 }
167 // Setting the list of dictionaries
168 $dictionaryList = shell_exec($this->AspellDirectory . ' dump dicts');
169 $dictionaryList = implode(',', GeneralUtility::trimExplode(LF, $dictionaryList, TRUE));
170 $dictionaryArray = GeneralUtility::trimExplode(',', $dictionaryList, TRUE);
171 $restrictToDictionaries = GeneralUtility::_POST('restrictToDictionaries');
172 if ($restrictToDictionaries) {
173 $dictionaryArray = array_intersect($dictionaryArray, GeneralUtility::trimExplode(',', $restrictToDictionaries, 1));
174 }
175 if (!count($dictionaryArray)) {
176 $dictionaryArray[] = 'en';
177 }
178 $this->dictionary = GeneralUtility::_POST('dictionary');
179 $defaultDictionary = $this->dictionary;
180 if (!$defaultDictionary || !in_array($defaultDictionary, $dictionaryArray)) {
181 $defaultDictionary = 'en';
182 }
183 uasort($dictionaryArray, 'strcoll');
184 $dictionaryList = implode(',', $dictionaryArray);
185 // Setting the dictionary
186 if (empty($this->dictionary) || !in_array($this->dictionary, $dictionaryArray)) {
187 $this->dictionary = 'en';
188 }
189 // Setting the pspell suggestion mode
190 $this->pspellMode = GeneralUtility::_POST('pspell_mode') ? GeneralUtility::_POST('pspell_mode') : $this->pspellMode;
191 // Now sanitize $this->pspellMode
192 $this->pspellMode = GeneralUtility::inList('ultra,fast,normal,bad-spellers', $this->pspellMode) ? $this->pspellMode : 'normal';
193 switch ($this->pspellMode) {
194 case 'ultra':
195
196 case 'fast':
197 $pspellModeFlag = PSPELL_FAST;
198 break;
199 case 'bad-spellers':
200 $pspellModeFlag = PSPELL_BAD_SPELLERS;
201 break;
202 case 'normal':
203
204 default:
205 $pspellModeFlag = PSPELL_NORMAL;
206 }
207 // Setting the charset
208 if (GeneralUtility::_POST('pspell_charset')) {
209 $this->charset = trim(GeneralUtility::_POST('pspell_charset'));
210 }
211 if (strtolower($this->charset) == 'iso-8859-1') {
212 $this->parserCharset = strtolower($this->charset);
213 }
214 // In some configurations, Aspell uses 'iso8859-1' instead of 'iso-8859-1'
215 $this->aspellEncoding = $this->parserCharset;
216 if ($this->parserCharset == 'iso-8859-1' && strstr($this->defaultAspellEncoding, '8859-1')) {
217 $this->aspellEncoding = $this->defaultAspellEncoding;
218 }
219 // However, we are going to work only in the parser charset
220 if ($this->pspell_is_available && !$this->forceCommandMode) {
221 $this->pspell_link = pspell_new($this->dictionary, '', '', $this->parserCharset, $pspellModeFlag);
222 }
223 // Setting the path to main dictionary
224 $this->setMainDictionaryPath();
225 // Setting the path to user personal dictionary, if any
226 $this->setPersonalDictionaryPath();
227 $this->fixPersonalDictionaryCharacterSet();
228 $cmd = GeneralUtility::_POST('cmd');
229 if ($cmd == 'learn') {
230 // Only availble for BE_USERS, die silently if someone has gotten here by accident
231 if (TYPO3_MODE !== 'BE' || !is_object($GLOBALS['BE_USER'])) {
232 die('');
233 }
234 // Updating the personal word list
235 $to_p_dict = GeneralUtility::_POST('to_p_dict');
236 $to_p_dict = $to_p_dict ? $to_p_dict : array();
237 $to_r_list = GeneralUtility::_POST('to_r_list');
238 $to_r_list = $to_r_list ? $to_r_list : array();
239 header('Content-Type: text/plain; charset=' . strtoupper($this->parserCharset));
240 header('Pragma: no-cache');
241 if ($to_p_dict || $to_r_list) {
242 $tmpFileName = GeneralUtility::tempnam($this->filePrefix);
243 $filehandle = fopen($tmpFileName, 'wb');
244 if ($filehandle) {
245 // Get the character set of the main dictionary
246 // We need to convert the input into the character set of the main dictionary
247 $mainDictionaryCharacterSet = $this->getMainDictionaryCharacterSet();
248 // Write the personal words addition commands to the temporary file
249 foreach ($to_p_dict as $personal_word) {
250 $cmd = '&' . $this->csConvObj->conv($personal_word, $this->parserCharset, $mainDictionaryCharacterSet) . LF;
251 fwrite($filehandle, $cmd, strlen($cmd));
252 }
253 // Write the replacent pairs addition commands to the temporary file
254 foreach ($to_r_list as $replace_pair) {
255 $cmd = '$$ra ' . $this->csConvObj->conv($replace_pair[0], $this->parserCharset, $mainDictionaryCharacterSet) . ' , ' . $this->csConvObj->conv($replace_pair[1], $this->parserCharset, $mainDictionaryCharacterSet) . LF;
256 fwrite($filehandle, $cmd, strlen($cmd));
257 }
258 $cmd = '#' . LF;
259 $result = fwrite($filehandle, $cmd, strlen($cmd));
260 if ($result === FALSE) {
261 GeneralUtility::sysLog('SpellChecker tempfile write error: ' . $tmpFileName, $this->extKey, GeneralUtility::SYSLOG_SEVERITY_ERROR);
262 } else {
263 // Assemble the Aspell command
264 $aspellCommand = ((TYPO3_OS === 'WIN') ? 'type ' : 'cat ') . escapeshellarg($tmpFileName) . ' | '
265 . $this->AspellDirectory
266 . ' -a --mode=none'
267 . ($this->personalDictionaryPath ? ' --home-dir=' . escapeshellarg($this->personalDictionaryPath) : '')
268 . ' --lang=' . escapeshellarg($this->dictionary)
269 . ' --encoding=' . escapeshellarg($mainDictionaryCharacterSet)
270 . ' 2>&1';
271 $aspellResult = shell_exec($aspellCommand);
272 // Close and delete the temporary file
273 fclose($filehandle);
274 GeneralUtility::unlink_tempfile($tmpFileName);
275 }
276 } else {
277 GeneralUtility::sysLog('SpellChecker tempfile open error: ' . $tmpFileName, $this->extKey, GeneralUtility::SYSLOG_SEVERITY_ERROR);
278 }
279 }
280 flush();
281 die;
282 } else {
283 // Check spelling content
284 // Initialize output
285 $this->result = '<?xml version="1.0" encoding="' . $this->parserCharset . '"?>
286 <!DOCTYPE html
287 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
288 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
289 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . substr($this->dictionary, 0, 2) . '" lang="' . substr($this->dictionary, 0, 2) . '">
290 <head>
291 <meta http-equiv="Content-Type" content="text/html; charset=' . $this->parserCharset . '" />
292 <link rel="stylesheet" type="text/css" media="all" href="' . (TYPO3_MODE == 'BE' ? '../' : '') . \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::siteRelPath($this->extKey) . '/Resources/Public/Css/Skin/Plugins/spell-checker-iframe.css" />
293 <script type="text/javascript">
294 /*<![CDATA[*/
295 <!--
296 ';
297 // Getting the input content
298 $content = GeneralUtility::_POST('content');
299 // Parsing the input HTML
300 $parser = xml_parser_create(strtoupper($this->parserCharset));
301 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
302 xml_set_object($parser, $this);
303 if (!xml_set_element_handler($parser, 'startHandler', 'endHandler')) {
304 echo 'Bad xml handler setting';
305 }
306 if (!xml_set_character_data_handler($parser, 'collectDataHandler')) {
307 echo 'Bad xml handler setting';
308 }
309 if (!xml_set_default_handler($parser, 'defaultHandler')) {
310 echo 'Bad xml handler setting';
311 }
312 if (!xml_parse($parser, ('<?xml version="1.0" encoding="' . $this->parserCharset . '"?><spellchecker> ' . preg_replace(('/&nbsp;/' . ($this->parserCharset == 'utf-8' ? 'u' : '')), ' ', $content) . ' </spellchecker>'))) {
313 echo 'Bad parsing';
314 }
315 if (xml_get_error_code($parser)) {
316 throw new \UnexpectedException('Line ' . xml_get_current_line_number($parser) . ': ' . xml_error_string(xml_get_error_code($parser)), 1294585788);
317 }
318 xml_parser_free($parser);
319 if ($this->pspell_is_available && !$this->forceCommandMode) {
320 pspell_clear_session($this->pspell_link);
321 }
322 $this->result .= 'var suggestedWords = {' . $this->suggestedWords . '};
323 var dictionaries = "' . $dictionaryList . '";
324 var selectedDictionary = "' . $this->dictionary . '";
325 ';
326 // Calculating parsing and spell checkting time
327 $time = number_format(microtime(TRUE) - $time_start, 2, ',', ' ');
328 // Insert spellcheck info
329 $this->result .= 'var spellcheckInfo = { "Total words":"' . $this->wordCount . '","Misspelled words":"' . sizeof($this->misspelled) . '","Total suggestions":"' . $this->suggestionCount . '","Total words suggested":"' . $this->suggestedWordCount . '","Spelling checked in":"' . $time . '" };
330 // -->
331 /*]]>*/
332 </script>
333 </head>
334 ';
335 $this->result .= '<body onload="window.parent.RTEarea[\'' . GeneralUtility::_POST('editorId') . '\'].editor.getPlugin(\'SpellChecker\').spellCheckComplete();">';
336 $this->result .= preg_replace('/' . preg_quote('<?xml') . '.*' . preg_quote('?>') . '[' . preg_quote((LF . CR . chr(32))) . ']*/' . ($this->parserCharset == 'utf-8' ? 'u' : ''), '', $this->text);
337 $this->result .= '<div style="display: none;">' . $dictionaries . '</div>';
338 // Closing
339 $this->result .= '
340 </body></html>';
341 // Outputting
342 header('Content-Type: text/html; charset=' . strtoupper($this->parserCharset));
343 echo $this->result;
344 }
345 }
346
347 /**
348 * Sets the path to the main dictionary
349 *
350 * @return string path to the main dictionary
351 */
352 protected function setMainDictionaryPath() {
353 $this->mainDictionaryPath = '';
354 $aspellCommand = $this->AspellDirectory . ' config dict-dir';
355 $aspellResult = shell_exec($aspellCommand);
356 if ($aspellResult) {
357 $this->mainDictionaryPath = trim($aspellResult);
358 }
359 if (!$aspellResult || !$this->mainDictionaryPath) {
360 GeneralUtility::sysLog('SpellChecker main dictionary path retrieval error: ' . $aspellCommand, $this->extKey, GeneralUtility::SYSLOG_SEVERITY_ERROR);
361 }
362 return $this->mainDictionaryPath;
363 }
364
365 /**
366 * Gets the character set the main dictionary
367 *
368 * @return string character set the main dictionary
369 */
370 protected function getMainDictionaryCharacterSet() {
371 $characterSet = '';
372 if ($this->mainDictionaryPath) {
373 // Keep only the first part of the dictionary name
374 $mainDictionary = preg_split('/[-_]/', $this->dictionary, 2);
375 // Read the options of the dictionary
376 $dictionaryFileName = $this->mainDictionaryPath . '/' . $mainDictionary[0] . '.dat';
377 $dictionaryHandle = fopen($dictionaryFileName, 'rb');
378 if (!$dictionaryHandle) {
379 GeneralUtility::sysLog('SpellChecker main dictionary open error: ' . $dictionaryFileName, $this->extKey, GeneralUtility::SYSLOG_SEVERITY_ERROR);
380 } else {
381 $dictionaryContent = fread($dictionaryHandle, 500);
382 if ($dictionaryContent === FALSE) {
383 GeneralUtility::sysLog('SpellChecker main dictionary read error: ' . $dictionaryFileName, $this->extKey, GeneralUtility::SYSLOG_SEVERITY_ERROR);
384 } else {
385 fclose($dictionaryHandle);
386 // Get the line that contains the character set option
387 $dictionaryContent = preg_split('/charset\s*/', $dictionaryContent, 2);
388 if ($dictionaryContent[1]) {
389 // Isolate the character set
390 $dictionaryContent = GeneralUtility::trimExplode(LF, $dictionaryContent[1]);
391 // Fix Aspell character set oddity (i.e. iso8859-1)
392 $characterSet = str_replace(
393 array('iso', '--'),
394 array('iso-', '-'),
395 $dictionaryContent[0]
396 );
397 }
398 if (!$characterSet) {
399 GeneralUtility::sysLog('SpellChecker main dictionary character set retrieval error: ' . $dictionaryContent[1], $this->extKey, GeneralUtility::SYSLOG_SEVERITY_ERROR);
400 }
401 }
402 }
403 }
404 return $characterSet;
405 }
406
407 /**
408 * Sets the path to the personal dictionary
409 *
410 * @return string path to the personal dictionary
411 */
412 protected function setPersonalDictionaryPath() {
413 $this->personalDictionaryPath = '';
414 if (GeneralUtility::_POST('enablePersonalDicts') == 'true' && TYPO3_MODE == 'BE' && is_object($GLOBALS['BE_USER'])) {
415 if ($GLOBALS['BE_USER']->user['uid']) {
416 $personalDictionaryFolderName = 'BE_' . $GLOBALS['BE_USER']->user['uid'];
417 // Check for pre-FAL personal dictionary folder
418 try {
419 $personalDictionaryFolder = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier(PATH_site . $this->uploadFolder . $personalDictionaryFolderName);
420 } catch (\Exception $e) {
421 $personalDictionaryFolder = FALSE;
422 }
423 // The personal dictionary folder is created in the user's default upload folder and named BE_(uid)_personaldictionary
424 if (!$personalDictionaryFolder) {
425 $personalDictionaryFolderName .= '_personaldictionary';
426 $backendUserDefaultFolder = $GLOBALS['BE_USER']->getDefaultUploadFolder();
427 if ($backendUserDefaultFolder->hasFolder($personalDictionaryFolderName)) {
428 $personalDictionaryFolder = $backendUserDefaultFolder->getSubfolder($personalDictionaryFolderName);
429 } else {
430 $personalDictionaryFolder = $backendUserDefaultFolder->createFolder($personalDictionaryFolderName);
431 }
432 }
433 $this->personalDictionaryPath = PATH_site . rtrim($personalDictionaryFolder->getPublicUrl(), '/');
434 }
435 }
436 return $this->personalDictionaryPath;
437 }
438
439 /**
440 * Ensures that the personal dictionary is utf-8 encoded
441 *
442 * @return void
443 */
444 protected function fixPersonalDictionaryCharacterSet() {
445 if ($this->personalDictionaryPath) {
446 // Fix the options of the personl word list and of the replacement pairs files
447 // Aspell creates such files only for the main dictionary
448 $fileNames = array();
449 $mainDictionary = preg_split('/[-_]/', $this->dictionary, 2);
450 $fileNames[0] = $this->personalDictionaryPath . '/' . '.aspell.' . $mainDictionary[0] . '.pws';
451 $fileNames[1] = $this->personalDictionaryPath . '/' . '.aspell.' . $mainDictionary[0] . '.prepl';
452 foreach ($fileNames as $fileName) {
453 if (file_exists($fileName)) {
454 $fileContent = file_get_contents($fileName);
455 if ($fileContent === FALSE) {
456 GeneralUtility::sysLog('SpellChecker personal word list read error: ' . $fileName, $this->extKey, GeneralUtility::SYSLOG_SEVERITY_ERROR);
457 } else {
458 $fileContent = explode(LF, $fileContent);
459 if (strpos($fileContent[0], 'utf-8') === FALSE) {
460 $fileContent[0] .= ' utf-8';
461 $fileContent = implode(LF, $fileContent);
462 $result = file_put_contents($fileName, $fileContent);
463 if ($result === FALSE) {
464 GeneralUtility::sysLog('SpellChecker personal word list write error: ' . $fileName, $this->extKey, GeneralUtility::SYSLOG_SEVERITY_ERROR);
465 }
466 }
467 }
468 }
469 }
470 }
471 }
472
473 /**
474 * Handler for the opening of a tag
475 */
476 public function startHandler($xml_parser, $tag, $attributes) {
477 if (strlen($this->xmlCharacterData)) {
478 $this->spellCheckHandler($xml_parser, $this->xmlCharacterData);
479 $this->xmlCharacterData = '';
480 }
481 switch ($tag) {
482 case 'spellchecker':
483 break;
484 case 'br':
485
486 case 'BR':
487
488 case 'img':
489
490 case 'IMG':
491
492 case 'hr':
493
494 case 'HR':
495
496 case 'area':
497
498 case 'AREA':
499 $this->text .= '<' . $this->csConvObj->conv_case($this->parserCharset, $tag, 'toLower') . ' ';
500 foreach ($attributes as $key => $val) {
501 $this->text .= $key . '="' . $val . '" ';
502 }
503 $this->text .= ' />';
504 break;
505 default:
506 $this->text .= '<' . $this->csConvObj->conv_case($this->parserCharset, $tag, 'toLower') . ' ';
507 foreach ($attributes as $key => $val) {
508 $this->text .= $key . '="' . $val . '" ';
509 }
510 $this->text .= '>';
511 }
512 }
513
514 /**
515 * Handler for the closing of a tag
516 */
517 public function endHandler($xml_parser, $tag) {
518 if (strlen($this->xmlCharacterData)) {
519 $this->spellCheckHandler($xml_parser, $this->xmlCharacterData);
520 $this->xmlCharacterData = '';
521 }
522 switch ($tag) {
523 case 'spellchecker':
524
525 case 'br':
526
527 case 'BR':
528
529 case 'img':
530
531 case 'IMG':
532
533 case 'hr':
534
535 case 'HR':
536
537 case 'input':
538
539 case 'INPUT':
540
541 case 'area':
542
543 case 'AREA':
544 break;
545 default:
546 $this->text .= '</' . $tag . '>';
547 }
548 }
549
550 /**
551 * Handler for the content of a tag
552 */
553 public function spellCheckHandler($xml_parser, $string) {
554 $incurrent = array();
555 $stringText = $string;
556 $words = preg_split($this->parserCharset == 'utf-8' ? '/\\P{L}+/u' : '/\\W+/', $stringText);
557 while (list(, $word) = each($words)) {
558 $word = preg_replace('/ /' . ($this->parserCharset == 'utf-8' ? 'u' : ''), '', $word);
559 if ($word && !is_numeric($word)) {
560 if ($this->pspell_is_available && !$this->forceCommandMode) {
561 if (!pspell_check($this->pspell_link, $word)) {
562 if (!in_array($word, $this->misspelled)) {
563 if (sizeof($this->misspelled) != 0) {
564 $this->suggestedWords .= ',';
565 }
566 $suggest = array();
567 $suggest = pspell_suggest($this->pspell_link, $word);
568 if (sizeof($suggest) != 0) {
569 $this->suggestionCount++;
570 $this->suggestedWordCount += sizeof($suggest);
571 }
572 $this->suggestedWords .= '"' . $word . '":"' . implode(',', $suggest) . '"';
573 $this->misspelled[] = $word;
574 unset($suggest);
575 }
576 if (!in_array($word, $incurrent)) {
577 $stringText = preg_replace('/\\b' . $word . '\\b/' . ($this->parserCharset == 'utf-8' ? 'u' : ''), '<span class="htmlarea-spellcheck-error">' . $word . '</span>', $stringText);
578 $incurrent[] = $word;
579 }
580 }
581 } else {
582 $tmpFileName = GeneralUtility::tempnam($this->filePrefix);
583 if (!($filehandle = fopen($tmpFileName, 'wb'))) {
584 echo 'SpellChecker tempfile open error';
585 }
586 if (!fwrite($filehandle, $word)) {
587 echo 'SpellChecker tempfile write error';
588 }
589 if (!fclose($filehandle)) {
590 echo 'SpellChecker tempfile close error';
591 }
592 $catCommand = TYPO3_OS === 'WIN' ? 'type' : 'cat';
593 $AspellCommand = $catCommand . ' ' . escapeshellarg($tmpFileName) . ' | '
594 . $this->AspellDirectory
595 . ' -a check'
596 . ' --mode=none'
597 . ' --sug-mode=' . escapeshellarg($this->pspellMode)
598 . ($this->personalDictionaryPath ? ' --home-dir=' . escapeshellarg($this->personalDictionaryPath) : '')
599 . ' --lang=' . escapeshellarg($this->dictionary)
600 . ' --encoding=' . escapeshellarg($this->aspellEncoding)
601 . ' 2>&1';
602 $AspellAnswer = shell_exec($AspellCommand);
603 $AspellResultLines = array();
604 $AspellResultLines = GeneralUtility::trimExplode(LF, $AspellAnswer, TRUE);
605 if (substr($AspellResultLines[0], 0, 6) == 'Error:') {
606 echo '{' . $AspellAnswer . '}';
607 }
608 GeneralUtility::unlink_tempfile($tmpFileName);
609 if ($AspellResultLines['1'][0] !== '*') {
610 if (!in_array($word, $this->misspelled)) {
611 if (sizeof($this->misspelled) != 0) {
612 $this->suggestedWords .= ',';
613 }
614 $suggest = array();
615 $suggestions = array();
616 if ($AspellResultLines['1'][0] === '&') {
617 $suggestions = GeneralUtility::trimExplode(':', $AspellResultLines['1'], TRUE);
618 $suggest = GeneralUtility::trimExplode(',', $suggestions['1'], TRUE);
619 }
620 if (sizeof($suggest) != 0) {
621 $this->suggestionCount++;
622 $this->suggestedWordCount += sizeof($suggest);
623 }
624 $this->suggestedWords .= '"' . $word . '":"' . implode(',', $suggest) . '"';
625 $this->misspelled[] = $word;
626 unset($suggest);
627 unset($suggestions);
628 }
629 if (!in_array($word, $incurrent)) {
630 $stringText = preg_replace('/\\b' . $word . '\\b/' . ($this->parserCharset == 'utf-8' ? 'u' : ''), '<span class="htmlarea-spellcheck-error">' . $word . '</span>', $stringText);
631 $incurrent[] = $word;
632 }
633 }
634 unset($AspellResultLines);
635 }
636 $this->wordCount++;
637 }
638 }
639 $this->text .= $stringText;
640 unset($incurrent);
641 }
642
643 /**
644 * Handler for collecting data within a tag
645 */
646 public function collectDataHandler($xml_parser, $string) {
647 $this->xmlCharacterData .= $string;
648 }
649
650 /**
651 * Default handler for the xml parser
652 */
653 public function defaultHandler($xml_parser, $string) {
654 $this->text .= $string;
655 }
656
657 }