<?php
function user_login($username,$user_password)
{

    global $connection, $login, $loged_user_name, $loged_user_id,$rememberme;

    $user_password = md5($user_password);

    $user_query = mysql_query("SELECT * FROM user WHERE user_name='$username'",$connection);

    if($user_fetch = mysql_fetch_array($user_query))

    {

        extract($user_fetch);

        if($password==$user_password && $state=="1")

        {

            $login = 1;
            // mysql_query("UPDATE user SET user_lastlogin='".time()."' WHERE user_id='$user_id'");
            if(isset($rememberme))
            {
                setcookie('userid', $id, time()+604800);
                setcookie('userpassword', $password, time()+604800);
                setcookie('username', $username, time()+604800);
                setcookie('remem',"true",time()+604800);
            }
            else
            {
                setcookie('userid', $id, time()+3600);
                setcookie('userpassword', $password, time()+3600);
                setcookie('username', $username, time()+3600);
                setcookie('remem',"false",time()+3600);
            }
            //echo
            $username = $password = "";

        }

        elseif($user_activation!="0" && $password==$user_password)

        {

            $login = 2;

        }

        elseif($user_state!="1" && $password==$user_password)

        {

            $login = 3;

        }

        else

        {

            $login = 4;

        }

    }

    else

    {

        $login = 0;

    }

}
///////////////////////////////////////////////////////////////////////////
/// user_check() function varifies the cookies details for site user
function user_check()
{

    /* NOTE    */

    global $connection, $login, $loged_user_name, $loged_user_id;

    $user_query = mysql_query("SELECT * FROM user WHERE id='$_COOKIE[userid]'",$connection);

    if($user_fetch = mysql_fetch_array($user_query))

    {

        extract($user_fetch);

        if($_COOKIE['userpassword']==$password && $state=="1")

        {

            $login = 1;

            if($_COOKIE["remem"] == "true")
            {
                setcookie('userid', $id, time()+604800);
                setcookie('userpassword', $password, time()+604800);
                setcookie('username', $user_name, time()+604800);
                setcookie('remem',"true",time()+604800);
            }
            else
            {
                setcookie('userid', $id, time()+3600);
                setcookie('userpassword', $password, time()+3600);
                setcookie('username', $user_name, time()+3600);
                setcookie('remem',"false",time()+3600);
            }


        }

        elseif($user_activation!="0" && $password==$user_password)

        {

            $login = 2;

        }

        elseif($user_state!="1" && $password==$user_password)

        {

            $login = 3;

        }

        else

        {

            $login = 4;

            setcookie("userpassword", "", time()+3600);

            setcookie("userid", "", time()+3600);

        }

    }

    else

    {

        $login = 0;

    }

}
//////////////////////////////////////////////////////////////////////////
function headmenu()
{
  global $connection, $site_template, $msg, $sub, $pages, $templates, $start, $state, $statet, $state0, $cat,$per_page,$_GET;
  global $head_id,$name,$content,$place;
  $headquery=mysql_query("select * from head_menu where view_place='1' && state='1'");
  $head=Template_Array("$site_template/headmenu.html",0);
  while($headrow=mysql_fetch_array($headquery))
  {
    extract($headrow);
    $head .=Template_Array("$site_template/headmenu.html",1);
  }
  $head .=Template_Array("$site_template/headmenu.html",2);
  return $head;
}
/////////////////////////////////////////////////////////////////////////
function adsright()
{
  global $connection, $site_template, $msg, $sub, $pages, $templates, $start, $state, $statet, $state0, $cat,$per_page,$_GET;
  global $id,$link,$pic,$view_place,$place;
 $offerquery=mysql_query("select * from ads where view_place='3'");
$hostuser=Template_Array("$site_template/ads.html",0);
while($offerrow=mysql_fetch_array($offerquery))
{
  extract($offerrow);
  $hostuser .=Template_Array("$site_template/ads.html",1);
}
$hostuser .=Template_Array("$site_template/ads.html",2);
return $hostuser;
}
////////////////////////////////////////////////////////////////////////////
function ticket()
{
  global $connection, $site_template, $msg, $sub, $pages, $templates, $start, $state, $statet, $state0, $cat,$per_page,$_GET;
  global $id,$title,$cat_id,$catname,$impotance,$ticket_date,$user_id;
  $headquery=mysql_query("select * from support_ticket where user_id='$_COOKIE[userid]'");
  $head=Template_Array("$site_template/allticket.html",0);
  while($headrow=mysql_fetch_array($headquery))
  {
    extract($headrow);
    $catname=mysql_result(mysql_query("select name as cname from support_cat where id=$headrow[cat_id]"),"cname");
    switch($impotance)
    {
      case 1;
         $impotance="عادى";
         break;
         case 2;
         $impotance="متوسط";
         break;
         case 3;
         $impotance="مهم";
         break;
         case 4;
         $impotance="مهم جدا";
         break ; 
    }
    $ticket_date=date("Y-m-d",$ticket_date);
     if($state==1)
        {
            $state = "مفتوح";
            $state0 = "اغلاق";
            $statet = "تعطيل";
        }
        else
        {
            $state = "مغلق";
            
        }
    $head .=Template_Array("$site_template/allticket.html",1);
  }
  $head .=Template_Array("$site_template/allticket.html",2);
  return $head;
}
///////////////////////////////////////////////////////////////////////////
function sidemenu()
{
  global $connection, $site_template, $msg, $sub, $pages, $templates, $start, $state, $statet, $state0, $cat,$per_page,$_GET;
  global $head_id,$name,$content,$place;
  $headquery=mysql_query("select * from head_menu where view_place='3' && state='1'");
  $head=Template_Array("$site_template/mainmenu.html",0);
  while($headrow=mysql_fetch_array($headquery))
  {
    extract($headrow);
    $head .=Template_Array("$site_template/mainmenu.html",1);
  }
  $head .=Template_Array("$site_template/mainmenu.html",2);
  return $head;
}
?>