<?php
/**
 * Version:			1.0.0
 * Author:			ali mogheimi
 * Author URI:		http://wpcar.ir
 * Date: 17/01/2016
 */

if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
    die('Error , cannot Access To this File .');
}

$nav ='';
$error ='';
$direction = ' <link href="../assets/main-ltr.css" rel="stylesheet">';
if (isset($_GET['lang']) &&( $_GET['lang']=='fa_IR' ||  $_GET['lang']=='fa')){
    $direction = ' <link href="../assets/main-rtl.css" rel="stylesheet">';
}
if (empty($_POST) || (isset($_POST)&& $_POST['page']=='first') ){
    $nav = 1;
    $section = '<h2>'.$step1.' - '.$choicelang.'</h2>' ;
    $section .='<p>'.$step1_p.'</p>';
    $section .='<script type="text/javascript">
                $(document).ready(function() {
                    $("#ch_lang").submit(function() {
                        var dropdownVal = $("#select_lang").val();
                        switch( dropdownVal ) {

                            case "English":
                                $(this).attr("action", "/index.php?lang=en");

                                break;
                            case "Persian":
                                $(this).attr("action", "/index.php?lang=fa");

                                break;
                        }
                    })
                });
                </script>';

    $section .='
                <form action="" method="post" id="ch_lang"><h1><select id="select_lang">
                    <option value="English">English</option>
                     <option value="Persian" selected>Persian</option>
                </select></h1>
                <input type="hidden" name="page"  value="step1"/>
                <input type="submit" name="submit" class="button" value="'.$next_label.'"/></form>' ;

}elseif (!empty($_POST))
{

    switch ( $_POST["page"] )
    {
        case  'step1' :


            $nav = 2;
            $section = '<h2>'.$step2.' - '.$createdatabase.'</h2>' ;


            if (isset($_POST['createtable'])){
                if (isset($_POST['createtable']) && isset($_POST['mysqlserver']) && isset($_POST['dbname']) &&isset($_POST['dbusername']) && isset($_POST['dbpassword'])&& isset($_POST['tablename'])
                    && $_POST['createtable']!='' &&  $_POST['mysqlserver']!='' && $_POST['dbname']!='' && $_POST['dbusername']!='' && $_POST['tablename']!=''
                ){
                    $resultfunc = createtable(wpcar_sanitize_data($_POST['mysqlserver']),wpcar_sanitize_data($_POST['dbusername']),wpcar_sanitize_data($_POST['dbpassword']),wpcar_sanitize_data($_POST['dbname']),wpcar_sanitize_data($_POST['tablename']));
                    if ($resultfunc== 'TRUE'){
                        // table created
                        $section .='<form action="" id="createdbform" method="post">
                                   <div style="margin:10px;">'.$create_table_success.'</div>
                                   <input type="hidden" name="mysqlserver"  value="'.$_POST['mysqlserver'].'"/>
                                   <input type="hidden" name="dbusername"  value="'.$_POST['dbusername'].'"/>
                                   <input type="hidden" name="dbpassword"  value="'.$_POST['dbpassword'].'"/>
                                   <input type="hidden" name="dbname"  value="'.$_POST['dbname'].'"/>
                                   <input type="hidden" name="tablename"  value="'.$_POST['tablename'].'"/>

                                   <input type="hidden" name="page" id="page" value="step2"/>
                                   <input type="submit" name="submit"  value="'.$next_label.'"/>
                                   </form>';
                        break;
                    } else{
                        // table not created please check data

                        $section .='<div style="margin:20px;color:orangered;">'.$create_tb_mysqlerror.'</div>';
                    }
                }else{
                    // not enough variable
                    $section .='<div style="margin:20px;color:orangered;">'.$create_tb_variable.'</div>';
                }
            }
            $section .='<p>'.$step2_p.'</p>';
            $section .='<script type="text/javascript">
                $(document).ready(function() {
                    $("#back").click(function() {
                     $("#page").attr("value", "first");
                     $("#createdbform").submit();
                    });
                    $("#skip").click(function() {

                     $("#page").attr("value", "step2");
                     $("#createdbform").reset();
                     $("#createdbform").submit();
                    });


             });
                </script>';
            $section .='

                <form action="" id="createdbform" method="post"><p>
                <div><p>
                     '.$label_dbmysqlser.'</p><span>'.$label_sqlserver_span.'</span>

                </div><input type="text" name="mysqlserver"  value=""/>
                <div><p>
               '.$label_dbname.'</p><span>'.$label_dbname_span.'</span>


                </div><input type="text" name="dbname"  value=""/>
                <div><p>
               '.$label_dbuser.'</p><span>'.$label_dbuser_span.'</span>


                </div><input type="text" name="dbusername"  value=""/>
                <div><p>
                '.$label_dbpass.'</p><span>'.$label_dbpass_span.'</span>


                </div><input type="text" name="dbpassword"  value=""/>
                <div><p>
               '.$label_dbtable.'</p><span>'.$label_table_span.'</span>

                </div> <input type="text" name="tablename"  value=""/>
                ';

            $section .='
                <div style="margin:1em 0;">
                <input type="hidden" name="page" id="page" value="step1"/>
                <input type="submit" name="back" id="back" value="'.$back_label.'"/>
                <input type="submit" name="skip" id="skip" value="'.$skip_label.'"/>
                <input type="submit" name="createtable" id="createtable" value="'.$create_label.'"/>

                </div>
                </p>
                </form>' ;
            break;
        case 'step2' :
            $nav = 3;
            $section = '<h2>'.$step3.' - '.$otherconfig.'</h2>' ;

            if (isset($_POST['createfile'])){
                if (isset($_POST['passphrase']) && $_POST['passphrase']!='' ){
                    // create config file

                    $acceptedserver_list = str_replace(array("\r"," "), '', $_POST['acceptedserver']);
                    $acceptedserver_list = trim($acceptedserver_list);
                    $acceptedserver_list = 'array("'.str_replace("\n", '","', $acceptedserver_list).'")';
                    $acceptedserver_list = strtolower($acceptedserver_list);
                    $mydomain = strtolower($_POST['domain_for_error_page']);
                    $mydomain = clean_url($mydomain);
                    if (!empty ($mydomain))  $mydomain = 'http://'.clean_url($mydomain);
                    $writearray[] = "<?php ";
                    $writearray[] = "if (basename("."$"."_SERVER['PHP_SELF']) == basename(__FILE__))  die('Error , cannot Access To this File .');";
                    $writearray[] = "$"."config = array();";
                    $writearray[] = "$"."config['version'] = '1.0';";
                    $writearray[] = "$"."config['mysqlserver'] = '".$_POST['mysqlserver']."';";
                    $writearray[] = "$"."config['dbusername'] = '".$_POST['dbusername']."';";
                    $writearray[] = "$"."config['dbpassword'] = '".$_POST['dbpassword']."';";
                    $writearray[] = "$"."config['dbname'] = '".$_POST['dbname']."';";
                    $writearray[] = "$"."config['tablename'] = '".$_POST['tablename']."';";
                    $writearray[] = "$"."config['passphrase'] = '".$_POST['passphrase']."';// secret password";
                    $accepted_state = isset($_POST['accepted'])?$_POST['accepted']:'off';
                    $lockip_state = isset($_POST['lockip'])?$_POST['lockip']:'off';
                    $writearray[] = "$"."config['lock_to_ip'] = '". $lockip_state."';// on or off"; // on or ''
                    $writearray[] = "$"."config['hidden_folder'] = '".$_POST['hiddenfolder']."';";
                    $writearray[] = "$"."config['accepted'] = '". $accepted_state."';// on or off"; // on or ''
                    $writearray[] = "$"."config['acceptedserver'] = ".$acceptedserver_list."; // example: array('test.com','test1.com')";
                    $writearray[] = "$"."config['domain_for_error_page'] = '".$mydomain."';// example:'http://test.com'";
                    $writearray[] = "$"."config['state'] = 'INSTALLED';//NOT or INSTALLED";


                    $writestr = implode("\n", $writearray);
                    if (file_put_contents('include/configs.php', $writestr)>100 ){
                        $nav = 4;

                        $section = '<h2>'.$step4.' - '.$tankyou.'</h2>' ;
                        $section .='<p>'.$step4_p.'</p>';

                        break;
                    }else {
                        // problem with create file
                        $section .='<div style="margin:20px;color:orangered;">'.$createfile_problem.'</div>';

                    }



                }else{
                    $section .='<div style="margin:20px;color:orangered;">'.$createfile_pass_need.'</div>';
                }
            }

            $section .='<p>'.$step3_p.'</p>';
            $section .='<script type="text/javascript">
                $(document).ready(function() {
                    $("#back").click(function() {
                     $("#page").attr("value", "step1");
                     $("#settings").reset();
                     $("#settings").submit();
                    });

                });
                </script>';
            $section .='<form id="settings" action="" method="post">
                <div>
                 <p>'.$label_hiddenfolder.'</p><span>'.$label_hiddenfolder_span.'</span>

                </div>
                <input type="text" name="hiddenfolder"  value=""/>

                <div>
                <p>'.$label_passphrase.'</p><span>'.$label_passphrase_span.'</span>
                </div>
                <input type="text" name="passphrase"  value=""/>

                <div>
                <p>'.$label_errordomain.'</p><span>'.$label_errordomain_span.'</span>
                </div>
                <input type="text" name="domain_for_error_page"  value=""/>

                <div>
                 <p>'.$label_lockip.'</p>
                </div>
                <input type="checkbox" name="lockip"  ><span>'.$label_lockip_span.'</span>

                <div>
                <p>'.$label_acceptedmode.'</p>
                </div>
                <input type="checkbox" name="accepted"  ><span>'.$label_acceptedmode_span.'</span>

                <div>
                <p>'.$label_accepted_txt.'</p>
                </div>
                <div>
                <textarea   name="acceptedserver"></textarea>
                </div>


                <div style="margin:1em 0;">
                <input type="hidden" name="page" id="page" value="step2"/>

                <input type="hidden" name="mysqlserver"  value="'.$_POST['mysqlserver'].'"/>
                <input type="hidden" name="dbusername"  value="'.$_POST['dbusername'].'"/>
                <input type="hidden" name="dbpassword"  value="'.$_POST['dbpassword'].'"/>
                <input type="hidden" name="dbname"  value="'.$_POST['dbname'].'"/>
                <input type="hidden" name="tablename"  value="'.$_POST['tablename'].'"/>


                <input type="submit" name="back"  id="back" value="'.$back_label.'"/>
                <input type="submit" name="createfile"  id="createfile" value="'.$createfile_label.'"/>
                <!--input type="submit" name="submit"  id="" value="'.$next_label.'"/-->
                </div>
                </form>' ;


            break;


    }
}

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Setup Remote Host Settings For Edd Remote</title>
    <?php echo $direction;?>

    <script src="../assets/jquery.min.js"></script>
    <script src="../assets/jquery-ui.js"></script>
</head>
<body >
<div class="container">
    <div id="header">
        <h1><?php   echo $header_title;?></h1>

    </div>



    <div id="nav">
        <ul class="list">
            <li ><a href="#" class="<?php if ($nav == 1) echo 'active'; ?>"><?php echo $choicelang;?></a> </li>
            <li><a href="#" class="<?php if ($nav == 2) echo 'active'; ?>"><?php echo $createdatabase;?></a></li>
            <li><a href="#" class="<?php if ($nav == 3) echo 'active'; ?>"><?php echo $getdata;?></a></li>
            <li ><a href="#" class="<?php if ($nav == 4) echo 'active'; ?>"><?php echo $tankyou ;?></a></li>
        </ul>
    </div>

    <div id="section">
        <div class="error">
            <?php   echo $error;?>
        </div>
        <div class="content">
            <?php   echo $section;?>

        </div>
    </div>

</div>
<div id="footer">
    <?php   echo $footer;?>

</div>
</body>
</html>