<?
set_time_limit(6000);

class admin_dialog_import extends admin_dialog {

    function makeData() {
        parent::makeData();

        $this->form_fields["dbhost"] = $_REQUEST["dbhost"];
        $this->form_fields["dbname"] = $_REQUEST["dbname"];
        $this->form_fields["dbuser"] = $_REQUEST["dbuser"];
        $this->form_fields["dbpwd"] = $_REQUEST["dbpwd"];
        $this->form_fields["dbtable"] = $_REQUEST["dbtable"];
        $this->form_fields["femail"] = $_REQUEST["femail"];
        $this->form_fields["fpwd"] = $_REQUEST["fpwd"];
    }



    function action_import() {
        $dbhost = $_REQUEST["dbhost"];
        $dbname = $_REQUEST["dbname"];
        $dbuser = $_REQUEST["dbuser"];
        $dbpwd = $_REQUEST["dbpwd"];

        if($dblink = mysql_connect($dbhost, $dbuser, $dbpwd)) {
            if(mysql_select_db($dbname, $dblink)) {
                $dbtable = addslashes($_REQUEST["dbtable"]);
                $femail = addslashes($_REQUEST["femail"]);
                $fpwd = addslashes($_REQUEST["fpwd"]);

                $idx = 0;
                $this->total = 0;
                $this->imported = 0;
                $this->duplicated = 0;

                $test_sql = "
                    SELECT
                        id
                    FROM
                        "._DB_TABLE_PREFIX."users
                    WHERE
                        email = %e%";

                do {
                    $sql = "
                        SELECT
                            $femail,
                            $fpwd
                        FROM
                            $dbtable
                        LIMIT $idx, 50";
                    $res = mysql_query($sql, $dblink);

                    while($row = mysql_fetch_row($res)) {
                        
                        $t = $this->db->get_one($test_sql, $row[0]);
                        if($t) {
                            $this->duplicated ++;
                        } else {
                            $sql = "
                                INSERT INTO
                                    "._DB_TABLE_PREFIX."users
                                    (email, password)
                                VALUES
                                    ('".$row[0]."', '".$row[1]."')";
                            $this->db->query($sql);
                            $this->imported ++;
                        }
                        $this->total ++;
                    }
                    $idx += 50;
                } while (mysql_num_rows($res) > 0);

                $this->msg = "success";

            } else {
                $this->msg = "error_select";
            }
        } else {
        die("A");
            $this->msg = "error_connect";
        }
    }


    function action_create() {
        $_SESSION["email"] = $_REQUEST["email"];
        $this->info = $this->db->insert(_DB_TABLE_PREFIX."users", 
                                        array("id"       => "1", 
                                              "email"    => $_REQUEST["email"],
                                              "password" => $_REQUEST["password"]));
    }
    

}

?>