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