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