PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/concreteOLD/js/tiny_mce/plugins/spellchecker/classes/PSpellShell.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 112 lines | 63 code | 27 blank | 22 comment | 9 complexity | 8e71fc6dbe69a34854eac628be7977e6 MD5 | raw file
  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. class PSpellShell extends SpellChecker {
  9. /**
  10. * Spellchecks an array of words.
  11. *
  12. * @param {String} $lang Language code like sv or en.
  13. * @param {Array} $words Array of words to spellcheck.
  14. * @return {Array} Array of misspelled words.
  15. */
  16. function &checkWords($lang, $words) {
  17. $cmd = $this->_getCMD($lang);
  18. if ($fh = fopen($this->_tmpfile, "w")) {
  19. fwrite($fh, "!\n");
  20. foreach($words as $key => $value)
  21. fwrite($fh, "^" . $value . "\n");
  22. fclose($fh);
  23. } else
  24. $this->throwError("PSpell support was not found.");
  25. $data = shell_exec($cmd);
  26. @unlink($this->_tmpfile);
  27. $returnData = array();
  28. $dataArr = preg_split("/[\r\n]/", $data, -1, PREG_SPLIT_NO_EMPTY);
  29. foreach ($dataArr as $dstr) {
  30. $matches = array();
  31. // Skip this line.
  32. if (strpos($dstr, "@") === 0)
  33. continue;
  34. preg_match("/\& ([^ ]+) .*/i", $dstr, $matches);
  35. if (!empty($matches[1]))
  36. $returnData[] = utf8_encode(trim($matches[1]));
  37. }
  38. return $returnData;
  39. }
  40. /**
  41. * Returns suggestions of for a specific word.
  42. *
  43. * @param {String} $lang Language code like sv or en.
  44. * @param {String} $word Specific word to get suggestions for.
  45. * @return {Array} Array of suggestions for the specified word.
  46. */
  47. function &getSuggestions($lang, $word) {
  48. $cmd = $this->_getCMD($lang);
  49. if (function_exists("mb_convert_encoding"))
  50. $word = mb_convert_encoding($word, "ISO-8859-1", mb_detect_encoding($word, "UTF-8"));
  51. else
  52. $word = utf8_encode($word);
  53. if ($fh = fopen($this->_tmpfile, "w")) {
  54. fwrite($fh, "!\n");
  55. fwrite($fh, "^$word\n");
  56. fclose($fh);
  57. } else
  58. $this->throwError("Error opening tmp file.");
  59. $data = shell_exec($cmd);
  60. @unlink($this->_tmpfile);
  61. $returnData = array();
  62. $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
  63. foreach($dataArr as $dstr) {
  64. $matches = array();
  65. // Skip this line.
  66. if (strpos($dstr, "@") === 0)
  67. continue;
  68. preg_match("/\&[^:]+:(.*)/i", $dstr, $matches);
  69. if (!empty($matches[1])) {
  70. $words = array_slice(explode(',', $matches[1]), 0, 10);
  71. for ($i=0; $i<count($words); $i++)
  72. $words[$i] = trim($words[$i]);
  73. return $words;
  74. }
  75. }
  76. return array();
  77. }
  78. function _getCMD($lang) {
  79. $this->_tmpfile = tempnam($this->_config['PSpellShell.tmp'], "tinyspell");
  80. if(preg_match("#win#i", php_uname()))
  81. return $this->_config['PSpellShell.aspell'] . " -a --lang=". escapeshellarg($lang) . " --encoding=utf-8 -H < " . $this->_tmpfile . " 2>&1";
  82. return "cat ". $this->_tmpfile ." | " . $this->_config['PSpellShell.aspell'] . " -a --encoding=utf-8 -H --lang=". escapeshellarg($lang);
  83. }
  84. }
  85. ?>