PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 117 lines | 66 code | 28 blank | 23 comment | 11 complexity | 0948dbeb60b9c12ff90bcd847be1cee8 MD5 | raw file
  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 ($dstr[0] == "@")
  34. continue;
  35. preg_match("/(\&|#) ([^ ]+) .*/i", $dstr, $matches);
  36. if (!empty($matches[2]))
  37. $returnData[] = utf8_encode(trim($matches[2]));
  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 ($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. $file = $this->_tmpfile;
  82. $lang = preg_replace("/[^-_a-z]/", "", strtolower($lang));
  83. $bin = $this->_config['PSpellShell.aspell'];
  84. if (preg_match("#win#i", php_uname()))
  85. return "$bin -a --lang=$lang --encoding=utf-8 -H < $file 2>&1";
  86. return "cat $file | $bin -a --lang=$lang --encoding=utf-8 -H";
  87. }
  88. }
  89. ?>