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