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