[ Index ]
 

Code source de IMP H3 (4.1.5)

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/ -> spelling.php (source)

   1  <?php
   2  /**
   3   * $Horde: imp/spelling.php,v 2.67.6.9 2007/02/05 02:56:04 chuck Exp $
   4   *
   5   * Copyright 1999-2007 Charles J. Hagenbuch <chuck@horde.org>
   6   * Copyright 1999-2007 Jon Parise <jon@horde.org>
   7   *
   8   * See the enclosed file COPYING for license information (GPL).  If you
   9   * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  10   */
  11  
  12  define('IMP_SPELL_CHANGE',     1);
  13  define('IMP_SPELL_CHANGE_ALL', 2);
  14  define('IMP_SPELL_IGNORE',     3);
  15  define('IMP_SPELL_IGNORE_ALL', 4);
  16  
  17  /* Base list of words to ignore. */
  18  $ignore_list = array(
  19      'com', 'cc', 'www', 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
  20      'aug', 'sep', 'oct', 'nov', 'dec', 'fwd', 'dns', 'http', 'ca', 'html',
  21      'tm', 'mmunity', 'co', 'op', 'https', 'netscape', 'webmail', 'bcc',
  22      'jpg', 'gif', 'email', 'tel', 'ie', 'eg'
  23  );
  24  
  25  /**
  26   * Find the offset of a given word.
  27   *
  28   * @param string $message  The text of the message.
  29   * @param string $word     The word to find.
  30   * @param integer $start   The offset.
  31   *
  32   * @return integer  The offset of the word.
  33   */
  34  function _findOffset($message, $word, $start)
  35  {
  36      $offset = $start;
  37      $pos = -1;
  38  
  39      /* If all things are right the word is at offset - 1 */
  40      if ($start > 3) {
  41          $start -= 3;
  42      } else {
  43          $start = 0;
  44      }
  45      while (($pos == -1) && ($start >= 0)) {
  46          $pos = String::pos($message, $word, $start);
  47          if (($pos == '') && !is_int($pos)) {
  48              $start--;
  49              $pos = -1;
  50          }
  51      }
  52  
  53      return $pos;
  54  }
  55  
  56  
  57  /**
  58   * Highlight in error in a given message.
  59   *
  60   * @param string $error    The misspelled word.
  61   * @param string $message  The text of the message.
  62   * @param integer $start   The offset.
  63   *
  64   * @return string  The string with the error highlighted.
  65   */
  66  function _highlightError($error, $message, $offset)
  67  {
  68      $pos = strpos($message, $error, ((($offset - 1) > 0) ? ($offset - 1) : 0));
  69      if (($pos - 15) > 0) {
  70          $start = $pos - 15;
  71      } else {
  72          $start = 0;
  73      }
  74      $length = String::length($error) + 30;
  75  
  76      $message = substr_replace($message, '<span style="color:#ff0000">' . $error . '</span>', $pos, String::length($error));
  77      $message = String::substr($message, $start, $length + 28);
  78  
  79      return $message;
  80  }
  81  
  82  /* Fetch and clean form data.  These are spelling.php specific form variables
  83   * only. */
  84  $f_opt = Util::getFormData('opt');
  85  $f_subs = Util::getFormData('subs');
  86  $f_oldword = Util::getFormData('oldword');
  87  $f_subtext = Util::getFormData('subtext');
  88  $f_wordoffset = Util::getFormData('wordoffset');
  89  $f_oldmsg = Util::getFormData('oldmsg', Util::getFormData('message'));
  90  $f_currmsg = Util::getFormData('currmsg', $f_oldmsg);
  91  $f_newmsg = Util::getFormData('newmsg');
  92  $f_done_action = Util::getFormData('done_action');
  93  
  94  /* Get from data from compose.php input that is used locally in
  95   * spelling.php. */
  96  $f_rtemode = Util::getFormData('rtemode');
  97  
  98  /* ignoreall is an array - we need to unserialize the data. */
  99  if (($ignoreall = Util::getFormData('ignoreall'))) {
 100      $ignoreall = unserialize($ignoreall);
 101  } else {
 102      $ignoreall = array();
 103  }
 104  
 105  switch ($actionID) {
 106  case 'spell_check_forward':
 107      for ($i = 0; $i < count($f_opt); $i++) {
 108          $skipword = false;
 109  
 110          /* If they have an word with no suggestions and they
 111             don't type in a replacement, ignore it. */
 112          if (!empty($f_subtext[$i])) {
 113              $replacement = $f_subtext[$i];
 114          } else {
 115              if ($f_subs[$i] == '0') {
 116                  $ignoreall[] = String::lower($f_oldword[$i], true);
 117                  $skipword = true;
 118              } else {
 119                  $replacement = $f_subs[$i];
 120              }
 121          }
 122  
 123          if (!$skipword) {
 124              $pos = -1;
 125  
 126              $realoffset = $f_wordoffset[$i];
 127  
 128              /* Just in case things are whackily out. */
 129              $msg_length = String::length($f_currmsg);
 130              if ($realoffset > $msg_length) {
 131                  $realoffset = $msg_length - 1;
 132              }
 133  
 134              $pos = _findOffset($f_currmsg, $f_oldword[$i], $realoffset);
 135  
 136              switch ($f_opt[$i]) {
 137              case IMP_SPELL_IGNORE:
 138                  $consume = $pos + String::length($f_oldword[$i]);
 139                  $addition = String::substr($f_currmsg, 0, $consume);
 140                  $f_newmsg .= $addition;
 141                  $f_currmsg = String::substr($f_currmsg, $consume);
 142  
 143                  /* Adjust offsets, as they could be wildly out */
 144                  for ($msgnum = 0; $msgnum < count($f_wordoffset); $msgnum++) {
 145                      $f_wordoffset[$msgnum] -= $consume;
 146                  }
 147                  break;
 148  
 149              case IMP_SPELL_IGNORE_ALL:
 150                  $ignoreall[] = String::lower($f_oldword[$i], true);
 151                  break;
 152  
 153              case IMP_SPELL_CHANGE_ALL:
 154                  $f_currmsg = str_replace($f_oldword[$i], $replacement, $f_currmsg);
 155                  break;
 156  
 157              case IMP_SPELL_CHANGE:
 158                  if (!in_array(String::lower($f_oldword[$i], true), $ignoreall)) {
 159                      /* Let's try and keep those offsets semi correct. */
 160                      $adjoffset = String::length($replacement) - String::length($f_oldword[$i]);
 161                      for ($msgnum = 0; $msgnum < count($f_wordoffset); $msgnum++) {
 162                          $f_wordoffset[$msgnum] += $adjoffset;
 163                      }
 164  
 165                      $tempmessage  = String::substr($f_currmsg, 0, $pos);
 166                      $tempmessage .= $replacement;
 167                      $tempmessage .= String::substr($f_currmsg, $pos + String::length($f_oldword[$i]));
 168                      $f_currmsg = $tempmessage;
 169                  }
 170                  break;
 171              }
 172          }
 173      }
 174      break;
 175  
 176  case 'spell_check_send':
 177      $f_done_action = 'send';
 178      /* FALLTHROUGH */
 179  
 180  default:
 181      $f_message = $f_currmsg;
 182  
 183      /* Have to start another wordlist to incorporate into the spell
 184         check dictionary methinks. */
 185      $ignoreall = $ignore_list;
 186      break;
 187  }
 188  
 189  /* Special treatment depending on language (quotes are not equally treated
 190     by ispell in english and in french). */
 191  switch ($language) {
 192  case 'fr_FR':
 193      $tocheck = str_replace("'", "\\'", escapeShellCmd($f_currmsg));
 194      break;
 195  
 196  default:
 197      $tocheck = $f_currmsg;
 198      break;
 199  }
 200  
 201  $spellchecker = $conf['utils']['spellchecker'];
 202  
 203  /* Protect against lines beginning with "special" characters as defined at
 204     http://aspell.net/man-html/Through-A-Pipe.html#Through%20A%20Pipe */
 205  if (strpos($spellchecker, 'aspell') !== false) {
 206      $tocheck = '^' . str_replace("\n", "\n^", $tocheck);
 207  }
 208  
 209  /* Save the message to a temporary file. */
 210  $spellFile = Horde::getTempFile('spell');
 211  $fp = fopen($spellFile, 'w');
 212  fwrite($fp, $tocheck);
 213  fclose($fp);
 214  
 215  /* Run the actual spell check. */
 216  if (empty($spellchecker)) {
 217      $notification->push(_("No spellchecking program configured."), 'horde.error');
 218  } else {
 219      /* Retrieve any spelling options. */
 220      $spell_opt = '';
 221      if (isset($nls['spelling'][$language])) {
 222          $spell_opt = $nls['spelling'][$language];
 223      }
 224      if (strpos($spellchecker, 'aspell') != false) {
 225          $spell_opt .= ' -e';
 226      }
 227      if ($f_rtemode) {
 228          // Try to use the Spellchecker's HTML mode
 229          if (strpos($spellchecker, 'ispell') !== false) {
 230              $spell_opt .= ' -h';
 231          } elseif (strpos($spellchecker, 'aspell') !== false) {
 232              $spell_opt .= ' -H';
 233          }
 234      }
 235  
 236      exec(escapeshellcmd($spellchecker . ' -a ' . $spell_opt) . ' < ' . $spellFile, $warnings);
 237  }
 238  
 239  $msg = '';
 240  for ($i = 0; $i < count($warnings); $i++) {
 241      if (substr($warnings[$i], 0, 1) == '&') {
 242          $parts = explode(': ', $warnings[$i]);
 243          $info = explode(' ', $parts[0]);
 244          if (preg_match('|^[A-Z]*$|', $info[1])) {
 245              $ignoreall[] = String::lower($info[1], true);
 246          } else {
 247              $error[] = array($info[1], $info[3], $parts[1]);
 248          }
 249      }
 250      if (preg_match('|^#|', $warnings[$i])) {
 251          $info = explode(' ', $warnings[$i]);
 252          if (preg_match('|^[A-Z]*$|', $info[1])) {
 253              $ignoreall[] = String::lower($info[1], true);
 254          } else {
 255              $error[] = array($info[1], $info[2], '');
 256          }
 257      }
 258  }
 259  
 260  /* Generate a pretty word-wrapped version for displaying. */
 261  $display_msg = $f_message = "\n" . $f_newmsg . $f_currmsg;
 262  if (!$f_rtemode) {
 263      $display_msg = htmlspecialchars($display_msg);
 264      $display_msg = String::wrap($display_msg, 80, "\n", NLS::getCharset(), true);
 265  }
 266  if ($browser->hasQuirk('double_linebreak_textarea')) {
 267      $display_msg = preg_replace('/(\r?\n){3}/', '$1', $display_msg);
 268  }
 269  
 270  /* Alter the "Done" behavior if we're sending the message after spell check. */
 271  if ($f_done_action === 'send') {
 272      $spell_check_done_action = 'send_message';
 273      $spell_check_done_caption = _("(Send message after applying all changes made thus far.  Changes on the current screen will NOT be applied.)");
 274  } else {
 275      $spell_check_done_action = 'spell_check_done';
 276      $spell_check_done_caption = _("(Return to the compose screen after applying all changes made thus far.  Changes on the current screen will NOT be applied.)");
 277  }


Généré le : Thu Nov 29 12:30:07 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics