<?php
   include_once '../../scripts/bd.php';
   
   if(isset($_GET['action']))
   {
    $action=$_GET['action'];
    if($action=='signup')signup();
    if($action=='signin')signin();
    if($action=='forgot')forgot();
   }
   
   /*function signup(){ 
        $firstname=$_POST["firstname"];
        $lastname=$_POST["lastname"];
        $phone=$_POST["phone"];
        $email=$_POST["email"];
        $imid=$_POST["imid"];
        $imservice=$_POST["imservice"];
        $username=$_POST["username"];
        $password=$_POST["password"];
        $challengequestion=addslashes($_POST["challengequestion"]);
        $answer=$_POST["answer"];
        $paymentname=$_POST["paymentname"];
        $paymentmethod=$_POST["paymentmethod"];
        $paymentinfos=$_POST["paymentinfos"];
        $statut="Under Review";
        $dateadd=date('Y-m-d h:i:s');
        $dateset=date('Y-m-d h:i:s');
        bd::query("INSERT INTO mailer VALUES (NULL, '$firstname', '$lastname', '$phone', '$email', '$imid', '$imservice', '$username', '$password', '$challengequestion', '$answer', '$paymentname', '$paymentmethod', '$paymentinfos', '$statut','$dateadd', '$dateset');");
   }*/
   
   
   function signin()
   {
        if(isset($_POST["username"]) && isset($_POST["password"]))
		{
            $username=str_replace(array("'","=","".'"'),"",$_POST["username"]);
            $password=str_replace(array("'","=","".'"'),"",$_POST["password"]); 
            
            get_server_session();
            $query = bd::query("SELECT id FROM server where id='{$_SESSION['id-server']}' and active=1");
            if(mysql_num_rows($query)>0)
			{
                $data = bd::query("SELECT * FROM account_mailer WHERE username='$username'");
                $row  = mysql_fetch_array($data);
                if($row!=null){
                   if($row['password']==$password){
                       if($row['status'] == 'Active') {
                            session_start();
                            $_SESSION['id-mailer'] = $row['id'];
                            $_SESSION['username-mailer'] = $row['username'];
                            $_SESSION['start'] = time();
                            $_SESSION['expire'] = $_SESSION['start'] + (60*60);
                            add_log("<span class='badge badge-success'>Logged in sucessfully</span>");
                            header("Location: /app/mailer/");
                        }else{
                            return 'Your acount is ' . $row['status'];
                        }
                    }else{
                        return 'Username or password not match';
                    }
                }else{
                  return 'Username or password not match';
                }
            }
			else
			{
                return 'Server unavailable';
            }
        }
   }
   
   function get_server_session()
   {
       
	    $server_ip=$_SERVER['SERVER_ADDR'];
	    $data = bd::query("SELECT server.id,server.name,server_vmta.address_ip FROM server,server_vmta WHERE main_ip='$server_ip' || server_vmta.address_ip='$server_ip' && server.id=server_vmta.id_server");
       
	   	 
	   if(mysql_num_rows($data)>0)
	   {
          $row=  mysql_fetch_array($data);
          session_start();
          $_SESSION["id-server"] = $row['id'];
          $_SESSION["name-server"] = $row['name']; 
       }
	   else
	   {
          
		  session_start();
          $_SESSION["id-server"] = '0';
          $_SESSION["name-server"] = 'Error'; 
       }
	   
	   
   }
   
   function add_log($str){
    $id_mailer=$_SESSION['id-mailer'];
    $id_server=$_SESSION['id-server'];
    $connect_ip=$_SERVER["REMOTE_ADDR"];
    $activity=addslashes($str);
    date_default_timezone_set('Africa/Casablanca');
    $date_activity=date('Y-m-d h:i:s');
    bd::query("INSERT INTO mailer_log VALUES (NULL, '$id_server', '$id_mailer', '$connect_ip', '$activity', '$date_activity');");
   }


   function forgot(){ 
        
   }
   
  
