Documentation TYPO3 par Ameos |
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 2003-2006 Stanislas Rolland <stanislas.rolland(arobas)fructifor.ca> 00006 * All rights reserved 00007 * 00008 * This script is part of the Typo3 project. The Typo3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * 00017 * This script is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU General Public License for more details. 00021 * 00022 * This copyright notice MUST APPEAR in all copies of the script! 00023 ***************************************************************/ 00032 require_once(PATH_tslib.'class.tslib_pibase.php'); 00033 00034 class tx_rtehtmlarea_pi1 extends tslib_pibase { 00035 var $cObj; // The backReference to the mother cObj object set at call time 00036 var $prefixId = 'tx_rtehtmlarea_pi1'; // Same as class name 00037 var $scriptRelPath = 'pi1/class.tx_rtehtmlarea_pi1.php'; // Path to this script relative to the extension dir. 00038 var $extKey = 'rtehtmlarea'; // The extension key. 00039 var $conf = array(); 00040 var $siteUrl; 00041 var $charset = 'utf-8'; 00042 var $parserCharset = 'utf-8'; 00043 var $result; 00044 var $text; 00045 var $misspelled = array(); 00046 var $suggestedWords; 00047 var $wordCount = 0; 00048 var $suggestionCount = 0; 00049 var $suggestedWordCount = 0; 00050 var $pspell_link; 00051 var $pspellMode = 'normal'; 00052 var $dictionary; 00053 var $AspellDirectory; 00054 var $pspell_is_available; 00055 var $forceCommandMode = 0; 00056 var $filePrefix = 'rtehtmlarea_'; 00057 var $uploadFolder = 'uploads/tx_rtehtmlarea/'; 00058 var $userUid; 00059 var $personalDictsArg = ''; 00060 00068 function main($conf) { 00069 global $TYPO3_CONF_VARS, $TYPO3_DB; 00070 00071 $this->conf = $conf; 00072 $this->tslib_pibase(); 00073 $this->pi_setPiVarDefaults(); 00074 $this->pi_loadLL(); 00075 $this->pi_USER_INT_obj = 1; // Disable caching 00076 // Setting start time 00077 $time_start = microtime(true); 00078 $this->pspell_is_available = in_array('pspell', get_loaded_extensions()); 00079 $this->AspellDirectory = trim($TYPO3_CONF_VARS['EXTCONF'][$this->extKey]['AspellDirectory'])? trim($TYPO3_CONF_VARS['EXTCONF'][$this->extKey]['AspellDirectory']) : '/usr/bin/aspell'; 00080 $this->forceCommandMode = (trim($TYPO3_CONF_VARS['EXTCONF'][$this->extKey]['forceCommandMode']))? trim($TYPO3_CONF_VARS['EXTCONF'][$this->extKey]['forceCommandMode']) : 0; 00081 $safe_mode_is_enabled = ini_get('safe_mode'); 00082 if($safe_mode_is_enabled && !$this->pspell_is_available ) echo('Configuration problem: Spell checking cannot be performed'); 00083 if($safe_mode_is_enabled && $this->forceCommandMode) echo('Configuration problem: Spell checking cannot be performed in command mode'); 00084 if(!$safe_mode_is_enabled && (!$this->pspell_is_available || $this->forceCommandMode)) { 00085 $AspellVersionString = explode('Aspell', shell_exec( $this->AspellDirectory.' -v')); 00086 $AspellVersion = substr( $AspellVersionString[1], 0, 4); 00087 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'); 00088 } 00089 00090 // Setting the list of dictionaries 00091 if(!$safe_mode_is_enabled && (!$this->pspell_is_available || $this->forceCommandMode)) { 00092 $dictionaryList = shell_exec( $this->AspellDirectory.' dump dicts'); 00093 $dictionaryList = implode(',', t3lib_div::trimExplode(chr(10), $dictionaryList, 1)); 00094 } 00095 if( empty($dictionaryList) ) { 00096 $dictionaryList = trim($TYPO3_CONF_VARS['EXTCONF'][$this->extKey]['dictionaryList']); 00097 } 00098 if( empty($dictionaryList) ) { 00099 $dictionaryList = 'en'; 00100 } 00101 $dictionaryArray = t3lib_div::trimExplode(',', $dictionaryList, 1); 00102 00103 $defaultDictionary = trim($TYPO3_CONF_VARS['EXTCONF'][$this->extKey]['defaultDictionary']); 00104 if(!$defaultDictionary || !in_array($defaultDictionary, $dictionaryArray)) { 00105 $defaultDictionary = 'en'; 00106 } 00107 00108 // Get the defined sys_language codes 00109 $languageArray = array(); 00110 $tableA = 'sys_language'; 00111 $tableB = 'static_languages'; 00112 $selectFields = $tableA . '.uid,' . $tableB . '.lg_iso_2,' . $tableB . '.lg_country_iso_2'; 00113 $table = $tableA . ' LEFT JOIN ' . $tableB . ' ON ' . $tableA . '.static_lang_isocode=' . $tableB . '.uid'; 00114 $whereClause = '1=1 '; 00115 $whereClause .= ' AND ' . $tableA . '.hidden != 1'; 00116 $res = $TYPO3_DB->exec_SELECTquery($selectFields, $table, $whereClause); 00117 while($row = $TYPO3_DB->sql_fetch_assoc($res)) { 00118 $languageArray[] = strtolower($row['lg_iso_2']).($row['lg_country_iso_2']?'_'.$row['lg_country_iso_2']:''); 00119 } 00120 if(!in_array($defaultDictionary, $languageArray)) { 00121 $languageArray[] = $defaultDictionary; 00122 } 00123 foreach ($dictionaryArray as $key => $dict) { 00124 $lang = explode('-', $dict); 00125 if( !in_array(substr($dict, 0, 2), $languageArray) || !empty($lang[1])) { 00126 unset($dictionaryArray[$key]); 00127 } else { 00128 $dictionaryArray[$key] = $lang[0]; 00129 } 00130 } 00131 uasort($dictionaryArray, 'strcoll'); 00132 $dictionaryList = implode(',', $dictionaryArray); 00133 00134 // Setting the dictionary 00135 $this->dictionary = t3lib_div::_POST('dictionary'); 00136 if( empty($this->dictionary) || !in_array($this->dictionary, $dictionaryArray)) { 00137 $this->dictionary = $defaultDictionary; 00138 } 00139 $dictionaries = substr_replace($dictionaryList, '@'.$this->dictionary, strpos($dictionaryList, $this->dictionary), strlen($this->dictionary)); 00140 00141 //$locale = setlocale(LC_ALL, $this->dictionary); 00142 00143 // Setting the pspell suggestion mode 00144 $this->pspellMode = t3lib_div::_POST('pspell_mode')?t3lib_div::_POST('pspell_mode'): $this->pspellMode; 00145 switch($this->pspellMode) { 00146 case 'ultra': 00147 case 'fast': 00148 $pspellModeFlag = PSPELL_FAST; 00149 break; 00150 case 'bad-spellers': 00151 $pspellModeFlag = PSPELL_BAD_SPELLERS; 00152 break; 00153 case 'normal': 00154 default: 00155 $pspellModeFlag = PSPELL_NORMAL; 00156 break; 00157 } 00158 00159 // Setting the charset 00160 if( t3lib_div::_POST('pspell_charset') ) $this->charset = trim(t3lib_div::_POST('pspell_charset')); 00161 if(strtolower($this->charset) == 'iso-8859-1') $this->parserCharset = strtolower($this->charset); 00162 $internal_encoding = mb_internal_encoding(strtoupper($this->parserCharset)); 00163 //$regex_encoding = mb_regex_encoding (strtoupper($this->parserCharset)); 00164 // However, we are going to work only in the parser charset 00165 if($this->pspell_is_available && !$this->forceCommandMode) { 00166 $this->pspell_link = pspell_new($this->dictionary, '', '', $this->parserCharset, $pspellModeFlag); 00167 } 00168 00169 // Setting the path to user personal dicts, if any 00170 if (t3lib_div::_POST('enablePersonalDicts') == 'true') { 00171 $this->userUid = t3lib_div::_POST('userUid'); 00172 if ($this->userUid) { 00173 $this->personalDictPath = t3lib_div::getFileAbsFileName($this->uploadFolder . $this->userUid); 00174 if (!is_dir($this->personalDictPath)) { 00175 t3lib_div::mkdir($this->personalDictPath); 00176 } 00177 $this->personalDictsArg = ' --home-dir=' . $this->personalDictPath; 00178 } 00179 } 00180 00181 $cmd = t3lib_div::_POST('cmd'); 00182 if ($cmd == 'learn' && !$safe_mode_is_enabled) { 00183 // Updating the personal word list 00184 $to_p_dict = t3lib_div::_POST('to_p_dict'); 00185 $to_p_dict = $to_p_dict ? $to_p_dict : array(); 00186 $to_r_list = t3lib_div::_POST('to_r_list'); 00187 $to_r_list = $to_r_list ? $to_r_list : array(); 00188 header('Content-Type: text/plain; charset=' . strtoupper($this->parserCharset)); 00189 header('Pragma: no-cache'); 00190 //print_r($to_r_list); 00191 if($to_p_dict || $to_r_list) { 00192 $tmpFileName = t3lib_div::tempnam($this->filePrefix); 00193 if($filehandle = fopen($tmpFileName,'wb')) { 00194 foreach ($to_p_dict as $personal_word) { 00195 $cmd = '&' . $personal_word . "\n"; 00196 echo $cmd; 00197 fwrite($filehandle, $cmd, strlen($cmd)); 00198 } 00199 foreach ($to_r_list as $replace_pair) { 00200 $cmd = '$$ra ' . $replace_pair[0] . ' , ' . $replace_pair[1] . "\n"; 00201 echo $cmd; 00202 fwrite($filehandle, $cmd, strlen($cmd)); 00203 } 00204 $cmd = "#\n"; 00205 echo $cmd; 00206 fwrite($filehandle, $cmd, strlen($cmd)); 00207 fclose($filehandle); 00208 $AspellCommand = 'cat ' . $tmpFileName . ' | ' . $this->AspellDirectory . ' -a --mode=none' . $this->personalDictsArg . ' --lang=' .$this->dictionary . ' --encoding=' . $this->parserCharset . ' 2>&1'; 00209 print $AspellCommand . "\n"; 00210 print shell_exec($AspellCommand); 00211 t3lib_div::unlink_tempfile($tmpFileName); 00212 echo('Personal word list was updated.'); 00213 } else { 00214 echo('SpellChecker tempfile open error.'); 00215 } 00216 } else { 00217 echo('Nothing to add to the personal word list.'); 00218 } 00219 flush(); 00220 exit(); 00221 } else { 00222 // Check spelling content 00223 // Initialize output 00224 $this->result = '<?xml version="1.0" encoding="' . $this->parserCharset . '"?> 00225 <!DOCTYPE html 00226 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 00227 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 00228 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . substr($this->dictionary, 0, 2) . '" lang="' . substr($this->dictionary, 0, 2) . '"> 00229 <html> 00230 <head> 00231 <meta http-equiv="Content-Type" content="text/html; charset=' . $this->parserCharset . '" /> 00232 <link rel="stylesheet" type="text/css" media="all" href="spell-check-style.css" /> 00233 <script type="text/javascript"> 00234 /*<![CDATA[*/ 00235 '; 00236 00237 // Getting the input content 00238 $content = t3lib_div::_POST('content'); 00239 00240 // Parsing the input HTML 00241 $parser = xml_parser_create(strtoupper($this->parserCharset)); 00242 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 00243 xml_set_object($parser, &$this); 00244 if( !xml_set_element_handler( $parser, 'startHandler', 'endHandler')) echo('Bad xml handler setting'); 00245 if( !xml_set_character_data_handler ( $parser, 'spellCheckHandler')) echo('Bad xml handler setting'); 00246 if( !xml_set_default_handler( $parser, 'defaultHandler')) echo('Bad xml handler setting'); 00247 if(! xml_parse($parser,'<?xml version="1.0" encoding="' . $this->parserCharset . '"?><SPELLCHECKER> ' . mb_ereg_replace(' ', ' ', $content) . ' </SPELLCHECKER>')) echo('Bad parsing'); 00248 if( xml_get_error_code($parser)) { 00249 die('Line '.xml_get_current_line_number($parser).': '.xml_error_string(xml_get_error_code($parser))); 00250 } 00251 xml_parser_free($parser); 00252 if($this->pspell_is_available && !$this->forceCommandMode) { 00253 pspell_clear_session ($this->pspell_link); 00254 } 00255 $this->result .= 'var suggested_words = {' . $this->suggestedWords . '}; 00256 '; 00257 00258 // Calculating parsing and spell checkting time 00259 $time = number_format(microtime(true) - $time_start, 2, ',', ' '); 00260 00261 // Insert spellcheck info 00262 $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.'" }; 00263 /*]]>*/ 00264 </script> 00265 </head> 00266 '; 00267 $this->result .= '<body onload="window.parent.finishedSpellChecking();">'; 00268 $this->result .= $this->text; 00269 $this->result .= '<div id="HA-spellcheck-dictionaries">'.$dictionaries.'</div>'; 00270 00271 // Closing 00272 $this->result .= ' 00273 </body></html>'; 00274 00275 // Outputting 00276 echo $this->result; 00277 } 00278 00279 } // end of function main 00280 00281 function startHandler($xml_parser, $tag, $attributes) { 00282 switch($tag) { 00283 case 'SPELLCHECKER': 00284 break; 00285 case 'br': 00286 case 'BR': 00287 case 'img': 00288 case 'IMG': 00289 case 'hr': 00290 case 'HR': 00291 case 'area': 00292 case 'AREA': 00293 $this->text .= '<'. mb_strtolower($tag) . ' '; 00294 foreach( $attributes as $key => $val) { 00295 $this->text .= $key . '="' . $val . '" '; 00296 } 00297 $this->text .= ' />'; 00298 break; 00299 default: 00300 $this->text .= '<'. mb_strtolower($tag) . ' '; 00301 foreach( $attributes as $key => $val) { 00302 $this->text .= $key . '="' . $val . '" '; 00303 } 00304 $this->text .= '>'; 00305 break; 00306 } 00307 return; 00308 } 00309 00310 function endHandler($xml_parser, $tag) { 00311 switch($tag) { 00312 case 'SPELLCHECKER': 00313 break; 00314 case 'br': 00315 case 'BR': 00316 case 'img': 00317 case 'IMG': 00318 case 'hr': 00319 case 'HR': 00320 case 'input': 00321 case 'INPUT': 00322 case 'area': 00323 case 'AREA': 00324 break; 00325 default: 00326 $this->text .= '</' . $tag . '>'; 00327 break; 00328 } 00329 return; 00330 } 00331 00332 function spellCheckHandler($xml_parser, $string) { 00333 $incurrent=array(); 00334 $stringText = $string; 00335 $words = mb_split('\W+', $stringText); 00336 while( list(,$word) = each($words) ) { 00337 $word = mb_ereg_replace(' ', '', $word); 00338 if( $word && !is_numeric($word)) { 00339 if($this->pspell_is_available && !$this->forceCommandMode) { 00340 if (!pspell_check($this->pspell_link, $word)) { 00341 if(!in_array($word, $this->misspelled)) { 00342 if(sizeof($this->misspelled) != 0 ) { 00343 $this->suggestedWords .= ','; 00344 } 00345 $suggest = array(); 00346 $suggest = pspell_suggest($this->pspell_link, $word); 00347 if(sizeof($suggest) != 0 ) { 00348 $this->suggestionCount++; 00349 $this->suggestedWordCount += sizeof($suggest); 00350 } 00351 $this->suggestedWords .= '"'.$word.'":"'.implode(',',$suggest).'"'; 00352 $this->misspelled[] = $word; 00353 unset($suggest); 00354 } 00355 if( !in_array($word, $incurrent) ) { 00356 $stringText = mb_ereg_replace('\b'.$word.'\b', '<span class="HA-spellcheck-error">'.$word.'</span>', $stringText); 00357 $incurrent[] = $word; 00358 } 00359 } 00360 } else { 00361 $tmpFileName = t3lib_div::tempnam($this->filePrefix); 00362 if(!$filehandle = fopen($tmpFileName,'wb')) echo('SpellChecker tempfile open error'); 00363 if(!fwrite($filehandle, $word)) echo('SpellChecker tempfile write error'); 00364 if(!fclose($filehandle)) echo('SpellChecker tempfile close error'); 00365 $AspellCommand = 'cat ' . $tmpFileName . ' | ' . $this->AspellDirectory . ' -a check --mode=none --sug-mode=' . $this->pspellMode . $this->personalDictsArg . ' --lang=' .$this->dictionary . ' --encoding=' . $this->parserCharset . ' 2>&1'; 00366 $AspellAnswer = shell_exec($AspellCommand); 00367 $AspellResultLines = array(); 00368 $AspellResultLines = t3lib_div::trimExplode(chr(10), $AspellAnswer, 1); 00369 if(substr($AspellResultLines[0],0,6) == 'Error:') echo("{$AspellAnswer}"); 00370 t3lib_div::unlink_tempfile($tmpFileName); 00371 if(substr($AspellResultLines['1'],0,1) != '*') { 00372 if(!in_array($word, $this->misspelled)) { 00373 if(sizeof($this->misspelled) != 0 ) { 00374 $this->suggestedWords .= ','; 00375 } 00376 $suggest = array(); 00377 $suggestions = array(); 00378 if (substr($AspellResultLines['1'],0,1) == '&') { 00379 $suggestions = t3lib_div::trimExplode(':', $AspellResultLines['1'], 1); 00380 $suggest = t3lib_div::trimExplode(',', $suggestions['1'], 1); 00381 } 00382 if (sizeof($suggest) != 0) { 00383 $this->suggestionCount++; 00384 $this->suggestedWordCount += sizeof($suggest); 00385 } 00386 $this->suggestedWords .= '"'.$word.'":"'.implode(',',$suggest).'"'; 00387 $this->misspelled[] = $word; 00388 unset($suggest); 00389 unset($suggestions); 00390 } 00391 if (!in_array($word, $incurrent)) { 00392 $stringText = mb_ereg_replace('\b'.$word.'\b', '<span class="HA-spellcheck-error">'.$word.'</span>', $stringText); 00393 $incurrent[] = $word; 00394 } 00395 } 00396 unset($AspellResultLines); 00397 } 00398 $this->wordCount++; 00399 } 00400 } 00401 $this->text .= $stringText; 00402 unset($incurrent); 00403 return; 00404 } 00405 00406 function defaultHandler($xml_parser, $string) { 00407 $this->text .= $string; 00408 return; 00409 } 00410 00411 } // end of class 00412 00413 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/pi1/class.tx_rtehtmlarea_pi1.php']) { 00414 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/pi1/class.tx_rtehtmlarea_pi1.php']); 00415 } 00416 00417 ?>