2014-09-04 17:52:18 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This software is governed by the CeCILL-B license. If a copy of this license
|
|
|
|
* is not distributed with this file, you can obtain one at
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
|
|
|
|
*
|
|
|
|
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
|
|
|
|
* Authors of Framadate/OpenSondate: Framasoft (https://github.com/framasoft)
|
|
|
|
*
|
|
|
|
* =============================
|
|
|
|
*
|
|
|
|
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
|
|
|
|
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
|
|
|
|
*
|
|
|
|
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
|
|
|
|
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
|
|
|
|
*/
|
|
|
|
namespace Framadate;
|
|
|
|
|
|
|
|
class Utils
|
|
|
|
{
|
|
|
|
public static function get_server_name()
|
|
|
|
{
|
|
|
|
$scheme = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on') ? 'https' : 'http';
|
2014-12-03 21:08:08 +01:00
|
|
|
$port = in_array($_SERVER['SERVER_PORT'], [80, 443]) ? '' : ':' . $_SERVER['SERVER_PORT'];
|
|
|
|
$dirname = dirname($_SERVER['SCRIPT_NAME']);
|
|
|
|
$dirname = $dirname === '\\' ? '/' : $dirname . '/';
|
|
|
|
$server_name = $_SERVER['SERVER_NAME'] . $port . $dirname;
|
2014-09-04 17:52:18 +02:00
|
|
|
return $scheme . '://' . str_replace('/admin','',str_replace('//','/',str_replace('///','/',$server_name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function is_error($cerr)
|
|
|
|
{
|
|
|
|
global $err;
|
|
|
|
if ($err == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($err & $cerr) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function is_user()
|
|
|
|
{
|
|
|
|
return (USE_REMOTE_USER && isset($_SERVER['REMOTE_USER'])) || isset($_SESSION['nom']);
|
|
|
|
}
|
|
|
|
|
2014-12-23 00:58:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $title
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2014-09-04 17:52:18 +02:00
|
|
|
public static function print_header($title = '')
|
|
|
|
{
|
|
|
|
global $lang;
|
|
|
|
|
|
|
|
echo '<!DOCTYPE html>
|
|
|
|
<html lang="'.$lang.'">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">';
|
|
|
|
|
|
|
|
if (! empty($title)) {
|
|
|
|
echo '<title>' . stripslashes($title) . ' - ' . NOMAPPLICATION . '</title>';
|
|
|
|
} else {
|
|
|
|
echo '<title>' . NOMAPPLICATION . '</title>';
|
|
|
|
}
|
|
|
|
|
|
|
|
echo '
|
|
|
|
<link rel="stylesheet" href="' . self::get_server_name() . 'css/bootstrap.min.css">
|
|
|
|
<link rel="stylesheet" href="' . self::get_server_name() . 'css/datepicker3.css">
|
|
|
|
<link rel="stylesheet" href="' . self::get_server_name() . 'css/style.css">
|
2014-10-21 01:31:26 +02:00
|
|
|
<link rel="stylesheet" href="' . self::get_server_name() . 'css/frama.css">
|
2014-09-04 17:52:18 +02:00
|
|
|
<link rel="stylesheet" href="' . self::get_server_name() . 'css/print.css" media="print">
|
|
|
|
<script type="text/javascript" src="' . self::get_server_name() . 'js/jquery-1.11.1.min.js"></script>
|
|
|
|
<script type="text/javascript" src="' . self::get_server_name() . 'js/bootstrap.min.js"></script>
|
|
|
|
<script type="text/javascript" src="' . self::get_server_name() . 'js/bootstrap-datepicker.js"></script>
|
|
|
|
<script type="text/javascript" src="' . self::get_server_name() . 'js/locales/bootstrap-datepicker.'.$lang.'.js"></script>
|
|
|
|
<script type="text/javascript" src="' . self::get_server_name() . 'js/core.js"></script>';
|
|
|
|
if (file_exists($_SERVER['DOCUMENT_ROOT']."/nav/nav.js")) {
|
|
|
|
echo '<script src="/nav/nav.js" id="nav_js" type="text/javascript" charset="utf-8"></script><!-- /Framanav -->';
|
|
|
|
}
|
|
|
|
|
|
|
|
echo '
|
|
|
|
</head>
|
|
|
|
<body>
|
2014-10-21 01:31:26 +02:00
|
|
|
<div class="container ombre">';
|
2014-09-04 17:52:18 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if an email address is valid using PHP filters
|
|
|
|
*
|
|
|
|
* @param string $email Email address to check
|
|
|
|
* @return bool True if valid. False if not valid.
|
2014-12-23 00:58:00 +01:00
|
|
|
* @deprecated
|
2014-09-04 17:52:18 +02:00
|
|
|
*/
|
|
|
|
public static function isValidEmail($email)
|
|
|
|
{
|
|
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fonction permettant de générer les URL pour les sondage
|
|
|
|
* @param string $id L'identifiant du sondage
|
|
|
|
* @param bool $admin True pour générer une URL pour l'administration d'un sondage, False pour un URL publique
|
|
|
|
* @return string L'url pour le sondage
|
|
|
|
*/
|
|
|
|
public static function getUrlSondage($id, $admin = false)
|
|
|
|
{
|
|
|
|
if (URL_PROPRE) {
|
|
|
|
if ($admin === true) {
|
|
|
|
$url = str_replace('/admin', '', self::get_server_name()) . $id . '/admin';
|
|
|
|
} else {
|
|
|
|
$url = str_replace('/admin', '', self::get_server_name()) . $id;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($admin === true) {
|
2014-12-12 13:43:43 +01:00
|
|
|
$url = str_replace('/admin', '', self::get_server_name()) . 'adminstuds.php?poll=' . $id;
|
2014-09-04 17:52:18 +02:00
|
|
|
} else {
|
2014-12-12 13:43:43 +01:00
|
|
|
$url = str_replace('/admin', '', self::get_server_name()) . 'studs.php?poll=' . $id;
|
2014-09-04 17:52:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2014-12-03 21:08:08 +01:00
|
|
|
/**
|
|
|
|
* Completly delete data about the given poll
|
2014-12-17 13:52:52 +01:00
|
|
|
* TODO Move this function to FramaDB
|
2014-12-03 21:08:08 +01:00
|
|
|
*/
|
2014-12-23 00:58:00 +01:00
|
|
|
public static function removeSondage($poll_id) {
|
2014-12-03 21:08:08 +01:00
|
|
|
global $connect;
|
2014-09-04 17:52:18 +02:00
|
|
|
|
2014-12-03 21:08:08 +01:00
|
|
|
$prepared = $connect->prepare('DELETE FROM sujet_studs WHERE id_sondage = ?');
|
|
|
|
$prepared->execute(array($poll_id));
|
2014-09-04 17:52:18 +02:00
|
|
|
|
2014-12-03 21:08:08 +01:00
|
|
|
$prepared = $connect->prepare('DELETE FROM user_studs WHERE id_sondage = ?');
|
|
|
|
$prepared->execute(array($poll_id));
|
2014-09-04 17:52:18 +02:00
|
|
|
|
2014-12-03 21:08:08 +01:00
|
|
|
$prepared = $connect->prepare('DELETE FROM comments WHERE id_sondage = ?');
|
|
|
|
$prepared->execute(array($poll_id));
|
2014-09-04 17:52:18 +02:00
|
|
|
|
2014-12-05 01:08:38 +01:00
|
|
|
$prepared = $connect->prepare('DELETE FROM sondage WHERE poll_id = ?');
|
2014-12-03 21:08:08 +01:00
|
|
|
$prepared->execute(array($poll_id));
|
2014-12-12 13:43:43 +01:00
|
|
|
|
2014-09-04 17:52:18 +02:00
|
|
|
}
|
|
|
|
|
2014-12-17 13:52:52 +01:00
|
|
|
/**
|
|
|
|
* Clean old poll (end_date < now).
|
2014-12-23 00:58:00 +01:00
|
|
|
* TODO Move this function to PurgePollService
|
2014-12-17 13:52:52 +01:00
|
|
|
*/
|
2014-12-03 21:08:08 +01:00
|
|
|
public static function cleaningOldPolls($log_txt) {
|
|
|
|
global $connect;
|
2014-12-12 13:43:43 +01:00
|
|
|
|
2014-12-05 01:08:38 +01:00
|
|
|
$resultSet = $connect->query('SELECT poll_id, format, admin_name FROM sondage WHERE end_date < NOW() LIMIT 20');
|
2014-12-03 21:08:08 +01:00
|
|
|
$toClean = $resultSet->fetchAll(\PDO::FETCH_CLASS);
|
|
|
|
|
|
|
|
$connect->beginTransaction();
|
|
|
|
foreach ($toClean as $row) {
|
2014-12-05 01:08:38 +01:00
|
|
|
if (self::removeSondage($row->poll_id)) {
|
|
|
|
error_log(date('H:i:s d/m/Y:') . ' EXPIRATION: '. $row->poll_id."\t".$row->format."\t".$row->admin_name."\n", 3, $log_txt);
|
2014-09-04 17:52:18 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-03 21:08:08 +01:00
|
|
|
$connect->commit();
|
2014-09-04 17:52:18 +02:00
|
|
|
}
|
2014-12-12 13:43:43 +01:00
|
|
|
|
2014-12-17 13:52:52 +01:00
|
|
|
/**
|
|
|
|
* This method pretty prints an object to the page framed by pre tags.
|
2014-12-23 00:30:05 +01:00
|
|
|
* @param mixed $object The object to print.
|
2014-12-17 13:52:52 +01:00
|
|
|
*/
|
2014-12-23 00:58:00 +01:00
|
|
|
public static function debug($object) {
|
2014-12-06 19:42:01 +01:00
|
|
|
echo '<pre>';
|
|
|
|
print_r($object);
|
|
|
|
echo '</pre>';
|
|
|
|
}
|
2014-09-04 17:52:18 +02:00
|
|
|
}
|