<?php

// version 1.0 du 24/03/2009

$cas_hostname="auth.univ.fr";
$cas_port=443;
$cas_uri='';

$imap_server="mail.univ.fr";
$imap_port=143;
$imap_box="INBOX";
$imap_service="imap://$imap_server";

include_once('CAS/CAS.php');

session_start();

$HAVE_PGT=false;
$HAVE_PT=false;

$USER=null;
$CREDENTIAL=null;
$err='';
$msg='';
$classique=false; 

$sess_user = isset($_SESSION['USER']) ? $_SESSION['USER'] : "";
$sess_password = isset($_SESSION['PASSWORD']) ? $_SESSION['PASSWORD'] : "";


// logout applicatif sans logout CAS
if( !empty($_POST['BT_LOGOUT_PHP'])){
  session_unset();                   // supprime la session CAS
  $_GET['ticket']='';                // supprime le ticket
}

// logout applicatif et CAS
if( !empty($_POST['BT_LOGOUT_CAS'])){
  session_unset();                   // supprime la session CAS
  $_GET['ticket']='';                // supprime le ticket

  $CAS = new CASClient(CAS_VERSION_2_0,TRUE,$cas_hostname,$cas_port,$cas_uri);// objet CAS en mode proxy
  $CAS->setNoCasServerValidation();
  $cas_url = $CAS->getServerLogoutURL(); // recupere l'URL du logout de CAS
  $cas_url.="?service=".$CAS->getURL();  // ajoute l'URL du service courant pour revenir


  header('Location: '.$cas_url);         // redirige vers le logout CAS
  exit;
}

// procedure pour obtenir un PGT
if( !empty($_POST['BT_GET_PGT']) ||     // obtenir un PGT
    !empty($_GET['ticket']) ||          // si on recoit le ticket pour le valider
    !empty($_GET['pgtIou'])){           // si on recoit le PGT

    if(!empty($_SESSION['PGT'])){
        session_unset(); // supprime la session CAS
        $_GET['ticket']=''; // supprime le ticket
    }

    $CAS = new CASClient(CAS_VERSION_2_0,TRUE,$cas_hostname,$cas_port,$cas_uri);// objet CAS en mode proxy
    $CAS->setNoCasServerValidation();
    // pour mettre le fichier d'echange de PGT dans un emplacement spécifique
    //$CAS->setPGTStorageFile('plain',"/home/cri/jmarchal/public_html/www");
    $auth = $CAS->isAuthenticated();                                            // test si deja authentifié (session applicative)
    if(!$auth) { 
      $CAS->forceAuthentication();              // force l'authentification si pas authentifié
    }
    if($CAS->hasPGT()){
        $PGT=$CAS->getPGT();                                                  // recupère le PGT
        $_SESSION['PGT']=$PGT;                                                // met le PGT EN SESSION
    }
}

// procedure pour obtenir ou reobtenir un PT
if(!empty($_POST['BT_GET_PT'])){
    $CAS = new CASClient(CAS_VERSION_2_0,TRUE,$cas_hostname,$cas_port,$cas_uri);// objet CAS en mode proxy
    $CAS->setNoCasServerValidation();
    $auth = $CAS->isAuthenticated();                                            // test si deja authentifié (session applicative)
    if(!$auth) $CAS->forceAuthentication();                                     // force l'authentification si pas authentifié

    if($CAS->hasPGT()){                                                         // si on a un PGT
        $PT = $CAS->retrievePT($imap_service,$err_code,$output);                // on récupère un PT
        $_SESSION['PT']=$PT;                                                    // met le PT EN SESSION
    }

}


// CONNEXION IMAP avec PT
if(!empty($_POST['BT_IMAP_PT'])){
    $CAS = new CASClient(CAS_VERSION_2_0,TRUE,$cas_hostname,$cas_port,$cas_uri);// objet CAS en mode proxy
    $CAS->setNoCasServerValidation();
    $auth = $CAS->isAuthenticated();                                            // test si deja authentifié (session applicative)
    if($auth){                                                                  // test si deja authentifié (session applicative)
       $USER=$CAS->getUser();                                                   // recupere le login utilisateur
       $CREDENTIAL=$_SESSION['PT'];   
    }else{
        $err="Erreur vous devez deja avoir une session CAS (PT)";
    }
}

// variable servant pour l'afficahge correct des boutons
if(!empty($PGT) || !empty($_SESSION['PGT'])){
        $HAVE_PGT=true;                                                          // ON A UN PGT
}

if(!empty($PT) || !empty($_SESSION['PT'])){                                      // ON A UN PT
        $HAVE_PT=true;
		if (!empty($_SESSION['PT'])){
		  $CAS_UrlValidTicket = $CAS->getServerBaseURL() . "proxyValidate" . "?ticket=" . $_SESSION['PT'] . "&service=" . $imap_service;
		}
}



