PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/manage/views/new/js/tiny_mce/plugins/spellchecker/classes/PSpellShell.php

https://bitbucket.org/pooshonk/esw
PHP | 113 lines | 63 code | 27 blank | 23 comment | 9 complexity | b127d36232b02902aed99c16a0fd7f45 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
  4. *
  5. * @package MCManager.includes
  6. * @author Moxiecode
  7. * @copyright Copyright Š 2004-2007, Moxiecode Systems AB, All rights reserved.
  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. if ($fh = fopen($this->_tmpfile, "w")) {
  20. fwrite($fh, "!\n");
  21. foreach($words as $key => $value)
  22. fwrite($fh, "^" . $value . "\n");
  23. fclose($fh);
  24. } else
  25. $this->throwError("PSpell support was not found.");
  26. $data = shell_exec($cmd);
  27. @unlink($this->_tmpfile);
  28. $returnData = array();
  29. $dataArr = preg_split("/[\r\n]/", $data, -1, PREG_SPLIT_NO_EMPTY);
  30. foreach ($dataArr as $dstr) {
  31. $matches = array();
  32. // Skip this line.
  33. if (strpos($dstr, "@") === 0)
  34. continue;
  35. preg_match("/\& ([^ ]+) .*/i", $dstr, $matches);
  36. if (!empty($matches[1]))
  37. $returnData[] = utf8_encode(trim($matches[1]));
  38. }
  39. return $returnData;
  40. }
  41. /**
  42. * Returns suggestions of for a specific word.
  43. *
  44. * @param {String} $lang Language code like sv or en.
  45. * @param {String} $word Specific word to get suggestions for.
  46. * @return {Array} Array of suggestions for the specified word.
  47. */
  48. function &getSuggestions($lang, $word) {
  49. $cmd = $this->_getCMD($lang);
  50. if (function_exists("mb_convert_encoding"))
  51. $word = mb_convert_encoding($word, "ISO-8859-1", mb_detect_encoding($word, "UTF-8"));
  52. else
  53. $word = utf8_encode($word);
  54. if ($fh = fopen($this->_tmpfile, "w")) {
  55. fwrite($fh, "!\n");
  56. fwrite($fh, "^$word\n");
  57. fclose($fh);
  58. } else
  59. $this->throwError("Error opening tmp file.");
  60. $data = shell_exec($cmd);
  61. @unlink($this->_tmpfile);
  62. $returnData = array();
  63. $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
  64. foreach($dataArr as $dstr) {
  65. $matches = array();
  66. // Skip this line.
  67. if (strpos($dstr, "@") === 0)
  68. continue;
  69. preg_match("/\&[^:]+:(.*)/i", $dstr, $matches);
  70. if (!empty($matches[1])) {
  71. $words = array_slice(explode(',', $matches[1]), 0, 10);
  72. for ($i=0; $i<count($words); $i++)
  73. $words[$i] = trim($words[$i]);
  74. return $words;
  75. }
  76. }
  77. return array();
  78. }
  79. function _getCMD($lang) {
  80. $this->_tmpfile = tempnam($this->_config['PSpellShell.tmp'], "tinyspell");
  81. if(preg_match("#win#i", php_uname()))
  82. return $this->_config['PSpellShell.aspell'] . " -a --lang=". escapeshellarg($lang) . " --encoding=utf-8 -H < " . $this->_tmpfile . " 2>&1";
  83. return "cat ". $this->_tmpfile ." | " . $this->_config['PSpellShell.aspell'] . " -a --encoding=utf-8 -H --lang=". escapeshellarg($lang);
  84. }
  85. }
  86. ?>