Followup to #11148: Remove static require_once() to t3lib and tslib and use autoloadi...
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / pi1 / class.tx_rtehtmlarea_pi1.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2003-2009 Stanislas Rolland <typo3(arobas)sjbr.ca>
6 * All rights reserved
7 *
8 * This script is part of the Typo3 project. The Typo3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24 /**
25 * Spell checking plugin 'tx_rtehtmlarea_pi1' for the htmlArea RTE extension.
26 *
27 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
28 *
29 * TYPO3 SVN ID: $Id$
30 *
31 */
32
33 class tx_rtehtmlarea_pi1 {
34
35 protected $csConvObj;
36 var $extKey = 'rtehtmlarea'; // The extension key.
37 var $siteUrl;
38 var $charset = 'utf-8';
39 var $parserCharset = 'utf-8';
40 var $defaultAspellEncoding = 'utf-8';
41 var $aspellEncoding;
42 var $result;
43 var $text;
44 var $misspelled = array();
45 var $suggestedWords;
46 var $wordCount = 0;
47 var $suggestionCount = 0;
48 var $suggestedWordCount = 0;
49 var $pspell_link;
50 var $pspellMode = 'normal';
51 var $dictionary;
52 var $AspellDirectory;
53 var $pspell_is_available;
54 var $forceCommandMode = 0;
55 var $filePrefix = 'rtehtmlarea_';
56 var $uploadFolder = 'uploads/tx_rtehtmlarea/';
57 var $userUid;
58 var $personalDictsArg = '';
59 var $xmlCharacterData = '';
60
61 /**
62 * Main class of Spell Checker plugin for Typo3 CMS
63 *
64 * @return string content produced by the plugin
65 */
66 function main() {
67
68 $this->csConvObj = t3lib_div::makeInstance('t3lib_cs');
69
70 // Setting start time
71 $time_start = microtime(true);
72 $this->pspell_is_available = in_array('pspell', get_loaded_extensions());
73 $this->AspellDirectory = trim($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey]['plugins']['SpellChecker']['AspellDirectory'])? trim($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey]['plugins']['SpellChecker']['AspellDirectory']) : '/usr/bin/aspell';
74 $this->forceCommandMode = (trim($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey]['plugins']['SpellChecker']['forceCommandMode']))? trim($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey]['plugins']['SpellChecker']['forceCommandMode']) : 0;
75 $safe_mode_is_enabled = ini_get('safe_mode');
76 if($safe_mode_is_enabled && !$this->pspell_is_available ) echo('Configuration problem: Spell checking cannot be performed');
77 if($safe_mode_is_enabled && $this->forceCommandMode) echo('Configuration problem: Spell checking cannot be performed in command mode');
78 if(!$safe_mode_is_enabled && (!$this->pspell_is_available || $this->forceCommandMode)) {
79 $AspellVersionString = explode('Aspell', shell_exec( $this->AspellDirectory.' -v'));
80 $AspellVersion = substr( $AspellVersionString[1], 0, 4);
81 if( doubleval($AspellVersion) < doubleval('0.5') && (!$this->pspell_is_available || $this->forceCommandMode)) echo('Configuration problem: Aspell version ' . $AspellVersion . ' too old. Spell checking cannot be performed in command mode');
82 $this->defaultAspellEncoding = trim(shell_exec($this->AspellDirectory.' config encoding'));
83 }
84
85 // Setting the list of dictionaries
86 if (!$safe_mode_is_enabled && (!$this->pspell_is_available || $this->forceCommandMode)) {
87 $dictionaryList = shell_exec( $this->AspellDirectory.' dump dicts');
88 $dictionaryList = implode(',', t3lib_div::trimExplode(chr(10), $dictionaryList, 1));
89 }
90 if (empty($dictionaryList)) {
91 $dictionaryList = t3lib_div::_POST('showDictionaries');
92 // Applying EM variable DEPRECATED as of TYPO3 4.3.0
93 $dictionaryList = $dictionaryList ? $dictionaryList : trim($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey]['plugins']['SpellChecker']['dictionaryList']);
94 }
95 $dictionaryArray = t3lib_div::trimExplode(',', $dictionaryList, 1);
96 $restrictToDictionaries = t3lib_div::_POST('restrictToDictionaries');
97 if ($restrictToDictionaries) {
98 $dictionaryArray = array_intersect($dictionaryArray, t3lib_div::trimExplode(',', $restrictToDictionaries, 1));
99 }
100 if (!count($dictionaryArray)) {
101 $dictionaryArray[] = 'en';
102 }
103 $this->dictionary = t3lib_div::_POST('dictionary');
104 // Applying EM variable DEPRECATED as of TYPO3 4.3.0
105 $defaultDictionary = $this->dictionary ? $this->dictionary : trim($TYPO3_CONF_VARS['EXTCONF'][$this->extKey]['plugins']['SpellChecker']['defaultDictionary']);
106 if (!$defaultDictionary || !in_array($defaultDictionary, $dictionaryArray)) {
107 $defaultDictionary = 'en';
108 }
109 uasort($dictionaryArray, 'strcoll');
110 $dictionaryList = implode(',', $dictionaryArray);
111 // Setting the dictionary
112 if (empty($this->dictionary) || !in_array($this->dictionary, $dictionaryArray)) {
113 $this->dictionary = 'en';
114 }
115 $dictionaries = substr_replace($dictionaryList, '@'.$this->dictionary, strpos($dictionaryList, $this->dictionary), strlen($this->dictionary));
116
117 // Setting the pspell suggestion mode
118 $this->pspellMode = t3lib_div::_POST('pspell_mode')?t3lib_div::_POST('pspell_mode'): $this->pspellMode;
119 // Now sanitize $this->pspellMode
120 $this->pspellMode = t3lib_div::inList('ultra,fast,normal,bad-spellers',$this->pspellMode)?$this->pspellMode:'normal';
121 switch($this->pspellMode) {
122 case 'ultra':
123 case 'fast':
124 $pspellModeFlag = PSPELL_FAST;
125 break;
126 case 'bad-spellers':
127 $pspellModeFlag = PSPELL_BAD_SPELLERS;
128 break;
129 case 'normal':
130 default:
131 $pspellModeFlag = PSPELL_NORMAL;
132 break;
133 }
134
135 // Setting the charset
136 if (t3lib_div::_POST('pspell_charset')) {
137 $this->charset = trim(t3lib_div::_POST('pspell_charset'));
138 }
139 if (strtolower($this->charset) == 'iso-8859-1') {
140 $this->parserCharset = strtolower($this->charset);
141 }
142
143 // In some configurations, Aspell uses 'iso8859-1' instead of 'iso-8859-1'
144 $this->aspellEncoding = $this->parserCharset;
145 if ($this->parserCharset == 'iso-8859-1' && strstr($this->defaultAspellEncoding, '8859-1')) {
146 $this->aspellEncoding = $this->defaultAspellEncoding;
147 }
148
149 // However, we are going to work only in the parser charset
150 if($this->pspell_is_available && !$this->forceCommandMode) {
151 $this->pspell_link = pspell_new($this->dictionary, '', '', $this->parserCharset, $pspellModeFlag);
152 }
153
154 // Setting the path to user personal dicts, if any
155 if (t3lib_div::_POST('enablePersonalDicts') == 'true' && TYPO3_MODE == 'BE' && is_object($GLOBALS['BE_USER'])) {
156 $this->userUid = 'BE_' . $GLOBALS['BE_USER']->user['uid'];
157 if ($this->userUid) {
158 $this->personalDictPath = t3lib_div::getFileAbsFileName($this->uploadFolder . $this->userUid);
159 if (!is_dir($this->personalDictPath)) {
160 t3lib_div::mkdir($this->personalDictPath);
161 }
162 // escape here for later use
163 $this->personalDictsArg = ' --home-dir=' . escapeshellarg($this->personalDictPath);
164 }
165 }
166
167 $cmd = t3lib_div::_POST('cmd');
168 if ($cmd == 'learn' && !$safe_mode_is_enabled) {
169 // Only availble for BE_USERS, die silently if someone has gotten here by accident
170 if (TYPO3_MODE !='BE' || !is_object($GLOBALS['BE_USER'])) die('');
171 // Updating the personal word list
172 $to_p_dict = t3lib_div::_POST('to_p_dict');
173 $to_p_dict = $to_p_dict ? $to_p_dict : array();
174 $to_r_list = t3lib_div::_POST('to_r_list');
175 $to_r_list = $to_r_list ? $to_r_list : array();
176 header('Content-Type: text/plain; charset=' . strtoupper($this->parserCharset));
177 header('Pragma: no-cache');
178 if($to_p_dict || $to_r_list) {
179 $tmpFileName = t3lib_div::tempnam($this->filePrefix);
180 if($filehandle = fopen($tmpFileName,'wb')) {
181 foreach ($to_p_dict as $personal_word) {
182 $cmd = '&' . $personal_word . "\n";
183 echo $cmd;
184 fwrite($filehandle, $cmd, strlen($cmd));
185 }
186 foreach ($to_r_list as $replace_pair) {
187 $cmd = '$$ra ' . $replace_pair[0] . ' , ' . $replace_pair[1] . "\n";
188 echo $cmd;
189 fwrite($filehandle, $cmd, strlen($cmd));
190 }
191 $cmd = "#\n";
192 echo $cmd;
193 fwrite($filehandle, $cmd, strlen($cmd));
194 fclose($filehandle);
195 // $this->personalDictsArg has already been escapeshellarg()'ed above, it is an optional paramter and might be empty here
196 $AspellCommand = 'cat ' . escapeshellarg($tmpFileName) . ' | ' . $this->AspellDirectory . ' -a --mode=none' . $this->personalDictsArg . ' --lang=' . escapeshellarg($this->dictionary) . ' --encoding=' . escapeshellarg($this->aspellEncoding) . ' 2>&1';
197 print $AspellCommand . "\n";
198 print shell_exec($AspellCommand);
199 t3lib_div::unlink_tempfile($tmpFileName);
200 echo('Personal word list was updated.');
201 } else {
202 echo('SpellChecker tempfile open error.');
203 }
204 } else {
205 echo('Nothing to add to the personal word list.');
206 }
207 flush();
208 exit();
209 } else {
210 // Check spelling content
211 // Initialize output
212 $this->result = '<?xml version="1.0" encoding="' . $this->parserCharset . '"?>
213 <!DOCTYPE html
214 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
215 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
216 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . substr($this->dictionary, 0, 2) . '" lang="' . substr($this->dictionary, 0, 2) . '">
217 <html>
218 <head>
219 <meta http-equiv="Content-Type" content="text/html; charset=' . $this->parserCharset . '" />
220 <link rel="stylesheet" type="text/css" media="all" href="spell-check-style.css" />
221 <script type="text/javascript">
222 /*<![CDATA[*/
223 <!--
224 ';
225
226 // Getting the input content
227 $content = t3lib_div::_POST('content');
228
229 // Parsing the input HTML
230 $parser = xml_parser_create(strtoupper($this->parserCharset));
231 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
232 xml_set_object($parser, &$this);
233 if (!xml_set_element_handler($parser, 'startHandler', 'endHandler')) echo('Bad xml handler setting');
234 if (!xml_set_character_data_handler($parser, 'collectDataHandler')) echo('Bad xml handler setting');
235 if (!xml_set_default_handler($parser, 'defaultHandler')) echo('Bad xml handler setting');
236 if (!xml_parse($parser,'<?xml version="1.0" encoding="' . $this->parserCharset . '"?><spellchecker> ' . preg_replace('/&nbsp;/'.(($this->parserCharset == 'utf-8')?'u':''), ' ', $content) . ' </spellchecker>')) echo('Bad parsing');
237 if (xml_get_error_code($parser)) {
238 die('Line '.xml_get_current_line_number($parser).': '.xml_error_string(xml_get_error_code($parser)));
239 }
240 xml_parser_free($parser);
241 if ($this->pspell_is_available && !$this->forceCommandMode) {
242 pspell_clear_session ($this->pspell_link);
243 }
244 $this->result .= 'var suggested_words = {' . $this->suggestedWords . '};
245 ';
246
247 // Calculating parsing and spell checkting time
248 $time = number_format(microtime(true) - $time_start, 2, ',', ' ');
249
250 // Insert spellcheck info
251 $this->result .= 'var spellcheck_info = { "Total words":"'.$this->wordCount.'","Misspelled words":"'.sizeof($this->misspelled).'","Total suggestions":"'.$this->suggestionCount.'","Total words suggested":"'.$this->suggestedWordCount.'","Spelling checked in":"'.$time.'" };
252 // -->
253 /*]]>*/
254 </script>
255 </head>
256 ';
257 $this->result .= '<body onload="window.parent.finishedSpellChecking();">';
258 $this->result .= preg_replace('/'.preg_quote('<?xml').'.*'.preg_quote('?>').'['.preg_quote(chr(10).chr(13).chr(32)).']*/'.(($this->parserCharset == 'utf-8')?'u':''), '', $this->text);
259 $this->result .= '<div id="HA-spellcheck-dictionaries">'.$dictionaries.'</div>';
260
261 // Closing
262 $this->result .= '
263 </body></html>';
264
265 // Outputting
266 header('Content-Type: text/html; charset=' . strtoupper($this->parserCharset));
267 echo $this->result;
268 }
269
270 } // end of function main
271
272 function startHandler($xml_parser, $tag, $attributes) {
273
274 if (strlen($this->xmlCharacterData)) {
275 $this->spellCheckHandler($xml_parser, $this->xmlCharacterData);
276 $this->xmlCharacterData = '';
277 }
278
279 switch($tag) {
280 case 'spellchecker':
281 break;
282 case 'br':
283 case 'BR':
284 case 'img':
285 case 'IMG':
286 case 'hr':
287 case 'HR':
288 case 'area':
289 case 'AREA':
290 $this->text .= '<'. $this->csConvObj->conv_case($this->parserCharset, $tag, 'toLower') . ' ';
291 foreach( $attributes as $key => $val) {
292 $this->text .= $key . '="' . $val . '" ';
293 }
294 $this->text .= ' />';
295 break;
296 default:
297 $this->text .= '<'. $this->csConvObj->conv_case($this->parserCharset, $tag, 'toLower') . ' ';
298 foreach( $attributes as $key => $val) {
299 $this->text .= $key . '="' . $val . '" ';
300 }
301 $this->text .= '>';
302 break;
303 }
304 return;
305 }
306
307 function endHandler($xml_parser, $tag) {
308 if (strlen($this->xmlCharacterData)) {
309 $this->spellCheckHandler($xml_parser, $this->xmlCharacterData);
310 $this->xmlCharacterData = '';
311 }
312
313 switch($tag) {
314 case 'spellchecker':
315 break;
316 case 'br':
317 case 'BR':
318 case 'img':
319 case 'IMG':
320 case 'hr':
321 case 'HR':
322 case 'input':
323 case 'INPUT':
324 case 'area':
325 case 'AREA':
326 break;
327 default:
328 $this->text .= '</' . $tag . '>';
329 break;
330 }
331 return;
332 }
333
334 function spellCheckHandler($xml_parser, $string) {
335 $incurrent=array();
336 $stringText = $string;
337 $words = preg_split((($this->parserCharset == 'utf-8')?'/\P{L}+/u':'/\W+/'), $stringText);
338 while( list(,$word) = each($words) ) {
339 $word = preg_replace('/ /'.(($this->parserCharset == 'utf-8')?'u':''), '', $word);
340 if( $word && !is_numeric($word)) {
341 if($this->pspell_is_available && !$this->forceCommandMode) {
342 if (!pspell_check($this->pspell_link, $word)) {
343 if(!in_array($word, $this->misspelled)) {
344 if(sizeof($this->misspelled) != 0 ) {
345 $this->suggestedWords .= ',';
346 }
347 $suggest = array();
348 $suggest = pspell_suggest($this->pspell_link, $word);
349 if(sizeof($suggest) != 0 ) {
350 $this->suggestionCount++;
351 $this->suggestedWordCount += sizeof($suggest);
352 }
353 $this->suggestedWords .= '"'.$word.'":"'.implode(',',$suggest).'"';
354 $this->misspelled[] = $word;
355 unset($suggest);
356 }
357 if( !in_array($word, $incurrent) ) {
358 $stringText = preg_replace('/\b'.$word.'\b/'.(($this->parserCharset == 'utf-8')?'u':''), '<span class="HA-spellcheck-error">'.$word.'</span>', $stringText);
359 $incurrent[] = $word;
360 }
361 }
362 } else {
363 $tmpFileName = t3lib_div::tempnam($this->filePrefix);
364 if(!$filehandle = fopen($tmpFileName,'wb')) echo('SpellChecker tempfile open error');
365 if(!fwrite($filehandle, $word)) echo('SpellChecker tempfile write error');
366 if(!fclose($filehandle)) echo('SpellChecker tempfile close error');
367 $AspellCommand = 'cat ' . escapeshellarg($tmpFileName) . ' | ' . $this->AspellDirectory . ' -a check --mode=none --sug-mode=' . escapeshellarg($this->pspellMode) . $this->personalDictsArg . ' --lang=' . escapeshellarg($this->dictionary) . ' --encoding=' . escapeshellarg($this->aspellEncoding) . ' 2>&1';
368 $AspellAnswer = shell_exec($AspellCommand);
369 $AspellResultLines = array();
370 $AspellResultLines = t3lib_div::trimExplode(chr(10), $AspellAnswer, 1);
371 if(substr($AspellResultLines[0],0,6) == 'Error:') echo("{$AspellAnswer}");
372 t3lib_div::unlink_tempfile($tmpFileName);
373 if(substr($AspellResultLines['1'],0,1) != '*') {
374 if(!in_array($word, $this->misspelled)) {
375 if(sizeof($this->misspelled) != 0 ) {
376 $this->suggestedWords .= ',';
377 }
378 $suggest = array();
379 $suggestions = array();
380 if (substr($AspellResultLines['1'],0,1) == '&') {
381 $suggestions = t3lib_div::trimExplode(':', $AspellResultLines['1'], 1);
382 $suggest = t3lib_div::trimExplode(',', $suggestions['1'], 1);
383 }
384 if (sizeof($suggest) != 0) {
385 $this->suggestionCount++;
386 $this->suggestedWordCount += sizeof($suggest);
387 }
388 $this->suggestedWords .= '"'.$word.'":"'.implode(',',$suggest).'"';
389 $this->misspelled[] = $word;
390 unset($suggest);
391 unset($suggestions);
392 }
393 if (!in_array($word, $incurrent)) {
394 $stringText = preg_replace('/\b'.$word.'\b/'.(($this->parserCharset == 'utf-8')?'u':''), '<span class="HA-spellcheck-error">'.$word.'</span>', $stringText);
395 $incurrent[] = $word;
396 }
397 }
398 unset($AspellResultLines);
399 }
400 $this->wordCount++;
401 }
402 }
403 $this->text .= $stringText;
404 unset($incurrent);
405 return;
406 }
407
408 function collectDataHandler($xml_parser, $string) {
409 $this->xmlCharacterData .= $string;
410 }
411
412 function defaultHandler($xml_parser, $string) {
413 $this->text .= $string;
414 return;
415 }
416
417 }
418
419 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/pi1/class.tx_rtehtmlarea_pi1.php']) {
420 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/pi1/class.tx_rtehtmlarea_pi1.php']);
421 }
422
423 if (TYPO3_MODE=='FE') {
424 tslib_eidtools::connectDB();
425 $spellChecker = t3lib_div::makeInstance('tx_rtehtmlarea_pi1');
426 $spellChecker->main();
427 }
428
429 ?>