includes/clientside/tinymce/plugins/spellchecker/classes/PSpellShell.php
changeset 784 72df14a56a03
equal deleted inserted replaced
783:368a07e59bfe 784:72df14a56a03
       
     1 <?php
       
     2 /**
       
     3  * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
       
     4  *
       
     5  * @author Moxiecode
       
     6  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
       
     7  */
       
     8 
       
     9 class PSpellShell extends SpellChecker {
       
    10 	/**
       
    11 	 * Spellchecks an array of words.
       
    12 	 *
       
    13 	 * @param {String} $lang Language code like sv or en.
       
    14 	 * @param {Array} $words Array of words to spellcheck.
       
    15 	 * @return {Array} Array of misspelled words.
       
    16 	 */
       
    17 	function &checkWords($lang, $words) {
       
    18 		$cmd = $this->_getCMD($lang);
       
    19 
       
    20 		if ($fh = fopen($this->_tmpfile, "w")) {
       
    21 			fwrite($fh, "!\n");
       
    22 
       
    23 			foreach($words as $key => $value)
       
    24 				fwrite($fh, "^" . $value . "\n");
       
    25 
       
    26 			fclose($fh);
       
    27 		} else
       
    28 			$this->throwError("PSpell support was not found.");
       
    29 
       
    30 		$data = shell_exec($cmd);
       
    31 		@unlink($this->_tmpfile);
       
    32 
       
    33 		$returnData = array();
       
    34 		$dataArr = preg_split("/[\r\n]/", $data, -1, PREG_SPLIT_NO_EMPTY);
       
    35 
       
    36 		foreach ($dataArr as $dstr) {
       
    37 			$matches = array();
       
    38 
       
    39 			// Skip this line.
       
    40 			if (strpos($dstr, "@") === 0)
       
    41 				continue;
       
    42 
       
    43 			preg_match("/\& ([^ ]+) .*/i", $dstr, $matches);
       
    44 
       
    45 			if (!empty($matches[1]))
       
    46 				$returnData[] = utf8_encode(trim($matches[1]));
       
    47 		}
       
    48 
       
    49 		return $returnData;
       
    50 	}
       
    51 
       
    52 	/**
       
    53 	 * Returns suggestions of for a specific word.
       
    54 	 *
       
    55 	 * @param {String} $lang Language code like sv or en.
       
    56 	 * @param {String} $word Specific word to get suggestions for.
       
    57 	 * @return {Array} Array of suggestions for the specified word.
       
    58 	 */
       
    59 	function &getSuggestions($lang, $word) {
       
    60 		$cmd = $this->_getCMD($lang);
       
    61 
       
    62         if (function_exists("mb_convert_encoding"))
       
    63             $word = mb_convert_encoding($word, "ISO-8859-1", mb_detect_encoding($word, "UTF-8"));
       
    64         else
       
    65             $word = utf8_encode($word);
       
    66 
       
    67 		if ($fh = fopen($this->_tmpfile, "w")) {
       
    68 			fwrite($fh, "!\n");
       
    69 			fwrite($fh, "^$word\n");
       
    70 			fclose($fh);
       
    71 		} else
       
    72 			$this->throwError("Error opening tmp file.");
       
    73 
       
    74 		$data = shell_exec($cmd);
       
    75 		@unlink($this->_tmpfile);
       
    76 
       
    77 		$returnData = array();
       
    78 		$dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
       
    79 
       
    80 		foreach($dataArr as $dstr) {
       
    81 			$matches = array();
       
    82 
       
    83 			// Skip this line.
       
    84 			if (strpos($dstr, "@") === 0)
       
    85 				continue;
       
    86 
       
    87 			preg_match("/\&[^:]+:(.*)/i", $dstr, $matches);
       
    88 
       
    89 			if (!empty($matches[1])) {
       
    90 				$words = array_slice(explode(',', $matches[1]), 0, 10);
       
    91 
       
    92 				for ($i=0; $i<count($words); $i++)
       
    93 					$words[$i] = trim($words[$i]);
       
    94 
       
    95 				return $words;
       
    96 			}
       
    97 		}
       
    98 
       
    99 		return array();
       
   100 	}
       
   101 
       
   102 	function _getCMD($lang) {
       
   103 		$this->_tmpfile = tempnam($this->_config['PSpellShell.tmp'], "tinyspell");
       
   104 
       
   105 		if(preg_match("#win#i", php_uname()))
       
   106 			return $this->_config['PSpellShell.aspell'] . " -a --lang=". escapeshellarg($lang) . " --encoding=utf-8 -H < " . $this->_tmpfile . " 2>&1";
       
   107 
       
   108 		return "cat ". $this->_tmpfile ." | " . $this->_config['PSpellShell.aspell'] . " -a --encoding=utf-8 -H --lang=". escapeshellarg($lang);
       
   109 	}
       
   110 }
       
   111 
       
   112 ?>