Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  
  10  class PSpellShell extends SpellChecker {
  11  	 /**

  12  	  * Spellchecks an array of words.

  13  	  *

  14  	  * @param {String} $lang Language code like sv or en.

  15  	  * @param {Array} $words Array of words to spellcheck.

  16  	  * @return {Array} Array of misspelled words.

  17  	  */
  18  	 function &checkWords($lang, $words) {
  19  	 	 $cmd = $this->_getCMD($lang);
  20  
  21  	 	 if ($fh = fopen($this->_tmpfile, "w")) {
  22  	 	 	 fwrite($fh, "!\n");
  23  
  24  	 	 	 foreach($words as $key => $value)
  25  	 	 	 	 fwrite($fh, "^" . $value . "\n");
  26  
  27  	 	 	 fclose($fh);
  28  	 	 } else
  29  	 	 	 $this->throwError("PSpell support was not found.");
  30  
  31  	 	 $data = shell_exec($cmd);
  32  	 	 @unlink($this->_tmpfile);
  33  
  34  	 	 $returnData = array();
  35  	 	 $dataArr = preg_split("/[\r\n]/", $data, -1, PREG_SPLIT_NO_EMPTY);
  36  
  37  	 	 foreach ($dataArr as $dstr) {
  38  	 	 	 $matches = array();
  39  
  40  	 	 	 // Skip this line.

  41  	 	 	 if ($dstr[0] == "@")
  42  	 	 	 	 continue;
  43  
  44  	 	 	 preg_match("/(\&|#) ([^ ]+) .*/i", $dstr, $matches);
  45  
  46  	 	 	 if (!empty($matches[2]))
  47  	 	 	 	 $returnData[] = utf8_encode(trim($matches[2]));
  48  	 	 }
  49  
  50  	 	 return $returnData;
  51  	 }
  52  
  53  	 /**

  54  	  * Returns suggestions of for a specific word.

  55  	  *

  56  	  * @param {String} $lang Language code like sv or en.

  57  	  * @param {String} $word Specific word to get suggestions for.

  58  	  * @return {Array} Array of suggestions for the specified word.

  59  	  */
  60  	 function &getSuggestions($lang, $word) {
  61  	 	 $cmd = $this->_getCMD($lang);
  62  
  63          if (function_exists("mb_convert_encoding"))
  64              $word = mb_convert_encoding($word, "ISO-8859-1", mb_detect_encoding($word, "UTF-8"));
  65          else
  66              $word = utf8_encode($word);
  67  
  68  	 	 if ($fh = fopen($this->_tmpfile, "w")) {
  69  	 	 	 fwrite($fh, "!\n");
  70  	 	 	 fwrite($fh, "^$word\n");
  71  	 	 	 fclose($fh);
  72  	 	 } else
  73  	 	 	 $this->throwError("Error opening tmp file.");
  74  
  75  	 	 $data = shell_exec($cmd);
  76  	 	 @unlink($this->_tmpfile);
  77  
  78  	 	 $returnData = array();
  79  	 	 $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
  80  
  81  	 	 foreach($dataArr as $dstr) {
  82  	 	 	 $matches = array();
  83  
  84  	 	 	 // Skip this line.

  85  	 	 	 if ($dstr[0] == "@")
  86  	 	 	 	 continue;
  87  
  88  	 	 	 preg_match("/\&[^:]+:(.*)/i", $dstr, $matches);
  89  
  90  	 	 	 if (!empty($matches[1])) {
  91  	 	 	 	 $words = array_slice(explode(',', $matches[1]), 0, 10);
  92  
  93  	 	 	 	 for ($i=0; $i<count($words); $i++)
  94  	 	 	 	 	 $words[$i] = trim($words[$i]);
  95  
  96  	 	 	 	 return $words;
  97  	 	 	 }
  98  	 	 }
  99  
 100  	 	 return array();
 101  	 }
 102  
 103  	function _getCMD($lang) {
 104  	 	 $this->_tmpfile = tempnam($this->_config['PSpellShell.tmp'], "tinyspell");
 105  
 106  	 	 $file = $this->_tmpfile;
 107  	 	 $lang = preg_replace("/[^-_a-z]/", "", strtolower($lang));
 108  	 	 $bin  = $this->_config['PSpellShell.aspell'];
 109  
 110  	 	 if (preg_match("#win#i", php_uname()))
 111  	 	 	 return "$bin -a --lang=$lang --encoding=utf-8 -H < $file 2>&1";
 112  
 113  	 	 return "cat $file | $bin -a --lang=$lang --encoding=utf-8 -H";
 114  	 }
 115  }
 116  
 117  ?>