<?
require_once("core/mailer.php");

class module extends controller {

    var $params = array("act");
    var $isNeedMysql = true;


    function createAnonymus() {
        $this->db->query("INSERT INTO "._DB_TABLE_PREFIX."users (email, password) VALUES ('','')");
        return $this->db->getLastId();
    }                                                                                

 
    function run() {
        $user_id = isset($_REQUEST['user_id']) ? getSafeStr($_REQUEST['user_id']) : null;
        $email = isset($_REQUEST['email']) ? getSafeStr($_REQUEST['email']) : null;
        $password = isset($_REQUEST['password']) ? getSafeStr($_REQUEST['password']) : null;

        switch($this->act) {
            case "start_anonymus":
                if($user_id) {
                    $res = $this->db->query("SELECT id FROM "._DB_TABLE_PREFIX."users WHERE id = '".$user_id."' AND email = '' AND password = '' LIMIT 1");
                    if($res) {
                        $res_id = $res["id"];
                    } else {
                        $res_id = $this->createAnonymus();
                    }
                } else {
                    $res_id = $this->createAnonymus();
                }
                $_SESSION['user_id'] = $res_id;
                return array("status" => "start_result", 
                             "user" => array("id" => $res_id, 
                                             "email" => "", 
                                             "password" => ""));
                break;



            case "start_user":
                if($email && $password) {
                    if($res = $this->db->query("SELECT id FROM "._DB_TABLE_PREFIX."users WHERE email='".$email."' AND password='".$password."' LIMIT 1")) {
                        $_SESSION['user_id'] = $res["id"];
                        return array("status" => "start_result", 
                                     "user" => array("id" => $res["id"], 
                                                     "email" => $email, 
                                                     "password" => $password ) );
                    } 
                }
                $res_id = $this->createAnonymus();
                $_SESSION['user_id'] = $res_id;
                return array("status" => "start_result", 
                             "user" => array("id" => $res_id, 
                                             "email" => "", 
                                             "password" => ""));
                break;



            case "login_user":
                if(!$email || !$password) {
                    return false;
                }
                $sql = "
                    SELECT 
                        id, email
                    FROM 
                        "._DB_TABLE_PREFIX."users 
                    WHERE 
                        email='".$email."' AND password='".$password."' 
                    LIMIT 1";
                $res = $this->db->query($sql);
                if(isset($res["id"])) {
                    $_SESSION['user_id'] = $res["id"];
                    return array("status" => "login_ok", 
                                 "user" => array("id" => $res["id"], 
                                                 "email"=>$email, 
                                                 "password"=>$password ) );
                } else {
                    return array("status" => "login_error");
                }
                break;



            case "register_user":
                if(!$email || !$password) {
                    return false;
                }
                $sql = "
                    SELECT 
                        id, email, password
                    FROM
                        "._DB_TABLE_PREFIX."users
                    WHERE
                        email='".$email."'
                    LIMIT 1";
                $res = $this->db->query($sql);
                if(isset($res["id"])) {
                    return array("status" => "register_error");
                } else {
                    $sql = "
                        UPDATE 
                            "._DB_TABLE_PREFIX."users 
                        SET 
                            email = '".$email."', 
                            password = '".$password."' 
                        WHERE 
                            id = '".$this->user_id."'
                        LIMIT 1";

                    $this->db->query($sql);

                    $m = new Mailer;
                    $m->email = $email;
                    $m->pwd = $password;
                    $m->server = $_SERVER["SERVER_NAME"];
                    $m->compose("templates/mail/registration");
                    $m->send($email);

                    return array("status" => "register_ok", 
                                 "user" => array("id" => $this->user_id, 
                                                 "email" => $email, 
                                                 "password" => $password) );
                }
                break;


            case "logout":
                session_destroy();
                return array("status" => "logout_ok");                
                break;


            case "remind_pwd":
                if($email) {
                    $sql = "
                        SELECT
                            id, password
                        FROM
                            "._DB_TABLE_PREFIX."users
                        WHERE
                            email='".$email."'
                        LIMIT 1";

                    $res = $this->db->query($sql);
                    if(isset($res["id"])) {
                        $m = new Mailer;
                        $m->pwd = $res["password"];
                        $m->compose("templates/mail/password_recovery");
                        $m->send($email);
                    }
                }
                return array("status" => "pwd_sent");
                break;
        }
    }

}
?>
