<?php

// Spellchecking Webservice
// Copyright (C) 2005 Nikolas Coukouma
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

$lang $_REQUEST'lang' ];
if( !
$lang ) { $lang 'en'; }
$var $_REQUEST'var' ];
$enc $_REQUEST'enc' ];
if( !
$enc ) { $enc 'utf-8'; }
$mode $_REQUEST'mode' ];

$pspell = @pspell_new$lang$var''$encPSPELL_FAST );

// send content-type header
if( $mode == 'text' ) {
    
header'Content-Type: text/plain;charset=' $enc );
} else if( 
$mode == 'xml' ) {
    
header'Content-Type: text/xml;charset=' $enc );
} else if( 
$mode == 'javascript' ) {
    
header'Content-Type: application/x-javascript;charset=' $enc );
} else {
    
$mode 'text';
    
header'Content-Type: text/plain;charset=' $enc );
}

if( 
$mode == 'xml' ) {
    echo 
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
}

if( 
$pspell === false ) {
    if( 
$mode == 'text' ) {
        echo 
"! Error - an error ocurred while intializing pspell'\n";
    } else if( 
$mode == 'xml' ) {
        echo 
"<error>an error ocurred while intializing pspell</error>\n";
    } else if( 
$mode == 'javascript' ) {
        echo 
"error = \"an error ocurred while intializing pspell\"\n";
    }
} else if( 
array_key_exists'word'$_REQUEST ) ) {
    if( 
$mode == 'xml' ) {
        echo 
"<words>\n";
    }
    
//replace all whitespace with a single space
    
$word preg_replace"/\s+/"" "$_REQUEST'word' ] );
    if( 
pspell_check$pspell$word ) ) {
        if( 
$mode == 'text' ) {
            echo 
"# $word\n";
            echo 
"# Correct\n";
            echo 
"#\n";
        } else if( 
$mode == 'xml' ) {
            echo 
"  <word>\n";
            echo 
"    <text>" htmlspecialchars$word ) . "</text>\n";
            echo 
"    <correct/>\n";
            echo 
"  </word>\n";
        } else if( 
$mode == 'javascript' ) {
            echo 
"word = \"" str_replace'"''\"'$word ) . "\"\n";
            echo 
"suggestions = null;\n";
        }
    } else {
        
$sugs pspell_suggest$pspell$word );
        if( 
$mode == 'text' ) {
            echo 
"# $word\n";
            foreach( 
$sugs as $sug ) {
                echo 
"$sug\n";
            }
            echo 
"#\n";
        } else if( 
$mode == 'xml' ) {
            echo 
"  <word>\n";
            echo 
"    <text>"  htmlspecialchars$word ) . "</text>\n";
            echo 
"    <suggestions>\n";
            foreach( 
$sugs as $sug ) {
                echo 
"      <suggestion>"  htmlspecialchars$sug ) . "</suggestion>\n";
            }
            echo 
"    </suggestions>\n";
            echo 
"  </word>\n";
        } else if( 
$mode == 'javascript' ) {
            echo 
"word = \"" str_replace'"''\"'$word ) . "\"\n";
            echo 
"suggestions = [\n";
            
$len count$sugs ) - 1;
            foreach( 
$sugs as $key => $sug ) {
                if( 
$key == $len ) {
                    
// no comma for the last element
                    
echo "  \"" str_replace'"''\"'$sug ) . "\" ]\n";
                } else {
                    echo 
"  \"" str_replace'"''\"'$sug ) . "\",\n";
                }
            }
        }
    }
    if( 
$mode == 'xml' ) {
        echo 
"</words>\n";
    }
} else if( 
array_key_exists'text'$_REQUEST ) ) {
    
//replace all whitespace with a single space
    
$words preg_replace"/\s+/"" "$_REQUEST'text' ] );
    
$words array_uniqueexplode' '$words ) );
    
$words_len count$words ) - 1;
    if( 
$mode == 'javascript' ) {
        echo 
"suggestions = [\n";
    } else if( 
$mode == 'xml' ) {
        echo 
"<words>\n";
    }
    foreach( 
$words as $index => $word ) {
        
// text preceding words
        
if( $mode == 'text' ) {
            echo 
"# $word\n";
        } else if( 
$mode == 'javascript' ) {
            echo 
"  [ \"" str_replace'"''\"'$word ) . "\", \n";
        } else if( 
$mode == 'xml' ) {
            echo 
"  <word>\n";
            echo 
"    <text>" htmlspecialchars$word ) . "</text>\n";
        }
        
        if( 
pspell_check$pspell$word ) ) {
            if( 
$mode == 'text' ) {
                echo 
"# Correct\n";
            } else if( 
$mode == 'javascript' ) {
                echo 
"     null ] \n";
            } else if( 
$mode == 'xml' ) {
                echo 
"    <correct/>\n";
            }
        } else {
            
$sugs pspell_suggest$pspell$word );
            if( 
$mode == 'text' ) {
                foreach( 
$sugs as $sug ) {
                    echo 
"$sug\n";
                }
            } else if( 
$mode == 'xml' ) {
                echo 
"    <suggestions>\n";
                foreach( 
$sugs as $sug ) {
                    echo 
"      <suggestion>"  htmlspecialchars$sug ) .
                        
"</suggestion>\n";
                }
                echo 
"    </suggestions>\n";
            } else if( 
$mode == 'javascript' ) {
                echo 
"    [\n";
                
$len count$sugs ) - 1;
                foreach( 
$sugs as $key => $sug ) {
                    if( 
$key == $len ) {
                        
// no comma for the last element
                        
echo "      \"" str_replace'"''\"'$sug ) . "\" ]\n";
                    } else {
                        echo 
"      \"" str_replace'"''\"'$sug ) . "\",\n";
                    }
                }
            }
        }
        
        
// following suggestions
        
if( $mode == 'text' ) {
            echo 
"#\n";
        } else if( 
$mode == 'javascript' ) {
            if( 
$index == $words_len ) {
                echo 
"  ]\n";
            } else {
                echo 
"  ],\n";
            }
        } else if( 
$mode == 'xml' ) {
            echo 
"  </word>\n";
        }
    }
    if( 
$mode == 'javascript' ) {
        echo 
"]\n";
    } else if( 
$mode == 'xml' ) {
        echo 
"</words>\n";
    }
} else {
    if( 
$mode == 'text' ) {
        echo 
"! Error - need to send either 'word' or 'text'\n";
    } else if( 
$mode == 'xml' ) {
        echo 
"<error>need to send either 'word' or 'text'</error>\n";
    } else if( 
$mode == 'javascript' ) {
        echo 
"error = \"need to send either 'word' or 'text'\"\n";
    }
}

?>