2018-03-02 08:27:58 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: lucas
|
|
|
|
* Date: 27/02/18
|
|
|
|
* Time: 16:19
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace api\module;
|
|
|
|
|
|
|
|
|
|
|
|
use database\core\Repo;
|
|
|
|
use database\repo\professor;
|
2018-03-20 17:44:43 +00:00
|
|
|
use database\repo\meta;
|
2018-03-02 08:27:58 +00:00
|
|
|
use error\core\Error;
|
|
|
|
use error\core\Err;
|
|
|
|
|
|
|
|
class casController{
|
|
|
|
|
|
|
|
|
|
|
|
/* (1) Authentication callback
|
|
|
|
*
|
2018-03-20 23:41:44 +00:00
|
|
|
* @popup_mode<boolean> Whether to manage the popup
|
|
|
|
* @GET[ticket]<String> CAS callback @ticket
|
2018-03-20 22:35:16 +00:00
|
|
|
*
|
|
|
|
* @return headers|body<array> The download content
|
2018-03-02 08:27:58 +00:00
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
public static function get($args){
|
2018-03-20 23:41:44 +00:00
|
|
|
$popup_mode = false;
|
|
|
|
extract($args);
|
2018-03-02 08:27:58 +00:00
|
|
|
|
|
|
|
// login: https://sso.univ-pau.fr/cas/login?service=http://ptut.com:8080/api/v/1.0/cas
|
2018-03-03 14:50:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Communicate over popups
|
|
|
|
// ----------------------------
|
|
|
|
// window.pop = { closed: false };
|
|
|
|
//
|
|
|
|
// window.cas_callback = function(cas_login){
|
|
|
|
//
|
|
|
|
// setTimeout( function(){
|
|
|
|
//
|
|
|
|
// if( window.pop.closed )
|
|
|
|
// console.log('CAS login (null means error): '+cas_login);
|
|
|
|
//
|
|
|
|
// }, 1);
|
|
|
|
//
|
|
|
|
// };
|
|
|
|
|
|
|
|
// Launch PopUp
|
|
|
|
// window.pop = window.open('https://sso.univ-pau.fr/cas/login?service=http://ptut.com:8080/api/v/1.0/cas', '_blank', 'location=no,height=1024,width=1024,scrollbars=yes,status=no');
|
|
|
|
|
2018-03-19 18:06:00 +00:00
|
|
|
/* (0) Initialize
|
2018-03-03 14:50:11 +00:00
|
|
|
---------------------------------------------------------*/
|
2018-03-19 18:06:00 +00:00
|
|
|
/* (1) Global DOWNLOAD data */
|
2018-03-20 09:19:32 +00:00
|
|
|
$headers = ['Content-Type' => 'text/html; charset=UTF-8' ];
|
2018-03-03 14:50:11 +00:00
|
|
|
|
2018-03-20 23:41:44 +00:00
|
|
|
/* (2) If @popup_mode */
|
|
|
|
if( $popup_mode !== 0 ){
|
|
|
|
|
|
|
|
$body_start = "Veuillez patienter...<br>Vous allez être redirigés<script type='text/javascript'>( typeof window.opener.cas_callback === 'function' ) && window.opener.cas_callback(";
|
|
|
|
$body_end = "); window.close();</script>";
|
|
|
|
|
|
|
|
/* (3) Else -> redirection */
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
$homepage = ($_SERVER['SERVER_NAME'] == 'ptut.com' ) ? 'http' : 'https';
|
|
|
|
$homepage .= '://'.$_SERVER['HTTP_HOST'].'/home';
|
|
|
|
|
|
|
|
$body_start = "Veuillez patienter...<br>Vous allez être redirigés<script type='text/javascript'>console.log(";
|
|
|
|
$body_end = "); document.location = '$homepage'; </script>";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Reset SESSION */
|
2018-03-20 23:50:19 +00:00
|
|
|
// $_SESSION['CAS'] = [];
|
2018-03-19 18:06:00 +00:00
|
|
|
|
|
|
|
|
2018-03-02 08:27:58 +00:00
|
|
|
|
2018-03-03 13:57:43 +00:00
|
|
|
/* (1) Check if already connected
|
2018-03-02 08:27:58 +00:00
|
|
|
---------------------------------------------------------*/
|
2018-03-03 13:57:43 +00:00
|
|
|
/* (1) If already -> return @cas_login */
|
2018-03-03 14:50:11 +00:00
|
|
|
if( in_array('cas_user', $_SESSION['AUTH']) ){
|
|
|
|
|
|
|
|
return [
|
|
|
|
'headers' => $headers,
|
|
|
|
'body' => $body_start."'".$_SESSION['CAS']['login']."'".$body_end
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
2018-03-02 08:27:58 +00:00
|
|
|
|
|
|
|
/* (2) Fail if no ticket */
|
|
|
|
if( !isset($_GET['ticket']) || !is_string($_GET['ticket']) || strlen($_GET['ticket']) < 1 )
|
2018-03-20 09:19:32 +00:00
|
|
|
return [ 'headers' => $headers, 'body' => $body_start.'-1'.$body_end ];
|
2018-03-02 08:27:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Check ticket (validate)
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Build useful variables */
|
2018-06-09 13:08:46 +00:00
|
|
|
$service = ($_SERVER['SERVER_PORT'] == 80) ? 'http' : 'https';
|
2018-03-20 23:41:44 +00:00
|
|
|
$service .= '://'.$_SERVER['HTTP_HOST'].'/api/v/1.0/cas/'.$popup_mode;
|
2018-03-02 08:27:58 +00:00
|
|
|
$ticket = urlencode($_GET['ticket']);
|
|
|
|
$validate_url = "https://sso.univ-pau.fr/cas/serviceValidate?ticket=$ticket&service=$service";
|
|
|
|
|
|
|
|
/* (2) Configure & Prepare CURL */
|
2018-03-20 22:35:16 +00:00
|
|
|
$ch = \curl_init();
|
2018-03-02 08:27:58 +00:00
|
|
|
|
2018-03-20 22:35:16 +00:00
|
|
|
\curl_setopt($ch, CURLOPT_URL, $validate_url);
|
|
|
|
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
2018-03-02 08:27:58 +00:00
|
|
|
|
|
|
|
/* (3) Execute CURL & Close it */
|
2018-03-20 22:35:16 +00:00
|
|
|
$output = \curl_exec($ch);
|
|
|
|
\curl_close($ch);
|
2018-03-02 08:27:58 +00:00
|
|
|
|
|
|
|
/* (4) Fail if not validated */
|
|
|
|
if( strpos($output, 'user') === false )
|
2018-03-20 09:19:32 +00:00
|
|
|
return [ 'headers' => $headers, 'body' => $body_start.'-2'.$body_end ];
|
2018-03-02 08:27:58 +00:00
|
|
|
|
|
|
|
/* (5) Extract cas_login */
|
|
|
|
$cas_login = trim(strip_tags($output));
|
|
|
|
|
|
|
|
/* (6) Check empty */
|
|
|
|
if( strlen($cas_login) < 1 )
|
2018-03-20 09:19:32 +00:00
|
|
|
return [ 'headers' => $headers, 'body' => $body_start.'-2'.$body_end ];
|
2018-03-02 08:27:58 +00:00
|
|
|
|
|
|
|
|
2018-03-19 18:06:00 +00:00
|
|
|
|
|
|
|
/* (3) Meta database: check if @cas_login referenced
|
2018-03-03 13:57:43 +00:00
|
|
|
---------------------------------------------------------*/
|
2018-03-19 18:06:00 +00:00
|
|
|
/** @var professor $prof_repo */
|
|
|
|
$prof_repo = Repo::getRepo('professor');
|
2018-03-20 17:44:43 +00:00
|
|
|
/** @var meta $meta_repo */
|
|
|
|
$meta_repo = Repo::getRepo('meta');
|
2018-03-19 18:06:00 +00:00
|
|
|
|
|
|
|
/* (1) Get the list of linked departments for this @cas_login */
|
2018-03-20 17:44:43 +00:00
|
|
|
$departments = $meta_repo->get_prof_departments($cas_login);
|
2018-03-19 18:06:00 +00:00
|
|
|
|
|
|
|
/* (2) Failure: if no department for @cas_login */
|
|
|
|
if( count($departments) === 0 )
|
2018-03-20 09:19:32 +00:00
|
|
|
return [ 'headers' => $headers, 'body' => $body_start.'-3'.$body_end ];
|
2018-03-19 18:06:00 +00:00
|
|
|
|
|
|
|
/* (3) Set departments data */
|
2018-03-20 09:49:53 +00:00
|
|
|
$_SESSION['AvailableDepartments'] = $departments;
|
2018-03-19 18:06:00 +00:00
|
|
|
|
|
|
|
/* (4) Choose first department by default */
|
|
|
|
$_SESSION['CurrentDepartmentId'] = $departments[0]['idDep'];
|
2018-05-09 10:36:12 +00:00
|
|
|
$_SESSION['VERSION'] = [
|
|
|
|
'list' => $departments[0]['versions'],
|
|
|
|
'current' => null
|
|
|
|
];
|
|
|
|
|
2018-05-09 15:43:20 +00:00
|
|
|
/* (5) select version with default = 1 */
|
2018-05-09 10:36:12 +00:00
|
|
|
foreach($_SESSION['VERSION']['list'] as $v){
|
2018-05-09 15:43:20 +00:00
|
|
|
if( $v['default'] == 1 ){
|
2018-05-09 10:36:12 +00:00
|
|
|
$_SESSION['VERSION']['current'] = intval($v['iddatabase']);
|
2018-05-09 15:43:20 +00:00
|
|
|
$_SESSION['CurrentDatabase'] = $v['dbName'];
|
2018-05-09 10:36:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 18:06:00 +00:00
|
|
|
|
2018-05-09 15:43:20 +00:00
|
|
|
/* (6) if no default -> select first */
|
|
|
|
if( !is_int($_SESSION['VERSION']) ){
|
|
|
|
$_SESSION['VERSION']['current'] = intval($_SESSION['VERSION']['list'][0]['iddatabase']);
|
|
|
|
$_SESSION['CurrentDatabase'] = $_SESSION['VERSION']['list'][0]['dbName'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (7) Use this department's database */
|
2018-03-19 18:06:00 +00:00
|
|
|
Repo::switchDatabase($_SESSION['CurrentDatabase']);
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-05-09 10:36:12 +00:00
|
|
|
|
2018-03-19 18:06:00 +00:00
|
|
|
/* (4) Fetch @cas_login professor data
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Try to fetch professor */
|
|
|
|
$by_login = $prof_repo->getByLogin($cas_login);
|
|
|
|
|
|
|
|
/* (2) If not found -> reset SESSION */
|
|
|
|
if( !is_array($by_login) || !isset($by_login['idProfesseur']) || !isset($by_login['admin']) )
|
2018-03-20 09:19:32 +00:00
|
|
|
return [ 'headers' => $headers, 'body' => $body_start.'-4'.$body_end ];
|
2018-03-19 18:06:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (5) Store data in session
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Security */
|
|
|
|
\session_regenerate_id();
|
|
|
|
|
|
|
|
/* (2) Store CAS user data in SESSION */
|
2018-03-03 13:57:43 +00:00
|
|
|
$_SESSION['CAS'] = [
|
|
|
|
'login' => $cas_login,
|
2018-03-19 18:06:00 +00:00
|
|
|
'ticket' => $ticket,
|
|
|
|
'id' => (int) $by_login['idProfesseur'],
|
|
|
|
'admin' => (bool) $by_login['admin']
|
2018-03-03 13:57:43 +00:00
|
|
|
];
|
|
|
|
|
2018-03-19 18:06:00 +00:00
|
|
|
/* (3) Success CAS login */
|
2018-03-03 14:50:11 +00:00
|
|
|
return [
|
|
|
|
'headers' => $headers,
|
|
|
|
'body' => $body_start."'".$_SESSION['CAS']['login']."'".$body_end
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-20 22:35:16 +00:00
|
|
|
/* (2) Logout from CAS server
|
|
|
|
*
|
|
|
|
* @return logged_out<bool> Whether you have been logged out
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
public function put(){
|
2018-03-03 14:50:11 +00:00
|
|
|
|
2018-03-20 22:35:16 +00:00
|
|
|
/* (1) Call logout script
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Build useful variables */
|
2018-03-20 22:51:39 +00:00
|
|
|
$logout_url = "https://sso.univ-pau.fr/cas/logout";
|
2018-03-20 22:35:16 +00:00
|
|
|
|
|
|
|
/* (2) Configure & Prepare CURL */
|
|
|
|
$ch = \curl_init();
|
|
|
|
|
|
|
|
\curl_setopt($ch, CURLOPT_URL, $logout_url);
|
2018-03-20 22:51:39 +00:00
|
|
|
\curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
|
2018-03-20 22:35:16 +00:00
|
|
|
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
2018-03-03 14:50:11 +00:00
|
|
|
|
2018-03-20 22:35:16 +00:00
|
|
|
/* (3) Execute CURL & Close it */
|
|
|
|
$output = \curl_exec($ch);
|
|
|
|
\curl_close($ch);
|
|
|
|
|
|
|
|
/* (4) Error if no output */
|
|
|
|
if( strlen($output) < 1 )
|
|
|
|
return ['logged_out' => false, 'redirect_url' => $logout_url];
|
|
|
|
|
|
|
|
/* (4) Destroy session */
|
|
|
|
\session_destroy();
|
|
|
|
|
|
|
|
/* (5) Return if logged out */
|
|
|
|
return ['logged_out' => true, 'redirect_url' => $logout_url];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Logout (not from CAS server)
|
|
|
|
*
|
|
|
|
* @return logged_out<bool> Whether you have been logged out
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-03-03 14:50:11 +00:00
|
|
|
public function delete(){
|
|
|
|
|
2018-03-15 12:13:35 +00:00
|
|
|
\session_destroy();
|
2018-03-03 14:50:11 +00:00
|
|
|
|
|
|
|
/* (3) Return if logged out */
|
2018-03-15 12:13:35 +00:00
|
|
|
return ['logged_out' => true];
|
2018-03-02 08:27:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-09 13:08:46 +00:00
|
|
|
}
|