[PLUGINS] +set de base
[lhc/web/www.git] / www / plugins / verifier / inc / verifier.php
1 <?php
2
3 // Sécurité
4 if (!defined("_ECRIRE_INC_VERSION")) return;
5
6 /**
7 * Fonction de base de l'API de vérification.
8 * @param mixed $valeur La valeur a verifier.
9 * @param string $type Le type de verification a appliquer.
10 * @param array $options Un eventuel tableau d'options suivant le type.
11 * @param array $valeur_normalisee
12 * Si des options de verification modifient la valeur entrante (normalisent),
13 * alors la valeur modifie sera stockee dans cette variable.
14 * @return string Retourne une chaine vide si c'est valide, sinon une chaine expliquant l'erreur.
15 */
16 function inc_verifier_dist($valeur, $type, $options=null, &$valeur_normalisee=null){
17
18 // On vérifie que les options sont bien un tableau
19 if (!is_array($options))
20 $options = array();
21
22 // Si la valeur est vide, il n'y a rien a verifier donc c'est bon
23 if (is_null($valeur) or (is_string($valeur) and $valeur == '')) return '';
24 // Si c'est une date avec horaire c'est un tableau
25 if (is_array($valeur) and isset($valeur['date']) and $valeur[date] == '') return '';
26
27 // On cherche si une fonction correspondant au type existe
28 if ($verifier = charger_fonction($type, 'verifier',true)){
29 $erreur = $verifier($valeur, $options, $valeur_normalisee);
30 }
31
32 // On passe le tout dans le pipeline du meme nom
33 $erreur = pipeline(
34 'verifier',
35 array(
36 'args' => array(
37 'valeur' => $valeur,
38 'type' => $type,
39 'options' => $options,
40 ),
41 'data' => $erreur
42 )
43 );
44
45 return $erreur;
46 }
47
48 /**
49 * Liste toutes les vérifications possibles
50 *
51 * @param string $repertoire
52 * Dans quel repertoire chercher les yaml.
53 *
54 * @return Retourne un tableau listant les vérifications et leurs options
55 */
56 function verifier_lister_disponibles($repertoire='verifier'){
57 static $verifications = array();
58
59 if (!isset($verifications[$repertoire])) {
60 $verifications[$repertoire] = array();
61 $liste = find_all_in_path("$repertoire/", '.+[.]yaml$');
62
63 if (count($liste)){
64 foreach ($liste as $fichier=>$chemin){
65 $type = preg_replace(',[.]yaml$,i', '', $fichier);
66 $dossier = str_replace($fichier, '', $chemin);
67 // On ne garde que les vérifications qui ont bien la fonction !
68 if (charger_fonction($type, $repertoire, true)
69 and (
70 is_array($verif = verifier_charger_infos($type, $repertoire))
71 )
72 ) {
73 $verifications[$repertoire][$type] = $verif;
74 }
75 }
76 }
77 }
78
79 return $verifications[$repertoire];
80 }
81
82
83 /**
84 * Charger les informations contenues dans le yaml d'une vérification
85 *
86 * @param string $type_verif
87 * Le type de la vérification
88 *
89 * @param string $repertoire
90 * Dans quel repertoire chercher les yaml.
91 *
92 * @return array Un tableau contenant le YAML décodé
93 */
94 function verifier_charger_infos($type_verif, $repertoire='verifier'){
95 include_spip('inc/yaml');
96 $fichier = find_in_path("$repertoire/$type_verif.yaml");
97 $verif = yaml_decode_file($fichier);
98 if (is_array($verif)){
99 $verif['titre'] = (isset($verif['titre']) and $verif['titre']) ? _T_ou_typo($verif['titre']) : $type_verif;
100 $verif['description'] = (isset($verif['description']) and $verif['description']) ? _T_ou_typo($verif['description']) : '';
101 $verif['icone'] = (isset($verif['icone']) and $verif['icone']) ? _T_ou_typo($verif['icone']) : '';
102 }
103 return $verif;
104 }
105
106 ?>