date.chapril.org-framadate/fonctions.php

263 lines
8.3 KiB
PHP
Raw Normal View History

2011-05-15 01:32:47 +02:00
<?php
//==========================================================================
//
//Université de Strasbourg - Direction Informatique
//Auteur : Guilhem BORGHESI
//Création : Février 2008
//
//borghesi@unistra.fr
//
//Ce logiciel est régi par la licence CeCILL-B soumise au droit français et
//respectant les principes de diffusion des logiciels libres. Vous pouvez
//utiliser, modifier et/ou redistribuer ce programme sous les conditions
//de la licence CeCILL-B telle que diffusée par le CEA, le CNRS et l'INRIA
//sur le site "http://www.cecill.info".
//
//Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
//pris connaissance de la licence CeCILL-B, et que vous en avez accepté les
//termes. Vous pouvez trouver une copie de la licence dans le fichier LICENCE.
//
//==========================================================================
//
//Université de Strasbourg - Direction Informatique
//Author : Guilhem BORGHESI
//Creation : Feb 2008
//
//borghesi@unistra.fr
//
//This software is governed by the CeCILL-B license under French law and
//abiding by the rules of distribution of free software. You can use,
//modify and/ or redistribute the software under the terms of the CeCILL-B
//license as circulated by CEA, CNRS and INRIA at the following URL
//"http://www.cecill.info".
//
//The fact that you are presently reading this means that you have had
//knowledge of the CeCILL-B license and that you accept its terms. You can
//find a copy of this license in the file LICENSE.
//
//==========================================================================
2011-05-15 03:56:54 +02:00
if(ini_get('date.timezone') == '') {
2011-05-15 01:32:47 +02:00
date_default_timezone_set("Europe/Paris");
2011-05-15 03:56:54 +02:00
}
2011-05-15 01:32:47 +02:00
include_once('variables.php');
include_once('i18n.php');
require_once('adodb/adodb.inc.php');
2011-05-15 03:56:54 +02:00
function connexion_base()
{
$DB = NewADOConnection(BASE_TYPE);
$DB->Connect(SERVEURBASE, USERBASE, USERPASSWD, BASE);
//$DB->debug = true;
return $DB;
2011-05-15 01:32:47 +02:00
}
2011-05-15 03:56:54 +02:00
function get_server_name()
{
2011-05-15 01:32:47 +02:00
$scheme = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? 'https' : 'http';
$get = explode('/', dirname($_SERVER["SCRIPT_NAME"]));
$folder = explode('/', str_replace('\\', '/', dirname(__FILE__)));
$communs = array_intersect($get, $folder);
$base = implode('/', $communs);
$url = sprintf("%s://%s%s", $scheme, STUDS_URL, $base);
2011-05-15 03:56:54 +02:00
if (!preg_match("|/$|", $url)) {
$url = $url."/";
}
return $url;
2011-05-15 01:32:47 +02:00
}
2011-05-15 03:56:54 +02:00
function get_sondage_from_id($id)
{
2011-05-15 01:32:47 +02:00
global $connect;
2011-05-15 03:56:54 +02:00
2011-05-15 01:32:47 +02:00
// Ouverture de la base de données
if(preg_match(";^[\w\d]{16}$;i",$id)) {
$sql = 'SELECT sondage.*,sujet_studs.sujet FROM sondage
2011-05-15 03:56:54 +02:00
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);
$sondage=$connect->Execute($sql, array($id));
if ($sondage === false) {
return false;
}
2011-05-15 01:32:47 +02:00
$psondage = $sondage->FetchObject(false);
$psondage->date_fin = strtotime($psondage->date_fin);
return $psondage;
}
2011-05-15 03:56:54 +02:00
2011-05-15 01:32:47 +02:00
return false;
}
2011-05-15 03:56:54 +02:00
function is_error($cerr)
{
2011-05-15 01:32:47 +02:00
global $err;
2011-05-15 03:56:54 +02:00
if ( $err == 0 ) {
2011-05-15 01:32:47 +02:00
return false;
2011-05-15 03:56:54 +02:00
}
2011-05-15 01:32:47 +02:00
return (($err & $cerr) != 0 );
}
2011-05-15 03:56:54 +02:00
function is_user()
{
2011-05-15 01:32:47 +02:00
return isset($_SERVER['REMOTE_USER']) || (isset($_SESSION['nom']));
}
2011-05-15 03:56:54 +02:00
function print_header($js = false, $nom_sondage = '')
2011-05-15 03:56:54 +02:00
{
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
2011-05-15 01:32:47 +02:00
<html>
2011-05-15 03:56:54 +02:00
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
if (empty($nom_sondage) === false) {
echo '
<title>'.stripslashes($nom_sondage).' - '.NOMAPPLICATION.'</title>';
} else {
echo '
<title>'.NOMAPPLICATION.'</title>';
}
echo '
<link rel="stylesheet" type="text/css" href="'.get_server_name().'style.css">
<link rel="stylesheet" type="text/css" href="'.get_server_name().'print.css" media="print">';
2011-05-15 03:56:54 +02:00
echo '</head>';
2011-05-15 01:32:47 +02:00
}
2011-05-15 03:56:54 +02:00
function check_table_sondage()
{
2011-05-15 01:32:47 +02:00
global $connect;
$tables = $connect->MetaTables('TABLES');
2011-05-15 03:56:54 +02:00
if (in_array("sondage", $tables)) {
2011-05-15 01:32:47 +02:00
return true;
2011-05-15 03:56:54 +02:00
}
2011-05-15 01:32:47 +02:00
return false;
}
2011-05-15 01:47:36 +02:00
/**
* Vérifie une adresse e-mail selon les normes RFC
2011-05-15 03:56:54 +02:00
* @param string $email l'adresse e-mail a vérifier
* @return bool vrai si l'adresse est correcte, faux sinon
2011-05-15 01:47:36 +02:00
* @see http://fightingforalostcause.net/misc/2006/compare-email-regex.php
* @see http://svn.php.net/viewvc/php/php-src/trunk/ext/filter/logical_filters.c?view=markup
*/
2011-05-15 03:56:54 +02:00
function validateEmail($email)
{
2011-05-15 01:47:36 +02:00
$pattern = '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD';
return (bool)preg_match($pattern, $email);
}
# Envoi un courrier avec un codage correct de To et Subject
# Les en-têtes complémentaires ne sont pas gérés
# Inspiré de http://geoland.org/2007/12/utf8-ready-php-mail-function/
function sendEmail( $to, $subject, $body, $headers, $param)
{
$to_list = explode( ',', $to ) ;
$to = "" ;
$first = 1 ;
foreach ( $to_list as $one ) {
if ( $first == 0 ) $to .= ',' ;
2012-01-09 01:55:07 +01:00
if ( preg_match( "/</", $one ) ) {
$to_cut = explode( '<' ,$one ) ;
$to .= '=?UTF-8?B?' . base64_encode( $to_cut[ 0 ] ) . '?= <' . $to_cut[ 1 ] ;
} else {
$to .= $one ;
}
$first = 0 ;
} ;
$subject = '=?UTF-8?B?' . base64_encode( $subject ) . '?=' ;
mail( $to, $subject, $body, $headers, $param ) ;
}
2012-01-09 02:18:25 +01:00
function makeHeaders() {
$headers_str = <<<EOF
From: %s <%s>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
EOF;
$encoded_app = '=?UTF-8?B?' . base64_encode( NOMAPPLICATION, ) . '?=' ;
$headers = sprintf( $headers_str, $encoded_app, ADRESSEMAILADMIN ) ;
return $headers ;
}
2011-05-15 03:56:54 +02:00
2011-05-15 04:49:16 +02:00
/**
* Fonction vérifiant l'existance et la valeur non vide d'une clé d'un tableau
* @param string $name La clé à tester
* @param array $tableau Le tableau rechercher la clé ($_POST par défaut)
* @return bool Vrai si la clé existe et renvoie une valeur non vide
*/
function issetAndNoEmpty($name, $tableau = null)
{
if ($tableau === null) {
$tableau = $_POST;
}
return (isset($tableau[$name]) === true && empty($tableau[$name]) === false);
}
/**
* 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
*/
function getUrlSondage($id, $admin = false)
{
if (URL_PROPRE === true) {
if ($admin === true) {
$url = get_server_name().$id.'/admin';
} else {
$url = get_server_name().$id;
}
} else {
if ($admin === true) {
$url = get_server_name().'adminstuds.php?sondage='.$id;
} else {
$url = get_server_name().'studs.php?sondage='.$id;
}
}
return $url;
}
2011-05-15 03:56:54 +02:00
$connect=connexion_base();
define('COMMENT_EMPTY', 0x0000000001);
define('COMMENT_USER_EMPTY', 0x0000000010);
define('COMMENT_INSERT_FAILED', 0x0000000100);
define('NAME_EMPTY', 0x0000001000);
define('NAME_TAKEN', 0x0000010000);
define('NO_POLL', 0x0000100000);
define('NO_POLL_ID', 0x0001000000);
define('INVALID_EMAIL', 0x0010000000);
define('TITLE_EMPTY', 0x0100000000);
define('INVALID_DATE', 0x1000000000);
$err = 0;