275 lines
9.8 KiB
PHP
275 lines
9.8 KiB
PHP
<?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
|
||
{
|
||
/**
|
||
* @return string Server name
|
||
*/
|
||
public static function get_server_name()
|
||
{
|
||
$scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
|
||
$port = in_array($_SERVER['SERVER_PORT'], [80, 443]) ? '/' : ':' . $_SERVER['SERVER_PORT'] . '/';
|
||
$server_name = $_SERVER['SERVER_NAME'] . $port . dirname($_SERVER['SCRIPT_NAME']) . '/';
|
||
|
||
return $scheme . '://' . str_replace('/admin', '', str_replace('//', '/', str_replace('///', '/', $server_name)));
|
||
}
|
||
|
||
/**
|
||
* Returns a poll or false if it fails
|
||
*/
|
||
public static function get_poll_from_id($id)
|
||
{
|
||
global $connect;
|
||
|
||
// Open database
|
||
if (preg_match(';^[\w\d]{16}$;i', $id)) {
|
||
$sql = 'SELECT sondage.*,sujet_studs.sujet FROM sondage
|
||
LEFT OUTER JOIN sujet_studs ON sondage.id_sondage = sujet_studs.id_sondage
|
||
WHERE sondage.id_sondage = ' . $connect->Param('id_sondage');
|
||
|
||
$sql = $connect->Prepare($sql);
|
||
$poll = $connect->Execute($sql, [$id]);
|
||
|
||
if ($poll === false) {
|
||
return false;
|
||
}
|
||
|
||
$psondage = $poll->FetchObject(false);
|
||
$psondage->date_fin = strtotime($psondage->date_fin);
|
||
|
||
return $psondage;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* Use get_poll_from_id that is fully english name
|
||
* @deprecated
|
||
*/
|
||
public static function get_sondage_from_id($id)
|
||
{
|
||
return self::get_poll_from_id($id);
|
||
}
|
||
|
||
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']);
|
||
}
|
||
|
||
public static function print_header($title = '')
|
||
{
|
||
global $lang;
|
||
|
||
echo '<!DOCTYPE html>
|
||
<html lang="'.$lang.'">
|
||
<head>
|
||
<meta charset="utf-8" />';
|
||
|
||
echo '<title>';
|
||
if (! empty($title)) {
|
||
echo stripslashes($title) . ' - ';
|
||
}
|
||
echo 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" />
|
||
<link rel="stylesheet" href="' . self::get_server_name() . 'css/frama.css" />
|
||
<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>
|
||
<div class="container ombre">';
|
||
}
|
||
|
||
public static function check_table_sondage()
|
||
{
|
||
global $connect;
|
||
return in_array('sondage', $connect->MetaTables('TABLES'));
|
||
}
|
||
|
||
/**
|
||
* 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.
|
||
*/
|
||
public static function isValidEmail($email)
|
||
{
|
||
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
||
}
|
||
|
||
/**
|
||
* Envoi un courrier avec un codage correct de To et Subject
|
||
* Les en-têtes complémentaires ne sont pas gérés
|
||
*/
|
||
public static function sendEmail( $to, $subject, $body, $headers='', $param='')
|
||
{
|
||
mb_internal_encoding('UTF-8');
|
||
|
||
$subject = mb_encode_mimeheader(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'), 'UTF-8', 'B', "\n", 9);
|
||
|
||
$encoded_app = mb_encode_mimeheader(NOMAPPLICATION, 'UTF-8', 'B', "\n", 6);
|
||
$size_encoded_app = (6 + strlen($encoded_app)) % 75;
|
||
$size_admin_email = strlen(ADRESSEMAILADMIN);
|
||
|
||
if (($size_encoded_app + $size_admin_email + 9) > 74 ) {
|
||
$folding = "\n";
|
||
} else {
|
||
$folding = '';
|
||
};
|
||
|
||
/*
|
||
Si $headers ne contient qu'une adresse email, on la considère comme
|
||
adresse de reply-to, sinon on met l'adresse de no-reply definie
|
||
dans constants.php
|
||
*/
|
||
if (self::isValidEmail($headers)) {
|
||
$replyTo = $headers;
|
||
$headers = ''; // on reinitialise $headers
|
||
} else {
|
||
$replyTo = ADRESSEMAILREPONSEAUTO;
|
||
}
|
||
|
||
$from = sprintf( "From: %s%s <%s>\n", $encoded_app, $folding, ADRESSEMAILADMIN);
|
||
|
||
if ($headers) {
|
||
$headers .= "\n" ;
|
||
}
|
||
|
||
$headers .= $from;
|
||
$headers .= "Reply-To: $replyTo\n";
|
||
$headers .= "MIME-Version: 1.0\n";
|
||
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
|
||
$headers .= "Content-Transfer-Encoding: 8bit\n";
|
||
$headers .= "Auto-Submitted:auto-generated\n";
|
||
$headers .= 'Return-Path: <>';
|
||
|
||
$body = html_entity_decode($body, ENT_QUOTES, 'UTF-8'). "\n--\n\n" . _('« La route est longue, mais la voie est libre… »') ."\n" . _('Framasoft ne vit que par vos dons (déductibles des impôts).') ."\n". _('Merci d’avance pour votre soutien http://soutenir.framasoft.org.');
|
||
|
||
mail($to, $subject, $body, $headers, $param);
|
||
}
|
||
|
||
/**
|
||
* Fonction vérifiant l'existance et la valeur non vide d'une clé d'un tableau
|
||
* @param string $name La clé à tester
|
||
* @param array $table Le tableau où rechercher la clé ($_POST par défaut)
|
||
* @return bool Vrai si la clé existe et renvoie une valeur non vide
|
||
*/
|
||
public static function issetAndNoEmpty($name, $table = null)
|
||
{
|
||
if (is_null($table)) {
|
||
$table = $_POST;
|
||
}
|
||
|
||
return isset($table[$name]) && ! empty($table[$name]);
|
||
}
|
||
|
||
/**
|
||
* 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) {
|
||
return str_replace('/admin', '', self::get_server_name()) . $id . '/admin';
|
||
} else {
|
||
return str_replace('/admin', '', self::get_server_name()) . $id;
|
||
}
|
||
} else {
|
||
if ($admin === true) {
|
||
return str_replace('/admin', '', self::get_server_name()) . 'adminstuds.php?sondage=' . $id;
|
||
} else {
|
||
return str_replace('/admin', '', self::get_server_name()) . 'studs.php?sondage=' . $id;
|
||
}
|
||
}
|
||
}
|
||
|
||
public static function remove_sondage($connect, $numsondage)
|
||
{
|
||
$connect->StartTrans();
|
||
|
||
$req = 'DELETE FROM sondage WHERE id_sondage = ' . $connect->Param('numsondage') ;
|
||
$sql = $connect->Prepare($req);
|
||
$connect->Execute($sql, [$numsondage]);
|
||
|
||
$req = 'DELETE FROM sujet_studs WHERE id_sondage = ' . $connect->Param('numsondage') ;
|
||
$sql = $connect->Prepare($req);
|
||
$connect->Execute($sql, [$numsondage]);
|
||
|
||
$req = 'DELETE FROM user_studs WHERE id_sondage = ' . $connect->Param('numsondage') ;
|
||
$sql = $connect->Prepare($req);
|
||
$connect->Execute($sql, [$numsondage]);
|
||
|
||
$req = 'DELETE FROM comments WHERE id_sondage = ' . $connect->Param('numsondage') ;
|
||
$sql = $connect->Prepare($req);
|
||
$connect->Execute($sql, [$numsondage]);
|
||
|
||
$suppression_OK = ! $connect->HasFailedTrans();
|
||
$connect->CompleteTrans();
|
||
|
||
return $suppression_OK ;
|
||
}
|
||
|
||
/**
|
||
* @param $connect
|
||
* @param $log_txt
|
||
* @return void
|
||
*/
|
||
public static function cleaning_polls($connect, $log_txt) {
|
||
$connect->StartTrans();
|
||
$req = 'SELECT * FROM sondage WHERE date_fin < NOW() LIMIT 20';
|
||
$sql = $connect->Prepare($req);
|
||
$cleaning = $connect->Execute($sql);
|
||
|
||
while ($dcleaning = $cleaning->FetchNextObject(false)) {
|
||
if (self::remove_sondage($connect, $dcleaning->id_sondage)) {
|
||
error_log(date('H:i:s d/m/Y:') . ' EXPIRATION: '. $dcleaning->id_sondage."\t".$dcleaning->format."\t".$dcleaning->nom_admin."\t".$dcleaning->mail_admin."\n", 3, $log_txt);
|
||
}
|
||
}
|
||
$connect->CompleteTrans();
|
||
}
|
||
}
|