plateforme-corres2math/server_files/config.php

228 lines
6.2 KiB
PHP

<?php
/**
* Config options
*/
$YEAR = getenv("CORRES2MATH_YEAR");
$URL_BASE = getenv("CORRES2MATH_URL_BASE");
$LOCAL_PATH = getenv("CORRES2MATH_LOCAL_PATH");
$MAIL_DOMAIN = getenv("CORRES2MATH_MAIL_DOMAIN");
/**
* DB infos
*/
$DB_HOST = getenv("CORRES2MATH_DB_HOST");
$DB_NAME = getenv("CORRES2MATH_DB_NAME");
$DB_USER = getenv("CORRES2MATH_DB_USER");
$DB_PASSWORD = getenv("CORRES2MATH_DB_PASSWORD");
try {
$DB = new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=utf8", "$DB_USER", "$DB_PASSWORD", array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch (Exception $ex) {
die("Erreur lors de la connexion à la base de données : " . $ex->getMessage());
}
$CONFIG = new Config();
$CONFIG->initDB();
$CONFIG->loadConfigValues();
class Config
{
private $inscription_date;
private $start_phase1_date;
private $end_phase1_date;
private $start_phase2_date;
private $end_phase2_date;
private $start_phase3_date;
private $end_phase3_date;
private $start_phase4_date;
private $end_phase4_date;
private $index_page;
private $views;
public function initDB()
{
global $DB, $LOCAL_PATH, $YEAR;
$index_template_page = htmlspecialchars(file_get_contents($LOCAL_PATH . "/server_files/views/index.html"));
$DB->exec("SET GLOBAL time_zone = 'Europe/Paris';");
$DB->prepare("INSERT IGNORE INTO `config`(`key`, `value`)
VALUES ('inscription_date_$YEAR', CURRENT_TIMESTAMP + INTERVAL 2 DAY),
('start_phase1_date_$YEAR', CURRENT_TIMESTAMP + INTERVAL 1 DAY),
('end_phase1_date_$YEAR', CURRENT_TIMESTAMP + INTERVAL 3 DAY),
('start_phase2_date_$YEAR', CURRENT_TIMESTAMP + INTERVAL 4 DAY),
('end_phase2_date_$YEAR', CURRENT_TIMESTAMP + INTERVAL 5 DAY),
('start_phase3_date_$YEAR', CURRENT_TIMESTAMP + INTERVAL 6 DAY),
('end_phase3_date_$YEAR', CURRENT_TIMESTAMP + INTERVAL 7 DAY),
('start_phase4_date_$YEAR', CURRENT_TIMESTAMP + INTERVAL 8 DAY),
('end_phase4_date_$YEAR', CURRENT_TIMESTAMP + INTERVAL 9 DAY),
('index_page_$YEAR', ?),
('views_$YEAR', 0);")->execute([$index_template_page]);
}
public function loadConfigValues()
{
global $DB, $YEAR;
$req = $DB->query("SELECT * FROM `config` WHERE `key` REGEXP '$YEAR';");
while (($data = $req->fetch()) !== false) {
$key = substr($data["key"], 0, -5);
$this->$key = $data["value"];
}
}
public function getInscriptionDate()
{
return $this->inscription_date;
}
public function setInscriptionDate($inscription_date)
{
global $DB, $YEAR;
$DB->exec("UPDATE `config` SET `value` = '$inscription_date' WHERE `key` = 'inscription_date_$YEAR'");
$this->inscription_date = $inscription_date;
}
public function getStartPhase1Date()
{
return $this->start_phase1_date;
}
public function setStartPhase1Date($start_phase1_date)
{
global $DB, $YEAR;
$DB->exec("UPDATE `config` SET `value` = '$start_phase1_date' WHERE `key` = 'start_phase1_date_$YEAR'");
$this->start_phase1_date = $start_phase1_date;
}
public function getEndPhase1Date()
{
return $this->end_phase1_date;
}
public function setEndPhase1Date($end_phase1_date)
{
global $DB, $YEAR;
$DB->exec("UPDATE `config` SET `value` = '$end_phase1_date' WHERE `key` = 'end_phase1_date_$YEAR'");
$this->end_phase1_date = $end_phase1_date;
}
public function getStartPhase2Date()
{
return $this->start_phase2_date;
}
public function setStartPhase2Date($start_phase2_date)
{
global $DB, $YEAR;
$DB->exec("UPDATE `config` SET `value` = '$start_phase2_date' WHERE `key` = 'start_phase2_date_$YEAR'");
$this->start_phase2_date = $start_phase2_date;
}
public function getEndPhase2Date()
{
return $this->end_phase2_date;
}
public function setEndPhase2Date($end_phase2_date)
{
global $DB, $YEAR;
$DB->exec("UPDATE `config` SET `value` = '$end_phase2_date' WHERE `key` = 'end_phase2_date_$YEAR'");
$this->end_phase2_date = $end_phase2_date;
}
public function getStartPhase3Date()
{
return $this->start_phase3_date;
}
public function setStartPhase3Date($start_phase3_date)
{
global $DB, $YEAR;
$DB->exec("UPDATE `config` SET `value` = '$start_phase3_date' WHERE `key` = 'start_phase3_date_$YEAR'");
$this->start_phase3_date = $start_phase3_date;
}
public function getEndPhase3Date()
{
return $this->end_phase3_date;
}
public function setEndPhase3Date($end_phase3_date)
{
global $DB, $YEAR;
$DB->exec("UPDATE `config` SET `value` = '$end_phase3_date' WHERE `key` = 'end_phase3_date_$YEAR'");
$this->end_phase3_date = $end_phase3_date;
}
public function getStartPhase4Date()
{
return $this->start_phase4_date;
}
public function setStartPhase4Date($start_phase4_date)
{
global $DB, $YEAR;
$DB->exec("UPDATE `config` SET `value` = '$start_phase4_date' WHERE `key` = 'start_phase4_date_$YEAR'");
$this->start_phase4_date = $start_phase4_date;
}
public function getEndPhase4Date()
{
return $this->end_phase4_date;
}
public function setEndPhase4Date($end_phase4_date)
{
global $DB, $YEAR;
$DB->exec("UPDATE `config` SET `value` = '$end_phase4_date' WHERE `key` = 'end_phase4_date_$YEAR'");
$this->end_phase4_date = $end_phase4_date;
}
public function getIndexPage()
{
return $this->index_page;
}
public function setIndexPage($index_page)
{
global $DB, $YEAR;
$DB->prepare("UPDATE `config` SET `value` = ? WHERE `key` = 'index_page_$YEAR'")->execute([$index_page]);
$this->index_page = $index_page;
}
public function getViews()
{
return $this->views;
}
public function incrViews()
{
global $DB, $YEAR;
if (isset($_SESSION["user_id"]) && $_SESSION["role"] == Role::ADMIN || isset($_SESSION["admin"]))
return;
$DB->exec("UPDATE `config` SET `value` = " . ($this->views + 1) . " WHERE `key` = 'views_$YEAR';");
$this->views++;
}
}
session_start();
setlocale(LC_ALL, "fr_FR.utf8");
date_default_timezone_set("Europe/Paris");