Commit 2a5ba83c authored by stc's avatar stc

Project init

parents
connexion.php
index.html
TODO
<?php
session_start();
if (!isset($_SESSION['login'])) {
/* 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';
$info = CAS::authenticate();
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'];
}
else
CAS::login();
}
echo $_SESSION['ticket']." ".$_SESSION['utclogin']
?>
<?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';
$cas = new CAS('https://cas.utc.fr/cas/','https');
$info = CAS::authenticate();
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'];
}
else
CAS::login();
}
include $_SERVER['DOCUMENT_ROOT'].'/apisub/lib/db.php';
$db = new DB();
$db->printHtmlBegin();
$db->printUser($_SESSION['utclogin'], $_SESSION['surname'], $_SESSION['firstname']);
if (isset($_GET['api']) && isset($_GET['utclogin'])) {
if (isset($_GET['action'])) {
if ($_GET['action']=='sub') {
$db->subToApi($_GET['utclogin'], $_GET['api']);
}
elseif ($_GET['action']=='unsub') {
$db->unsubToApi($_GET['utclogin'], $_GET['api']);
}
}
}
$db->subList($_SESSION['utclogin']);
$db->apiList($_SESSION['utclogin'], 'H', 2019);
?>
<?php
include "xmltoarray.php";
class CAS {
private $server;
private $protocol;
function __construct ($server='https://cas.utc.fr/cas/', $protocol='https') {
$this->server = $server;
$this->protocol = $protocol;
}
public static function authenticate() {
if (!isset($_GET['ticket']) || empty($_GET['ticket']))
return -1;
$data = file_get_contents($this->server.'serviceValidate?service='.$this->protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
//'&ticket='.$_GET['ticket'] inutile?
if (empty($data))
return -1;
$parsed = new xmlToArrayParser($data);
if (!isset($parsed->array['cas:serviceResponse']['cas:authenticationSuccess']))
return -1;
return $parsed->array['cas:serviceResponse']['cas:authenticationSuccess'];
}
public static function login() {
header('Location: '.$this->server.'login?service='.$this->protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}
public static function logout() {
header('Location: '.$this->server.'logout?service='.$this->protocol.'://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']));//ou SCRIPT_NAME?
// On n'utilise pas REQUEST_URI sinon cela déconnecterait à l'infini.
}
}
<?php
$db_user='apisub';
$db_pass='password';
?>
<?php
class DB {
private $conn;
function __construct () {
include 'connexion.php';
try {
$this->conn = new PDO('pgsql:host=localhost;port=5432;dbname=apisub', $db_user, $db_pass);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
}
public static function printHtmlBegin () {
echo '<html>';
echo '<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head>';
echo '<title>Api Sub</title>';
echo '<link href="css/main.css" type="text/css" rel="stylesheet"/>';
echo '<body>';
echo '<h1>Inscriptions Api</h1>';
}
function printUser($utclogin, $surname, $firstname) {
echo '<p><i>'.$firstname.' '. $surname.'</i> (<b>'.$utclogin.'</b>)</p>';
}
function subList($utclogin) {
$sql = "SELECT code, name, year, semester, TO_CHAR(subdate,'DD/MM/YYYY') as subdate
FROM api JOIN subscribe ON api.code=subscribe.api
WHERE student='".$utclogin."'";
$st = $this->conn->prepare($sql);
$st->execute();
$res = $st->fetchAll(PDO::FETCH_ASSOC);
$st = $this->conn->prepare($sql);
$st->execute();
$res = $st->fetchAll(PDO::FETCH_ASSOC);
if ($res) {
echo '<h2>Vos inscriptions</h2>';
foreach ($res as $row) {
echo '<p>';
echo ' <a href="index.php?action=unsub&api='.$row['code'].'&utclogin='.$utclogin.'">[désinscription]</a> ';
echo $row['code']."-".$row['name']." (".$row['semester'].$row['year'].") [le ".$row['subdate']."]";
echo '</p>';
}
}
else {
echo '<h2>Aucune inscription</h2>';
}
}
function apiList($utclogin, $semester, $year) {
$sql = "SELECT code, name
FROM api
WHERE semester='".$semester."' AND year=".$year."
ORDER BY code, name";
$st = $this->conn->prepare($sql);
$st->execute();
$res = $st->fetchAll(PDO::FETCH_ASSOC);
$st = $this->conn->prepare($sql);
$st->execute();
$res = $st->fetchAll(PDO::FETCH_ASSOC);
if ($res) {
echo '<h2>Liste des Api '.$semester.$year.'</h2>';
foreach ($res as $row) {
echo '<p>';
echo ' <a href="index.php?action=sub&api='.$row['code'].'&utclogin='.$utclogin.'">[inscription]</a> ';
echo $row['code']."-".$row['name'];
echo '</p>';
}
}
else {
echo '<h2>Aucune inscription</h2>';
}
}
function subToApi($utclogin, $api) {
$today = date("Ymd");
$sql = "INSERT INTO subscribe VALUES ('".$utclogin."',".$api.",TO_DATE('".$today."','YYYYMMDD'))";
$res=$this->conn->exec($sql);
}
function unsubToApi($utclogin, $api) {
$sql = "DELETE FROM subscribe WHERE student='".$utclogin."' AND api=".$api;
$res=$this->conn->exec($sql);
}
}
<?php
class xmlToArrayParser {
/** The array created by the parser can be assigned to any variable: $anyVarArr = $domObj->array.*/
public $array = array();
public $parse_error = false;
private $parser;
private $pointer;
/** Constructor: $domObj = new xmlToArrayParser($xml); */
public function __construct($xml) {
$this->pointer =& $this->array;
$this->parser = xml_parser_create("UTF-8");
xml_set_object($this->parser, $this);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($this->parser, "tag_open", "tag_close");
xml_set_character_data_handler($this->parser, "cdata");
$this->parse_error = xml_parse($this->parser, ltrim($xml))? false : true;
}
/** Free the parser. */
public function __destruct() { xml_parser_free($this->parser);}
/** Get the xml error if an an error in the xml file occured during parsing. */
public function get_xml_error() {
if($this->parse_error) {
$errCode = xml_get_error_code ($this->parser);
$thisError = "Error Code [". $errCode ."] \"<strong style='color:red;'>" . xml_error_string($errCode)."</strong>\",
at char ".xml_get_current_column_number($this->parser) . "
on line ".xml_get_current_line_number($this->parser)."";
}else $thisError = $this->parse_error;
return $thisError;
}
private function tag_open($parser, $tag, $attributes) {
$this->convert_to_array($tag, 'attrib');
$idx=$this->convert_to_array($tag, 'cdata');
if(isset($idx)) {
$this->pointer[$tag][$idx] = Array('@idx' => $idx,'@parent' => &$this->pointer);
$this->pointer =& $this->pointer[$tag][$idx];
}else {
$this->pointer[$tag] = Array('@parent' => &$this->pointer);
$this->pointer =& $this->pointer[$tag];
}
if (!empty($attributes)) { $this->pointer['attrib'] = $attributes; }
}
/** Adds the current elements content to the current pointer[cdata] array. */
private function cdata($parser, $cdata) { $this->pointer['cdata'] = trim($cdata); }
private function tag_close($parser, $tag) {
$current = & $this->pointer;
if(isset($this->pointer['@idx'])) {unset($current['@idx']);}
$this->pointer = & $this->pointer['@parent'];
unset($current['@parent']);
if(isset($current['cdata']) && count($current) == 1) { $current = $current['cdata'];}
else if(empty($current['cdata'])) {unset($current['cdata']);}
}
/** Converts a single element item into array(element[0]) if a second element of the same name is encountered. */
private function convert_to_array($tag, $item) {
if(isset($this->pointer[$tag][$item])) {
$content = $this->pointer[$tag];
$this->pointer[$tag] = array((0) => $content);
$idx = 1;
}else if (isset($this->pointer[$tag])) {
$idx = count($this->pointer[$tag]);
if(!isset($this->pointer[$tag][0])) {
foreach ($this->pointer[$tag] as $key => $value) {
unset($this->pointer[$tag][$key]);
$this->pointer[$tag][0][$key] = $value;
}}}else $idx = null;
return $idx;
}
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment