<?php

/*
    LOGIN CLASS
    belépés végrehajtása

*/


class login {

    public function check_login($_user_name, $_user_password) {
        global $sql;
        //ez a függvény ellenőrzi le a bevitt adatok
        //vissszadja a user_id-t, ha helyesek az adatok
        //null ha nem
        $check_query =
        "
        SELECT ua_id FROM user_coach WHERE
        (ua_name = '" . $_user_name . "' AND
        ua_password = '" . md5($_user_password ) . "')";

        if ($sql->num_of_rows($check_query)) return $sql->single_variable($check_query);


        $check_query =
        "
        SELECT uk_id FROM user_kid WHERE
        (uk_name = '" . $_user_name . "' AND
        uk_password = '" . md5($_user_password ) . "') AND uk_deleted = 0 AND uk_is_active = 1";

        if ($sql->num_of_rows($check_query)) return $sql->single_variable($check_query);

        $check_query =
        "
        SELECT up_id FROM user_parent WHERE
        (up_name = '" . $_user_name . "' AND
        up_password = '" . md5($_user_password ) . "')";

        if ($sql->num_of_rows($check_query)) return $sql->single_variable($check_query);

        return null;
    }

    public function check_admin_login($_user_name, $_user_password) {
        global $sql;
        //ez a függvény ellenőrzi le a bevitt adatok
        //vissszadja a user_id-t, ha helyesek az adatok
        //null ha nem
        $check_query =
        "
        SELECT ua_id FROM user_coach WHERE
        (ua_name = '" . $_user_name . "' AND
        ua_password = '" . md5($_user_password ) . "') AND ua_deleted = 0 AND ua_admin = 1";

        //echo  $check_query;

        if ($sql->num_of_rows($check_query)) return $sql->single_variable($check_query);

        return null;
    }

    public function check_kid_login($_user_name, $_user_password) {
        global $sql;
        $check_query =
        "
        SELECT uk_id FROM user_kid WHERE
        (uk_name = '" . $_user_name . "' AND
        uk_password = '" . $_user_password . "') AND uk_deleted = 0 AND uk_is_active = 1";

        if ($sql->num_of_rows($check_query)) return $sql->single_variable($check_query);

        return null;
    }

    public function check_coach_login($_user_name, $_user_password) {
        global $sql;
        $check_query =
        "
        SELECT ua_id FROM user_coach WHERE
        (ua_name = '" . $_user_name . "' AND
        ua_password = '" . md5($_user_password ) . "') AND ua_deleted = 0";

        //echo  $check_query;

        if ($sql->num_of_rows($check_query)) return $sql->single_variable($check_query);

        return null;
    }

    public function check_camp_login($_user_name, $_user_password) {
        global $sql;
        $check_query =
        "
        SELECT cu_id FROM camp_user WHERE
        (cu_email = '" . $_user_name . "' AND
        cu_password = '" . md5($_user_password ) . "') AND cu_deleted = 0";

        //echo  $check_query;

        if ($sql->num_of_rows($check_query)) return $sql->single_variable($check_query);

        return null;
    }

    public function check_camp_leader_login($_user_name, $_user_password) {
        global $sql;
        $check_query =
        "
        SELECT ucl_id FROM user_camp_leader WHERE
        (ucl_name = '" . $_user_name . "' AND
        ucl_password = '" . md5($_user_password ) . "') AND ucl_deleted = 0";

        //echo  $check_query;

        if ($sql->num_of_rows($check_query)) return $sql->single_variable($check_query);

        return null;
    }

    public function login_user($_user_id, $_cookie_name, $_user_type) {
        //beállítja a belépett user adatait cookieba (ha még nincs)
        global $sql, $config;

        if (!isset($_COOKIE[$_cookie_name])) {
            //user objektumot nem lehet cookie-ban tárolni, ezért user_id-t rakunk bele
            if ($_user_type == 1) {
                $user_login = new user();
            }
            elseif ($_user_type == 2) {
                $user_login = new user_kid();
            }
            elseif ($_user_type == 3) {
                $user_login = new user();
            }
            elseif ($_user_type == 4) {
                $user_login = new camp_user();
            }
            elseif ($_user_type == 5) {
                $user_login = new user_camp_leader();
            }
            $user_login->set_user_data_by_id($_user_id);
            $user_login->update_login_time();
            setcookie($_cookie_name, $_user_id, time()+60*60*168, '/', $config['domain']);
            //var_dump(setcookie($_cookie_name, $_user_id));

        }


    }

}




?>
