217 lines
5.1 KiB
PHP
217 lines
5.1 KiB
PHP
<?php
|
|
|
|
class DescripteurChamp {
|
|
|
|
const ACTION_OBLIGATOIRE = 'o';
|
|
const ACTION_FACULTATIF = 'f';
|
|
const ACTION_IGNORER = 'i';
|
|
|
|
const TYPE_STRING = 'string';
|
|
const TYPE_INT = 'int';
|
|
const TYPE_FLOAT = 'float';
|
|
const TYPE_EMAIL = 'email';
|
|
const TYPE_FILE = 'file';
|
|
const TYPE_CAPTCHA = 'captcha';
|
|
|
|
const CAPTCHA_URL_FORMAT = '%s/client/plugins/formesmagiques/captcha.php?sid=%s';
|
|
|
|
public $formvar;
|
|
public $var;
|
|
public $type;
|
|
public $action;
|
|
public $label;
|
|
|
|
public $tableau = false;
|
|
|
|
public $erreur = false;
|
|
public $valeur = "";
|
|
|
|
public function __construct($textdesc = false) {
|
|
|
|
if ($textdesc) {
|
|
$data = explode('|', $textdesc);
|
|
|
|
$formvar = $data[0];
|
|
$var = "#" . strtoupper($formvar);
|
|
|
|
if (isset($data[1])) {
|
|
|
|
switch($data[1]) {
|
|
case 'o' :
|
|
$action = self::ACTION_OBLIGATOIRE;
|
|
break;
|
|
|
|
case 'f' :
|
|
$action = self::ACTION_FACULTATIF;
|
|
break;
|
|
|
|
case 'i' :
|
|
$action = self::ACTION_IGNORER;
|
|
break;
|
|
|
|
case '' :
|
|
$action = self::ACTION_FACULTATIF;
|
|
break;
|
|
|
|
default :
|
|
die("Formesmagiques Action '".$data[1]."' sur le champ '$formvar' invalide. Valeurs valides: o, f, i");
|
|
}
|
|
}
|
|
|
|
$type = self::TYPE_STRING;
|
|
|
|
if (isset($data[2])) {
|
|
|
|
switch($data[2]) {
|
|
case 'chaine' :
|
|
$type = self::TYPE_STRING;
|
|
break;
|
|
|
|
case 'entier' :
|
|
$type = self::TYPE_INT;
|
|
break;
|
|
|
|
case 'reel' :
|
|
$type = self::TYPE_FLOAT;
|
|
break;
|
|
|
|
case 'email' :
|
|
$type = self::TYPE_EMAIL;
|
|
break;
|
|
|
|
case 'fichier' :
|
|
$type = self::TYPE_FILE;
|
|
break;
|
|
|
|
case 'captcha' :
|
|
$type = self::TYPE_CAPTCHA;
|
|
break;
|
|
|
|
case '' :
|
|
$type = self::TYPE_STRING;
|
|
break;
|
|
|
|
default :
|
|
die("Formesmagiques Type '".$data[2]."' sur le champ '$formvar' invalide. Valeurs valides: chaine, entier, reel, email, fichier, captcha");
|
|
}
|
|
}
|
|
|
|
$label = isset($data[3]) ? $data[3] : ucfirst($formvar);
|
|
|
|
$valeur = isset($data[4]) ? $data[4] : '';
|
|
|
|
$this->creer($formvar, $var, $type, $action, false, $label, $valeur);
|
|
}
|
|
}
|
|
|
|
public function creer($formvar, $var, $type, $action, $tableau, $label, $valeur)
|
|
{
|
|
$this->formvar = $formvar;
|
|
$this->var =$var;
|
|
$this->type =$type;
|
|
$this->action = $action;
|
|
$this->tableau = $tableau;
|
|
$this->label = $label;
|
|
$this->valeur = $valeur;
|
|
}
|
|
|
|
public function copie($champ)
|
|
{
|
|
$this->formvar = $champ->formvar;
|
|
$this->var =$champ->var;
|
|
$this->type =$champ->type;
|
|
$this->action = $champ->action;
|
|
$this->tableau = $champ->tableau;
|
|
$this->label = $champ->label;
|
|
$this->valeur = $champ->valeur;
|
|
}
|
|
|
|
public function controler($text) {
|
|
if ($this->action == self::ACTION_OBLIGATOIRE) {
|
|
switch($this->type) {
|
|
case DescripteurChamp::TYPE_STRING:
|
|
$text = filter_var($text, FILTER_SANITIZE_STRING);
|
|
if ($text == '') $text = false;
|
|
break;
|
|
|
|
case DescripteurChamp::TYPE_INT:
|
|
$text = filter_var($text, FILTER_VALIDATE_INT);
|
|
break;
|
|
|
|
case DescripteurChamp::TYPE_FLOAT:
|
|
$text = filter_var($text, FILTER_VALIDATE_FLOAT);
|
|
break;
|
|
|
|
case DescripteurChamp::TYPE_EMAIL:
|
|
|
|
$text = filter_var($text, FILTER_VALIDATE_EMAIL);
|
|
break;
|
|
|
|
case DescripteurChamp::TYPE_CAPTCHA:
|
|
$text = filter_var($text, FILTER_SANITIZE_STRING);
|
|
|
|
// Vérifier que le captcha est correct
|
|
if (md5(strtoupper(trim($text))) != $_SESSION['formesmagiques-captcha'][$this->formvar]) $text = false;
|
|
break;
|
|
}
|
|
|
|
if ($text === false) $this->erreur = true;
|
|
}
|
|
|
|
$text = trim($text);
|
|
|
|
$valeur = get_magic_quotes_gpc() ? stripslashes($text) : $text;
|
|
|
|
if ($this->tableau) {
|
|
$this->valeur[] = $valeur;
|
|
}
|
|
else {
|
|
$this->valeur = $valeur;
|
|
}
|
|
}
|
|
|
|
public function substitution($texte) {
|
|
|
|
// Pour un champ de type captcha
|
|
if ($this->type == self::TYPE_CAPTCHA) {
|
|
|
|
// Les URLs du captcha, sous forme de substitution simple.
|
|
$url = sprintf(self::CAPTCHA_URL_FORMAT, Variable::lire('urlsite'), $this->formvar);
|
|
|
|
$texte = str_replace($this->var.'_URL_IMAGE', $url, $texte);
|
|
$texte = str_replace($this->var.'_URL_CHANGER_IMAGE', $url.'&id='.rand(1,1000), $texte);
|
|
}
|
|
|
|
// Forme #varname[texte...]
|
|
$texte = preg_replace("/\\".$this->var."\[([^]]*)\]/", $this->erreur ? "\\1" : '', $texte);
|
|
|
|
if ($this->tableau) {
|
|
// Forme #varname(@), pour retrouver le nombre d'élements dans une variable de type tableau
|
|
$texte = str_replace("$this->var(@)", is_array($this->valeur) ? count($this->valeur) : '0', $texte);
|
|
|
|
// Forme #varname(index), pour les variables de type tableau)
|
|
if (preg_match_all("/\\$this->var\(([0-9]+)\)/", $texte, $matches, PREG_SET_ORDER)) {
|
|
foreach($matches as $match) {
|
|
$idx = intval($match[1]) - 1;
|
|
|
|
$val = isset($this->valeur[$idx]) ? $this->valeur[$idx] : '';
|
|
|
|
$texte = str_replace($match[0], $val, $texte);
|
|
}
|
|
}
|
|
|
|
// Supprimer tous les #varname(n) qui n'ont pas été évalués précédemment.
|
|
$texte = preg_replace("/$this->var\([0-9]+\)/", '', $texte, -1, $count);
|
|
}
|
|
else {
|
|
// Forme #varname, pour par exemple #FILTRE_egalite(#varname||1||...)
|
|
$texte = str_replace($this->var, $this->valeur, $texte);
|
|
}
|
|
|
|
return $texte;
|
|
}
|
|
|
|
}
|
|
|
|
?>
|