<?php

    error_reporting(0);
    set_time_limit(0);
    
    define( 'DEBUG', FALSE );
    define( 'BUFFER', FALSE );
    define( 'LOCAL_SPIN', TRUE );
    
    require '../../../wp-config.php';

    if( $_POST['goform'] ) {
    
        if( DEBUG ) {
            $fp = fopen( dirname(__FILE__).'/lost_ajax.txt', 'a' );
            fwrite($fp, "\n\n\n[".date("Y-m-d H:i:s")."] POST: \n".$_POST['content']);
        }
        
        $text = trim( base64_decode( trim( $_POST['content'] ) ) );
        $text = preg_replace( '|\s+|', ' ', $text );
        $text = str_replace( "%u201C", '"', str_replace( "%u201D", '"', str_replace( "%u2019", "'", $text ) ) );
        $text = str_replace( "&nbsp;", '', urldecode( $text ) );
        $text = str_replace( "&amp;", '&', $text );
        $text = preg_replace( "/^[&nbsp;]*/","",$text );
        $text = preg_replace( "/[&nbsp;]*$/","",$text );
        $text = preg_replace( "/(?:\<p\>\&nbsp\;\<\/p\>){2,}/","",$text );

        if( !$text ) return 0;
        if( DEBUG ) fwrite($fp, "\n\n[".date("Y-m-d H:i:s")."] Clean: \n".$text);
        if( BUFFER ) {
            if( is_file('buffer.txt') ) { 
                $buffer = fopen( 'buffer.txt', 'r' );
                $myBuffer = fread($buffer, filesize('buffer.txt'));
                fclose( $buffer );
                if( trim($myBuffer) == trim( $text ) ) {
                    echo "666";
                    exit;
                }
            }
            $buffer = fopen( 'buffer.txt', 'w' );
            fwrite( $buffer, $text );
            fclose( $buffer );
        }
        
        include_once( dirname(__FILE__) . '/class.bestspin.php' );
        $login = get_option('wpspin_login');
        $pass  = get_option('wpspin_pass');
        
        $mySpin = new Spin( 'http://thebestspinner.com/api.php', $login, $pass );
        if( !$mySpin->auth() ) {
            if( LOCAL_SPIN ) {
                $success = $mySpin->localSpinTextFile( $text, $_POST['maxsyns'] );
                $success = stripslashes( $success );
                echo $success;
                if( DEBUG ) fwrite($fp, "\n\n[".date("Y-m-d H:i:s")."] Info: \n - method: ".$_POST['method']." [local]\n - maxsyns: ".$_POST['maxsyns']."\n - quality: ".$_POST['quality']."\n - excluded: ".$_POST['excluded']."\nSpin: \n".$success);
                exit;
            }
            $errno = $mySpin->getError();
            if( $errno == '28' ) echo "Server timeout";
            else echo "Information is incorrect [1]";
            if( DEBUG ) fwrite($fp, "\n\n[".date("Y-m-d H:i:s")."] Information is incorrect [1][$errno] \n");
            exit;
        }

        if( !$success = $mySpin->spinText( $text, $_POST['method'], $_POST['maxsyns'], $_POST['quality'], $_POST['excluded'], FALSE, FALSE ) ) {
            $errno = $mySpin->getError();
            if( $errno == 28 ) echo "Server timeout";
            echo "Information is incorrect [2]";
            if( DEBUG ) fwrite($fp, "\n\n[".date("Y-m-d H:i:s")."] Information is incorrect [2][$errno] \n");
            exit;
        } else {
            $success = stripslashes( $success );
            echo $success;
            if( DEBUG ) fwrite($fp, "\n\n[".date("Y-m-d H:i:s")."] Info: \n - method: ".$_POST['method']."\n - maxsyns: ".$_POST['maxsyns']."\n - quality: ".$_POST['quality']."\n - excluded: ".$_POST['excluded']."\nSpin: \n".$success);
        }

    }
    
    if( isset( $_GET['logout'] ) ) {
        update_option( 'wpspin_login', '' );
        update_option( 'wpspin_pass', '' );
        echo 'done';
    }
    
    if( $_POST['login'] AND $_POST['pass'] ) {
        include_once( dirname(__FILE__) . '/class.bestspin.php' );
        $login = stripslashes( $_POST['login'] );
        $pass = stripslashes( $_POST['pass'] );
        $mySpin = new Spin( 'http://thebestspinner.com/api.php', $login, $pass );
        if( $mySpin->auth() ) {
            update_option( 'wpspin_login', $login );
            update_option( 'wpspin_pass', $pass );
            echo 1;
        } else {
            $errno = $mySpin->getError();
            if( $errno == 28 ) echo 28;
            else {
                update_option( 'wpspin_login', '' );
                update_option( 'wpspin_pass', '' );
                echo 0;
            }
        }
    }
    
    if( $_POST['status'] ) {
        global $wpdb;
        $status = $wpdb->get_var("SELECT `status` FROM `".$wpdb->prefix."wpspin_base` WHERE `id`='".stripslashes($_POST['status'])."' LIMIT 1");
        if( isset( $status ) ) {
            if( $status == 0 ) {
                $wpdb->query("UPDATE `".$wpdb->prefix."wpspin_base` SET `status`='1' WHERE `id`='".stripslashes($_POST['status'])."'");
                echo "<font color='green'>enabled</font>";
            } else {
                $wpdb->query("UPDATE `".$wpdb->prefix."wpspin_base` SET `status`='0' WHERE `id`='".stripslashes($_POST['status'])."'");
                echo "<font color='red'>disabled</font>";
            }
        } else echo "<font color='red'>error</font>";
    }
    
    if( $_POST['synonym'] ) {
        global $wpdb;
        if( $_POST['mode'] == 'add' ) {
            $sql = "INSERT INTO `".$wpdb->prefix."wpspin_synonyms` (`id`, `word`, `synonyms`) VALUES (NULL, '".stripslashes($_POST['w'])."', '".stripslashes($_POST['s'])."');";
            $wpdb->query($sql);
        } 
        
        if( $rows = $wpdb->get_results("SELECT * FROM `".$wpdb->prefix."wpspin_synonyms` WHERE 1", ARRAY_A) ) {
            //print_r( $rows );
            echo "<center><table style='width:50%'><thead><tr class='odd'><th scope='col'>Word</th><th scope='col'>Synonym</th></tr></thead><tbody>";
            foreach( $rows as $row ) {
                echo "<tr aligin='center'><td>".$row['word']."</td><td>".$row['synonyms']."</td></tr>";
            }
            echo "</tbody></table></center>";
        } else {
            echo "<center><font color='red'>Your synonyms not found.</font></center>";
        }
    }
?>