index.php 2.37 KB
Newer Older
stc's avatar
stc committed
1 2 3 4 5 6 7 8 9 10 11
<?php
session_start();

if (!isset($_SESSION['ticket'])) {
  /*  If session is already open for current PHP file, user is already logged in, do nothing
      If session is not open on current PHP file, check if it is open on CAS server
        If it is open on CAS server retrieve session informations
        If it is not on open on CAS server, ask for login to CAS server
  */

  include $_SERVER['DOCUMENT_ROOT'].'/apisub/lib/cas.php';
12 13
  $cas = new CAS('https://cas.utc.fr/cas/','http');
  $info = $cas->authenticate();
stc's avatar
stc committed
14 15 16 17 18 19 20
  if ($info != -1) 	{
    $_SESSION['ticket'] = $_GET['ticket'];
    $_SESSION['utclogin'] = $info['cas:user'];
    $_SESSION['mail'] = $info['cas:attributes']['cas:mail'];
    $_SESSION['surname'] = strtoupper($info['cas:attributes']['cas:sn']);
    $_SESSION['firstname'] = $info['cas:attributes']['cas:givenName'];
  }
21
  else {
22
    $cas->login();
23 24
  }
  $_SESSION['localcopy'] =  true; //call localcopy once per session
stc's avatar
stc committed
25 26
}

27
include 'lib/db.php';
28
include 'lib/views.php';
29
include 'lib/admin.php';
30

stc's avatar
stc committed
31
$db = new DB();
32
if ($_SESSION['localcopy']) {
33
  $db->copyUser($_SESSION['utclogin'], $_SESSION['surname'], $_SESSION['firstname'], $_SESSION['mail']);
34 35
  $_SESSION['localcopy']=false;
}
36 37 38 39 40
$admin = new Admin($db);
$view = new Views($admin, $_SESSION['utclogin'], $_SESSION['surname'], $_SESSION['firstname']);

$view->printHtmlBegin();
$view->printUser($db->isResp($_SESSION['utclogin']), $db->isAdmin($_SESSION['utclogin']));
41 42 43

if (isset($_GET['mode'])) {
  if ($_GET['mode']=='resp') {
44 45
    if (isset($_GET['action']) && isset($_GET['api']) && isset($_GET['login'])) {
      if ($_GET['action']=='validate') {
46
        $db->validate($_SESSION['utclogin'], $_GET['api'], $_GET['login']);
47 48
      }
    }
49
    $view->respValidation($db->apiListResp($admin, $_SESSION['utclogin']));
50 51 52 53 54
  }
  if ($_GET['mode']=='admin') {
    $view->adminFunction($_SESSION['utclogin']);
  }
}
55 56 57 58 59 60 61 62 63 64
else {
  /** Subscription and unsubscription management **/
  if (isset($_GET['api']) && $admin->isActive()) {
    if (isset($_GET['action'])) {
      if ($_GET['action']=='sub') {
        $db->subToApi($_SESSION['utclogin'], $_GET['api']);
      }
      elseif ($_GET['action']=='unsub') {
        $db->unsubToApi($_SESSION['utclogin'], $_GET['api']);
      }
stc's avatar
stc committed
65 66
    }
  }
67 68
  $view->printInstructions($db->config());
  $view->printSubList($db->subList($_SESSION['utclogin']));
69
  $view->printApiList($db->apiList($admin, $_SESSION['utclogin']));
stc's avatar
stc committed
70 71
}
?>