<?php
 
    session_start();
 
    require_once('config.php');
 
    require_once('usefullfunctions.php');
 
    $message='';
 
    $recurrence=false;
 
    // echo $_POST['pseudo'];
 
    // echo md5($_POST ['password']);
 
    if (empty($_POST['pseudo'])||empty($_POST['password']))
 
    //Oublie d'un champ
 
    {
 
    $message='<p>An error occured during your authentification.You must fill in all the fields</p>';
 
    }
 
    else
 
    //On check le mot de passe
 
    {
 
    $query=$bdd->prepare('SELECT user_id,password,username,groupid,statutvalidation,role
 
     FROM users 
 
     WHERE username =:pseudo');
 
    $query->bindValue(':pseudo',$_POST['pseudo'],PDO::PARAM_STR);
 
    $query->execute();
 
    $data=$query->fetch();
 
    // var_dump($data);
 
    if( $data ['password'] == md5($_POST['password']))
 
    // Acces OK !
 
    {
 
        if( $data ['statutvalidation'] ==1){
 
            $_SESSION [ 'pseudo' ] = $data [ 'username' ]; 
 
            $_SESSION [ 'idgroup' ] = $data [ 'groupid' ];
 
            $_SESSION [ 'id' ] = $data [ 'user_id' ];
 
            $_SESSION [ 'role' ] = $data [ 'role' ];
 
            // $message='ok' ;
 
            $query->CloseCursor();
 
 
            if($_SESSION['idgroup']){
 
                // $query=$bdd->prepare('SELECT * FROM groups WHERE idgroup =:groupid');
 
                $query=$bdd->prepare('SELECT idgroup,name,startaccesstime,endaccesstime,datecreation,datecreationtimestamp,recurrence,days FROM groups WHERE idgroup =:groupid');
 
                $query->bindValue(':groupid',$_SESSION['idgroup'],PDO::PARAM_STR);
 
                $query->execute();
 
                $data=$query->fetch();
 
                // var_dump($data);
 
                if($data['recurrence']=='once'){
 
                    if($data['datecreation']==date('Y-m-d')){
 
                        $recurrence=true;
 
                    }else{
 
                        $message="You could connect only the ".$data['datecreation']." and it's too late now.Please contact the administrator";
 
                    }
 
                }elseif($data['recurrence']=='custom'){
 
                    $days=explode(',',$data['days']);
 
                    foreach($days as $k=>$v){
 
                        $days[$k]=strtolower($days[$k]);
 
                    }
 
                    // var_dump($days);
 
                    // var_dump(in_array(strtolower(jddayofweek ( cal_to_jd(CAL_GREGORIAN, date("m"),date("d"), date("Y")) , 1 )),$days));
 
                    if(in_array(strtolower(jddayofweek ( cal_to_jd(CAL_GREGORIAN, date("m"),date("d"), date("Y")) , 1 )),$days)){
 
                        $recurrence=true;
 
                    }else{
 
                        $num=count($days);
 
                        $daystr="";
 
                        foreach($days as $key=>$value){
 
                            if($key==0){
 
                                $daystr.=" ".$value;
 
                            }
 
                            elseif($key==$num-1){
 
                                $daystr.="&".$value;
 
                            }else{
 
                                $daystr.=",".$value;
 
                            }
 
                        }
 
                        $message="You could connect only the ".$daystr." .Please retry these days";
 
                    }
 
                }else{
 
                // var_dump(reccurence_reached($data['datecreationtimestamp'],$data['recurrence']));var_dump($data['datecreationtimestamp']);
 
                    // var_dump($data['recurrence']);
 
                    if(reccurence_reached((int)$data['datecreationtimestamp'],$data['recurrence'])){
 
                        $recurrence=true;
 
                    }else{
 
                        $recur=array('dayly'=>86400,'weekly'=>604800,'monthly'=>2592000,'quartely'=>7776000,'yearly'=>31536000);
 
                        $time=time();
 
                        $r=(int)$data['datecreationtimestamp'];
 
                        while($r<$time){
 
                            $r+=$recur[$data['recurrence']];
 
                        }
 
                        $timeleft=$r-$time;
 
                        $d=(int)($timeleft/86400);
 
                        $h=(int)(($timeleft%86400)/3600);
 
                        $min=(int)((($timeleft%86400)%3600)/60);
 
                        $sec=((($timeleft%86400)%3600)%60);
 
                        $TIMELEFT=$d." days ".$h." hours ".$min." minutes and ".$sec." seconds";
 
                        $message="You could connect only in ".$TIMELEFT.", the ".date(("l jS F  Y \a\\t\ G:i:s "),$r).". Please retry this day at this time.";
 
                    }
 
                }
 
 
                if($recurrence){
 
 
                    if(inrangetime(explode(':',date('H:I:s')),explode(':',$data["startaccesstime"]),explode(':',$data["endaccesstime"]))){
 
                        $_SESSION [ 'groupstatut' ]=true;
 
                        $_SESSION["startaccesstime"] =$data["startaccesstime"];
 
                        $_SESSION["endaccesstime"] =$data["endaccesstime"];
 
                        $_SESSION [ 'groupname' ]=$data["name"];
 
                        $message='ok';
 
                    }else{
 
                        $message='<p>You can\'t actually login please try again between:<br />'.$data["startaccesstime"].' and '.$data["endaccesstime"].'</p>';
 
                    }
 
                }else{
 
                    $message='<p>You can\'t actually login please try again later</p>'.$message;
 
                }
 
            }
 
 
        }else{
 
            echo"You have not yet validate your account.Please check your email box and click the validation link to confirm your email address ";
 
        }
 
    }
 
    else
 
    // Acces pas OK !
 
    {
 
    $message='<p>An error occured during your authentification The pseudo or the password you entered is not correct</p>';
 
    }
 
    $query->CloseCursor();
 
    }
 
    echo $message;
 
    ?>
 
 |