2016-04-10 14:40:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace manager\repo;
|
|
|
|
use \manager\Database;
|
|
|
|
|
|
|
|
class token{
|
|
|
|
|
|
|
|
/* Gestion de la table Token (pour secu) et des derivations
|
|
|
|
*
|
|
|
|
* 1. Gestion des access
|
|
|
|
* - getById(id_token)
|
|
|
|
* - getAll()
|
|
|
|
*
|
|
|
|
* 2. Gestion de la verification
|
|
|
|
* - check(token, id_personne)
|
|
|
|
*
|
|
|
|
* 3. Gestion de la
|
|
|
|
* - merge(idA, idB)
|
|
|
|
*
|
|
|
|
* 4. Gestion de creation
|
|
|
|
* - create(pseudo, nom, prenom, facebook, telephone)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE LA LISTE DE TOUS LES TOKENS
|
|
|
|
*
|
|
|
|
* @return tokens<Array> Liste de tous les tokens
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getAll(){
|
|
|
|
/* [1] On prepare et execute la requete
|
|
|
|
=========================================================*/
|
2016-04-10 16:50:07 +00:00
|
|
|
$request = Database::getPDO()->query("SELECT id_token as id, name, token, expires
|
2016-04-10 14:40:49 +00:00
|
|
|
FROM api_token
|
2016-04-10 16:50:07 +00:00
|
|
|
ORDER BY expires DESC");
|
2016-04-10 14:40:49 +00:00
|
|
|
|
2016-04-10 16:50:07 +00:00
|
|
|
/* [2] On recupere les donnees et ajout le status d'expire ou non
|
2016-04-10 14:40:49 +00:00
|
|
|
=========================================================*/
|
2016-04-10 16:50:07 +00:00
|
|
|
$data = Database::delNumeric( $request->fetchAll() );
|
|
|
|
|
|
|
|
// Si le token est expire
|
|
|
|
foreach($data as $i=>$token)
|
|
|
|
$data[$i]['expired'] = time() > strtotime($token['expires']);
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On retourne le resultat
|
|
|
|
=========================================================*/
|
|
|
|
return $data;
|
2016-04-10 14:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-10 15:40:09 +00:00
|
|
|
/* VERIFIE SI UN TOKEN EST VALIDE
|
|
|
|
*
|
|
|
|
* @token<String> Token en question
|
|
|
|
*
|
|
|
|
* @return valid<bool> Retourne si VRAI ou FAUX le token est valid
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function check($token){
|
|
|
|
/* [0] Verification des INPUT
|
|
|
|
=========================================================*/
|
|
|
|
// si le format est incorrect, on retourne FAUX
|
|
|
|
if( !Database::check('sha1', $token) ) return false;
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] Verification dans la base de donnees
|
|
|
|
=========================================================*/
|
|
|
|
$check = Database::getPDO()->prepare("SELECT id_token
|
|
|
|
FROM api_token
|
2016-04-10 16:50:07 +00:00
|
|
|
WHERE CURDATE() < expires
|
2016-04-10 15:40:09 +00:00
|
|
|
AND token = :token");
|
|
|
|
$check->execute(array( ':token' => $token ));
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On retourne le resultat
|
|
|
|
=========================================================*/
|
|
|
|
return $check->fetch() !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-10 14:40:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|