// login classique IMAP avec user et pass
if(!empty($_POST['BT_IMAP_LOGIN'])){
    if(!empty($_POST['USER_LOGIN']) && !empty($_POST['USER_PASS'])){
        $USER=$_POST['USER_LOGIN'];
		$_SESSION['USER']=$USER;
        $CREDENTIAL=$_POST['USER_PASS'];
		$_SESSION['PASSWORD']=$CREDENTIAL;		     // pas beau. C'est juste pour une demo
	    $classique=true;
    }else{
        $err="Erreur vous devez fournir un login et un mot de passe <BR> pour une connexion classique.";
    }

}

// SI ON A UN USER ET MDP ON TENTE UNE CONNEXION IMAP
if(!empty($USER) && !empty($CREDENTIAL)){
    $messa = "";
    $mbox  = @imap_open("{".$imap_server.":".$imap_port."}".$imap_box,$USER,$CREDENTIAL);
	if (! $classique){
		$messa = " - PT : $CREDENTIAL";
	}
    if(!$mbox){
        $err="Erreur d'ouverture de connexion IMAP.<BR>AVEC :<BR> - USER : $USER$messa<BR>";
    }else{
        $msg="Ouverture de connexion IMAP OK.<BR>AVEC :<BR> - USER : $USER$messa<BR>";
    }
}

?>

<html>
<head>
<title>Test IMAP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form method="post" action="<?=$_SERVER['PHP_SELF']?>" name="form">
  <table border="0" cellspacing="3" cellpadding="2" align="center">
<? if(!empty($err)): ?>
    <tr>
      <td colspan="2" align="center"> <b><font size="4" color="#ff0000"><?=$err?></font></b>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center"> <HR>&nbsp;</b>
      </td>
    </tr>
<? endif; ?>
<? if(!empty($msg)): ?>
    <tr>
      <td colspan="2" align="center"> <b><font size="4" ><?=$msg?></font></b>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center"> <HR>&nbsp;</b>
      </td>
    </tr>
<? endif; ?>
    <tr>
      <td colspan="2" align="center"> <b><font size="4">PARTIE CAS</font></b>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
  <? $version = phpCAS::getVersion(); ?>
         Version phpCAS : <?=$version?>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
        <input type="submit" name="BT_LOGOUT_CAS" value="Logout applicatif et CAS">
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
        <input type="submit" name="BT_LOGOUT_PHP" value="Logout applicatif  sans CAS">
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
        <input type="submit" name="BT_GET_PGT" value="Obtenir un PGT (redirection CAS)">
      </td>
    </tr>
<? if($HAVE_PGT) : ?>
    <tr>
      <td colspan="2" align="center">
       <? $pgt_obtenu = isset($PGT) ? $PGT : ""; ?>
        <b>PGT obtenu : <?=$pgt_obtenu?></b>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
       <? $pgt_courant = isset($_SESSION['PGT']) ? $_SESSION['PGT'] : ""; ?>
        <b>PGT courant : <?=$pgt_courant?></b>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
        <input type="submit" name="BT_GET_PT" value="Récupérer un PT">
      </td>
    </tr>
    <? if($HAVE_PT) : ?>
    <tr>
      <td colspan="2" align="center">
       <? $pt_obtenu = isset($PT) ? $PT : ""; ?>
        <b>PT obtenu : <?=$PT?></b>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
        <b>PT courant : <?=$_SESSION['PT']?></b>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
        <input type="submit" name="BT_IMAP_PT" value="Ouvrir une connexion Imap avec le PT courant">
      </td>
    </tr>
	<tr>
	  <td colspan="2" align="center"><?=$CAS_UrlValidTicket;?></td>
	</tr>
    <? endif; ?>
<? endif; ?>
    <tr>
      <td colspan="2">
        <HR size="3">
        &nbsp;</td>
    </tr>
    <tr>
      <td  colspan="2" align="center"><b><font size="4">PARTIE IMAP CLASSIQUE</font></b>
      </td>
    </tr>
    <tr>
      <td  colspan="2" align="center">
        <table border="0" cellspacing="3" cellpadding="2" align="center">
          <tr>
            <td>Login</td>
            <td>
              <input type="text" name="USER_LOGIN" value=<?=$sess_user?>>
            </td>
          </tr>
          <tr>
            <td>Mot de passe</td>
            <td>
              <input type="password" name="USER_PASS" value=<?=$sess_password?>>
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
        <input type="submit" name="BT_IMAP_LOGIN" value="Ouvrir une connexion Imap avec un login mot de passe">
      </td>
    </tr>
  </table>
  </form>
</body>
</html>



