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